Skip to content

Commit adb21d2

Browse files
cypharAl Viro
authored andcommitted
namei: LOOKUP_BENEATH: O_BENEATH-like scoped resolution
/* Background. */ There are many circumstances when userspace wants to resolve a path and ensure that it doesn't go outside of a particular root directory during resolution. Obvious examples include archive extraction tools, as well as other security-conscious userspace programs. FreeBSD spun out O_BENEATH from their Capsicum project[1,2], so it also seems reasonable to implement similar functionality for Linux. This is part of a refresh of Al's AT_NO_JUMPS patchset[3] (which was a variation on David Drysdale's O_BENEATH patchset[4], which in turn was based on the Capsicum project[5]). /* Userspace API. */ LOOKUP_BENEATH will be exposed to userspace through openat2(2). /* Semantics. */ Unlike most other LOOKUP flags (most notably LOOKUP_FOLLOW), LOOKUP_BENEATH applies to all components of the path. With LOOKUP_BENEATH, any path component which attempts to "escape" the starting point of the filesystem lookup (the dirfd passed to openat) will yield -EXDEV. Thus, all absolute paths and symlinks are disallowed. Due to a security concern brought up by Jann[6], any ".." path components are also 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_BENEATH is tested as part of the openat2(2) selftests. [1]: https://reviews.freebsd.org/D2808 [2]: https://reviews.freebsd.org/D17547 [3]: https://lore.kernel.org/lkml/[email protected]/ [4]: https://lore.kernel.org/lkml/[email protected]/ [5]: https://lore.kernel.org/lkml/[email protected]/ [6]: https://lore.kernel.org/lkml/CAG48ez1jzNvxB+bfOBnERFGp=oMM0vHWuLD6EULmne3R6xa53w@mail.gmail.com/ Cc: Christian Brauner <[email protected]> Suggested-by: David Drysdale <[email protected]> Suggested-by: Al Viro <[email protected]> Suggested-by: Andy Lutomirski <[email protected]> Suggested-by: Linus Torvalds <[email protected]> Signed-off-by: Aleksa Sarai <[email protected]> Signed-off-by: Al Viro <[email protected]>
1 parent 72ba292 commit adb21d2

File tree

2 files changed

+78
-6
lines changed

2 files changed

+78
-6
lines changed

fs/namei.c

Lines changed: 74 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,14 @@ static bool legitimize_links(struct nameidata *nd)
641641

642642
static bool legitimize_root(struct nameidata *nd)
643643
{
644+
/*
645+
* For scoped-lookups (where nd->root has been zeroed), we need to
646+
* restart the whole lookup from scratch -- because set_root() is wrong
647+
* for these lookups (nd->dfd is the root, not the filesystem root).
648+
*/
649+
if (!nd->root.mnt && (nd->flags & LOOKUP_IS_SCOPED))
650+
return false;
651+
/* Nothing to do if nd->root is zero or is managed by the VFS user. */
644652
if (!nd->root.mnt || (nd->flags & LOOKUP_ROOT))
645653
return true;
646654
nd->flags |= LOOKUP_ROOT_GRABBED;
@@ -776,12 +784,37 @@ static int complete_walk(struct nameidata *nd)
776784
int status;
777785

778786
if (nd->flags & LOOKUP_RCU) {
779-
if (!(nd->flags & LOOKUP_ROOT))
787+
/*
788+
* We don't want to zero nd->root for scoped-lookups or
789+
* externally-managed nd->root.
790+
*/
791+
if (!(nd->flags & (LOOKUP_ROOT | LOOKUP_IS_SCOPED)))
780792
nd->root.mnt = NULL;
781793
if (unlikely(unlazy_walk(nd)))
782794
return -ECHILD;
783795
}
784796

797+
if (unlikely(nd->flags & LOOKUP_IS_SCOPED)) {
798+
/*
799+
* While the guarantee of LOOKUP_IS_SCOPED is (roughly) "don't
800+
* ever step outside the root during lookup" and should already
801+
* be guaranteed by the rest of namei, we want to avoid a namei
802+
* BUG resulting in userspace being given a path that was not
803+
* scoped within the root at some point during the lookup.
804+
*
805+
* So, do a final sanity-check to make sure that in the
806+
* worst-case scenario (a complete bypass of LOOKUP_IS_SCOPED)
807+
* we won't silently return an fd completely outside of the
808+
* requested root to userspace.
809+
*
810+
* Userspace could move the path outside the root after this
811+
* check, but as discussed elsewhere this is not a concern (the
812+
* resolved file was inside the root at some point).
813+
*/
814+
if (!path_is_under(&nd->path, &nd->root))
815+
return -EXDEV;
816+
}
817+
785818
if (likely(!(nd->flags & LOOKUP_JUMPED)))
786819
return 0;
787820

@@ -802,6 +835,14 @@ static int set_root(struct nameidata *nd)
802835
{
803836
struct fs_struct *fs = current->fs;
804837

838+
/*
839+
* Jumping to the real root in a scoped-lookup is a BUG in namei, but we
840+
* still have to ensure it doesn't happen because it will cause a breakout
841+
* from the dirfd.
842+
*/
843+
if (WARN_ON(nd->flags & LOOKUP_IS_SCOPED))
844+
return -ENOTRECOVERABLE;
845+
805846
if (nd->flags & LOOKUP_RCU) {
806847
unsigned seq;
807848

@@ -838,6 +879,8 @@ static inline void path_to_nameidata(const struct path *path,
838879

839880
static int nd_jump_root(struct nameidata *nd)
840881
{
882+
if (unlikely(nd->flags & LOOKUP_BENEATH))
883+
return -EXDEV;
841884
if (unlikely(nd->flags & LOOKUP_NO_XDEV)) {
842885
/* Absolute path arguments to path_init() are allowed. */
843886
if (nd->path.mnt != NULL && nd->path.mnt != nd->root.mnt)
@@ -883,6 +926,9 @@ int nd_jump_link(struct path *path)
883926
if (nd->path.mnt != path->mnt)
884927
goto err;
885928
}
929+
/* Not currently safe for scoped-lookups. */
930+
if (unlikely(nd->flags & LOOKUP_IS_SCOPED))
931+
goto err;
886932

887933
path_put(&nd->path);
888934
nd->path = *path;
@@ -1385,8 +1431,11 @@ static int follow_dotdot_rcu(struct nameidata *nd)
13851431
struct inode *inode = nd->inode;
13861432

13871433
while (1) {
1388-
if (path_equal(&nd->path, &nd->root))
1434+
if (path_equal(&nd->path, &nd->root)) {
1435+
if (unlikely(nd->flags & LOOKUP_BENEATH))
1436+
return -ECHILD;
13891437
break;
1438+
}
13901439
if (nd->path.dentry != nd->path.mnt->mnt_root) {
13911440
struct dentry *old = nd->path.dentry;
13921441
struct dentry *parent = old->d_parent;
@@ -1516,9 +1565,12 @@ static int path_parent_directory(struct path *path)
15161565

15171566
static int follow_dotdot(struct nameidata *nd)
15181567
{
1519-
while(1) {
1520-
if (path_equal(&nd->path, &nd->root))
1568+
while (1) {
1569+
if (path_equal(&nd->path, &nd->root)) {
1570+
if (unlikely(nd->flags & LOOKUP_BENEATH))
1571+
return -EXDEV;
15211572
break;
1573+
}
15221574
if (nd->path.dentry != nd->path.mnt->mnt_root) {
15231575
int ret = path_parent_directory(&nd->path);
15241576
if (ret)
@@ -1741,6 +1793,13 @@ static inline int handle_dots(struct nameidata *nd, int type)
17411793
if (type == LAST_DOTDOT) {
17421794
int error = 0;
17431795

1796+
/*
1797+
* Scoped-lookup flags resolving ".." is not currently safe --
1798+
* races can cause our parent to have moved outside of the root
1799+
* and us to skip over it.
1800+
*/
1801+
if (unlikely(nd->flags & LOOKUP_IS_SCOPED))
1802+
return -EXDEV;
17441803
if (!nd->root.mnt) {
17451804
error = set_root(nd);
17461805
if (error)
@@ -2258,7 +2317,6 @@ static const char *path_init(struct nameidata *nd, unsigned flags)
22582317
get_fs_pwd(current->fs, &nd->path);
22592318
nd->inode = nd->path.dentry->d_inode;
22602319
}
2261-
return s;
22622320
} else {
22632321
/* Caller must check execute permissions on the starting path component */
22642322
struct fd f = fdget_raw(nd->dfd);
@@ -2283,8 +2341,18 @@ static const char *path_init(struct nameidata *nd, unsigned flags)
22832341
nd->inode = nd->path.dentry->d_inode;
22842342
}
22852343
fdput(f);
2286-
return s;
22872344
}
2345+
/* For scoped-lookups we need to set the root to the dirfd as well. */
2346+
if (flags & LOOKUP_IS_SCOPED) {
2347+
nd->root = nd->path;
2348+
if (flags & LOOKUP_RCU) {
2349+
nd->root_seq = nd->seq;
2350+
} else {
2351+
path_get(&nd->root);
2352+
nd->flags |= LOOKUP_ROOT_GRABBED;
2353+
}
2354+
}
2355+
return s;
22882356
}
22892357

22902358
static const char *trailing_symlink(struct nameidata *nd)

include/linux/namei.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#ifndef _LINUX_NAMEI_H
33
#define _LINUX_NAMEI_H
44

5+
#include <linux/fs.h>
56
#include <linux/kernel.h>
67
#include <linux/path.h>
78
#include <linux/fcntl.h>
@@ -43,6 +44,9 @@ enum {LAST_NORM, LAST_ROOT, LAST_DOT, LAST_DOTDOT, LAST_BIND};
4344
#define LOOKUP_NO_SYMLINKS 0x010000 /* No symlink crossing. */
4445
#define LOOKUP_NO_MAGICLINKS 0x020000 /* No nd_jump_link() crossing. */
4546
#define LOOKUP_NO_XDEV 0x040000 /* No mountpoint crossing. */
47+
#define LOOKUP_BENEATH 0x080000 /* No escaping from starting point. */
48+
/* LOOKUP_* flags which do scope-related checks based on the dirfd. */
49+
#define LOOKUP_IS_SCOPED LOOKUP_BENEATH
4650

4751
extern int path_pts(struct path *path);
4852

0 commit comments

Comments
 (0)