From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id EBE02C02181 for ; Sun, 26 Jan 2025 14:48:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 341932800FC; Sun, 26 Jan 2025 09:48:48 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 2F1982800FA; Sun, 26 Jan 2025 09:48:48 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1B9DB2800FC; Sun, 26 Jan 2025 09:48:48 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 016632800FA for ; Sun, 26 Jan 2025 09:48:47 -0500 (EST) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 766C3A199F for ; Sun, 26 Jan 2025 14:48:47 +0000 (UTC) X-FDA: 83049884694.07.D914AD0 Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf16.hostedemail.com (Postfix) with ESMTP id A8534180003 for ; Sun, 26 Jan 2025 14:48:45 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=KUQxaxbz; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf16.hostedemail.com: domain of sashal@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=sashal@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737902925; a=rsa-sha256; cv=none; b=j+C63EcNs7/bV0bIvXTAi/OO8nmRRdBkv8wcVjJpjHh/x6tbdq0hccrIKrOlfba+LhqEWg EkfpocQw3cXwkb4FuHXtf+c/0eE1J6fN1uxQ9b0zqPnx2xlnUn2NTCz+ff8B/VfC5TZUqA DEyL0OzZ472yl3bjkxUGNt4B3ajN1tE= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=KUQxaxbz; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf16.hostedemail.com: domain of sashal@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=sashal@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1737902925; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=3hOYCE+eOQXBqzDnYHL9YrTTUbJiPZUq6IYVsT1wkfk=; b=BNT2lkgrzRfYjFYjtWsj8VK/YjBMWXPldp8COCtH37BqNZoRxgM/uqUkNEti8HFwEu061Z PgcdD8jf4yJu/nATJb5JfhUSZmIWm9OaTpr4pUwN0q+HgmtXpIv+0MIUSd7IUeMV7WQzam hgmer6zDMauRsEKBTeZfyAjKLGXdNJw= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id A7A91A403BD; Sun, 26 Jan 2025 14:46:57 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 20DE0C4CEE5; Sun, 26 Jan 2025 14:48:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1737902924; bh=p6l/2yH8BFKnwN+xhLvxUcbnlo/+9LfnC9msHx4euyM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=KUQxaxbzYkDome5wkdy9lKp980XHlomUfunpMJOzq5SDi7elp8BSVZAwb8rB6FWNS q1KrgZeYG64K32IQ+cYtxHSRzlas6S36Q4VJ/6Cec9939noiL1rw8qqNPj5V68aZIO 9eTyYo2BHj5IMnptdQ6QcQ9kejFxQWxuBNF0IhqmfmXVHjmAPFga2EyF2I2QSbDd+Q NrEUil4ol+wk9PQBQFaQnB8/7iw4nnqgn1TcRIOhpeN01IJwp7vjOvc/krX9RyT1TM FbEkGKjl0fkj0+sh/HXYbWV99TPZICNcvlfKEAr9EsRBG8SrgIdoA/CQnyYqKTyZOn M2PrkUqWQav9w== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Kees Cook , =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= , Tycho Andersen , Al Viro , Linus Torvalds , Aleksa Sarai , Sasha Levin , brauner@kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org Subject: [PATCH AUTOSEL 6.13 2/7] exec: fix up /proc/pid/comm in the execveat(AT_EMPTY_PATH) case Date: Sun, 26 Jan 2025 09:48:33 -0500 Message-Id: <20250126144839.925271-2-sashal@kernel.org> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250126144839.925271-1-sashal@kernel.org> References: <20250126144839.925271-1-sashal@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-stable: review X-Patchwork-Hint: Ignore X-stable-base: Linux 6.13 Content-Transfer-Encoding: 8bit X-Rspam-User: X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: A8534180003 X-Stat-Signature: aeisoznaeo8xsnh81j78ptceyredmzt7 X-HE-Tag: 1737902925-868942 X-HE-Meta: U2FsdGVkX1+TeZ0BqM3YKSRWAumh4OuuUUW8jOyEUBujkov0zFLy4LMPMsiH9+7+eF1Rs4xLDQ97YD+gDTStuODMbkXJjW8qxLsI84jiq1w1OI2h/+RhWk/Oh8Dd6qC2ACb9IBrKZiX45WLpVlFSZzdQOHyp/3aRRXNey7vfYMIItjLBNV+r1J/VMYRwj6GMivoejghfjNKcgZFhT1aTzZ7Bk717hCcMFd7EjRANOfkMn2zSoMr18j5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/n7r0/M3BczTb35tI2gsj2yPt6iqR+3ZymsFgHhzcAA1H/2Y1GtMTY6/a6Q7A6YX8B745gKvYvhvQB6+7kdkoG36P4KY0rDamzK2AK8ViPNmDVnKgQUomi+OSD9MH1HVQh2W0z08b1BgpS2N+E0uP0fQAJqaBDMqyUFrYYDjCbki1Q16orNwcu8Bfevxtxkp61Xq1VecnuEu X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Kees Cook [ Upstream commit 543841d1806029889c2f69f040e88b247aba8e22 ] 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 Suggested-by: Tycho Andersen Suggested-by: Al Viro Suggested-by: Linus Torvalds Link: https://github.com/uapi-group/kernel-features#set-comm-field-before-exec [1] Reviewed-by: Aleksa Sarai Tested-by: Zbigniew Jędrzejewski-Szmek Signed-off-by: Kees Cook Signed-off-by: Sasha Levin --- fs/exec.c | 29 ++++++++++++++++++++++++++--- include/linux/binfmts.h | 4 +++- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/fs/exec.c b/fs/exec.c index 98cb7ba9983c7..b1f6b47ad20e1 100644 --- a/fs/exec.c +++ b/fs/exec.c @@ -1341,7 +1341,28 @@ int begin_new_exec(struct linux_binprm * bprm) set_dumpable(current->mm, SUID_DUMP_USER); perf_event_exec(); - __set_task_comm(me, kbasename(bprm->filename), true); + + /* + * If the original filename was empty, alloc_bprm() made up a path + * that will probably not be useful to admins running ps or similar. + * Let's fix it up to be something reasonable. + */ + if (bprm->comm_from_dentry) { + /* + * Hold RCU lock to keep the name from being freed behind our back. + * Use acquire semantics to make sure the terminating NUL from + * __d_alloc() is seen. + * + * Note, we're deliberately sloppy here. We don't need to care about + * detecting a concurrent rename and just want a terminated name. + */ + rcu_read_lock(); + __set_task_comm(me, smp_load_acquire(&bprm->file->f_path.dentry->d_name.name), + true); + rcu_read_unlock(); + } else { + __set_task_comm(me, kbasename(bprm->filename), true); + } /* An exec changes our domain. We are no longer part of the thread group */ @@ -1517,11 +1538,13 @@ static struct linux_binprm *alloc_bprm(int fd, struct filename *filename, int fl if (fd == AT_FDCWD || filename->name[0] == '/') { bprm->filename = filename->name; } else { - if (filename->name[0] == '\0') + if (filename->name[0] == '\0') { bprm->fdpath = kasprintf(GFP_KERNEL, "/dev/fd/%d", fd); - else + bprm->comm_from_dentry = 1; + } else { bprm->fdpath = kasprintf(GFP_KERNEL, "/dev/fd/%d/%s", fd, filename->name); + } if (!bprm->fdpath) goto out_free; diff --git a/include/linux/binfmts.h b/include/linux/binfmts.h index e6c00e860951a..3305c849abd66 100644 --- a/include/linux/binfmts.h +++ b/include/linux/binfmts.h @@ -42,7 +42,9 @@ struct linux_binprm { * Set when errors can no longer be returned to the * original userspace. */ - point_of_no_return:1; + point_of_no_return:1, + /* Set when "comm" must come from the dentry. */ + comm_from_dentry:1; struct file *executable; /* Executable to pass to the interpreter */ struct file *interpreter; struct file *file; -- 2.39.5