From patchwork Sat Aug 17 02:56:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13766923 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 82F24C3DA4A for ; Sat, 17 Aug 2024 02:57:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EE2956B03EE; Fri, 16 Aug 2024 22:57:03 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E94BB8D0066; Fri, 16 Aug 2024 22:57:03 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D5AC76B03F0; Fri, 16 Aug 2024 22:57:03 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id BBDDF6B03EE for ; Fri, 16 Aug 2024 22:57:03 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 7511816043E for ; Sat, 17 Aug 2024 02:57:03 +0000 (UTC) X-FDA: 82460225526.26.2BF54E4 Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) by imf08.hostedemail.com (Postfix) with ESMTP id 82CFD160004 for ; Sat, 17 Aug 2024 02:56:59 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=cD2jT3EQ; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf08.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.174 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723863405; a=rsa-sha256; cv=none; b=Y3Oco5/bfskU2Wiv6TxkzalVrrOOaZzJ5LIAx/t8PxgBhb716p6usm5cH6mqVORqp79X9L W/WhrF4v6vffyeb3nHLb5nn2XIIbnzAwB9YTCDpYKBljZjJmp7yYShg/Kd5LLDflTOWvmd jM1bPSntVQiCt3ri0jTX4ZxDuqcgsiE= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=cD2jT3EQ; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf08.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.174 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=1723863405; 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=iSXeW14jggb39wKxD5hCG2hdCk95xYh67NieDdymzHU=; b=cm6OpDa5GHG+1pulZ8tJh35yaKGc/W+4DB+xu/g34xpOyg55OH33kfBplfcfGn5GtHN2bl 5VbgqN/XIzhJYKyK5ZZR7SrgMtrojRfNcNKPiHLOXP87rOpfZ8PpLLa6DwvPj110rbAFpK P96rfqlHCJRvbHIiLy53DAn4bJzAC5Q= Received: by mail-pl1-f174.google.com with SMTP id d9443c01a7336-1fd9e6189d5so23703705ad.3 for ; Fri, 16 Aug 2024 19:56:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723863418; x=1724468218; 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=iSXeW14jggb39wKxD5hCG2hdCk95xYh67NieDdymzHU=; b=cD2jT3EQ5CYFQ3ylkdE+LktEpxw4V9x5kfJZzb0wXXtP7joGkPoxd8hWJT8Z/Ijnv8 WCyyJEjAwmicWgRlv0svv7DbuImf9fGU8OY6p0fqqB3p0RbzjhABnuy4vnAQ9L9bHn9L 8gaL09WOjOGLcNxKiFOp/U1LqwX5p8yb0MbypV7xtT9mtWI3XlDny36fuJNB65kK3fKB DjjPUNLg1Iyppn2+RCFyxnn0Fe0Noj1Tp3qXNowKChPpvr2tX4royTGmTtrIP6v19JO5 aHI76AlrHWWvDq0lMxsiZaz2DEdbNSO8nKpeG6qeNDLeTFBlj7nTpFcebE5ZLWZtO0Eq 0jSQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723863418; x=1724468218; 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=iSXeW14jggb39wKxD5hCG2hdCk95xYh67NieDdymzHU=; b=wnYV9sHicBJmgsFJhEQJAdEJ6Ywqhi4moHA/lMD9l3ISlSE55GoU+ky+6SHfPaeTsc TB7t3TEbU4RBLk11p0EhwztQktaFKJkcTNIya5YP2QcQ7ffg6oU1esKiRqLzpwOMvqbh jQNDBwAY23PWGRd0NtP7IncTUZYyfeaVSq132VncXPVk8hPTC5KGS68fONE/3JE3z2ak LXa7XiYTLuaExHZC/3EWlLykX22St1qkXaJoLfKfVKpivxXO0QfzSRDeCM9rEkRko4bg ek4UbiMdgOKhrTouRpvl2WIV7SmZMZtktwG5Fkwtah04k/VUZpY14hnnwZ24bUBW9hqE BGpA== X-Forwarded-Encrypted: i=1; AJvYcCW1Et/AVw6jH+IGJnWgztDrMz/TCNRqNmhVdVJfkwLkGf8eZfOXZvXLlOgYOkIs6EurkvlgR2XdA9tGdy6PllWxKdI= X-Gm-Message-State: AOJu0Yys6orVFOhskQEJo4Xo4+glU8Y6WFGvbsUEcMCcpbrQUF6MD35d xUtBoCTSDyaP+Kof0lYg9bgUd/wngvgvPBcgghcBB04Ye/Q3vOdm X-Google-Smtp-Source: AGHT+IFVf7h2xaEBLW2CupHL7L7C8Pe40CvC/Mw6eEkJQnE3Swhf1+Ors/i9DDP0sM5dBvy2264FSw== X-Received: by 2002:a17:902:f54b:b0:202:37:f721 with SMTP id d9443c01a7336-20203f454fbmr48859335ad.57.1723863418122; Fri, 16 Aug 2024 19:56:58 -0700 (PDT) Received: from localhost.localdomain ([183.193.177.10]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-201f031c5e1sm31801785ad.94.2024.08.16.19.56.47 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 16 Aug 2024 19:56:57 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, alx@kernel.org, justinstitt@google.com, 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 , Alexander Viro , Christian Brauner , Jan Kara , Kees Cook , Matus Jokay , "Serge E. Hallyn" Subject: [PATCH v7 1/8] Get rid of __get_task_comm() Date: Sat, 17 Aug 2024 10:56:17 +0800 Message-Id: <20240817025624.13157-2-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240817025624.13157-1-laoar.shao@gmail.com> References: <20240817025624.13157-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Queue-Id: 82CFD160004 X-Rspamd-Server: rspam01 X-Stat-Signature: yaewaacfniezuhongkhxo4utn9ka1yae X-HE-Tag: 1723863419-823932 X-HE-Meta: U2FsdGVkX1/SAk61LLME4wrZ3N0rs34y9BsWI48zQBp26GJ4pmUjG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MEMOla87kjsdHiW3j3nUht7avT84B8WTVAO3f2VH15ZvgSSt054B4yl93gNQYQhT81fblXT9niQ2H+McYdCxjEQzwoCFQZiOI6is/g1DkN94B9VSlQcgFa7lsfWMf7hVodokAbHMMoa4qhphii4frVt2aNgddzuMA8WodRmcKxc1M/oE6taLBxUr8bhgXlChDERtMKM9oijAXP3bTe1/bE/iwEt4NjWl1LxVKN5BABeTUjGN5NLsa96W58KiTzvPyYCIZiX8zyIf3hoeoxHsVQQKuccy2R2v6S8RqnI0Qjxb+MEjgUiLLDOFcIpY11TQdc3hdt5K+8ZKl95nnxuKtIT9tk9ulJNgrN0Odwk6vuvuMW+Ps4rF6a7Mt1DHkUTWrx+GWmQYbnGzE1rHIalDIXZyhWuybP/igjA8T7XkbzHdLqzcOpAIfJeeFp8AwNthHM4w+TnT0Uo48AjpuH3T3LC4FPtUnv40xtedrdhTj2mMSDGlasmoFNNUs8mfLBhuCwK93DEEPn6vWWDUV+or0nO5jKXLrh+9H3nQ80TwN8JafKTy+v1nw9BO98tGcF5+zs4V9Z4lAMcZpZ8qAXxOhWTL3o6EL8BGNMq8WftP3iLW5X+xd67G5n/08itOaPyyFMZMytm+muTBdmO5YnMmb8PHFIMp62fKErimkcHSq+V15gbVaEyk8I7ChqRX0LzeMJj0UborVBAwg6KQ9u7 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 | 32 ++++++++++++++++++++++++++------ kernel/kthread.c | 2 +- 4 files changed, 28 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..5f1c8a58bb76 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -1096,9 +1096,12 @@ 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 and + * zero-padded + * - task_lock() to ensure the operation is atomic and the name is + * fully updated. */ char comm[TASK_COMM_LEN]; @@ -1912,10 +1915,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 and zero-padded. 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; }