Linux Sound subsystem development
 help / color / mirror / Atom feed
* [PATCH] sound:Fix a memory leak in snd_ctl_elem_add_compat function
@ 2024-03-23  9:27 LuMingYin
  2024-03-23  9:31 ` Takashi Iwai
  0 siblings, 1 reply; 2+ messages in thread
From: LuMingYin @ 2024-03-23  9:27 UTC (permalink / raw)
  To: linux-sound, linux-kernel; +Cc: perex, tiwai, minhuadotchen, LuMingYin

In the function snd_ctl_elem_add_compat defined in /linux/sound/core/control_compat.c, a pointer named data is declared. This pointer allocates a block of dynamic memory using the kzalloc function at line 354. When the if statement at line 355 returns false, it indicates successful allocation of the dynamic memory area pointed to by data. However, when the if statements at lines 359 or 362 are true, the program will not execute the snd_ctl_elem_add(file, data, replace); operation at line 389 and will return directly. During this process, the dynamic memory area pointed to by data is neither freed nor used, leading to a memory leak bug. This commit fixes the aforementioned memory leak issue.

Signed-off-by: LuMingYin <lumingyindetect@163.com>
---
 sound/core/control_compat.c | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)

diff --git a/sound/core/control_compat.c b/sound/core/control_compat.c
index 934bb945e702..685f88e2835a 100644
--- a/sound/core/control_compat.c
+++ b/sound/core/control_compat.c
@@ -357,29 +357,39 @@ static int snd_ctl_elem_add_compat(struct snd_ctl_file *file,
 
 	/* id, type, access, count */ \
 	if (copy_from_user(&data->id, &data32->id, sizeof(data->id)) ||
-	    copy_from_user(&data->type, &data32->type, 3 * sizeof(u32)))
+	    copy_from_user(&data->type, &data32->type, 3 * sizeof(u32))){
+		kfree(data);
 		return -EFAULT;
-	if (get_user(data->owner, &data32->owner))
+		}
+	if (get_user(data->owner, &data32->owner)){
+		kfree(data);
 		return -EFAULT;
+	}
 	switch (data->type) {
 	case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
 	case SNDRV_CTL_ELEM_TYPE_INTEGER:
 		if (get_user(data->value.integer.min, &data32->value.integer.min) ||
 		    get_user(data->value.integer.max, &data32->value.integer.max) ||
-		    get_user(data->value.integer.step, &data32->value.integer.step))
+		    get_user(data->value.integer.step, &data32->value.integer.step)){
+			kfree(data);
 			return -EFAULT;
+		}
 		break;
 	case SNDRV_CTL_ELEM_TYPE_INTEGER64:
 		if (copy_from_user(&data->value.integer64,
 				   &data32->value.integer64,
-				   sizeof(data->value.integer64)))
+				   sizeof(data->value.integer64))){
+			kfree(data);
 			return -EFAULT;
+		}
 		break;
 	case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
 		if (copy_from_user(&data->value.enumerated,
 				   &data32->value.enumerated,
-				   sizeof(data->value.enumerated)))
+				   sizeof(data->value.enumerated))){
+			kfree(data);
 			return -EFAULT;
+		}
 		data->value.enumerated.names_ptr =
 			(uintptr_t)compat_ptr(data->value.enumerated.names_ptr);
 		break;
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-03-23  9:31 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-03-23  9:27 [PATCH] sound:Fix a memory leak in snd_ctl_elem_add_compat function LuMingYin
2024-03-23  9:31 ` Takashi Iwai

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox