Skip to contentSkip to conversation

open16: allow restricted O_CREAT of FIFOs and regular files

PATCH Superseded Wei Gao via ltp 2 participants 15 messages Jul 22 – Aug 28 Library code

v12 of 17 Superseded Latest is v17 →

lore
Share & tools
lore
#0 lore raw

v12 cover open16: allow restricted O_CREAT of FIFOs and regular files

v11->v12:
- Restructured the series into three clean commits

Wei Gao (3):
lib/tst_uid: Remove spurious TERRNO from tst_get_free_gid success path
lib: New library function tst_get_free_uid
open16: allow restricted O_CREAT of FIFOs and regular files

 include/tst_uid.h                         |  29 ++++-
 lib/tst_uid.c                             |  30 ++++-
 runtest/syscalls                          |   1 +
 testcases/kernel/syscalls/open/.gitignore |   1 +
 testcases/kernel/syscalls/open/open16.c   | 135 ++++++++++++++++++++++
 5 files changed, 191 insertions(+), 5 deletions(-)
 create mode 100644 testcases/kernel/syscalls/open/open16.c
Signature · 5 lines
2.54.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#1 lore raw

PATCH v12 1/3 lib/tst_uid: Remove spurious TERRNO from tst_get_free_gid success path

tst_get_free_gid_() prints the found GID when it succeeds, but it incorrectly includes TERRNO in tst_res_(). This prints whatever value happens to be in errno (often 0 or a stale value) which is confusing in a success path.

Remove the spurious TERRNO flag from the tst_res_() success message.

---
 lib/tst_uid.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/tst_uid.c +1 -1
