Skip to content

Commit 543841d

Browse files
committed
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 [1] 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>
1 parent 3a3f61c commit 543841d

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

fs/exec.c

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1347,7 +1347,28 @@ int begin_new_exec(struct linux_binprm * bprm)
13471347
set_dumpable(current->mm, SUID_DUMP_USER);
13481348

13491349
perf_event_exec();
1350-
__set_task_comm(me, kbasename(bprm->filename), true);
1350+
1351+
/*
1352+
* If the original filename was empty, alloc_bprm() made up a path
1353+
* that will probably not be useful to admins running ps or similar.
1354+
* Let's fix it up to be something reasonable.
1355+
*/
1356+
if (bprm->comm_from_dentry) {
1357+
/*
1358+
* Hold RCU lock to keep the name from being freed behind our back.
1359+
* Use acquire semantics to make sure the terminating NUL from
1360+
* __d_alloc() is seen.
1361+
*
1362+
* Note, we're deliberately sloppy here. We don't need to care about
1363+
* detecting a concurrent rename and just want a terminated name.
1364+
*/
1365+
rcu_read_lock();
1366+
__set_task_comm(me, smp_load_acquire(&bprm->file->f_path.dentry->d_name.name),
1367+
true);
1368+
rcu_read_unlock();
1369+
} else {
1370+
__set_task_comm(me, kbasename(bprm->filename), true);
1371+
}
13511372

13521373
/* An exec changes our domain. We are no longer part of the thread
13531374
group */
@@ -1521,11 +1542,13 @@ static struct linux_binprm *alloc_bprm(int fd, struct filename *filename, int fl
15211542
if (fd == AT_FDCWD || filename->name[0] == '/') {
15221543
bprm->filename = filename->name;
15231544
} else {
1524-
if (filename->name[0] == '\0')
1545+
if (filename->name[0] == '\0') {
15251546
bprm->fdpath = kasprintf(GFP_KERNEL, "/dev/fd/%d", fd);
1526-
else
1547+
bprm->comm_from_dentry = 1;
1548+
} else {
15271549
bprm->fdpath = kasprintf(GFP_KERNEL, "/dev/fd/%d/%s",
15281550
fd, filename->name);
1551+
}
15291552
if (!bprm->fdpath)
15301553
goto out_free;
15311554

include/linux/binfmts.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ struct linux_binprm {
4242
* Set when errors can no longer be returned to the
4343
* original userspace.
4444
*/
45-
point_of_no_return:1;
45+
point_of_no_return:1,
46+
/* Set when "comm" must come from the dentry. */
47+
comm_from_dentry:1;
4648
struct file *executable; /* Executable to pass to the interpreter */
4749
struct file *interpreter;
4850
struct file *file;

0 commit comments

Comments
 (0)