linux-parisc.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Alexey Gladkov <legion@kernel.org>
To: LKML <linux-kernel@vger.kernel.org>,
	Arnd Bergmann <arnd@arndb.de>,
	linux-api@vger.kernel.org, linux-fsdevel@vger.kernel.org,
	viro@zeniv.linux.org.uk
Cc: James.Bottomley@HansenPartnership.com, acme@kernel.org,
	alexander.shishkin@linux.intel.com, axboe@kernel.dk,
	benh@kernel.crashing.org, borntraeger@de.ibm.com, bp@alien8.de,
	catalin.marinas@arm.com, christian@brauner.io, dalias@libc.org,
	davem@davemloft.net, deepa.kernel@gmail.com, deller@gmx.de,
	dhowells@redhat.com, fenghua.yu@intel.com, fweimer@redhat.com,
	geert@linux-m68k.org, glebfm@altlinux.org, gor@linux.ibm.com,
	hare@suse.com, hpa@zytor.com, ink@jurassic.park.msu.ru,
	jhogan@kernel.org, kim.phillips@arm.com, ldv@altlinux.org,
	linux-alpha@vger.kernel.org, linux-arch@vger.kernel.org,
	linux-ia64@vger.kernel.org, linux-m68k@lists.linux-m68k.org,
	linux-mips@vger.kernel.org, linux-parisc@vger.kernel.org,
	linux-s390@vger.kernel.org, linux-sh@vger.kernel.org,
	linux@armlinux.org.uk, linuxppc-dev@lists.ozlabs.org,
	luto@kernel.org, mattst88@gmail.com, mingo@redhat.com,
	monstr@monstr.eu, mpe@ellerman.id.au, namhyung@kernel.org,
	paulus@samba.org, peterz@infradead.org, ralf@linux-mips.org,
	sparclinux@vger.kernel.org, stefan@agner.ch, tglx@linutronix.de,
	tony.luck@intel.com, tycho@tycho.ws, will@kernel.org,
	x86@kernel.org, ysato@users.sourceforge.jp
Subject: [PATCH v4 5/5] selftests: Add fchmodat2 selftest
Date: Tue, 11 Jul 2023 18:16:07 +0200	[thread overview]
Message-ID: <4532a04a870ff589ba62ceeacf76f0bd81b9ba01.1689092120.git.legion@kernel.org> (raw)
In-Reply-To: <cover.1689092120.git.legion@kernel.org>

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: Alexey Gladkov <legion@kernel.org>
---
 tools/testing/selftests/Makefile              |   1 +
 tools/testing/selftests/fchmodat2/.gitignore  |   2 +
 tools/testing/selftests/fchmodat2/Makefile    |   6 +
 .../selftests/fchmodat2/fchmodat2_test.c      | 162 ++++++++++++++++++
 4 files changed, 171 insertions(+)
 create mode 100644 tools/testing/selftests/fchmodat2/.gitignore
 create mode 100644 tools/testing/selftests/fchmodat2/Makefile
 create mode 100644 tools/testing/selftests/fchmodat2/fchmodat2_test.c

