Skip to contentSkip to conversation

open16: allow restricted O_CREAT of FIFOs and regular files

PATCH In review Wei Gao via ltp 2 participants 5 messages Oct 27 Library code

lore
Share & tools
lore
#0 lore raw

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

v16->v17:
- lib/tst_uid.c: Align tst_get_free_uid() with tst_get_free_gid_() by setting errno = 0 before the loop and combining the check
(ret == skip || getpwuid(ret)).
- lib/tst_uid.c: Add TERRNO flag (TINFO | TERRNO) when reporting unused UID.
- include/tst_path_defs.h: Split PATH_FS_PROTECTED_REGULAR and PATH_FS_PROTECTED_FIFOS into a separate patch preceding open16.
- open16.c: Remove redundant exit(0) call at the end of verify_open().
- Add Reviewed-by tags from Petr Vorel.

Wei Gao (3):
lib: New library function tst_get_free_uid
lib: Add PATH_FS_PROTECTED_REGULAR and PATH_FS_PROTECTED_FIFOS
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                             |  26 ++++
 runtest/syscalls                          |   1 +
 testcases/kernel/syscalls/open/.gitignore |   1 +
 testcases/kernel/syscalls/open/open16.c   | 141 ++++++++++++++++++++++
 6 files changed, 185 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 v17 1/3 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.

---
v16->v17:
- Align with tst_get_free_gid_() pattern: set errno = 0 before the loop and combine the check (ret == skip || getpwuid(ret)).
- Add TERRNO flag (TINFO | TERRNO) when reporting unused UID.
- Add Reviewed-by: Petr Vorel <[email protected]>.

 include/tst_uid.h | 14 ++++++++++++++
 lib/tst_uid.c     | 26 ++++++++++++++++++++++++++
 2 files changed, 40 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 +26 -0
index af4ef8cf7..09a1450ca 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.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

---
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

PATCH v17 2/3 lib: Add PATH_FS_PROTECTED_REGULAR and PATH_FS_PROTECTED_FIFOS

In reply to Wei Gao via ltp

Add /proc/sys/fs/protected_regular and /proc/sys/fs/protected_fifos path definitions to tst_path_defs.h for sysctl configuration.

These sysctls control protections against spoofing attacks when opening regular files and FIFOs in sticky directories, and will be used by tests verifying this behavior.

---
v16->v17:
- New patch in v17: split PATH_FS_PROTECTED_* definitions from open16 into a separate commit.

 include/tst_path_defs.h | 2 ++
 1 file changed, 2 insertions(+)
include/tst_path_defs.h +2 -0
index ecee8d426..a0ac978e1 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"
Signature · 5 lines
2.55.0


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

PATCH v17 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 (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.

---
v16->v17:
- Move PATH_FS_PROTECTED_* definitions into a separate preceding commit.
- Remove redundant exit(0) call at the end of verify_open().
- Add Reviewed-by: Petr Vorel <[email protected]>.

 runtest/syscalls                          |   1 +
 testcases/kernel/syscalls/open/.gitignore |   1 +
 testcases/kernel/syscalls/open/open16.c   | 141 ++++++++++++++++++++++
 3 files changed, 143 insertions(+)
 create mode 100644 testcases/kernel/syscalls/open/open16.c
runtest/syscalls +1 -0
index 2bdb4a9a2..eb63511f3 100644
@@ -1062,6 +1062,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 +141 -0

141 lines not on this page · raw message

Signature · 5 lines
2.55.0


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