Message ID | 20190430130736.9191-1-oneukum@suse.com (mailing list archive) |
---|---|
State | New, archived |
Headers | show |
Series | dvb: usb: fix use after free in dvb_usb_device_exit | expand |
On Tue, 2019-04-30 at 15:07 +0200, Oliver Neukum wrote: > dvb_usb_device_exit() frees and uses the device name in that order > Fix by storing the name in a buffer before freeing it > > v2: fixed style issues > v3: strscpy used and variable names changed > v4: really use strscpy everywhere > > Signed-off-by: Oliver Neukum <oneukum@suse.com> > Reported-by: syzbot+26ec41e9f788b3eba396@syzkaller.appspotmail.com This doesn't fix that bug (and I don't think it fixes a bug at all). The name string is static and doesn't get freed until the module it's in is freed. Look again at the stack traces in <https://syzkaller.appspot.com/bug?extid=26ec41e9f788b3eba396>: > Allocated by task 21: [...] > kmemdup+0x23/0x50 mm/util.c:118 > kmemdup include/linux/string.h:428 [inline] > dw2102_probe+0x62c/0xc50 drivers/media/usb/dvb-usb/dw2102.c:2375 [...] > Freed by task 21: [...] > kfree+0xce/0x290 mm/slub.c:3958 > dw2102_probe+0x876/0xc50 drivers/media/usb/dvb-usb/dw2102.c:2409 So, d->desc was freed during probe, and is a dangling pointer before dvb_usb_device_exit() runs at all. The bug seems to have been introduced by: commit 299c7007e93645067e1d2743f4e50156de78c4ff Author: Anton Vasilyev <vasilyev@ispras.ru> Date: Mon Jul 23 13:04:54 2018 -0400 media: dw2102: Fix memleak on sequence of probes Ben. > --- > drivers/media/usb/dvb-usb/dvb-usb-init.c | 7 +++++-- > 1 file changed, 5 insertions(+), 2 deletions(-) > > diff --git a/drivers/media/usb/dvb-usb/dvb-usb-init.c b/drivers/media/usb/dvb-usb/dvb-usb-init.c > index 99951e02a880..dd063a736df5 100644 > --- a/drivers/media/usb/dvb-usb/dvb-usb-init.c > +++ b/drivers/media/usb/dvb-usb/dvb-usb-init.c > @@ -287,12 +287,15 @@ EXPORT_SYMBOL(dvb_usb_device_init); > void dvb_usb_device_exit(struct usb_interface *intf) > { > struct dvb_usb_device *d = usb_get_intfdata(intf); > - const char *name = "generic DVB-USB module"; > + const char *default_name = "generic DVB-USB module"; > + char name[40]; > > usb_set_intfdata(intf, NULL); > if (d != NULL && d->desc != NULL) { > - name = d->desc->name; > + strscpy(name, d->desc->name, sizeof(name)); > dvb_usb_exit(d); > + } else { > + strscpy(name, default_name, sizeof(name)); > } > info("%s successfully deinitialized and disconnected.", name); >
Am Dienstag, den 20.08.2019, 19:55 +0100 schrieb Ben Hutchings: > On Tue, 2019-04-30 at 15:07 +0200, Oliver Neukum wrote: > > dvb_usb_device_exit() frees and uses the device name in that order > > Fix by storing the name in a buffer before freeing it > > > > v2: fixed style issues > > v3: strscpy used and variable names changed > > v4: really use strscpy everywhere > > > > Signed-off-by: Oliver Neukum <oneukum@suse.com> > > Reported-by: syzbot+26ec41e9f788b3eba396@syzkaller.appspotmail.com > > This doesn't fix that bug (and I don't think it fixes a bug at all). > The name string is static and doesn't get freed until the module it's > in is freed. I see. > Look again at the stack traces in > <https://syzkaller.appspot.com/bug?extid=26ec41e9f788b3eba396>: > > > Allocated by task 21: > > [...] > > kmemdup+0x23/0x50 mm/util.c:118 > > > > kmemdup include/linux/string.h:428 [inline] > > dw2102_probe+0x62c/0xc50 > > drivers/media/usb/dvb-usb/dw2102.c:2375 > [...] > > Freed by task 21: > > [...] > > > > kfree+0xce/0x290 mm/slub.c:3958 > > dw2102_probe+0x876/0xc50 > > drivers/media/usb/dvb-usb/dw2102.c:2409 > > So, d->desc was freed during probe, and is a dangling pointer before > dvb_usb_device_exit() runs at all. In that case KASAN would have reported a double free in testing, which it did not. > The bug seems to have been introduced by: > > commit 299c7007e93645067e1d2743f4e50156de78c4ff > Author: Anton Vasilyev <vasilyev@ispras.ru> > Date: Mon Jul 23 13:04:54 2018 -0400 > > media: dw2102: Fix memleak on sequence of probes AFAICT this patch only does anything if probe() succeeds, which it does not. Something is strange. Regards Oliver
On Wed, 2019-08-21 at 10:18 +0200, Oliver Neukum wrote: > Am Dienstag, den 20.08.2019, 19:55 +0100 schrieb Ben Hutchings: > > On Tue, 2019-04-30 at 15:07 +0200, Oliver Neukum wrote: > > > dvb_usb_device_exit() frees and uses the device name in that order > > > Fix by storing the name in a buffer before freeing it > > > > > > v2: fixed style issues > > > v3: strscpy used and variable names changed > > > v4: really use strscpy everywhere > > > > > > Signed-off-by: Oliver Neukum <oneukum@suse.com> > > > Reported-by: syzbot+26ec41e9f788b3eba396@syzkaller.appspotmail.com > > > > This doesn't fix that bug (and I don't think it fixes a bug at all). > > The name string is static and doesn't get freed until the module it's > > in is freed. > > I see. > > > Look again at the stack traces in > > <https://syzkaller.appspot.com/bug?extid=26ec41e9f788b3eba396>;: > > > > > Allocated by task 21: > > > > [...] > > > kmemdup+0x23/0x50 mm/util.c:118 > > > > > > > kmemdup include/linux/string.h:428 [inline] > > > dw2102_probe+0x62c/0xc50 > > > > drivers/media/usb/dvb-usb/dw2102.c:2375 > > [...] > > > Freed by task 21: > > > > [...] > > > > kfree+0xce/0x290 mm/slub.c:3958 > > > dw2102_probe+0x876/0xc50 > > > > drivers/media/usb/dvb-usb/dw2102.c:2409 > > > > So, d->desc was freed during probe, and is a dangling pointer before > > dvb_usb_device_exit() runs at all. > > In that case KASAN would have reported a double free in testing, which > it did not. So far as I could see, the descriptors are normally static data in the driver and nothing in the DVB core frees them. dw2102 is unusual in that it heap-allocated descriptors, which is why this patched fixes a leak: > > The bug seems to have been introduced by: > > > > commit 299c7007e93645067e1d2743f4e50156de78c4ff > > Author: Anton Vasilyev <vasilyev@ispras.ru> > > Date: Mon Jul 23 13:04:54 2018 -0400 > > > > media: dw2102: Fix memleak on sequence of probes > > AFAICT this patch only does anything if probe() succeeds, which it does > not. Something is strange. How did you determine that it doesn't succeed? Ben.
diff --git a/drivers/media/usb/dvb-usb/dvb-usb-init.c b/drivers/media/usb/dvb-usb/dvb-usb-init.c index 99951e02a880..dd063a736df5 100644 --- a/drivers/media/usb/dvb-usb/dvb-usb-init.c +++ b/drivers/media/usb/dvb-usb/dvb-usb-init.c @@ -287,12 +287,15 @@ EXPORT_SYMBOL(dvb_usb_device_init); void dvb_usb_device_exit(struct usb_interface *intf) { struct dvb_usb_device *d = usb_get_intfdata(intf); - const char *name = "generic DVB-USB module"; + const char *default_name = "generic DVB-USB module"; + char name[40]; usb_set_intfdata(intf, NULL); if (d != NULL && d->desc != NULL) { - name = d->desc->name; + strscpy(name, d->desc->name, sizeof(name)); dvb_usb_exit(d); + } else { + strscpy(name, default_name, sizeof(name)); } info("%s successfully deinitialized and disconnected.", name);
dvb_usb_device_exit() frees and uses the device name in that order Fix by storing the name in a buffer before freeing it v2: fixed style issues v3: strscpy used and variable names changed v4: really use strscpy everywhere Signed-off-by: Oliver Neukum <oneukum@suse.com> Reported-by: syzbot+26ec41e9f788b3eba396@syzkaller.appspotmail.com --- drivers/media/usb/dvb-usb/dvb-usb-init.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)