HID: memory leak in dualshock4_get_calibration_data
Subsystems (3)
PATCH v3 1/1 HID: memory leak in dualshock4_get_calibration_data
Function dualshock4_get_calibration_data allocates memory to pointer buf .However, the function may exit prematurely due to transfer_failure in this case it does not handle freeing memory.
This patch handles memory deallocation at exit.
---
v3:
* Address issues reported by checkpatch and re-format commit message for better readability
* kfree() is safe so no need to check for NULL pointer
v2: https://lore.kernel.org/all/[email protected]/
* Adding tag "Cc: [email protected]"
v1: https://lore.kernel.org/all/[email protected]/
drivers/hid/hid-playstation.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/hid/hid-playstation.c +3 -3
@@ -1994,9 +1994,6 @@ static int dualshock4_get_calibration_data(struct dualshock4 *ds4) acc_z_plus =get_unaligned_le16 (&buf[31 ]); acc_z_minus =get_unaligned_le16 (&buf[33 ]); -/* Done parsing the buffer, so let's free it. */ -kfree (buf); -/* * Set gyroscope calibration and normalization parameters. * Data values will be normalized to 1/DS4_GYRO_RES_PER_DEG_S degree/s.
@@ -2043,6 +2040,9 @@ static int dualshock4_get_calibration_data(struct dualshock4 *ds4) ds4->accel_calib_data[2 ].sens_denom = range_2g; transfer_failed: +/* First free buf if still allocated */ +kfree (buf); +/* * Sanity check gyro calibration data. This is needed to prevent crashes * during report handling of virtual, clone or broken devices not implementing
Signature · 1 line
2.43.0
Quoted 5 lines · Eslam Khafagy
Function
dualshock4_get_calibration_dataallocates memory to pointer buf .However, the function may exit prematurely due totransfer_failurein this case it does not handle freeing memory.This patch handles memory deallocation at exit.
Thanks, Eslam!
Quoted 2 lines
Function
dualshock4_get_calibration_dataallocates memory to pointer buf .However, the function may exit prematurely due totransfer_failure
transfer failure.?
Quoted 3 lines
in this case it does not handle freeing memory.
This patch handles memory deallocation at exit.
Would a corresponding imperative wording become helpful for an improved change description?
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v6.18-rc6#n94
Would a summary phrase like “Prevent memory leak in dualshock4_get_calibration_data()” be nicer?
Regards,
Markus
Quoted 5 lines · Eslam Khafagy
Function
dualshock4_get_calibration_dataallocates memory to pointer buf .However, the function may exit prematurely due totransfer_failurein this case it does not handle freeing memory.This patch handles memory deallocation at exit.
Isn't this fixed already by:
commit 8513c154f8ad7097653dd9bf43d6155e5aad4ab3
Author: Abdun Nihaal <[email protected]> Date: Mon Nov 10 22:45:50 2025 +0530
HID: playstation: Fix memory leak in dualshock4_get_calibration_data()
?
Anyway, this is a typical use-case for __free(). Why not to use that?
Quoted 45 lines
Reported-by [email protected]Tested-by [email protected]Fixes 947992c7fa9e0 ("HID: playstation: DS4: Fix calibration workaround for clone devices")Signed-off-by Eslam Khafagy <[email protected]>---
v3:
* Address issues reported by checkpatch and re-format commit message for better readability
*kfree()is safe so no need to check for NULL pointerv2: https://lore.kernel.org/all/[email protected]/
* Adding tag "Cc: [email protected]"v1: https://lore.kernel.org/all/[email protected]/
drivers/hid/hid-playstation.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)drivers/hid/hid-playstation.c +3
-3@@ -1994,9 +1994,6 @@ static int dualshock4_get_calibration_data(struct dualshock4 *ds4) acc_z_plus =get_unaligned_le16 (&buf[31 ]); acc_z_minus =get_unaligned_le16 (&buf[33 ]); -/* Done parsing the buffer, so let's free it. */ -kfree (buf); -/* * Set gyroscope calibration and normalization parameters. * Data values will be normalized to 1/DS4_GYRO_RES_PER_DEG_S degree/s. @@ -2043,6 +2040,9 @@ static int dualshock4_get_calibration_data(struct dualshock4 *ds4) ds4->accel_calib_data[2 ].sens_denom = range_2g; transfer_failed: +/* First free buf if still allocated */ +kfree (buf); +/* * Sanity check gyro calibration data. This is needed to prevent crashes * during report handling of virtual, clone or broken devices not implementing
Signature · 2 lines
js suse labs
Quoted 7 lines · Jiri Slaby
Isn't this fixed already by:
commit8513c154f8ad7097653dd9bf43d6155e5aad4ab3
Author: Abdun Nihaal <[email protected]>
Date: Mon Nov 10 22:45:50 2025 +0530HID: playstation: Fix memory leak in
dualshock4_get_calibration_data()
?
As far as I can see, that patch does indeed fix the same issue, and it is already upstream.
Thanks for the hint - Abdun's patch has been upstreamed quite recently, hence I guess Eslam missed it by accident. But maybe I'm wrong and Eslam can chime in himself?
Quoted 1 line
Anyway, this is a typical use-case for
__free(). Why not to use that?
Wow, there's been a lot of interesting stuff happening around cleanup.h. I've been out of the kernel for too long, this looks like fun. Thanks for pointing it out :)
Max
Quoted 6 lines · Max Staudt
Quoted 5 lines · Jiri Slaby
Isn't this fixed already by:
commit8513c154f8ad7097653dd9bf43d6155e5aad4ab3
Author: Abdun Nihaal <[email protected]>
Date: Mon Nov 10 22:45:50 2025 +0530
Quoted 6 lines
Quoted 2 lines
HID: playstation: Fix memory leak in
dualshock4_get_calibration_data()?As far as I can see, that patch does indeed fix the same issue, and it is already upstream.
Thanks for the hint - Abdun's patch has been upstreamed quite recently, hence I guess Eslam missed it by accident. But maybe I'm wrong and Eslam can chime in himself?
Thank's Max & Jiri,
sorry i was sick the past couple of days i missed your replies.
yes. that patch fixes it. I guess i missed it because it wasn't merged yet when i submitted v1. So please ignore this patch.
Quoted 8 lines
Quoted 3 lines
Anyway, this is a typical use-case for
__free(). Why not to use that?Wow, there's been a lot of interesting stuff happening around
cleanup.h. I've been out of the kernel for too long, this looks like fun. Thanks for pointing it out :)Max
Lastly, One question to max,
at the beginning of the function dualshock4_get_calibration_data
buf = kzalloc(DS4_FEATURE_REPORT_CALIBRATION_SIZE, GFP_KERNEL); if (!buf) { ret = -ENOMEM; goto transfer_failed; } if the allocation fails. can't we just return here . or do we need to go the the end of the function and do sanity checks at the end?
Quoted 4 lines · Eslam Khafagy
Lastly, One question to max,
at the beginning of the functiondualshock4_get_calibration_data
buf =kzalloc(DS4_FEATURE_REPORT_CALIBRATION_SIZE, GFP_KERNEL); if (! buf) { ret =-ENOMEM; gototransfer_failed; } if the allocation fails. can't we just return here
Never.
Quoted 2 lines
or do we need to go the the end of the function and do sanity checks at the end?
Correct.
Without the sanitisation code, the driver will divide by zero.
Max