linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] binfmt: replace deprecated strncpy
@ 2024-03-21 20:04 Justin Stitt
  2024-03-22  3:23 ` Kees Cook
  0 siblings, 1 reply; 2+ messages in thread
From: Justin Stitt @ 2024-03-21 20:04 UTC (permalink / raw)
  To: Alexander Viro, Christian Brauner, Jan Kara, Eric Biederman, Kees Cook
  Cc: linux-fsdevel, linux-mm, linux-kernel, linux-hardening, Justin Stitt

strncpy() is deprecated for use on NUL-terminated destination strings
[1] and as such we should prefer more robust and less ambiguous string
interfaces.

There is a _nearly_ identical implementation of fill_psinfo present in
binfmt_elf.c -- except that one uses get_task_comm over strncpy(). Let's
mirror that in binfmt_elf_fdpic.c

Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings [1]
Link: https://github.com/KSPP/linux/issues/90
Cc: linux-hardening@vger.kernel.org
Signed-off-by: Justin Stitt <justinstitt@google.com>
---
Changes in v2:
- use get_task_comm (thanks Eric)
- Link to v1: https://lore.kernel.org/r/20240321-strncpy-fs-binfmt_elf_fdpic-c-v1-1-fdde26c8989e@google.com
---
Note: build-tested only.

Found with: $ rg "strncpy\("
---
 fs/binfmt_elf_fdpic.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/fs/binfmt_elf_fdpic.c b/fs/binfmt_elf_fdpic.c
index 1920ed69279b..3314249e8674 100644
--- a/fs/binfmt_elf_fdpic.c
+++ b/fs/binfmt_elf_fdpic.c
@@ -1359,7 +1359,7 @@ static int fill_psinfo(struct elf_prpsinfo *psinfo, struct task_struct *p,
 	SET_UID(psinfo->pr_uid, from_kuid_munged(cred->user_ns, cred->uid));
 	SET_GID(psinfo->pr_gid, from_kgid_munged(cred->user_ns, cred->gid));
 	rcu_read_unlock();
-	strncpy(psinfo->pr_fname, p->comm, sizeof(psinfo->pr_fname));
+	get_task_comm(psinfo->pr_fname, p);
 
 	return 0;
 }

---
base-commit: a4145ce1e7bc247fd6f2846e8699473448717b37
change-id: 20240320-strncpy-fs-binfmt_elf_fdpic-c-828286d76310

Best regards,
--
Justin Stitt <justinstitt@google.com>



^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH v2] binfmt: replace deprecated strncpy
  2024-03-21 20:04 [PATCH v2] binfmt: replace deprecated strncpy Justin Stitt
@ 2024-03-22  3:23 ` Kees Cook
  0 siblings, 0 replies; 2+ messages in thread
From: Kees Cook @ 2024-03-22  3:23 UTC (permalink / raw)
  To: Alexander Viro, Christian Brauner, Jan Kara, Eric Biederman,
	Justin Stitt
  Cc: Kees Cook, linux-fsdevel, linux-mm, linux-kernel, linux-hardening

On Thu, 21 Mar 2024 20:04:08 +0000, Justin Stitt wrote:
> strncpy() is deprecated for use on NUL-terminated destination strings
> [1] and as such we should prefer more robust and less ambiguous string
> interfaces.
> 
> There is a _nearly_ identical implementation of fill_psinfo present in
> binfmt_elf.c -- except that one uses get_task_comm over strncpy(). Let's
> mirror that in binfmt_elf_fdpic.c
> 
> [...]

Applied to for-next/execve, thanks!

[1/1] binfmt: replace deprecated strncpy
      https://git.kernel.org/kees/c/5248f4097308

Take care,

-- 
Kees Cook



^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-03-22  3:31 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-03-21 20:04 [PATCH v2] binfmt: replace deprecated strncpy Justin Stitt
2024-03-22  3:23 ` Kees Cook

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox