diff mbox series

[v5,4/5] Add fchmodat2_01 test

Message ID 20240802-fchmodat2-v5-4-bff2ec1a4f06@suse.com
State Accepted
Headers show
Series Add fchmodat2 testing suite | expand

Commit Message

Andrea Cervesato Aug. 2, 2024, 8:23 a.m. UTC
From: Andrea Cervesato <andrea.cervesato@suse.com>

This test verifies that fchmodat2() syscall is properly working with
AT_SYMLINK_NOFOLLOW on regular files. When AT_SYMLINK_NOFOLLOW is used
on symbolic links instead, we check for EOPNOTSUPP, since the feature is
not implemented for VFS.

Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
---
 runtest/syscalls                                   |   2 +
 testcases/kernel/syscalls/fchmodat2/.gitignore     |   1 +
 testcases/kernel/syscalls/fchmodat2/Makefile       |   7 ++
 testcases/kernel/syscalls/fchmodat2/fchmodat2_01.c | 123 +++++++++++++++++++++
 4 files changed, 133 insertions(+)

Comments

Petr Vorel Aug. 2, 2024, 8:47 a.m. UTC | #1
> From: Andrea Cervesato <andrea.cervesato@suse.com>

> This test verifies that fchmodat2() syscall is properly working with
> AT_SYMLINK_NOFOLLOW on regular files. When AT_SYMLINK_NOFOLLOW is used
> on symbolic links instead, we check for EOPNOTSUPP, since the feature is
> not implemented for VFS.

> Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
> ---
>  runtest/syscalls                                   |   2 +
>  testcases/kernel/syscalls/fchmodat2/.gitignore     |   1 +
>  testcases/kernel/syscalls/fchmodat2/Makefile       |   7 ++
>  testcases/kernel/syscalls/fchmodat2/fchmodat2_01.c | 123 +++++++++++++++++++++
>  4 files changed, 133 insertions(+)

> diff --git a/runtest/syscalls b/runtest/syscalls
> index 9b3cba667..50298d01e 100644
> --- a/runtest/syscalls
> +++ b/runtest/syscalls
> @@ -270,6 +270,8 @@ fchmod06 fchmod06
>  fchmodat01 fchmodat01
>  fchmodat02 fchmodat02

