public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Anton Blanchard <anton@samba.org>
To: a.p.zijlstra@chello.nl, mingo@elte.hu, paulus@samba.org,
	fweisbec@gmail.com
Cc: linux-kernel@vger.kernel.org
Subject: [patch 1/5] perf_counter: Make sure we dont leak kernel memory to userspace
Date: Thu, 16 Jul 2009 20:42:48 +1000	[thread overview]
Message-ID: <20090716104817.273972048@samba.org> (raw)
In-Reply-To: 20090716104247.536695580@samba.org

There are a few places we are leaking tiny amounts of kernel memory
to userspace. This happens when writing out strings because we always align
the end to 64 bits.

To avoid this we should always use an appropriately sized temporary buffer
and ensure it is zeroed.

Since d_path assembles the string from the end of the buffer backwards, we
need to add 64 bits after the buffer to allow for alignment.

We also need to copy arch_vma_name to the temporary buffer, because if we use
it directly we may end up copying to userspace a number of bytes after the end
of the string constant.

Signed-off-by: Anton Blanchard <anton@samba.org>
---

Index: linux.trees.git/kernel/perf_counter.c
===================================================================
--- linux.trees.git.orig/kernel/perf_counter.c	2009-07-16 19:44:23.000000000 +1000
+++ linux.trees.git/kernel/perf_counter.c	2009-07-16 20:25:37.000000000 +1000
@@ -2969,8 +2969,10 @@
 	struct perf_cpu_context *cpuctx;
 	struct perf_counter_context *ctx;
 	unsigned int size;
-	char *comm = comm_event->task->comm;
+	char comm[TASK_COMM_LEN];
 
+	memset(comm, 0, sizeof(comm));
+	strncpy(comm, comm_event->task->comm, sizeof(comm));
 	size = ALIGN(strlen(comm)+1, sizeof(u64));
 
 	comm_event->comm = comm;
@@ -3089,8 +3091,15 @@
 	char *buf = NULL;
 	const char *name;
 
+	memset(tmp, 0, sizeof(tmp));
+
 	if (file) {
-		buf = kzalloc(PATH_MAX, GFP_KERNEL);
+		/*
+		 * d_path works from the end of the buffer backwards, so we
+		 * need to add enough zero bytes after the string to handle
+		 * the 64bit alignment we do later.
+		 */
+		buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
 		if (!buf) {
 			name = strncpy(tmp, "//enomem", sizeof(tmp));
 			goto got_name;
@@ -3101,9 +3110,11 @@
 			goto got_name;
 		}
 	} else {
-		name = arch_vma_name(mmap_event->vma);
-		if (name)
+		if (arch_vma_name(mmap_event->vma)) {
+			name = strncpy(tmp, arch_vma_name(mmap_event->vma),
+				       sizeof(tmp));
 			goto got_name;
+		}
 
 		if (!vma->vm_mm) {
 			name = strncpy(tmp, "[vdso]", sizeof(tmp));

-- 


  reply	other threads:[~2009-07-16 10:57 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-07-16 10:42 [patch 0/5] Various perfcounter fixes Anton Blanchard
2009-07-16 10:42 ` Anton Blanchard [this message]
2009-07-18  9:50   ` [tip:perfcounters/urgent] perf_counter: Make sure we dont leak kernel memory to userspace tip-bot for Anton Blanchard
2009-07-16 10:42 ` [patch 2/5] perf_counter: Synthesize VDSO mmap event Anton Blanchard
2009-07-18  9:50   ` [tip:perfcounters/urgent] " tip-bot for Anton Blanchard
2009-07-16 10:42 ` [patch 3/5] perf_counter: Log vfork as a fork event Anton Blanchard
2009-07-18  9:50   ` [tip:perfcounters/urgent] " tip-bot for Anton Blanchard
2009-07-16 10:42 ` [patch 4/5] perf_counter: Add perf record option to log addresses Anton Blanchard
2009-07-18  9:50   ` [tip:perfcounters/urgent] " tip-bot for Anton Blanchard
2009-07-16 10:42 ` [patch 5/5] perf_counter: Make call graph option consistent Anton Blanchard
2009-07-16 14:49   ` Frederic Weisbecker
2009-07-18  9:50   ` [tip:perfcounters/urgent] " tip-bot for Anton Blanchard
2009-07-16 14:16 ` [patch 0/5] Various perfcounter fixes Peter Zijlstra

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=20090716104817.273972048@samba.org \
    --to=anton@samba.org \
    --cc=a.p.zijlstra@chello.nl \
    --cc=fweisbec@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@elte.hu \
    --cc=paulus@samba.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
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox