Skip to contentSkip to conversation

open15: allow restricted O_CREAT of FIFOs and regular files

PATCH Superseded Wei Gao via ltp 3 participants 28 messages Oct 9, 2023 – Jul 7 Library code

v1 of 17 Superseded Latest is v17 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 open15: allow restricted O_CREAT of FIFOs and regular files

Fix: #574

---
 runtest/syscalls                          |   1 +
 testcases/kernel/syscalls/open/.gitignore |   1 +
 testcases/kernel/syscalls/open/open15.c   | 188 ++++++++++++++++++++++
 3 files changed, 190 insertions(+)
 create mode 100644 testcases/kernel/syscalls/open/open15.c
runtest/syscalls +1 -0
index 4f1ee1f34..4152e1e5f 100644
@@ -935,6 +935,7 @@ open11 open11
 open12 open12
 open13 open13
 open14 open14
+open15 open15
 
 openat01 openat01
 openat02 openat02
testcases/kernel/syscalls/open/.gitignore +1 -0
index 001d874d6..af5997572 100644
@@ -12,3 +12,4 @@
 /open12_child
 /open13
 /open14
+/open15
testcases/kernel/syscalls/open/open15.c new file +188 -0

188 lines not on this page · raw message

Signature · 5 lines
2.35.3


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

Hi!

Quoted 1 line

+// SPDX-License-Identifier: GPL-2.0-only

The default license should be GPL-2.0-or-later for new tests.

Quoted 44 lines
testcases/kernel/syscalls/open/open15.c new file +44 -0
+/*
+ * Copyright (c) 2023 Wei Gao <[email protected]>
+ */
+
+/*\
+ * [Description]
+ *
+ * Verify disallows open of FIFOs or regular files not owned by the user in world
+ * writable sticky directories
+ *
+ * Linux commit 30aba6656f61ed44cba445a3c0d38b296fa9e8f5
+ */
+
+#include <pwd.h>
+#include <stdlib.h>
+#include "tst_test.h"
+#include "tst_safe_file_at.h"
+
+#define  FILENAME  "setuid04_testfile"
+#define  DIR "ltp_tmp_check1"
+#define  TEST_FILE "test_file_1"
+#define  TEST_FIFO "test_fifo_1"
+#define  LTP_USER1 "ltp_user1"
+#define  LTP_USER2 "ltp_user2"
+#define  CONCAT(dir, filename) dir "/" filename
+#define  PROTECTED_REGULAR "/proc/sys/fs/protected_regular"
+#define  PROTECTED_FIFOS "/proc/sys/fs/protected_fifos"
+
+static int LTP_USER1_UID;
+static int LTP_USER2_UID;
+static int dir_fd;
+
+static void run(void)
+{
+	int pid;
+
+	SAFE_FILE_PRINTF(PROTECTED_REGULAR, "%d", 0);
+	SAFE_FILE_PRINTF(PROTECTED_FIFOS, "%d", 0);
+
+	pid = SAFE_FORK();
+	if (pid == 0) {
+		SAFE_SETUID(LTP_USER1_UID);
+
+		int fd = SAFE_OPENAT(dir_fd, TEST_FILE, O_CREAT | O_RDWR, 0777);

testcases/kernel/syscalls/open/open15.c line 55

Quoted 10 lines
testcases/kernel/syscalls/open/open15.c new file +10 -0
+
+		SAFE_CLOSE(fd);
+		fd = SAFE_MKFIFO(CONCAT(DIR, TEST_FIFO), 0777);
+		SAFE_CLOSE(fd);
+
+		TST_CHECKPOINT_WAKE(0);
+		exit(0);
+	}
+
+	TST_CHECKPOINT_WAIT(0);

This shouldn't be checkpoint, but rather you should wait for the child process with tst_reap_children() and the same in all the other cases.

testcases/kernel/syscalls/open/open15.c line 62

Quoted 6 lines
testcases/kernel/syscalls/open/open15.c new file +6 -0
+	pid = SAFE_FORK();
+
+	if (pid == 0) {
+		SAFE_SETUID(LTP_USER2_UID);
+
+		int fd = SAFE_OPENAT(dir_fd, TEST_FILE, O_CREAT | O_RDWR, 0777);

This can't be SAFE_OPENAT() since it will TBROK instead of TFAIL in a case of a failure. You have to do the TST_EXP_FD(openat(...)) instead.

And the same for all the cases that shouldn't fail.

Quoted 16 lines
testcases/kernel/syscalls/open/open15.c new file +16 -0
+		tst_res(TPASS, "check protect_regural == 0 pass");
+		SAFE_CLOSE(fd);
+
+		fd = SAFE_OPEN(CONCAT(DIR, TEST_FIFO), O_RDWR | O_CREAT);
+		tst_res(TPASS, "check protect_fifos == 0 pass");
+		SAFE_CLOSE(fd);
+
+		TST_CHECKPOINT_WAKE(1);
+		exit(0);
+	}
+
+	TST_CHECKPOINT_WAIT(1);
+
+	SAFE_FILE_PRINTF(PROTECTED_REGULAR, "%d", 1);
+	SAFE_FILE_PRINTF(PROTECTED_FIFOS, "%d", 1);
+

testcases/kernel/syscalls/open/open15.c line 89

Quoted 10 lines
testcases/kernel/syscalls/open/open15.c new file +10 -0
+	pid = SAFE_FORK();
+
+	if (pid == 0) {
+		SAFE_SETUID(LTP_USER2_UID);
+
+		TEST(openat(dir_fd, TEST_FILE, O_RDWR | O_CREAT, 0777));
+		if (TST_RET == -1 && TST_ERR == EACCES)
+			tst_res(TPASS, "check protect_regural == 1 pass");
+		else
+			tst_res(TFAIL | TERRNO, "check protect_regural == 1 failed");

And here you have to do TST_EXP_FAIL(openat(...)) and in all the failing cases as well.

Quoted 66 lines
testcases/kernel/syscalls/open/open15.c new file +66 -0
+		TEST(open(CONCAT(DIR, TEST_FIFO), O_RDWR | O_CREAT, 0777));
+		if (TST_RET == -1 && TST_ERR == EACCES)
+			tst_res(TPASS, "check protect_fifos == 1 pass");
+		else
+			tst_res(TFAIL | TERRNO, "check protect_fifos == 1 failed");
+
+		TST_CHECKPOINT_WAKE(2);
+		exit(0);
+	}
+
+	TST_CHECKPOINT_WAIT(2);
+
+	SAFE_FILE_PRINTF(PROTECTED_REGULAR, "%d", 2);
+	SAFE_FILE_PRINTF(PROTECTED_FIFOS, "%d", 2);
+	SAFE_CHMOD(DIR, 0020 | S_ISVTX);
+
+	pid = SAFE_FORK();
+
+	if (pid == 0) {
+		SAFE_SETUID(LTP_USER2_UID);
+
+		TEST(openat(dir_fd, TEST_FILE, O_RDWR | O_CREAT, 0777));
+		if (TST_RET == -1 && TST_ERR == EACCES)
+			tst_res(TPASS, "check protect_regural == 2 pass");
+		else
+			tst_res(TFAIL | TERRNO, "check protect_regural == 2 failed");
+
+		TEST(open(CONCAT(DIR, TEST_FIFO), O_RDWR | O_CREAT, 0777));
+		if (TST_RET == -1 && TST_ERR == EACCES)
+			tst_res(TPASS, "check protect_fifos == 2 pass");
+		else
+			tst_res(TFAIL | TERRNO, "check protect_fifos == 2 failed");
+
+		TST_CHECKPOINT_WAKE(3);
+		exit(0);
+	}
+
+	TST_CHECKPOINT_WAIT(3);
+}
+
+static int add_user(char *username)
+{
+	const char *const cmd_useradd[] = {"useradd", username, NULL};
+	struct passwd *ltpuser;
+	int rc, uid = -1;
+
+	switch ((rc = tst_cmd(cmd_useradd, NULL, NULL, TST_CMD_PASS_RETVAL))) {
+	case 0:
+	case 9:
+		ltpuser = SAFE_GETPWNAM(username);
+		uid = ltpuser->pw_uid;
+		break;
+	default:
+		tst_brk(TBROK, "Useradd failed (%d)", rc);
+	}
+
+	return uid;
+}
+
+static void del_user(char *username)
+{
+	const char *const cmd_userdel[] = {"userdel", "-r", username, NULL};
+
+	tst_cmd(cmd_userdel, NULL, NULL, TST_CMD_PASS_RETVAL);
+
+}

