[PATCH v2] HID: memory leak in dualshock4_get_calibration_data

Eslam Khafagy posted 1 patch 2 months, 3 weeks ago
There is a newer version of this series
drivers/hid/hid-playstation.c | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)
[PATCH v2] HID: memory leak in dualshock4_get_calibration_data
Posted by Eslam Khafagy 2 months, 3 weeks ago
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.

Reported-by: syzbot+4f5f81e1456a1f645bf8@syzkaller.appspotmail.com
Tested-by: syzbot+4f5f81e1456a1f645bf8@syzkaller.appspotmail.com
Closes: https://lore.kernel.org/all/691560c4.a70a0220.3124cb.0019.GAE@google.com/T/
Fixes: 947992c7fa9e0 ("HID: playstation: DS4: Fix calibration workaround for clone devices")
Cc: stable@vger.kernel.org
Signed-off-by: Eslam Khafagy <eslam.medhat1993@gmail.com>
---
v2:
* Adding tag "Cc: stable@vger.kernel.org"
v1: https://lore.kernel.org/all/20251115022323.1395726-1-eslam.medhat1993@gmail.com/
---
 drivers/hid/hid-playstation.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/drivers/hid/hid-playstation.c b/drivers/hid/hid-playstation.c
index 63f6eb9030d1..fef81b7e27c1 100644
--- a/drivers/hid/hid-playstation.c
+++ b/drivers/hid/hid-playstation.c
@@ -1992,9 +1992,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.
@@ -2041,6 +2038,10 @@ 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 */
+	if(buf)
+		kfree(buf);
+
 	/*
 	 * Sanity check gyro calibration data. This is needed to prevent crashes
 	 * during report handling of virtual, clone or broken devices not implementing
-- 
2.43.0
Re: [PATCH v2] HID: memory leak in dualshock4_get_calibration_data
Posted by Max Staudt 2 months, 3 weeks ago
On 11/16/25 11:27 AM, Eslam Khafagy wrote:
> 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.

Reviewed-by: Max Staudt <max@enpas.org>


One minor thing that I forgot: I see that the commit message is all 
lower case. Proper capitalisation helps readability and matches the 
kernel style. No need to resend this time, but in case you're sending a 
v3 for any other reason, it'd be great if you picked this up as well :)


Thanks for your patch!

Max
Re: [PATCH v2] HID: memory leak in dualshock4_get_calibration_data
Posted by Eslam Khafagy 2 months, 3 weeks ago
On 11/16/25 06:34, Max Staudt wrote:
> On 11/16/25 11:27 AM, Eslam Khafagy wrote:
>> 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.
>
> Reviewed-by: Max Staudt <max@enpas.org>
>
>
> One minor thing that I forgot: I see that the commit message is all 
> lower case. Proper capitalisation helps readability and matches the 
> kernel style. No need to resend this time, but in case you're sending 
> a v3 for any other reason, it'd be great if you picked this up as well :)
>
Oh, ok. but, no plans for v3 for now.
> Thanks for your patch!
>
welcome
> Max
>