[PATCH v2 3/3] mm/util: make get_cmdline() and /proc/pid/cmdline share one reader

From: Chris Down

Date: Thu Dec 03 2026 - 21:18:17 EST


get_cmdline() and /proc/pid/cmdline each have their own code to read a
task's command line. In terms of archaeology, commit 21a6457a79a0
("proc: Update get proc_pid_cmdline() to use mm.h helpers") made /proc
call get_cmdline(), but get_cmdline() can only fill one buffer from the
start of the arguments, so commit c2c0bb44620d ("proc: fix PAGE_SIZE
limit of /proc/$PID/cmdline") gave /proc a reader of its own.

Unfortunately this has caused some divergences over the years. For
example, /proc dropped FOLL_FORCE in 2016 and started passing FOLL_ANON
in 2018, but get_cmdline() didn't catch up to that until now. The two
have also come to disagree about tasks that overwrite their arguments,
as setproctitle() does.

Let's move /proc's get_mm_cmdline() to mm/util.c and have it copy to an
iov_iter instead of a user buffer.

Here is a (I hope complete?) list of get_cmdline() changes made to match
it up with /proc:

- Before it would treat a task as having used setproctitle() when the
last byte it read wasn't NUL, and only if the arguments were shorter
than the buffer. Now it looks at the last byte of the arguments, as
/proc does, so audit, which asks for 128 bytes, cuts a title in longer
arguments at NUL instead of logging the old bytes behind it. A
read that stops early at a file mapping is also no longer taken for a
title.
- Before it would append the environment to a title wherever the
environment was. Now it only reads on if the environment starts where
the arguments end.
- Before it would leave the NUL out of the length it returned for a
title. Now it counts the NUL, as it always has for ordinary arguments.
Both of its callers trim trailing NULs.

get_cmdline() also reads through a bounce page now, as /proc does, and
returns 0 if it can't allocate one. Both of its callers allocate with
GFP_KERNEL right before they call it.

Signed-off-by: Chris Down <chris@xxxxxxxxxxxxxx>
---
fs/proc/base.c | 134 ++--------------------------------
include/linux/mm.h | 3 +
mm/util.c | 174 ++++++++++++++++++++++++++++++++++++---------
3 files changed, 147 insertions(+), 164 deletions(-)

diff --git a/fs/proc/base.c b/fs/proc/base.c
index 7685660c3624..2a7897833333 100644
--- a/fs/proc/base.c
+++ b/fs/proc/base.c
@@ -49,6 +49,7 @@
*/

#include <linux/uaccess.h>
+#include <linux/uio.h>

#include <linux/errno.h>
#include <linux/time.h>
@@ -239,145 +240,20 @@ static int proc_root_link(struct dentry *dentry, struct path *path,
return get_task_root(task, path);
}

-/*
- * If the user used setproctitle(), we just get the string from
- * user space at arg_start, and limit it to a maximum of one page.
- */
-static ssize_t get_mm_proctitle(struct mm_struct *mm, char __user *buf,
- size_t count, unsigned long pos,
- unsigned long arg_start)
-{
- char *page;
- int ret, got;
-
- if (pos >= PAGE_SIZE)
- return 0;
-
- page = kmalloc(PAGE_SIZE, GFP_KERNEL);
- if (!page)
- return -ENOMEM;
-
- ret = 0;
- got = access_remote_vm(mm, arg_start, page, PAGE_SIZE, FOLL_ANON);
- if (got > 0) {
- int len = strnlen(page, got);
-
- /* Include the NUL character if it was found */
- if (len < got)
- len++;
-
- if (len > pos) {
- len -= pos;
- if (len > count)
- len = count;
- len -= copy_to_user(buf, page+pos, len);
- if (!len)
- len = -EFAULT;
- ret = len;
- }
- }
- kfree(page);
- return ret;
-}
-
-static ssize_t get_mm_cmdline(struct mm_struct *mm, char __user *buf,
- size_t count, loff_t *ppos)
-{
- unsigned long arg_start, arg_end, env_start, env_end;
- unsigned long pos, len;
- char *page, c;
-
- /* Check if process spawned far enough to have cmdline. */
- if (!mm->env_end)
- return 0;
-
- spin_lock(&mm->arg_lock);
- arg_start = mm->arg_start;
- arg_end = mm->arg_end;
- env_start = mm->env_start;
- env_end = mm->env_end;
- spin_unlock(&mm->arg_lock);
-
- if (arg_start >= arg_end)
- return 0;
-
- /*
- * We allow setproctitle() to overwrite the argument
- * strings, and overflow past the original end. But
- * only when it overflows into the environment area.
- */
- if (env_start != arg_end || env_end < env_start)
- env_start = env_end = arg_end;
- len = env_end - arg_start;
-
- /* We're not going to care if "*ppos" has high bits set */
- pos = *ppos;
- if (pos >= len)
- return 0;
- if (count > len - pos)
- count = len - pos;
- if (!count)
- return 0;
-
- /*
- * Magical special case: if the argv[] end byte is not
- * zero, the user has overwritten it with setproctitle(3).
- *
- * Possible future enhancement: do this only once when
- * pos is 0, and set a flag in the 'struct file'.
- */
- if (access_remote_vm(mm, arg_end-1, &c, 1, FOLL_ANON) == 1 && c)
- return get_mm_proctitle(mm, buf, count, pos, arg_start);
-
- /*
- * For the non-setproctitle() case we limit things strictly
- * to the [arg_start, arg_end[ range.
- */
- pos += arg_start;
- if (pos < arg_start || pos >= arg_end)
- return 0;
- if (count > arg_end - pos)
- count = arg_end - pos;
-
- page = kmalloc(PAGE_SIZE, GFP_KERNEL);
- if (!page)
- return -ENOMEM;
-
- len = 0;
- while (count) {
- int got;
- size_t size = min_t(size_t, PAGE_SIZE, count);
-
- got = access_remote_vm(mm, pos, page, size, FOLL_ANON);
- if (got <= 0)
- break;
- got -= copy_to_user(buf, page, got);
- if (unlikely(!got)) {
- if (!len)
- len = -EFAULT;
- break;
- }
- pos += got;
- buf += got;
- len += got;
- count -= got;
- }
-
- kfree(page);
- return len;
-}
-
static ssize_t get_task_cmdline(struct task_struct *tsk, char __user *buf,
size_t count, loff_t *pos)
{
struct mm_struct *mm;
+ struct iov_iter to;
ssize_t ret;

mm = get_task_mm(tsk);
if (!mm)
return 0;

- ret = get_mm_cmdline(mm, buf, count, pos);
+ iov_iter_ubuf(&to, ITER_DEST, buf, count);
+ /* We're not going to care if "*pos" has high bits set */
+ ret = get_mm_cmdline(mm, &to, *pos);
mmput(mm);
return ret;
}
diff --git a/include/linux/mm.h b/include/linux/mm.h
index d09173a94e9c..5ccbe4ba4e28 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -3600,6 +3600,9 @@ bool folio_mark_dirty_lock(struct folio *folio);
bool set_page_dirty(struct page *page);
int set_page_dirty_lock(struct page *page);

+struct iov_iter;
+ssize_t get_mm_cmdline(struct mm_struct *mm, struct iov_iter *to,
+ unsigned long pos);
int get_cmdline(struct task_struct *task, char *buffer, int buflen);

/*
diff --git a/mm/util.c b/mm/util.c
index 8073af6708fb..bbc4f09d918c 100644
--- a/mm/util.c
+++ b/mm/util.c
@@ -28,6 +28,7 @@
#include <linux/page_idle.h>

#include <linux/uaccess.h>
+#include <linux/uio.h>

#include <kunit/visibility.h>

@@ -1015,26 +1016,66 @@ error:
return -ENOMEM;
}

+static ssize_t get_mm_proctitle(struct mm_struct *mm, struct iov_iter *to,
+ size_t count, unsigned long pos,
+ unsigned long arg_start)
+{
+ char *page;
+ int ret, got;
+
+ if (pos >= PAGE_SIZE)
+ return 0;
+
+ page = kmalloc(PAGE_SIZE, GFP_KERNEL);
+ if (!page)
+ return -ENOMEM;
+
+ ret = 0;
+ got = access_remote_vm(mm, arg_start, page, PAGE_SIZE, FOLL_ANON);
+ if (got > 0) {
+ int len = strnlen(page, got);
+
+ /* Include the NUL character if it was found */
+ if (len < got)
+ len++;
+
+ if (len > pos) {
+ len -= pos;
+ if (len > count)
+ len = count;
+ len = copy_to_iter(page + pos, len, to);
+ if (!len)
+ len = -EFAULT;
+ ret = len;
+ }
+ }
+ kfree(page);
+ return ret;
+}
+
/**
- * get_cmdline() - copy the cmdline value to a buffer.
- * @task: the task whose cmdline value to copy.
- * @buffer: the buffer to copy to.
- * @buflen: the length of the buffer. Larger cmdline values are truncated
- * to this length.
+ * get_mm_cmdline() - read the command line of an mm.
+ * @mm: the mm whose command line to read.
+ * @to: where to copy it to.
+ * @pos: the offset in the command line to start reading at.
*
- * Return: the size of the cmdline field copied. Note that the copy does
- * not guarantee an ending NULL byte.
+ * This is what /proc/pid/cmdline shows. The caller must hold an mm_users
+ * reference on @mm, as from get_task_mm().
+ *
+ * Return: the number of bytes copied, 0 at the end of the command line,
+ * or a negative error code if nothing was copied.
*/
-int get_cmdline(struct task_struct *task, char *buffer, int buflen)
+ssize_t get_mm_cmdline(struct mm_struct *mm, struct iov_iter *to,
+ unsigned long pos)
{
- int res = 0;
- unsigned int len;
- struct mm_struct *mm = get_task_mm(task);
unsigned long arg_start, arg_end, env_start, env_end;
- if (!mm)
- goto out;
- if (!mm->arg_end)
- goto out_mm; /* Shh! No looking before we're done */
+ size_t count = iov_iter_count(to);
+ unsigned long len;
+ char *page, c;
+
+ /* Check if process spawned far enough to have cmdline. */
+ if (!mm->env_end)
+ return 0;

spin_lock(&mm->arg_lock);
arg_start = mm->arg_start;
@@ -1043,34 +1084,97 @@ int get_cmdline(struct task_struct *task, char *buffer, int buflen)
env_end = mm->env_end;
spin_unlock(&mm->arg_lock);

- len = arg_end - arg_start;
+ if (arg_start >= arg_end)
+ return 0;

- if (len > buflen)
- len = buflen;
+ /*
+ * We allow setproctitle() to overwrite the argument
+ * strings, and overflow past the original end. But
+ * only when it overflows into the environment area.
+ */
+ if (env_start != arg_end || env_end < env_start)
+ env_start = env_end = arg_end;
+ len = env_end - arg_start;

- res = access_process_vm(task, arg_start, buffer, len, FOLL_ANON);
+ if (pos >= len)
+ return 0;
+ if (count > len - pos)
+ count = len - pos;
+ if (!count)
+ return 0;

/*
- * If the nul at the end of args has been overwritten, then
- * assume application is using setproctitle(3).
+ * Magical special case: if the argv[] end byte is not
+ * zero, the user has overwritten it with setproctitle(3).
*/
- if (res > 0 && buffer[res-1] != '\0' && len < buflen) {
- len = strnlen(buffer, res);
- if (len < res) {
- res = len;
- } else {
- len = env_end - env_start;
- if (len > buflen - res)
- len = buflen - res;
- res += access_process_vm(task, env_start,
- buffer + res, len, FOLL_ANON);
- res = strnlen(buffer, res);
+ if (access_remote_vm(mm, arg_end-1, &c, 1, FOLL_ANON) == 1 && c)
+ return get_mm_proctitle(mm, to, count, pos, arg_start);
+
+ /*
+ * For the non-setproctitle() case we limit things strictly
+ * to the [arg_start, arg_end[ range.
+ */
+ pos += arg_start;
+ if (pos < arg_start || pos >= arg_end)
+ return 0;
+ if (count > arg_end - pos)
+ count = arg_end - pos;
+
+ page = kmalloc(PAGE_SIZE, GFP_KERNEL);
+ if (!page)
+ return -ENOMEM;
+
+ len = 0;
+ while (count) {
+ int got;
+ size_t size = min_t(size_t, PAGE_SIZE, count);
+
+ got = access_remote_vm(mm, pos, page, size, FOLL_ANON);
+ if (got <= 0)
+ break;
+ got = copy_to_iter(page, got, to);
+ if (unlikely(!got)) {
+ if (!len)
+ len = -EFAULT;
+ break;
}
+ pos += got;
+ len += got;
+ count -= got;
}
-out_mm:
+
+ kfree(page);
+ return len;
+}
+
+/**
+ * get_cmdline() - copy the cmdline value to a buffer.
+ * @task: the task whose cmdline value to copy.
+ * @buffer: the buffer to copy to.
+ * @buflen: the length of the buffer. Larger cmdline values are truncated
+ * to this length.
+ *
+ * This copies the start of what /proc/pid/cmdline shows for @task.
+ *
+ * Return: the size of the cmdline field copied. Note that the copy does
+ * not guarantee an ending NULL byte.
+ */
+int get_cmdline(struct task_struct *task, char *buffer, int buflen)
+{
+ struct kvec kvec = { .iov_base = buffer, .iov_len = buflen };
+ struct mm_struct *mm;
+ struct iov_iter to;
+ ssize_t res;
+
+ mm = get_task_mm(task);
+ if (!mm)
+ return 0;
+
+ iov_iter_kvec(&to, ITER_DEST, &kvec, 1, buflen);
+ res = get_mm_cmdline(mm, &to, 0);
mmput(mm);
-out:
- return res;
+
+ return res > 0 ? res : 0;
}

/**
--
2.56.1