> +fchmodat2_01 fchmodat2_01
> +
>  fchown01 fchown01
>  fchown01_16 fchown01_16
>  fchown02 fchown02
> diff --git a/testcases/kernel/syscalls/fchmodat2/.gitignore b/testcases/kernel/syscalls/fchmodat2/.gitignore
> new file mode 100644
> index 000000000..47d5e2427
> --- /dev/null
> +++ b/testcases/kernel/syscalls/fchmodat2/.gitignore
> @@ -0,0 +1 @@
> +fchmodat2_01
> diff --git a/testcases/kernel/syscalls/fchmodat2/Makefile b/testcases/kernel/syscalls/fchmodat2/Makefile
> new file mode 100644
> index 000000000..8cf1b9024
> --- /dev/null
> +++ b/testcases/kernel/syscalls/fchmodat2/Makefile
> @@ -0,0 +1,7 @@
> +# SPDX-License-Identifier: GPL-2.0-or-later
> +# Copyright (C) 2024 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
> +
> +top_srcdir		?= ../../../..
> +
> +include $(top_srcdir)/include/mk/testcases.mk
> +include $(top_srcdir)/include/mk/generic_leaf_target.mk
> diff --git a/testcases/kernel/syscalls/fchmodat2/fchmodat2_01.c b/testcases/kernel/syscalls/fchmodat2/fchmodat2_01.c
> new file mode 100644
> index 000000000..d12c4a8fd
> --- /dev/null
> +++ b/testcases/kernel/syscalls/fchmodat2/fchmodat2_01.c
> @@ -0,0 +1,123 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * Copyright (C) 2024 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
> + */
> +
> +/*\
> + * [Description]
> + *
> + * This test verifies that fchmodat2() syscall is properly working with
> + * regular files, symbolic links and directories. AT_SYMLINK_NOFOLLOW is a
> + * special feature that is not enabled in VFS for symbolic links, so we only
> + * verify that EOPNOTSUPP is correctly raised when used on those particular
> + * files.

If you don't mind, I'll add here explicit note that change done in 5d1f903f75a8.

> + */
> +
> +#include "tst_test.h"
> +#include "tst_safe_file_at.h"
> +#include "lapi/fcntl.h"
> +#include "lapi/stat.h"
> +
> +#define MNTPOINT "mntpoint"
> +#define FNAME "myfile"
> +#define SNAME "symlink"
> +#define DNAME "mydir"
> +#define DNAME_PATH MNTPOINT"/"DNAME
> +
> +static int fd_dir = -1;
> +
> +static void verify_mode(int dirfd, const char *path, mode_t mode)
> +{
> +	struct stat st;
> +
> +	SAFE_FSTATAT(dirfd, path, &st, AT_SYMLINK_NOFOLLOW);
> +	TST_EXP_EQ_LI(st.st_mode, mode);
> +}
> +
> +static void test_regular_file(void)
> +{
> +	tst_res(TINFO, "Using regular files");
> +
> +	SAFE_CHMOD(MNTPOINT"/"FNAME, 0640);
> +
> +	SAFE_FCHMODAT2(fd_dir, FNAME, 0700, 0);
> +	verify_mode(fd_dir, FNAME, S_IFREG | 0700);
> +
> +	SAFE_FCHMODAT2(fd_dir, FNAME, 0700, AT_SYMLINK_NOFOLLOW);
> +	verify_mode(fd_dir, FNAME, S_IFREG | 0700);
> +}
> +
> +static void test_symbolic_link(void)
> +{
> +	tst_res(TINFO, "Using symbolic link");
> +
> +	SAFE_FCHMODAT2(fd_dir, SNAME, 0700, 0);
> +	verify_mode(fd_dir, FNAME, S_IFREG | 0700);
> +	verify_mode(fd_dir, SNAME, S_IFLNK | 0777);
> +
> +	if (tst_kvercmp(6, 6, 0)) {
> +		TST_EXP_FAIL(tst_syscall(__NR_fchmodat2,
> +			fd_dir, SNAME, 0640, AT_SYMLINK_NOFOLLOW),
> +			EOPNOTSUPP);
> +	}
I wrote later this is wrong, because the change was backported to all active
stable/LTS trees. We should expect this functionality on all kernels
(tested on 6.6.15, 6.9.9, 6.10.1).

Also, I hesitated, whether we should check AT_SYMLINK_NOFOLLOW also in
safe_fchmodat2(), but we can probably ignore that.

I can change it before merge.

> +}
> +
> +static void test_empty_folder(void)
> +{
> +	tst_res(TINFO, "Using empty folder");
> +
> +	int fd;
> +
> +	SAFE_CHMOD(DNAME_PATH, 0640);
> +	fd = SAFE_OPEN(DNAME_PATH, O_PATH | O_DIRECTORY, 0640);
> +
> +	SAFE_FCHMODAT2(fd, "", 0777, AT_EMPTY_PATH);
> +	verify_mode(fd_dir, DNAME, S_IFDIR | 0777);
> +
> +	SAFE_CLOSE(fd);
> +}
> +
> +static void run(void)
> +{
> +	test_regular_file();
> +	test_empty_folder();
> +	test_symbolic_link();
> +}
> +
> +static void setup(void)
> +{
> +	fd_dir = SAFE_OPEN(MNTPOINT, O_PATH | O_DIRECTORY, 0640);
> +
> +	if (access(DNAME_PATH, F_OK) == -1)
> +		SAFE_MKDIR(DNAME_PATH, 0640);
> +
> +	SAFE_TOUCH(MNTPOINT"/"FNAME, 0640, NULL);
> +	SAFE_SYMLINKAT(FNAME, fd_dir, SNAME);
> +}
> +
> +static void cleanup(void)
> +{
> +	SAFE_UNLINKAT(fd_dir, SNAME, 0);
> +	SAFE_RMDIR(DNAME_PATH);
> +
> +	if (fd_dir != -1)
> +		SAFE_CLOSE(fd_dir);
> +}
> +
> +static struct tst_test test = {
> +	.test_all = run,
> +	.setup = setup,
> +	.cleanup = cleanup,
> +	.needs_root = 1,
> +	.mntpoint = MNTPOINT,
> +	.format_device = 1,
> +	.all_filesystems = 1,
> +	.skip_filesystems = (const char *const []) {
> +		"fuse",
> +		NULL
> +	},
You were faster than I managed to write. IMHO skipping fuse is not needed.
IMHO it was broken due EOPNOTSUPP.

With these 2 changes:

Reviewed-by: Petr Vorel <pvorel@suse.cz>

Kind regards,
Petr

> +	.tags = (const struct tst_tag[]) {
> +		{"linux-git", "5d1f903f75a8"},
> +		{}
> +	}
> +};
diff mbox series

Patch

diff --git a/runtest/syscalls b/runtest/syscalls
index 9b3cba667..50298d01e 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -270,6 +270,8 @@  fchmod06 fchmod06
 fchmodat01 fchmodat01
 fchmodat02 fchmodat02
 
+fchmodat2_01 fchmodat2_01
+
 fchown01 fchown01
 fchown01_16 fchown01_16
 fchown02 fchown02
