Skip to contentSkip to conversation

implement OA2_CRED_INHERIT flag for openat2()

PATCH In review Stas Sergeev 4 participants 12 messages Apr 27, 2024 – May 21, 2024

Subsystems (8)
lore
Share & tools
lore
#0 lore raw

v6 cover implement OA2_CRED_INHERIT flag for openat2()

This patch-set implements the OA2_CRED_INHERIT flag for openat2() syscall. It is needed to perform an open operation with the creds that were in effect when the dir_fd was opened, if the dir was opened with O_CRED_ALLOW flag. This allows the process to pre-open some dirs and switch eUID (and other UIDs/GIDs) to the less-privileged user, while still retaining the possibility to open/create files within the pre-opened directory set.

The sand-boxing is security-oriented: symlinks leading outside of a sand-box are rejected. /proc magic links are rejected. fds opened with O_CRED_ALLOW are always closed on exec() and cannot be passed via unix socket.
The more detailed description (including security considerations) is available in the log messages of individual patches.

Changes in v6:
- it appears open flags bit 23 is already taken on parisc, and bit 24 is taken on alpha. Move O_CRED_ALLOW to bit 25.
- added selftests for both O_CRED_ALLOW and O_CRED_INHERIT additions

Changes in v5:
- rename OA2_INHERIT_CRED to OA2_CRED_INHERIT
- add an "opt-in" flag O_CRED_ALLOW as was suggested by many reviewers
- stop using 64bit types, as suggested by
Christian Brauner <[email protected]>
- add BUILD_BUG_ON() for VALID_OPENAT2_FLAGS, based on Christian Brauner's comments
- fixed problems reported by patch-testing bot
- made O_CRED_ALLOW fds not passable via unix sockets and exec(), based on Christian Brauner's comments

Changes in v4:
- add optimizations suggested by David Laight <[email protected]>
- move security checks to build_open_flags()
- force RESOLVE_NO_MAGICLINKS as suggested by Andy Lutomirski <[email protected]>

Changes in v3:
- partially revert v2 changes to avoid overriding capabilities. Only the bare minimum is overridden: fsuid, fsgid and group_info. Document the fact the full cred override is unwanted, as it may represent an unneeded security risk.

Changes in v2:
- capture full struct cred instead of just fsuid/fsgid. Suggested by Stefan Metzmacher <[email protected]>

CC Stefan Metzmacher <[email protected]>
CC Eric Biederman <[email protected]>
CC Alexander Viro <[email protected]>
CC Andy Lutomirski <[email protected]>
CC Christian Brauner <[email protected]>
CC Jan Kara <[email protected]>
CC Jeff Layton <[email protected]>
CC Chuck Lever <[email protected]>
CC Alexander Aring <[email protected]>
CC David Laight <[email protected]>
CC Paolo Bonzini <[email protected]>
CC Christian Göttsche <[email protected]>
Signature · 1 line
2.44.0
#1 lore raw

PATCH v6 1/3 fs: reorganize path_openat()

This patch moves the call to alloc_empty_file() down the if branches. That changes is needed for the next patch, which adds a cred override for alloc_empty_file(). The cred override is only needed in one branch, i.e. it is not needed for O_PATH and O_TMPFILE..

No functional changes are intended by that patch.

CC Eric Biederman <[email protected]>
CC Alexander Viro <[email protected]>
CC Christian Brauner <[email protected]>
CC Jan Kara <[email protected]>
CC Andy Lutomirski <[email protected]>
CC David Laight <[email protected]>
---
 fs/namei.c | 25 ++++++++++++++++---------
 1 file changed, 16 insertions(+), 9 deletions(-)
