qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Greg Kurz <groug@kaod.org>
To: Eric Blake <eblake@redhat.com>
Cc: qemu-devel@nongnu.org, Jann Horn <jannh@google.com>,
	Prasad J Pandit <prasad@redhat.com>,
	"Aneesh Kumar K.V" <aneesh.kumar@linux.vnet.ibm.com>,
	Stefan Hajnoczi <stefanha@redhat.com>
Subject: Re: [Qemu-devel] [PATCH v2 04/28] 9pfs: introduce openat_nofollow() helper
Date: Tue, 28 Feb 2017 01:32:24 +0100	[thread overview]
Message-ID: <20170228013224.5d8fe797@bahia.lan> (raw)
In-Reply-To: <851bab51-f448-e21a-32df-c2b2e3b8dd8d@redhat.com>

[-- Attachment #1: Type: text/plain, Size: 2962 bytes --]

On Mon, 27 Feb 2017 17:28:33 -0600
Eric Blake <eblake@redhat.com> wrote:

> On 02/26/2017 04:42 PM, Greg Kurz wrote:
> > When using the passthrough security mode, symbolic links created by the
> > guest are actual symbolic links on the host file system.
> >   
> 
> > 
> > diff --git a/hw/9pfs/9p-util.c b/hw/9pfs/9p-util.c
> > new file mode 100644
> > index 000000000000..62fd7a76212a
> > --- /dev/null  
> 
> > +int openat_nofollow(int dirfd, const char *path, int flags, mode_t mode)
> > +{
> > +    int fd;
> > +
> > +    fd = dup(dirfd);
> > +    if (fd == -1) {
> > +        return -1;
> > +    }
> > +  
> 
> Do you want to assert that the caller's path does not start with '/'?

Yes, I've added this for the pull request.

> This function ignores dirfd in that case, which may not be what you want.
> 

Indeed, it really needs the path to be relative.

> > +    while (*path) {
> > +        const char *c;
> > +        int next_fd;
> > +        char *head;
> > +
> > +        head = g_strdup(path);
> > +        c = strchr(path, '/');  
> 
> So if the caller passes path="a//b", then the first iteration sets
> head="a", but the second iteration sets head="".
> 

This doesn't happen with the current code, but you're right, we should
assert here also. We only wany a/b/c/d

> 
> > +        if (c) {
> > +            head[c - path] = 0;
> > +            next_fd = openat_dir(fd, head);  
> 
> The second iteration will then fail (openat_dir on "" should fail with
> ENOENT, right?).  Oops.
> 
> > +        } else {
> > +            next_fd = openat_file(fd, head, flags, mode);
> > +        }
> > +        g_free(head);
> > +        if (next_fd == -1) {
> > +            close_preserve_errno(fd);
> > +            return -1;
> > +        }
> > +        close(fd);
> > +        fd = next_fd;
> > +
> > +        if (!c) {
> > +            break;
> > +        }
> > +        path = c + 1;  
> 
> I think the fix is that you should skip past all consecutive '/' here,
> rather than assuming there is just one.  Or can you assert that all
> callers are well-behaved, and that *path is not '/' at this point?
> 

Again you're right :-\

> > +    }  
> 
> > +static inline int openat_file(int dirfd, const char *name, int flags,
> > +                              mode_t mode)
> > +{
> > +    int fd, serrno;
> > +
> > +    fd = openat(dirfd, name, flags | O_NOFOLLOW | O_NOCTTY | O_NONBLOCK,
> > +                mode);
> > +    if (fd == -1) {
> > +        return -1;
> > +    }
> > +
> > +    serrno = errno;
> > +    /* O_NONBLOCK was only needed to open the file. Let's drop it. */
> > +    assert(!fcntl(fd, F_SETFL, flags));  
> 
> Ouch - side effect inside an assertion.  We don't support use of NDEBUG,
> but this is poor practice.
> 

And I now remember you already made a similar comment in the past... I hope
I will remember this time.

Thanks!

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 181 bytes --]

  reply	other threads:[~2017-02-28  0:32 UTC|newest]

Thread overview: 47+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-02-26 22:41 [Qemu-devel] [PATCH v2 00/28] Series short description Greg Kurz
2017-02-26 22:41 ` [Qemu-devel] [PATCH v2 01/28] 9pfs: local: move xattr security ops to 9p-xattr.c Greg Kurz
2017-02-26 22:41 ` [Qemu-devel] [PATCH v2 02/28] 9pfs: remove side-effects in local_init() Greg Kurz
2017-02-26 22:41 ` [Qemu-devel] [PATCH v2 03/28] 9pfs: remove side-effects in local_open() and local_opendir() Greg Kurz
2017-02-26 22:42 ` [Qemu-devel] [PATCH v2 04/28] 9pfs: introduce openat_nofollow() helper Greg Kurz
2017-02-27 12:44   ` Stefan Hajnoczi
2017-02-27 14:31     ` Greg Kurz
2017-02-27 15:32       ` Stefan Hajnoczi
2017-02-27 23:28   ` Eric Blake
2017-02-28  0:32     ` Greg Kurz [this message]
2017-02-26 22:42 ` [Qemu-devel] [PATCH v2 05/28] 9pfs: local: keep a file descriptor on the shared folder Greg Kurz
2017-02-26 22:42 ` [Qemu-devel] [PATCH v2 06/28] 9pfs: local: open/opendir: don't follow symlinks Greg Kurz
2017-02-27 12:49   ` Stefan Hajnoczi
2017-02-27 14:35     ` Greg Kurz
2017-02-26 22:42 ` [Qemu-devel] [PATCH v2 07/28] 9pfs: local: lgetxattr: " Greg Kurz
2017-02-27 12:58   ` Stefan Hajnoczi
2017-02-26 22:42 ` [Qemu-devel] [PATCH v2 08/28] 9pfs: local: llistxattr: " Greg Kurz
2017-02-27 13:08   ` Stefan Hajnoczi
2017-02-26 22:42 ` [Qemu-devel] [PATCH v2 09/28] 9pfs: local: lsetxattr: " Greg Kurz
2017-02-27 13:10   ` Stefan Hajnoczi
2017-02-26 22:42 ` [Qemu-devel] [PATCH v2 10/28] 9pfs: local: lremovexattr: " Greg Kurz
2017-02-27 13:12   ` Stefan Hajnoczi
2017-02-26 22:43 ` [Qemu-devel] [PATCH v2 11/28] 9pfs: local: unlinkat: " Greg Kurz
2017-02-27 13:14   ` Stefan Hajnoczi
2017-02-26 22:43 ` [Qemu-devel] [PATCH v2 12/28] 9pfs: local: remove: " Greg Kurz
2017-02-26 22:43 ` [Qemu-devel] [PATCH v2 13/28] 9pfs: local: utimensat: " Greg Kurz
2017-02-26 22:43 ` [Qemu-devel] [PATCH v2 14/28] 9pfs: local: statfs: " Greg Kurz
2017-02-26 22:43 ` [Qemu-devel] [PATCH v2 15/28] 9pfs: local: truncate: " Greg Kurz
2017-02-26 22:43 ` [Qemu-devel] [PATCH v2 16/28] 9pfs: local: readlink: " Greg Kurz
2017-02-26 22:43 ` [Qemu-devel] [PATCH v2 17/28] 9pfs: local: lstat: " Greg Kurz
2017-02-26 22:43 ` [Qemu-devel] [PATCH v2 18/28] 9pfs: local: renameat: " Greg Kurz
2017-02-26 22:44 ` [Qemu-devel] [PATCH v2 19/28] 9pfs: local: rename: use renameat Greg Kurz
2017-02-26 22:44 ` [Qemu-devel] [PATCH v2 20/28] 9pfs: local: improve error handling in link op Greg Kurz
2017-02-26 22:44 ` [Qemu-devel] [PATCH v2 21/28] 9pfs: local: link: don't follow symlinks Greg Kurz
2017-02-26 22:44 ` [Qemu-devel] [PATCH v2 22/28] 9pfs: local: chmod: " Greg Kurz
2017-02-27 13:17   ` Stefan Hajnoczi
2017-02-26 22:44 ` [Qemu-devel] [PATCH v2 23/28] 9pfs: local: chown: " Greg Kurz
2017-02-26 22:44 ` [Qemu-devel] [PATCH v2 24/28] 9pfs: local: symlink: " Greg Kurz
2017-02-26 22:44 ` [Qemu-devel] [PATCH v2 25/28] 9pfs: local: mknod: " Greg Kurz
2017-02-27 13:18   ` Stefan Hajnoczi
2017-02-26 22:45 ` [Qemu-devel] [PATCH v2 26/28] 9pfs: local: mkdir: " Greg Kurz
2017-02-26 22:45 ` [Qemu-devel] [PATCH v2 27/28] 9pfs: local: open2: " Greg Kurz
2017-02-27 13:18   ` Stefan Hajnoczi
2017-02-26 22:45 ` [Qemu-devel] [PATCH v2 28/28] 9pfs: local: drop unused code Greg Kurz
2017-02-26 23:45 ` [Qemu-devel] [PATCH v2 00/28] 9pfs: local: fix vulnerability to symlink attacks Greg Kurz
2017-02-27 13:24 ` [Qemu-devel] [PATCH v2 00/28] Series short description Stefan Hajnoczi
2017-02-27 15:33 ` Stefan Hajnoczi

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=20170228013224.5d8fe797@bahia.lan \
    --to=groug@kaod.org \
    --cc=aneesh.kumar@linux.vnet.ibm.com \
    --cc=eblake@redhat.com \
    --cc=jannh@google.com \
    --cc=prasad@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=stefanha@redhat.com \
    /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;
as well as URLs for NNTP newsgroup(s).