Skip to contentSkip to conversation

get-rid-of-__get_task_comm.patch removed from -mm tree

merged mm-nonmm-stable Andrew Morton 1 participant 1 message Nov 6, 2024

lore
Share & tools
lore

Andrew Morton

author tree notice Applied
#0 lore raw

The quilt patch titled
Subject: get rid of __get_task_comm()
has been removed from the -mm tree. Its filename was
get-rid-of-__get_task_comm.patch

This patch was dropped because it was merged into the mm-nonmm-stable branch of git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm

------------------------------------------------------
From: Yafang Shao <[email protected]>
Subject: get rid of __get_task_comm()
Date: Mon, 7 Oct 2024 22:49:05 +0800

Patch series "Improve the copy of task comm", v8.

Using {memcpy,strncpy,strcpy,kstrdup} to copy the task comm relies on the length of task comm. Changes in the task comm could result in a destination string that is overflow. Therefore, we should explicitly ensure the destination string is always NUL-terminated, regardless of the task comm. This approach will facilitate future extensions to the task comm.

As suggested by Linus [0], we can identify all relevant code with the following git grep command:

git grep 'memcpy.*->comm\>'
git grep 'kstrdup.*->comm\>'
git grep 'strncpy.*->comm\>'
git grep 'strcpy.*->comm\>'

PATCH #2~#4:   memcpy
PATCH #5~#6:   kstrdup
PATCH #7:      strcpy

Please note that strncpy() is not included in this series as it is being tracked by another effort. [1]

This patch (of 7):

We want to eliminate the use of __get_task_comm() for the following reasons:

- The task_lock() is unnecessary
Quoted from Linus [0]:
: Since user space can randomly change their names anyway, using locking : was always wrong for readers (for writers it probably does make sense : to have some lock - although practically speaking nobody cares there : either, but at least for a writer some kind of race could have : long-term mixed results

Cc Alexander Viro <[email protected]>
Cc Christian Brauner <[email protected]>
Cc Jan Kara <[email protected]>
Cc Eric Biederman <[email protected]>
Cc Kees Cook <[email protected]>
Cc Alexei Starovoitov <[email protected]>
Cc Matus Jokay <[email protected]>
Cc Alejandro Colomar <[email protected]>
Cc "Serge E. Hallyn" <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Justin Stitt <[email protected]>
Cc Steven Rostedt (Google) <[email protected]>
Cc Tetsuo Handa <[email protected]>
Cc Andy Shevchenko <[email protected]>
Cc Daniel Vetter <[email protected]>
Cc David Airlie <[email protected]>
Cc Eric Paris <[email protected]>
Cc James Morris <[email protected]>
Cc Maarten Lankhorst <[email protected]>
Cc Matthew Wilcox <[email protected]>
Cc Maxime Ripard <[email protected]>
Cc Ondrej Mosnacek <[email protected]>
Cc Paul Moore <[email protected]>
Cc Quentin Monnet <[email protected]>
Cc Simon Horman <[email protected]>
Cc Stephen Smalley <[email protected]>
Cc Thomas Zimmermann <[email protected]>

---

 fs/exec.c             |   10 ----------
 fs/proc/array.c       |    2 +-
 include/linux/sched.h |   28 ++++++++++++++++++++++------
 kernel/kthread.c      |    2 +-
 4 files changed, 24 insertions(+), 18 deletions(-)
fs/exec.c renamed from fs/exec.c~get-rid-of-__get_task_comm +0 -10
@@ -1189,16 +1189,6 @@ static int unshare_sighand(struct task_s
 	return 0;
 }
 
-char *__get_task_comm(char *buf, size_t buf_size, struct task_struct *tsk)
-{
-	task_lock(tsk);
-	/* Always NUL terminated and zero-padded */
-	strscpy_pad(buf, tsk->comm, buf_size);
-	task_unlock(tsk);
-	return buf;
-}
-EXPORT_SYMBOL_GPL(__get_task_comm);
-
 /*
  * These functions flushes out all traces of the currently running executable
  * so that a new one can be started
fs/proc/array.c renamed from fs/proc/array.c~get-rid-of-__get_task_comm +1 -1
@@ -109,7 +109,7 @@ void proc_task_name(struct seq_file *m,
 	else if (p->flags & PF_KTHREAD)
 		get_kthread_comm(tcomm, sizeof(tcomm), p);
 	else
-		__get_task_comm(tcomm, sizeof(tcomm), p);
+		get_task_comm(tcomm, p);
 
 	if (escape)
 		seq_escape_str(m, tcomm, ESCAPE_SPACE | ESCAPE_SPECIAL, "\n\\");
include/linux/sched.h renamed from include/linux/sched.h~get-rid-of-__get_task_comm +22 -6
@@ -1121,9 +1121,12 @@ struct task_struct {
 	/*
 	 * executable name, excluding path.
 	 *
-	 * - normally initialized setup_new_exec()
-	 * - access it with [gs]et_task_comm()
-	 * - lock it with task_lock()
+	 * - normally initialized begin_new_exec()
+	 * - set it with set_task_comm()
+	 *   - strscpy_pad() to ensure it is always NUL-terminated and
+	 *     zero-padded
+	 *   - task_lock() to ensure the operation is atomic and the name is
+	 *     fully updated.
 	 */
 	char				comm[TASK_COMM_LEN];
 
@@ -1938,10 +1941,23 @@ static inline void set_task_comm(struct
 	__set_task_comm(tsk, from, false);
 }
 
-extern char *__get_task_comm(char *to, size_t len, struct task_struct *tsk);
+/*
+ * - Why not use task_lock()?
+ *   User space can randomly change their names anyway, so locking for readers
+ *   doesn't make sense. For writers, locking is probably necessary, as a race
+ *   condition could lead to long-term mixed results.
+ *   The strscpy_pad() in __set_task_comm() can ensure that the task comm is
+ *   always NUL-terminated and zero-padded. Therefore the race condition between
+ *   reader and writer is not an issue.
+ *
+ * - BUILD_BUG_ON() can help prevent the buf from being truncated.
+ *   Since the callers don't perform any return value checks, this safeguard is
+ *   necessary.
+ */
 #define get_task_comm(buf, tsk) ({			\
-	BUILD_BUG_ON(sizeof(buf) != TASK_COMM_LEN);	\
-	__get_task_comm(buf, sizeof(buf), tsk);		\
+	BUILD_BUG_ON(sizeof(buf) < TASK_COMM_LEN);	\
+	strscpy_pad(buf, (tsk)->comm);			\
+	buf;						\
 })
 
 #ifdef CONFIG_SMP
kernel/kthread.c renamed from kernel/kthread.c~get-rid-of-__get_task_comm +1 -1
@@ -101,7 +101,7 @@ void get_kthread_comm(char *buf, size_t
 	struct kthread *kthread = to_kthread(tsk);
 
 	if (!kthread || !kthread->full_name) {
-		__get_task_comm(buf, buf_size, tsk);
+		strscpy(buf, tsk->comm, buf_size);
 		return;
 	}
 

_

Patches currently in -mm which might be from [email protected] are