mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Al Viro <viro@ZenIV.linux.org.uk>
To: "Eric W. Biederman" <ebiederm@xmission.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>,
	"H. Peter Anvin" <hpa@zytor.com>,
	Peter Hurley <peter@hurleysoftware.com>, Greg KH <greg@kroah.com>,
	Jiri Slaby <jslaby@suse.com>,
	Aurelien Jarno <aurelien@aurel32.net>,
	Andy Lutomirski <luto@amacapital.net>,
	Florian Weimer <fw@deneb.enyo.de>,
	Serge Hallyn <serge.hallyn@ubuntu.com>,
	Jann Horn <jann@thejh.net>,
	"security@kernel.org" <security@kernel.org>,
	security@ubuntu.com, security@debian.org,
	Willy Tarreau <w@1wt.eu>,
	Linux Kernel Mailing List <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH 01/13] devpts: Teach /dev/ptmx to find the associated devpts via path lookup
Date: Tue, 5 Apr 2016 03:54:25 +0100	[thread overview]
Message-ID: <20160405025425.GK17997@ZenIV.linux.org.uk> (raw)
In-Reply-To: <1459819769-30387-1-git-send-email-ebiederm@xmission.com>

On Mon, Apr 04, 2016 at 08:29:17PM -0500, Eric W. Biederman wrote:

> +#ifdef CONFIG_DEVPTS_MULTIPLE_INSTANCES
> +static int devpts_path_ptmx(struct file *filp)
> +{
> +	struct pts_fs_info *fsi;
> +	struct path root, path;
> +	struct dentry *old;
> +	int err = -ENOENT;
> +	int ret;
> +
> +	/* Can the pts filesystem be found with a path walk? */
> +	path = filp->f_path;
> +	path_get(&path);
> +	get_fs_root(current->fs, &root);
> +	ret = path_parent(&root, &path);
> +	path_put(&root);
> +	if (ret != 1)
> +		goto fail;

That, I take it, is a lookup for .. and buggering off if it fails *or* if
we had been in caller's root or something that overmount it?  Not that the
latter had been possible - root is a directory and can be overmounted only
by another such, and we are called from ->open() of a device node.

> +	/* Remember the result of this permission check for later */
> +	ret = inode_permission(path.dentry->d_inode, MAY_EXEC);
> +	if (path_pts(&path))
> +		goto fail;

Egads, man - you've just introduced a special function for looking up
something named "pts" in a given directory!

The reason not to use kern_path() would be what, the fact that it doesn't
allow starting at given location?  So let's make a variant that would - and
rather than bothering with RCU, just go for something like (completely
untested)

/* on success overwrite *path with the result of walk; do _not_ drop the
   reference to old contents - let the caller arrange that */
int kern_path_relative(struct path *path, const char *s, int flags)
{
        int err;
        struct nameidata nd = {.path = *path};
	struct filename *name;

	if (!*s || *s == '/' || flags & (LOOKUP_ROOT | LOOKUP_RCU))
		return -EINVAL;

	name = getname_kernel(s);
        if (IS_ERR(name))
                return PTR_ERR(name);

        set_nameidata(&nd, AT_FDCWD, name);  

        nd.last_type = LAST_ROOT;
        nd.flags = flags | LOOKUP_REVAL | LOOKUP_JUMPED | LOOKUP_PARENT;
        nd.m_seq = read_seqbegin(&mount_lock);
	path_get(&nd.path);
	nd.inode = nd.path.dentry->d_inode;

        while (!(err = link_path_walk(s, &nd)) 
                && ((err = lookup_last(&nd)) > 0)) {
                s = trailing_symlink(&nd);
                if (IS_ERR(s)) {
                        err = PTR_ERR(s);
                        break;
                }
        }
        if (!err)
                err = complete_walk(&nd);

        if (!err && flags & LOOKUP_DIRECTORY)
                if (!d_can_lookup(nd.path.dentry))
                        err = -ENOTDIR;
        if (!err) {
                *path = nd.path;
                nd.path.mnt = NULL;
                nd.path.dentry = NULL;
        }
        terminate_walk(&nd);
        restore_nameidata();
        putname(name);
        return err;
}

and use it as

	path = filp->f_path;
	err = kern_path_relative(&path, "../pts", LOOKUP_DIRECTORY);
	if (err)
		return err;
	/* from here on we need to path_put() it */
	if (path.mnt->mnt_sb->s_magic != DEVPTS_SUPER_MAGIC)
		goto fail;
	/* must be its root; no other directories on that puppy */

> +	fsi = DEVPTS_SB(path.mnt->mnt_sb);
> +
> +	/* Get out if the path walk resulted in the default devpts instance */
> +	if (devpts_mnt->mnt_sb == path.mnt->mnt_sb)
> +		goto fail;
> +
> +	/* Don't allow bypassing the existing /dev/pts/ptmx permission check */

	err = inode_permission(path.dentry->d_inode, MAY_EXEC);
	if (err)
		goto fail;
	err = inode_permission(fsi->ptmx_dentry->d_inode,
				       ACC_MODE(filp->f_flags));
	if (err)
		goto fail;

> +	/* Advance path to the ptmx dentry */
> +	old = path.dentry;
> +	path.dentry = dget(fsi->ptmx_dentry);
> +	dput(old);
> +
> +	/* Make it look like /dev/pts/ptmx was opened */
> +	err = update_file_path(filp, &path);
> +	if (err)
> +		goto fail;
> +
> +	return 0;
> +fail:
> +	path_put(&path);
> +	return err;
> +}
> +#else
> +static inline int devpts_path_ptmx(struct file *filp)
> +{
> +	return -ENOENT;
> +}
> +#endif
> +
> +struct inode *devpts_ptmx(struct inode *inode, struct file *filp)
> +{
> +	int err;
> +	if (inode->i_sb->s_magic == DEVPTS_SUPER_MAGIC)
> +		return inode;
> +
> +	err = devpts_path_ptmx(filp);
> +	if (err == 0)
> +		return filp->f_inode;
> +	if (err != -ENOENT)
> +		return ERR_PTR(err);
> +
> +	return inode;
> +}

