Commit 4859c257 authored by Alexey Gladkov's avatar Alexey Gladkov Committed by Christian Brauner

selftests: Add fchmodat2 selftest

The test marks as skipped if a syscall with the AT_SYMLINK_NOFOLLOW flag
fails. This is because not all filesystems support changing the mode
bits of symlinks properly. These filesystems return an error but change
the mode bits:

newfstatat(4, "regfile", {st_mode=S_IFREG|0640, st_size=0, ...}, AT_SYMLINK_NOFOLLOW) = 0
newfstatat(4, "symlink", {st_mode=S_IFLNK|0777, st_size=7, ...}, AT_SYMLINK_NOFOLLOW) = 0
syscall_0x1c3(0x4, 0x55fa1f244396, 0x180, 0x100, 0x55fa1f24438e, 0x34) = -1 EOPNOTSUPP (Operation not supported)
newfstatat(4, "regfile", {st_mode=S_IFREG|0640, st_size=0, ...}, AT_SYMLINK_NOFOLLOW) = 0

This happens with btrfs and xfs:

 $ tools/testing/selftests/fchmodat2/fchmodat2_test
 TAP version 13
 1..1
 ok 1 # SKIP fchmodat2(symlink)
 # Totals: pass:0 fail:0 xfail:0 xpass:0 skip:1 error:0

 $ stat /tmp/ksft-fchmodat2.*/symlink
   File: /tmp/ksft-fchmodat2.3NCqlE/symlink -> regfile
   Size: 7               Blocks: 0          IO Block: 4096   symbolic link
 Device: 7,0     Inode: 133         Links: 1
 Access: (0600/lrw-------)  Uid: (    0/    root)   Gid: (    0/    root)
