exec: fix up /proc/pid/comm in the execveat(AT_EMPTY_PATH) case
authorKees Cook <kees@kernel.org>
Thu, 21 Nov 2024 15:07:05 +0000 (07:07 -0800)
committerKees Cook <kees@kernel.org>
Tue, 17 Dec 2024 00:54:00 +0000 (16:54 -0800)
commit543841d1806029889c2f69f040e88b247aba8e22
tree884ec17ff7ca5896763830ecfb056920e3ec0df7
parent3a3f61ce5e0b4bcf730acc09c1af91012d241f85
exec: fix up /proc/pid/comm in the execveat(AT_EMPTY_PATH) case

Zbigniew mentioned at Linux Plumber's that systemd is interested in
switching to execveat() for service execution, but can't, because the
contents of /proc/pid/comm are the file descriptor which was used,
instead of the path to the binary[1]. This makes the output of tools like
top and ps useless, especially in a world where most fds are opened
CLOEXEC so the number is truly meaningless.

When the filename passed in is empty (e.g. with AT_EMPTY_PATH), use the
dentry's filename for "comm" instead of using the useless numeral from
the synthetic fdpath construction. This way the actual exec machinery
is unchanged, but cosmetically the comm looks reasonable to admins
investigating things.

Instead of adding TASK_COMM_LEN more bytes to bprm, use one of the unused
flag bits to indicate that we need to set "comm" from the dentry.

Suggested-by: Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Suggested-by: Tycho Andersen <tandersen@netflix.com>
Suggested-by: Al Viro <viro@zeniv.linux.org.uk>
Suggested-by: Linus Torvalds <torvalds@linux-foundation.org>
Link: https://github.com/uapi-group/kernel-features#set-comm-field-before-exec
Reviewed-by: Aleksa Sarai <cyphar@cyphar.com>
Tested-by: Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Signed-off-by: Kees Cook <kees@kernel.org>
fs/exec.c
include/linux/binfmts.h