Skip to content

Commit 8db52c7

Browse files
cypharAl Viro
authored andcommitted
namei: LOOKUP_IN_ROOT: chroot-like scoped resolution
/* Background. */ Container runtimes or other administrative management processes will often interact with root filesystems while in the host mount namespace, because the cost of doing a chroot(2) on every operation is too prohibitive (especially in Go, which cannot safely use vfork). However, a malicious program can trick the management process into doing operations on files outside of the root filesystem through careful crafting of symlinks. Most programs that need this feature have attempted to make this process safe, by doing all of the path resolution in userspace (with symlinks being scoped to the root of the malicious root filesystem). Unfortunately, this method is prone to foot-guns and usually such implementations have subtle security bugs. Thus, what userspace needs is a way to resolve a path as though it were in a chroot(2) -- with all absolute symlinks being resolved relative to the dirfd root (and ".." components being stuck under the dirfd root). It is much simpler and more straight-forward to provide this functionality in-kernel (because it can be done far more cheaply and correctly). More classical applications that also have this problem (which have their own potentially buggy userspace path sanitisation code) include web servers, archive extraction tools, network file servers, and so on. /* Userspace API. */ LOOKUP_IN_ROOT will be exposed to userspace through openat2(2). /* Semantics. */ Unlike most other LOOKUP flags (most notably LOOKUP_FOLLOW), LOOKUP_IN_ROOT applies to all components of the path. With LOOKUP_IN_ROOT, any path component which attempts to cross the starting point of the pathname lookup (the dirfd passed to openat) will remain at the starting point. Thus, all absolute paths and symlinks will be scoped within the starting point. There is a slight change in behaviour regarding pathnames -- if the pathname is absolute then the dirfd is still used as the root of resolution of LOOKUP_IN_ROOT is specified (this is to avoid obvious foot-guns, at the cost of a minor API inconsistency). As with LOOKUP_BENEATH, Jann's security concern about ".."[1] applies to LOOKUP_IN_ROOT -- therefore ".." resolution is blocked. This restriction will be lifted in a future patch, but requires more work to ensure that permitting ".." is done safely. Magic-link jumps are also blocked, because they can beam the path lookup across the starting point. It would be possible to detect and block only the "bad" crossings with path_is_under() checks, but it's unclear whether it makes sense to permit magic-links at all. However, userspace is recommended to pass LOOKUP_NO_MAGICLINKS if they want to ensure that magic-link crossing is entirely disabled. /* Testing. */ LOOKUP_IN_ROOT is tested as part of the openat2(2) selftests. [1]: https://lore.kernel.org/lkml/CAG48ez1jzNvxB+bfOBnERFGp=oMM0vHWuLD6EULmne3R6xa53w@mail.gmail.com/ Cc: Christian Brauner <[email protected]> Signed-off-by: Aleksa Sarai <[email protected]> Signed-off-by: Al Viro <[email protected]>
1 parent adb21d2 commit 8db52c7

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

fs/namei.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2296,13 +2296,16 @@ static const char *path_init(struct nameidata *nd, unsigned flags)
22962296

22972297
nd->m_seq = read_seqbegin(&mount_lock);
22982298

2299-
/* Figure out the starting path and root (if needed). */
2300-
if (*s == '/') {
2299+
/* Absolute pathname -- fetch the root (LOOKUP_IN_ROOT uses nd->dfd). */
2300+
if (*s == '/' && !(flags & LOOKUP_IN_ROOT)) {
23012301
error = nd_jump_root(nd);
23022302
if (unlikely(error))
23032303
return ERR_PTR(error);
23042304
return s;
2305-
} else if (nd->dfd == AT_FDCWD) {
2305+
}
2306+
2307+
/* Relative pathname -- get the starting-point it is relative to. */
2308+
if (nd->dfd == AT_FDCWD) {
23062309
if (flags & LOOKUP_RCU) {
23072310
struct fs_struct *fs = current->fs;
23082311
unsigned seq;
@@ -2342,6 +2345,7 @@ static const char *path_init(struct nameidata *nd, unsigned flags)
23422345
}
23432346
fdput(f);
23442347
}
2348+
23452349
/* For scoped-lookups we need to set the root to the dirfd as well. */
23462350
if (flags & LOOKUP_IS_SCOPED) {
23472351
nd->root = nd->path;

include/linux/namei.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@ enum {LAST_NORM, LAST_ROOT, LAST_DOT, LAST_DOTDOT, LAST_BIND};
4545
#define LOOKUP_NO_MAGICLINKS 0x020000 /* No nd_jump_link() crossing. */
4646
#define LOOKUP_NO_XDEV 0x040000 /* No mountpoint crossing. */
4747
#define LOOKUP_BENEATH 0x080000 /* No escaping from starting point. */
48+
#define LOOKUP_IN_ROOT 0x100000 /* Treat dirfd as fs root. */
4849
/* LOOKUP_* flags which do scope-related checks based on the dirfd. */
49-
#define LOOKUP_IS_SCOPED LOOKUP_BENEATH
50+
#define LOOKUP_IS_SCOPED (LOOKUP_BENEATH | LOOKUP_IN_ROOT)
5051

5152
extern int path_pts(struct path *path);
5253

0 commit comments

Comments
 (0)