aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2017-05-12 14:39:59 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2017-05-12 14:39:59 -0400
commit050453295f808dd683b9a88f58a1b29540097394 (patch)
tree1d43b39b72779d9133de345a96a6ea92cf96582c
parente0c4a5fc750e22c6f8d5c1ab7cc18592b88852ab (diff)
parent4f757f3cbf54edef7b75c68d6d6d2f1a0ca08d2e (diff)
Merge branch 'work.sane_pwd' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs
Pull misc vfs updates from Al Viro: "Making sure that something like a referral point won't end up as pwd or root. The main part is the last commit (fixing mntns_install()); that one fixes a hard-to-hit race. The fchdir() commit is making fchdir(2) a bit more robust - it should be impossible to get opened files (even O_PATH ones) for referral points in the first place, so the existing checks are OK, but checking the same thing as in chdir(2) is just as cheap. The path_init() commit removes a redundant check that shouldn't have been there in the first place" * 'work.sane_pwd' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs: make sure that mntns_install() doesn't end up with referral for root path_init(): don't bother with checking MAY_EXEC for LOOKUP_ROOT make sure that fchdir() won't accept referral points, etc.
-rw-r--r--fs/namei.c48
-rw-r--r--fs/namespace.c18
-rw-r--r--fs/open.c9
-rw-r--r--include/linux/namei.h1
4 files changed, 55 insertions, 21 deletions
diff --git a/fs/namei.c b/fs/namei.c
index 7286f87ce863..6571a5f5112e 100644
--- a/fs/namei.c
+++ b/fs/namei.c
@@ -2142,7 +2142,6 @@ OK:
2142 2142
2143static const char *path_init(struct nameidata *nd, unsigned flags) 2143static const char *path_init(struct nameidata *nd, unsigned flags)
2144{ 2144{
2145 int retval = 0;
2146 const char *s = nd->name->name; 2145 const char *s = nd->name->name;
2147 2146
2148 if (!*s) 2147 if (!*s)
@@ -2154,13 +2153,8 @@ static const char *path_init(struct nameidata *nd, unsigned flags)
2154 if (flags & LOOKUP_ROOT) { 2153 if (flags & LOOKUP_ROOT) {
2155 struct dentry *root = nd->root.dentry; 2154 struct dentry *root = nd->root.dentry;
2156 struct inode *inode = root->d_inode; 2155 struct inode *inode = root->d_inode;
2157 if (*s) { 2156 if (*s && unlikely(!d_can_lookup(root)))
2158 if (!d_can_lookup(root)) 2157 return ERR_PTR(-ENOTDIR);
2159 return ERR_PTR(-ENOTDIR);
2160 retval = inode_permission(inode, MAY_EXEC);
2161 if (retval)
2162 return ERR_PTR(retval);
2163 }
2164 nd->path = nd->root; 2158 nd->path = nd->root;
2165 nd->inode = inode; 2159 nd->inode = inode;
2166 if (flags & LOOKUP_RCU) { 2160 if (flags & LOOKUP_RCU) {
@@ -2258,6 +2252,35 @@ static inline int lookup_last(struct nameidata *nd)
2258 return walk_component(nd, 0); 2252 return walk_component(nd, 0);
2259} 2253}
2260 2254
2255static int handle_lookup_down(struct nameidata *nd)
2256{
2257 struct path path = nd->path;
2258 struct inode *inode = nd->inode;
2259 unsigned seq = nd->seq;
2260 int err;
2261
2262 if (nd->flags & LOOKUP_RCU) {
2263 /*
2264 * don't bother with unlazy_walk on failure - we are
2265 * at the very beginning of walk, so we lose nothing
2266 * if we simply redo everything in non-RCU mode
2267 */
2268 if (unlikely(!__follow_mount_rcu(nd, &path, &inode, &seq)))
2269 return -ECHILD;
2270 } else {
2271 dget(path.dentry);
2272 err = follow_managed(&path, nd);
2273 if (unlikely(err < 0))
2274 return err;
2275 inode = d_backing_inode(path.dentry);
2276 seq = 0;
2277 }
2278 path_to_nameidata(&path, nd);
2279 nd->inode = inode;
2280 nd->seq = seq;
2281 return 0;
2282}
2283
2261/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */ 2284/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
2262static int path_lookupat(struct nameidata *nd, unsigned flags, struct path *path) 2285static int path_lookupat(struct nameidata *nd, unsigned flags, struct path *path)
2263{ 2286{
@@ -2266,6 +2289,15 @@ static int path_lookupat(struct nameidata *nd, unsigned flags, struct path *path
2266 2289
2267 if (IS_ERR(s)) 2290 if (IS_ERR(s))
2268 return PTR_ERR(s); 2291 return PTR_ERR(s);
2292
2293 if (unlikely(flags & LOOKUP_DOWN)) {
2294 err = handle_lookup_down(nd);
2295 if (unlikely(err < 0)) {
2296 terminate_walk(nd);
2297 return err;
2298 }
2299 }
2300
2269 while (!(err = link_path_walk(s, nd)) 2301 while (!(err = link_path_walk(s, nd))
2270 && ((err = lookup_last(nd)) > 0)) { 2302 && ((err = lookup_last(nd)) > 0)) {
2271 s = trailing_symlink(nd); 2303 s = trailing_symlink(nd);
diff --git a/fs/namespace.c b/fs/namespace.c
index b3b115bd4e1e..8bd3e4d448b9 100644
--- a/fs/namespace.c
+++ b/fs/namespace.c
@@ -3462,8 +3462,9 @@ static void mntns_put(struct ns_common *ns)
3462static int mntns_install(struct nsproxy *nsproxy, struct ns_common *ns) 3462static int mntns_install(struct nsproxy *nsproxy, struct ns_common *ns)
3463{ 3463{
3464 struct fs_struct *fs = current->fs; 3464 struct fs_struct *fs = current->fs;
3465 struct mnt_namespace *mnt_ns = to_mnt_ns(ns); 3465 struct mnt_namespace *mnt_ns = to_mnt_ns(ns), *old_mnt_ns;
3466 struct path root; 3466 struct path root;
3467 int err;
3467 3468
3468 if (!ns_capable(mnt_ns->user_ns, CAP_SYS_ADMIN) || 3469 if (!ns_capable(mnt_ns->user_ns, CAP_SYS_ADMIN) ||
3469 !ns_capable(current_user_ns(), CAP_SYS_CHROOT) || 3470 !ns_capable(current_user_ns(), CAP_SYS_CHROOT) ||
@@ -3474,15 +3475,18 @@ static int mntns_install(struct nsproxy *nsproxy, struct ns_common *ns)
3474 return -EINVAL; 3475 return -EINVAL;
3475 3476
3476 get_mnt_ns(mnt_ns); 3477 get_mnt_ns(mnt_ns);
3477 put_mnt_ns(nsproxy->mnt_ns); 3478 old_mnt_ns = nsproxy->mnt_ns;
3478 nsproxy->mnt_ns = mnt_ns; 3479 nsproxy->mnt_ns = mnt_ns;
3479 3480
3480 /* Find the root */ 3481 /* Find the root */
3481 root.mnt = &mnt_ns->root->mnt; 3482 err = vfs_path_lookup(mnt_ns->root->mnt.mnt_root, &mnt_ns->root->mnt,
3482 root.dentry = mnt_ns->root->mnt.mnt_root; 3483 "/", LOOKUP_DOWN, &root);
3483 path_get(&root); 3484 if (err) {
3484 while(d_mountpoint(root.dentry) && follow_down_one(&root)) 3485 /* revert to old namespace */
3485 ; 3486 nsproxy->mnt_ns = old_mnt_ns;
3487 put_mnt_ns(mnt_ns);
3488 return err;
3489 }
3486 3490
3487 /* Update the pwd and root */ 3491 /* Update the pwd and root */
3488 set_fs_pwd(fs, &root); 3492 set_fs_pwd(fs, &root);
diff --git a/fs/open.c b/fs/open.c
index 6d2d2b33ac54..cd0c5be8d012 100644
--- a/fs/open.c
+++ b/fs/open.c
@@ -460,20 +460,17 @@ out:
460SYSCALL_DEFINE1(fchdir, unsigned int, fd) 460SYSCALL_DEFINE1(fchdir, unsigned int, fd)
461{ 461{
462 struct fd f = fdget_raw(fd); 462 struct fd f = fdget_raw(fd);
463 struct inode *inode; 463 int error;
464 int error = -EBADF;
465 464
466 error = -EBADF; 465 error = -EBADF;
467 if (!f.file) 466 if (!f.file)
468 goto out; 467 goto out;
469 468
470 inode = file_inode(f.file);
471
472 error = -ENOTDIR; 469 error = -ENOTDIR;
473 if (!S_ISDIR(inode->i_mode)) 470 if (!d_can_lookup(f.file->f_path.dentry))
474 goto out_putf; 471 goto out_putf;
475 472
476 error = inode_permission(inode, MAY_EXEC | MAY_CHDIR); 473 error = inode_permission(file_inode(f.file), MAY_EXEC | MAY_CHDIR);
477 if (!error) 474 if (!error)
478 set_fs_pwd(current->fs, &f.file->f_path); 475 set_fs_pwd(current->fs, &f.file->f_path);
479out_putf: 476out_putf:
diff --git a/include/linux/namei.h b/include/linux/namei.h
index f29abda31e6d..8b4794e83196 100644
--- a/include/linux/namei.h
+++ b/include/linux/namei.h
@@ -44,6 +44,7 @@ enum {LAST_NORM, LAST_ROOT, LAST_DOT, LAST_DOTDOT, LAST_BIND};
44#define LOOKUP_JUMPED 0x1000 44#define LOOKUP_JUMPED 0x1000
45#define LOOKUP_ROOT 0x2000 45#define LOOKUP_ROOT 0x2000
46#define LOOKUP_EMPTY 0x4000 46#define LOOKUP_EMPTY 0x4000
47#define LOOKUP_DOWN 0x8000
47 48
48extern int path_pts(struct path *path); 49extern int path_pts(struct path *path);
49 50