public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] task name handling in proc fs
@ 2004-07-01 22:05 Mike Kravetz
  2004-07-01 22:19 ` Andrew Morton
  0 siblings, 1 reply; 11+ messages in thread
From: Mike Kravetz @ 2004-07-01 22:05 UTC (permalink / raw)
  To: akpm, viro, linux-kernel

We've seen the the top command segfault when dealing with a badly (very
badly) formed task name obtained from a 'stat' file in proc fs.  Upon
further examination, it appears that the task name could be updated at
the same time it is handed off to sprintf in proc_pid_stat().  Now one
could argue that top should be more intelligent and deal with these
badly formed names.  However, I think it's bad to be passing strings that
could be changing to sprintf within the kernel.  I'm pretty sure sprintf
expects the character strings to be static.  Below is a patch to address
this code and one other place dealing with task names in the same file.

-- 
Mike


diff -Naur linux-2.6.7/fs/proc/array.c linux-2.6.7.ptest/fs/proc/array.c
--- linux-2.6.7/fs/proc/array.c	Wed Jun 16 05:19:36 2004
+++ linux-2.6.7.ptest/fs/proc/array.c	Thu Jul  1 17:44:14 2004
@@ -97,14 +97,14 @@
 		name++;
 		i--;
 		*buf = c;
-		if (!c)
+		if (!*buf)
 			break;
-		if (c == '\\') {
-			buf[1] = c;
+		if (*buf == '\\') {
+			buf[1] = *buf;
 			buf += 2;
 			continue;
 		}
-		if (c == '\n') {
+		if (*buf == '\n') {
 			buf[0] = '\\';
 			buf[1] = 'n';
 			buf += 2;
@@ -308,14 +308,11 @@
 	int num_threads = 0;
 	struct mm_struct *mm;
 	unsigned long long start_time;
+	char tname[sizeof(task->comm)];
 
 	state = *get_task_state(task);
 	vsize = eip = esp = 0;
-	task_lock(task);
-	mm = task->mm;
-	if(mm)
-		mm = mmgrab(mm);
-	task_unlock(task);
+	mm = get_task_mm(task);
 	if (mm) {
 		down_read(&mm->mmap_sem);
 		vsize = task_vsize(mm);
@@ -357,11 +354,15 @@
 	/* Temporary variable needed for gcc-2.96 */
 	start_time = jiffies_64_to_clock_t(task->start_time - INITIAL_JIFFIES);
 
+	/* Make a static copy of task name for sprintf */
+	memcpy(tname, task->comm, sizeof(tname));
+	tname[sizeof(tname)-1] = '\0';
+
 	res = sprintf(buffer,"%d (%s) %c %d %d %d %d %d %lu %lu \
 %lu %lu %lu %lu %lu %ld %ld %ld %ld %d %ld %llu %lu %ld %lu %lu %lu %lu %lu \
 %lu %lu %lu %lu %lu %lu %lu %lu %d %d %lu %lu\n",
 		task->pid,
-		task->comm,
+		tname,
 		state,
 		ppid,
 		pgid,

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2004-07-08 17:02 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-07-01 22:05 [PATCH] task name handling in proc fs Mike Kravetz
2004-07-01 22:19 ` Andrew Morton
2004-07-01 22:42   ` Mike Kravetz
2004-07-01 23:03     ` Andrew Morton
2004-07-01 23:38       ` Mike Kravetz
2004-07-07 21:52       ` Mike Kravetz
2004-07-07 22:11         ` Andrew Morton
2004-07-07 23:35           ` Mike Kravetz
2004-07-08  2:32             ` Paul Jackson
2004-07-08 17:01               ` Mike Kravetz
2004-07-02  1:27     ` Andrew Rodland

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox