Linux-Trace-Devel Archive on lore.kernel.org
 help / color / Atom feed
From: vincent.donnefort@arm.com
To: linux-trace-devel@vger.kernel.org
Cc: Vincent Donnefort <vincent.donnefort@arm.com>
Subject: [PATCH v2 1/2] trace-cmd: Enable kptr_restrict
Date: Tue, 12 Nov 2019 12:03:26 +0000
Message-ID: <1573560207-52550-1-git-send-email-vincent.donnefort@arm.com> (raw)
In-Reply-To: <20191111175203.252cc439@gandalf.local.home>

From: Vincent Donnefort <vincent.donnefort@arm.com>

kptr_restrict might prevent trace-cmd from accessing /proc/kallsyms,
leading to a trace without the kernel function names resolved.

Signed-off-by: Vincent Donnefort <vincent.donnefort@arm.com>

diff --git a/lib/trace-cmd/trace-output.c b/lib/trace-cmd/trace-output.c
index 41932ee..be4d3f5 100644
--- a/lib/trace-cmd/trace-output.c
+++ b/lib/trace-cmd/trace-output.c
@@ -674,6 +674,46 @@ static int read_event_files(struct tracecmd_output *handle,
 	return ret;
 }
 
+#define KPTR_UNINITIALIZED 'X'
+
+static void set_proc_kptr_restrict(int reset)
+{
+	char *path = "/proc/sys/kernel/kptr_restrict";
+	static char saved = KPTR_UNINITIALIZED;
+	int fd, ret = -1;
+	struct stat st;
+	char buf;
+
+	if ((reset && saved == KPTR_UNINITIALIZED) ||
+	    (stat(path, &st) < 0))
+		return;
+
+	fd = open(path, O_RDONLY);
+	if (fd < 0)
+		goto err;
+
+	if (reset) {
+		buf = saved;
+	} else {
+		if (read(fd, &buf, 1) < 0)
+			goto err;
+		saved = buf;
+		buf = '0';
+	}
+	close(fd);
+
+	fd = open(path, O_WRONLY);
+	if (fd < 0)
+		goto err;
+	if (write(fd, &buf, 1) > 0)
+		ret = 0;
+err:
+	if (fd > 0)
+		close(fd);
+	if (ret)
+		warning("can't set kptr_restrict");
+}
+
 static int read_proc_kallsyms(struct tracecmd_output *handle,
 			      const char *kallsyms)
 {
@@ -698,12 +738,16 @@ static int read_proc_kallsyms(struct tracecmd_output *handle,
 	endian4 = convert_endian_4(handle, size);
 	if (do_write_check(handle, &endian4, 4))
 		return -1;
+
+	set_proc_kptr_restrict(0);
 	check_size = copy_file(handle, path);
 	if (size != check_size) {
 		errno = EINVAL;
 		warning("error in size of file '%s'", path);
+		set_proc_kptr_restrict(1);
 		return -1;
 	}
+	set_proc_kptr_restrict(1);
 
 	return 0;
 }
-- 
2.7.4


  parent reply index

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-11-07 10:51 [PATCH " vincent.donnefort
2019-11-07 10:51 ` [PATCH 2/2] trace-cmd: Add an option to set saved_cmdlines_size vincent.donnefort
2019-11-11 22:52   ` Steven Rostedt
2019-11-12 12:03     ` Vincent Donnefort
2019-11-12 14:40       ` Steven Rostedt
2019-11-12 19:01         ` Vincent Donnefort
2019-11-12 19:09           ` Steven Rostedt
2019-11-13 12:14             ` [PATCH v2 1/2] trace-cmd: Enable kptr_restrict vincent.donnefort
2019-11-13 12:14               ` [PATCH v2 2/2] trace-cmd: Add an option to set saved_cmdlines_size vincent.donnefort
2019-11-13 12:27             ` [PATCH " Vincent Donnefort
2019-11-13 15:08               ` Steven Rostedt
2019-11-12 12:03     ` vincent.donnefort [this message]
2019-11-12 12:03       ` [PATCH v2 " vincent.donnefort
2019-11-11 22:47 ` [PATCH 1/2] trace-cmd: Enable kptr_restrict Steven Rostedt

Reply instructions:

You may reply publically 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=1573560207-52550-1-git-send-email-vincent.donnefort@arm.com \
    --to=vincent.donnefort@arm.com \
    --cc=linux-trace-devel@vger.kernel.org \
    /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

Linux-Trace-Devel Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-trace-devel/0 linux-trace-devel/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-trace-devel linux-trace-devel/ https://lore.kernel.org/linux-trace-devel \
		linux-trace-devel@vger.kernel.org
	public-inbox-index linux-trace-devel

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-trace-devel


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git