Skip to content

Commit 39de521

Browse files
jjuhldtor
authored andcommitted
Input: serio HIL MLC - don't deref null, don't leak and return proper error
While reviewing various users of kernel memory allocation functions I came across drivers/input/serio/hil_mlc.c::hil_mlc_register() and noticed that: - it calls kzalloc() but fails to check for a NULL return before use. - it makes several allocations and if one fails it doesn't free the previous ones. - It doesn't return -ENOMEM in the failed memory allocation case (it just crashes). This patch corrects all of the above and also reworks the only caller of this function that I could find (drivers/input/serio/hp_sdc_mlc.c::hp_sdc_mlc_out()) so that it now checks the return value of hil_mlc_register() and properly propagates it on failure and I also restructured the code to remove some labels and goto's to make it, IMHO nicer to read. Signed-off-by: Jesper Juhl <jj@chaosbits.net> Tested-by: Helge Deller <deller@gmx.de> Acked-by: Helge Deller <deller@gmx.de> Signed-off-by: Dmitry Torokhov <dtor@mail.ru>
1 parent bc95df7 commit 39de521

2 files changed

Lines changed: 14 additions & 9 deletions

File tree

‎drivers/input/serio/hil_mlc.c‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -932,6 +932,11 @@ int hil_mlc_register(hil_mlc *mlc)
932932
hil_mlc_copy_di_scratch(mlc, i);
933933
mlc_serio = kzalloc(sizeof(*mlc_serio), GFP_KERNEL);
934934
mlc->serio[i] = mlc_serio;
935+
if (!mlc->serio[i]) {
936+
for (; i >= 0; i--)
937+
kfree(mlc->serio[i]);
938+
return -ENOMEM;
939+
}
935940
snprintf(mlc_serio->name, sizeof(mlc_serio->name)-1, "HIL_SERIO%d", i);
936941
snprintf(mlc_serio->phys, sizeof(mlc_serio->phys)-1, "HIL%d", i);
937942
mlc_serio->id = hil_mlc_serio_id;

‎drivers/input/serio/hp_sdc_mlc.c‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ static void hp_sdc_mlc_out(hil_mlc *mlc)
305305
static int __init hp_sdc_mlc_init(void)
306306
{
307307
hil_mlc *mlc = &hp_sdc_mlc;
308+
int err;
308309

309310
#ifdef __mc68000__
310311
if (!MACH_IS_HP300)
@@ -323,22 +324,21 @@ static int __init hp_sdc_mlc_init(void)
323324
mlc->out = &hp_sdc_mlc_out;
324325
mlc->priv = &hp_sdc_mlc_priv;
325326

326-
if (hil_mlc_register(mlc)) {
327+
err = hil_mlc_register(mlc);
328+
if (err) {
327329
printk(KERN_WARNING PREFIX "Failed to register MLC structure with hil_mlc\n");
328-
goto err0;
330+
return err;
329331
}
330332

331333
if (hp_sdc_request_hil_irq(&hp_sdc_mlc_isr)) {
332334
printk(KERN_WARNING PREFIX "Request for raw HIL ISR hook denied\n");
333-
goto err1;
335+
if (hil_mlc_unregister(mlc))
336+
printk(KERN_ERR PREFIX "Failed to unregister MLC structure with hil_mlc.\n"
337+
"This is bad. Could cause an oops.\n");
338+
return -EBUSY;
334339
}
340+
335341
return 0;
336-
err1:
337-
if (hil_mlc_unregister(mlc))
338-
printk(KERN_ERR PREFIX "Failed to unregister MLC structure with hil_mlc.\n"
339-
"This is bad. Could cause an oops.\n");
340-
err0:
341-
return -EBUSY;
342342
}
343343

344344
static void __exit hp_sdc_mlc_exit(void)

0 commit comments

Comments
 (0)