diff --git a/testcases/kernel/syscalls/fchmodat2/.gitignore b/testcases/kernel/syscalls/fchmodat2/.gitignore
new file mode 100644
index 000000000..47d5e2427
--- /dev/null
+++ b/testcases/kernel/syscalls/fchmodat2/.gitignore
@@ -0,0 +1 @@ 
+fchmodat2_01
diff --git a/testcases/kernel/syscalls/fchmodat2/Makefile b/testcases/kernel/syscalls/fchmodat2/Makefile
new file mode 100644
index 000000000..8cf1b9024
--- /dev/null
+++ b/testcases/kernel/syscalls/fchmodat2/Makefile
@@ -0,0 +1,7 @@ 
+# SPDX-License-Identifier: GPL-2.0-or-later
+# Copyright (C) 2024 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
+
+top_srcdir		?= ../../../..
+
+include $(top_srcdir)/include/mk/testcases.mk
+include $(top_srcdir)/include/mk/generic_leaf_target.mk
diff --git a/testcases/kernel/syscalls/fchmodat2/fchmodat2_01.c b/testcases/kernel/syscalls/fchmodat2/fchmodat2_01.c
new file mode 100644
index 000000000..d12c4a8fd
--- /dev/null
+++ b/testcases/kernel/syscalls/fchmodat2/fchmodat2_01.c
@@ -0,0 +1,123 @@ 
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2024 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
+ */
+
+/*\
+ * [Description]
+ *
+ * This test verifies that fchmodat2() syscall is properly working with
+ * regular files, symbolic links and directories. AT_SYMLINK_NOFOLLOW is a
+ * special feature that is not enabled in VFS for symbolic links, so we only
+ * verify that EOPNOTSUPP is correctly raised when used on those particular
+ * files.
+ */
+
+#include "tst_test.h"
+#include "tst_safe_file_at.h"
+#include "lapi/fcntl.h"
+#include "lapi/stat.h"
+
+#define MNTPOINT "mntpoint"
+#define FNAME "myfile"
+#define SNAME "symlink"
+#define DNAME "mydir"
+#define DNAME_PATH MNTPOINT"/"DNAME
+
+static int fd_dir = -1;
+
+static void verify_mode(int dirfd, const char *path, mode_t mode)
+{
+	struct stat st;
+
+	SAFE_FSTATAT(dirfd, path, &st, AT_SYMLINK_NOFOLLOW);
+	TST_EXP_EQ_LI(st.st_mode, mode);
+}
+
+static void test_regular_file(void)
+{
+	tst_res(TINFO, "Using regular files");
+
+	SAFE_CHMOD(MNTPOINT"/"FNAME, 0640);
+
+	SAFE_FCHMODAT2(fd_dir, FNAME, 0700, 0);
+	verify_mode(fd_dir, FNAME, S_IFREG | 0700);
+
+	SAFE_FCHMODAT2(fd_dir, FNAME, 0700, AT_SYMLINK_NOFOLLOW);
+	verify_mode(fd_dir, FNAME, S_IFREG | 0700);
+}
+
+static void test_symbolic_link(void)
+{
+	tst_res(TINFO, "Using symbolic link");
+
+	SAFE_FCHMODAT2(fd_dir, SNAME, 0700, 0);
+	verify_mode(fd_dir, FNAME, S_IFREG | 0700);
+	verify_mode(fd_dir, SNAME, S_IFLNK | 0777);
+
+	if (tst_kvercmp(6, 6, 0)) {
+		TST_EXP_FAIL(tst_syscall(__NR_fchmodat2,
+			fd_dir, SNAME, 0640, AT_SYMLINK_NOFOLLOW),
+			EOPNOTSUPP);
+	}
+}
+
+static void test_empty_folder(void)
+{
+	tst_res(TINFO, "Using empty folder");
+
+	int fd;
+
+	SAFE_CHMOD(DNAME_PATH, 0640);
+	fd = SAFE_OPEN(DNAME_PATH, O_PATH | O_DIRECTORY, 0640);
+
+	SAFE_FCHMODAT2(fd, "", 0777, AT_EMPTY_PATH);
+	verify_mode(fd_dir, DNAME, S_IFDIR | 0777);
+
+	SAFE_CLOSE(fd);
+}
+
+static void run(void)
+{
+	test_regular_file();
+	test_empty_folder();
+	test_symbolic_link();
+}
+
+static void setup(void)
+{
+	fd_dir = SAFE_OPEN(MNTPOINT, O_PATH | O_DIRECTORY, 0640);
+
+	if (access(DNAME_PATH, F_OK) == -1)
+		SAFE_MKDIR(DNAME_PATH, 0640);
+
+	SAFE_TOUCH(MNTPOINT"/"FNAME, 0640, NULL);
+	SAFE_SYMLINKAT(FNAME, fd_dir, SNAME);
+}
+
+static void cleanup(void)
+{
+	SAFE_UNLINKAT(fd_dir, SNAME, 0);
+	SAFE_RMDIR(DNAME_PATH);
+
+	if (fd_dir != -1)
+		SAFE_CLOSE(fd_dir);
+}
+
+static struct tst_test test = {
+	.test_all = run,
+	.setup = setup,
+	.cleanup = cleanup,
+	.needs_root = 1,
+	.mntpoint = MNTPOINT,
+	.format_device = 1,
+	.all_filesystems = 1,
+	.skip_filesystems = (const char *const []) {
+		"fuse",
+		NULL
+	},
+	.tags = (const struct tst_tag[]) {
+		{"linux-git", "5d1f903f75a8"},
+		{}
+	}
+};