Skip to contentSkip to conversation

tools: Fix handle leak in mmap_fdt function

PATCH In review [email protected] 2 participants 2 messages Apr 18, 2025 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

PATCH 1/1 tools: Fix handle leak in mmap_fdt function

From: Maks Mishin <[email protected]>

The handle 'ptr' is created at fit_common.c:91 by calling function 'mmap' and lost at fit_common.c:127:
Added call of free for ptr if ptr != MAP_FAILED.

Trigger was found by the Svace static analyzer.

---
 tools/fit_common.c | 1 +
 1 file changed, 1 insertion(+)
tools/fit_common.c +1 -0
index d1cde16c1c..135e105929 100644
@@ -123,6 +123,7 @@ err:
 		close(fd);
 	if (delete_on_error)
 		unlink(fname);
+	free(ptr);
 
 	return -1;
 }
Signature · 1 line
2.34.1
#2 lore raw
Quoted 5 lines · On Fri, Apr 18, 2025 at 11:15:23AM +0300, [email protected] wrote:

From: Maks Mishin <[email protected]>

The handle 'ptr' is created at fit_common.c:91 by calling function 'mmap' and lost at fit_common.c:127:
Added call of free for ptr if ptr != MAP_FAILED.

First, why do you free() a mmapped memory chunk? This really sounds unreasonable to me.

Second, you don't validate ptr before freeing it actually. It's possible to branch to label "err" before ptr is initialized, thus you must guard the free in case of garbage data in ptr (or just initialize ptr to NULL).

Quoted 21 lines

Trigger was found by the Svace static analyzer.

---
 tools/fit_common.c | 1 +
 1 file changed, 1 insertion(+)
tools/fit_common.c +1 -0
index d1cde16c1c..135e105929 100644
@@ -123,6 +123,7 @@ err:
 		close(fd);
 	if (delete_on_error)
 		unlink(fname);
+	free(ptr);
 
 	return -1;
 }
Signature · 1 line
2.34.1

Thanks,
Yao Zi