Skip to contentSkip to conversation

perf unwind: handle allocation failure in libdw__get_entries()

PATCH Superseded Triet Hoang 5 participants 7 messages Aug 18 – Sep 8

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

PATCH 1/1 perf unwind: handle allocation failure in libdw__get_entries()

Check the return value of zalloc() before dereferencing the allocated dwfl_ui_ti structure.

Return -ENOMEM when the allocation fails to avoid a NULL pointer dereference.

---
 tools/perf/util/unwind-libdw.c | 3 +++
 1 file changed, 3 insertions(+)
tools/perf/util/unwind-libdw.c +3 -0
index 7f35042be567..ff36552ca175 100644
@@ -379,6 +379,9 @@ int libdw__get_entries(unwind_entry_cb_t cb, void *arg,
 		dwfl = dwfl_ui_ti->dwfl;
 	} else {
 		dwfl_ui_ti = zalloc(sizeof(*dwfl_ui_ti));
+		if (!dwfl_ui_ti)
+			return -ENOMEM;
+
 		dwfl = dwfl_begin(&offline_callbacks);
 		if (!dwfl)
 			goto out;
Signature · 1 line
2.53.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Memory leak of `ui` structure upon `dwfl_ui_ti` allocation failure. --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Memory leak of ui structure upon dwfl_ui_ti allocation failure. --

--- Patch [1]: [PATCH] perf unwind: handle allocation failure in libdw__get_entries() --- commit cfa13e49d271d0f1a99af3ba9b78fd579579b1d0
Author: Triet Hoang <[email protected]>

perf unwind: handle allocation failure in libdw__get_entries()

This commit adds a return value check for zalloc() when allocating the dwfl_ui_ti structure. It returns -ENOMEM upon failure to avoid a NULL pointer dereference.

Quoted 4 lines
tools/perf/util/unwind-libdw.c +0 -0
index 7f35042be5677..ff36552ca1759 100644

[ ... ]

tools/perf/util/unwind-libdw.c int libdw__get_entries(unwind_entry_cb_t cb, void *arg, · line 383

Quoted 6 lines
tools/perf/util/unwind-libdw.c +2 -0
@@ -379,6 +379,9 @@ int libdw__get_entries(unwind_entry_cb_t cb, void *arg,
 		dwfl = dwfl_ui_ti->dwfl;
 	} else {
 		dwfl_ui_ti = zalloc(sizeof(*dwfl_ui_ti));
+		if (!dwfl_ui_ti)
+			return -ENOMEM;

[Severity: Medium]
Does this code leak the previously allocated ui structure?

Earlier in libdw__get_entries(), ui is allocated with:

    ui = zalloc(sizeof(*ui) + sizeof(ui->entries[0]) * max_stack);

Returning directly here bypasses the cleanup path at the out label, which normally calls free(ui). Should we free the ui structure before returning -ENOMEM?

Quoted 4 lines
tools/perf/util/unwind-libdw.c +1 -0
+
 		dwfl = dwfl_begin(&offline_callbacks);
 		if (!dwfl)
 			goto out;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw

PATCH v2 1/1 perf unwind: handle allocation failure in libdw__get_entries()

Check the return value of zalloc() before dereferencing the allocated dwfl_ui_ti structure.

Return -ENOMEM when the allocation fails to avoid a NULL pointer dereference.

---
Changes in v2:
- Free the ui structure before returning -ENOMEM to avoid memory leak.

---
 tools/perf/util/unwind-libdw.c | 5 +++++
 1 file changed, 5 insertions(+)
tools/perf/util/unwind-libdw.c +5 -0
index 7f35042be567..fd8a5125468c 100644
@@ -379,6 +379,11 @@ int libdw__get_entries(unwind_entry_cb_t cb, void *arg,
 		dwfl = dwfl_ui_ti->dwfl;
 	} else {
 		dwfl_ui_ti = zalloc(sizeof(*dwfl_ui_ti));
+		if (!dwfl_ui_ti) {
+			free(ui);
+			return -ENOMEM;
+		}
+
 		dwfl = dwfl_begin(&offline_callbacks);
 		if (!dwfl)
 			goto out;
Signature · 1 line
2.53.0
#3 lore raw
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 7 lines

Check the return value of zalloc() before dereferencing the allocated dwfl_ui_ti structure.

Return -ENOMEM when the allocation fails to avoid a NULL pointer dereference.

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#4 lore raw
In reply to Triet Hoang
Quoted 5 lines

Check the return value of zalloc() before dereferencing the allocated dwfl_ui_ti structure.

Return -ENOMEM when the allocation fails to avoid a NULL pointer dereference.

How do you think about to add any tags (like “Fixes” and “Cc”) accordingly?
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v7.2#n145
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/stable-kernel-rules.rst?h=v7.2#n34

https://elixir.bootlin.com/linux/v7.2-rc7/source/tools/perf/util/unwind-libdw.c#L342-L447

Regards,
Markus

maintainer Acked-by on v2 1/1
#5 lore raw
In reply to Triet Hoang
Quoted 7 lines · Triet Hoang

Check the return value of zalloc() before dereferencing the allocated dwfl_ui_ti structure.

Return -ENOMEM when the allocation fails to avoid a NULL pointer dereference.

Thanks,
Namhyung

Quoted 26 lines

---
Changes in v2:
- Free the ui structure before returning -ENOMEM to avoid memory leak.

---
 tools/perf/util/unwind-libdw.c | 5 +++++
 1 file changed, 5 insertions(+)
tools/perf/util/unwind-libdw.c +5 -0
index 7f35042be567..fd8a5125468c 100644
@@ -379,6 +379,11 @@ int libdw__get_entries(unwind_entry_cb_t cb, void *arg,
 		dwfl = dwfl_ui_ti->dwfl;
 	} else {
 		dwfl_ui_ti = zalloc(sizeof(*dwfl_ui_ti));
+		if (!dwfl_ui_ti) {
+			free(ui);
+			return -ENOMEM;
+		}
+
 		dwfl = dwfl_begin(&offline_callbacks);
 		if (!dwfl)
 			goto out;
Signature · 1 line
2.53.0