static bool legitimize_root(struct nameidata *nd)
 {
+       /*
+        * For scoped-lookups (where nd->root has been zeroed), we need to
+        * restart the whole lookup from scratch -- because set_root() is wrong
+        * for these lookups (nd->dfd is the root, not the filesystem root).
+        */
+       if (!nd->root.mnt && (nd->flags & LOOKUP_IS_SCOPED))
+               return false;
+       /* Nothing to do if nd->root is zero or is managed by the VFS user. */
        if (!nd->root.mnt || (nd->flags & LOOKUP_ROOT))
                return true;
        nd->flags |= LOOKUP_ROOT_GRABBED;
        int status;
 
        if (nd->flags & LOOKUP_RCU) {
-               if (!(nd->flags & LOOKUP_ROOT))
+               /*
+                * We don't want to zero nd->root for scoped-lookups or
+                * externally-managed nd->root.
+                */
+               if (!(nd->flags & (LOOKUP_ROOT | LOOKUP_IS_SCOPED)))
                        nd->root.mnt = NULL;
                if (unlikely(unlazy_walk(nd)))
                        return -ECHILD;
        }
 
+       if (unlikely(nd->flags & LOOKUP_IS_SCOPED)) {
+               /*
+                * While the guarantee of LOOKUP_IS_SCOPED is (roughly) "don't
+                * ever step outside the root during lookup" and should already
+                * be guaranteed by the rest of namei, we want to avoid a namei
+                * BUG resulting in userspace being given a path that was not
+                * scoped within the root at some point during the lookup.
+                *
+                * So, do a final sanity-check to make sure that in the
+                * worst-case scenario (a complete bypass of LOOKUP_IS_SCOPED)
+                * we won't silently return an fd completely outside of the
+                * requested root to userspace.
+                *
+                * Userspace could move the path outside the root after this
+                * check, but as discussed elsewhere this is not a concern (the
+                * resolved file was inside the root at some point).
+                */
+               if (!path_is_under(&nd->path, &nd->root))
+                       return -EXDEV;
+       }
+
        if (likely(!(nd->flags & LOOKUP_JUMPED)))
                return 0;
 
 {
        struct fs_struct *fs = current->fs;
 
+       /*
+        * Jumping to the real root in a scoped-lookup is a BUG in namei, but we
+        * still have to ensure it doesn't happen because it will cause a breakout
+        * from the dirfd.
+        */
+       if (WARN_ON(nd->flags & LOOKUP_IS_SCOPED))
+               return -ENOTRECOVERABLE;
+
        if (nd->flags & LOOKUP_RCU) {
                unsigned seq;
 
 
 static int nd_jump_root(struct nameidata *nd)
 {
+       if (unlikely(nd->flags & LOOKUP_BENEATH))
+               return -EXDEV;
        if (unlikely(nd->flags & LOOKUP_NO_XDEV)) {
                /* Absolute path arguments to path_init() are allowed. */
                if (nd->path.mnt != NULL && nd->path.mnt != nd->root.mnt)
                if (nd->path.mnt != path->mnt)
                        goto err;
        }
+       /* Not currently safe for scoped-lookups. */
+       if (unlikely(nd->flags & LOOKUP_IS_SCOPED))
+               goto err;
 
        path_put(&nd->path);
        nd->path = *path;
        struct inode *inode = nd->inode;
 
        while (1) {
-               if (path_equal(&nd->path, &nd->root))
+               if (path_equal(&nd->path, &nd->root)) {
+                       if (unlikely(nd->flags & LOOKUP_BENEATH))
+                               return -ECHILD;
                        break;
+               }
                if (nd->path.dentry != nd->path.mnt->mnt_root) {
                        struct dentry *old = nd->path.dentry;
                        struct dentry *parent = old->d_parent;
 
 static int follow_dotdot(struct nameidata *nd)
 {
-       while(1) {
-               if (path_equal(&nd->path, &nd->root))
+       while (1) {
+               if (path_equal(&nd->path, &nd->root)) {
+                       if (unlikely(nd->flags & LOOKUP_BENEATH))
+                               return -EXDEV;
                        break;
+               }
                if (nd->path.dentry != nd->path.mnt->mnt_root) {
                        int ret = path_parent_directory(&nd->path);
                        if (ret)
        if (type == LAST_DOTDOT) {
                int error = 0;
 
+               /*
+                * Scoped-lookup flags resolving ".." is not currently safe --
+                * races can cause our parent to have moved outside of the root
+                * and us to skip over it.
+                */
+               if (unlikely(nd->flags & LOOKUP_IS_SCOPED))
+                       return -EXDEV;
                if (!nd->root.mnt) {
                        error = set_root(nd);
                        if (error)
                        get_fs_pwd(current->fs, &nd->path);
                        nd->inode = nd->path.dentry->d_inode;
                }
-               return s;
        } else {
                /* Caller must check execute permissions on the starting path component */
                struct fd f = fdget_raw(nd->dfd);
                        nd->inode = nd->path.dentry->d_inode;
                }
                fdput(f);
-               return s;
        }
+       /* For scoped-lookups we need to set the root to the dirfd as well. */
+       if (flags & LOOKUP_IS_SCOPED) {
+               nd->root = nd->path;
+               if (flags & LOOKUP_RCU) {
+                       nd->root_seq = nd->seq;
+               } else {
+                       path_get(&nd->root);
+                       nd->flags |= LOOKUP_ROOT_GRABBED;
+               }
+       }
+       return s;
 }
 
 static const char *trailing_symlink(struct nameidata *nd)