+ proc-fix-comm_write-return-value-when-truncated-or-error.patch added to mm-nonmm-unstable branch
Andrew Morton
tree noticeThe patch titled
Subject: proc: fix comm_write return value when truncated or error
has been added to the -mm mm-nonmm-unstable branch. Its filename is
proc-fix-comm_write-return-value-when-truncated-or-error.patch
This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/proc-fix-comm_write-return-value-when-truncated-or-error.patch
This patch will later appear in the mm-nonmm-unstable branch at
git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
Before you just go and hit "reply", please:
a) Consider who else should be cc'ed
b) Prefer to cc a suitable mailing list as well
c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's
*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***
The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there most days
------------------------------------------------------
From: "Shengzhuo Wei" <[email protected]>
Subject: proc: fix comm_write return value when truncated or error
Date: Fri, 24 Apr 2026 04:06:21 +0800
When count exceeds TASK_COMM_LEN-1, comm_write() copies at most TASK_COMM_LEN-1 bytes but returns the original count. This violates write(2) semantics, which require returning the number of bytes actually written.
The count parameter is size_t and should not be repurposed to carry a negative error code on the same_thread_group() failure path.
Introduce a local len for the truncated length and a separate ssize_t ret for the return value.
---
fs/proc/base.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-)
fs/proc/base.c renamed from fs/proc/base.c~proc-fix-comm_write-return-value-when-truncated-or-error +7 -4
@@ -1727,8 +1727,10 @@ static ssize_t comm_write(struct file *fstruct task_struct *p;char buffer[TASK_COMM_LEN ] = {};const size_t maxlen =sizeof (buffer) -1 ; +size_t len = count > maxlen ? maxlen : count; +ssize_t ret; -if (copy_from_user (buffer, buf, count > maxlen ? maxlen : count)) +if (copy_from_user (buffer, buf, len))return -EFAULT ; p =get_proc_task (inode);
@@ -1738,13 +1740,14 @@ static ssize_t comm_write(struct file *fif (same_thread_group (current, p)) {set_task_comm (p, buffer);proc_comm_connector (p); + ret = len; + }else { + ret = -EINVAL ; } -else - count = -EINVAL ;put_task_struct (p); -return count; +return ret; }static int comm_show (struct seq_file *m,void *v)
_
Patches currently in -mm which might be from [email protected] are
proc-fix-comm_write-return-value-when-truncated-or-error.patch
Quoted 8 lines · Andrew Morton
From: "Shengzhuo Wei" <[email protected]>
Subject: proc: fixcomm_writereturn value when truncated or error
Date: Fri, 24 Apr 2026 04:06:21 +0800When count exceeds TASK_COMM_LEN-1,
comm_write()copies at most TASK_COMM_LEN-1 bytes but returns the original count. This violates write(2) semantics, which require returning the number of bytes actually written.
This is sketchy for reasons:
1) not consuming whole buffer may (and will) break programs which write overlong string _and_ use "while (len > 0) { len -= write(); } " full write idiom.
2) adding filesystems semantics of writing into the middle of the file is counter productive here.
If "comm" was regular API, there would be "read comm", "write comm"
+ some locking inside of the kernel. Partial update is kind of silly here because string is small.
IIRC there was sysctl fixes banning partial update of modprobe path or something like that for security/predictability reasons.
Quoted 6 lines
fs/proc/base.c renamed from fs/proc/base.c~proc-fix-comm_write-return-value-when-truncated-or-error +1
-1@@ -1727,8 +1727,10 @@ static ssize_t comm_write(struct file *f -return count; +return ret;