From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.180 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796350692; b=ur+/EYWqjzmEs8DLkHe0MauJe6rFMoAq2zRPdqyjDUjaPkkUqwlpS1+qkClJz9lauFp3 z9MpuMyeQJFQNMfgn5YMDFxnnM4xnCrUOteqGvOl4Fgy0fdFzapu0CMKgCEzq2N/39ASZ yFCL9tbRsykAsGXxPrCcG4Y2FnvGLEbyXPwiwlUdwJ+3b9yZvSBRANUxNrZAAyk7g1GpH p0qMuKSPLO3hZOGcpLUt3n+jPIs0OwsQ7nTpNq9NBl+GV0Jv/TwoVdrGNkeHLy1w2KLae /IlHc2aB/dpGoHaiUk5YtE0KZlhs+PqIfQ2FVnVMBiE3O3Rh4TMrNgxCUhlduVArglg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796350692; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: Date:Message-ID:In-Reply-To:References:From:Subject:To:Cc: MIME-Version:Content-Type:Content-Disposition:User-Agent; bh=z7mhsfLDSvQ3LOJtElyu/yRh5eTv1GM7qZHzVLZocp0=; b=1Vdd+hRB4VumHMleQ2uThg3o5M3zAWnTXxgN55Cq31lVY1LsQFI/+8ocx30Aeopd+cPw 630ApBe4T6rTKtDgNstzZFcBwhbZ57rYGlr6FH63cUrpkoo9X53NOuG3fqwCIP/4b1pWh Jmz/gEVmNSzb3SRqrvtQLPEtHpU2iaP9jsvmIzWfa0fJPqsddH6QpFN046x1F/v0mltIL Z1Nz96XlUp/5tSiq0tDi2qG0eZMvMQe4SbSmin6QUlmKZiKNMJNM1rOjpXiJDP/1dZokv hBNfVYYwzakOsUnCC+inHvF54Xhmxle16g1/vWeC/M/qfiCKQ2DRRni+wY3qUSExqkA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=chrisdown.name; spf=pass smtp.mailfrom=chrisdown.name; dkim=pass header.d=chrisdown.name header.i=@chrisdown.name header.a=rsa-sha256 header.s=google header.b=tEgfgMcf; arc=none smtp.remote-ip=209.85.128.180 Received: from mail-yw1-f180.google.com (mail-yw1-f180.google.com [209.85.128.180]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E895B606A68C for ; Fri, 4 Dec 2026 02:18:11 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org E895B606A68C Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=chrisdown.name Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chrisdown.name Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chrisdown.name header.i=@chrisdown.name header.a=rsa-sha256 header.s=google header.b=tEgfgMcf Received: by mail-yw1-f180.google.com with SMTP id 00721157ae682-8b4cd576f2aso3798047b3.0 for ; Thu, 03 Dec 2026 18:18:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chrisdown.name; s=google; t=1796350691; x=1796955491; darn=vger.kernel.org; h=user-agent:content-disposition:content-type:mime-version:cc:to :subject:from:references:in-reply-to:message-id:date:from:to:cc :subject:date:message-id:reply-to:content-type; bh=z7mhsfLDSvQ3LOJtElyu/yRh5eTv1GM7qZHzVLZocp0=; b=tEgfgMcf89aLUNaorqyWZHV4Y8pq49XqK5J/buEzWBCy3/+azBmllGBXAx93erMYYw L7wIpx6kiOU4Z7J6kkPHfNbuMiicw8mvhOf1UxgbAclZ9B6wvwXhKCIxfpipE/zJlR/R ie7b8TXRXoYVyrPlX1Mkb7EO8UT4bln3xiIH0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796350691; x=1796955491; h=user-agent:content-disposition:content-type:mime-version:cc:to :subject:from:references:in-reply-to:message-id:date:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=z7mhsfLDSvQ3LOJtElyu/yRh5eTv1GM7qZHzVLZocp0=; b=asAxXgfrzlEi7VM5jfVoOJswHJnNTbJel86jKaJLkgAUaG6EHm+Ch/i8h8QVNf6K4y yaM62zawD6C/dyoE7Px6E5sIU/0BYTg0die0sWNxJ2Dy32Riu5ZLHpi/IP5MoyQtYb4U xilCTLBf2eO+VdROpHydGD426PeA+2DZxPskJbDtZSYx4KKQrTd/cGhmYSijRDwpCaO5 fNF+KlI64DzQ8lMU0zpKXf1R3TNIvBSHoO8Ag1P4Gd81VccY7obDak3IaAjq4aPreHAX TOY5VQ4ZkO35XQKR8QE6sMAv7QSVEevZnGBN4DoO32nTmA21cqFopkYmg2f3e4kbP/1L RiKQ== X-Forwarded-Encrypted: i=1; AL+hErF0MUZyTB1bjkKX3soU4iW9q7KivLDg23heMbmfiNm5Qv5Rtpz3O137jrbddUAXa1bw6jPGcr7ONWydpgE=@vger.kernel.org X-Gm-Message-State: APuU4Z+xvdaZKrBYbXWY0LgwIS5G6gacwXy1x4MjoNCwNrn1qgbKmISJ 5C2fkhgmzZdVWJZsISg26ub/RF32M/9Ux5rDhq+ICQ/B5MjnkJGSscFYPoaQRRnMl9c= X-Gm-Gg: AZFdTZcO9+NEu2BpJvdTfi8ZWw9AnQ1MRjMrsriXmonAynclu7akCRXKgLlNsNmlOpu JFZSlA2i+VaA5Znbq7dsgEck82SXwkXZEeUunSS8uTSbHt1zq8IymLd2qqTvwD1MeJebxvTji44 0xDZXGIPyNsctb1e0/9sEq3YFZPFVzeYZw+jsgZD9WrFJQkzRehtzH/Wzhl1SVnZB3j5zrDpBJI bNtY7NchGDd2KZtiR8GxsA0me7Cqze7IXkvCWbc77lhQozy2HYhrL25ps5gxJDqLPtiDrbnzWuO JwwmJ84NG9v5st53ozOzDZLQXBMgKCkA0swkpWcNMTp5x0k6CusN92qQJbKcGF2x5WEWyKQD/cR HEOl1rlZG6NjcpBiH2w29ZMGlwVu3VMgR0VSd6uAYBClwWdhlZBZ64/++9d21uxAGpgrvv4j79r 93X6GL7uXIdHcSDBwl9gECaef4bZkLVmBn/XcoBnMi6wH0hOmVPhw120OCKCdGnI0fuznkjV1b6 CdwuBCkkG5cgi1z8QXpzk5Z2oOwQcTKTQ3oCeEgOsO8+jbtK/F3ILpMvPtIuD72HID2JAlzYSC0 rOZVrONH2N5pLES0bvuGkuToCcFRDxWtLIOLJJXh2NzsobofWDsZMtXdMpVVzE4N3dnF8xT1AzU zMx1q+ebpH7tf2QUwKroCOjbm/ycUHlQeVPSC9PSejrhFXHbkN5vVY+T+GRVU9i4dO+5PMN0irp t6xv1TOunf8mRXQhIFlJFCd0asEypa/sGCeEr4slkFb1mKj0I7y8GtafZAUjc8Lnh1MbCeXMPhc GGhyeKQzr5+N7AIRDe4JnQ= X-Received: by 2002:a05:690c:3702:b0:8b3:5a91:61e8 with SMTP id 00721157ae682-8b660e21694mr4552377b3.69.1796350690873; Thu, 03 Dec 2026 18:18:10 -0800 (PST) Received: from [172.18.0.2] (c-73-207-185-9.hsd1.ga.comcast.net. [73.207.185.9]) by smtp.gmail.com with ESMTPSA id 00721157ae682-8b660e10477sm3733717b3.43.2026.12.03.18.18.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Dec 2026 18:18:10 -0800 (PST) Date: Thu, 03 Dec 2026 18:18:08 -0800 Message-ID: <4e25716adf168f4c59dc0ef1d2caa889f1b4e1a2.1796349991.git.chris@chrisdown.name> In-Reply-To: References: From: Chris Down Subject: [PATCH v2 3/3] mm/util: make get_cmdline() and /proc/pid/cmdline share one reader To: Andrew Morton , David Hildenbrand Cc: Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Chris Mason , Paul Moore , Kees Cook , Willy Tarreau , audit@vger.kernel.org, sashiko-bot@kernel.org, kernel-team@meta.com X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/2.4.3 (cc9e3943) (2026-10-09) 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 --- 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 +#include #include #include @@ -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 #include +#include #include @@ -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