implement OA2_INHERIT_CRED flag for openat2()
Subsystems (3)
v3 cover implement OA2_INHERIT_CRED flag for openat2()
This patch-set implements the OA2_INHERIT_CRED 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. 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 more detailed description (including security considerations) is available in the log messages of individual patches.
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]>
Signature · 1 line
2.44.0
PATCH v3 1/2 fs: reorganize path_openat()
This patch moves the call to alloc_empty_file() below the call to path_init(). That changes is needed for the next patch, which adds a cred override for alloc_empty_file(). The needed cred info is only available after the call to path_init().
No functional changes are intended by that patch.
--- fs/namei.c | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-)
fs/namei.c +17 -9
@@ -3782,22 +3782,30 @@ static struct file *path_openat(struct nameidata *nd,struct file *file;int error; - file =alloc_empty_file (op->open_flag,current_cred ()); -if (IS_ERR (file)) -return file; - -if (unlikely (file->f_flags & __O_TMPFILE)) { +if (unlikely (op->open_flag & __O_TMPFILE)) { + file =alloc_empty_file (op->open_flag,current_cred ()); +if (IS_ERR (file)) +return file; error =do_tmpfile (nd, flags, op, file); - }else if (unlikely (file->f_flags &O_PATH )) { + }else if (unlikely (op->open_flag &O_PATH )) { + file =alloc_empty_file (op->open_flag,current_cred ()); +if (IS_ERR (file)) +return file; error =do_o_path (nd, flags, file); }else {const char *s =path_init (nd, flags); -while (!(error =link_path_walk (s, nd)) && - (s =open_last_lookups (nd, file, op)) !=NULL ) - ; + file =alloc_empty_file (op->open_flag,current_cred ()); + error =PTR_ERR_OR_ZERO (file); +if (!error) { +while (!(error =link_path_walk (s, nd)) && + (s =open_last_lookups (nd, file, op)) !=NULL ) + ; + }if (!error) error =do_open (nd, file, op);terminate_walk (nd); +if (IS_ERR (file)) +return file; }if (likely (!error)) {if (likely (file->f_mode &FMODE_OPENED ))
Signature · 1 line
2.44.0
PATCH v3 2/2 openat2: add OA2_INHERIT_CRED flag
In reply to Stas SergeevThis flag performs the open operation with the fs credentials (fsuid, fsgid, group_info) that were in effect when dir_fd was opened. 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.
- To avoid leaking creds across exec, this patch requires O_CLOEXEC flag on a directory.
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.
--- fs/internal.h | 2 +- fs/namei.c | 61 ++++++++++++++++++++++++++++++++++-- fs/open.c | 2 +- include/linux/fcntl.h | 2 ++ include/uapi/linux/openat2.h | 3 ++ 5 files changed, 66 insertions(+), 4 deletions(-)
fs/internal.h +1 -1
@@ -169,7 +169,7 @@ static inline void sb_end_ro_state_change(struct super_block *sb)* open.c */ struct open_flags { -int open_flag; +u64 open_flag;umode_t mode;int acc_mode;int intent;
fs/namei.c +59 -2
103 lines not on this page · raw message
fs/open.c +1 -1
@@ -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 ;
include/linux/fcntl.h +2 -0
@@ -12,6 +12,8 @@ FASYNC | O_DIRECT | O_LARGEFILE | O_DIRECTORY | O_NOFOLLOW | \ O_NOATIME | O_CLOEXEC | O_PATH | __O_TMPFILE) +#define VALID_OPENAT2_FLAGS (VALID_OPEN_FLAGS | OA2_INHERIT_CRED) +/* 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 +3 -0
@@ -40,4 +40,7 @@ struct open_how {return -EAGAIN if that's not possible. */ +/* openat2-specific flags go to upper 4 bytes. */ +#define OA2_INHERIT_CRED (1ULL << 32) +#endif /* _UAPI_LINUX_OPENAT2_H */
Signature · 1 line
2.44.0