Skip to contentSkip to conversation

Add prctl to modify current->comm

PATCH In review Andi Kleen 2 participants 3 messages Sep 7, 2004

lore
Share & tools
lore

Andi Kleen

author
#0 lore raw

PATCH 1/1 Add prctl to modify current->comm

This patch adds a prctl to modify current->comm as shown in /proc. This feature was requested by KDE developers. In KDE most programs are started by forking from a kdeinit program that already has the libraries loaded and some other state.

Problem is to give these forked programs the proper name. It already writes the command line in the environment (as seen in ps), but top uses a different field in /proc/pid/status that reports current->comm. And that was always "kdeinit" instead of the real command name. So you ended up with lots of kdeinits
in your top listing, which was not very useful.

This patch adds a new prctl PR_SET_NAME to allow a program to change its comm field.

I considered the potential security issues of a program obscuring itself with this interface, but I don't think it matters much because a program can already obscure itself when the admin uses ps instead of top. In case of a KDE desktop calling everything kdeinit is much more obfuscation than the alternative.

kernel/sys.c renamed from kernel/sys.c-o +7 -0
diff -u linux-2.6.8-5/kernel/sys.c-o linux-2.6.8-5/kernel/sys.c
@@ -1660,6 +1660,13 @@
 			}
 			current->keep_capabilities = arg2;
 			break;
+		case PR_SET_NAME: {
+			struct task_struct *me = current;
+			me->comm[sizeof(me->comm)-1] = 0;
+			if (strncpy_from_user(me->comm, (char *)arg2, sizeof(me->comm)-1) < 0)
+				return -EFAULT;
+			return 0;
+		}
 		default:
 			error = -EINVAL;
 			break;
include/linux/prctl.h renamed from include/linux/prctl.h-o +1 -0
diff -u linux-2.6.8-5/include/linux/prctl.h-o linux-2.6.8-5/include/linux/prctl.h
@@ -49,5 +49,6 @@
 # define PR_TIMING_TIMESTAMP    1       /* Accurate timestamp based
                                                    process timing */
 
+#define PR_SET_NAME    15		/* Set process name. */
 
 #endif /* _LINUX_PRCTL_H */
#1 lore raw
Quoted 5 lines · Andi Kleen

I considered the potential security issues of a program obscuring itself with this interface, but I don't think it matters much because a program can already obscure itself when the admin uses ps instead of top. In case of a KDE desktop calling everything kdeinit is much more obfuscation than the alternative.

Actually its a lot simpler than that. I long ago as a student wrong a shell which simply did ln [binarytorun] banana; execve(.."banana", ...)

So I agree its not security related. You just need to fix the implementation

kernel/sys.c line 1669

Quoted 7 lines
kernel/sys.c renamed from kernel/sys.c-o +7 -0
+		case PR_SET_NAME: {
+			struct task_struct *me = current;
+			me->comm[sizeof(me->comm)-1] = 0;
+			if (strncpy_from_user(me->comm, (char *)arg2, sizeof(me->comm)-1) < 0)
+				return -EFAULT;
+			return 0;
+		}

If the strncpy_from_user faults the state of current->comm is undefined. This strikes me as bad design.

Alan

author
#2 lore raw
Quoted 9 lines · Alan Cox

Review

Quoted 7 lines
(quoted diff) +7 -0
+		case PR_SET_NAME: {
+			struct task_struct *me = current;
+			me->comm[sizeof(me->comm)-1] = 0;
+			if (strncpy_from_user(me->comm, (char *)arg2, sizeof(me->comm)-1) < 0)
+				return -EFAULT;
+			return 0;
+		}

If the strncpy_from_user faults the state of current->comm is undefined.

I didn't see it as a big issue because the result will be 0 terminated anyways.

Quoted 1 line

This strikes me as bad design.

Here's is a new patch with this fixed.

-Andi

--------------------------------------------------------------------

Allow a program to change its current->comm

Useful for KDE & kdeinit.

kernel/sys.c renamed from kernel/sys.c-o +9 -0
diff -u linux-2.6.8-5/kernel/sys.c-o linux-2.6.8-5/kernel/sys.c
@@ -1660,6 +1660,15 @@
 			}
 			current->keep_capabilities = arg2;
 			break;
+		case PR_SET_NAME: {
+			struct task_struct *me = current;
+			unsigned char ncomm[sizeof(me->comm)];
+			ncomm[sizeof(me->comm)-1] = 0;
+			if (strncpy_from_user(ncomm, (char *)arg2, sizeof(me->comm)-1) < 0)
+				return -EFAULT;
+			memcpy(me->comm, ncomm, sizeof(me->comm));
+			return 0;
+		}
 		default:
 			error = -EINVAL;
 			break;
include/linux/prctl.h renamed from include/linux/prctl.h-o +1 -0
diff -u linux-2.6.8-5/include/linux/prctl.h-o linux-2.6.8-5/include/linux/prctl.h
@@ -49,5 +49,6 @@
 # define PR_TIMING_TIMESTAMP    1       /* Accurate timestamp based
                                                    process timing */
 
+#define PR_SET_NAME    15		/* Set process name. */
 
 #endif /* _LINUX_PRCTL_H */