From: Alejandro Colomar <alx@kernel.org>
To: Yafang Shao <laoar.shao@gmail.com>
Cc: akpm@linux-foundation.org, torvalds@linux-foundation.org,
justinstitt@google.com, ebiederm@xmission.com,
alexei.starovoitov@gmail.com, rostedt@goodmis.org,
catalin.marinas@arm.com, penguin-kernel@i-love.sakura.ne.jp,
linux-mm@kvack.org, linux-fsdevel@vger.kernel.org,
linux-trace-kernel@vger.kernel.org, audit@vger.kernel.org,
linux-security-module@vger.kernel.org, selinux@vger.kernel.org,
bpf@vger.kernel.org, netdev@vger.kernel.org,
dri-devel@lists.freedesktop.org,
Alexander Viro <viro@zeniv.linux.org.uk>,
Christian Brauner <brauner@kernel.org>, Jan Kara <jack@suse.cz>,
Kees Cook <keescook@chromium.org>,
Matus Jokay <matus.jokay@stuba.sk>,
"Serge E. Hallyn" <serge@hallyn.com>
Subject: Re: [PATCH v8 1/8] Get rid of __get_task_comm()
Date: Wed, 28 Aug 2024 14:58:15 +0200 [thread overview]
Message-ID: <n2fxqs3tekvljezaqpfnwhsmjymch4vb47y744zwmy7urf3flv@zvjtepkem4l7> (raw)
In-Reply-To: <lql4y2nvs3ewadszhmv4m6fnqja4ff4ymuurpidlwvgf4twvru@esnh37a2jxbd>
[-- Attachment #1: Type: text/plain, Size: 5628 bytes --]
On Wed, Aug 28, 2024 at 12:15:40PM GMT, Alejandro Colomar wrote:
> Hi Yafang,
>
> On Wed, Aug 28, 2024 at 11:03:14AM GMT, Yafang Shao wrote:
> > 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
> >
> > - The BUILD_BUG_ON() doesn't add any value
> > The only requirement is to ensure that the destination buffer is a valid
> > array.
> >
> > - Zeroing is not necessary in current use cases
> > To avoid confusion, we should remove it. Moreover, not zeroing could
> > potentially make it easier to uncover bugs. If the caller needs a
> > zero-padded task name, it should be explicitly handled at the call site.
> >
> > Suggested-by: Linus Torvalds <torvalds@linux-foundation.org>
> > Link: https://lore.kernel.org/all/CAHk-=wivfrF0_zvf+oj6==Sh=-npJooP8chLPEfaFV0oNYTTBA@mail.gmail.com [0]
> > Link: https://lore.kernel.org/all/CAHk-=whWtUC-AjmGJveAETKOMeMFSTwKwu99v7+b6AyHMmaDFA@mail.gmail.com/
> > Suggested-by: Alejandro Colomar <alx@kernel.org>
> > Link: https://lore.kernel.org/all/2jxak5v6dfxlpbxhpm3ey7oup4g2lnr3ueurfbosf5wdo65dk4@srb3hsk72zwq
> > Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
> > Cc: Alexander Viro <viro@zeniv.linux.org.uk>
> > Cc: Christian Brauner <brauner@kernel.org>
> > Cc: Jan Kara <jack@suse.cz>
> > Cc: Eric Biederman <ebiederm@xmission.com>
> > Cc: Kees Cook <keescook@chromium.org>
> > Cc: Alexei Starovoitov <alexei.starovoitov@gmail.com>
> > Cc: Matus Jokay <matus.jokay@stuba.sk>
> > Cc: Alejandro Colomar <alx@kernel.org>
> > Cc: "Serge E. Hallyn" <serge@hallyn.com>
> > ---
> > fs/exec.c | 10 ----------
> > fs/proc/array.c | 2 +-
> > include/linux/sched.h | 32 ++++++++++++++++++++++++++------
> > kernel/kthread.c | 2 +-
> > 4 files changed, 28 insertions(+), 18 deletions(-)
> >
>
> [...]
>
> > diff --git a/include/linux/sched.h b/include/linux/sched.h
> > index f8d150343d42..c40b95a79d80 100644
> > --- a/include/linux/sched.h
> > +++ b/include/linux/sched.h
>
> [...]
>
> > @@ -1914,10 +1917,27 @@ static inline void set_task_comm(struct task_struct *tsk, const char *from)
> > __set_task_comm(tsk, from, false);
> > }
> >
> > -extern char *__get_task_comm(char *to, size_t len, struct task_struct *tsk);
> > +/*
>
> [...]
>
> > + * - ARRAY_SIZE() can help ensure that @buf is indeed an array.
> > + */
> > #define get_task_comm(buf, tsk) ({ \
> > - BUILD_BUG_ON(sizeof(buf) != TASK_COMM_LEN); \
> > - __get_task_comm(buf, sizeof(buf), tsk); \
> > + strscpy(buf, (tsk)->comm, ARRAY_SIZE(buf)); \
>
> I see that there's a two-argument macro
>
> #define strscpy(dst, src) sized_strscpy(dst, src, sizeof(dst))
>
> which is used in patch 2/8
>
> diff --git a/kernel/auditsc.c b/kernel/auditsc.c
> index 6f0d6fb6523f..e4ef5e57dde9 100644
> --- a/kernel/auditsc.c
> +++ b/kernel/auditsc.c
> @@ -2730,7 +2730,7 @@ void __audit_ptrace(struct task_struct *t)
> context->target_uid = task_uid(t);
> context->target_sessionid = audit_get_sessionid(t);
> security_task_getsecid_obj(t, &context->target_sid);
> - memcpy(context->target_comm, t->comm, TASK_COMM_LEN);
> + strscpy(context->target_comm, t->comm);
> }
>
> /**
Ahh, the actual generic definition is in <include/linux/string.h>.
You could do
diff --git i/include/linux/string.h w/include/linux/string.h
index 9edace076ddb..060504719904 100644
--- i/include/linux/string.h
+++ w/include/linux/string.h
@@ -76,11 +76,11 @@ ssize_t sized_strscpy(char *, const char *, size_t);
* known size.
*/
#define __strscpy0(dst, src, ...) \
- sized_strscpy(dst, src, sizeof(dst) + __must_be_array(dst))
+ sized_strscpy(dst, src, ARRAY_SIZE(dst))
#define __strscpy1(dst, src, size) sized_strscpy(dst, src, size)
#define __strscpy_pad0(dst, src, ...) \
- sized_strscpy_pad(dst, src, sizeof(dst) + __must_be_array(dst))
+ sized_strscpy_pad(dst, src, ARRAY_SIZE(dst))
#define __strscpy_pad1(dst, src, size) sized_strscpy_pad(dst, src, size)
/**
>
> I propose modifying that macro to use ARRAY_SIZE() instead of sizeof(),
> and then calling that macro here too. That would not only make sure
> that this is an array, but make sure that every call to that macro is an
> array. An if there are macros for similar string functions that reduce
> the argument with a usual sizeof(), the same thing could be done to
> those too.
>
> Have a lovley day!
> Alex
>
> > + buf; \
> > })
> >
> > #ifdef CONFIG_SMP
> > diff --git a/kernel/kthread.c b/kernel/kthread.c
> > index f7be976ff88a..7d001d033cf9 100644
> > --- a/kernel/kthread.c
> > +++ b/kernel/kthread.c
> > @@ -101,7 +101,7 @@ void get_kthread_comm(char *buf, size_t buf_size, struct task_struct *tsk)
> > 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;
> > }
> >
> > --
> > 2.43.5
> >
>
> --
> <https://www.alejandro-colomar.es/>
--
<https://www.alejandro-colomar.es/>
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
next prev parent reply other threads:[~2024-08-28 12:58 UTC|newest]
Thread overview: 29+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-08-28 3:03 [PATCH v8 0/8] Improve the copy of task comm Yafang Shao
2024-08-28 3:03 ` [PATCH v8 1/8] Get rid of __get_task_comm() Yafang Shao
2024-08-28 10:15 ` Alejandro Colomar
2024-08-28 12:57 ` Yafang Shao
2024-08-28 12:58 ` Alejandro Colomar [this message]
2024-08-28 13:40 ` Yafang Shao
2024-08-28 13:48 ` Kees Cook
2024-08-28 15:09 ` Alejandro Colomar
2024-08-29 0:17 ` Kees Cook
2024-08-29 0:25 ` Alejandro Colomar
2024-08-28 14:10 ` Alejandro Colomar
2024-08-28 14:03 ` Kees Cook
2024-08-29 6:30 ` Yafang Shao
2024-08-28 3:03 ` [PATCH v8 2/8] auditsc: Replace memcpy() with strscpy() Yafang Shao
2024-09-12 20:59 ` Justin Stitt
2024-08-28 3:03 ` [PATCH v8 3/8] security: Replace memcpy() with get_task_comm() Yafang Shao
2024-08-28 3:03 ` [PATCH v8 4/8] bpftool: Ensure task comm is always NUL-terminated Yafang Shao
2024-09-12 21:14 ` Justin Stitt
2024-09-13 2:20 ` Yafang Shao
2024-08-28 3:03 ` [PATCH v8 5/8] mm/util: Fix possible race condition in kstrdup() Yafang Shao
2024-08-28 3:03 ` [PATCH v8 6/8] mm/util: Deduplicate code in {kstrdup,kstrndup,kmemdup_nul} Yafang Shao
2024-08-28 10:32 ` Alejandro Colomar
2024-08-28 10:33 ` Alejandro Colomar
2024-08-28 12:58 ` Yafang Shao
2024-08-28 3:03 ` [PATCH v8 7/8] net: Replace strcpy() with strscpy() Yafang Shao
2024-09-12 21:22 ` Justin Stitt
2024-08-28 3:03 ` [PATCH v8 8/8] drm: " Yafang Shao
2024-09-12 21:28 ` Justin Stitt
2024-09-13 2:23 ` Yafang Shao
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=n2fxqs3tekvljezaqpfnwhsmjymch4vb47y744zwmy7urf3flv@zvjtepkem4l7 \
--to=alx@kernel.org \
--cc=akpm@linux-foundation.org \
--cc=alexei.starovoitov@gmail.com \
--cc=audit@vger.kernel.org \
--cc=bpf@vger.kernel.org \
--cc=brauner@kernel.org \
--cc=catalin.marinas@arm.com \
--cc=dri-devel@lists.freedesktop.org \
--cc=ebiederm@xmission.com \
--cc=jack@suse.cz \
--cc=justinstitt@google.com \
--cc=keescook@chromium.org \
--cc=laoar.shao@gmail.com \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=linux-security-module@vger.kernel.org \
--cc=linux-trace-kernel@vger.kernel.org \
--cc=matus.jokay@stuba.sk \
--cc=netdev@vger.kernel.org \
--cc=penguin-kernel@i-love.sakura.ne.jp \
--cc=rostedt@goodmis.org \
--cc=selinux@vger.kernel.org \
--cc=serge@hallyn.com \
--cc=torvalds@linux-foundation.org \
--cc=viro@zeniv.linux.org.uk \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox