Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

ppdev: Add an error check in register_device

In register_device, the return value of ida_simple_get is unchecked,
in witch ida_simple_get will use an invalid index value.

To address this issue, index should be checked after ida_simple_get. When
the index value is abnormal, a warning message should be printed, the port
should be dropped, and the value should be recorded.

Fixes: 9a69645dde11 ("ppdev: fix registering same device name")
Signed-off-by: Huai-Yuan Liu <qq810974084@gmail.com>
Link: https://lore.kernel.org/r/20240412083840.234085-1-qq810974084@gmail.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Huai-Yuan Liu and committed by
Greg Kroah-Hartman
fbf740ae f866b653

+11 -4
+11 -4
drivers/char/ppdev.c
··· 296 296 if (!port) { 297 297 pr_warn("%s: no associated port!\n", name); 298 298 rc = -ENXIO; 299 - goto err; 299 + goto err_free_name; 300 300 } 301 301 302 302 index = ida_alloc(&ida_index, GFP_KERNEL); 303 + if (index < 0) { 304 + pr_warn("%s: failed to get index!\n", name); 305 + rc = index; 306 + goto err_put_port; 307 + } 308 + 303 309 memset(&ppdev_cb, 0, sizeof(ppdev_cb)); 304 310 ppdev_cb.irq_func = pp_irq; 305 311 ppdev_cb.flags = (pp->flags & PP_EXCL) ? PARPORT_FLAG_EXCL : 0; 306 312 ppdev_cb.private = pp; 307 313 pdev = parport_register_dev_model(port, name, &ppdev_cb, index); 308 - parport_put_port(port); 309 314 310 315 if (!pdev) { 311 316 pr_warn("%s: failed to register device!\n", name); 312 317 rc = -ENXIO; 313 318 ida_free(&ida_index, index); 314 - goto err; 319 + goto err_put_port; 315 320 } 316 321 317 322 pp->pdev = pdev; 318 323 pp->index = index; 319 324 dev_dbg(&pdev->dev, "registered pardevice\n"); 320 - err: 325 + err_put_port: 326 + parport_put_port(port); 327 + err_free_name: 321 328 kfree(name); 322 329 return rc; 323 330 }