index af4ef8cf7..b0b087362 100644
@@ -24,7 +24,7 @@ gid_t tst_get_free_gid_(const char *file, const int lineno, gid_t skip)
 			continue;
 
 		if (errno == 0 || errno == ENOENT || errno == ESRCH) {
-			tst_res_(file, lineno, TINFO | TERRNO,
+			tst_res_(file, lineno, TINFO,
 				"Found unused GID %d", (int)ret);
 			return ret;
 		}
Signature · 5 lines
2.54.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#2 lore raw

Re: [LTP] lib/tst_uid: Remove spurious TERRNO from tst_get_free_gid success path

Hi Wei,

Quoted 1 line · Wei Gao

lib/tst_uid: Remove spurious TERRNO from tst_get_free_gid success path

Verdict - Reviewed

---
Note:

The agent can sometimes produce false positives although often its findings are genuine. If you find issues with the review, please comment this email or ignore the suggestions.

Regards,
LTP AI Reviewer

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#3 lore raw

Re: [LTP] [PATCH v12 1/3] lib/tst_uid: Remove spurious TERRNO from tst_get_free_gid success path

In reply to Wei Gao via ltp

Hi Wei,

Quoted 17 lines

tst_get_free_gid_() prints the found GID when it succeeds, but it incorrectly includes TERRNO in tst_res_(). This prints whatever value happens to be in errno (often 0 or a stale value) which is confusing in a success path.

Remove the spurious TERRNO flag from the tst_res_() success message.

---
 lib/tst_uid.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/tst_uid.c +0 -0
index af4ef8cf7..b0b087362 100644

lib/tst_uid.c gid_t tst_get_free_gid_(const char *file, const int lineno, gid_t skip) · line 28

Quoted 6 lines
lib/tst_uid.c +1 -1
@@ -24,7 +24,7 @@ gid_t tst_get_free_gid_(const char *file, const int lineno, gid_t skip)
 			continue;
 
 		if (errno == 0 || errno == ENOENT || errno == ESRCH) {
-			tst_res_(file, lineno, TINFO | TERRNO,
+			tst_res_(file, lineno, TINFO,
 				"Found unused GID %d", (int)ret);

uhm? technically it should print 0, ENOENT or ESRCH. That's what the statement is all about. Do you have a log showing what's the issue with it?

--
Andrea Cervesato
SUSE QE Automation Engineer Linux
[email protected]

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#4 lore raw

Re: [LTP] [PATCH v12 1/3] lib/tst_uid: Remove spurious TERRNO from tst_get_free_gid success path

Quoted 21 lines · Andrea Cervesato

Hi Wei,

Quoted 19 lines

tst_get_free_gid_() prints the found GID when it succeeds, but it incorrectly includes TERRNO in tst_res_(). This prints whatever value happens to be in errno (often 0 or a stale value) which is confusing in a success path.

Remove the spurious TERRNO flag from the tst_res_() success message.

---
 lib/tst_uid.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/tst_uid.c +0 -0
index af4ef8cf7..b0b087362 100644
@@ -24,7 +24,7 @@ gid_t tst_get_free_gid_(const char *file, const int lineno, gid_t skip)
 			continue;
 
Quoted 7 lines

lib/tst_uid.c gid_t tst_get_free_gid_(const char *file, const int lineno, gid_t skip) · line 28

Quoted 4 lines
lib/tst_uid.c +1 -1
 		if (errno == 0 || errno == ENOENT || errno == ESRCH) {
-			tst_res_(file, lineno, TINFO | TERRNO,
+			tst_res_(file, lineno, TINFO,
 				"Found unused GID %d", (int)ret);

uhm? technically it should print 0, ENOENT or ESRCH. That's what the statement is all about. Do you have a log showing what's the issue with it?

AI feedback error is misleading need remove in patch10 , so do you want add new AI rule fix it?

Following is AI feedback:
https://patchwork.ozlabs.org/project/ltp/patch/[email protected]/

Review

Quoted 4 lines
(quoted diff) +4 -0
+		if (errno == 0 || errno == ENOENT || errno == ESRCH) {
+			tst_res_(file, lineno, TINFO | TERRNO,
+				"Found unused UID %d", (int)ret);
+			return ret;

When getpwuid() returns NULL with errno set to ENOENT or ESRCH, TERRNO appends the errno string to the message, producing output like:
"Found unused UID 5: No such file or directory". The function succeeded, so the error string is misleading. Should TERRNO be dropped here, or should it be conditionally omitted when errno indicates "not found"?

Quoted 5 lines

--
Andrea Cervesato
SUSE QE Automation Engineer Linux
[email protected]

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#5 lore raw

Re: [LTP] [PATCH v12 1/3] lib/tst_uid: Remove spurious TERRNO from tst_get_free_gid success path

Quoted 2 lines

AI feedback error is misleading need remove in patch10 , so do you want add new AI rule fix it?

sorry, I overlooked. It's correct, thanks for pointing it out :)

--
Andrea Cervesato
SUSE QE Automation Engineer Linux
[email protected]

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#7 lore raw

PATCH v12 2/3 lib: New library function tst_get_free_uid

In reply to Wei Gao via ltp

Add tst_get_free_uid() to dynamically find unused UIDs for tests.

Some tests need a completely unassigned, unused UID. This is used by open16 to verify restricted O_CREAT in sticky directories by running as a sandboxed user with no file ownership or privileges.

---
 include/tst_uid.h | 29 +++++++++++++++++++++++++----
 lib/tst_uid.c     | 28 ++++++++++++++++++++++++++++
 2 files changed, 53 insertions(+), 4 deletions(-)
include/tst_uid.h +25 -4
index 2237ddcbf..c73450956 100644
@@ -7,12 +7,33 @@
 
 #include <sys/types.h>
 
-/*
- * Find unassigned gid. The skip argument can be used to ignore e.g. the main
- * group of a specific user in case it's not listed in the group file. If you
- * do not need to skip any specific gid, simply set it to 0.
+uid_t tst_get_free_uid_(const char *file, const int lineno, uid_t skip);
+
+/**
+ * tst_get_free_uid() - Find a UID not assigned to any user.
+ * @skip: UID value to skip (pass 0 to skip none).
+ *
+ * Scans the password database for the first unused UID starting
+ * from 1, skipping @skip. Calls tst_brk(TBROK) if no free UID
+ * is found or a lookup error occurs.
+ *
+ * Return: An unused uid_t value.
  */
+#define tst_get_free_uid(skip) tst_get_free_uid_(__FILE__, __LINE__, (skip))
+
 gid_t tst_get_free_gid_(const char *file, const int lineno, gid_t skip);
+
+/**
+ * tst_get_free_gid() - Find a GID not assigned to any group.
+ * @skip: GID value to skip (pass 0 to skip none).
+ *
+ * Scans the group database for the first unused GID starting from 1,
+ * skipping @skip. The @skip argument can be used to ignore e.g. the main
+ * group of a specific user in case it's not listed in the group file.
+ * Calls tst_brk(TBROK) if no free GID is found or a lookup error occurs.
+ *
+ * Return: An unused gid_t value.
+ */
 #define tst_get_free_gid(skip) tst_get_free_gid_(__FILE__, __LINE__, (skip))
 
 /*
lib/tst_uid.c +28 -0
index b0b087362..47c267bc4 100644
@@ -5,6 +5,7 @@
 
 #include <sys/types.h>
 #include <grp.h>
+#include <pwd.h>
 #include <errno.h>
 
 #define TST_NO_DEFAULT_MAIN
@@ -12,6 +13,33 @@
 #include "tst_uid.h"
 
 #define MAX_GID 32767
+#define MAX_UID 32767
+
+uid_t tst_get_free_uid_(const char *file, const int lineno, uid_t skip)
+{
+	uid_t ret;
+
+	for (ret = 1; ret < MAX_UID; ret++) {
+		if (ret == skip)
+			continue;
+
+		errno = 0;
+		if (getpwuid(ret))
+			continue;
+
+		if (errno == 0 || errno == ENOENT || errno == ESRCH) {
+			tst_res_(file, lineno, TINFO,
+				"Found unused UID %d", (int)ret);
+			return ret;
+		}
+
+		tst_brk_(file, lineno, TBROK | TERRNO, "User ID lookup failed");
+		return (uid_t)-1;
+	}
+
+	tst_brk_(file, lineno, TBROK, "No free user ID found");
+	return (uid_t)-1;
+}
 
 gid_t tst_get_free_gid_(const char *file, const int lineno, gid_t skip)
 {
Signature · 5 lines
2.54.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#8 lore raw

Re: [LTP] [PATCH v12 2/3] lib: New library function tst_get_free_uid

Hi Wei,

Quoted 11 lines

+/**
+ * tst_get_free_gid() - Find a GID not assigned to any group.
+ * @skip: GID value to skip (pass 0 to skip none).
+ *
+ * Scans the group database for the first unused GID starting from 1,
+ * skipping @skip. The @skip argument can be used to ignore e.g. the main
+ * group of a specific user in case it's not listed in the group file.
+ * Calls tst_brk(TBROK) if no free GID is found or a lookup error occurs.
+ *
+ * Return: An unused gid_t value.
+ */

This documentation is out of scope and should be sent from an another patch-set.

lib/tst_uid.c line 30

Quoted 5 lines
lib/tst_uid.c +5 -0
+		errno = 0;
+		if (getpwuid(ret))
+			continue;
+
+		if (errno == 0 || errno == ENOENT || errno == ESRCH) {

This contradicts the patch 1/3.

--
Andrea Cervesato
SUSE QE Automation Engineer Linux
[email protected]

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#9 lore raw

Re: [LTP] [PATCH v12 2/3] lib: New library function tst_get_free_uid

Quoted 17 lines · Andrea Cervesato

Hi Wei,

Quoted 11 lines

+/**
+ * tst_get_free_gid() - Find a GID not assigned to any group.
+ * @skip: GID value to skip (pass 0 to skip none).
+ *
+ * Scans the group database for the first unused GID starting from 1,
+ * skipping @skip. The @skip argument can be used to ignore e.g. the main
+ * group of a specific user in case it's not listed in the group file.
+ * Calls tst_brk(TBROK) if no free GID is found or a lookup error occurs.
+ *
+ * Return: An unused gid_t value.
+ */

This documentation is out of scope and should be sent from an another patch-set.

Quoted 7 lines

lib/tst_uid.c line 30

Quoted 5 lines
lib/tst_uid.c +5 -0
+		errno = 0;
+		if (getpwuid(ret))
+			continue;
+
+		if (errno == 0 || errno == ENOENT || errno == ESRCH) {

This contradicts the patch 1/3.

This line is same as patch 1/3, could you give more detail where contradicts?

Quoted 5 lines

--
Andrea Cervesato
SUSE QE Automation Engineer Linux
[email protected]

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#10 lore raw

Re: [LTP] [PATCH v12 2/3] lib: New library function tst_get_free_uid

Hi Wei,

Quoted 3 lines
Quoted 1 line

This contradicts the patch 1/3.

This line is same as patch 1/3, could you give more detail where contradicts?

you are right, forget about my comment.

Regards,
--
Andrea Cervesato
SUSE QE Automation Engineer Linux
[email protected]

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#11 lore raw

Re: [LTP] [PATCH v12 2/3] lib: New library function tst_get_free_uid

In reply to Andrea Cervesato via ltp

Hi Wei,

Quoted 12 lines
Quoted 11 lines

+/**
+ * tst_get_free_gid() - Find a GID not assigned to any group.
+ * @skip: GID value to skip (pass 0 to skip none).
+ *
+ * Scans the group database for the first unused GID starting from 1,
+ * skipping @skip. The @skip argument can be used to ignore e.g. the main
+ * group of a specific user in case it's not listed in the group file.
+ * Calls tst_brk(TBROK) if no free GID is found or a lookup error occurs.
+ *
+ * Return: An unused gid_t value.
+ */

Quoted 2 lines

This documentation is out of scope and should be sent from an another patch-set.

this is still valid. Can you please remove it from the patch, or append a new patch just for this documentation? So we can split the current implementation from the documentation part.

--
Andrea Cervesato
SUSE QE Automation Engineer Linux
[email protected]

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#12 lore raw

PATCH v12 3/3 open16: allow restricted O_CREAT of FIFOs and regular files

In reply to Wei Gao via ltp

Add LTP coverage for kernel commit 30aba6656f61 (Linux 4.19), which introduced protection against spoofing attacks via O_CREAT of FIFOs and regular files in world-writable or group-writable sticky directories.

This commit adds test cases to verify these security restrictions (Level 1 and Level 2 protections) for opening FIFOs and regular files in world-writable or group-writable sticky directories when the file is not owned by the opener.

---
 runtest/syscalls                          |   1 +
 testcases/kernel/syscalls/open/.gitignore |   1 +
 testcases/kernel/syscalls/open/open16.c   | 135 ++++++++++++++++++++++
 3 files changed, 137 insertions(+)
 create mode 100644 testcases/kernel/syscalls/open/open16.c
runtest/syscalls +1 -0
index a021c79da..4fd62efa8 100644
@@ -1008,6 +1008,7 @@ open12 open12
 open13 open13
 open14 open14
 open15 open15
+open16 open16
 
 openat01 openat01
 openat02 openat02
testcases/kernel/syscalls/open/.gitignore +1 -0
index af5997572..d2cacc02e 100644
@@ -13,3 +13,4 @@
 /open13
 /open14
 /open15
+/open16
testcases/kernel/syscalls/open/open16.c new file +135 -0

135 lines not on this page · raw message

Signature · 5 lines
2.54.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#13 lore raw

Hi Wei,

Quoted 123 lines

Add LTP coverage for kernel commit 30aba6656f61 (Linux 4.19), which introduced protection against spoofing attacks via O_CREAT of FIFOs and regular files in world-writable or group-writable sticky directories.

This commit adds test cases to verify these security restrictions (Level 1 and Level 2 protections) for opening FIFOs and regular files in world-writable or group-writable sticky directories when the file is not owned by the opener.

---
 runtest/syscalls                          |   1 +
 testcases/kernel/syscalls/open/.gitignore |   1 +
 testcases/kernel/syscalls/open/open16.c   | 135 ++++++++++++++++++++++
 3 files changed, 137 insertions(+)
 create mode 100644 testcases/kernel/syscalls/open/open16.c
runtest/syscalls +1 -0
index a021c79da..4fd62efa8 100644
@@ -1008,6 +1008,7 @@ open12 open12
 open13 open13
 open14 open14
 open15 open15
+open16 open16
 
 openat01 openat01
 openat02 openat02
testcases/kernel/syscalls/open/.gitignore +1 -0
index af5997572..d2cacc02e 100644
@@ -13,3 +13,4 @@
 /open13
 /open14
 /open15
+/open16
testcases/kernel/syscalls/open/open16.c new file +78 -0
new file mode 100644
index 000000000..c74601032
@@ -0,0 +1,135 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2026 Wei Gao <[email protected]>
+ */
+
+/*\
+ * Verify restricted opening (:manpage:`open(2)` and :manpage:`openat(2)`) of
+ * FIFOs and regular files in sticky directories. This test covers the positive
+ * case where access is allowed when protection is disabled (level 0), and the
+ * negative cases where access is disallowed (EACCES) in world-writable (level
+ * 1) or group-writable (level 2) sticky directories when the file is not owned
+ * by the opener.
+ *
+ * This test requires root to modify /proc/sys/fs/protected_* sysctls and
+ * to manage file ownership and permissions in sticky directories.
+ */
+
+#include <pwd.h>
+#include <stdlib.h>
+#include "tst_test.h"
+#include "tst_safe_file_at.h"
+#include "tst_uid.h"
+
+#define DIR "ltp_tmp_check1"
+#define TEST_FILE "test_file_1"
+#define TEST_FIFO "test_fifo_1"
+#define PROTECTED_REGULAR "/proc/sys/fs/protected_regular"
+#define PROTECTED_FIFOS "/proc/sys/fs/protected_fifos"
+#define TEST_FIFO_PATH DIR "/" TEST_FIFO
+
+static int dir_fd = -1;
+static uid_t uid1, uid2;
+static gid_t gid1;
+
+static struct tcase {
+	char *level;
+	int exp_errno;
+	uid_t owner_uid;
+	int use_nobody_gid;
+	mode_t dir_mode;
+} tcases[] = {
+	{"0", 0,      0,  0, 0777 | S_ISVTX},
+	{"1", EACCES, 0,  0, 0777 | S_ISVTX},
+	{"2", EACCES, -1, 1, 0030 | S_ISVTX},
+};
+
+static void verify_open(unsigned int n)
+{
+	struct tcase *tc = &tcases[n];
+	pid_t pid;
+
+	SAFE_FILE_PRINTF(PROTECTED_REGULAR, "%s", tc->level);
+	SAFE_FILE_PRINTF(PROTECTED_FIFOS, "%s", tc->level);
+
+	if (tc->owner_uid != (uid_t)-1 || tc->use_nobody_gid) {
+		gid_t gid = tc->use_nobody_gid ? gid1 : 0;
+
+		SAFE_CHOWN(DIR, tc->owner_uid, gid);
+	}
+
+	if (tc->dir_mode)
+		SAFE_CHMOD(DIR, tc->dir_mode);
+
+	pid = SAFE_FORK();
+	if (!pid) {
+		SAFE_SETGID(gid1);
+		SAFE_SETUID(uid2);
+
+		if (tc->exp_errno) {
+			TST_EXP_FAIL2(openat(dir_fd, TEST_FILE, O_RDWR | O_CREAT, 0777),
+				      tc->exp_errno, "openat %s (Level %s)", TEST_FILE, tc->level);
+			TST_EXP_FAIL2(open(TEST_FIFO_PATH, O_RDWR | O_CREAT, 0777),
+				      tc->exp_errno, "open %s (Level %s)", TEST_FIFO, tc->level);
+		} else {
+			int fd = TST_EXP_FD(openat(dir_fd, TEST_FILE, O_CREAT | O_RDWR, 0777));
+
+			if (TST_PASS)
+				SAFE_CLOSE(fd);

testcases/kernel/syscalls/open/open16.c line 88

Quoted 10 lines
testcases/kernel/syscalls/open/open16.c new file +10 -0
+
+			fd = TST_EXP_FD(open(TEST_FIFO_PATH, O_RDWR | O_CREAT, 0777));
+			if (TST_PASS)
+				SAFE_CLOSE(fd);
+		}
+
+		exit(0);
+	}
+
+	SAFE_WAITPID(pid, NULL, 0);

not needed, we have tst_reap_children().

Quoted 50 lines
testcases/kernel/syscalls/open/open16.c new file +47 -0
+}
+
+static void setup(void)
+{
+	struct passwd *pw;
+
+	pw = SAFE_GETPWNAM("nobody");
+	uid1 = pw->pw_uid;
+	gid1 = pw->pw_gid;
+	uid2 = tst_get_free_uid(uid1);
+
+	umask(0);
+	SAFE_MKDIR(DIR, 0777 | S_ISVTX);
+	dir_fd = SAFE_OPEN(DIR, O_DIRECTORY);
+
+	int fd = SAFE_OPENAT(dir_fd, TEST_FILE, O_CREAT | O_RDWR, 0777);
+
+	SAFE_CLOSE(fd);
+	SAFE_MKFIFO(TEST_FIFO_PATH, 0777);
+	SAFE_CHOWN(TEST_FIFO_PATH, uid1, gid1);
+	SAFE_CHOWN(DIR "/" TEST_FILE, uid1, gid1);
+}
+
+static void cleanup(void)
+{
+	if (dir_fd != -1)
+		SAFE_CLOSE(dir_fd);
+}
+
+static struct tst_test test = {
+	.setup = setup,
+	.cleanup = cleanup,
+	.needs_root = 1,
+	.tcnt = ARRAY_SIZE(tcases),
+	.test = verify_open,
+	.needs_tmpdir = 1,
+	.forks_child = 1,
+	.save_restore = (const struct tst_path_val[]) {
+		{PROTECTED_REGULAR, NULL, TST_SR_TCONF},
+		{PROTECTED_FIFOS, NULL, TST_SR_TCONF},
+		{}
+	},
+	.tags = (const struct tst_tag[]) {
+		{"linux-git", "30aba6656f61"},
+		{}
+	}
+};
Signature · 1 line
2.54.0

--
Andrea Cervesato
SUSE QE Automation Engineer Linux
[email protected]

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#14 lore raw
Quoted 3 lines

testcases/kernel/syscalls/open/open16.c line 88

Quoted 1 line
testcases/kernel/syscalls/open/open16.c new file +1 -0
+	SAFE_WAITPID(pid, NULL, 0);

not needed, we have tst_reap_children().

this is also still valid. Please send a v13

--
Andrea Cervesato
SUSE QE Automation Engineer Linux
[email protected]

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp