[PATCH] perf: Fix off-by-one stack buffer overflow in kallsyms__parse()

Rui Qi posted 1 patch 3 days, 9 hours ago
There is a newer version of this series
tools/lib/symbol/kallsyms.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
[PATCH] perf: Fix off-by-one stack buffer overflow in kallsyms__parse()
Posted by Rui Qi 3 days, 9 hours ago
In kallsyms__parse(), the loop reading symbol names iterates with
i < sizeof(symbol_name), which allows i to reach sizeof(symbol_name)
upon loop exit. The subsequent symbol_name[i] = '\0' then writes one
byte past the end of the stack-allocated symbol_name[] array.

Fix this by changing the loop bound to sizeof(symbol_name) - 1, so
the null terminator always lands within the array. The overflow is
triggerable by a kallsyms entry with a symbol name of KSYM_NAME_LEN+1
or more characters (e.g., long Rust mangled names or a malicious
/proc/kallsyms).

Signed-off-by: Rui Qi <qirui.001@bytedance.com>
---
 tools/lib/symbol/kallsyms.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/lib/symbol/kallsyms.c b/tools/lib/symbol/kallsyms.c
index e335ac2b9e19..f7100d43ecf4 100644
--- a/tools/lib/symbol/kallsyms.c
+++ b/tools/lib/symbol/kallsyms.c
@@ -60,7 +60,7 @@ int kallsyms__parse(const char *filename, void *arg,
 			read_to_eol(&io);
 			continue;
 		}
-		for (i = 0; i < sizeof(symbol_name); i++) {
+		for (i = 0; i < sizeof(symbol_name) - 1; i++) {
 			ch = io__get_char(&io);
 			if (ch < 0 || ch == '\n')
 				break;
-- 
2.20.1