Umm...  I'm not sure it makes for good calling conventions - the caller can
do inode = file_inode(filp) just as well, so why not simply return 0 or -E...?
"return inode;" cases become simply return 0...

> --- a/fs/namei.c
> +++ b/fs/namei.c
> @@ -1415,29 +1415,41 @@ static void follow_mount(struct path *path)
>  	}
>  }
>  
> -static int follow_dotdot(struct nameidata *nd)
> +int path_parent(struct path *root, struct path *path)

Please, don't.

> +#ifdef CONFIG_UNIX98_PTYS
> +int path_pts(struct path *path)

Fuck, no.

> index 17cb6b1dab75..e1ed78fa474b 100644
> --- a/fs/open.c
> +++ b/fs/open.c
> @@ -679,6 +679,24 @@ int open_check_o_direct(struct file *f)
>  	return 0;
>  }
>  
> +int update_file_path(struct file *filp, struct path *path)
> +{
> +	/* Only valid during f_op->open, and even in open use very carefully */
> +	struct path old;
> +	struct inode *inode;
> +
> +	if (filp->f_mode & FMODE_WRITER)
> +		return -EINVAL;

That really needs to be commented.

> +	old = filp->f_path;
> +	inode = path->dentry->d_inode;
> +	filp->f_path = *path;
> +	filp->f_inode = inode;
> +	filp->f_mapping = inode->i_mapping;
> +	path_put(&old);
> +	return 0;
> +}