Signed-off-by: default avatarAlexey Gladkov <legion@kernel.org>
Message-Id: <4532a04a870ff589ba62ceeacf76f0bd81b9ba01.1689092120.git.legion@kernel.org>
Signed-off-by: default avatarChristian Brauner <brauner@kernel.org>
parent 78252deb
......@@ -18,6 +18,7 @@ TARGETS += drivers/net/bonding
TARGETS += drivers/net/team
TARGETS += efivarfs
TARGETS += exec
TARGETS += fchmodat2
TARGETS += filesystems
TARGETS += filesystems/binderfs
TARGETS += filesystems/epoll
......
# SPDX-License-Identifier: GPL-2.0-only
/*_test
# SPDX-License-Identifier: GPL-2.0-or-later
CFLAGS += -Wall -O2 -g -fsanitize=address -fsanitize=undefined
TEST_GEN_PROGS := fchmodat2_test
include ../lib.mk
// SPDX-License-Identifier: GPL-2.0-or-later
#define _GNU_SOURCE
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <syscall.h>
#include <unistd.h>
#include "../kselftest.h"
#ifndef __NR_fchmodat2
#if defined __alpha__
#define __NR_fchmodat2 562
#elif defined _MIPS_SIM
#if _MIPS_SIM == _MIPS_SIM_ABI32 /* o32 */
#define __NR_fchmodat2 (452 + 4000)
#endif
#if _MIPS_SIM == _MIPS_SIM_NABI32 /* n32 */
#define __NR_fchmodat2 (452 + 6000)
#endif
#if _MIPS_SIM == _MIPS_SIM_ABI64 /* n64 */
#define __NR_fchmodat2 (452 + 5000)
#endif
#elif defined __ia64__
#define __NR_fchmodat2 (452 + 1024)
#else
#define __NR_fchmodat2 452
#endif
#endif
int sys_fchmodat2(int dfd, const char *filename, mode_t mode, int flags)
{
int ret = syscall(__NR_fchmodat2, dfd, filename, mode, flags);
return ret >= 0 ? ret : -errno;
}
int setup_testdir(void)
{
int dfd, ret;
char dirname[] = "/tmp/ksft-fchmodat2.XXXXXX";
/* Make the top-level directory. */
if (!mkdtemp(dirname))
ksft_exit_fail_msg("%s: failed to create tmpdir\n", __func__);
dfd = open(dirname, O_PATH | O_DIRECTORY);
if (dfd < 0)
ksft_exit_fail_msg("%s: failed to open tmpdir\n", __func__);
ret = openat(dfd, "regfile", O_CREAT | O_WRONLY | O_TRUNC, 0644);
if (ret < 0)
ksft_exit_fail_msg("%s: failed to create file in tmpdir\n",
__func__);
close(ret);
ret = symlinkat("regfile", dfd, "symlink");
if (ret < 0)
ksft_exit_fail_msg("%s: failed to create symlink in tmpdir\n",
__func__);
return dfd;
}
int expect_mode(int dfd, const char *filename, mode_t expect_mode)
{
struct stat st;
int ret = fstatat(dfd, filename, &st, AT_SYMLINK_NOFOLLOW);
if (ret)
ksft_exit_fail_msg("%s: %s: fstatat failed\n",
__func__, filename);
return (st.st_mode == expect_mode);
}
void test_regfile(void)
{
int dfd, ret;
dfd = setup_testdir();
ret = sys_fchmodat2(dfd, "regfile", 0640, 0);
if (ret < 0)
ksft_exit_fail_msg("%s: fchmodat2(noflag) failed\n", __func__);
if (!expect_mode(dfd, "regfile", 0100640))
ksft_exit_fail_msg("%s: wrong file mode bits after fchmodat2\n",
__func__);
ret = sys_fchmodat2(dfd, "regfile", 0600, AT_SYMLINK_NOFOLLOW);
if (ret < 0)
ksft_exit_fail_msg("%s: fchmodat2(AT_SYMLINK_NOFOLLOW) failed\n",
__func__);
if (!expect_mode(dfd, "regfile", 0100600))
ksft_exit_fail_msg("%s: wrong file mode bits after fchmodat2 with nofollow\n",
__func__);
ksft_test_result_pass("fchmodat2(regfile)\n");
}
void test_symlink(void)
{
int dfd, ret;
dfd = setup_testdir();
ret = sys_fchmodat2(dfd, "symlink", 0640, 0);
if (ret < 0)
ksft_exit_fail_msg("%s: fchmodat2(noflag) failed\n", __func__);
if (!expect_mode(dfd, "regfile", 0100640))
ksft_exit_fail_msg("%s: wrong file mode bits after fchmodat2\n",
__func__);
if (!expect_mode(dfd, "symlink", 0120777))
ksft_exit_fail_msg("%s: wrong symlink mode bits after fchmodat2\n",
__func__);
ret = sys_fchmodat2(dfd, "symlink", 0600, AT_SYMLINK_NOFOLLOW);
/*
* On certain filesystems (xfs or btrfs), chmod operation fails. So we
* first check the symlink target but if the operation fails we mark the
* test as skipped.
*
* https://sourceware.org/legacy-ml/libc-alpha/2020-02/msg00467.html
*/
if (ret == 0 && !expect_mode(dfd, "symlink", 0120600))
ksft_exit_fail_msg("%s: wrong symlink mode bits after fchmodat2 with nofollow\n",
__func__);
if (!expect_mode(dfd, "regfile", 0100640))
ksft_exit_fail_msg("%s: wrong file mode bits after fchmodat2 with nofollow\n",
__func__);
if (ret != 0)
ksft_test_result_skip("fchmodat2(symlink)\n");
else
ksft_test_result_pass("fchmodat2(symlink)\n");
}
#define NUM_TESTS 2
int main(int argc, char **argv)
{
ksft_print_header();
ksft_set_plan(NUM_TESTS);
test_regfile();
test_symlink();
if (ksft_get_fail_cnt() + ksft_get_error_cnt() > 0)
ksft_exit_fail();
else
ksft_exit_pass();
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment