Skip to contentSkip to conversation

exec: use strnlen() in __set_task_comm

PATCH Applied Thorsten Blum 3 participants 4 messages Apr 1

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 exec: use strnlen() in __set_task_comm

Use strnlen() to limit source string scanning to 'TASK_COMM_LEN - 1' bytes.

---
 fs/exec.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/exec.c +1 -1
index 9ea3a775d51e..ba12b4c466f6 100644
@@ -1074,7 +1074,7 @@ static int unshare_sighand(struct task_struct *me)
  */
 void __set_task_comm(struct task_struct *tsk, const char *buf, bool exec)
 {
-	size_t len = min(strlen(buf), sizeof(tsk->comm) - 1);
+	size_t len = strnlen(buf, sizeof(tsk->comm) - 1);
 
 	trace_task_rename(tsk, buf);
 	memcpy(tsk->comm, buf, len);
#1 lore raw

PATCH 2/2 sched: update task_struct->comm comment

Since commit 3a3f61ce5e0b ("exec: Make sure task->comm is always NUL-terminated"), __set_task_comm() is unlocked and no longer uses strscpy_pad() - update the stale comment accordingly.

---
 include/linux/sched.h | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
include/linux/sched.h +3 -6
index 5a5d3dbc9cdf..f510b465db04 100644
@@ -1159,12 +1159,9 @@ struct task_struct {
 	/*
 	 * executable name, excluding path.
 	 *
-	 * - 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.
+	 * - normally initialized by begin_new_exec()
+	 * - set it with set_task_comm() to ensure it is always
+	 *   NUL-terminated and zero-padded
 	 */
 	char				comm[TASK_COMM_LEN];
 
reviewer Reviewed-by on v1 1/2
#2 lore raw
In reply to Thorsten Blum
Quoted 4 lines · Thorsten Blum

Use strnlen() to limit source string scanning to 'TASK_COMM_LEN - 1' bytes.

Looks good. Feel free to add:

Honza

Quoted 17 lines
---
 fs/exec.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/exec.c +1 -1
index 9ea3a775d51e..ba12b4c466f6 100644
@@ -1074,7 +1074,7 @@ static int unshare_sighand(struct task_struct *me)
  */
 void __set_task_comm(struct task_struct *tsk, const char *buf, bool exec)
 {
-	size_t len = min(strlen(buf), sizeof(tsk->comm) - 1);
+	size_t len = strnlen(buf, sizeof(tsk->comm) - 1);
 
 	trace_task_rename(tsk, buf);
 	memcpy(tsk->comm, buf, len);
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR