Skip to contentSkip to conversation

open16: allow restricted O_CREAT of FIFOs and regular files

PATCH Superseded Wei Gao via ltp 4 participants 14 messages Oct 22 – Oct 26 Library code

v16 of 17 Superseded Latest is v17 →

lore
Share & tools
lore
#0 lore raw

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

v15->v16:
Remove change for tst_get_free_gid_

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

 include/tst_path_defs.h                   |   2 +
 include/tst_uid.h                         |  14 +++
 lib/tst_uid.c                             |  28 +++++
 runtest/syscalls                          |   1 +
 testcases/kernel/syscalls/open/.gitignore |   1 +
 testcases/kernel/syscalls/open/open16.c   | 143 ++++++++++++++++++++++
 6 files changed, 189 insertions(+)
 create mode 100644 testcases/kernel/syscalls/open/open16.c
Signature · 5 lines
2.55.0


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

PATCH v16 1/2 lib: New library function tst_get_free_uid

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 | 14 ++++++++++++++
 lib/tst_uid.c     | 28 ++++++++++++++++++++++++++++
 2 files changed, 42 insertions(+)
include/tst_uid.h +14 -0
index 2237ddcbf..03f9dc073 100644
@@ -7,6 +7,20 @@
 
 #include <sys/types.h>
 
+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))
+
 /*
  * 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
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.55.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#2 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

Verdict - Reviewed

Runtime validation was not performed because access to the required sysctls was denied.

---
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
Reviewed-by on v16 1/2
#3 lore raw

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

In reply to Wei Gao via ltp

Hi Wei,

Quoted 36 lines
include/tst_uid.h +14 -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))
+
 /*
  * 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
lib/tst_uid.c +4 -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)

lib/tst_uid.c line 28

Quoted 10 lines
lib/tst_uid.c +10 -0
+{
+	uid_t ret;
+
+	for (ret = 1; ret < MAX_UID; ret++) {
+		if (ret == skip)
+			continue;
+
+		errno = 0;
+		if (getpwuid(ret))
+			continue;

If you don't mind I'd prefer to use the same approach which is on tst_get_free_gid_(), e.g. set errno first:

	errno = 0;

	for (ret = 1; ret < MAX_UID; ret++) {
		if (ret == skip || getpwuid(ret))
			continue;

Other than that LGTM.

Cc Martin in case he wants to add his ack before I merge.

Kind regards,
Petr

Quoted 14 lines
lib/tst_uid.c +14 -0
+
+		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;
+}
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#4 lore raw

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

Quoted 63 lines

Hi Wei,

Quoted 36 lines
include/tst_uid.h +14 -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))
+
 /*
  * 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
lib/tst_uid.c +4 -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)

lib/tst_uid.c line 28

Quoted 10 lines
lib/tst_uid.c +10 -0
+{
+	uid_t ret;
+
+	for (ret = 1; ret < MAX_UID; ret++) {
+		if (ret == skip)
+			continue;
+
+		errno = 0;
+		if (getpwuid(ret))
+			continue;

If you don't mind I'd prefer to use the same approach which is on tst_get_free_gid_(), e.g. set errno first:

	errno = 0;

	for (ret = 1; ret < MAX_UID; ret++) {
		if (ret == skip || getpwuid(ret))
			continue;

Other than that LGTM.

Quoted 8 lines

Cc Martin in case he wants to add his ack before I merge.

Kind regards,
Petr

lib/tst_uid.c line 31

Quoted 3 lines
lib/tst_uid.c +3 -0
+
+		if (errno == 0 || errno == ENOENT || errno == ESRCH) {
+			tst_res_(file, lineno, TINFO,

Actually also here TERRNO probably makes sense.

I suggest to merge with following diff:

+++ lib/tst_uid.c
@@ -19,16 +19,14 @@ 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;

- errno = 0;
- if (getpwuid(ret))
+ 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,
+ tst_res_(file, lineno, TINFO | TERRNO,
"Found unused UID %d", (int)ret);
return ret;
}

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

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

Quoted 68 lines · Petr Vorel
Quoted 67 lines

Hi Wei,

Quoted 36 lines
include/tst_uid.h +14 -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))
+
 /*
  * 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
lib/tst_uid.c +4 -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)

lib/tst_uid.c line 28

Quoted 10 lines
lib/tst_uid.c +10 -0
+{
+	uid_t ret;
+
+	for (ret = 1; ret < MAX_UID; ret++) {
+		if (ret == skip)
+			continue;
+
+		errno = 0;
+		if (getpwuid(ret))
+			continue;

If you don't mind I'd prefer to use the same approach which is on tst_get_free_gid_(), e.g. set errno first:

	errno = 0;

	for (ret = 1; ret < MAX_UID; ret++) {
		if (ret == skip || getpwuid(ret))
			continue;

Other than that LGTM.

Cc Martin in case he wants to add his ack before I merge.

Kind regards,
Petr

Quoted 6 lines
Quoted 3 lines

lib/tst_uid.c line 31

Quoted 3 lines
lib/tst_uid.c +3 -0
+
+		if (errno == 0 || errno == ENOENT || errno == ESRCH) {
+			tst_res_(file, lineno, TINFO,

Actually also here TERRNO probably makes sense.

I suggest to merge with following diff:

Thanks. That's ok for me, will use your suggest on next patch once Martin give feedback.

Quoted 23 lines

+++ lib/tst_uid.c

(quoted diff) +4 -6
@@ -19,16 +19,14 @@ 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;
 
-		errno = 0;
-		if (getpwuid(ret))
+	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,
+			tst_res_(file, lineno, TINFO | TERRNO,
 				"Found unused UID %d", (int)ret);
 			return ret;
 		}
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#6 lore raw

PATCH v16 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 or group-writable sticky directories.

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

---
 include/tst_path_defs.h                   |   2 +
 runtest/syscalls                          |   1 +
 testcases/kernel/syscalls/open/.gitignore |   1 +
 testcases/kernel/syscalls/open/open16.c   | 143 ++++++++++++++++++++++
 4 files changed, 147 insertions(+)
 create mode 100644 testcases/kernel/syscalls/open/open16.c
include/tst_path_defs.h +2 -0
index 1a60028d3..2995150be 100644
@@ -47,6 +47,8 @@
 #define PATH_FS_PIPE_MAX_PAGES			"/proc/sys/fs/pipe-max-pages"
 #define PATH_FS_MAX_USER_GROUPS			"/proc/sys/fs/fanotify/max_user_groups"
 #define PATH_FS_MAX_USER_MARKS			"/proc/sys/fs/fanotify/max_user_marks"
+#define PATH_FS_PROTECTED_REGULAR		"/proc/sys/fs/protected_regular"
+#define PATH_FS_PROTECTED_FIFOS			"/proc/sys/fs/protected_fifos"
 
 /* VM */
 #define PATH_VM_DROP_CACHES			"/proc/sys/vm/drop_caches"
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 +143 -0