testcases/kernel/syscalls/open/open15.c line 166

Quoted 10 lines
testcases/kernel/syscalls/open/open15.c new file +10 -0
+
+static void setup(void)
+{
+	umask(0);
+	SAFE_MKDIR(DIR, 0777 | S_ISVTX);
+
+	dir_fd = SAFE_OPEN(DIR, O_DIRECTORY);
+
+	LTP_USER1_UID = add_user(LTP_USER1);
+	LTP_USER2_UID = add_user(LTP_USER2);

We actually does not need to add users for these tests, we can just choose two random UIDs and use them, since all the kernel does is to compare the UIDs of the processes...

So we can just do:

#define LTP_USR_UID1 1000
#define LTP_USR_UID2 1001

Quoted 8 lines
testcases/kernel/syscalls/open/open15.c new file +8 -0
+}
+
+static void cleanup(void)
+{
+	del_user(LTP_USER1);
+	del_user(LTP_USER2);
+}
+

testcases/kernel/syscalls/open/open15.c line 184

Quoted 10 lines
testcases/kernel/syscalls/open/open15.c new file +10 -0
+static struct tst_test test = {
+	.setup = setup,
+	.cleanup = cleanup,
+	.needs_root = 1,
+	.test_all = run,
+	.needs_tmpdir = 1,
+	.forks_child = 1,
+	.save_restore = (const struct tst_path_val[]) {
+		{PROTECTED_REGULAR, NULL, TST_SR_SKIP},
+		{PROTECTED_FIFOS, NULL, TST_SR_SKIP},
                                         ^
					 TST_SR_TCONF

Since we want to skip the test if these files are missing.

Quoted 10 lines
testcases/kernel/syscalls/open/open15.c new file +4 -0
+		{}
+	},
+	.needs_checkpoints = 1,
+};
Signature · 5 lines
2.35.3


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Signature · 5 lines
Cyril Hrubis
[email protected]

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

PATCH v2 1/1 open15: allow restricted O_CREAT of FIFOs and regular files

In reply to Wei Gao via ltp

Fix: #574

---
 runtest/syscalls                          |   1 +
 testcases/kernel/syscalls/open/.gitignore |   1 +
 testcases/kernel/syscalls/open/open15.c   | 138 ++++++++++++++++++++++
 3 files changed, 140 insertions(+)
 create mode 100644 testcases/kernel/syscalls/open/open15.c
runtest/syscalls +1 -0
index 4f1ee1f34..4152e1e5f 100644
@@ -935,6 +935,7 @@ open11 open11
 open12 open12
 open13 open13
 open14 open14
+open15 open15
 
 openat01 openat01
 openat02 openat02
testcases/kernel/syscalls/open/.gitignore +1 -0
index 001d874d6..af5997572 100644
@@ -12,3 +12,4 @@
 /open12_child
 /open13
 /open14
+/open15
testcases/kernel/syscalls/open/open15.c new file +138 -0

138 lines not on this page · raw message

Signature · 5 lines
2.35.3


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

Hi!

Quoted 27 lines
testcases/kernel/syscalls/open/open15.c new file +27 -0
+/*\
+ * [Description]
+ *
+ * Verify disallows open of FIFOs or regular files not owned by the user in world
+ * writable sticky directories
+ *
+ * commit 30aba6656f61ed44cba445a3c0d38b296fa9e8f5
+ * Author: Salvatore Mesoraca <[email protected]>
+ * Date:   Thu Aug 23 17:00:35 2018 -0700
+ *     namei: allow restricted O_CREAT of FIFOs and regular files
+ */
+
+#include <pwd.h>
+#include <stdlib.h>
+#include "tst_test.h"
+#include "tst_safe_file_at.h"
+
+#define  FILENAME  "setuid04_testfile"
+#define  DIR "ltp_tmp_check1"
+#define  TEST_FILE "test_file_1"
+#define  TEST_FIFO "test_fifo_1"
+#define  LTP_USR_UID1 1000
+#define  LTP_USR_UID2 1001
+#define  CONCAT(dir, filename) dir "/" filename
+#define  PROTECTED_REGULAR "/proc/sys/fs/protected_regular"
+#define  PROTECTED_FIFOS "/proc/sys/fs/protected_fifos"
+

testcases/kernel/syscalls/open/open15.c line 42

Quoted 10 lines
testcases/kernel/syscalls/open/open15.c new file +10 -0
+static int dir_fd;
+
+static void run(void)
+{
+	int pid;
+
+	SAFE_FILE_PRINTF(PROTECTED_REGULAR, "%d", 0);
+	SAFE_FILE_PRINTF(PROTECTED_FIFOS, "%d", 0);
+
+	pid = SAFE_FORK();

No need to store the pid if we are not using it. We can do instead just:

	if (!SAFE_FORK()) {

testcases/kernel/syscalls/open/open15.c line 50

Quoted 8 lines
testcases/kernel/syscalls/open/open15.c new file +8 -0
+	if (pid == 0) {
+		SAFE_SETUID(LTP_USR_UID1);
+
+		int fd = TST_EXP_FD(openat(dir_fd, TEST_FILE, O_CREAT | O_RDWR, 0777));
+
+		SAFE_CLOSE(fd);
+		fd = SAFE_MKFIFO(CONCAT(DIR, TEST_FIFO), 0777);
+		SAFE_CLOSE(fd);

This part has to be done in the test setup() otherwise the test will fail with EEXIST with -i 2.

testcases/kernel/syscalls/open/open15.c line 64

Quoted 13 lines
testcases/kernel/syscalls/open/open15.c new file +13 -0
+		exit(0);
+	}
+
+	tst_reap_children();
+
+	pid = SAFE_FORK();
+
+	if (pid == 0) {
+		SAFE_SETUID(LTP_USR_UID2);
+
+		int fd = TST_EXP_FD(openat(dir_fd, TEST_FILE, O_CREAT | O_RDWR, 0777));
+
+		tst_res(TPASS, "check protect_regural == 0 pass");

The TST_EXP_FD() should print TPASS message, there is no point in adding another.

Review

Quoted 1 line
(quoted diff) +1 -0
+		SAFE_CLOSE(fd);

This should be closed only if the fd is valid.

testcases/kernel/syscalls/open/open15.c line 69

Quoted 3 lines
testcases/kernel/syscalls/open/open15.c new file +3 -0
+		fd = SAFE_OPEN(CONCAT(DIR, TEST_FIFO), O_RDWR | O_CREAT);
+		tst_res(TPASS, "check protect_fifos == 0 pass");
+		SAFE_CLOSE(fd);

Again this should be TST_EXP_FD().

Quoted 15 lines
testcases/kernel/syscalls/open/open15.c new file +15 -0
+		exit(0);
+	}
+
+	tst_reap_children();
+
+	SAFE_FILE_PRINTF(PROTECTED_REGULAR, "%d", 1);
+	SAFE_FILE_PRINTF(PROTECTED_FIFOS, "%d", 1);
+
+	pid = SAFE_FORK();
+
+	if (pid == 0) {
+		SAFE_SETUID(LTP_USR_UID2);
+
+		TST_EXP_FAIL(openat(dir_fd, TEST_FILE, O_RDWR | O_CREAT, 0777), EACCES);
+

testcases/kernel/syscalls/open/open15.c line 95

Quoted 10 lines
testcases/kernel/syscalls/open/open15.c new file +10 -0
+		TST_EXP_FAIL(open(CONCAT(DIR, TEST_FIFO), O_RDWR | O_CREAT, 0777), EACCES);
+
+		exit(0);
+	}
+
+	tst_reap_children();
+
+	SAFE_FILE_PRINTF(PROTECTED_REGULAR, "%d", 2);
+	SAFE_FILE_PRINTF(PROTECTED_FIFOS, "%d", 2);
+	SAFE_CHMOD(DIR, 0020 | S_ISVTX);

I suppose that this will break the test with -i 2 as well, you need to chmod the directory back at the end of the test.

Quoted 31 lines
testcases/kernel/syscalls/open/open15.c new file +31 -0
+	pid = SAFE_FORK();
+
+	if (pid == 0) {
+		SAFE_SETUID(LTP_USR_UID2);
+
+		TST_EXP_FAIL(openat(dir_fd, TEST_FILE, O_RDWR | O_CREAT, 0777), EACCES);
+
+		TST_EXP_FAIL(open(CONCAT(DIR, TEST_FIFO), O_RDWR | O_CREAT, 0777), EACCES);
+
+		exit(0);
+	}
+
+	tst_reap_children();
+}
+
+static void setup(void)
+{
+	umask(0);
+	SAFE_MKDIR(DIR, 0777 | S_ISVTX);
+
+	dir_fd = SAFE_OPEN(DIR, O_DIRECTORY);
+}
+
+static void cleanup(void)
+{
+	SAFE_CLOSE(dir_fd);
+}
+
+static struct tst_test test = {
+	.setup = setup,
+	.cleanup = cleanup,

testcases/kernel/syscalls/open/open15.c line 137

Quoted 10 lines
testcases/kernel/syscalls/open/open15.c new file +10 -0
+	.needs_root = 1,
+	.test_all = run,
+	.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},
+		{}
+	},
+	.needs_checkpoints = 1,

This should have been removed.

Quoted 7 lines

+};

Signature · 5 lines
2.35.3


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Signature · 5 lines
Cyril Hrubis
[email protected]

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

PATCH v3 1/1 open15: allow restricted O_CREAT of FIFOs and regular files

In reply to Wei Gao via ltp

Fix: #574

---
 runtest/syscalls                          |   1 +
 testcases/kernel/syscalls/open/.gitignore |   1 +
 testcases/kernel/syscalls/open/open15.c   | 125 ++++++++++++++++++++++
 3 files changed, 127 insertions(+)
 create mode 100644 testcases/kernel/syscalls/open/open15.c
runtest/syscalls +1 -0
index 4f1ee1f34..4152e1e5f 100644
@@ -935,6 +935,7 @@ open11 open11
 open12 open12
 open13 open13
 open14 open14
+open15 open15
 
 openat01 openat01
 openat02 openat02
testcases/kernel/syscalls/open/.gitignore +1 -0
index 001d874d6..af5997572 100644
@@ -12,3 +12,4 @@
 /open12_child
 /open13
 /open14
+/open15
testcases/kernel/syscalls/open/open15.c new file +125 -0

125 lines not on this page · raw message

Signature · 5 lines
2.35.3


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

Hi Wei,

If we want to merge this patch, we need to do a couple of things more. The open15 is now taken by an another test, so we need to move it to open16. Also we don't have a nice git commit message describing what's the purpose of the test.

Quoted 37 lines · Wei Gao via ltp

Fix: #574

---
  runtest/syscalls                          |   1 +
  testcases/kernel/syscalls/open/.gitignore |   1 +
  testcases/kernel/syscalls/open/open15.c   | 125 ++++++++++++++++++++++
  3 files changed, 127 insertions(+)

create mode 100644 testcases/kernel/syscalls/open/open15.c

runtest/syscalls +1 -0
index 4f1ee1f34..4152e1e5f 100644
@@ -935,6 +935,7 @@ open11 open11
  open12 open12
  open13 open13
  open14 open14
+open15 open15
 
  openat01 openat01
  openat02 openat02
testcases/kernel/syscalls/open/.gitignore +1 -0
index 001d874d6..af5997572 100644
@@ -12,3 +12,4 @@
  /open12_child
  /open13
  /open14
+/open15
testcases/kernel/syscalls/open/open15.c new file +0 -0
new file mode 100644
index 000000000..de5325e01

testcases/kernel/syscalls/open/open15.c line 7

Quoted 7 lines
testcases/kernel/syscalls/open/open15.c new file +7 -0
@@ -0,0 +1,125 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2023 Wei Gao <[email protected]>
+ */
+
+/*\
+ * [Description]

Not needed anymore.

testcases/kernel/syscalls/open/open15.c line 15

Quoted 8 lines
testcases/kernel/syscalls/open/open15.c new file +8 -0
+ *
+ * Verify disallows open of FIFOs or regular files not owned by the user in world
+ * writable sticky directories
+ *
+ * commit 30aba6656f61ed44cba445a3c0d38b296fa9e8f5
+ * Author: Salvatore Mesoraca <[email protected]>
+ * Date:   Thu Aug 23 17:00:35 2018 -0700
+ *     namei: allow restricted O_CREAT of FIFOs and regular files

we should update .tags instead of copying commit message here.

Quoted 73 lines
testcases/kernel/syscalls/open/open15.c new file +73 -0
+ */
+
+#include <pwd.h>
+#include <stdlib.h>
+#include "tst_test.h"
+#include "tst_safe_file_at.h"
+
+#define  FILENAME  "setuid04_testfile"
+#define  DIR "ltp_tmp_check1"
+#define  TEST_FILE "test_file_1"
+#define  TEST_FIFO "test_fifo_1"
+#define  LTP_USR_UID1 1000
+#define  LTP_USR_UID2 1001
+#define  CONCAT(dir, filename) dir "/" filename
+#define  PROTECTED_REGULAR "/proc/sys/fs/protected_regular"
+#define  PROTECTED_FIFOS "/proc/sys/fs/protected_fifos"
+
+static int dir_fd;
+
+static void run(void)
+{
+	int pid;
+
+	SAFE_CHMOD(DIR, 0777 | S_ISVTX);
+	SAFE_FILE_PRINTF(PROTECTED_REGULAR, "%d", 0);
+	SAFE_FILE_PRINTF(PROTECTED_FIFOS, "%d", 0);
+
+	if (!SAFE_FORK()) {
+		SAFE_SETUID(LTP_USR_UID1);
+
+		int fd = TST_EXP_FD(openat(dir_fd, TEST_FILE, O_CREAT | O_RDWR, 0777));
+
+		SAFE_CLOSE(fd);
+
+		SAFE_MKFIFO(CONCAT(DIR, TEST_FIFO), 0777);
+
+		exit(0);
+	}
+
+	tst_reap_children();
+
+	if (!SAFE_FORK()) {
+		SAFE_SETUID(LTP_USR_UID2);
+
+		int fd = TST_EXP_FD(openat(dir_fd, TEST_FILE, O_CREAT | O_RDWR, 0777));
+
+		SAFE_CLOSE(fd);
+
+		fd = TST_EXP_FD(open(CONCAT(DIR, TEST_FIFO), O_RDWR | O_CREAT, 0777));
+		SAFE_CLOSE(fd);
+
+		exit(0);
+	}
+
+	tst_reap_children();
+
+	SAFE_FILE_PRINTF(PROTECTED_REGULAR, "%d", 1);
+	SAFE_FILE_PRINTF(PROTECTED_FIFOS, "%d", 1);
+
+	if (!SAFE_FORK()) {
+		SAFE_SETUID(LTP_USR_UID2);
+		TST_EXP_FAIL(openat(dir_fd, TEST_FILE, O_RDWR | O_CREAT, 0777), EACCES);
+		TST_EXP_FAIL(open(CONCAT(DIR, TEST_FIFO), O_RDWR | O_CREAT, 0777), EACCES);
+
+		exit(0);
+	}
+
+	tst_reap_children();
+
+	SAFE_FILE_PRINTF(PROTECTED_REGULAR, "%d", 2);
+	SAFE_FILE_PRINTF(PROTECTED_FIFOS, "%d", 2);
+	SAFE_CHMOD(DIR, 0020 | S_ISVTX);
+

testcases/kernel/syscalls/open/open15.c line 98

Quoted 10 lines
testcases/kernel/syscalls/open/open15.c new file +10 -0
+	if (!SAFE_FORK()) {
+		SAFE_SETUID(LTP_USR_UID2);
+		TST_EXP_FAIL(openat(dir_fd, TEST_FILE, O_RDWR | O_CREAT, 0777), EACCES);
+		TST_EXP_FAIL(open(CONCAT(DIR, TEST_FIFO), O_RDWR | O_CREAT, 0777), EACCES);
+
+		exit(0);
+	}
+
+	tst_reap_children();
+	unlink(CONCAT(DIR, TEST_FIFO));

SAFE_UNLINK()

testcases/kernel/syscalls/open/open15.c line 110

Quoted 12 lines
testcases/kernel/syscalls/open/open15.c new file +12 -0
+}
+
+static void setup(void)
+{
+	umask(0);
+	SAFE_MKDIR(DIR, 0777 | S_ISVTX);
+	dir_fd = SAFE_OPEN(DIR, O_DIRECTORY);
+}
+
+static void cleanup(void)
+{
+	SAFE_CLOSE(dir_fd);

if (dir_fd != -1)
    SAFE_CLOSE(dir_fd);

Quoted 4 lines
testcases/kernel/syscalls/open/open15.c new file +4 -0
+}
+
+static struct tst_test test = {
+	.setup = setup,

testcases/kernel/syscalls/open/open15.c line 124

Quoted 10 lines
testcases/kernel/syscalls/open/open15.c new file +10 -0
+	.cleanup = cleanup,
+	.needs_root = 1,
+	.test_all = run,
+	.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 is missing.

testcases/kernel/syscalls/open/open15.c line 125

Quoted 1 line
testcases/kernel/syscalls/open/open15.c new file +1 -0
+};

Andrea

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

PATCH v4 1/1 open16: allow restricted O_CREAT of FIFOs and regular files

In reply to Wei Gao via ltp

This commit adds test cases to verify the security restrictions for opening FIFOs and regular files in world-writable sticky directories.

---
 runtest/syscalls                          |   1 +
 testcases/kernel/syscalls/open/.gitignore |   1 +
 testcases/kernel/syscalls/open/open16.c   | 121 ++++++++++++++++++++++
 3 files changed, 123 insertions(+)
 create mode 100644 testcases/kernel/syscalls/open/open16.c
runtest/syscalls +1 -0
index 839c23d0a..7a1a64c01 100644
@@ -974,6 +974,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 +121 -0

121 lines not on this page · raw message

Signature · 5 lines
2.35.3


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

Re: [LTP] [PATCH v4] open16: allow restricted O_CREAT of FIFOs and regular files

Hi!

Quoted 9 lines
testcases/kernel/syscalls/open/open16.c new file +9 -0
+/*\
+ * Verify disallows open of FIFOs or regular files not owned by the user in world
+ * writable sticky directories
+ */
+
+#include <pwd.h>
+#include <stdlib.h>
+#include "tst_test.h"
+#include "tst_safe_file_at.h"

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

Quoted 10 lines
testcases/kernel/syscalls/open/open16.c new file +10 -0
+
+#define  FILENAME  "setuid04_testfile"
+#define  DIR "ltp_tmp_check1"
+#define  TEST_FILE "test_file_1"
+#define  TEST_FIFO "test_fifo_1"
+#define  LTP_USR_UID1 1000
+#define  LTP_USR_UID2 1001
+#define  CONCAT(dir, filename) dir "/" filename
+#define  PROTECTED_REGULAR "/proc/sys/fs/protected_regular"
+#define  PROTECTED_FIFOS "/proc/sys/fs/protected_fifos"

^
Just a single space?

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

