lp registers one pardevice per port when a parallel port is announced and stores it in the static lp_table[] array. The character device is destroyed when the port goes away, but a file that is already open keeps referring to that entry. lp_detach() unregisters the pardevice, and device_unregister() frees it, while leaving lp_table[n].dev dangling. Closing the still-open file then runs lp_release(), which calls lp_claim_parport_or_block() and writes dev->waiting in parport_claim_or_block(), touching freed memory: BUG: KASAN: slab-use-after-free in parport_claim_or_block Write of size 8 at addr ... parport_claim_or_block lp_claim_parport_or_block lp_release __fput The same dangling pointer is used by lp_write(), lp_read() and several ioctl paths. Keep the pardevice registered until the open file is closed: lp_detach() now clears LP_EXIST and defers parport_unregister_device() when LP_BUSY is set, and lp_release() unregisters it after the last user is gone. This is the same lifetime model ppdev already uses. parport keeps the port and the low-level driver referenced until the deferred unregister, and parport_remove_port() switches to dead_ops, so no operation after detach reaches the removed device. lp_attach() must also skip a minor that still owns a deferred pardevice, otherwise re-plugging the adapter before the old file is closed would overwrite lp_table[n].dev and alias the state of the old and new opens. Fixes: d6318c0e8318 ("char: lp: detach the device when parallel port is removed") Cc: stable@vger.kernel.org Co-developed-by: Bin Yu Signed-off-by: Bin Yu Co-developed-by: Cheng Wen Signed-off-by: Cheng Wen Signed-off-by: YuanZhe Liu <25031212351@stu.xidian.edu.cn> --- drivers/char/lp.c | 52 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 45 insertions(+), 7 deletions(-) diff --git a/drivers/char/lp.c b/drivers/char/lp.c index 24417a00dfe9..cdf27f9d306d 100644 --- a/drivers/char/lp.c +++ b/drivers/char/lp.c @@ -569,13 +569,29 @@ static int lp_release(struct inode *inode, struct file *file) { unsigned int minor = iminor(inode); - lp_claim_parport_or_block(&lp_table[minor]); - parport_negotiate(lp_table[minor].dev->port, IEEE1284_MODE_COMPAT); - lp_table[minor].current_mode = IEEE1284_MODE_COMPAT; - lp_release_parport(&lp_table[minor]); + mutex_lock(&lp_mutex); + if (LP_F(minor) & LP_EXIST) { + mutex_unlock(&lp_mutex); + lp_claim_parport_or_block(&lp_table[minor]); + parport_negotiate(lp_table[minor].dev->port, + IEEE1284_MODE_COMPAT); + lp_table[minor].current_mode = IEEE1284_MODE_COMPAT; + lp_release_parport(&lp_table[minor]); + mutex_lock(&lp_mutex); + } + + /* + * If the port was removed while this file was open, lp_detach() + * deferred unregistering the parport device until now. + */ + if (!(LP_F(minor) & LP_EXIST) && lp_table[minor].dev) { + parport_unregister_device(lp_table[minor].dev); + lp_table[minor].dev = NULL; + } kfree(lp_table[minor].lp_buffer); lp_table[minor].lp_buffer = NULL; LP_F(minor) &= ~LP_BUSY; + mutex_unlock(&lp_mutex); return 0; } @@ -960,20 +976,26 @@ static void lp_attach(struct parport *port) { unsigned int i; + mutex_lock(&lp_mutex); switch (parport_nr[0]) { case LP_PARPORT_UNSPEC: case LP_PARPORT_AUTO: if (parport_nr[0] == LP_PARPORT_AUTO && port->probe_info[0].class != PARPORT_CLASS_PRINTER) - return; + goto out; if (lp_count == LP_NO) { printk(KERN_INFO "lp: ignoring parallel port (max. %d)\n",LP_NO); - return; + goto out; } for (i = 0; i < LP_NO; i++) - if (port_num[i] == -1) + if (port_num[i] == -1 && !lp_table[i].dev) break; + if (i == LP_NO) { + pr_info("lp: ignoring parallel port (max. %d)\n", LP_NO); + goto out; + } + if (!lp_register(i, port)) lp_count++; break; @@ -981,6 +1003,9 @@ static void lp_attach(struct parport *port) default: for (i = 0; i < LP_NO; i++) { if (port->number == parport_nr[i]) { + /* Still used by an open file from a removed port. */ + if (lp_table[i].dev) + break; if (!lp_register(i, port)) lp_count++; break; @@ -988,6 +1013,8 @@ static void lp_attach(struct parport *port) } break; } +out: + mutex_unlock(&lp_mutex); } static void lp_detach(struct parport *port) @@ -1002,14 +1029,25 @@ static void lp_detach(struct parport *port) } #endif /* CONFIG_LP_CONSOLE */ + mutex_lock(&lp_mutex); for (n = 0; n < LP_NO; n++) { if (port_num[n] == port->number) { port_num[n] = -1; lp_count--; device_destroy(&lp_class, MKDEV(LP_MAJOR, n)); + LP_F(n) &= ~LP_EXIST; + /* + * An open file may still use the parport device after + * the port has gone away; keep it registered until + * lp_release() runs. + */ + if (LP_F(n) & LP_BUSY) + continue; parport_unregister_device(lp_table[n].dev); + lp_table[n].dev = NULL; } } + mutex_unlock(&lp_mutex); } static struct parport_driver lp_driver = { -- 2.34.1