perf record: Fix segfault when running with suid and kptr_restrict is 1
PATCH 1/1 perf record: Fix segfault when running with suid and kptr_restrict is 1
Before this patch perf panic if kptr_restrict set to 1 and perf is owned by root with suid set:
$ whoami wangnan $ ls -l ./perf -rwsr-xr-x 1 root root 19781908 Sep 21 19:29 /home/wangnan/perf $ cat /proc/sys/kernel/kptr_restrict 1 $ cat /proc/sys/kernel/perf_event_paranoid -1 $ ./perf record -a Segmentation fault (core dumped)
The reason is perf assumes it is allowed to read kptr from /proc/kallsyms when euid is root, but in fact kernel doesn't allow it reading kptr when euid and uid are not match with each other:
$ cp /bin/cat . $ sudo chown root:root ./cat $ sudo chmod u+s ./cat $ cat /proc/kallsyms | grep do_fork 0000000000000000 T _do_fork <--- kptr is hidden even euid is root $ sudo cat /proc/kallsyms | grep do_fork ffffffff81080230 T _do_fork
See lib/vsprintf.c for kernel side code.
This patch fixes this problem by checking both uid and euid.
--- tools/perf/util/symbol.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
tools/perf/util/symbol.c +2 -1
@@ -1946,8 +1946,9 @@ static bool symbol__read_kptr_restrict(void)if (fp !=NULL ) {char line[8 ]; +if (fgets (line,sizeof (line), fp) !=NULL ) - value = (geteuid () !=0 ) ? + value = ((geteuid () !=0 ) || (getuid () !=0 )) ? (atoi (line) !=0 ) : (atoi (line) ==2 );
Signature · 1 line
1.8.3.4
Quoted 41 lines · Wang Nan
Before this patch perf panic if
kptr_restrictset to 1 and perf is owned by root with suid set:$ whoami wangnan $ ls -l ./perf -rwsr-xr-x 1 root root 19781908 Sep 21 19:29 /home/wangnan/perf $ cat /proc/sys/kernel/kptr_restrict 1 $ cat /proc/sys/kernel/perf_event_paranoid -1 $ ./perf record -a Segmentation fault (core dumped)The reason is perf assumes it is allowed to read kptr from
/proc/kallsymswhen euid is root, but in fact kernel doesn't allow it reading kptr when euid and uid are not match with each other:$ cp /bin/cat . $ sudo chown root:root ./cat $ sudo chmod u+s ./cat $ cat /proc/kallsyms | grep do_fork 0000000000000000 T _do_fork <--- kptr is hidden even euid is root $ sudo cat /proc/kallsyms | grep do_fork ffffffff81080230 T _do_forkSee
lib/vsprintf.cfor kernel side code.This patch fixes this problem by checking both uid and euid.
Signed-off-by Wang Nan <[email protected]>Cc Arnaldo Carvalho de Melo <[email protected]>--- tools/perf/util/symbol.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)tools/perf/util/symbol.c +0
-0
tools/perf/util/symbol.c static bool symbol__read_kptr_restrict(void) · line 1949
Quoted 4 lines
tools/perf/util/symbol.c +1
-0@@ -1946,8 +1946,9 @@ static bool symbol__read_kptr_restrict(void)if (fp !=NULL ) {char line[8 ]; +
Sorry for this blank line. Will fix it.
Quoted 6 lines
tools/perf/util/symbol.c +1
-1if (fgets (line,sizeof (line), fp) !=NULL ) - value = (geteuid () !=0 ) ? + value = ((geteuid () !=0 ) || (getuid () !=0 )) ? (atoi (line) !=0 ) : (atoi (line) ==2 );