143 lines not on this page · raw message

Signature · 5 lines
2.55.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by on v16 2/2
#7 lore raw

Hi Wei,

...

Quoted 23 lines
testcases/kernel/syscalls/open/open16.c new file +23 -0
+static void verify_open(unsigned int n)
+{
+	struct tcase *tc = &tcases[n];
+
+	SAFE_FILE_PRINTF(PATH_FS_PROTECTED_REGULAR, "%u", n);
+	SAFE_FILE_PRINTF(PATH_FS_PROTECTED_FIFOS, "%u", n);
+
+	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);
+
+	if (SAFE_FORK())
+		return;
+
+	SAFE_SETGID(gid1);
+	SAFE_SETUID(uid2);
+
+	TST_EXP_FD_OR_FAIL(openat(dir_fd, TEST_FILE, O_RDWR | O_CREAT, 0777),

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

Quoted 10 lines
testcases/kernel/syscalls/open/open16.c new file +10 -0
+			   tc->exp_errno, "openat %s (sysctl_val=%u)", TEST_FILE, n);
+	if (TST_RET != -1)
+		SAFE_CLOSE(TST_RET);
+
+	TST_EXP_FD_OR_FAIL(open(TEST_FIFO_PATH, O_RDWR | O_CREAT, 0777),
+			   tc->exp_errno, "open %s (sysctl_val=%u)", TEST_FIFO, n);
+	if (TST_RET != -1)
+		SAFE_CLOSE(TST_RET);
+
+	exit(0);

Do we need this explicit exit?

The rest LGTM.

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#8 lore raw
Quoted 30 lines · Petr Vorel

Hi Wei,

...

Quoted 27 lines
testcases/kernel/syscalls/open/open16.c new file +27 -0
+static void verify_open(unsigned int n)
+{
+	struct tcase *tc = &tcases[n];
+
+	SAFE_FILE_PRINTF(PATH_FS_PROTECTED_REGULAR, "%u", n);
+	SAFE_FILE_PRINTF(PATH_FS_PROTECTED_FIFOS, "%u", n);
+
+	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);
+
+	if (SAFE_FORK())
+		return;
+
+	SAFE_SETGID(gid1);
+	SAFE_SETUID(uid2);
+
+	TST_EXP_FD_OR_FAIL(openat(dir_fd, TEST_FILE, O_RDWR | O_CREAT, 0777),
+			   tc->exp_errno, "openat %s (sysctl_val=%u)", TEST_FILE, n);
+	if (TST_RET != -1)
+		SAFE_CLOSE(TST_RET);
+
Quoted 8 lines

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

Quoted 6 lines
testcases/kernel/syscalls/open/open16.c new file +6 -0
+	TST_EXP_FD_OR_FAIL(open(TEST_FIFO_PATH, O_RDWR | O_CREAT, 0777),
+			   tc->exp_errno, "open %s (sysctl_val=%u)", TEST_FIFO, n);
+	if (TST_RET != -1)
+		SAFE_CLOSE(TST_RET);
+
+	exit(0);

Do we need this explicit exit?

Yes i think exit(0) is necessary because only the child process reaches this point.

Quoted 6 lines

The rest LGTM.

Kind regards,
Petr

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

Hi!

Quoted 4 lines
Quoted 3 lines

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

Quoted 1 line
testcases/kernel/syscalls/open/open16.c new file +1 -0
+	exit(0);

Do we need this explicit exit?

Yes i think exit(0) is necessary because only the child process reaches this point.

Any child that returns to the test library calls exit(0).

Signature · 5 lines
Cyril Hrubis
[email protected]

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#10 lore raw
Quoted 7 lines · Cyril Hrubis

Hi!

Quoted 4 lines
Quoted 3 lines

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

Quoted 1 line
testcases/kernel/syscalls/open/open16.c new file +1 -0
+	exit(0);

Do we need this explicit exit?

Yes i think exit(0) is necessary because only the child process reaches this point.

Any child that returns to the test library calls exit(0).

Thanks Cyril and Petr for pointing this out, and sorry for the oversight on my part. For above case SAFE_FORK() returns early in the parent process, only the child reaches this last point and we do not need duplicate exit(0) since child will encounter run_tests check if (tst_getpid() != context->main_pid) and do exit(0).

Will update it in next patch.

Quoted 4 lines
Signature · 3 lines
-- 
Cyril Hrubis
[email protected]
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#11 lore raw
In reply to Cyril Hrubis
Quoted 7 lines

Hi!

Quoted 4 lines
Quoted 3 lines

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

Quoted 1 line
testcases/kernel/syscalls/open/open16.c new file +1 -0
+	exit(0);

Do we need this explicit exit?

Yes i think exit(0) is necessary because only the child process reaches this point.

Any child that returns to the test library calls exit(0).

Thanks, Cyril, for confirmation.

Yet another rule
https://github.com/linux-test-project/ltp-agent/pull/12

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#12 lore raw
In reply to Wei Gao via ltp

Hi Wei,

include/tst_path_defs.h line 51

Quoted 10 lines
include/tst_path_defs.h +2 -0
index 1a60028d3..2995150be 100644
@@ -47,6 +47,8 @@
 #define PATH_FS_PIPE_MAX_PAGES			"/proc/sys/fs/pipe-max-pages"
 #define PATH_FS_MAX_USER_GROUPS			"/proc/sys/fs/fanotify/max_user_groups"
 #define PATH_FS_MAX_USER_MARKS			"/proc/sys/fs/fanotify/max_user_marks"
+#define PATH_FS_PROTECTED_REGULAR		"/proc/sys/fs/protected_regular"
+#define PATH_FS_PROTECTED_FIFOS			"/proc/sys/fs/protected_fifos"

I just figured out that these were added in the same commit that is modifying open16. They should be inside an another commit.

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

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#13 lore raw
Quoted 15 lines · Andrea Cervesato

Hi Wei,

include/tst_path_defs.h line 51

Quoted 10 lines
include/tst_path_defs.h +2 -0
index 1a60028d3..2995150be 100644
@@ -47,6 +47,8 @@
 #define PATH_FS_PIPE_MAX_PAGES			"/proc/sys/fs/pipe-max-pages"
 #define PATH_FS_MAX_USER_GROUPS			"/proc/sys/fs/fanotify/max_user_groups"
 #define PATH_FS_MAX_USER_MARKS			"/proc/sys/fs/fanotify/max_user_marks"
+#define PATH_FS_PROTECTED_REGULAR		"/proc/sys/fs/protected_regular"
+#define PATH_FS_PROTECTED_FIFOS			"/proc/sys/fs/protected_fifos"

I just figured out that these were added in the same commit that is modifying open16. They should be inside an another commit.

Thanks for point this , will update in next patch :)

Quoted 6 lines

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

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