[hverkuil-media:for-v5.16c 7/24] drivers/media/usb/siano/smsusb.c:354 smsusb_term_device() error: we previously assumed 'dev' could be null (see line 340)

From: Dan Carpenter
Date: Fri Sep 03 2021 - 04:11:24 EST


tree: git://linuxtv.org/hverkuil/media_tree.git for-v5.16c
head: 1ff8a1958e10f52762323ab98ad84bd417c4bd24
commit: 84f96613da19427380d40262262745e27ffc7c75 [7/24] media: smsusb: Use usb_get_dev() for the reference count of udev
config: arm64-randconfig-m031-20210831 (attached as .config)
compiler: aarch64-linux-gcc (GCC) 11.2.0

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <lkp@xxxxxxxxx>
Reported-by: Dan Carpenter <dan.carpenter@xxxxxxxxxx>

smatch warnings:
drivers/media/usb/siano/smsusb.c:354 smsusb_term_device() error: we previously assumed 'dev' could be null (see line 340)
drivers/media/usb/siano/smsusb.c:354 smsusb_term_device() error: dereferencing freed memory 'dev'

vim +/dev +354 drivers/media/usb/siano/smsusb.c

0c071f374f66f0 drivers/media/dvb/siano/smsusb.c Michael Krufky 2008-06-21 336 static void smsusb_term_device(struct usb_interface *intf)
2e5c1ec8865abd drivers/media/mdtv/smsusb.c Michael Krufky 2008-05-19 337 {
8350e1551c27ee drivers/media/dvb/siano/smsusb.c Joe Perches 2010-11-30 338 struct smsusb_device_t *dev = usb_get_intfdata(intf);
2e5c1ec8865abd drivers/media/mdtv/smsusb.c Michael Krufky 2008-05-19 339
822374165d6b11 drivers/media/dvb/siano/smsusb.c Michael Krufky 2008-06-15 @340 if (dev) {
^^^
Check for NULL here. (Unnecessary, "dev" can't be NULL at this point).

05f0ffbc487517 drivers/media/usb/siano/smsusb.c Mauro Carvalho Chehab 2013-03-06 341 dev->state = SMSUSB_DISCONNECTED;
05f0ffbc487517 drivers/media/usb/siano/smsusb.c Mauro Carvalho Chehab 2013-03-06 342
2e5c1ec8865abd drivers/media/mdtv/smsusb.c Michael Krufky 2008-05-19 343 smsusb_stop_streaming(dev);
2e5c1ec8865abd drivers/media/mdtv/smsusb.c Michael Krufky 2008-05-19 344
fa830e8a014a20 drivers/media/dvb/siano/smsusb.c Michael Krufky 2008-06-15 345 /* unregister from smscore */
2e5c1ec8865abd drivers/media/mdtv/smsusb.c Michael Krufky 2008-05-19 346 if (dev->coredev)
2e5c1ec8865abd drivers/media/mdtv/smsusb.c Michael Krufky 2008-05-19 347 smscore_unregister_device(dev->coredev);
2e5c1ec8865abd drivers/media/mdtv/smsusb.c Michael Krufky 2008-05-19 348
0dd5f20cb35b59 drivers/media/usb/siano/smsusb.c Mauro Carvalho Chehab 2015-02-22 349 pr_debug("device 0x%p destroyed\n", dev);
08921ac9e9d185 drivers/media/dvb/siano/smsusb.c Jesper Juhl 2011-04-21 350 kfree(dev);
^^^
"dev" is freed.

2e5c1ec8865abd drivers/media/mdtv/smsusb.c Michael Krufky 2008-05-19 351 }
2e5c1ec8865abd drivers/media/mdtv/smsusb.c Michael Krufky 2008-05-19 352
2e5c1ec8865abd drivers/media/mdtv/smsusb.c Michael Krufky 2008-05-19 353 usb_set_intfdata(intf, NULL);
84f96613da1942 drivers/media/usb/siano/smsusb.c Cai Huoqing 2021-08-02 @354 usb_put_dev(dev->udev);
^^^
Use after free.

2e5c1ec8865abd drivers/media/mdtv/smsusb.c Michael Krufky 2008-05-19 355 }

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all@xxxxxxxxxxxx