From patchwork Mon Aug 12 02:29:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13760005 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 C87C3C3DA4A for ; Mon, 12 Aug 2024 02:30:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5DF896B008C; Sun, 11 Aug 2024 22:30:40 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5676B6B009A; Sun, 11 Aug 2024 22:30:40 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 408EE6B009E; Sun, 11 Aug 2024 22:30:40 -0400 (EDT) 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 1CA766B008C for ; Sun, 11 Aug 2024 22:30:40 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id C6E6FA0739 for ; Mon, 12 Aug 2024 02:30:39 +0000 (UTC) X-FDA: 82442014998.17.ABD3999 Received: from mail-pg1-f177.google.com (mail-pg1-f177.google.com [209.85.215.177]) by imf06.hostedemail.com (Postfix) with ESMTP id DB0C8180003 for ; Mon, 12 Aug 2024 02:30:37 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="ZA6/VzQk"; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf06.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.215.177 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723429827; a=rsa-sha256; cv=none; b=Lt96Ci7eGehHeufSLOGHMVfJTO1Vy4N4lmyLrwwRKz7Qp61M/Syk5jkzoCaSe/xOaDZZ5w Sp455hNWo0k0PXPpoNe8rbzJmVhQt/evstWKm1THPCopjTt2qkFCQQvyXbEixt7ZO82Ri0 LSKV1kNnNUy142vWrknXzrCqwo07bTE= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="ZA6/VzQk"; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf06.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.215.177 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1723429827; 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=khhjXbPZMt7GBHDJwocHjgjVouiC9Zx1GPF6Kpp8feU=; b=Z6NtKY3vCGF03/RFaJ6QXfsqd2IcYfgQgKCJ/m0pT/lCvDSOCfkqo6XPf9RB1c0NyZ48CP S3DovmoByZyZZJynURS/Pkpfm4vunbvLhGCIpSKy/cUVJCMSBI95DQS67wDLNGGCCZMxV+ RGOiYNq8mhx/m7CevUjj/8BYwdoWbHg= Received: by mail-pg1-f177.google.com with SMTP id 41be03b00d2f7-70b2421471aso2305311a12.0 for ; Sun, 11 Aug 2024 19:30:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723429836; x=1724034636; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=khhjXbPZMt7GBHDJwocHjgjVouiC9Zx1GPF6Kpp8feU=; b=ZA6/VzQkXhj5WgMdM8vYnujGAIj59aqsZWf3KtXVZ5/22IKdGjFlBWPfg5b2AdFlim W7t2NXXA4+WgwDk3keQmh4IejKmfKfvmVUUN5S6xnaqgpftjCslyXScp+pWQwIa+V3RM aZQazBGwlPSftmI2Dw33D3igI7qeDuIaB81Qdm0KZy7VHG7j1OWKP/pKDJK3v3/JoWu8 YnZQP73+YHPh2dpC18i5yBabKo66FRCnUnTXcvmOMsLy3H7WClpLoYQj3OzjYk0XyGT7 H/dBwZq6W13p3UaIserq1MG7UBjKDhN9zcoh5iMJfOcJ4fB0ubVLNs9fx5pa+WZVZYEj 03JA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723429836; x=1724034636; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=khhjXbPZMt7GBHDJwocHjgjVouiC9Zx1GPF6Kpp8feU=; b=Qyfmcaw6qlGrwYBERxdBTtjuNGD4KdJULNNJZj6YpURQS5TqUsZgwKPR0avscwnQ9C 3aCbZ9b/eVyLWKxm2Y5uN9JB1ZR8k1XT/Qv2Rbb3RMoZBiMMtUDCdtqwCJyEMk0y65wS T0MjvE2epx36iuL5y4st0ubtU9vCLxYZSDWgNlBHYQpS1cU1kZhnkKOaKGsq0xncaBa0 m2PFojCmZlUure/EN+EkcMVD6t+Z2AzZl41B2a2b2JnyUm5JE432IBKsQHj3LaHNpfdv ZWaMps5lkNZiTOqkT1zp+OsvBE7gm3YFPNkN+pGSrU4Y8EnirvVOigZ0pF/br5+svA6P UhBg== X-Forwarded-Encrypted: i=1; AJvYcCWlPa55DqhtR+X5OtXHTDPnVoMFAAczR1i8TKQ77SdYQYWBSiMainuGCtxJX0eD2sZSdrEorOwKKUTbP2ufEEtYtTM= X-Gm-Message-State: AOJu0YyIsrCc7Dkt6nW83O1GUgpRVbrT86rfwzzTUeP5c8cFLE1LGcnZ gtadv2Thg/oSc/Vp41HF8hRzneDzQ6oCRJegPbmzPiMDgCuJuaM4 X-Google-Smtp-Source: AGHT+IHZ0gPVVBMqlR8AM+LuQyCNFAmVaej1h0WqXQaGCWBBgBWQLJgV4NwTDuyMDp98EgrIiW/RCQ== X-Received: by 2002:a17:902:ce83:b0:1fb:3474:9500 with SMTP id d9443c01a7336-200ae56b418mr67022635ad.27.1723429836312; Sun, 11 Aug 2024 19:30:36 -0700 (PDT) Received: from localhost.localdomain ([39.144.39.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-200bb9fed69sm27884765ad.188.2024.08.11.19.30.04 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 11 Aug 2024 19:30:35 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, 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, Yafang Shao , Alejandro Colomar , Alexander Viro , Christian Brauner , Jan Kara , Kees Cook , Matus Jokay , "Serge E. Hallyn" Subject: [PATCH v6 1/9] Get rid of __get_task_comm() Date: Mon, 12 Aug 2024 10:29:25 +0800 Message-Id: <20240812022933.69850-2-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240812022933.69850-1-laoar.shao@gmail.com> References: <20240812022933.69850-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Queue-Id: DB0C8180003 X-Rspamd-Server: rspam01 X-Stat-Signature: yrug9te6dh4nhuok67a1droinjsiu1g4 X-HE-Tag: 1723429837-381223 X-HE-Meta: U2FsdGVkX193RdqFsWbTj4EmT5I5dRjJz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/StCuJxph2p+UkhgsmgKP1CEyxW/183RtWUUoX70ph1JysdJJuB8hFf4bz29rTJlUpYMYdxp+EZOQqNkTZlTZpkx4eoMGOwyk2ZoGY0xmKwIs8cbSGQzBswA+lewf6UkBfSx2Vm4/idsX0PxnGZiiM+TMyliF10baE882wd5x1a/WTwtmToSoeMrq5WuqX1feDG3z6xTaUAIiTT7NLFMEy7ceYwikatWoVq8vnRAy0keMhDFTuXWxvP6yQInoTwjqN1qKnpFd7f8+Y1uZB4TkQO3h3wCG+g49qWRL9eaOd1HI0T157AzcTXmHnmB0x17wqT4Nj7tqMO9OpdZT6seVFaVTM1kE5cOtUjNIlYUJNEsNJP3P59MhG6dQT+a7IXyerzdShwGHDgFMYF5276M7yKMZk0ALp023IvT1NoDdUdJ6yGJRZ5PRqjESB2CrpcS9FWNtNQTF7QYpqvOvNySBq3cADNYDhQJhM5WXz8JEO97z8rQWFiXStNfeK3+ezX8y4EK0RQi9980yI84VOFI4l4QQKrbzENeJoGyVmIle956cMsopvMyxLgQJbQb6OumQZOHOarXqbCWIaMF+PHb60uLiUFfU1YHO2p+poUzbpBK3N9mQtC0Z2YURNWF/9WbRn7M9VPC+I0j/5MPsC3cYLxGppdRb6PysrFwVOfz8I8a3ouNM6Tg2S1m2wsuggFVk5Tf6zTuUQ7tjzCVG1OxmAzY/x0u9cj 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: 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 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 Link: https://lore.kernel.org/all/2jxak5v6dfxlpbxhpm3ey7oup4g2lnr3ueurfbosf5wdo65dk4@srb3hsk72zwq Signed-off-by: Yafang Shao Cc: Alexander Viro Cc: Christian Brauner Cc: Jan Kara Cc: Eric Biederman Cc: Kees Cook Cc: Alexei Starovoitov Cc: Matus Jokay Cc: Alejandro Colomar Cc: "Serge E. Hallyn" --- fs/exec.c | 10 ---------- fs/proc/array.c | 2 +- include/linux/sched.h | 31 +++++++++++++++++++++++++------ kernel/kthread.c | 2 +- 4 files changed, 27 insertions(+), 18 deletions(-) diff --git a/fs/exec.c b/fs/exec.c index a47d0e4c54f6..2e468ddd203a 100644 --- a/fs/exec.c +++ b/fs/exec.c @@ -1264,16 +1264,6 @@ static int unshare_sighand(struct task_struct *me) return 0; } -char *__get_task_comm(char *buf, size_t buf_size, struct task_struct *tsk) -{ - task_lock(tsk); - /* Always NUL terminated and zero-padded */ - strscpy_pad(buf, tsk->comm, buf_size); - task_unlock(tsk); - return buf; -} -EXPORT_SYMBOL_GPL(__get_task_comm); - /* * These functions flushes out all traces of the currently running executable * so that a new one can be started diff --git a/fs/proc/array.c b/fs/proc/array.c index 34a47fb0c57f..55ed3510d2bb 100644 --- a/fs/proc/array.c +++ b/fs/proc/array.c @@ -109,7 +109,7 @@ void proc_task_name(struct seq_file *m, struct task_struct *p, bool escape) else if (p->flags & PF_KTHREAD) get_kthread_comm(tcomm, sizeof(tcomm), p); else - __get_task_comm(tcomm, sizeof(tcomm), p); + get_task_comm(tcomm, p); if (escape) seq_escape_str(m, tcomm, ESCAPE_SPACE | ESCAPE_SPECIAL, "\n\\"); diff --git a/include/linux/sched.h b/include/linux/sched.h index 33dd8d9d2b85..e0e26edbda61 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -1096,9 +1096,11 @@ struct task_struct { /* * executable name, excluding path. * - * - normally initialized setup_new_exec() - * - access it with [gs]et_task_comm() - * - lock it with task_lock() + * - normally initialized begin_new_exec() + * - set it with set_task_comm() + * - strscpy_pad() to ensure it is always NUL-terminated + * - task_lock() to ensure the operation is atomic and the name is + * fully updated. */ char comm[TASK_COMM_LEN]; @@ -1912,10 +1914,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); +/* + * - Why not use task_lock()? + * User space can randomly change their names anyway, so locking for readers + * doesn't make sense. For writers, locking is probably necessary, as a race + * condition could lead to long-term mixed results. + * The strscpy_pad() in __set_task_comm() can ensure that the task comm is + * always NUL-terminated. Therefore the race condition between reader and + * writer is not an issue. + * + * - Why not use strscpy_pad()? + * While strscpy_pad() prevents writing garbage past the NUL terminator, which + * is useful when using the task name as a key in a hash map, most use cases + * don't require this. Zero-padding might confuse users if it’s unnecessary, + * and not zeroing might even make it easier to expose bugs. If you need a + * zero-padded task name, please handle that explicitly at the call site. + * + * - 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)); \ + 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; }