fs/namei.c +16 -9
index c5b2a25be7d0..dd50345f7260 100644
@@ -3781,17 +3781,24 @@ static struct file *path_openat(struct nameidata *nd,
 {
 	struct file *file;
 	int error;
+	int open_flags = op->open_flag;
 
-	file = alloc_empty_file(op->open_flag, current_cred());
-	if (IS_ERR(file))
-		return file;
-
-	if (unlikely(file->f_flags & __O_TMPFILE)) {
-		error = do_tmpfile(nd, flags, op, file);
-	} else if (unlikely(file->f_flags & O_PATH)) {
-		error = do_o_path(nd, flags, file);
+	if (unlikely(open_flags & (__O_TMPFILE | O_PATH))) {
+		file = alloc_empty_file(open_flags, current_cred());
+		if (IS_ERR(file))
+			return file;
+		if (open_flags & __O_TMPFILE)
+			error = do_tmpfile(nd, flags, op, file);
+		else
+			error = do_o_path(nd, flags, file);
 	} else {
-		const char *s = path_init(nd, flags);
+		const char *s;
+
+		file = alloc_empty_file(open_flags, current_cred());
+		if (IS_ERR(file))
+			return file;
+
+		s = path_init(nd, flags);
 		while (!(error = link_path_walk(s, nd)) &&
 		       (s = open_last_lookups(nd, file, op)) != NULL)
 			;
Signature · 1 line
2.44.0
#2 lore raw

PATCH v6 2/3 open: add O_CRED_ALLOW flag

In reply to Stas Sergeev

This flag prevents an fd from being passed via unix socket, and makes it to be always closed on exec().

Selftest is added to check for both properties.

It is needed for the subsequent OA2_CRED_INHERIT addition, to work as an "opt-in" for the new cred-inherit functionality. Without using O_CRED_ALLOW when opening dir fd, OA2_CRED_INHERIT is going to return EPERM.

CC Eric Biederman <[email protected]>
CC Alexander Viro <[email protected]>
CC Christian Brauner <[email protected]>
CC Jan Kara <[email protected]>
CC Andy Lutomirski <[email protected]>
CC David Laight <[email protected]>
CC Arnd Bergmann <[email protected]>
CC "David S. Miller" <[email protected]>
CC Eric Dumazet <[email protected]>
CC Jakub Kicinski <[email protected]>
CC Paolo Abeni <[email protected]>
CC Jens Axboe <[email protected]>
CC Kuniyuki Iwashima <[email protected]>
CC Pavel Begunkov <[email protected]>
---
 fs/fcntl.c                                |   2 +-
 fs/file.c                                 |  15 +--
 include/linux/fcntl.h                     |   2 +-
 include/uapi/asm-generic/fcntl.h          |   5 +
 net/core/scm.c                            |   5 +
 tools/testing/selftests/core/Makefile     |   2 +-
 tools/testing/selftests/core/cred_allow.c | 139 ++++++++++++++++++++++
 7 files changed, 160 insertions(+), 10 deletions(-)
 create mode 100644 tools/testing/selftests/core/cred_allow.c
fs/fcntl.c +1 -1
index 54cc85d3338e..78c96b1293c2 100644
@@ -1039,7 +1039,7 @@ static int __init fcntl_init(void)
 	 * Exceptions: O_NONBLOCK is a two bit define on parisc; O_NDELAY
 	 * is defined as O_NONBLOCK on some platforms and not on others.
 	 */
-	BUILD_BUG_ON(21 - 1 /* for O_RDONLY being 0 */ !=
+	BUILD_BUG_ON(22 - 1 /* for O_RDONLY being 0 */ !=
 		HWEIGHT32(
 			(VALID_OPEN_FLAGS & ~(O_NONBLOCK | O_NDELAY)) |
 			__FMODE_EXEC | __FMODE_NONOTIFY));
fs/file.c +8 -7
index 3b683b9101d8..2a09d5276676 100644
@@ -827,22 +827,23 @@ void do_close_on_exec(struct files_struct *files)
 	/* exec unshares first */
 	spin_lock(&files->file_lock);
 	for (i = 0; ; i++) {
+		int j;
 		unsigned long set;
 		unsigned fd = i * BITS_PER_LONG;
 		fdt = files_fdtable(files);
 		if (fd >= fdt->max_fds)
 			break;
 		set = fdt->close_on_exec[i];
-		if (!set)
-			continue;
 		fdt->close_on_exec[i] = 0;
-		for ( ; set ; fd++, set >>= 1) {
-			struct file *file;
-			if (!(set & 1))
-				continue;
-			file = fdt->fd[fd];
+		for (j = 0; j < BITS_PER_LONG; j++, fd++, set >>= 1) {
+			struct file *file = fdt->fd[fd];
 			if (!file)
 				continue;
+			/* Close all cred-allow files. */
+			if (file->f_flags & O_CRED_ALLOW)
+				set |= 1;
+			if (!(set & 1))
+				continue;
 			rcu_assign_pointer(fdt->fd[fd], NULL);
 			__put_unused_fd(files, fd);
 			spin_unlock(&files->file_lock);
include/linux/fcntl.h +1 -1
index a332e79b3207..e074ee9c1e36 100644
@@ -10,7 +10,7 @@
 	(O_RDONLY | O_WRONLY | O_RDWR | O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC | \
 	 O_APPEND | O_NDELAY | O_NONBLOCK | __O_SYNC | O_DSYNC | \
 	 FASYNC	| O_DIRECT | O_LARGEFILE | O_DIRECTORY | O_NOFOLLOW | \
-	 O_NOATIME | O_CLOEXEC | O_PATH | __O_TMPFILE)
+	 O_NOATIME | O_CLOEXEC | O_PATH | __O_TMPFILE | O_CRED_ALLOW)
 
 /* List of all valid flags for the how->resolve argument: */
 #define VALID_RESOLVE_FLAGS \
include/uapi/asm-generic/fcntl.h +5 -0
index 80f37a0d40d7..9244c54bb933 100644
@@ -89,6 +89,11 @@
 #define __O_TMPFILE	020000000
 #endif
 
+#ifndef O_CRED_ALLOW
+/* On parisc bit 23 is taken. On alpha bit 24 is also taken. Try bit 25. */
+#define O_CRED_ALLOW	0200000000
+#endif
+
 /* a horrid kludge trying to make sure that this will fail on old kernels */
 #define O_TMPFILE (__O_TMPFILE | O_DIRECTORY)
 
net/core/scm.c +5 -0
index 9cd4b0a01cd6..f54fb0ee9727 100644
@@ -111,6 +111,11 @@ static int scm_fp_copy(struct cmsghdr *cmsg, struct scm_fp_list **fplp)
 			fput(file);
 			return -EINVAL;
 		}
+		/* don't allow files with creds */
+		if (file->f_flags & O_CRED_ALLOW) {
+			fput(file);
+			return -EPERM;
+		}
 		if (unix_get_socket(file))
 			fpl->count_unix++;
 
tools/testing/selftests/core/Makefile +1 -1
index ce262d097269..347a5a9d3f29 100644
@@ -1,7 +1,7 @@
 # SPDX-License-Identifier: GPL-2.0-only
 CFLAGS += -g $(KHDR_INCLUDES)
 
-TEST_GEN_PROGS := close_range_test
+TEST_GEN_PROGS := close_range_test cred_allow
 
 include ../lib.mk
 
tools/testing/selftests/core/cred_allow.c new file +139 -0

139 lines not on this page · raw message

Signature · 1 line
2.44.0
#3 lore raw

PATCH v6 3/3 openat2: add OA2_CRED_INHERIT flag

In reply to Stas Sergeev

This flag performs the open operation with the fs credentials (fsuid, fsgid, group_info) that were in effect when dir_fd was opened. dir_fd must be opened with O_CRED_ALLOW, or EPERM is returned.

Selftests are added to check for these properties as well as for the invalid flag combinations.

This allows the process to pre-open some directories and then change eUID (and all other UIDs/GIDs) to a less-privileged user, retaining the ability to open/create files within these directories.

Design goal:
The idea is to provide a very light-weight sandboxing, where the process, without the use of any heavy-weight techniques like chroot within namespaces, can restrict the access to the set of pre-opened directories.
This patch is just a first step to such sandboxing. If things go well, in the future the same extension can be added to more syscalls. These should include at least unlinkat(), renameat2() and the not-yet-upstreamed setxattrat().

Security considerations:
- Only the bare minimal set of credentials is overridden:
fsuid, fsgid and group_info. The rest, for example capabilities, are not overridden to avoid unneeded security risks.
- To avoid sandboxing escape, this patch makes sure the restricted lookup modes are used. Namely, RESOLVE_BENEATH or RESOLVE_IN_ROOT.
- Magic /proc symlinks are discarded, as suggested by
Andy Lutomirski <[email protected]>
- O_CRED_ALLOW fds cannot be passed via unix socket and are always closed on exec() to prevent "unsuspecting userspace" from not being able to fully drop privs.

Use cases:
Virtual machines that deal with untrusted code, can use that instead of a more heavy-weighted approaches. Currently the approach is being tested on a dosemu2 VM.

CC Stefan Metzmacher <[email protected]>
CC Eric Biederman <[email protected]>
CC Alexander Viro <[email protected]>
CC Andy Lutomirski <[email protected]>
CC Christian Brauner <[email protected]>
CC Jan Kara <[email protected]>
CC Jeff Layton <[email protected]>
CC Chuck Lever <[email protected]>
CC Alexander Aring <[email protected]>
CC Paolo Bonzini <[email protected]>
CC Christian Göttsche <[email protected]>
---
 fs/fcntl.c                                    |   2 +
 fs/namei.c                                    |  56 +++++++++-
 fs/open.c                                     |  10 +-
 include/linux/fcntl.h                         |   2 +
 include/uapi/linux/openat2.h                  |   2 +
 tools/testing/selftests/openat2/Makefile      |   2 +-
 .../testing/selftests/openat2/cred_inherit.c  | 105 ++++++++++++++++++
 .../testing/selftests/openat2/openat2_test.c  |  12 +-
 8 files changed, 186 insertions(+), 5 deletions(-)
 create mode 100644 tools/testing/selftests/openat2/cred_inherit.c
fs/fcntl.c +2 -0
index 78c96b1293c2..283c2e65fc2c 100644
@@ -1043,6 +1043,8 @@ static int __init fcntl_init(void)
 		HWEIGHT32(
 			(VALID_OPEN_FLAGS & ~(O_NONBLOCK | O_NDELAY)) |
 			__FMODE_EXEC | __FMODE_NONOTIFY));
+	BUILD_BUG_ON(HWEIGHT32(VALID_OPENAT2_FLAGS) !=
+			HWEIGHT32(VALID_OPEN_FLAGS) + 1);
 
 	fasync_cache = kmem_cache_create("fasync_cache",
 					 sizeof(struct fasync_struct), 0,
fs/namei.c +54 -2

78 lines not on this page · raw message

fs/open.c +9 -1
index ee8460c83c77..dd4fab536135 100644
@@ -1225,7 +1225,7 @@ inline int build_open_flags(const struct open_how *how, struct open_flags *op)
 	 * values before calling build_open_flags(), but openat2(2) checks all
 	 * of its arguments.
 	 */
-	if (flags & ~VALID_OPEN_FLAGS)
+	if (flags & ~VALID_OPENAT2_FLAGS)
 		return -EINVAL;
 	if (how->resolve & ~VALID_RESOLVE_FLAGS)
 		return -EINVAL;
@@ -1326,6 +1326,14 @@ inline int build_open_flags(const struct open_how *how, struct open_flags *op)
 		lookup_flags |= LOOKUP_CACHED;
 	}
 
+	if (flags & OA2_CRED_INHERIT) {
+		/* Inherit creds only with scoped look-up modes. */
+		if (!(lookup_flags & LOOKUP_IS_SCOPED))
+			return -EPERM;
+		/* Reject /proc "magic" links if inheriting creds. */
+		lookup_flags |= LOOKUP_NO_MAGICLINKS;
+	}
+
 	op->lookup_flags = lookup_flags;
 	return 0;
 }
include/linux/fcntl.h +2 -0
index e074ee9c1e36..33b9c7ad056b 100644
@@ -12,6 +12,8 @@
 	 FASYNC	| O_DIRECT | O_LARGEFILE | O_DIRECTORY | O_NOFOLLOW | \
 	 O_NOATIME | O_CLOEXEC | O_PATH | __O_TMPFILE | O_CRED_ALLOW)
 
+#define VALID_OPENAT2_FLAGS (VALID_OPEN_FLAGS | OA2_CRED_INHERIT)
+
 /* List of all valid flags for the how->resolve argument: */
 #define VALID_RESOLVE_FLAGS \
 	(RESOLVE_NO_XDEV | RESOLVE_NO_MAGICLINKS | RESOLVE_NO_SYMLINKS | \
include/uapi/linux/openat2.h +2 -0
index a5feb7604948..f803558ad62f 100644
@@ -40,4 +40,6 @@ struct open_how {
 					return -EAGAIN if that's not
 					possible. */
 
+#define OA2_CRED_INHERIT		(1UL << 28)
+
 #endif /* _UAPI_LINUX_OPENAT2_H */
tools/testing/selftests/openat2/Makefile +1 -1
index 254d676a2689..a1f4b5395f82 100644
@@ -1,7 +1,7 @@
 # SPDX-License-Identifier: GPL-2.0-or-later
 
 CFLAGS += -Wall -O2 -g -fsanitize=address -fsanitize=undefined -static-libasan
-TEST_GEN_PROGS := openat2_test resolve_test rename_attack_test
+TEST_GEN_PROGS := openat2_test resolve_test rename_attack_test cred_inherit
 
 include ../lib.mk
 
tools/testing/selftests/openat2/cred_inherit.c new file +105 -0

105 lines not on this page · raw message

tools/testing/selftests/openat2/openat2_test.c +11 -1
index 9024754530b2..5095288fe1ac 100644
@@ -28,6 +28,10 @@
 #define	O_LARGEFILE 0x8000
 #endif
 
+#ifndef OA2_CRED_INHERIT
+#define OA2_CRED_INHERIT (1UL << 28)
+#endif
+
 struct open_how_ext {
 	struct open_how inner;
 	uint32_t extra1;
@@ -159,7 +163,7 @@ struct flag_test {
 	int err;
 };
 
-#define NUM_OPENAT2_FLAG_TESTS 25
+#define NUM_OPENAT2_FLAG_TESTS 27
 
 void test_openat2_flags(void)
 {
@@ -233,6 +237,12 @@ void test_openat2_flags(void)
 		{ .name = "invalid how.resolve and O_PATH",
 		  .how.flags = O_PATH,
 		  .how.resolve = 0x1337, .err = -EINVAL },
+		{ .name = "invalid how.resolve and OA2_CRED_INHERIT",
+		  .how.flags = OA2_CRED_INHERIT,
+		  .how.resolve = 0, .err = -EPERM },
+		{ .name = "invalid AT_FDCWD and OA2_CRED_INHERIT",
+		  .how.flags = OA2_CRED_INHERIT,
+		  .how.resolve = 0x08, .err = -EINVAL },
 
 		/* currently unknown upper 32 bit rejected. */
 		{ .name = "currently unknown bit (1 << 63)",
Signature · 1 line
2.44.0
#4 lore raw

Re: [PATCH v6 3/3] openat2: add OA2_CRED_INHERIT flag

Quoted 21 lines · Stas Sergeev

This flag performs the open operation with the fs credentials (fsuid, fsgid, group_info) that were in effect when dir_fd was opened. dir_fd must be opened with O_CRED_ALLOW, or EPERM is returned.

Selftests are added to check for these properties as well as for the invalid flag combinations.

This allows the process to pre-open some directories and then change eUID (and all other UIDs/GIDs) to a less-privileged user, retaining the ability to open/create files within these directories.

Design goal:
The idea is to provide a very light-weight sandboxing, where the process, without the use of any heavy-weight techniques like chroot within namespaces, can restrict the access to the set of pre-opened directories.
This patch is just a first step to such sandboxing. If things go well, in the future the same extension can be added to more syscalls. These should include at least unlinkat(), renameat2() and the not-yet-upstreamed setxattrat().

Quoted 10 lines

Security considerations:
- Only the bare minimal set of credentials is overridden:
fsuid, fsgid and group_info. The rest, for example capabilities,
are not overridden to avoid unneeded security risks.
- To avoid sandboxing escape, this patch makes sure the restricted
lookup modes are used. Namely, RESOLVE_BENEATH or RESOLVE_IN_ROOT.
- Magic /proc symlinks are discarded, as suggested by
Andy Lutomirski <[email protected]>> - O_CRED_ALLOW fds cannot be passed via unix socket and are always closed on exec() to prevent "unsuspecting userspace" from not being
able to fully drop privs.

What about hard links?

== snip ==
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <stdarg.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/openat2.h>

#define O_CRED_ALLOW 0x2000000
#define OA2_CRED_INHERIT (1UL << 28)

#define SYS_openat2 437

long openat2(int dirfd, const char *pathname, struct open_how *how, size_t size) {
    return syscall(SYS_openat2, dirfd, pathname, how, size);
}

__attribute__ ((noreturn, format(printf, 1, 2)))

static void die(const char *restrict fmt, ...) {
    va_list ap;
    va_start(ap, fmt);
    vfprintf(stderr, fmt, ap);
    va_end(ap);
    _exit(1);
}

int main() {

    unlink("/tmp/d/test.dat");
    unlink("/tmp/d/hostname");
    if (rmdir("/tmp/d") != 0 && errno != ENOENT)
        die("/tmp/d: %m\n");
    
    umask(0);
    if (mkdir("/tmp/d", 0777) != 0)
        die("/tmp/d: %m\n");

    int dirfd = open("/tmp/d", O_RDONLY + O_CRED_ALLOW);
    if (dirfd == -1)
        die("/tmp/d: %m\n");

    if (setuid(1000) != 0)
        die("setuid: %m\n");

    if (link("/etc/hostname", "/tmp/d/hostname") == -1)
        die ("/etc/hostname: %m\n");

    if(openat(dirfd, "hostname", O_RDWR) != -1)
        die("/tmp/d/hostname could be opened by uid 1000");

    {   struct open_how how = { .flags = O_RDWR + OA2_CRED_INHERIT, .resolve = RESOLVE_BENEATH };
        if (openat2(dirfd, "hostname", &how, sizeof(how)) == -1)
            die("hostname: %m\n");
        printf("able to open /etc/hostname RDWR \n");
    }
}

== snip ==

buczek@dose:~$ gcc -O0 -Wall -Wextra -Werror -g -o test test.c
buczek@dose:~$ sudo ./test
able to open /etc/hostname RDWR
buczek@dose:~$
Signature · 3 lines
Donald Buczek
[email protected]
Tel: +49 30 8413 1433
author
#5 lore raw

Re: [PATCH v6 3/3] openat2: add OA2_CRED_INHERIT flag

04.05.2024 23:38, Donald Buczek пишет:

Quoted 33 lines · Stas Sergeev
Quoted 21 lines

This flag performs the open operation with the fs credentials (fsuid, fsgid, group_info) that were in effect when dir_fd was opened. dir_fd must be opened with O_CRED_ALLOW, or EPERM is returned.

Selftests are added to check for these properties as well as for the invalid flag combinations.

This allows the process to pre-open some directories and then change eUID (and all other UIDs/GIDs) to a less-privileged user, retaining the ability to open/create files within these directories.

Design goal:
The idea is to provide a very light-weight sandboxing, where the process, without the use of any heavy-weight techniques like chroot within namespaces, can restrict the access to the set of pre-opened directories.
This patch is just a first step to such sandboxing. If things go well, in the future the same extension can be added to more syscalls. These should include at least unlinkat(), renameat2() and the not-yet-upstreamed setxattrat().

Quoted 10 lines

Security considerations:
- Only the bare minimal set of credentials is overridden:
   fsuid, fsgid and group_info. The rest, for example capabilities,
   are not overridden to avoid unneeded security risks.
- To avoid sandboxing escape, this patch makes sure the restricted
   lookup modes are used. Namely, RESOLVE_BENEATH or RESOLVE_IN_ROOT.
- Magic /proc symlinks are discarded, as suggested by
   Andy Lutomirski <[email protected]>> - O_CRED_ALLOW fds cannot be passed via unix socket and are always closed on exec() to prevent "unsuspecting userspace" from not being
   able to fully drop privs.

Quoted 1 line

What about hard links?

Well, you set umask to 0 in your example. If you didn't do that, the dir wouldn't have 0777 perms, and the hard link would not be created.
But yes, that demonstrates the unsafe usage scenario, i.e. unsafe directory perms immediately lead to a security hole. Maybe O_CRED_ALLOW should check for safe perms, or maybe it shouldn't... So far there are no signs of this patch to ever be accepted, so I am not sure if more complexity needs to be added to it.

#6 lore raw
In reply to Stas Sergeev
Quoted 7 lines · Stas Sergeev

This patch-set implements the OA2_CRED_INHERIT flag for openat2() syscall. It is needed to perform an open operation with the creds that were in effect when the dir_fd was opened, if the dir was opened with O_CRED_ALLOW flag. This allows the process to pre-open some dirs and switch eUID (and other UIDs/GIDs) to the less-privileged user, while still retaining the possibility to open/create files within the pre-opened directory set.

Quoted 6 lines

The sand-boxing is security-oriented: symlinks leading outside of a sand-box are rejected. /proc magic links are rejected. fds opened with O_CRED_ALLOW are always closed on exec() and cannot be passed via unix socket.
The more detailed description (including security considerations) is available in the log messages of individual patches.

(I meant to reply last week but I couldn't get my mail server to send mail...)

It seems to me that this can already be implemented using MOUNT_ATTR_IDMAP, without creating a new form of credential overriding within the filesystem (and with such a deceptively simple implementation...)

If you are a privileged process which plans to change users, you can create a detached tree with a user mapping that gives that user access to only that tree. This is far more effective at restricting possible attacks because id-mapped mounts don't override credentials during VFS operations (meaning that if you miss something, you have a big problem), instead they only affect uid-related operations within the filesystem for that mount. Since this implementation does no inherit CAP_DAC_OVERRIDE, being able to rewrite uid/gids is all you need.

A new attack I just thought of while writing this mail is that because there is no RESOLVE_NO_XDEV requirement, it should be possible for the process to get an arbitrary write primitive by creating a new userns+mountns and then bind-mounting / underneath the directory. Since O_CRED_INHERIT uses override_creds, it doesn't care about whether something about the O_CRED_ALLOW directory changed afterwards. Yes, you can "just fix this" by adding a RESOLVE_NO_XDEV requirement too, but given that there have been 2-3 security issues with this design found already, it makes me feel really uneasy. Using id-mapped mounts avoids this issue because the new mount will not have the id-mapping applied and thus there is no security issue.

Signature · 4 lines
Aleksa Sarai
Senior Software Engineer (Containers)
SUSE Linux GmbH
<https://www.cyphar.com/>
author
#7 lore raw

07.05.2024 10:50, Aleksa Sarai пишет:

Quoted 1 line

If you are a privileged process which plans to change users,

Not privileged at all.
But I think what you say is still possible with userns?

Quoted 4 lines

A new attack I just thought of while writing this mail is that because there is no RESOLVE_NO_XDEV requirement, it should be possible for the process to get an arbitrary write primitive by creating a new userns+mountns and then bind-mounting / underneath the directory.

Doesn't this need a write perm to a directory? In his case this is not a threat, because you are not supposed to have a write perm to that dir. OA2_CRED_INHERIT is the only way to write.

#8 lore raw
Quoted 5 lines · stsp

07.05.2024 10:50, Aleksa Sarai пишет:

Quoted 1 line

If you are a privileged process which plans to change users,

Not privileged at all. But I think what you say is still possible with userns?

It is possible to configure MOUNT_ATTR_IDMAP in a user namespace but there are some restrictions that I suspect will make this complicated. If you try to do something with a regular filesystem you'll probably run into issues because you won't have CAP_SYS_ADMIN in the super block's userns. But you could probably do it with tmpfs.

Quoted 9 lines
Quoted 4 lines

A new attack I just thought of while writing this mail is that because there is no RESOLVE_NO_XDEV requirement, it should be possible for the process to get an arbitrary write primitive by creating a new userns+mountns and then bind-mounting / underneath the directory.

Doesn't this need a write perm to a directory? In his case this is not a threat, because you are not supposed to have a write perm to that dir. OA2_CRED_INHERIT is the only way to write.

No, bind-mounts don't require write permission. As long as you can resolve the target path you can bind-mount on top of it, so if there's a subdirectory you can bind-mount / underneath (and if there is only a file you can bind-mount any file you want to access/overwrite instead).

There are restrictions on mounting through /proc/self/fd/... but they don't apply here (all files opened by a process doing setns/unshare have their vfsmounts updated to be from the new mount namespace, meaning you can do mounts through them with /proc/self/fd/... without issue.)

Signature · 4 lines
Aleksa Sarai
Senior Software Engineer (Containers)
SUSE Linux GmbH
<https://www.cyphar.com/>
author
#9 lore raw

07.05.2024 14:58, Aleksa Sarai пишет:

Quoted 10 lines · stsp
Quoted 4 lines

07.05.2024 10:50, Aleksa Sarai пишет:

Quoted 1 line

If you are a privileged process which plans to change users,

Not privileged at all. But I think what you say is still possible with userns?

It is possible to configure MOUNT_ATTR_IDMAP in a user namespace but there are some restrictions that I suspect will make this complicated. If you try to do something with a regular filesystem you'll probably run into issues because you won't have CAP_SYS_ADMIN in the super block's userns. But you could probably do it with tmpfs.

Then its likely not a replacement for my proposal, as I really don't need that on tmpfs.
Perhaps right now I can use the helper process and an rpc as a replacement. This is much more work and is slower, but more or less can approximate my original design decision quite precisely. Another disadvantage of an rpc approach is that the fds I get from the helper process, can not be trusted, as in this case kernel doesn't guarantee the fd actually refers to the resource I requested. I've seen a few OSes where rpc is checked by a trusted entity to avoid such problem.

Quoted 10 lines
Quoted 9 lines
Quoted 4 lines

A new attack I just thought of while writing this mail is that because there is no RESOLVE_NO_XDEV requirement, it should be possible for the process to get an arbitrary write primitive by creating a new userns+mountns and then bind-mounting / underneath the directory.

Doesn't this need a write perm to a directory? In his case this is not a threat, because you are not supposed to have a write perm to that dir. OA2_CRED_INHERIT is the only way to write.

No, bind-mounts don't require write permission.

Oh, isn't this a problem by itself?
Yes, in this case my patch needs to
avoid RESOLVE_NO_XDEV, but I find this a harsh restriction. Maybe the bind mount was done before a priv drop? Then it is fully legitimate. Anyway, I don't know if I should work on it or not, as there seem to be no indication of a possible acceptance.

#10 lore raw
In reply to Stas Sergeev
Quoted 6 lines · Stas Sergeev

This patch-set implements the OA2_CRED_INHERIT flag for openat2() syscall. It is needed to perform an open operation with the creds that were in effect when the dir_fd was opened, if the dir was opened with O_CRED_ALLOW flag. This allows the process to pre-open some dirs and switch eUID (and other UIDs/GIDs) to the less-privileged user, while still retaining the possibility to open/create files within the pre-opened directory set.

As Andy Lutomirski mentioned before, Linux already has Landlock
(https://docs.kernel.org/userspace-api/landlock.html) for unprivileged filesystem sandboxing. What benefits does OA2_CRED_INHERIT have compared to Landlock?

author
#11 lore raw

21.05.2024 22:01, Jann Horn пишет:

Quoted 11 lines · Stas Sergeev
Quoted 6 lines

This patch-set implements the OA2_CRED_INHERIT flag for openat2() syscall. It is needed to perform an open operation with the creds that were in effect when the dir_fd was opened, if the dir was opened with O_CRED_ALLOW flag. This allows the process to pre-open some dirs and switch eUID (and other UIDs/GIDs) to the less-privileged user, while still retaining the possibility to open/create files within the pre-opened directory set.

As Andy Lutomirski mentioned before, Linux already has Landlock
(https://docs.kernel.org/userspace-api/landlock.html) for unprivileged filesystem sandboxing. What benefits does OA2_CRED_INHERIT have compared to Landlock?

The idea is different.
OA2_CRED_INHERIT was supposed to give you an additional access (to what you can't access otherwise, after a priv drop), while landlock allows you to explicitly restrict an access. OA2_CRED_INHERIT more answered with idmapped mounts rather than the landlock, but idmapped mounts are not fully unpriv'd.