input/mouse/sermouse: fix memleak and potential buffer overflow
RFC PATCH 1/1 input/mouse/sermouse: fix memleak and potential buffer overflow
While strolling trough the sermouse driver for some example code, I noticed 2 strange things happening there :
* In the sermouse_connect function an input device structure is allocated (input_allocate_device), which is not deallocated in the sermouse_disconnect function.
If I understand this correctly someone repeatedly connecting and disconnecting the mouse would leak input_dev structures.
* In the sermouse_connect function the phys member of the sermouse structure (32 characters) is initialised with :
sprintf(sermouse->phys, "%s/input0", serio->phys);
Because serio->phys is also a 32 character field the sprintf could result in 39 characters being written to the sermouse->phys.
If my understanding of both these concepts is correct, this is a patch to fix the problems.
drivers/input/mouse/sermouse.c +2 -1
@@ -53,7 +53,7 @@unsigned char count;unsigned char type;unsigned long last; -char phys[32 ]; +char phys[39 ]; };/*
@@ -233,6 +233,7 @@serio_close (serio);serio_set_drvdata (serio,NULL );input_unregister_device (sermouse->dev); +input_free_device (sermouse->dev);kfree (sermouse); }
Quoted 10 lines · Wouter Paesen
While strolling trough the sermouse driver for some example code, I noticed 2 strange things happening there :
* In the
sermouse_connectfunction an input device structure is allocated (input_allocate_device), which is not deallocated in thesermouse_disconnectfunction.If I understand this correctly someone repeatedly connecting and disconnecting the mouse would leak
input_devstructures.
No, input_free_device() should not be called after input_register_device() returns successfully because input_dev will be freed automatically once last reference to it is dropped.
Quoted 8 lines
* In the
sermouse_connectfunction the phys member of the sermouse structure (32 characters) is initialised with :sprintf(sermouse->phys, "%s/input0", serio->phys);Because
serio->physis also a 32 character field the sprintf could result in 39 characters being written to thesermouse->phys.
Right, we need to change it to use snprintf.
Signature · 1 line
Dmitry
Quoted 4 lines · Dmitry Torokhov
Quoted 2 lines
Because
serio->physis also a 32 character field the sprintf could result in 39 characters being written to thesermouse->phys.Right, we need to change it to use snprintf.
Thanks, this patch will do just that.
Still, keeping the array 39 characters long will prevent truncation of the string.
drivers/input/mouse/sermouse.c 2006-06-20 08:31:41.000000000 +0200 renamed from drivers/input/mouse/sermouse.c 2006-06-20 08:31:12.000000000 +0200 +2 -2
@@ -53,7 +53,7 @@ struct sermouse { unsigned char count; unsigned char type; unsigned long last; - char phys[32]; + char phys[39]; }; /*
@@ -254,7 +254,7 @@ static int sermouse_connect(struct serio goto fail; sermouse->dev = input_dev; - sprintf(sermouse->phys, "%s/input0", serio->phys); + snprintf(sermouse->phys, 39, "%s/input0", serio->phys); sermouse->type = serio->id.proto; input_dev->name = sermouse_protocols[sermouse->type];
Quoted 25 lines · Wouter Paesen
Quoted 4 lines · Dmitry Torokhov
Quoted 2 lines
Because
serio->physis also a 32 character field the sprintf could result in 39 characters being written to thesermouse->phys.Right, we need to change it to use snprintf.
Thanks, this patch will do just that.
Still, keeping the array 39 characters long will prevent truncation of the string.Signed-off-by Wouter Paesen <[email protected]>---
linux-2.6.17-rc6.orig/drivers/input/mouse/sermouse.c2006-06-20 08:31:12.000000000 +0200 +++linux-2.6.17-rc6/drivers/input/mouse/sermouse.c2006-06-2008:31:41.000000000 +0200 @@ -53,7 +53,7 @@ struct sermouse { unsigned char count; unsigned char type; unsigned long last;drivers/input/mouse/sermouse.c 2006-06-20 08:31:41.000000000 +0200 renamed from drivers/input/mouse/sermouse.c 2006-06-20 08:31:12.000000000 +0200 +1
-1- char phys[32]; + char phys[39];};
/*
drivers/input/mouse/sermouse.c 2006-06-20 08:31:41.000000000 +0200 static int sermouse_connect(struct serio · line 257
Quoted 5 lines
drivers/input/mouse/sermouse.c 2006-06-20 08:31:41.000000000 +0200 renamed from drivers/input/mouse/sermouse.c 2006-06-20 08:31:12.000000000 +0200 +1
-1@@ -254,7 +254,7 @@ static int sermouse_connect(struct serio goto fail; sermouse->dev = input_dev; - sprintf(sermouse->phys, "%s/input0", serio->phys); + snprintf(sermouse->phys, 39, "%s/input0", serio->phys);
This adds a magic number here. I suggest using sizeof(sermouse->phys) instead.
Eike