From nobody Tue Dec 16 04:06:21 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id EF09AC678DB for ; Sat, 4 Mar 2023 14:29:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229698AbjCDO3T (ORCPT ); Sat, 4 Mar 2023 09:29:19 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40344 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229666AbjCDO3M (ORCPT ); Sat, 4 Mar 2023 09:29:12 -0500 Received: from 1wt.eu (wtarreau.pck.nerim.net [62.212.114.60]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id A5467975A for ; Sat, 4 Mar 2023 06:29:09 -0800 (PST) Received: (from willy@localhost) by pcw.home.local (8.15.2/8.15.2/Submit) id 324ESto3006570; Sat, 4 Mar 2023 15:28:55 +0100 From: Willy Tarreau To: paulmck@kernel.org Cc: chenhuacai@loongson.cn, chenfeiyang@loongson.cn, linux-kernel@vger.kernel.org, Willy Tarreau Subject: [PATCH 1/5] tools/nolibc: add getuid() and geteuid() Date: Sat, 4 Mar 2023 15:28:40 +0100 Message-Id: <20230304142844.6522-2-w@1wt.eu> X-Mailer: git-send-email 2.17.5 In-Reply-To: <20230304142844.6522-1-w@1wt.eu> References: <20230304142844.6522-1-w@1wt.eu> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" This can be useful to avoid attempting some privileged operations, starting from the nolibc-test tool that gets two failures when not privileged. We call getuid32() and geteuid32() when they are defined, and fall back to getuid() and geteuid() otherwise. Signed-off-by: Willy Tarreau --- tools/include/nolibc/sys.h | 42 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/tools/include/nolibc/sys.h b/tools/include/nolibc/sys.h index b5f8cd35c03b..115579e7f1db 100644 --- a/tools/include/nolibc/sys.h +++ b/tools/include/nolibc/sys.h @@ -410,6 +410,27 @@ int getdents64(int fd, struct linux_dirent64 *dirp, in= t count) } =20 =20 +/* + * uid_t geteuid(void); + */ + +static __attribute__((unused)) +uid_t sys_geteuid(void) +{ +#ifdef __NR_geteuid32 + return my_syscall0(__NR_geteuid32); +#else + return my_syscall0(__NR_geteuid); +#endif +} + +static __attribute__((unused)) +uid_t geteuid(void) +{ + return sys_geteuid(); +} + + /* * pid_t getpgid(pid_t pid); */ @@ -544,6 +565,27 @@ int gettimeofday(struct timeval *tv, struct timezone *= tz) } =20 =20 +/* + * uid_t getuid(void); + */ + +static __attribute__((unused)) +uid_t sys_getuid(void) +{ +#ifdef __NR_getuid32 + return my_syscall0(__NR_getuid32); +#else + return my_syscall0(__NR_getuid); +#endif +} + +static __attribute__((unused)) +uid_t getuid(void) +{ + return sys_getuid(); +} + + /* * int ioctl(int fd, unsigned long req, void *value); */ --=20 2.17.5 From nobody Tue Dec 16 04:06:21 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B49EAC64EC4 for ; Sat, 4 Mar 2023 14:29:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229568AbjCDO3G (ORCPT ); Sat, 4 Mar 2023 09:29:06 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40064 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229453AbjCDO3E (ORCPT ); Sat, 4 Mar 2023 09:29:04 -0500 Received: from 1wt.eu (wtarreau.pck.nerim.net [62.212.114.60]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 141BC3583 for ; Sat, 4 Mar 2023 06:29:02 -0800 (PST) Received: (from willy@localhost) by pcw.home.local (8.15.2/8.15.2/Submit) id 324ESuw0006571; Sat, 4 Mar 2023 15:28:56 +0100 From: Willy Tarreau To: paulmck@kernel.org Cc: chenhuacai@loongson.cn, chenfeiyang@loongson.cn, linux-kernel@vger.kernel.org, Willy Tarreau Subject: [PATCH 2/5] selftests/nolibc: skip the chroot_root and link_dir tests when not privileged Date: Sat, 4 Mar 2023 15:28:41 +0100 Message-Id: <20230304142844.6522-3-w@1wt.eu> X-Mailer: git-send-email 2.17.5 In-Reply-To: <20230304142844.6522-1-w@1wt.eu> References: <20230304142844.6522-1-w@1wt.eu> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" These two tests always fail when the program is started natively as an unprivileged user, and require the user to carefully check the output of "make run-user" and ignore them. Let's add an euid check and condition these two tests to euid=3D=3D0. Now the test case stops needlessly reporting failures. E.g.: $ make -C tools/testing/selftests/nolibc run-user ... CC nolibc-test 123 test(s) passed. Signed-off-by: Willy Tarreau --- tools/testing/selftests/nolibc/nolibc-test.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/s= elftests/nolibc/nolibc-test.c index 772f88bda0f1..6a7c13f0cd61 100644 --- a/tools/testing/selftests/nolibc/nolibc-test.c +++ b/tools/testing/selftests/nolibc/nolibc-test.c @@ -477,6 +477,7 @@ static int test_getpagesize(void) int run_syscall(int min, int max) { struct stat stat_buf; + int euid0; int proc; int test; int tmp; @@ -486,6 +487,9 @@ int run_syscall(int min, int max) /* indicates whether or not /proc is mounted */ proc =3D stat("/proc", &stat_buf) =3D=3D 0; =20 + /* this will be used to skip certain tests that can't be run unprivileged= */ + euid0 =3D geteuid() =3D=3D 0; + for (test =3D min; test >=3D 0 && test <=3D max; test++) { int llen =3D 0; // line length =20 @@ -511,7 +515,7 @@ int run_syscall(int min, int max) CASE_TEST(chmod_net); EXPECT_SYSZR(proc, chmod("/proc/self/net",= 0555)); break; CASE_TEST(chmod_self); EXPECT_SYSER(proc, chmod("/proc/self", 055= 5), -1, EPERM); break; CASE_TEST(chown_self); EXPECT_SYSER(proc, chown("/proc/self", 0, = 0), -1, EPERM); break; - CASE_TEST(chroot_root); EXPECT_SYSZR(1, chroot("/")); break; + CASE_TEST(chroot_root); EXPECT_SYSZR(euid0, chroot("/")); break; CASE_TEST(chroot_blah); EXPECT_SYSER(1, chroot("/proc/self/blah"),= -1, ENOENT); break; CASE_TEST(chroot_exe); EXPECT_SYSER(proc, chroot("/proc/self/exe"= ), -1, ENOTDIR); break; CASE_TEST(close_m1); EXPECT_SYSER(1, close(-1), -1, EBADF); bre= ak; @@ -536,7 +540,7 @@ int run_syscall(int min, int max) CASE_TEST(ioctl_tiocinq); EXPECT_SYSZR(1, ioctl(0, TIOCINQ, &tmp)); = break; CASE_TEST(link_root1); EXPECT_SYSER(1, link("/", "/"), -1, EEXIST= ); break; CASE_TEST(link_blah); EXPECT_SYSER(1, link("/proc/self/blah", "/= blah"), -1, ENOENT); break; - CASE_TEST(link_dir); EXPECT_SYSER(1, link("/", "/blah"), -1, EP= ERM); break; + CASE_TEST(link_dir); EXPECT_SYSER(euid0, link("/", "/blah"), -1= , EPERM); break; CASE_TEST(link_cross); EXPECT_SYSER(proc, link("/proc/self/net", = "/blah"), -1, EXDEV); break; CASE_TEST(lseek_m1); EXPECT_SYSER(1, lseek(-1, 0, SEEK_SET), -1= , EBADF); break; CASE_TEST(lseek_0); EXPECT_SYSER(1, lseek(0, 0, SEEK_SET), -1,= ESPIPE); break; --=20 2.17.5 From nobody Tue Dec 16 04:06:21 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2AAD5C64EC4 for ; Sat, 4 Mar 2023 14:29:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229509AbjCDO3M (ORCPT ); Sat, 4 Mar 2023 09:29:12 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40084 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229518AbjCDO3F (ORCPT ); Sat, 4 Mar 2023 09:29:05 -0500 Received: from 1wt.eu (wtarreau.pck.nerim.net [62.212.114.60]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id D6BFC4ED6 for ; Sat, 4 Mar 2023 06:29:03 -0800 (PST) Received: (from willy@localhost) by pcw.home.local (8.15.2/8.15.2/Submit) id 324ESurj006572; Sat, 4 Mar 2023 15:28:56 +0100 From: Willy Tarreau To: paulmck@kernel.org Cc: chenhuacai@loongson.cn, chenfeiyang@loongson.cn, linux-kernel@vger.kernel.org, Willy Tarreau Subject: [PATCH 3/5] tools/nolibc: check for S_I* macros before defining them Date: Sat, 4 Mar 2023 15:28:42 +0100 Message-Id: <20230304142844.6522-4-w@1wt.eu> X-Mailer: git-send-email 2.17.5 In-Reply-To: <20230304142844.6522-1-w@1wt.eu> References: <20230304142844.6522-1-w@1wt.eu> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Defining S_I* flags in types.h can cause some build failures if linux/stat.h is included prior to it. But if not defined, some toolchains that include some glibc parts will in turn fail because linux/stat.h already takes care of avoiding these definitions when glibc is present. Let's preserve the macros here but first include linux/stat.h and check for their definition before doing so. We also define the previously missing permission macros so that we don't get a different behavior depending on the first include found. Cc: Feiyang Chen Signed-off-by: Willy Tarreau --- tools/include/nolibc/types.h | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/tools/include/nolibc/types.h b/tools/include/nolibc/types.h index fbbc0e68c001..47a0997d2d74 100644 --- a/tools/include/nolibc/types.h +++ b/tools/include/nolibc/types.h @@ -9,6 +9,7 @@ =20 #include "std.h" #include +#include =20 =20 /* Only the generic macros and types may be defined here. The arch-specific @@ -16,7 +17,11 @@ * the layout of sys_stat_struct must not be defined here. */ =20 -/* stat flags (WARNING, octal here) */ +/* stat flags (WARNING, octal here). We need to check for an existing + * definition because linux/stat.h may omit to define those if it finds + * that any glibc header was already included. + */ +#if !defined(S_IFMT) #define S_IFDIR 0040000 #define S_IFCHR 0020000 #define S_IFBLK 0060000 @@ -34,6 +39,22 @@ #define S_ISLNK(mode) (((mode) & S_IFMT) =3D=3D S_IFLNK) #define S_ISSOCK(mode) (((mode) & S_IFMT) =3D=3D S_IFSOCK) =20 +#define S_IRWXU 00700 +#define S_IRUSR 00400 +#define S_IWUSR 00200 +#define S_IXUSR 00100 + +#define S_IRWXG 00070 +#define S_IRGRP 00040 +#define S_IWGRP 00020 +#define S_IXGRP 00010 + +#define S_IRWXO 00007 +#define S_IROTH 00004 +#define S_IWOTH 00002 +#define S_IXOTH 00001 +#endif + /* dirent types */ #define DT_UNKNOWN 0x0 #define DT_FIFO 0x1 --=20 2.17.5 From nobody Tue Dec 16 04:06:21 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E5444C678DB for ; Sat, 4 Mar 2023 14:29:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229609AbjCDO3O (ORCPT ); Sat, 4 Mar 2023 09:29:14 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40086 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229534AbjCDO3F (ORCPT ); Sat, 4 Mar 2023 09:29:05 -0500 Received: from 1wt.eu (wtarreau.pck.nerim.net [62.212.114.60]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 5101293E7 for ; Sat, 4 Mar 2023 06:29:04 -0800 (PST) Received: (from willy@localhost) by pcw.home.local (8.15.2/8.15.2/Submit) id 324ESuTI006573; Sat, 4 Mar 2023 15:28:56 +0100 From: Willy Tarreau To: paulmck@kernel.org Cc: chenhuacai@loongson.cn, chenfeiyang@loongson.cn, linux-kernel@vger.kernel.org, Willy Tarreau Subject: [PATCH 4/5] tools/nolibc: Include linux/fcntl.h and remove duplicate code Date: Sat, 4 Mar 2023 15:28:43 +0100 Message-Id: <20230304142844.6522-5-w@1wt.eu> X-Mailer: git-send-email 2.17.5 In-Reply-To: <20230304142844.6522-1-w@1wt.eu> References: <20230304142844.6522-1-w@1wt.eu> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Feiyang Chen Include linux/fcntl.h for O_* and AT_*. asm/fcntl.h is included by linux/fcntl.h, so it can be safely removed. Signed-off-by: Feiyang Chen Acked-by: Huacai Chen Signed-off-by: Willy Tarreau --- tools/include/nolibc/sys.h | 2 +- tools/include/nolibc/types.h | 5 ----- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/tools/include/nolibc/sys.h b/tools/include/nolibc/sys.h index 115579e7f1db..41cad6d6137e 100644 --- a/tools/include/nolibc/sys.h +++ b/tools/include/nolibc/sys.h @@ -11,7 +11,6 @@ #include "std.h" =20 /* system includes */ -#include // for O_* #include #include // for SIGCHLD #include @@ -20,6 +19,7 @@ #include #include #include +#include // for O_* and AT_* =20 #include "arch.h" #include "errno.h" diff --git a/tools/include/nolibc/types.h b/tools/include/nolibc/types.h index 47a0997d2d74..10823e5ac44b 100644 --- a/tools/include/nolibc/types.h +++ b/tools/include/nolibc/types.h @@ -81,11 +81,6 @@ #define MAXPATHLEN (PATH_MAX) #endif =20 -/* Special FD used by all the *at functions */ -#ifndef AT_FDCWD -#define AT_FDCWD (-100) -#endif - /* whence values for lseek() */ #define SEEK_SET 0 #define SEEK_CUR 1 --=20 2.17.5 From nobody Tue Dec 16 04:06:21 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 385CBC678DB for ; Sat, 4 Mar 2023 14:29:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229686AbjCDO3X (ORCPT ); Sat, 4 Mar 2023 09:29:23 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40758 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229678AbjCDO3Q (ORCPT ); Sat, 4 Mar 2023 09:29:16 -0500 Received: from 1wt.eu (wtarreau.pck.nerim.net [62.212.114.60]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id CCE151EFFA for ; Sat, 4 Mar 2023 06:29:11 -0800 (PST) Received: (from willy@localhost) by pcw.home.local (8.15.2/8.15.2/Submit) id 324ESu48006574; Sat, 4 Mar 2023 15:28:56 +0100 From: Willy Tarreau To: paulmck@kernel.org Cc: chenhuacai@loongson.cn, chenfeiyang@loongson.cn, linux-kernel@vger.kernel.org, Willy Tarreau Subject: [PATCH 5/5] tools/nolibc: Add statx() and make stat() rely on statx() if necessary Date: Sat, 4 Mar 2023 15:28:44 +0100 Message-Id: <20230304142844.6522-6-w@1wt.eu> X-Mailer: git-send-email 2.17.5 In-Reply-To: <20230304142844.6522-1-w@1wt.eu> References: <20230304142844.6522-1-w@1wt.eu> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Feiyang Chen LoongArch and RISC-V 32-bit only have statx(). ARC, Hexagon, Nios2 and OpenRISC have statx() and stat64() but not stat() or newstat(). Add statx() and make stat() rely on statx() if necessary to make them happy. We may just use statx() for all architectures in the future. Signed-off-by: Feiyang Chen Acked-by: Huacai Chen Signed-off-by: Willy Tarreau --- tools/include/nolibc/sys.h | 56 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/tools/include/nolibc/sys.h b/tools/include/nolibc/sys.h index 41cad6d6137e..5d624dc63a42 100644 --- a/tools/include/nolibc/sys.h +++ b/tools/include/nolibc/sys.h @@ -20,6 +20,7 @@ #include #include #include // for O_* and AT_* +#include // for statx() =20 #include "arch.h" #include "errno.h" @@ -1090,12 +1091,66 @@ pid_t setsid(void) return ret; } =20 +#if defined(__NR_statx) +/* + * int statx(int fd, const char *path, int flags, unsigned int mask, struc= t statx *buf); + */ + +static __attribute__((unused)) +int sys_statx(int fd, const char *path, int flags, unsigned int mask, stru= ct statx *buf) +{ + return my_syscall5(__NR_statx, fd, path, flags, mask, buf); +} + +static __attribute__((unused)) +int statx(int fd, const char *path, int flags, unsigned int mask, struct s= tatx *buf) +{ + int ret =3D sys_statx(fd, path, flags, mask, buf); + + if (ret < 0) { + SET_ERRNO(-ret); + ret =3D -1; + } + return ret; +} +#endif =20 /* * int stat(const char *path, struct stat *buf); * Warning: the struct stat's layout is arch-dependent. */ =20 +#if defined(__NR_statx) && !defined(__NR_newfstatat) && !defined(__NR_stat) +/* + * Maybe we can just use statx() when available for all architectures? + */ +static __attribute__((unused)) +int sys_stat(const char *path, struct stat *buf) +{ + struct statx statx; + long ret; + + ret =3D sys_statx(AT_FDCWD, path, AT_NO_AUTOMOUNT, STATX_BASIC_STATS, &st= atx); + buf->st_dev =3D ((statx.stx_dev_minor & 0xff) + | (statx.stx_dev_major << 8) + | ((statx.stx_dev_minor & ~0xff) << 12)); + buf->st_ino =3D statx.stx_ino; + buf->st_mode =3D statx.stx_mode; + buf->st_nlink =3D statx.stx_nlink; + buf->st_uid =3D statx.stx_uid; + buf->st_gid =3D statx.stx_gid; + buf->st_rdev =3D ((statx.stx_rdev_minor & 0xff) + | (statx.stx_rdev_major << 8) + | ((statx.stx_rdev_minor & ~0xff) << 12)); + buf->st_size =3D statx.stx_size; + buf->st_blksize =3D statx.stx_blksize; + buf->st_blocks =3D statx.stx_blocks; + buf->st_atime =3D statx.stx_atime.tv_sec; + buf->st_mtime =3D statx.stx_mtime.tv_sec; + buf->st_ctime =3D statx.stx_ctime.tv_sec; + return ret; +} +#else static __attribute__((unused)) int sys_stat(const char *path, struct stat *buf) { @@ -1125,6 +1180,7 @@ int sys_stat(const char *path, struct stat *buf) buf->st_ctime =3D stat.st_ctime; return ret; } +#endif =20 static __attribute__((unused)) int stat(const char *path, struct stat *buf) --=20 2.17.5