Skip to contentSkip to conversation

usb: ffs-test: add NULL check for malloc in descs_to_legacy

PATCH In review longlong yan 2 participants 2 messages Aug 17 – Nov 20 USB

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 usb: ffs-test: add NULL check for malloc in descs_to_legacy

In descs_to_legacy(), the return value of malloc() is used without a NULL check. If malloc() fails, dereferencing the resulting pointer leads to a NULL pointer dereference. Add a NULL check and return 0 on failure, consistent with the existing error handling at the start of the function.

---
 tools/usb/ffs-test.c | 2 ++
 1 file changed, 2 insertions(+)
tools/usb/ffs-test.c +2 -0
index 22b938fbdfb7..5c9281cbbff1 100644
@@ -293,6 +293,8 @@ static size_t descs_to_legacy(void **legacy, const void *descriptors_v2)
 
 		length = sizeof out->header + (descs_end - descs_start);
 		out = malloc(length);
+		if (!out)
+			return 0;
 		out->header.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC);
 		out->header.length = cpu_to_le32(length);
 		out->header.fs_count = cpu_to_le32(fs_count);
Signature · 1 line
2.43.0
maintainer
#1 lore raw
Quoted 28 lines · longlong yan

In descs_to_legacy(), the return value of malloc() is used without a NULL check. If malloc() fails, dereferencing the resulting pointer leads to a NULL pointer dereference. Add a NULL check and return 0 on failure, consistent with the existing error handling at the start of the function.

---
 tools/usb/ffs-test.c | 2 ++
 1 file changed, 2 insertions(+)
tools/usb/ffs-test.c +2 -0
index 22b938fbdfb7..5c9281cbbff1 100644
@@ -293,6 +293,8 @@ static size_t descs_to_legacy(void **legacy, const void *descriptors_v2)
 
 		length = sizeof out->header + (descs_end - descs_start);
 		out = malloc(length);
+		if (!out)
+			return 0;
 		out->header.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC);
 		out->header.length = cpu_to_le32(length);
 		out->header.fs_count = cpu_to_le32(fs_count);
Signature · 1 line
2.43.0

How did you trigger and test this? It's userspace code, how can malloc fail here?

thanks,

greg k-h