Message ID | 20160616065738.GA7154@1wt.eu (mailing list archive) |
---|---|
State | New, archived |
Headers | show |
On Thu, Jun 16, 2016 at 08:57:38AM +0200, Willy Tarreau wrote: (...) > + /* avoid reading a whole page if the FS only needs a string. */ > + if (!(type->fs_flags & FS_BINARY_MOUNTDATA)) { > + strlcpy(copy, data, PAGE_SIZE); BTW, I forgot that we're first supposed to come from user, make that strndup_user() instead. Willy -- To unsubscribe from this list: send the line "unsubscribe linux-fsdevel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
On Thu, Jun 16, 2016 at 08:57:38AM +0200, Willy Tarreau wrote: > + type = get_fs_type(fstype); > + if (!type) > + return NULL; > + > copy = kmalloc(PAGE_SIZE, GFP_KERNEL); > if (!copy) > return ERR_PTR(-ENOMEM); > > + /* avoid reading a whole page if the FS only needs a string. */ > + if (!(type->fs_flags & FS_BINARY_MOUNTDATA)) { > + strlcpy(copy, data, PAGE_SIZE); > + return copy; a) it leaks a file_system_type reference b) data is a userland pointer, for crying out loud! -- To unsubscribe from this list: send the line "unsubscribe linux-fsdevel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
On Thu, Jun 16, 2016 at 08:08:22AM +0100, Al Viro wrote: > On Thu, Jun 16, 2016 at 08:57:38AM +0200, Willy Tarreau wrote: > > + type = get_fs_type(fstype); > > + if (!type) > > + return NULL; > > + > > copy = kmalloc(PAGE_SIZE, GFP_KERNEL); > > if (!copy) > > return ERR_PTR(-ENOMEM); > > > > + /* avoid reading a whole page if the FS only needs a string. */ > > + if (!(type->fs_flags & FS_BINARY_MOUNTDATA)) { > > + strlcpy(copy, data, PAGE_SIZE); > > + return copy; > > a) it leaks a file_system_type reference I was not sure about this one, thanks for confirming. > b) data is a userland pointer, for crying out loud! Yep I noticed it and fixed it after sending. I was focused on the data coming from kernel due to the discussion. I also think that since there are only two call places for copy_mount_options(), we may move the test there and switch to copy_mount_string() instead depending on the fs type. Thanks, Willy -- To unsubscribe from this list: send the line "unsubscribe linux-fsdevel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
On Thu, Jun 16, 2016 at 09:25:57AM +0200, Willy Tarreau wrote: > On Thu, Jun 16, 2016 at 08:08:22AM +0100, Al Viro wrote: > > On Thu, Jun 16, 2016 at 08:57:38AM +0200, Willy Tarreau wrote: > > > + type = get_fs_type(fstype); > > > + if (!type) > > > + return NULL; > > > + > > > copy = kmalloc(PAGE_SIZE, GFP_KERNEL); > > > if (!copy) > > > return ERR_PTR(-ENOMEM); > > > > > > + /* avoid reading a whole page if the FS only needs a string. */ > > > + if (!(type->fs_flags & FS_BINARY_MOUNTDATA)) { > > > + strlcpy(copy, data, PAGE_SIZE); > > > + return copy; > > > > a) it leaks a file_system_type reference > > I was not sure about this one, thanks for confirming. > > > b) data is a userland pointer, for crying out loud! > > Yep I noticed it and fixed it after sending. I was focused on the > data coming from kernel due to the discussion. > > I also think that since there are only two call places for > copy_mount_options(), we may move the test there and switch > to copy_mount_string() instead depending on the fs type. Another problem is that it will oops with NULL fstype, which is absolutely normal both for mount --bind *and* mount -o remount. And while mount --bind doesn't care about string options, mount -o remount certainly does. IMO the latter makes that approach hopeless - with remount you don't know the type until well into do_mount() guts and I'd really hate to carry the userland pointer all the way into it. -- To unsubscribe from this list: send the line "unsubscribe linux-fsdevel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
On Thu, Jun 16, 2016 at 09:02:29AM +0100, Al Viro wrote: > On Thu, Jun 16, 2016 at 09:25:57AM +0200, Willy Tarreau wrote: > > On Thu, Jun 16, 2016 at 08:08:22AM +0100, Al Viro wrote: > > > On Thu, Jun 16, 2016 at 08:57:38AM +0200, Willy Tarreau wrote: > > > > + type = get_fs_type(fstype); > > > > + if (!type) > > > > + return NULL; > > > > + > > > > copy = kmalloc(PAGE_SIZE, GFP_KERNEL); > > > > if (!copy) > > > > return ERR_PTR(-ENOMEM); > > > > > > > > + /* avoid reading a whole page if the FS only needs a string. */ > > > > + if (!(type->fs_flags & FS_BINARY_MOUNTDATA)) { > > > > + strlcpy(copy, data, PAGE_SIZE); > > > > + return copy; > > > > > > a) it leaks a file_system_type reference > > > > I was not sure about this one, thanks for confirming. > > > > > b) data is a userland pointer, for crying out loud! > > > > Yep I noticed it and fixed it after sending. I was focused on the > > data coming from kernel due to the discussion. > > > > I also think that since there are only two call places for > > copy_mount_options(), we may move the test there and switch > > to copy_mount_string() instead depending on the fs type. > > Another problem is that it will oops with NULL fstype, which is > absolutely normal both for mount --bind *and* mount -o remount. OK thanks for explaining, I didn't know. > And while mount --bind doesn't care about string options, > mount -o remount certainly does. IMO the latter makes that > approach hopeless - with remount you don't know the type > until well into do_mount() guts and I'd really hate to carry > the userland pointer all the way into it. Agreed. However if the initial point was to avoid reading extra pages most of the time, we could possibly consider that the string copy is an optimization for the case where we have the information available. Thus if !fstype || !(type->fs_flags & FS_BINARY_MOUNTDATA) then we use copy_mount_options() otherwise we use copy_mount_string(). It will only leave the full page copy for mount --bind or -o remount then. Willy -- To unsubscribe from this list: send the line "unsubscribe linux-fsdevel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
diff --git a/fs/compat.c b/fs/compat.c index be6e48b..8494766 100644 --- a/fs/compat.c +++ b/fs/compat.c @@ -806,7 +806,7 @@ COMPAT_SYSCALL_DEFINE5(mount, const char __user *, dev_name, if (IS_ERR(kernel_dev)) goto out1; - options = copy_mount_options(data); + options = copy_mount_options(type, data); retval = PTR_ERR(options); if (IS_ERR(options)) goto out2; diff --git a/fs/internal.h b/fs/internal.h index b71deee..3c9bc7b 100644 --- a/fs/internal.h +++ b/fs/internal.h @@ -55,7 +55,7 @@ extern int vfs_path_lookup(struct dentry *, struct vfsmount *, /* * namespace.c */ -extern void *copy_mount_options(const void __user *); +extern void *copy_mount_options(const char __user *, const void __user *); extern char *copy_mount_string(const void __user *); extern struct vfsmount *lookup_mnt(struct path *); diff --git a/fs/namespace.c b/fs/namespace.c index 4fb1691..cf28d08 100644 --- a/fs/namespace.c +++ b/fs/namespace.c @@ -2609,19 +2609,30 @@ static long exact_copy_from_user(void *to, const void __user * from, return n; } -void *copy_mount_options(const void __user * data) +void *copy_mount_options(const void __user *fstype, const void __user * data) { int i; unsigned long size; char *copy; + struct file_system_type *type; if (!data) return NULL; + type = get_fs_type(fstype); + if (!type) + return NULL; + copy = kmalloc(PAGE_SIZE, GFP_KERNEL); if (!copy) return ERR_PTR(-ENOMEM); + /* avoid reading a whole page if the FS only needs a string. */ + if (!(type->fs_flags & FS_BINARY_MOUNTDATA)) { + strlcpy(copy, data, PAGE_SIZE); + return copy; + } + /* We only care that *some* data at the address the user * gave us is valid. Just in case, we'll zero * the remainder of the page. @@ -2917,7 +2928,7 @@ SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name, if (IS_ERR(kernel_dev)) goto out_dev; - options = copy_mount_options(data); + options = copy_mount_options(type, data); ret = PTR_ERR(options); if (IS_ERR(options)) goto out_data;