All of lore.kernel.org
 help / color / mirror / Atom feed
From: Wang Nan <wangnan0@huawei.com>
To: <acme@kernel.org>
Cc: <linux-kernel@vger.kernel.org>, <lizefan@huawei.com>,
	<pi3orama@163.com>, Wang Nan <wangnan0@huawei.com>,
	Arnaldo Carvalho de Melo <acme@redhat.com>
Subject: [PATCH] perf record: Fix segfault when running with suid and kptr_restrict is 1
Date: Wed, 21 Sep 2016 03:48:20 +0000	[thread overview]
Message-ID: <1474429700-130812-1-git-send-email-wangnan0@huawei.com> (raw)

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.

Signed-off-by: Wang Nan <wangnan0@huawei.com>
Cc: Arnaldo Carvalho de Melo <acme@redhat.com>
---
 tools/perf/util/symbol.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c
index 19c9c55..9528702 100644
--- a/tools/perf/util/symbol.c
+++ b/tools/perf/util/symbol.c
@@ -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);
 
-- 
1.8.3.4

             reply	other threads:[~2016-09-21  3:49 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-09-21  3:48 Wang Nan [this message]
2016-09-21  3:55 ` [PATCH] perf record: Fix segfault when running with suid and kptr_restrict is 1 Wangnan (F)
2016-09-21  3:56 Wang Nan
2016-09-22 15:41 ` Arnaldo Carvalho de Melo
2016-09-23  7:00   ` Wangnan (F)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1474429700-130812-1-git-send-email-wangnan0@huawei.com \
    --to=wangnan0@huawei.com \
    --cc=acme@kernel.org \
    --cc=acme@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lizefan@huawei.com \
    --cc=pi3orama@163.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.