qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Greg Kurz <groug@kaod.org>
To: Stefan Hajnoczi <stefanha@gmail.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: Mon, 27 Feb 2017 15:31:47 +0100	[thread overview]
Message-ID: <20170227153147.7875ca9c@bahia.lan> (raw)
In-Reply-To: <20170227124430.GG28403@stefanha-x1.localdomain>

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

On Mon, 27 Feb 2017 12:44:30 +0000
Stefan Hajnoczi <stefanha@gmail.com> wrote:

> On Sun, Feb 26, 2017 at 11:42:03PM +0100, Greg Kurz wrote:
> > +int openat_nofollow(int dirfd, const char *path, int flags, mode_t mode)
> > +{
> > +    int fd;
> > +
> > +    fd = dup(dirfd);
> > +    if (fd == -1) {
> > +        return -1;
> > +    }
> > +
> > +    while (*path) {
> > +        const char *c;
> > +        int next_fd;
> > +        char *head;
> > +
> > +        head = g_strdup(path);
> > +        c = strchr(path, '/');
> > +        if (c) {
> > +            head[c - path] = 0;
> > +            next_fd = openat_dir(fd, head);
> > +        } 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;
> > +    }
> > +
> > +    return fd;
> > +}  
> 
> If I understand the Linux openat(2) implementation correctly this
> function fails with ENOENT if:
> 
> 1. An absolute path is given
> 2. A path contains consecutive slashes ("a///b")
> 
> Both of these behaviors are problematic.  If the function doesn't
> support absolute paths it should be called relative_openat_nofollow()
> and have an error if path[0] == '/'.
> 

I agree with the name change since we don't want this function to deal
with absolute names, per-design. It shouldn't even return an error but
rather assert (see below for more details).

> I believe guests can pass in paths with consecutive slashes, so the
> function must cope with them.

Actually no. The 9P protocol implements paths as an array of path
elements, and the frontend prohibits '/' characters in individual
path elements (look for name_is_illegal in hw/9pfs/9p.c).

We cannot have consecutive '/' characters in the intermediate part or at
the end of the path.  But we do have '//' at the beginning.

This lies lies in the way the frontend internally forges paths to access
files.

A typical path is in the form:

${root}/${elem1}/${elem2}/${elem3}/ ... etc... /${elemN}

where ${root} is set to '/' when the client mounts the 9pfs share (see
v9fs_attach() in hw/9pfs/9p.c). Since all paths are supposed to be
relative to the path of shared directory on the host, I believe ${root}
should rather be '.' than '/'. Unfortunately, this contradicts this
snippet of code in the handle backend code (hw/9pfs/9p-handle.c):

static int handle_name_to_path(FsContext *ctx, V9fsPath *dir_path,
                              const char *name, V9fsPath *target)
{
[...]
    /* "." and ".." are not allowed */
    if (!strcmp(name, ".") || !strcmp(name, "..")) {
        errno = EINVAL;
        return -1;

    }

I should probably dig some more to see why it is coded that way. But since
I couldn't reproduce the vulnerability with the handle backend and this series
is already huge, I've opted to keep the hardcoded '/' in v9fs_attach().

The consequence is that I have to strip these leading '/' when calling openat().

But then the code can safely assume that paths don't have consecutive '/'.

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

  reply	other threads:[~2017-02-27 14: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 [this message]
2017-02-27 15:32       ` Stefan Hajnoczi
2017-02-27 23:28   ` Eric Blake
2017-02-28  0:32     ` Greg Kurz
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=20170227153147.7875ca9c@bahia.lan \
    --to=groug@kaod.org \
    --cc=aneesh.kumar@linux.vnet.ibm.com \
    --cc=jannh@google.com \
    --cc=prasad@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=stefanha@gmail.com \
    --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).