Received: by 2002:ac0:aa62:0:0:0:0:0 with SMTP id w31-v6csp1415728ima; Sun, 21 Oct 2018 11:12:52 -0700 (PDT) X-Google-Smtp-Source: ACcGV615KkzLvnF5zpWY9CgdRkzD5cd207XgXCXBMsLYN8LoM6vdcGzVMzltw0axBOc27zReRKai X-Received: by 2002:a63:3c46:: with SMTP id i6-v6mr39727706pgn.286.1540145572276; Sun, 21 Oct 2018 11:12:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1540145572; cv=none; d=google.com; s=arc-20160816; b=Yc1bdS4OrXahZQbSH044x8UeYyMzpn4UA3AdBMgu4ADdmEukxTJ2p5nEREqjyAgWk+ 6Hj6V+yQG4CtQe+loVsLBY4Dkc4yWSraD8FslEgFrPEer5dwxzkAEUveLoJ0T7anp00j BHTqGU/evxyO0F13unr3D2a8RCVMGjRy/STxSktpvZbxEe4jmTBXeuDv6b8IMeU3sm4L hfqk4yYUkpoxOjw7bJtJXCjcedZqONUYL69UHlNKoN2JyfpjS7wrVL3l8c1+Op4TRTvA OHI0DyIvy1+ReHrPSXK6v0bRuUfGHZ9uJG0ERaX7jHL28BJ2aqH/ABXfQL3JJKI9jJMg INSQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:subject:mime-version:user-agent :message-id:in-reply-to:date:references:cc:to:from; bh=RmVFkTIuOOxDPr/8GLVvIge+IDbXcph9hDDeeFMp3qE=; b=Jo9LqfZXv3gwk6/3dRzhc3zB/xoyeDIhXayBOw1MugDa50c7Sbi5N0fn9JwD19WYlH aXnUtCSTXSJLrr4fQEkV7itbsG0f9f+Pu6IqbbYym5y1O+Yy8rTzNVoCFm0EIPlaNS5a uJGauv9MMieOXA/3kUzjlCf12bVb9R/kiYXGESRDAqm04/vPFilD2BuJ1i4eKZudgCrS 4muHfd/Af0IcSrJBFLFrUCRWiCwk9BSZbTQ44RMbeBnHQHFKNmuGV/K/nQM2zuzXGTI+ VD7Gh8vOq+goamvZY/CQ24qNbxPZun0dGVwZNU0rC4gNBxs8F+kmdxSRmhNur3lmz2s/ lvkg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id c22-v6si30835718pgb.472.2018.10.21.11.12.22; Sun, 21 Oct 2018 11:12:52 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728031AbeJVA4x (ORCPT + 99 others); Sun, 21 Oct 2018 20:56:53 -0400 Received: from out03.mta.xmission.com ([166.70.13.233]:42320 "EHLO out03.mta.xmission.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727763AbeJVA4w (ORCPT ); Sun, 21 Oct 2018 20:56:52 -0400 Received: from in02.mta.xmission.com ([166.70.13.52]) by out03.mta.xmission.com with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.87) (envelope-from ) id 1gEGne-0005Dx-44; Sun, 21 Oct 2018 10:41:58 -0600 Received: from 67-3-154-154.omah.qwest.net ([67.3.154.154] helo=x220.xmission.com) by in02.mta.xmission.com with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.87) (envelope-from ) id 1gEGnc-0005XD-SD; Sun, 21 Oct 2018 10:41:57 -0600 From: ebiederm@xmission.com (Eric W. Biederman) To: David Howells Cc: viro@zeniv.linux.org.uk, linux-api@vger.kernel.org, torvalds@linux-foundation.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, mszeredi@redhat.com References: <153754740781.17872.7869536526927736855.stgit@warthog.procyon.org.uk> <153754741974.17872.9547912893344172802.stgit@warthog.procyon.org.uk> Date: Sun, 21 Oct 2018 11:41:36 -0500 In-Reply-To: <153754741974.17872.9547912893344172802.stgit@warthog.procyon.org.uk> (David Howells's message of "Fri, 21 Sep 2018 17:30:19 +0100") Message-ID: <87bm7n5k1r.fsf@xmission.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain X-XM-SPF: eid=1gEGnc-0005XD-SD;;;mid=<87bm7n5k1r.fsf@xmission.com>;;;hst=in02.mta.xmission.com;;;ip=67.3.154.154;;;frm=ebiederm@xmission.com;;;spf=neutral X-XM-AID: U2FsdGVkX1/2+iJel1JY/K7cWCvQs9KaK18Aj2OK3Wo= X-SA-Exim-Connect-IP: 67.3.154.154 X-SA-Exim-Mail-From: ebiederm@xmission.com X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on sa06.xmission.com X-Spam-Level: ****** X-Spam-Status: No, score=6.7 required=8.0 tests=ALL_TRUSTED,BAYES_50, DCC_CHECK_NEGATIVE,LotsOfNums_01,TR_Symld_Words,TVD_RCVD_IP, T_TM2_M_HEADER_IN_MSG,T_TooManySym_01,T_TooManySym_02,T_TooManySym_03, T_XMDrugObfuBody_08,T_XMDrugObfuBody_12,XMNoVowels,XMSubLong autolearn=disabled version=3.4.1 X-Spam-Report: * -1.0 ALL_TRUSTED Passed through trusted hosts only via SMTP * 0.8 BAYES_50 BODY: Bayes spam probability is 40 to 60% * [score: 0.5000] * 1.5 XMNoVowels Alpha-numberic number with no vowels * 1.5 TR_Symld_Words too many words that have symbols inside * 0.7 XMSubLong Long Subject * 0.0 TVD_RCVD_IP Message was received from an IP address * 0.0 T_TM2_M_HEADER_IN_MSG BODY: No description available. * 1.2 LotsOfNums_01 BODY: Lots of long strings of numbers * -0.0 DCC_CHECK_NEGATIVE Not listed in DCC * [sa06 1397; Body=1 Fuz1=1 Fuz2=1] * 0.0 T_TooManySym_03 6+ unique symbols in subject * 1.0 T_XMDrugObfuBody_08 obfuscated drug references * 1.0 T_XMDrugObfuBody_12 obfuscated drug references * 0.0 T_TooManySym_01 4+ unique symbols in subject * 0.0 T_TooManySym_02 5+ unique symbols in subject X-Spam-DCC: XMission; sa06 1397; Body=1 Fuz1=1 Fuz2=1 X-Spam-Combo: ******;David Howells X-Spam-Relay-Country: X-Spam-Timing: total 737 ms - load_scoreonly_sql: 0.04 (0.0%), signal_user_changed: 8 (1.1%), b_tie_ro: 6 (0.9%), parse: 3.1 (0.4%), extract_message_metadata: 22 (3.0%), get_uri_detail_list: 7 (0.9%), tests_pri_-1000: 8 (1.1%), tests_pri_-950: 1.46 (0.2%), tests_pri_-900: 1.18 (0.2%), tests_pri_-400: 57 (7.8%), check_bayes: 55 (7.5%), b_tokenize: 25 (3.5%), b_tok_get_all: 15 (2.1%), b_comp_prob: 6 (0.7%), b_tok_touch_all: 6 (0.8%), b_finish: 0.81 (0.1%), tests_pri_0: 614 (83.2%), check_dkim_signature: 1.07 (0.1%), check_dkim_adsp: 3.2 (0.4%), tests_pri_10: 3.2 (0.4%), tests_pri_500: 12 (1.6%), rewrite_mail: 0.00 (0.0%) Subject: Re: [PATCH 01/34] vfs: syscall: Add open_tree(2) to reference or clone a mount [ver #12] X-Spam-Flag: No X-SA-Exim-Version: 4.2.1 (built Thu, 05 May 2016 13:38:54 -0600) X-SA-Exim-Scanned: Yes (on in02.mta.xmission.com) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org David Howells writes: > From: Al Viro > > open_tree(dfd, pathname, flags) > > Returns an O_PATH-opened file descriptor or an error. > dfd and pathname specify the location to open, in usual > fashion (see e.g. fstatat(2)). flags should be an OR of > some of the following: > * AT_PATH_EMPTY, AT_NO_AUTOMOUNT, AT_SYMLINK_NOFOLLOW - > same meanings as usual > * OPEN_TREE_CLOEXEC - make the resulting descriptor > close-on-exec > * OPEN_TREE_CLONE or OPEN_TREE_CLONE | AT_RECURSIVE - > instead of opening the location in question, create a detached > mount tree matching the subtree rooted at location specified by > dfd/pathname. With AT_RECURSIVE the entire subtree is cloned, > without it - only the part within in the mount containing the > location in question. In other words, the same as mount --rbind > or mount --bind would've taken. The detached tree will be > dissolved on the final close of obtained file. Creation of such > detached trees requires the same capabilities as doing mount --bind. What happens when mounts propgate to such a detached mount tree? It looks to me like the test in propagate_one for setting CL_UNPRIVILEGED will trigger a NULL pointer dereference: AKA: /* Notice when we are propagating across user namespaces */ if (m->mnt_ns->user_ns != user_ns) type |= CL_UNPRIVILEGED; Since we don't know which mount namespace if any this subtree is going into the test should become: if (!m->mnt_ns || (m->mnt_ns->user_ns != user_ns)) type |= CL_UNPRIVILEGED; If the tree is never attached anywhere it won't hurt as we don't allow mounts or umounts on the detached subtree. We don't have enough information to know about the namespace we copied from if it would cause CL_UNPRIVILEGED to be set on any given propagation. Similarly we don't have any information at all about the mount namespace this tree may possibily be copied to. Eric > Signed-off-by: Al Viro > Signed-off-by: David Howells > cc: linux-api@vger.kernel.org > --- > > arch/x86/entry/syscalls/syscall_32.tbl | 1 > arch/x86/entry/syscalls/syscall_64.tbl | 1 > fs/file_table.c | 9 +- > fs/internal.h | 1 > fs/namespace.c | 132 +++++++++++++++++++++++++++----- > include/linux/fs.h | 7 +- > include/linux/syscalls.h | 1 > include/uapi/linux/fcntl.h | 2 > include/uapi/linux/mount.h | 10 ++ > 9 files changed, 137 insertions(+), 27 deletions(-) > create mode 100644 include/uapi/linux/mount.h > > diff --git a/arch/x86/entry/syscalls/syscall_32.tbl b/arch/x86/entry/syscalls/syscall_32.tbl > index 3cf7b533b3d1..ea1b413afd47 100644 > --- a/arch/x86/entry/syscalls/syscall_32.tbl > +++ b/arch/x86/entry/syscalls/syscall_32.tbl > @@ -398,3 +398,4 @@ > 384 i386 arch_prctl sys_arch_prctl __ia32_compat_sys_arch_prctl > 385 i386 io_pgetevents sys_io_pgetevents __ia32_compat_sys_io_pgetevents > 386 i386 rseq sys_rseq __ia32_sys_rseq > +387 i386 open_tree sys_open_tree __ia32_sys_open_tree > diff --git a/arch/x86/entry/syscalls/syscall_64.tbl b/arch/x86/entry/syscalls/syscall_64.tbl > index f0b1709a5ffb..0545bed581dc 100644 > --- a/arch/x86/entry/syscalls/syscall_64.tbl > +++ b/arch/x86/entry/syscalls/syscall_64.tbl > @@ -343,6 +343,7 @@ > 332 common statx __x64_sys_statx > 333 common io_pgetevents __x64_sys_io_pgetevents > 334 common rseq __x64_sys_rseq > +335 common open_tree __x64_sys_open_tree > > # > # x32-specific system call numbers start at 512 to avoid cache impact > diff --git a/fs/file_table.c b/fs/file_table.c > index e49af4caf15d..e03c8d121c6c 100644 > --- a/fs/file_table.c > +++ b/fs/file_table.c > @@ -255,6 +255,7 @@ static void __fput(struct file *file) > struct dentry *dentry = file->f_path.dentry; > struct vfsmount *mnt = file->f_path.mnt; > struct inode *inode = file->f_inode; > + fmode_t mode = file->f_mode; > > if (unlikely(!(file->f_mode & FMODE_OPENED))) > goto out; > @@ -277,18 +278,20 @@ static void __fput(struct file *file) > if (file->f_op->release) > file->f_op->release(inode, file); > if (unlikely(S_ISCHR(inode->i_mode) && inode->i_cdev != NULL && > - !(file->f_mode & FMODE_PATH))) { > + !(mode & FMODE_PATH))) { > cdev_put(inode->i_cdev); > } > fops_put(file->f_op); > put_pid(file->f_owner.pid); > - if ((file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ) > + if ((mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ) > i_readcount_dec(inode); > - if (file->f_mode & FMODE_WRITER) { > + if (mode & FMODE_WRITER) { > put_write_access(inode); > __mnt_drop_write(mnt); > } > dput(dentry); > + if (unlikely(mode & FMODE_NEED_UNMOUNT)) > + dissolve_on_fput(mnt); > mntput(mnt); > out: > file_free(file); > diff --git a/fs/internal.h b/fs/internal.h > index 364c20b5ea2d..17029b30e196 100644 > --- a/fs/internal.h > +++ b/fs/internal.h > @@ -85,6 +85,7 @@ extern int __mnt_want_write_file(struct file *); > extern void __mnt_drop_write(struct vfsmount *); > extern void __mnt_drop_write_file(struct file *); > > +extern void dissolve_on_fput(struct vfsmount *); > /* > * fs_struct.c > */ > diff --git a/fs/namespace.c b/fs/namespace.c > index 8a7e1a7d1d06..ded1a970ec40 100644 > --- a/fs/namespace.c > +++ b/fs/namespace.c > @@ -20,12 +20,14 @@ > #include /* init_rootfs */ > #include /* get_fs_root et.al. */ > #include /* fsnotify_vfsmount_delete */ > +#include > #include > #include > #include > #include > #include > #include > +#include > > #include "pnode.h" > #include "internal.h" > @@ -1779,6 +1781,16 @@ struct vfsmount *collect_mounts(const struct path *path) > return &tree->mnt; > } > > +void dissolve_on_fput(struct vfsmount *mnt) > +{ > + namespace_lock(); > + lock_mount_hash(); > + mntget(mnt); > + umount_tree(real_mount(mnt), UMOUNT_CONNECTED); > + unlock_mount_hash(); > + namespace_unlock(); > +} > + > void drop_collected_mounts(struct vfsmount *mnt) > { > namespace_lock(); > @@ -2138,6 +2150,30 @@ static bool has_locked_children(struct mount *mnt, struct dentry *dentry) > return false; > } > > +static struct mount *__do_loopback(struct path *old_path, int recurse) > +{ > + struct mount *mnt = ERR_PTR(-EINVAL), *old = real_mount(old_path->mnt); > + > + if (IS_MNT_UNBINDABLE(old)) > + return mnt; > + > + if (!check_mnt(old) && old_path->dentry->d_op != &ns_dentry_operations) > + return mnt; > + > + if (!recurse && has_locked_children(old, old_path->dentry)) > + return mnt; > + > + if (recurse) > + mnt = copy_tree(old, old_path->dentry, CL_COPY_MNT_NS_FILE); > + else > + mnt = clone_mnt(old, old_path->dentry, 0); > + > + if (!IS_ERR(mnt)) > + mnt->mnt.mnt_flags &= ~MNT_LOCKED; > + > + return mnt; > +} > + > /* > * do loopback mount. > */ > @@ -2145,7 +2181,7 @@ static int do_loopback(struct path *path, const char *old_name, > int recurse) > { > struct path old_path; > - struct mount *mnt = NULL, *old, *parent; > + struct mount *mnt = NULL, *parent; > struct mountpoint *mp; > int err; > if (!old_name || !*old_name) > @@ -2159,38 +2195,21 @@ static int do_loopback(struct path *path, const char *old_name, > goto out; > > mp = lock_mount(path); > - err = PTR_ERR(mp); > - if (IS_ERR(mp)) > + if (IS_ERR(mp)) { > + err = PTR_ERR(mp); > goto out; > + } > > - old = real_mount(old_path.mnt); > parent = real_mount(path->mnt); > - > - err = -EINVAL; > - if (IS_MNT_UNBINDABLE(old)) > - goto out2; > - > if (!check_mnt(parent)) > goto out2; > > - if (!check_mnt(old) && old_path.dentry->d_op != &ns_dentry_operations) > - goto out2; > - > - if (!recurse && has_locked_children(old, old_path.dentry)) > - goto out2; > - > - if (recurse) > - mnt = copy_tree(old, old_path.dentry, CL_COPY_MNT_NS_FILE); > - else > - mnt = clone_mnt(old, old_path.dentry, 0); > - > + mnt = __do_loopback(&old_path, recurse); > if (IS_ERR(mnt)) { > err = PTR_ERR(mnt); > goto out2; > } > > - mnt->mnt.mnt_flags &= ~MNT_LOCKED; > - > err = graft_tree(mnt, parent, mp); > if (err) { > lock_mount_hash(); > @@ -2204,6 +2223,75 @@ static int do_loopback(struct path *path, const char *old_name, > return err; > } > > +SYSCALL_DEFINE3(open_tree, int, dfd, const char *, filename, unsigned, flags) > +{ > + struct file *file; > + struct path path; > + int lookup_flags = LOOKUP_AUTOMOUNT | LOOKUP_FOLLOW; > + bool detached = flags & OPEN_TREE_CLONE; > + int error; > + int fd; > + > + BUILD_BUG_ON(OPEN_TREE_CLOEXEC != O_CLOEXEC); > + > + if (flags & ~(AT_EMPTY_PATH | AT_NO_AUTOMOUNT | AT_RECURSIVE | > + AT_SYMLINK_NOFOLLOW | OPEN_TREE_CLONE | > + OPEN_TREE_CLOEXEC)) > + return -EINVAL; > + > + if ((flags & (AT_RECURSIVE | OPEN_TREE_CLONE)) == AT_RECURSIVE) > + return -EINVAL; > + > + if (flags & AT_NO_AUTOMOUNT) > + lookup_flags &= ~LOOKUP_AUTOMOUNT; > + if (flags & AT_SYMLINK_NOFOLLOW) > + lookup_flags &= ~LOOKUP_FOLLOW; > + if (flags & AT_EMPTY_PATH) > + lookup_flags |= LOOKUP_EMPTY; > + > + if (detached && !may_mount()) > + return -EPERM; > + > + fd = get_unused_fd_flags(flags & O_CLOEXEC); > + if (fd < 0) > + return fd; > + > + error = user_path_at(dfd, filename, lookup_flags, &path); > + if (error) > + goto out; > + > + if (detached) { > + struct mount *mnt = __do_loopback(&path, flags & AT_RECURSIVE); > + if (IS_ERR(mnt)) { > + error = PTR_ERR(mnt); > + goto out2; > + } > + mntput(path.mnt); > + path.mnt = &mnt->mnt; > + } > + > + file = dentry_open(&path, O_PATH, current_cred()); > + if (IS_ERR(file)) { > + error = PTR_ERR(file); > + goto out3; > + } > + > + if (detached) > + file->f_mode |= FMODE_NEED_UNMOUNT; > + path_put(&path); > + fd_install(fd, file); > + return fd; > + > +out3: > + if (detached) > + dissolve_on_fput(path.mnt); > +out2: > + path_put(&path); > +out: > + put_unused_fd(fd); > + return error; > +} > + > static int change_mount_flags(struct vfsmount *mnt, int ms_flags) > { > int error = 0; > diff --git a/include/linux/fs.h b/include/linux/fs.h > index 4323b8fe353d..6dc32507762f 100644 > --- a/include/linux/fs.h > +++ b/include/linux/fs.h > @@ -157,10 +157,13 @@ typedef int (dio_iodone_t)(struct kiocb *iocb, loff_t offset, > #define FMODE_NONOTIFY ((__force fmode_t)0x4000000) > > /* File is capable of returning -EAGAIN if I/O will block */ > -#define FMODE_NOWAIT ((__force fmode_t)0x8000000) > +#define FMODE_NOWAIT ((__force fmode_t)0x8000000) > + > +/* File represents mount that needs unmounting */ > +#define FMODE_NEED_UNMOUNT ((__force fmode_t)0x10000000) > > /* File does not contribute to nr_files count */ > -#define FMODE_NOACCOUNT ((__force fmode_t)0x20000000) > +#define FMODE_NOACCOUNT ((__force fmode_t)0x20000000) > > /* > * Flag for rw_copy_check_uvector and compat_rw_copy_check_uvector > diff --git a/include/linux/syscalls.h b/include/linux/syscalls.h > index 2ff814c92f7f..6978f3c76d41 100644 > --- a/include/linux/syscalls.h > +++ b/include/linux/syscalls.h > @@ -906,6 +906,7 @@ asmlinkage long sys_statx(int dfd, const char __user *path, unsigned flags, > unsigned mask, struct statx __user *buffer); > asmlinkage long sys_rseq(struct rseq __user *rseq, uint32_t rseq_len, > int flags, uint32_t sig); > +asmlinkage long sys_open_tree(int dfd, const char __user *path, unsigned flags); > > /* > * Architecture-specific system calls > diff --git a/include/uapi/linux/fcntl.h b/include/uapi/linux/fcntl.h > index 6448cdd9a350..594b85f7cb86 100644 > --- a/include/uapi/linux/fcntl.h > +++ b/include/uapi/linux/fcntl.h > @@ -90,5 +90,7 @@ > #define AT_STATX_FORCE_SYNC 0x2000 /* - Force the attributes to be sync'd with the server */ > #define AT_STATX_DONT_SYNC 0x4000 /* - Don't sync attributes with the server */ > > +#define AT_RECURSIVE 0x8000 /* Apply to the entire subtree */ > + > > #endif /* _UAPI_LINUX_FCNTL_H */ > diff --git a/include/uapi/linux/mount.h b/include/uapi/linux/mount.h > new file mode 100644 > index 000000000000..e8db2911adca > --- /dev/null > +++ b/include/uapi/linux/mount.h > @@ -0,0 +1,10 @@ > +#ifndef _UAPI_LINUX_MOUNT_H > +#define _UAPI_LINUX_MOUNT_H > + > +/* > + * open_tree() flags. > + */ > +#define OPEN_TREE_CLONE 1 /* Clone the target tree and attach the clone */ > +#define OPEN_TREE_CLOEXEC O_CLOEXEC /* Close the file on execve() */ > + > +#endif /* _UAPI_LINUX_MOUNT_H */