diff --git a/tools/testing/selftests/Makefile b/tools/testing/selftests/Makefile
index 666b56f22a41..8dca8acdb671 100644
--- a/tools/testing/selftests/Makefile
+++ b/tools/testing/selftests/Makefile
@@ -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
diff --git a/tools/testing/selftests/fchmodat2/.gitignore b/tools/testing/selftests/fchmodat2/.gitignore
new file mode 100644
index 000000000000..82a4846cbc4b
--- /dev/null
+++ b/tools/testing/selftests/fchmodat2/.gitignore
@@ -0,0 +1,2 @@
+# SPDX-License-Identifier: GPL-2.0-only
+/*_test
diff --git a/tools/testing/selftests/fchmodat2/Makefile b/tools/testing/selftests/fchmodat2/Makefile
new file mode 100644
index 000000000000..45b519eab851
--- /dev/null
+++ b/tools/testing/selftests/fchmodat2/Makefile
@@ -0,0 +1,6 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+CFLAGS += -Wall -O2 -g -fsanitize=address -fsanitize=undefined
+TEST_GEN_PROGS := fchmodat2_test
+
+include ../lib.mk
diff --git a/tools/testing/selftests/fchmodat2/fchmodat2_test.c b/tools/testing/selftests/fchmodat2/fchmodat2_test.c
new file mode 100644
index 000000000000..2d98eb215bc6
--- /dev/null
+++ b/tools/testing/selftests/fchmodat2/fchmodat2_test.c
@@ -0,0 +1,162 @@
+// 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();
+}
-- 
2.33.8


  parent reply	other threads:[~2023-07-11 16:19 UTC|newest]

Thread overview: 69+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-07-17  1:27 Add a new fchmodat4() syscall, v2 Palmer Dabbelt
2019-07-17  1:27 ` [PATCH v2 1/4] Non-functional cleanup of a "__user * filename" Palmer Dabbelt
2019-07-17  1:27 ` [PATCH v2 2/4] Add fchmodat4(), a new syscall Palmer Dabbelt
2019-07-17  1:48   ` Al Viro
2019-07-17  2:12     ` Palmer Dabbelt
2019-07-17  2:40   ` Rich Felker
2019-07-17  3:02     ` Al Viro
2019-07-17  1:27 ` [PATCH v2 3/4] arch: Register fchmodat4, usually as syscall 434 Palmer Dabbelt
2019-07-17  1:27 ` [PATCH v2 4/4] tools: Add fchmodat4 Palmer Dabbelt
2019-07-17 12:39   ` Arnaldo Carvalho de Melo
2020-06-09 13:52 ` Add a new fchmodat4() syscall, v2 Florian Weimer
2023-07-11 11:25   ` [PATCH v3 0/5] Add a new fchmodat4() syscall Alexey Gladkov
2023-07-11 11:25     ` [PATCH v3 1/5] Non-functional cleanup of a "__user * filename" Alexey Gladkov
2023-07-11 11:32       ` Arnd Bergmann
2023-07-11 11:25     ` [PATCH v3 2/5] fs: Add fchmodat4() Alexey Gladkov
2023-07-11 11:42       ` Arnd Bergmann
2023-07-11 11:52         ` Christian Brauner
2023-07-11 12:51           ` Alexey Gladkov
2023-07-11 14:01             ` Christian Brauner
2023-07-11 15:23               ` Alexey Gladkov
2023-07-11 12:28       ` Matthew Wilcox
2023-07-11 12:49         ` Alexey Gladkov
2023-07-11 11:25     ` [PATCH v3 3/5] arch: Register fchmodat4, usually as syscall 451 Alexey Gladkov
2023-07-11 11:31       ` Arnd Bergmann
2023-07-11 11:25     ` [PATCH v3 4/5] tools headers UAPI: Sync files changed by new fchmodat4 syscall Alexey Gladkov
2023-07-11 11:25     ` [PATCH v3 5/5] selftests: add fchmodat4(2) selftest Alexey Gladkov
2023-07-11 12:10       ` Florian Weimer
2023-07-11 13:38         ` Alexey Gladkov
2023-07-11 12:24     ` [PATCH v3 0/5] Add a new fchmodat4() syscall Florian Weimer
2023-07-11 15:14       ` Christian Brauner
2023-07-25 11:05         ` Alexey Gladkov
2023-07-25 12:05           ` Christian Brauner
2023-07-11 16:16     ` [PATCH v4 0/5] Add a new fchmodat2() syscall Alexey Gladkov
2023-07-11 16:16       ` [PATCH v4 1/5] Non-functional cleanup of a "__user * filename" Alexey Gladkov
2023-07-11 16:16       ` [PATCH v4 2/5] fs: Add fchmodat2() Alexey Gladkov
2023-07-11 17:05         ` Christian Brauner
2023-07-25 16:36         ` Aleksa Sarai
2023-07-26 13:45           ` Alexey Gladkov
2023-07-27 10:26             ` Christian Brauner
2023-07-27 17:12             ` Aleksa Sarai
2023-07-27 17:39               ` Aleksa Sarai
2023-07-28  8:43                 ` David Laight
2023-07-28 18:42                   ` dalias
2023-07-27  9:01           ` David Laight
2023-07-27 16:28             ` Andreas Schwab
2023-07-27 17:02               ` Christian Brauner
2023-07-27 17:13                 ` dalias
2023-07-27 17:36                   ` Christian Brauner
2023-07-27 16:31             ` dalias
2023-07-11 16:16       ` [PATCH v4 3/5] arch: Register fchmodat2, usually as syscall 452 Alexey Gladkov
2023-07-11 16:26         ` Arnd Bergmann
2023-07-25  7:16         ` Geert Uytterhoeven
2023-07-25 16:43         ` Aleksa Sarai
2023-07-27 10:37           ` Christian Brauner
2023-07-27 17:42             ` Aleksa Sarai
2023-07-11 16:16       ` [PATCH v4 4/5] tools headers UAPI: Sync files changed by new fchmodat2 syscall Alexey Gladkov
2023-07-11 17:19         ` Namhyung Kim
2023-07-11 17:23           ` Alexey Gladkov
2023-07-11 16:16       ` Alexey Gladkov [this message]
2023-07-11 17:36       ` (subset) [PATCH v4 0/5] Add a new fchmodat2() syscall Christian Brauner
2023-07-12  2:42       ` Rich Felker
2023-07-25 15:58     ` Add fchmodat2() - or add a more general syscall? David Howells
2023-07-25 16:10       ` Florian Weimer
2023-07-25 16:50       ` Aleksa Sarai
2023-07-25 18:39       ` David Howells
2023-07-25 18:44         ` Rich Felker
2023-07-26 13:30         ` Christian Brauner
2023-07-27  3:57       ` Eric Biggers
2023-07-27 10:27         ` Christian Brauner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4532a04a870ff589ba62ceeacf76f0bd81b9ba01.1689092120.git.legion@kernel.org \
    --to=legion@kernel.org \
    --cc=James.Bottomley@HansenPartnership.com \
    --cc=acme@kernel.org \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=arnd@arndb.de \
    --cc=axboe@kernel.dk \
    --cc=benh@kernel.crashing.org \
    --cc=borntraeger@de.ibm.com \
    --cc=bp@alien8.de \
    --cc=catalin.marinas@arm.com \
    --cc=christian@brauner.io \
    --cc=dalias@libc.org \
    --cc=davem@davemloft.net \
    --cc=deepa.kernel@gmail.com \
    --cc=deller@gmx.de \
    --cc=dhowells@redhat.com \
    --cc=fenghua.yu@intel.com \
    --cc=fweimer@redhat.com \
    --cc=geert@linux-m68k.org \
    --cc=glebfm@altlinux.org \
    --cc=gor@linux.ibm.com \
    --cc=hare@suse.com \
    --cc=hpa@zytor.com \
    --cc=ink@jurassic.park.msu.ru \
    --cc=jhogan@kernel.org \
    --cc=kim.phillips@arm.com \
    --cc=ldv@altlinux.org \
    --cc=linux-alpha@vger.kernel.org \
    --cc=linux-api@vger.kernel.org \
    --cc=linux-arch@vger.kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-ia64@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-m68k@lists.linux-m68k.org \
    --cc=linux-mips@vger.kernel.org \
    --cc=linux-parisc@vger.kernel.org \
    --cc=linux-s390@vger.kernel.org \
    --cc=linux-sh@vger.kernel.org \
    --cc=linux@armlinux.org.uk \
    --cc=linuxppc-dev@lists.ozlabs.org \
    --cc=luto@kernel.org \
    --cc=mattst88@gmail.com \
    --cc=mingo@redhat.com \
    --cc=monstr@monstr.eu \
    --cc=mpe@ellerman.id.au \
    --cc=namhyung@kernel.org \
    --cc=paulus@samba.org \
    --cc=peterz@infradead.org \
    --cc=ralf@linux-mips.org \
    --cc=sparclinux@vger.kernel.org \
    --cc=stefan@agner.ch \
    --cc=tglx@linutronix.de \
    --cc=tony.luck@intel.com \
    --cc=tycho@tycho.ws \
    --cc=viro@zeniv.linux.org.uk \
    --cc=will@kernel.org \
    --cc=x86@kernel.org \
    --cc=ysato@users.sourceforge.jp \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).