Quoted 11 lines
testcases/kernel/syscalls/open/open16.c new file +8 -0
+
+static int dir_fd;
+
+static void run(void)
+{
+	SAFE_CHMOD(DIR, 0777 | S_ISVTX);
 
 
 
+	SAFE_FILE_PRINTF(PROTECTED_REGULAR, "%d", 0);
+	SAFE_FILE_PRINTF(PROTECTED_FIFOS, "%d", 0);

Huh, why not just SAFE_FILE_PRINTF(PROTECTED_FIFOS, "0") ?

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

Quoted 6 lines
testcases/kernel/syscalls/open/open16.c new file +6 -0
+	if (!SAFE_FORK()) {
+		SAFE_SETUID(LTP_USR_UID1);
+
+		int fd = TST_EXP_FD(openat(dir_fd, TEST_FILE, O_CREAT | O_RDWR, 0777));
+
+		SAFE_CLOSE(fd);
                ^
		if (TST_PASS)
			SAFE_CLOSE(TST_RET);

Also this seems like a setup phase, so we should probably use just SAFE_OPEN() instead of the TST_EXP_FD() at this point.

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

Quoted 1 line
testcases/kernel/syscalls/open/open16.c new file +1 -0
+		SAFE_MKFIFO(CONCAT(DIR, TEST_FIFO), 0777);
                                  ^

We usually do this as:

#define TEST_FIFO_PATH DIR "/" TEST_FIFO

And use the TEST_FIFO_PATH instead.

Or we can just do SAFE_CHDIR(DIR) after the fork and use just the file names.

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

Quoted 11 lines
testcases/kernel/syscalls/open/open16.c new file +11 -0
+		exit(0);
+	}
+
+	tst_reap_children();
+
+	if (!SAFE_FORK()) {
+		SAFE_SETUID(LTP_USR_UID2);
+
+		int fd = TST_EXP_FD(openat(dir_fd, TEST_FILE, O_CREAT | O_RDWR, 0777));
+
+		SAFE_CLOSE(fd);

Here as well:

if (TST_PASS)
SAFE_CLOSE(TST_RET)

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

Quoted 2 lines
testcases/kernel/syscalls/open/open16.c new file +2 -0
+		fd = TST_EXP_FD(open(CONCAT(DIR, TEST_FIFO), O_RDWR | O_CREAT, 0777));
+		SAFE_CLOSE(fd);

And here as well.

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

Quoted 7 lines
testcases/kernel/syscalls/open/open16.c new file +7 -0
+		exit(0);
+	}
+
+	tst_reap_children();
+
+	SAFE_FILE_PRINTF(PROTECTED_REGULAR, "%d", 1);
+	SAFE_FILE_PRINTF(PROTECTED_FIFOS, "%d", 1);

Here as well why the %d?

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

Quoted 12 lines
testcases/kernel/syscalls/open/open16.c new file +12 -0
+	if (!SAFE_FORK()) {
+		SAFE_SETUID(LTP_USR_UID2);
+		TST_EXP_FAIL(openat(dir_fd, TEST_FILE, O_RDWR | O_CREAT, 0777), EACCES);
+		TST_EXP_FAIL(open(CONCAT(DIR, TEST_FIFO), O_RDWR | O_CREAT, 0777), EACCES);
+
+		exit(0);
+	}
+
+	tst_reap_children();
+
+	SAFE_FILE_PRINTF(PROTECTED_REGULAR, "%d", 2);
+	SAFE_FILE_PRINTF(PROTECTED_FIFOS, "%d", 2);

And here as well.

Quoted 31 lines
testcases/kernel/syscalls/open/open16.c new file +31 -0
+	SAFE_CHMOD(DIR, 0020 | S_ISVTX);
+
+	if (!SAFE_FORK()) {
+		SAFE_SETUID(LTP_USR_UID2);
+		TST_EXP_FAIL(openat(dir_fd, TEST_FILE, O_RDWR | O_CREAT, 0777), EACCES);
+		TST_EXP_FAIL(open(CONCAT(DIR, TEST_FIFO), O_RDWR | O_CREAT, 0777), EACCES);
+
+		exit(0);
+	}
+
+	tst_reap_children();
+	SAFE_UNLINK(CONCAT(DIR, TEST_FIFO));
+}
+
+static void setup(void)
+{
+	umask(0);
+	SAFE_MKDIR(DIR, 0777 | S_ISVTX);
+	dir_fd = SAFE_OPEN(DIR, O_DIRECTORY);
+}
+
+static void cleanup(void)
+{
+	if (dir_fd != -1)
+		SAFE_CLOSE(dir_fd);
+}
+
+static struct tst_test test = {
+	.setup = setup,
+	.cleanup = cleanup,
+	.needs_root = 1,

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

Quoted 10 lines
testcases/kernel/syscalls/open/open16.c new file +10 -0
+	.test_all = run,
+	.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", "30aba6656f61ed44cba445a3c0d38b296fa9e8f5"},

^
We usually shorten these to just first 12 characters.

Quoted 9 lines
testcases/kernel/syscalls/open/open16.c new file +3 -0
+		{}
+	}
+};
Signature · 5 lines
2.35.3


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Signature · 5 lines
Cyril Hrubis
[email protected]

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

PATCH v5 1/1 open16: allow restricted O_CREAT of FIFOs and regular files

In reply to Wei Gao via ltp

This commit adds test cases to verify the security restrictions for opening FIFOs and regular files in world-writable sticky directories.

---
 runtest/syscalls                          |   1 +
 testcases/kernel/syscalls/open/.gitignore |   1 +
 testcases/kernel/syscalls/open/open16.c   | 124 ++++++++++++++++++++++
 3 files changed, 126 insertions(+)
 create mode 100644 testcases/kernel/syscalls/open/open16.c
runtest/syscalls +1 -0
index 3531c2a3c..0de9bfaef 100644
@@ -984,6 +984,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 +124 -0

124 lines not on this page · raw message

Signature · 5 lines
2.49.0


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

Re: [LTP] [PATCH v5] open16: allow restricted O_CREAT of FIFOs and regular files

Hi!

Quoted 44 lines · Wei Gao via ltp

This commit adds test cases to verify the security restrictions for opening FIFOs and regular files in world-writable sticky directories.

---
 runtest/syscalls                          |   1 +
 testcases/kernel/syscalls/open/.gitignore |   1 +
 testcases/kernel/syscalls/open/open16.c   | 124 ++++++++++++++++++++++
 3 files changed, 126 insertions(+)
 create mode 100644 testcases/kernel/syscalls/open/open16.c
runtest/syscalls +1 -0
index 3531c2a3c..0de9bfaef 100644
@@ -984,6 +984,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 +6 -0
new file mode 100644
index 000000000..6e01dde26
@@ -0,0 +1,124 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2023 Wei Gao <[email protected]>
+ */
+
+/*\

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

Quoted 10 lines
testcases/kernel/syscalls/open/open16.c new file +10 -0
+ * Verify disallows open of FIFOs or regular files not owned by the user in world
+ * writable sticky directories
+ */
+
+#include <pwd.h>
+#include <stdlib.h>
+#include "tst_test.h"
+#include "tst_safe_file_at.h"
+
+#define FILENAME  "setuid04_testfile"

Never used.

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

Quoted 5 lines
testcases/kernel/syscalls/open/open16.c new file +5 -0
+#define DIR "ltp_tmp_check1"
+#define TEST_FILE "test_file_1"
+#define TEST_FIFO "test_fifo_1"
+#define LTP_USR_UID1 1000
+#define LTP_USR_UID2 1001

If target is to have a UID that doesn't belong to the current user, we can just have:

  pw = SAFE_GETPWNAM("nobody");

and use the provided UID.

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

Quoted 5 lines
testcases/kernel/syscalls/open/open16.c new file +5 -0
+#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;

