-
Notifications
You must be signed in to change notification settings - Fork 540
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix potential memory leak in vinput #279
Conversation
In the export_store function, the error handling paths followed a successful vinput_alloc_vdevice call are missing a corresponding input_free_device call. Since vinput_alloc_vdevice internally calls input_allocate_device, and input_register_device has not been called yet, input_free_device should be used to properly free the allocated input_device struct in this scenario[1]. [1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/input/input.c#n2094
Can you make use of eBPF to locate memory errors? |
I never used eBPF before, I'll try it at night. |
kmemleak is really powerful, and you should try it. |
Great!
Update: I misunderstood how to use kmodleak. It only takes the module name as the tracing target and doesn't actively load or unload modules. |
Here's the outputs from kmodleak. (original code)
(patched code)
|
@@ -283,10 +283,12 @@ static ssize_t export_store(struct class *class, struct class_attribute *attr, | |||
return len; | |||
|
|||
fail_register_vinput: | |||
input_free_device(vinput->input); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we call input_allocate_device
in vinput_alloc_vdevice
, why do we handle it manually instead of putting input_free_device
in the corresponding release function?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe input_free_device should be decoupled from the release function vinput_destroy_vdevice. In both unexport_store and vinput_unregister, the vinput_unregister_vdevice function is called before device_unregister, with each invoking input_unregister_device followed by vinput_destroy_vdevice. Embedding input_free_device within vinput_destroy_vdevice could contradict the documented comments in the source code [1].
[1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/input/input.c#n2386
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, thanks for letting me know this.
We can even automate the use of kmodleak, enforcing the checks for all loadable Linux modules in CI pipeline. |
In the export_store function, the error handling paths followed a successful vinput_alloc_vdevice call are missing a corresponding input_free_device call. Since vinput_alloc_vdevice internally calls input_allocate_device, and input_register_device has not been called yet, input_free_device should be used to properly free the allocated input_device struct in this scenario[1].
[1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/input/input.c#n2094