> +
>  static int do_dentry_open(struct file *f,
>  			  struct inode *inode,
>  			  int (*open)(struct inode *, struct file *),
> @@ -736,6 +754,7 @@ static int do_dentry_open(struct file *f,
>  		error = open(inode, f);
>  		if (error)
>  			goto cleanup_all;
> +		inode = f->f_inode;
>  	}
>  	if ((f->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
>  		i_readcount_inc(inode);

BTW, have you looked through the callers of dentry_open()?  It can hit that
case as well...

  parent reply	other threads:[~2016-04-05  2:55 UTC|newest]

Thread overview: 154+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <43AD2BA7-B594-4299-95F3-D86FD38AF21B@zytor.com>
     [not found] ` <87egexpf4o.fsf@x220.int.ebiederm.org>
     [not found]   ` <CA+55aFw9Bg+Zh_T4zP487n3ieaxoMHgZ_nNJVdpSR4kQK9gQ9w@mail.gmail.com>
     [not found]     ` <1CB621EF-1647-463B-A144-D611DB150E15@zytor.com>
     [not found]       ` <20151208223135.GA8352@kroah.com>
     [not found]         ` <87oae0h2bo.fsf@x220.int.ebiederm.org>
     [not found]           ` <56677DE3.5040705@zytor.com>
     [not found]             ` <20151209012311.GA11794@kroah.com>
     [not found]               ` <84B136DF-55E4-476A-9CB2-062B15677EE5@zytor.com>
     [not found]                 ` <20151209013859.GA12442@kroah.com>
     [not found]                   ` <20151209083225.GA30452@1wt.eu>
2015-12-11 19:40                     ` [PATCH] devpts: Sensible /dev/ptmx & force newinstance Eric W. Biederman
2015-12-11 20:50                       ` Linus Torvalds
2015-12-11 21:03                         ` Eric W. Biederman
2015-12-11 21:04                       ` Al Viro
2015-12-11 21:11                         ` Eric W. Biederman
2015-12-11 21:48                           ` Andy Lutomirski
2015-12-11 22:07                             ` H. Peter Anvin
2015-12-11 22:12                               ` Andy Lutomirski
2015-12-11 22:18                                 ` H. Peter Anvin
2015-12-11 22:24                                   ` Andy Lutomirski
2015-12-11 22:29                                     ` H. Peter Anvin
2015-12-11 22:35                                 ` Eric W. Biederman
2015-12-11 22:52                                   ` Andy Lutomirski
2015-12-11 22:58                                     ` Jann Horn
2015-12-11 23:00                                       ` Andy Lutomirski
2015-12-11 23:07                                         ` H. Peter Anvin
2015-12-11 23:16                                           ` Andy Lutomirski
2015-12-11 23:30                                             ` H. Peter Anvin
2015-12-11 22:57                                   ` H. Peter Anvin
2015-12-14 19:47                       ` Peter Hurley
2015-12-14 19:55                         ` H. Peter Anvin
2015-12-19 21:13                         ` Eric W. Biederman
2015-12-20  4:11                           ` Eric W. Biederman
2015-12-20  4:35                             ` H. Peter Anvin
2015-12-20  9:42                               ` Eric W. Biederman
2015-12-21 22:03                                 ` Eric W. Biederman
2015-12-21 22:23                                   ` Linus Torvalds
2016-04-05  0:03                                     ` [PATCH 00/13] devpts: New instances for every mount Eric W. Biederman
2016-04-05  1:29                                       ` [PATCH 01/13] devpts: Teach /dev/ptmx to find the associated devpts via path lookup Eric W. Biederman
2016-04-05  1:29                                         ` [PATCH 02/13] devpts: More obvious check for the system devpts in pty allocation Eric W. Biederman
2016-04-05  1:29                                         ` [PATCH 03/13] devpts: Cleanup newinstance parsing Eric W. Biederman
2016-04-05  1:29                                         ` [PATCH 04/13] devpts: Stop rolling devpts_remount by hand in devpts_mount Eric W. Biederman
2016-04-05  1:29                                         ` [PATCH 05/13] devpts: Fail early (if appropriate) on overmount Eric W. Biederman
2016-04-05  1:29                                         ` [PATCH 06/13] devpts: Use the same default mode for both /dev/ptmx and dev/pts/ptmx Eric W. Biederman
2016-04-05  1:29                                         ` [PATCH 07/13] devpts: Move parse_mount_options into fill_super Eric W. Biederman
2016-04-05  1:29                                         ` [PATCH 08/13] devpts: Make devpts_kill_sb safe if fsi is NULL Eric W. Biederman
2016-04-05  1:29                                         ` [PATCH 09/13] devpts: Move the creation of /dev/pts/ptmx into fill_super Eric W. Biederman
2016-04-05  1:29                                         ` [PATCH 10/13] devpts: Simplify devpts_mount by using mount_nodev Eric W. Biederman
2016-04-05  1:29                                         ` [PATCH 11/13] vfs: Implement mount_super_once Eric W. Biederman
2016-04-05  1:29                                         ` [PATCH 12/13] devpts: Always return a distinct instance when mounting Eric W. Biederman
2016-04-05  1:29                                         ` [PATCH 13/13] devpts: Kill the DEVPTS_MULTIPLE_INSTANCE config option Eric W. Biederman
2016-04-05  2:54                                         ` Al Viro [this message]
2016-04-05  3:03                                           ` [PATCH 01/13] devpts: Teach /dev/ptmx to find the associated devpts via path lookup Al Viro
2016-04-08 18:54                                             ` Eric W. Biederman
2016-04-07 16:06                                         ` Linus Torvalds
2016-04-08 18:51                                           ` Eric W. Biederman
2016-04-08 19:05                                             ` Linus Torvalds
2016-04-08 20:05                                               ` Eric W. Biederman
2016-04-08 20:43                                               ` Andy Lutomirski
2016-04-08 21:29                                                 ` Eric W. Biederman
2016-04-08 21:54                                                   ` Linus Torvalds
2016-04-08 23:03                                                     ` Eric W. Biederman
2016-04-08 21:56                                                   ` Andy Lutomirski
2016-04-09 13:09                                             ` One Thousand Gnomes
2016-04-09 14:10                                               ` H. Peter Anvin
2016-04-09 14:45                                                 ` Eric W. Biederman
2016-04-09 22:37                                                   ` H. Peter Anvin
2016-04-10  0:01                                                     ` Linus Torvalds
2016-04-10  0:06                                                       ` H. Peter Anvin
2016-04-10  0:16                                                         ` Linus Torvalds
2016-04-10  0:44                                                           ` Andy Lutomirski
     [not found]                                                             ` <CA+55aFzs00iDkYhvFCq=AZMVcNL0+oZT4SeimTeVurJq=5ZS3A@mail.gmail.com>
2016-04-11 14:48                                                               ` H. Peter Anvin
2016-04-12  1:31                                                                 ` Al Viro
2016-04-11 20:12                                                               ` Andy Lutomirski
2016-04-11 20:10                                                                 ` Eric W. Biederman
2016-04-11 20:16                                                                 ` H. Peter Anvin
2016-04-11 23:37                                                                   ` Eric W. Biederman
2016-04-12  0:01                                                                     ` Linus Torvalds
2016-04-12  0:10                                                                       ` Eric W. Biederman
2016-04-12  1:06                                                                         ` H. Peter Anvin
2016-04-12  1:18                                                                           ` Linus Torvalds
2016-04-12  1:23                                                                           ` Eric W. Biederman
2016-04-12  1:47                                                                             ` Al Viro
2016-04-12  1:34                                                                         ` Al Viro
2016-04-12  2:16                                                                           ` Eric W. Biederman
2016-04-12 17:44                                                                 ` Andy Lutomirski
2016-04-12 18:12                                                                   ` Linus Torvalds
2016-04-12 19:07                                                                     ` H. Peter Anvin
2016-04-15 15:34                                                                       ` [PATCH 01/16] devpts: Attempting to get it right Eric W. Biederman
2016-04-15 15:35                                                                         ` [PATCH 01/16] devpts: Use the same default mode for both /dev/ptmx and dev/pts/ptmx Eric W. Biederman
2016-04-15 15:35                                                                           ` [PATCH 02/16] devpts: Set the proper fops for /dev/pts/ptmx Eric W. Biederman
2016-04-15 15:35                                                                           ` [PATCH 03/16] vfs: Implement vfs_loopback_mount Eric W. Biederman
2016-04-15 15:35                                                                           ` [PATCH 04/16] devpts: Teach /dev/ptmx to automount the appropriate devpts via path lookup Eric W. Biederman
2016-04-15 22:03                                                                             ` Jann Horn
2016-04-19 18:46                                                                               ` Eric W. Biederman
2016-04-15 15:35                                                                           ` [PATCH 05/16] vfs: Allow unlink, and rename on expirable file mounts Eric W. Biederman
2016-04-15 15:35                                                                           ` [PATCH 06/16] devpts: More obvious check for the system devpts in pty allocation Eric W. Biederman
2016-04-15 15:35                                                                           ` [PATCH 07/16] devpts: Cleanup newinstance parsing Eric W. Biederman
2016-04-15 15:35                                                                           ` [PATCH 08/16] devpts: Stop rolling devpts_remount by hand in devpts_mount Eric W. Biederman
2016-04-15 15:35                                                                           ` [PATCH 09/16] devpts: Fail early (if appropriate) on overmount Eric W. Biederman
2016-04-15 15:35                                                                           ` [PATCH 10/16] devpts: Move parse_mount_options into fill_super Eric W. Biederman
2016-04-15 15:35                                                                           ` [PATCH 11/16] devpts: Make devpts_kill_sb safe if fsi is NULL Eric W. Biederman
2016-04-15 15:35                                                                           ` [PATCH 12/16] devpts: Move the creation of /dev/pts/ptmx into fill_super Eric W. Biederman
2016-04-15 15:35                                                                           ` [PATCH 13/16] devpts: Simplify devpts_mount by using mount_nodev Eric W. Biederman
2016-04-15 15:35                                                                           ` [PATCH 14/16] vfs: Implement mount_super_once Eric W. Biederman
2016-04-15 23:02                                                                             ` Linus Torvalds
2016-04-19 18:22                                                                               ` Eric W. Biederman
2016-04-19 18:47                                                                                 ` H. Peter Anvin
2016-04-19 19:03                                                                                   ` Eric W. Biederman
2016-04-19 19:25                                                                                     ` H. Peter Anvin
2016-04-19 19:26                                                                                       ` H. Peter Anvin
2016-04-20  3:27                                                                                         ` Eric W. Biederman
2016-04-20 11:50                                                                                           ` Austin S. Hemmelgarn
2016-04-20 16:12                                                                                             ` H. Peter Anvin
2016-04-19 18:55                                                                                 ` H. Peter Anvin
2016-04-19 23:29                                                                                 ` Linus Torvalds
2016-04-20  1:24                                                                                   ` Linus Torvalds
2016-04-20  1:37                                                                                     ` H. Peter Anvin
2016-04-15 15:35                                                                           ` [PATCH 15/16] devpts: Always return a distinct instance when mounting Eric W. Biederman
2016-04-15 15:35                                                                           ` [PATCH 16/16] devpts: Kill the DEVPTS_MULTIPLE_INSTANCE config option Eric W. Biederman
2016-04-15 16:49                                                                         ` [PATCH 01/16] devpts: Attempting to get it right Andy Lutomirski
2016-04-15 20:43                                                                           ` Eric W. Biederman
2016-04-15 21:29                                                                             ` H. Peter Anvin
2016-04-19 19:00                                                                               ` Eric W. Biederman
2016-04-16 18:31                                                                         ` Linus Torvalds
2016-04-19 18:44                                                                           ` Does anyone care about a race free ptsname? Eric W. Biederman
2016-04-19 19:16                                                                             ` H. Peter Anvin
2016-04-19 20:32                                                                               ` Eric W. Biederman
2016-04-19 20:55                                                                                 ` H. Peter Anvin
2016-04-19 20:42                                                                             ` Serge E. Hallyn
2016-04-19 23:23                                                                             ` Linus Torvalds
2016-04-19 23:39                                                                               ` H. Peter Anvin
2016-04-20  0:18                                                                                 ` Linus Torvalds
2016-04-20  1:48                                                                                 ` Serge E. Hallyn
2016-04-19 22:06                                                                           ` [PATCH 01/16] devpts: Attempting to get it right Eric W. Biederman
2016-04-19 23:35                                                                             ` Linus Torvalds
2016-04-20  0:24                                                                               ` Peter Hurley
2016-04-20  0:49                                                                                 ` Peter Hurley
2016-04-20  3:04                                                                               ` [PATCH] devpts: Make each mount of devpts an independent filesystem Eric W. Biederman
2016-04-20  3:25                                                                                 ` Al Viro
2016-04-20  3:43                                                                                   ` Eric W. Biederman
2016-04-20  4:11                                                                                     ` Al Viro
2016-04-20  4:21                                                                                       ` Eric W. Biederman
2016-04-20  4:36                                                                                 ` Konstantin Khlebnikov
2016-04-20  4:49                                                                                   ` Linus Torvalds
2016-04-20 14:55                                                                                     ` Eric W. Biederman
2016-04-20 15:34                                                                                       ` Konstantin Khlebnikov
2016-04-20 15:50                                                                                         ` Eric W. Biederman
2016-04-20 17:00                                                                                         ` [PATCH v2] " Eric W. Biederman
     [not found]                                                                                           ` <874mabt3df.fsf_-_@x220.int.ebiederm.org>
2016-05-06 19:04                                                                                             ` [PATCH 1/1] " Eric W. Biederman
2016-05-06 19:35                                                                                             ` [PATCH 0/1] devpts: Removing the need for pt_chown Greg KH
2016-05-06 19:45                                                                                               ` Peter Hurley
2016-05-06 19:54                                                                                                 ` Greg KH
2016-06-02 15:29                                                                                                   ` [PATCH tty-next] devpts: Make each mount of devpts an independent filesystem Eric W. Biederman
2016-06-02 18:57                                                                                                     ` Linus Torvalds
2016-06-02 20:22                                                                                                       ` Eric W. Biederman
2016-06-02 20:36                                                                                                         ` H. Peter Anvin
2016-06-02 21:23                                                                                                           ` Eric W. Biederman
2016-06-02 21:44                                                                                                             ` Linus Torvalds
2016-04-11 23:49                                                               ` [PATCH 01/13] devpts: Teach /dev/ptmx to find the associated devpts via path lookup Eric W. Biederman
2016-04-12  0:08                                                                 ` Linus Torvalds
2016-04-12  0:22                                                                   ` Eric W. Biederman
2016-04-12  0:50                                                                     ` Linus Torvalds
2016-04-11 20:05                                                       ` Eric W. Biederman

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20160405025425.GK17997@ZenIV.linux.org.uk \
    --to=viro@zeniv.linux.org.uk \
    --cc=aurelien@aurel32.net \
    --cc=ebiederm@xmission.com \
    --cc=fw@deneb.enyo.de \
    --cc=greg@kroah.com \
    --cc=hpa@zytor.com \
    --cc=jann@thejh.net \
    --cc=jslaby@suse.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=luto@amacapital.net \
    --cc=peter@hurleysoftware.com \
    --cc=security@debian.org \
    --cc=security@kernel.org \
    --cc=security@ubuntu.com \
    --cc=serge.hallyn@ubuntu.com \
    --cc=torvalds@linux-foundation.org \
    --cc=w@1wt.eu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

Powered by JetHome