Not initialized to -1.

Quoted 98 lines
testcases/kernel/syscalls/open/open16.c new file +98 -0
+
+static void run(void)
+{
+	SAFE_CHMOD(DIR, 0777 | S_ISVTX);
+	SAFE_FILE_PRINTF(PROTECTED_REGULAR, "0");
+	SAFE_FILE_PRINTF(PROTECTED_FIFOS, "0");
+
+	if (!SAFE_FORK()) {
+		SAFE_SETUID(LTP_USR_UID1);
+
+		int fd = SAFE_OPENAT(dir_fd, TEST_FILE, O_CREAT | O_RDWR, 0777);
+
+		SAFE_CLOSE(fd);
+
+		SAFE_MKFIFO(TEST_FIFO_PATH, 0777);
+
+		exit(0);
+	}
+
+	tst_reap_children();
+
+	if (!SAFE_FORK()) {
+		SAFE_SETUID(LTP_USR_UID2);
+
+		int fd = TST_EXP_FD(openat(dir_fd, TEST_FILE, O_CREAT | O_RDWR, 0777));
+
+		if (TST_PASS)
+			SAFE_CLOSE(fd);
+
+		fd = TST_EXP_FD(open(TEST_FIFO_PATH, O_RDWR | O_CREAT, 0777));
+
+		if (TST_PASS)
+			SAFE_CLOSE(fd);
+
+		exit(0);
+	}
+
+	tst_reap_children();
+
+	SAFE_FILE_PRINTF(PROTECTED_REGULAR, "1");
+	SAFE_FILE_PRINTF(PROTECTED_FIFOS, "1");
+
+	if (!SAFE_FORK()) {
+		SAFE_SETUID(LTP_USR_UID2);
+		TST_EXP_FAIL(openat(dir_fd, TEST_FILE, O_RDWR | O_CREAT, 0777), EACCES);
+		TST_EXP_FAIL(open(TEST_FIFO_PATH, O_RDWR | O_CREAT, 0777), EACCES);
+
+		exit(0);
+	}
+
+	tst_reap_children();
+
+	SAFE_FILE_PRINTF(PROTECTED_REGULAR, "2");
+	SAFE_FILE_PRINTF(PROTECTED_FIFOS, "2");
+	SAFE_CHMOD(DIR, 0020 | S_ISVTX);
+
+	if (!SAFE_FORK()) {
+		SAFE_SETUID(LTP_USR_UID2);
+		TST_EXP_FAIL(openat(dir_fd, TEST_FILE, O_RDWR | O_CREAT, 0777), EACCES);
+		TST_EXP_FAIL(open(TEST_FIFO_PATH, O_RDWR | O_CREAT, 0777), EACCES);
+
+		exit(0);
+	}
+
+	tst_reap_children();
+	SAFE_UNLINK(TEST_FIFO_PATH);
+}
+
+static void setup(void)
+{
+	umask(0);
+	SAFE_MKDIR(DIR, 0777 | S_ISVTX);
+	dir_fd = SAFE_OPEN(DIR, O_DIRECTORY);
+}
+
+static void cleanup(void)
+{
+	if (dir_fd != -1)
+		SAFE_CLOSE(dir_fd);
+}
+
+static struct tst_test test = {
+	.setup = setup,
+	.cleanup = cleanup,
+	.needs_root = 1,
+	.test_all = run,
+	.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 · 7 lines
Andrea Cervesato
SUSE QE Automation Engineer Linux
[email protected]


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

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

In reply to Wei Gao via ltp

Wei Gao (2):
lib: New library function tst_get_free_uid
open16: allow restricted O_CREAT of FIFOs and regular files

v5->v6:
- Remove unused FILENAME
- Initialize dir_fd to -1
- Use tst_get_free_uid() dynamically find unused UIDs instead of fixed uids

 include/tst_uid.h                         |   7 +-
 lib/tst_uid.c                             |  26 +++++
 runtest/syscalls                          |   1 +
 testcases/kernel/syscalls/open/.gitignore |   1 +
 testcases/kernel/syscalls/open/open16.c   | 129 ++++++++++++++++++++++
 5 files changed, 162 insertions(+), 2 deletions(-)
 create mode 100644 testcases/kernel/syscalls/open/open16.c
Signature · 5 lines
2.52.0


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

PATCH v6 1/2 lib: New library function tst_get_free_uid

---
 include/tst_uid.h |  7 +++++--
 lib/tst_uid.c     | 26 ++++++++++++++++++++++++++
 2 files changed, 31 insertions(+), 2 deletions(-)
include/tst_uid.h +5 -2
index 2237ddcbf..394cb3edc 100644
@@ -8,10 +8,13 @@
 #include <sys/types.h>
 
 /*
- * Find unassigned gid. The skip argument can be used to ignore e.g. the main
+ * Find unassigned uid/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.
+ * do not need to skip any specific id, simply set it to 0.
  */
+uid_t tst_get_free_uid_(const char *file, const int lineno, uid_t skip);
+#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);
 #define tst_get_free_gid(skip) tst_get_free_gid_(__FILE__, __LINE__, (skip))
 
lib/tst_uid.c +26 -0
index af4ef8cf7..deff17085 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,31 @@
 #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;
+
+	errno = 0;
+
+	for (ret = 1; ret < MAX_UID; ret++) {
+		if (ret == skip || getpwuid(ret))
+			continue;
+
+		if (errno == 0 || errno == ENOENT || errno == ESRCH) {
+			tst_res_(file, lineno, TINFO | TERRNO,
+				"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.52.0


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

Re: [LTP] lib: New library function tst_get_free_uid

Hi Wei,

--- [PATCH 1/2] ---

Quoted 1 line · Wei Gao

[PATCH 1/2] lib: New library function tst_get_free_uid

The commit body is empty. Add at least one sentence explaining why this helper is needed — e.g. that open16 requires two distinct unprivileged UIDs and that this extends the existing tst_get_free_gid pattern to UIDs.

--- [PATCH 2/2] ---

Quoted 1 line · Wei Gao

[PATCH 2/2] open16: allow restricted O_CREAT of FIFOs and regular files

Same issue — no commit body. Describe what kernel feature is under test (protected_regular/protected_fifos, kernel commit 30aba6656f61) and why a dedicated test was needed.

Quoted 4 lines

+/*\
+ * Verify disallows open of FIFOs or regular files not owned by the user in world
+ * writable sticky directories
+ */

Incomplete. The test also verifies the positive case (protection=0 allows access) and level 2 protection (group-writable sticky directories via SAFE_CHMOD(DIR, 0020|S_ISVTX)). Update the doc comment to cover all three scenarios.

---
Note:

Our agent completed the review of the patch. The full review can be found at: https://github.com/linux-test-project/ltp-agent/actions/runs/24442661607

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
#13 lore raw

PATCH v6 2/2 open16: allow restricted O_CREAT of FIFOs and regular files

In reply to Wei Gao via ltp
---
 runtest/syscalls                          |   1 +
 testcases/kernel/syscalls/open/.gitignore |   1 +
 testcases/kernel/syscalls/open/open16.c   | 129 ++++++++++++++++++++++
 3 files changed, 131 insertions(+)
 create mode 100644 testcases/kernel/syscalls/open/open16.c
runtest/syscalls +1 -0
index d72fceb5e..c14ebb3a0 100644
@@ -1005,6 +1005,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 +129 -0

129 lines not on this page · raw message

Signature · 5 lines
2.52.0


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

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

In reply to Wei Gao via ltp

Wei Gao (2):
lib: New library function tst_get_free_uid
open16: allow restricted O_CREAT of FIFOs and regular files

v6->v7:
- Add commit body
- Update test description

 include/tst_uid.h                         |   7 +-
 lib/tst_uid.c                             |  26 +++++
 runtest/syscalls                          |   1 +
 testcases/kernel/syscalls/open/.gitignore |   1 +
 testcases/kernel/syscalls/open/open16.c   | 132 ++++++++++++++++++++++
 5 files changed, 165 insertions(+), 2 deletions(-)
 create mode 100644 testcases/kernel/syscalls/open/open16.c
Signature · 5 lines
2.52.0


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

PATCH v7 1/2 lib: New library function tst_get_free_uid

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

---
 include/tst_uid.h |  7 +++++--
 lib/tst_uid.c     | 26 ++++++++++++++++++++++++++
 2 files changed, 31 insertions(+), 2 deletions(-)
include/tst_uid.h +5 -2
index 2237ddcbf..394cb3edc 100644
@@ -8,10 +8,13 @@
 #include <sys/types.h>
 
 /*
- * Find unassigned gid. The skip argument can be used to ignore e.g. the main
+ * Find unassigned uid/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.
+ * do not need to skip any specific id, simply set it to 0.
  */
+uid_t tst_get_free_uid_(const char *file, const int lineno, uid_t skip);
+#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);
 #define tst_get_free_gid(skip) tst_get_free_gid_(__FILE__, __LINE__, (skip))
 
lib/tst_uid.c +26 -0
index af4ef8cf7..deff17085 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,31 @@
 #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;
+
+	errno = 0;
+
+	for (ret = 1; ret < MAX_UID; ret++) {
+		if (ret == skip || getpwuid(ret))
+			continue;
+
+		if (errno == 0 || errno == ENOENT || errno == ESRCH) {
+			tst_res_(file, lineno, TINFO | TERRNO,
+				"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.52.0


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

Re: [LTP] lib: New library function tst_get_free_uid

Hi Wei,

Quoted 1 line · Wei Gao

open16: allow restricted O_CREAT of FIFOs and regular files

--- [PATCH 2/2] ---

Review

Quoted 10 lines · Wei Gao

open16: allow restricted O_CREAT of FIFOs and regular files

(quoted diff) +8 -0
+	SAFE_FILE_PRINTF(PROTECTED_REGULAR, "2");
+	SAFE_FILE_PRINTF(PROTECTED_FIFOS, "2");
+	SAFE_CHMOD(DIR, 0020 | S_ISVTX);
+
+	if (!SAFE_FORK()) {
+		SAFE_SETUID(uid2);
+		TST_EXP_FAIL(openat(dir_fd, TEST_FILE, O_RDWR | O_CREAT, 0777), EACCES);
+		TST_EXP_FAIL(open(TEST_FIFO_PATH, O_RDWR | O_CREAT, 0777), EACCES);

uid2 is a free (unused) UID with no group memberships. With mode 0020|S_ISVTX the "other" bits are 0, so the kernel's DAC check rejects openat/open before reaching the protected_regular/protected_fifos logic in may_open(). EACCES fires for the wrong reason.

For a valid level-2 test the directory needs group-execute and group-write (e.g. 0770|S_ISVTX), and uid2 must be placed in the directory's owning group so it can access the directory but is still blocked by the protection check.

---
Note:

Our agent completed the review of the patch. The full review can be found at: <review_url>

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
#17 lore raw

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

In reply to Wei Gao via ltp

Wei Gao (2):
lib: New library function tst_get_free_uid
open16: allow restricted O_CREAT of FIFOs and regular files

v7->v8
- Update comments of test cases
- Refactor test case with tcase array
- Set file mode to 0030 for level2 check
- TST_EXP_FAIL->TST_EXP_FAIL2

 include/tst_uid.h                         |   7 +-
 lib/tst_uid.c                             |  26 +++++
 runtest/syscalls                          |   1 +
 testcases/kernel/syscalls/open/.gitignore |   1 +
 testcases/kernel/syscalls/open/open16.c   | 128 ++++++++++++++++++++++
 5 files changed, 161 insertions(+), 2 deletions(-)
 create mode 100644 testcases/kernel/syscalls/open/open16.c
Signature · 5 lines
2.52.0


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

PATCH v8 1/2 lib: New library function tst_get_free_uid

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

---
 include/tst_uid.h |  7 +++++--
 lib/tst_uid.c     | 26 ++++++++++++++++++++++++++
 2 files changed, 31 insertions(+), 2 deletions(-)
include/tst_uid.h +5 -2
index 2237ddcbf..394cb3edc 100644
@@ -8,10 +8,13 @@
 #include <sys/types.h>
 
 /*
- * Find unassigned gid. The skip argument can be used to ignore e.g. the main
+ * Find unassigned uid/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.
+ * do not need to skip any specific id, simply set it to 0.
  */
+uid_t tst_get_free_uid_(const char *file, const int lineno, uid_t skip);
+#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);
 #define tst_get_free_gid(skip) tst_get_free_gid_(__FILE__, __LINE__, (skip))
 
lib/tst_uid.c +26 -0
index af4ef8cf7..deff17085 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,31 @@
 #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;
+
+	errno = 0;
+
+	for (ret = 1; ret < MAX_UID; ret++) {
+		if (ret == skip || getpwuid(ret))
+			continue;
+
+		if (errno == 0 || errno == ENOENT || errno == ESRCH) {
+			tst_res_(file, lineno, TINFO | TERRNO,
+				"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.52.0


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

Re: [LTP] lib: New library function tst_get_free_uid

Hi Wei,

Quoted 4 lines · Wei Gao via ltp

[PATCH 2/2] open16: allow restricted O_CREAT of FIFOs and regular files

This commit adds test cases to verify the security restrictions for opening FIFOs and regular files in world-writable sticky directories.

The body only describes what the test does, not why it is being added. State the motivation explicitly, e.g. "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 sticky directories."

[...]

Regards,
LTP AI Reviewer

---
Note:

Our agent completed the review of the patch.

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.

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

Re: [LTP] lib: New library function tst_get_free_uid

Quoted 5 lines · On Thu, Apr 16, 2026 at 02:43:24AM +0000, [email protected]…

Hi Wei,

Quoted 2 lines · Wei Gao via ltp

[PATCH 2/2] open16: allow restricted O_CREAT of FIFOs and regular files

Quoted 8 lines
Quoted 2 lines

This commit adds test cases to verify the security restrictions for opening FIFOs and regular files in world-writable sticky directories.

The body only describes what the test does, not why it is being added. State the motivation explicitly, e.g. "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 sticky directories."

Patchset cover letter and the tags within the test code (which already reference linux-git 30aba6656f61) clearly established the purpose

Quoted 14 lines

[...]

Regards,
LTP AI Reviewer

---
Note:

Our agent completed the review of the patch.

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.

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

PATCH v8 2/2 open16: allow restricted O_CREAT of FIFOs and regular files

In reply to Wei Gao via ltp

This commit adds test cases to verify the security restrictions for opening FIFOs and regular files in world-writable sticky directories.

---
 runtest/syscalls                          |   1 +
 testcases/kernel/syscalls/open/.gitignore |   1 +
 testcases/kernel/syscalls/open/open16.c   | 128 ++++++++++++++++++++++
 3 files changed, 130 insertions(+)
 create mode 100644 testcases/kernel/syscalls/open/open16.c
runtest/syscalls +1 -0
index d72fceb5e..c14ebb3a0 100644
@@ -1005,6 +1005,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 +128 -0

128 lines not on this page · raw message

Signature · 5 lines
2.52.0


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

Re: [LTP] [PATCH v8 2/2] open16: allow restricted O_CREAT of FIFOs and regular files

Hi Wei,

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

Quoted 8 lines
testcases/kernel/syscalls/open/open16.c new file +8 -0
+
+	if (n == 2) {
+		SAFE_CHOWN(DIR, -1, gid1);
+		SAFE_CHMOD(DIR, 0030 | S_ISVTX);
+	} else {
+		SAFE_CHOWN(DIR, 0, 0);
+		SAFE_CHMOD(DIR, 0777 | S_ISVTX);
+	}

This is not convincing me. We should probably add these values inside the tcase struct and use a canary value for excluding SAFE_CHOWN/CHMOD from being used.

Regards,

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

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

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

In reply to Wei Gao via ltp

v8->v9:
- Use a canary value for excluding SAFE_CHOWN/CHMOD from being used

Wei Gao (2):
lib: New library function tst_get_free_uid
open16: allow restricted O_CREAT of FIFOs and regular files

 include/tst_uid.h                         |   7 +-
 lib/tst_uid.c                             |  26 +++++
 runtest/syscalls                          |   1 +
 testcases/kernel/syscalls/open/.gitignore |   1 +
 testcases/kernel/syscalls/open/open16.c   | 131 ++++++++++++++++++++++
 5 files changed, 164 insertions(+), 2 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
#24 lore raw

PATCH v9 1/2 lib: New library function tst_get_free_uid

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

---
 include/tst_uid.h |  7 +++++--
 lib/tst_uid.c     | 26 ++++++++++++++++++++++++++
 2 files changed, 31 insertions(+), 2 deletions(-)
include/tst_uid.h +5 -2
index 2237ddcbf..394cb3edc 100644
@@ -8,10 +8,13 @@
 #include <sys/types.h>
 
 /*
- * Find unassigned gid. The skip argument can be used to ignore e.g. the main
+ * Find unassigned uid/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.
+ * do not need to skip any specific id, simply set it to 0.
  */
+uid_t tst_get_free_uid_(const char *file, const int lineno, uid_t skip);
+#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);
 #define tst_get_free_gid(skip) tst_get_free_gid_(__FILE__, __LINE__, (skip))
 
lib/tst_uid.c +26 -0
index af4ef8cf7..deff17085 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,31 @@
 #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;
+
+	errno = 0;
+
+	for (ret = 1; ret < MAX_UID; ret++) {
+		if (ret == skip || getpwuid(ret))
+			continue;
+
+		if (errno == 0 || errno == ENOENT || errno == ESRCH) {
+			tst_res_(file, lineno, TINFO | TERRNO,
+				"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
#25 lore raw

Re: [LTP] lib: New library function tst_get_free_uid

Hi Wei,

Quoted 1 line · Wei Gao

lib: New library function tst_get_free_uid

--- [PATCH 1/2] ---

Quoted 1 line

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

The body restates what the subject already says. What scenario or test requires a dynamically allocated free UID that tst_get_uids() cannot cover?

include/tst_uid.h line 16

Quoted 2 lines
include/tst_uid.h +2 -0
+uid_t tst_get_free_uid_(const char *file, const int lineno, uid_t skip);
+#define tst_get_free_uid(skip) tst_get_free_uid_(__FILE__, __LINE__, (skip))

tst_get_free_uid_() is a new public function in lib/ and include/. LTP documentation rules require a kernel-doc /** ... */ comment for new public symbols there. Something like:

/**
* 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.
*/

Quoted 5 lines

- * Find unassigned gid. The skip argument can be used to ignore e.g. the main
+ * Find unassigned uid/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.
+ * do not need to skip any specific id, simply set it to 0.

"not listed in the group file" applies to GIDs only. UIDs live in the password database (/etc/passwd or NSS passwd). The comment should say "password or group file", or be split per-function.

lib/tst_uid.c line 28

Quoted 11 lines
lib/tst_uid.c +11 -0
+uid_t tst_get_free_uid_(const char *file, const int lineno, uid_t skip)
+{
+	uid_t ret;
+
+	errno = 0;
+
+	for (ret = 1; ret < MAX_UID; ret++) {
+		if (ret == skip || getpwuid(ret))
+			continue;
+
+		if (errno == 0 || errno == ENOENT || errno == ESRCH) {

POSIX requires callers to set errno = 0 before each call to getpwuid() so that a NULL return with non-zero errno means error rather than "entry not found". The reset here is done once before the loop. If a successful getpwuid() call for an earlier UID leaves errno non-zero (POSIX does not guarantee it is cleared on success), the next NULL-returning call could be misclassified as an error and trigger the tst_brk() path below.

A per-iteration reset:

  errno = 0;
  if (ret == skip || getpwuid(ret))
      continue;

would match the POSIX-recommended pattern and make the intent clear.

Verdict - Needs revision

Pre-existing issues:

tst_get_free_gid_() in lib/tst_uid.c has the same errno-reset issue described above (errno set once before the loop, not before each getgrgid() call).

---
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
#26 lore raw

PATCH v9 2/2 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 sticky directories.

This commit adds test cases to verify these security restrictions for opening FIFOs and regular files in world-writable sticky directories.

---
 runtest/syscalls                          |   1 +
 testcases/kernel/syscalls/open/.gitignore |   1 +
 testcases/kernel/syscalls/open/open16.c   | 131 ++++++++++++++++++++++
 3 files changed, 133 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 +131 -0

131 lines not on this page · raw message

Signature · 5 lines
2.54.0


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

PATCH v7 2/2 open16: allow restricted O_CREAT of FIFOs and regular files

In reply to Wei Gao via ltp

This commit adds test cases to verify the security restrictions for opening FIFOs and regular files in world-writable sticky directories.

---
 runtest/syscalls                          |   1 +
 testcases/kernel/syscalls/open/.gitignore |   1 +
 testcases/kernel/syscalls/open/open16.c   | 132 ++++++++++++++++++++++
 3 files changed, 134 insertions(+)
 create mode 100644 testcases/kernel/syscalls/open/open16.c
runtest/syscalls +1 -0
index d72fceb5e..c14ebb3a0 100644
@@ -1005,6 +1005,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 +132 -0

132 lines not on this page · raw message

Signature · 5 lines
2.52.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp