Skip to contentSkip to conversation

HID: memory leak in dualshock4_get_calibration_data

PATCH In review Eslam Khafagy 4 participants 7 messages Nov 22, 2025 – Nov 27, 2025

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

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.

Reported-by [email protected]
Fixes 947992c7fa9e0 ("HID: playstation: DS4: Fix calibration workaround for clone devices")

---
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
index 128aa6abd10b..05a8522ace4f 100644
@@ -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
Reviewed-by on v3 1/1
#1 lore raw
Quoted 5 lines · Eslam Khafagy

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.

Thanks, Eslam!

#2 lore raw
In reply to Eslam Khafagy
Quoted 2 lines

Function dualshock4_get_calibration_data allocates memory to pointer buf .However, the function may exit prematurely due to transfer_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

#3 lore raw
In reply to Eslam Khafagy
Quoted 5 lines · Eslam Khafagy

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.

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]
Fixes 947992c7fa9e0 ("HID: playstation: DS4: Fix calibration workaround for clone devices")

---
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
index 128aa6abd10b..05a8522ace4f 100644
@@ -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
#4 lore raw
Quoted 7 lines · Jiri Slaby

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()
?

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

#5 lore raw
Quoted 6 lines · Max Staudt
Quoted 5 lines · Jiri Slaby

Isn't this fixed already by:
commit 8513c154f8ad7097653dd9bf43d6155e5aad4ab3
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?

#6 lore raw
Quoted 4 lines · Eslam Khafagy

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

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