Skip to contentSkip to conversation

usb-mtp: use O_NOFOLLOW and O_CLOEXEC.

PATCH In review Gerd Hoffmann 5 participants 5 messages Dec 13, 2018

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 usb-mtp: use O_NOFOLLOW and O_CLOEXEC.

Open files and directories with O_NOFOLLOW to avoid symlinks attacks. While being at it also add O_CLOEXEC.

usb-mtp only handles regular files and directories and ignores everything else, so users should not see a difference.

Because qemu ignores symlinks carrying out an successfull symlink attack requires swapping an existing file or directory below rootdir for a symlink and winning the race against the inotify notification to qemu.

Note that the impact of this bug is rather low when qemu is managed by libvirt due to qemu running sandboxed, so there isn't much you can gain access to that way.

Fixes CVE-2018-pjp-please-get-one
Cc Prasad J Pandit <[email protected]>
Cc Bandan Das <[email protected]>
---
 hw/usb/dev-mtp.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
hw/usb/dev-mtp.c +9 -4
index 100b7171f4..36c43b8c20 100644
@@ -653,13 +653,18 @@ static void usb_mtp_object_readdir(MTPState *s, MTPObject *o)
 {
     struct dirent *entry;
     DIR *dir;
+    int fd;
 
     if (o->have_children) {
         return;
     }
     o->have_children = true;
 
-    dir = opendir(o->path);
+    fd = open(o->path, O_DIRECTORY | O_CLOEXEC | O_NOFOLLOW);
+    if (fd < 0) {
+        return;
+    }
+    dir = fdopendir(fd);
     if (!dir) {
         return;
     }
@@ -1007,7 +1012,7 @@ static MTPData *usb_mtp_get_object(MTPState *s, MTPControl *c,
 
     trace_usb_mtp_op_get_object(s->dev.addr, o->handle, o->path);
 
-    d->fd = open(o->path, O_RDONLY);
+    d->fd = open(o->path, O_RDONLY | O_CLOEXEC | O_NOFOLLOW);
     if (d->fd == -1) {
         usb_mtp_data_free(d);
         return NULL;
@@ -1031,7 +1036,7 @@ static MTPData *usb_mtp_get_partial_object(MTPState *s, MTPControl *c,
                                         c->argv[1], c->argv[2]);
 
     d = usb_mtp_data_alloc(c);
-    d->fd = open(o->path, O_RDONLY);
+    d->fd = open(o->path, O_RDONLY | O_CLOEXEC | O_NOFOLLOW);
     if (d->fd == -1) {
         usb_mtp_data_free(d);
         return NULL;
@@ -1658,7 +1663,7 @@ static void usb_mtp_write_data(MTPState *s)
                                  0, 0, 0, 0);
             goto done;
         }
-        d->fd = open(path, O_CREAT | O_WRONLY, mask);
+        d->fd = open(path, O_CREAT | O_WRONLY | O_CLOEXEC | O_NOFOLLOW, mask);
         if (d->fd == -1) {
             usb_mtp_queue_result(s, RES_STORE_FULL, d->trans,
                                  0, 0, 0, 0);
Signature · 1 line
2.9.3
#1 lore raw
Quoted 6 lines · Gerd Hoffmann

Open files and directories with O_NOFOLLOW to avoid symlinks attacks. While being at it also add O_CLOEXEC.

usb-mtp only handles regular files and directories and ignores everything else, so users should not see a difference.

Quoted 7 lines

Because qemu ignores symlinks carrying out an successfull symlink attack requires swapping an existing file or directory below rootdir for a symlink and winning the race against the inotify notification to qemu.

Note that the impact of this bug is rather low when qemu is managed by libvirt due to qemu running sandboxed, so there isn't much you can gain access to that way.

It is almost non-existant because libvirt doesn't support the MTP device at all yet, so no guest will have it unless the user tried CLI arg passthrough in libvirt :-)

Quoted 65 lines
Fixes CVE-2018-pjp-please-get-one
Cc Prasad J Pandit <[email protected]>
Cc Bandan Das <[email protected]>
---
 hw/usb/dev-mtp.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
hw/usb/dev-mtp.c +9 -4
index 100b7171f4..36c43b8c20 100644
@@ -653,13 +653,18 @@ static void usb_mtp_object_readdir(MTPState *s, MTPObject *o)
 {
     struct dirent *entry;
     DIR *dir;
+    int fd;
 
     if (o->have_children) {
         return;
     }
     o->have_children = true;
 
-    dir = opendir(o->path);
+    fd = open(o->path, O_DIRECTORY | O_CLOEXEC | O_NOFOLLOW);
+    if (fd < 0) {
+        return;
+    }
+    dir = fdopendir(fd);
     if (!dir) {
         return;
     }
@@ -1007,7 +1012,7 @@ static MTPData *usb_mtp_get_object(MTPState *s, MTPControl *c,
 
     trace_usb_mtp_op_get_object(s->dev.addr, o->handle, o->path);
 
-    d->fd = open(o->path, O_RDONLY);
+    d->fd = open(o->path, O_RDONLY | O_CLOEXEC | O_NOFOLLOW);
     if (d->fd == -1) {
         usb_mtp_data_free(d);
         return NULL;
@@ -1031,7 +1036,7 @@ static MTPData *usb_mtp_get_partial_object(MTPState *s, MTPControl *c,
                                         c->argv[1], c->argv[2]);
 
     d = usb_mtp_data_alloc(c);
-    d->fd = open(o->path, O_RDONLY);
+    d->fd = open(o->path, O_RDONLY | O_CLOEXEC | O_NOFOLLOW);
     if (d->fd == -1) {
         usb_mtp_data_free(d);
         return NULL;
@@ -1658,7 +1663,7 @@ static void usb_mtp_write_data(MTPState *s)
                                  0, 0, 0, 0);
             goto done;
         }
-        d->fd = open(path, O_CREAT | O_WRONLY, mask);
+        d->fd = open(path, O_CREAT | O_WRONLY | O_CLOEXEC | O_NOFOLLOW, mask);
         if (d->fd == -1) {
             usb_mtp_queue_result(s, RES_STORE_FULL, d->trans,
                                  0, 0, 0, 0);
Signature · 1 line
2.9.3

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
Reviewed-by on v1 1/1
#2 lore raw
In reply to Gerd Hoffmann
Quoted 7 lines · Gerd Hoffmann

Open files and directories with O_NOFOLLOW to avoid symlinks attacks. While being at it also add O_CLOEXEC.

usb-mtp only handles regular files and directories and ignores everything else, so users should not see a difference.

Because qemu ignores symlinks carrying out an successfull symlink attack

Minor typo: s/successfull/successful/

Quoted 7 lines

requires swapping an existing file or directory below rootdir for a symlink and winning the race against the inotify notification to qemu.

Note that the impact of this bug is rather low when qemu is managed by libvirt due to qemu running sandboxed, so there isn't much you can gain access to that way.

Quoted 5 lines
Fixes CVE-2018-pjp-please-get-one
Cc Prasad J Pandit <[email protected]>
Cc Bandan Das <[email protected]>

Thanks for the patch!

Best regards,
Michael

#3 lore raw
In reply to Gerd Hoffmann
Quoted 10 lines · Gerd Hoffmann

Open files and directories with O_NOFOLLOW to avoid symlinks attacks. While being at it also add O_CLOEXEC.

usb-mtp only handles regular files and directories and ignores everything else, so users should not see a difference.

Because qemu ignores symlinks carrying out an successfull symlink attack requires swapping an existing file or directory below rootdir for a symlink and winning the race against the inotify notification to qemu.

Quoted 5 lines

Note that the impact of this bug is rather low when qemu is managed by libvirt due to qemu running sandboxed, so there isn't much you can gain access to that way.

Fixes CVE-2018-pjp-please-get-one

Ah, looks like we've run out of numbers.

Quoted 4 lines
#4 lore raw

Hello Gerd,

+-- On Thu, 13 Dec 2018, Markus Armbruster wrote --+
| Gerd Hoffmann <[email protected]> writes:
| > Open files and directories with O_NOFOLLOW to avoid symlinks attacks.
| > While being at it also add O_CLOEXEC.
| >
| > usb-mtp only handles regular files and directories and ignores
| > everything else, so users should not see a difference.
| >
| > Because qemu ignores symlinks carrying out an successfull symlink attack

symlinks, carrying out a successful ...

| > requires swapping an existing file or directory below rootdir for a
| > symlink and winning the race against the inotify notification to qemu.
| >
| > Note that the impact of this bug is rather low when qemu is managed by
| > libvirt due to qemu running sandboxed, so there isn't much you can gain
| > access to that way.
| >
| > Fixes: CVE-2018-pjp-please-get-one
| 
| Ah, looks like we've run out of numbers.

Heh..:)

It's CVE-2018-16872. Thank you so much for the fix patch.

Thank you.
--
Prasad J Pandit / Red Hat Product Security Team
47AF CE69 3A90 54AA 9045 1053 DD13 3D32 FE5B 041F