Skip to contentSkip to conversation

perf tools: Check kptr_restrict for root

PATCH Applied Wang Nan 3 participants 7 messages May 24, 2016 – May 29, 2016 perf events

lore
Share & tools
lore

Wang Nan

author
#0 lore raw

PATCH 1/2 perf tools: Check kptr_restrict for root

If kptr_restrict is set to 2, even root is not allowed to see pointers. This patch checks kptr_restrict even if euid == 0. For root, report error if kptr_restrict is 2.

Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Zefan Li <[email protected]>
---
 tools/perf/util/symbol.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
tools/perf/util/symbol.c +8 -8
index 20f9cb3..54c4ff2 100644
@@ -1933,17 +1933,17 @@ int setup_intlist(struct intlist **list, const char *list_str,
 static bool symbol__read_kptr_restrict(void)
 {
 	bool value = false;
+	FILE *fp = fopen("/proc/sys/kernel/kptr_restrict", "r");
 
-	if (geteuid() != 0) {
-		FILE *fp = fopen("/proc/sys/kernel/kptr_restrict", "r");
-		if (fp != NULL) {
-			char line[8];
+	if (fp != NULL) {
+		char line[8];
 
-			if (fgets(line, sizeof(line), fp) != NULL)
-				value = atoi(line) != 0;
+		if (fgets(line, sizeof(line), fp) != NULL)
+			value = (geteuid() != 0) ?
+					(atoi(line) != 0) :
+					(atoi(line) == 2);
 
-			fclose(fp);
-		}
+		fclose(fp);
 	}
 
 	return value;
Signature · 1 line
1.8.3.4
author
#1 lore raw

PATCH 2/2 perf record: Fix crash when kptr is restricted

Before this patch, a simple 'perf record' could fail if kptr_restrict is set to 1 (for normal user) or 2 (for root):

 # perf record ls
 WARNING: Kernel address maps (/proc/{kallsyms,modules}) are restricted,
 check /proc/sys/kernel/kptr_restrict.

Samples in kernel functions may not be resolved if a suitable vmlinux file is not found in the buildid cache or in the vmlinux path.

Samples in kernel modules won't be resolved at all.

If some relocation was applied (e.g. kexec) symbols may be misresolved even with a suitable vmlinux or kallsyms file.

Segmentation fault (core dumped)

This patch skips perf_event__synthesize_kernel_mmap() when kptr is not available.

Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Zefan Li <[email protected]>
---
 tools/perf/util/event.c | 2 ++
 1 file changed, 2 insertions(+)
tools/perf/util/event.c +2 -0
index f6fcc68..9b141f1 100644
@@ -673,6 +673,8 @@ int perf_event__synthesize_kernel_mmap(struct perf_tool *tool,
 	int err;
 	union perf_event *event;
 
+	if (symbol_conf.kptr_restrict)
+		return -1;
 	if (map == NULL)
 		return -1;
 
Signature · 1 line
1.8.3.4
#2 lore raw

Re: [PATCH 2/2] perf record: Fix crash when kptr is restricted

Em Tue, May 24, 2016 at 09:21:28AM +0000, Wang Nan escreveu:

Quoted 2 lines

Before this patch, a simple 'perf record' could fail if kptr_restrict is set to 1 (for normal user) or 2 (for root):

sorry, hadn't seen this one, applying. :-)

- Arnaldo

Quoted 40 lines
 # perf record ls
 WARNING: Kernel address maps (/proc/{kallsyms,modules}) are restricted,
 check /proc/sys/kernel/kptr_restrict.

Samples in kernel functions may not be resolved if a suitable vmlinux file is not found in the buildid cache or in the vmlinux path.

Samples in kernel modules won't be resolved at all.

If some relocation was applied (e.g. kexec) symbols may be misresolved even with a suitable vmlinux or kallsyms file.

Segmentation fault (core dumped)

This patch skips perf_event__synthesize_kernel_mmap() when kptr is not available.

Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Zefan Li <[email protected]>
---
 tools/perf/util/event.c | 2 ++
 1 file changed, 2 insertions(+)
tools/perf/util/event.c +2 -0
index f6fcc68..9b141f1 100644
@@ -673,6 +673,8 @@ int perf_event__synthesize_kernel_mmap(struct perf_tool *tool,
 	int err;
 	union perf_event *event;
 
+	if (symbol_conf.kptr_restrict)
+		return -1;
 	if (map == NULL)
 		return -1;
 
Signature · 1 line
1.8.3.4
maintainer Applied
#3 lore raw

Re: [PATCH 2/2] perf record: Fix crash when kptr is restricted

In reply to Wang Nan

Em Tue, May 24, 2016 at 09:21:28AM +0000, Wang Nan escreveu:

Quoted 15 lines

Before this patch, a simple 'perf record' could fail if kptr_restrict is set to 1 (for normal user) or 2 (for root):

 # perf record ls
 WARNING: Kernel address maps (/proc/{kallsyms,modules}) are restricted,
 check /proc/sys/kernel/kptr_restrict.

Samples in kernel functions may not be resolved if a suitable vmlinux file is not found in the buildid cache or in the vmlinux path.

Samples in kernel modules won't be resolved at all.

If some relocation was applied (e.g. kexec) symbols may be misresolved even with a suitable vmlinux or kallsyms file.

Quoted 6 lines

Segmentation fault (core dumped)

This patch skips perf_event__synthesize_kernel_mmap() when kptr is not available.

Thanks, applied and added a:

Fixes 45e90056904b ("perf machine: Do not bail out if not managing to read ref reloc symbol")

But I'll investigate a bit more later, as the patch fixes the problem, but I think probably it'd be better to check what is not being set up in the ref reloc symbol stuff not bailed out by the patch above.

- Arnaldo

Quoted 22 lines
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Zefan Li <[email protected]>
---
 tools/perf/util/event.c | 2 ++
 1 file changed, 2 insertions(+)
tools/perf/util/event.c +2 -0
index f6fcc68..9b141f1 100644
@@ -673,6 +673,8 @@ int perf_event__synthesize_kernel_mmap(struct perf_tool *tool,
 	int err;
 	union perf_event *event;
 
+	if (symbol_conf.kptr_restrict)
+		return -1;
 	if (map == NULL)
 		return -1;
 
Signature · 1 line
1.8.3.4
#4 lore raw

[tip:perf/urgent] perf record: Fix crash when kptr is restricted

In reply to Wang Nan
perf record: Fix crash when kptr is restricted
Commit-ID:  3dc6c1d54ff4cc9ce7e8513c286c970304cde20b
Gitweb:     http://git.kernel.org/tip/3dc6c1d54ff4cc9ce7e8513c286c970304cde20b
Author:     Wang Nan <[email protected]>
AuthorDate: Tue, 24 May 2016 09:21:28 +0000
Committer:  Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Fri, 27 May 2016 09:41:39 -0300

perf record: Fix crash when kptr is restricted

Before this patch, a simple 'perf record' could fail if kptr_restrict is set to 1 (for normal user) or 2 (for root):

  # perf record ls
  WARNING: Kernel address maps (/proc/{kallsyms,modules}) are restricted,
  check /proc/sys/kernel/kptr_restrict.

Samples in kernel functions may not be resolved if a suitable vmlinux file is not found in the buildid cache or in the vmlinux path.

Samples in kernel modules won't be resolved at all.

If some relocation was applied (e.g. kexec) symbols may be misresolved even with a suitable vmlinux or kallsyms file.

Segmentation fault (core dumped)

This patch skips perf_event__synthesize_kernel_mmap() when kptr is not available.

Fixes 45e90056904b ("perf machine: Do not bail out if not managing to read ref reloc symbol")
Cc Zefan Li <[email protected]>
---
 tools/perf/util/event.c | 2 ++
 1 file changed, 2 insertions(+)
tools/perf/util/event.c +2 -0
index f6fcc68..9b141f1 100644
@@ -673,6 +673,8 @@ int perf_event__synthesize_kernel_mmap(struct perf_tool *tool,
 	int err;
 	union perf_event *event;
 
+	if (symbol_conf.kptr_restrict)
+		return -1;
 	if (map == NULL)
 		return -1;
 
#5 lore raw
In reply to Wang Nan

Em Tue, May 24, 2016 at 09:21:27AM +0000, Wang Nan escreveu:

Quoted 3 lines

If kptr_restrict is set to 2, even root is not allowed to see pointers. This patch checks kptr_restrict even if euid == 0. For root, report error if kptr_restrict is 2.

Improves the situation, but kptr_restrict=2 still causes a segfault:

[root@jouet ~]# echo 2 > /proc/sys/kernel/kptr_restrict [root@jouet ~]# perf record usleep 1
Segmentation fault (core dumped)
[root@jouet ~]# echo 1 > /proc/sys/kernel/kptr_restrict [root@jouet ~]# perf record usleep 1
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.018 MB perf.data (8 samples) ] [root@jouet ~]# echo 2 > /proc/sys/kernel/kptr_restrict [root@jouet ~]# perf record usleep 1
WARNING: Kernel address maps (/proc/{kallsyms,modules}) are restricted, check /proc/sys/kernel/kptr_restrict.

Samples in kernel functions may not be resolved if a suitable vmlinux file is not found in the buildid cache or in the vmlinux path.

Samples in kernel modules won't be resolved at all.

If some relocation was applied (e.g. kexec) symbols may be misresolved even with a suitable vmlinux or kallsyms file.

Segmentation fault (core dumped)
[root@jouet ~]#

Applying your patch and then investigating this other one.

Thanks,

- Arnaldo

Quoted 40 lines
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Zefan Li <[email protected]>
---
 tools/perf/util/symbol.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
tools/perf/util/symbol.c +8 -8
index 20f9cb3..54c4ff2 100644
@@ -1933,17 +1933,17 @@ int setup_intlist(struct intlist **list, const char *list_str,
 static bool symbol__read_kptr_restrict(void)
 {
 	bool value = false;
+	FILE *fp = fopen("/proc/sys/kernel/kptr_restrict", "r");
 
-	if (geteuid() != 0) {
-		FILE *fp = fopen("/proc/sys/kernel/kptr_restrict", "r");
-		if (fp != NULL) {
-			char line[8];
+	if (fp != NULL) {
+		char line[8];
 
-			if (fgets(line, sizeof(line), fp) != NULL)
-				value = atoi(line) != 0;
+		if (fgets(line, sizeof(line), fp) != NULL)
+			value = (geteuid() != 0) ?
+					(atoi(line) != 0) :
+					(atoi(line) == 2);
 
-			fclose(fp);
-		}
+		fclose(fp);
 	}
 
 	return value;
Signature · 1 line
1.8.3.4
#6 lore raw

[tip:perf/urgent] perf symbols: Check kptr_restrict for root

In reply to Wang Nan
perf symbols: Check kptr_restrict for root
Commit-ID:  38272dc4f1b17437871b786d567e1242d0904f5a
Gitweb:     http://git.kernel.org/tip/38272dc4f1b17437871b786d567e1242d0904f5a
Author:     Wang Nan <[email protected]>
AuthorDate: Tue, 24 May 2016 09:21:27 +0000
Committer:  Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Fri, 27 May 2016 09:41:23 -0300

perf symbols: Check kptr_restrict for root

If kptr_restrict is set to 2, even root is not allowed to see pointers. This patch checks kptr_restrict even if euid == 0. For root, report error if kptr_restrict is 2.

---
 tools/perf/util/symbol.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
tools/perf/util/symbol.c +8 -8
index 20f9cb3..54c4ff2 100644
@@ -1933,17 +1933,17 @@ int setup_intlist(struct intlist **list, const char *list_str,
 static bool symbol__read_kptr_restrict(void)
 {
 	bool value = false;
+	FILE *fp = fopen("/proc/sys/kernel/kptr_restrict", "r");
 
-	if (geteuid() != 0) {
-		FILE *fp = fopen("/proc/sys/kernel/kptr_restrict", "r");
-		if (fp != NULL) {
-			char line[8];
+	if (fp != NULL) {
+		char line[8];
 
-			if (fgets(line, sizeof(line), fp) != NULL)
-				value = atoi(line) != 0;
+		if (fgets(line, sizeof(line), fp) != NULL)
+			value = (geteuid() != 0) ?
+					(atoi(line) != 0) :
+					(atoi(line) == 2);
 
-			fclose(fp);
-		}
+		fclose(fp);
 	}
 
 	return value;