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; } From patchwork Sat Aug 17 02:56:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13766924 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 87C91C3DA4A for ; Sat, 17 Aug 2024 02:57:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2600C6B03F0; Fri, 16 Aug 2024 22:57:12 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 210686B03F1; Fri, 16 Aug 2024 22:57:12 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0D7A16B03F2; Fri, 16 Aug 2024 22:57:12 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id E38A76B03F0 for ; Fri, 16 Aug 2024 22:57:11 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 70E96A0485 for ; Sat, 17 Aug 2024 02:57:11 +0000 (UTC) X-FDA: 82460225862.08.6030F39 Received: from mail-pg1-f170.google.com (mail-pg1-f170.google.com [209.85.215.170]) by imf19.hostedemail.com (Postfix) with ESMTP id 7F3B61A0008 for ; Sat, 17 Aug 2024 02:57:08 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=GXWsv8eX; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf19.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.215.170 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723863414; a=rsa-sha256; cv=none; b=8SXLbx6HDhFw0iJ3h+8fA+msbxhvgTmANTC6DCXrjb94X/8WrokO6jdxKM+AIlSijdn6F8 S7h1f5EHaDTHin8Gl/6JtRyEvTnuMV/VepPmGFwxdiE2i+duzMaCc9wKblqFDgMpTXsfwD Goso851/QOxoE8iWCC+T0RimZkBfUlY= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=GXWsv8eX; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf19.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.215.170 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=1723863414; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=+RcIZVYUDMrvydSLTObUn9UWjAdkDM4CrmA64YTz3G4=; b=5BtxbE2g5fb3/O5dUsU58E0P/t/4qpH1zRkpv67tjidp8he1XXecMRDEBwFo74U9VcrS+h HkvOR1BleH7PBE0O7EBdAiQn3QX8+gSupMhNYriurf/p/t0UOH9yq7wHtnoktr8gPgX1Xc W0IpqIBm1kgdFUHjyYRwit2S6aAUDTQ= Received: by mail-pg1-f170.google.com with SMTP id 41be03b00d2f7-7a0e8b76813so1964985a12.3 for ; Fri, 16 Aug 2024 19:57:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723863427; x=1724468227; 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=+RcIZVYUDMrvydSLTObUn9UWjAdkDM4CrmA64YTz3G4=; b=GXWsv8eXr5bZE06RGy7XZqAwqEDnBollwj8q6ud+aGrzg81GZRTzqL8T+DSUHEtzjM 3qYjwe3w4pDacgaYD/svu108iECZT3DZ2p9qiZbrXgvLJAZ31oCqn1zH8x9fsv8bYUeH Lqry2qq6446nV8GIRRBOebddOSn6hVz2Xik9UIcTKRBMNAnm+37lmc/Ih0ofhVt5L7yh DLmKCz6Ux/RD/NVsyhVv/BAaa+WLANK8z0e05RnZ60ExP6b1rBsVWayfGZMikpzKMehj o1pw9bdEruuvfeBwUZ6oynI6mqGkkKSotHaJ1azSceVQBsO/8nYMmfL4qAmCYpYxGzwj 1qaA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723863427; x=1724468227; 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=+RcIZVYUDMrvydSLTObUn9UWjAdkDM4CrmA64YTz3G4=; b=cV8m6nKYEjQs3bMb+jaiWx4Pa7W5hTz6D6Ks3f88cdOxv7suM92iNB5IbRfjA0a6jF pokdaisFGGx4f5STH8OTEK48MLSHTHDGeZ2LscxARLOHUOjfaSgixfwBE91zjJBBXuSJ WN0zjqs1tmnGPd3eq0/gdYLMtBZ/AzEAZqB3KCjoUjuL5Jrd6oV10k8mlzhY5cmEFl16 DsWjOROnaFR+sLKDQ1IJbzZod/Nq90l4ZnRLoER35X6UV/tnEtxlsopCryJXvoLuq4Hj q0uMq/zGhVlkxKWlmr9glyy1ZK9TNckaigKh2/5Qpw5TRDHhUIP/uNzocLi+Q1/2YK0S e4Vw== X-Forwarded-Encrypted: i=1; AJvYcCUSD4+/G4ThLzREtNBswc2q3T0H+qmsy7hmdWn+WKJy24CRSbWEH00W+wXle1F+VOybbUlJ1l2Wnn2QG91VDF8ZdvA= X-Gm-Message-State: AOJu0YzFy95i41Pqj/Dbb1dBDuDyy2Ya11IjBCFwHotqBV0aKBM/KBcX qLhj6wg7TgkAgqdJ18EAn65/Vr5VU0eqSv5HlVnOkZP5vPjDzDBAOalJbq6S+MU= X-Google-Smtp-Source: AGHT+IGloZlxC12N+ArnDbFHNJc7Yrc4w00KFUrCatU21ag0p56SeEdkfwKvEveNvVnkzBGuU5AKpQ== X-Received: by 2002:a17:903:32c9:b0:202:bc3:3e6e with SMTP id d9443c01a7336-2020bc33fc8mr44372525ad.33.1723863427201; Fri, 16 Aug 2024 19:57:07 -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.58 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 16 Aug 2024 19:57:06 -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 , Paul Moore , Eric Paris Subject: [PATCH v7 2/8] auditsc: Replace memcpy() with strscpy() Date: Sat, 17 Aug 2024 10:56:18 +0800 Message-Id: <20240817025624.13157-3-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: 7F3B61A0008 X-Rspamd-Server: rspam01 X-Stat-Signature: msho9ftsje9om5yz6ys11iz7jcsbrdca X-HE-Tag: 1723863428-507341 X-HE-Meta: U2FsdGVkX18sBF1WsLw+Ls4/2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kzRmTfXyt9qu9cpsZJEm8z68ovLO8DA4Dc1vD5lllnOqKwjIjk+bjhbbbpDyihJGcKEYv4FZabBZxb1mUYdDTU4r4m/6sv5MVBzYUwQOHkYxiiqswRCSZ3JMvUlyWCiPGhHHfmnrb8cpMevZwpefGupwk08feVMuMo4Pq2KafwCBR8xq0pWCYqS9OCNz+ZRy3+J0LLBhNuq8RqGJ97dqZF8AD9jqdjpKGWlibHqNqUpw1q/p1boW5jqaCsJbajveDg/ldUu1QuNgCLjIM1Qrmvdu7uUbmHfM3xczl2L1KAo8llggZtktUdGv8MS/VIWyjwX1pUi0ibX5tPVFe86QaLiqP+5PIT/amj+uMxUZtgq/2QsdtrGC9h71e9IdmgSxSsiWuVfHlemxkqbqS120pRuyMks/i/VQEqOonefxE760MC0GrsAgccBtQ964HQnV4UwoDlptZ4+QjhArqNYet+ByUBfvcKSkYIDvHl7/0hb0dAG/8jk/lvdo= 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: Using strscpy() to read the task comm ensures that the name is always NUL-terminated, regardless of the source string. This approach also facilitates future extensions to the task comm. Signed-off-by: Yafang Shao Acked-by: Paul Moore Cc: Eric Paris --- kernel/auditsc.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/kernel/auditsc.c b/kernel/auditsc.c index 6f0d6fb6523f..e4ef5e57dde9 100644 --- a/kernel/auditsc.c +++ b/kernel/auditsc.c @@ -2730,7 +2730,7 @@ void __audit_ptrace(struct task_struct *t) context->target_uid = task_uid(t); context->target_sessionid = audit_get_sessionid(t); security_task_getsecid_obj(t, &context->target_sid); - memcpy(context->target_comm, t->comm, TASK_COMM_LEN); + strscpy(context->target_comm, t->comm); } /** @@ -2757,7 +2757,7 @@ int audit_signal_info_syscall(struct task_struct *t) ctx->target_uid = t_uid; ctx->target_sessionid = audit_get_sessionid(t); security_task_getsecid_obj(t, &ctx->target_sid); - memcpy(ctx->target_comm, t->comm, TASK_COMM_LEN); + strscpy(ctx->target_comm, t->comm); return 0; } @@ -2778,7 +2778,7 @@ int audit_signal_info_syscall(struct task_struct *t) axp->target_uid[axp->pid_count] = t_uid; axp->target_sessionid[axp->pid_count] = audit_get_sessionid(t); security_task_getsecid_obj(t, &axp->target_sid[axp->pid_count]); - memcpy(axp->target_comm[axp->pid_count], t->comm, TASK_COMM_LEN); + strscpy(axp->target_comm[axp->pid_count], t->comm); axp->pid_count++; return 0; From patchwork Sat Aug 17 02:56:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13766925 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 E34D9C3DA4A for ; Sat, 17 Aug 2024 02:57:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7B6E66B03F2; Fri, 16 Aug 2024 22:57:20 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7674F6B03F3; Fri, 16 Aug 2024 22:57:20 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6085B8D0066; Fri, 16 Aug 2024 22:57:20 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 4333F6B03F2 for ; Fri, 16 Aug 2024 22:57:20 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id BCAB9403D0 for ; Sat, 17 Aug 2024 02:57:19 +0000 (UTC) X-FDA: 82460226198.11.0450B85 Received: from mail-pl1-f179.google.com (mail-pl1-f179.google.com [209.85.214.179]) by imf10.hostedemail.com (Postfix) with ESMTP id E8246C0006 for ; Sat, 17 Aug 2024 02:57:17 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=KOcmoXxc; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf10.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.179 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=1723863354; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=mIIMsSPqVp5AusgTbRbdrgF3qEr8ccJBENe3BbQ0UBw=; b=1D6ne9VMX5cmpDH0+dydngY+W8t4VRtSXjQUrwHCKgHasvnIp5tffAx0lCU/UOD2G4gQbg 3gRgdKItg57DPVk6sHoIjISvEe3adRTw39icxmqiJbndqAN8q9ojYy1QGd2O/b6oXCUq7Q zYdsJMaH/HlVKBzrCRMaW2DTDfCzbiw= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723863354; a=rsa-sha256; cv=none; b=a1EBnwtD65/g40Gwvd7MlWaxEK/PzUHNFc/rwf90N5X4D6qOeiQ1SqlQ3j9k5t1vftyS0o stWl/lc7GVI6ReroSZdBp0C/V3Ngdv40d9VlVb9bzMjScP4qkRuHscx/xEQ7uDl9yRSwFp ydH7L76OD7MUenklPQl5DtyWTXXIYi4= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=KOcmoXxc; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf10.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.179 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com Received: by mail-pl1-f179.google.com with SMTP id d9443c01a7336-20202df1c2fso11364495ad.1 for ; Fri, 16 Aug 2024 19:57:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723863437; x=1724468237; 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=mIIMsSPqVp5AusgTbRbdrgF3qEr8ccJBENe3BbQ0UBw=; b=KOcmoXxcrYYweXAgIXiI66w37lOcL57fuOpfdRP/2pxXQmRYpUS3HN9q10FgKDQKJM XhFKG+9UHM+uVZyci+VLynNDwmlQ+tbIj5DEbm2XYShYSCMfaI+h4+kn/foL+eKaL/Sk pV3c4BhQne3Vv8HX8qIe2sA8M76VT5Zh1x7/7ddw4UzWqpbwbYbYklzxE+6eC36Z/mEO i+AB8lOAcrveJWSEfgi3jfW2B1FZq8mcaoGuq5VfwYDlB8Nbhgh/eNNKOoKAIcdbyQTf v9N1hZFJmSTy5YOIYNs7aEha0+LYGc5kOvsFsiGnn8k+Vo3HccVyeeqAGu4HXm4lGBan 1Fsw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723863437; x=1724468237; 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=mIIMsSPqVp5AusgTbRbdrgF3qEr8ccJBENe3BbQ0UBw=; b=q4vP4D6ZiNf2UR/YjZIt1lCu6ZKoRV5GwP7agZqLnXeNLzqZK7KVmF3rwe5IYrwYtC 0KcvR7XYkMnHgTo/ho1c3SOafEq64ToDhnUR4s3JkpAwsQjllYKkDw1O2BnUsKX5fLHC bUYIGAHrobShCIU0FD7UHX1X87SyJRSNOHJc+k73jrAp/GVEGH2tfGX34i2HEmNP3Wps xclcIilNP9SRM+O4CXbLCBpsRl6830LIHFMWQFQYhc2LXIz8eA3IaFNn3D7lbfIebwdo N7D2LVsMlFA+xH8BThVsZI6lwOM0LM7w2QJmxqqZk+obSX+OLfX3hJUz7ABLfCNy18p8 vk9w== X-Forwarded-Encrypted: i=1; AJvYcCWdHPE2eTMGAFcOcp9CnPeBsqjKI8o5TPr0JQlqEigM7es7fOVOdptv1JGa0kA7pkjBasOwiOUMUpbAPPeuT/ZYfkk= X-Gm-Message-State: AOJu0Yy35ZU9R2wWlee3UmC+OTsNSE8G5uuu+5SSsgXrxrK6QmdlBxzR EDb+5uGSw+70MnNPpz/Z0vz51LhH8iwvTFkwXrYQuGQr2NuKfY7i X-Google-Smtp-Source: AGHT+IEzug8bsagNMxwK7nm29ecXIwIfZ9FH+TLEi6IcVkbHtwlg4mlyU3SBLgFEVqycM3+Rn/pQ8Q== X-Received: by 2002:a17:903:2303:b0:1fb:9b91:d7d9 with SMTP id d9443c01a7336-202062963e7mr80764935ad.26.1723863436587; Fri, 16 Aug 2024 19:57:16 -0700 (PDT) Received: from localhost.localdomain ([183.193.177.10]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-201f031c5e1sm31801785ad.94.2024.08.16.19.57.07 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 16 Aug 2024 19:57:16 -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 , Paul Moore , James Morris , "Serge E. Hallyn" , Stephen Smalley , Ondrej Mosnacek Subject: [PATCH v7 3/8] security: Replace memcpy() with get_task_comm() Date: Sat, 17 Aug 2024 10:56:19 +0800 Message-Id: <20240817025624.13157-4-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-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: E8246C0006 X-Stat-Signature: 9a5gxt8df9n9bh5a6hy8zpaya1k5rnpu X-Rspam-User: X-HE-Tag: 1723863437-759515 X-HE-Meta: U2FsdGVkX19Z96Cx0UDWwQmC3DHOk/BxB1uRDYr0QOpdV53j8lgLR/W3SLtanrs1J9ISs1Q12FCJEdi16M+JmBOseqAmPeYWRfWiAwMRNzwdVnHkXxtG7q8O9nzFgepSKWrSpsQeR5HqVICl54DtC1kH6UqMZHrSPWESUBrzcqBb5tQLMwtzQwB4yzNK4V0N5ApdS+dTKUGr9/sUFn+S1ZS7V9bYW2fm2nR/oZb1b5Y+PVpMipBdotAWexs4sUtZpOnFOyXVuca6RGb7Kwe5DwqWITw3o39KEOFt/Z8yyywWc0tYdl1iZY6r+jdlS2+GU3bzHrF1Ihr22dNQfBJJMzD+Hj2n5nVgidUF5JkpMaCQ7LnltcEtU+2HnNoFesWKRJmwShrw6z44TdZSPEtfAiXqIW1m2ajpVSiwMSKMsHy2rOA2PW/n3EzVZPHF1f6PXtzEeaOMQNIW+LGAXOcGd7aZvf/5zVWrkOkZekHAnXBlUA/g1G/OoHtaBjMkr6CGL70bbXAXNg3vjmDEGTsodRcYhDRbUXU0w/m7UHaWzGYrL+ige7qqnQwIL9WQVtqQNV6DVXbZSK6PHnIk28Be6lFmcXrk0GQbCVmIcljAnXEMLQ1GMBsNGK/e8QgZV9o8od6ymVS+P4OA3KyVoMLLqyLTXZYVgcF9+/UUierMovh1rgBHYDCtKBZec4klZV0+gEfke/cfHIcjDyQmbh0+wmmn9q/ZH327Znz7xEAKSiF2oeLVy1jYADfAAOV0y1GHnk3ma5s0mx+pOcR+nAPPJH5uFE7e3RYxJ1CXuEDKJQD/3R9Sjw3efcezEMhkSs9LbAxqYngSVO0t3qTzGmfgEh40Wvadabz343BQPMXHeORXfwaSTNI2oVwObfAS4zBm9Ypz3uImr6kPgMKVHrhhTm4jGvCZ+IB/rzUlOWDEJoP7XXDy3Ab/V7x4vqR9rqzFAtbW9ND35sGAVZArr3f exMvldnH /LwFuwYY4+H2jX087zaJ+JMWto7kLhzKruqq9yfXZWm3X9CeOPhXeq5luvFmQ//f/4fsdSFrX/rQlBiOwb/658uz5ICA8gIdVZ2E3h3rvZUMkn7FwgpwjsoR9C9TknpqxZp0zZyaHoMjVp8SU5uq1IY5HmBOKSIah29PYdfaPrrQ5F1ArPlx5w8Aa53jlrq4MS9nfpicAam/7plVcSXat1rVGseBA9bdE4K2MHJTAh/mdNpKeaUE+oHmwWrGY7Tfr7jYfKAHRbvmkzkN9HnWoXV+XBT4vvA7Lh29eGRmcxAnMR7bOs3o0wh9ljSn2JAySrYb2HeYOuzt9rIcJHcpecCZu4SFOu/GH6+JI15ynIH8g7nf6AYZoMFGauj6gAx1gYDLWhMfqrlyokoaMV0cLmqW4MkHU6Wn+3FR8OGVX3iDBGTME7INpw1AgNRJTugYJBh4BC8Z/za47thlkuFCyI9cZIGovfo5bT8R3KTAUjUGcaPJheOx4xtnqaSK/2O7PNGgVZ6tOvVk8kCyNqrj533BWsqCR+eaIRTSdv82asx2DkbXDabCJl4cjBvCJ9/+ddNI7IwhKgW3LdoaKWBjTwr813yWP9O3CDMljd4aST4KvgPcAOOlUrMOIcDNGwyBxu1I6DFNGF3P4ke+ovjkJgPBTMHCIZtpNjoXedrzHhHMArlzjLXh3lPuRG0Xo2/uC8/eqF43NZnzGHGJeBgr0EX5Sfbr9EkbNoGdIMOIY6IdIyTI= 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: Quoted from Linus [0]: selinux never wanted a lock, and never wanted any kind of *consistent* result, it just wanted a *stable* result. Using get_task_comm() to read the task comm ensures that the name is always NUL-terminated, regardless of the source string. This approach also facilitates future extensions to the task comm. Signed-off-by: Yafang Shao LINK: https://lore.kernel.org/all/CAHk-=wivfrF0_zvf+oj6==Sh=-npJooP8chLPEfaFV0oNYTTBA@mail.gmail.com/ [0] Acked-by: Paul Moore Cc: James Morris Cc: "Serge E. Hallyn" Cc: Stephen Smalley Cc: Ondrej Mosnacek --- security/lsm_audit.c | 4 ++-- security/selinux/selinuxfs.c | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/security/lsm_audit.c b/security/lsm_audit.c index 849e832719e2..9a8352972086 100644 --- a/security/lsm_audit.c +++ b/security/lsm_audit.c @@ -207,7 +207,7 @@ static void dump_common_audit_data(struct audit_buffer *ab, BUILD_BUG_ON(sizeof(a->u) > sizeof(void *)*2); audit_log_format(ab, " pid=%d comm=", task_tgid_nr(current)); - audit_log_untrustedstring(ab, memcpy(comm, current->comm, sizeof(comm))); + audit_log_untrustedstring(ab, get_task_comm(comm, current)); switch (a->type) { case LSM_AUDIT_DATA_NONE: @@ -302,7 +302,7 @@ static void dump_common_audit_data(struct audit_buffer *ab, char comm[sizeof(tsk->comm)]; audit_log_format(ab, " opid=%d ocomm=", pid); audit_log_untrustedstring(ab, - memcpy(comm, tsk->comm, sizeof(comm))); + get_task_comm(comm, tsk)); } } break; diff --git a/security/selinux/selinuxfs.c b/security/selinux/selinuxfs.c index e172f182b65c..c9b05be27ddb 100644 --- a/security/selinux/selinuxfs.c +++ b/security/selinux/selinuxfs.c @@ -708,7 +708,7 @@ static ssize_t sel_write_checkreqprot(struct file *file, const char __user *buf, if (new_value) { char comm[sizeof(current->comm)]; - memcpy(comm, current->comm, sizeof(comm)); + strscpy(comm, current->comm); pr_err("SELinux: %s (%d) set checkreqprot to 1. This is no longer supported.\n", comm, current->pid); } From patchwork Sat Aug 17 02:56:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13766926 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 82689C531DC for ; Sat, 17 Aug 2024 02:57:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 19DA76B03F4; Fri, 16 Aug 2024 22:57:28 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 14D736B03F5; Fri, 16 Aug 2024 22:57:28 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 014766B03F6; Fri, 16 Aug 2024 22:57:27 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id D0AB26B03F4 for ; Fri, 16 Aug 2024 22:57:27 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 8CD6DA6639 for ; Sat, 17 Aug 2024 02:57:27 +0000 (UTC) X-FDA: 82460226534.24.33CADF1 Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182]) by imf06.hostedemail.com (Postfix) with ESMTP id BBC49180003 for ; Sat, 17 Aug 2024 02:57:25 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=mGzVKWrh; spf=pass (imf06.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.182 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1723863372; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=QQdkC9XISn7BWPlTE5/iE7aUj/L2jeLonKoaBsbNpS0=; b=yafMfwb+HSoYZwq9yXC5wS/U2NNWLBgbopuYYITBNcRZPiC/6/ZgCn76IeBSKXXo9TnT8+ 2ka1CormdYooGxJ1QDTKVMSbQHUbBhdozOJgCHk+09ZATacfJIoD66bBOd9JY1XfDQ3Urx mfhNbpA16T8+zyLPeSnnmnfGYXWmimk= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723863372; a=rsa-sha256; cv=none; b=ovxJj9O/uqu/OYfkIuU/fvPoHnll4cLKelz1bN2hcGwCAIuhVv+AuBwkTIQxI3IdayWfOm S6LXJ8BjfIkn5msWjvdKzPQyEra7iGcAiCplKlnB+l3YB60S7uR+j/XgaiyOOd+chWuRTF U9Q/SJvKk5oZ2eWj8N9qEdrCUKr9RfQ= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=mGzVKWrh; spf=pass (imf06.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.182 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pl1-f182.google.com with SMTP id d9443c01a7336-20219a0fe4dso3462085ad.2 for ; Fri, 16 Aug 2024 19:57:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723863444; x=1724468244; 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=QQdkC9XISn7BWPlTE5/iE7aUj/L2jeLonKoaBsbNpS0=; b=mGzVKWrhOgAMM/2qNLfm3X29yzh82kTQOGmQBjpS7U287pGqeBggOdCDp+AeXhdKRN e8eieaK4ICbK8auSskBlkV3aqouG9C26WjLRTiU0fL8Rd++hUgu360SLeHFGLv/wJyDJ o0iM6RS9CI/BaMMpRkRU+g6+lfDG2fwqMc4XfYByUXy4o92iTtMn6UcVR3kDTMEUYiDK ynBvGon7pFzq9ekhlsbMrfVFPuCBt7hCw07gFygqXuNBfsNY4lYH4SJVjPqn1jGFBBd1 2B+P3OJAzfC1aOv/HsdLcxpQb78uA0lNUlJO5v8oL/7Vk9XtMPc0ePc/4HrqNQqxutY3 rElA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723863444; x=1724468244; 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=QQdkC9XISn7BWPlTE5/iE7aUj/L2jeLonKoaBsbNpS0=; b=RMR7fpFsbNRmE3IsXYyP8gAlZyiUK/aQ2cxajxUeHs/WD9xJHaNs49EPB9vMC4hjpv G/DEAcCYVRl1hdzDB0HQrWtneZRi/ckNZANAKR2QZp4oVGOMfsQwWABCln9tB6a1LO1m g8rzQxr0vUe3CJ3HV9x+VrCvXUIhwsUnwei5ulAenR9XbNOc35B1vWTMnYZPi9BJrkQo 6BM6jKSwt/WiksoWaMKo4ruQTTg8LLyHYnpj9AP4Jc8KRmuT9QaGSCc7btjJAz50wG3f AEHrA8V0bL8Q+kXMlfy2nEwMz30oKN/hsblTWnC0Brq2An2dl7S+nefUlliV1iDMUvuE 1dlQ== X-Forwarded-Encrypted: i=1; AJvYcCUlvjcmuGptqgAyTmKs03PDXPws4IyIse5A2HCTguEnad5oc3t+pdY8+NzlHjfJ6pKJf9af00Dhng==@kvack.org X-Gm-Message-State: AOJu0YzLAmwCB6qvLJ+fnWSdAEDTtGBh/IBx4uwshOWesMdYwSfIAPc5 +jBrnG7Kxth3hmLz9U83UtoSr9Hus0zbGg5RUED9zFjIAo6chH/D X-Google-Smtp-Source: AGHT+IGimEn6HMsvkqgHYM+kruqPW+JJG9kJGgyigEFWYmxee2uabObu84uvkyN6MQRp9Qx9ywJBnQ== X-Received: by 2002:a17:903:2281:b0:201:febc:4366 with SMTP id d9443c01a7336-2021969a77dmr17762915ad.55.1723863444544; Fri, 16 Aug 2024 19:57:24 -0700 (PDT) Received: from localhost.localdomain ([183.193.177.10]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-201f031c5e1sm31801785ad.94.2024.08.16.19.57.17 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 16 Aug 2024 19:57:24 -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 , Quentin Monnet Subject: [PATCH v7 4/8] bpftool: Ensure task comm is always NUL-terminated Date: Sat, 17 Aug 2024 10:56:20 +0800 Message-Id: <20240817025624.13157-5-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-Rspamd-Queue-Id: BBC49180003 X-Stat-Signature: cae6uu9z1oc3gzxnesexnzeanmqp6196 X-Rspamd-Server: rspam09 X-Rspam-User: X-HE-Tag: 1723863445-756896 X-HE-Meta: U2FsdGVkX19We/oru6K0i7s5+TkVO602O4noBlgDC4Tbvtwr2yhXr4Msasaxl8YackcsFNmDC8JVsoDSq0t/ojMEItFo2u/yf0S5im5M3a9yF71TFIWvVfkmSzsv8lhb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uOkshZcwJGBFfG5yTsD/nQ9dp8oGSKc3jvuyQXcGCWmXDbfqOo77xGC6Wq0ft5kCR612YJ+eqgsUl5T2c/H/hr3Li5AIJDK+JmW+HQUvLjl4sYYYmEsgD9Z7d3LltUTZr4qUbE9rc8D6KzJjhQI53XGFjjPrfd8+04nt5Be0DTVpEMNoN1A3qMEzORGkoNkyBjnI267NIs8II8XCH1c3PuL/s2lGUZAPau8v66vj671Oa2WAREjEylhqdsTez/FxtQYe3Y+OLA8dJgMDCB0zIxnLPoSF5DwNrd+a/mEVMh7bhIC/x0CG5N2DYlDSXK6rkBZJ1CEicb0SZ0LBQgjic4vguEA3jem+4MJxSr4Pmu45mwO5IJ6tc4e78o3Gqfu9/K4EVOQ== 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: Let's explicitly ensure the destination string is NUL-terminated. This way, it won't be affected by changes to the source string. Signed-off-by: Yafang Shao Reviewed-by: Quentin Monnet --- tools/bpf/bpftool/pids.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tools/bpf/bpftool/pids.c b/tools/bpf/bpftool/pids.c index 9b898571b49e..23f488cf1740 100644 --- a/tools/bpf/bpftool/pids.c +++ b/tools/bpf/bpftool/pids.c @@ -54,6 +54,7 @@ static void add_ref(struct hashmap *map, struct pid_iter_entry *e) ref = &refs->refs[refs->ref_cnt]; ref->pid = e->pid; memcpy(ref->comm, e->comm, sizeof(ref->comm)); + ref->comm[sizeof(ref->comm) - 1] = '\0'; refs->ref_cnt++; return; @@ -77,6 +78,7 @@ static void add_ref(struct hashmap *map, struct pid_iter_entry *e) ref = &refs->refs[0]; ref->pid = e->pid; memcpy(ref->comm, e->comm, sizeof(ref->comm)); + ref->comm[sizeof(ref->comm) - 1] = '\0'; refs->ref_cnt = 1; refs->has_bpf_cookie = e->has_bpf_cookie; refs->bpf_cookie = e->bpf_cookie; From patchwork Sat Aug 17 02:56:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13766927 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 71E58C3DA4A for ; Sat, 17 Aug 2024 02:57:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0D95E6B03F6; Fri, 16 Aug 2024 22:57:36 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 08A4D8D0066; Fri, 16 Aug 2024 22:57:36 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E94E26B03F8; Fri, 16 Aug 2024 22:57:35 -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 C9F896B03F6 for ; Fri, 16 Aug 2024 22:57:35 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 7BDCD120432 for ; Sat, 17 Aug 2024 02:57:35 +0000 (UTC) X-FDA: 82460226870.23.A9D4DB9 Received: from mail-pl1-f171.google.com (mail-pl1-f171.google.com [209.85.214.171]) by imf21.hostedemail.com (Postfix) with ESMTP id A1D191C0004 for ; Sat, 17 Aug 2024 02:57:33 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=WWTw9p+5; spf=pass (imf21.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.171 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1723863394; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Dg8K6fIsZQLFiS7Xpg/1YZlm81yjN4oHJWyrWmInMGw=; b=PmrbQ+MjXOWujpoVIwLYGdlLLJ/rKgUAJtZfov4SlcJyr91WkDJY7b+JTfZFLA9kiyR5GY xsuHBaVJe7LTsnwtj+y74eG9kthUuQB7HUPUz7gV5wFInXeEM3wXGZp7v8TsQSWWgES1MH GS8fWZ/Oig1oTCyYk0es86bxz+07iAs= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=WWTw9p+5; spf=pass (imf21.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.171 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723863394; a=rsa-sha256; cv=none; b=0Zo4jGedGlAsQqq65m1zFX3Ifif3kxmd7MhHgmRaKaugLKeFv+wUJt60Uo8gBrJDoDQ0tR 1XrXynKtfYkK6j/AizRs/OdPpruCZV+LEE4hbClbkqUHSnjiGwLxGhUhkUKsbM9T5Ot4Ad 1QBz0dZ1rnFXJlp/AlEcBBIPl4GKLew= Received: by mail-pl1-f171.google.com with SMTP id d9443c01a7336-20203988f37so14318355ad.1 for ; Fri, 16 Aug 2024 19:57:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723863452; x=1724468252; 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=Dg8K6fIsZQLFiS7Xpg/1YZlm81yjN4oHJWyrWmInMGw=; b=WWTw9p+5fAv7UQJ1udYvkcpF91AldwNbGmPGcjHN9vLghr/F12kPJ4cPXXksE58FS5 ZY8c9XnK0sv3pEeum62Py3Ex1ztTaiL0PgqTQQ46vaV1/5/VC73aVG6rr+Jv5uifkejr 3ussERP5H2wIxs9GC2l1z3/BTY7CkF84gtzkZaHjh+xVkPatX+3zyyTOzYmZvKU4iEHo hgRy2YnUACOoCfTUyPK0ZVXAwp2HcSMYoVLJVX8iMwGOGhhom2kMvQhv7jaExzb23iz+ pnq2KLSQ+L4p1yngoH3r0DM3f0tq71gnasCsL9TR93NT6eVBvGQB7WT8QZA32ZyVmz9M LNIA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723863452; x=1724468252; 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=Dg8K6fIsZQLFiS7Xpg/1YZlm81yjN4oHJWyrWmInMGw=; b=XvrFP4/JpV3bTjl1I336Q8SNiooi2fUt+HMMpCOKfJyEZCU6OdU0se+wgmyV1tujXr iCrxoSgWQDHmT1OM49Vng4G9xS3TPMQhc8xmMtYmfSra1vDKJFgYLOv6S31kntvlmQZt R60mmppqACG4Lsguapt49Qy1gyP06Ni7wveS8oW/HOtumIhm50MZD1paI2DvjN+Lp4Jg TKoSA0aIsCoXvbntVPOTSapzavRaVKhkJPKImjy4NswFyy1e9ktivu5MFpfX/43gqpGs 5/4GGAf1bC946XRcqXPJ6g5F4x2/MjW+8PI3jJF7gQ+HJN9uaGiPdXSy0nUZtDIn9LrU iNtw== X-Forwarded-Encrypted: i=1; AJvYcCUnvl8JocnVNguaJiCEyeguwwfq4IpibZs0cPpY2LiP/KYPwiJRrLTQYRPSPHACwas3N2iClY+peg==@kvack.org X-Gm-Message-State: AOJu0Yw1ZhhhY2DOi9L88DJNBPM1V1coW3XTdPSJTwtK/Qn9Q2kdUzum ie6qy2JiZe457C99MuhC32QaAC5jroNLdHCJclY/0OekBWirPoEC X-Google-Smtp-Source: AGHT+IF3ZN+4FrJiR5tCQknEVTwkbUbAx80SeY+AiIkjp+qgoruwIjB5dCv+k+zHBrTId5++7+R3wA== X-Received: by 2002:a17:902:cccc:b0:1fb:7b01:7980 with SMTP id d9443c01a7336-20203af4193mr61360745ad.0.1723863452450; Fri, 16 Aug 2024 19:57:32 -0700 (PDT) Received: from localhost.localdomain ([183.193.177.10]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-201f031c5e1sm31801785ad.94.2024.08.16.19.57.24 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 16 Aug 2024 19:57:31 -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 Subject: [PATCH v7 5/8] mm/util: Fix possible race condition in kstrdup() Date: Sat, 17 Aug 2024 10:56:21 +0800 Message-Id: <20240817025624.13157-6-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-Stat-Signature: d4k31eqikomss7dzhb13upr1na36rmfa X-Rspam-User: X-Rspamd-Queue-Id: A1D191C0004 X-Rspamd-Server: rspam02 X-HE-Tag: 1723863453-184897 X-HE-Meta: U2FsdGVkX1+qxLr8jDskefJloL5L1RCFo5OdNGnLiMdphtt3OtQfn4x3WMJP4ubg5eXcJeMtPMm5kRzQDgT2qZyR4JG6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p44uXXzn1tl9QD5qttRSdjOPbCvoZO4R6wQ4RuhzBgnoHeqnlgQVeOCUAo7AWH12lh4VB4guVFEWmqgWSGziKI4E7bBA+soF3D2Bfm3Nr3sRktdpT6cMyzgjImr7+oqwt2s+n5UqaU1LrSebStuoVd2LfzVdZJZQzbmFysMExBA4tlSv6RuDtPPuuK32oxRyofz9tOmeDDS3JLBZjXvZ1jwWNtXWXEinKxoOTOB9OiMhedbLoSOLA3affPsBlkeOjU2aiR2/idFGVtECcsgYFXWVs7dL9TqFv15Or6hsfV8K1PZJLXQh1KPfjBmLcYfzcn3P6tgnzFRFw2rYigXHm3VPvFpe0QrA0mpQazGwurXIoTZYT3qmU2THmnuPr7gEn57Q+ecSL0EWWz7wEZqIJv7IEOS9/9KvJlERlQTimzfF/w== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000010, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: In kstrdup(), it is critical to ensure that the dest string is always NUL-terminated. However, potential race condidtion can occur between a writer and a reader. Consider the following scenario involving task->comm: reader writer len = strlen(s) + 1; strlcpy(tsk->comm, buf, sizeof(tsk->comm)); memcpy(buf, s, len); In this case, there is a race condition between the reader and the writer. The reader calculate the length of the string `s` based on the old value of task->comm. However, during the memcpy(), the string `s` might be updated by the writer to a new value of task->comm. If the new task->comm is larger than the old one, the `buf` might not be NUL-terminated. This can lead to undefined behavior and potential security vulnerabilities. Let's fix it by explicitly adding a NUL-terminator. Signed-off-by: Yafang Shao Cc: Andrew Morton --- mm/util.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/mm/util.c b/mm/util.c index 983baf2bd675..4542d8a800d9 100644 --- a/mm/util.c +++ b/mm/util.c @@ -62,8 +62,14 @@ char *kstrdup(const char *s, gfp_t gfp) len = strlen(s) + 1; buf = kmalloc_track_caller(len, gfp); - if (buf) + if (buf) { memcpy(buf, s, len); + /* During memcpy(), the string might be updated to a new value, + * which could be longer than the string when strlen() is + * called. Therefore, we need to add a null termimator. + */ + buf[len - 1] = '\0'; + } return buf; } EXPORT_SYMBOL(kstrdup); From patchwork Sat Aug 17 02:56:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13766928 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 8D872C3DA4A for ; Sat, 17 Aug 2024 02:57:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1EBD26B0257; Fri, 16 Aug 2024 22:57:45 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 14D3E6B025C; Fri, 16 Aug 2024 22:57:45 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0150B6B0258; Fri, 16 Aug 2024 22:57:44 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id D7D4B6B03F8 for ; Fri, 16 Aug 2024 22:57:44 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 8285EA048E for ; Sat, 17 Aug 2024 02:57:44 +0000 (UTC) X-FDA: 82460227248.30.F8A6ECE Received: from mail-pl1-f181.google.com (mail-pl1-f181.google.com [209.85.214.181]) by imf02.hostedemail.com (Postfix) with ESMTP id A82488000B for ; Sat, 17 Aug 2024 02:57:42 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=Pl2NAGtv; spf=pass (imf02.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.181 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1723863448; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=mY4VRCvd9L4i067FYJCdAQbgb2obGf5r9hy8hsyaNQ4=; b=00JnLdgUThysgTa/0WGkmh3kr+2NOke2XQlbI5bjARTgsBhKQjAkoQkuZOQHJWdFuRaevH ANVI2Axq+argF1+DB4g2CMvPmGzZfKEP0QUsl6BhCnn0SSF0JsTjAfzOlRxscMhrpn5Ea6 +El0Hk62sEnUatsoquXYi9BizbQcFS0= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=Pl2NAGtv; spf=pass (imf02.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.181 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723863448; a=rsa-sha256; cv=none; b=54/R8YLQaozWs+rr4qX/Yq8wBVWroiSw4GBXpEU64UsmTAqgVxhFpICdFNfmcjtISUOvPf mHN70EsslEv8JTkLFG4W8Im4Kl128IWihS+ma6brDt+lvKWqsnJup5g9/JHFcamrMX2Uj4 ZcsGAtbQKIDDWjZdsm6SlGA3D7Tqq2Y= Received: by mail-pl1-f181.google.com with SMTP id d9443c01a7336-202146e93f6so9373785ad.3 for ; Fri, 16 Aug 2024 19:57:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723863461; x=1724468261; 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=mY4VRCvd9L4i067FYJCdAQbgb2obGf5r9hy8hsyaNQ4=; b=Pl2NAGtv90CUuzeJl9Nv2LL4z7rKFkfE5xAWNl5yHompDjFjxWbJ1rVhuFOaPgydlD 9Y6axcVd6GWojIWYl65lckOLhAFWhTtgYqwzhbSUvmJnpzpDpfp0SRoSJz4PJx2k+Tq2 UaPS4Ifz8Enc7q6rcuzOBC6wo2UmT9nbdxe8HOT/CPdvNHgjfes1DcK2u3MHzJZyRzRo uRIuFKLawxa3owlxKp5u/PCmpTNkSsWfxZaMJG64WNTXk/3VZOW0QcNqMN3rZEqXP6X1 E7ROqP4A6w17rtPfY6qTaci+n7lmNMjeoBoJJfr4bWTkLbuFr0c2zJvjMfAkHNldX/Uh NL4A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723863461; x=1724468261; 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=mY4VRCvd9L4i067FYJCdAQbgb2obGf5r9hy8hsyaNQ4=; b=IfCggAr1dmF9jESdIuKLd0wOtHl/9VJWvrgSuSL1g5pA5ewGHuyclfrEaojxHcNTYi Aq/deWEdZeWSII0s6DasohO2XB4ws+NsjoDu8ZmwsHKxVJwwn92kfeJuxTAhlx77vCFb 5EI4rq8nCvmEiNXJbgpOn04J4vNJGPQPsEmzOLzoC+hcVuxV+ODuJvhk1v63dkznW16p Y7tM0NYQ6HfKJkFP2R44VJG96CMQnqjdCREgL5lVWdjrH70+Ice2vYFRyVpSDUFJqtFp nQxc/ENKkEt3ZvavAK0Gm325gWF0siGfq5uVsSpltjcI2KCj0FWkHVlwLgVR8VonTUx3 4SUQ== X-Forwarded-Encrypted: i=1; AJvYcCXsml/t9QLkQZc4MtyjHJNc8CI1kJUowo/BQnKtrG9fHPwHAdIXHDWYcUYLsm9wqhrETPwi1drSS5AAfNP1Ou7F5cw= X-Gm-Message-State: AOJu0YxGL9zCoFyYGbhH5kG1LoLBRpWx1OBXnjveVu+1GBWzPIsPpokF p9QkGyQQcErl4904RKKbLOjoSJ/PNNDQhQDuLL1OdYolZRmI/x4G X-Google-Smtp-Source: AGHT+IEL7YdwJrnU7j+3S0kOHwdlHJumXPs1OdWAsSouquvh+afE8cHfhlG2TFnMxzOYJRDIWJjJ4Q== X-Received: by 2002:a17:902:e549:b0:202:100f:7b9b with SMTP id d9443c01a7336-202100f7d6amr36403685ad.35.1723863461337; Fri, 16 Aug 2024 19:57:41 -0700 (PDT) Received: from localhost.localdomain ([183.193.177.10]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-201f031c5e1sm31801785ad.94.2024.08.16.19.57.32 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 16 Aug 2024 19:57:40 -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 , Simon Horman , Matthew Wilcox Subject: [PATCH v7 6/8] mm/util: Deduplicate code in {kstrdup,kstrndup,kmemdup_nul} Date: Sat, 17 Aug 2024 10:56:22 +0800 Message-Id: <20240817025624.13157-7-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-Stat-Signature: h6tzc4yt6w6fzjiygtp8bmhcdry6mo1p X-Rspamd-Queue-Id: A82488000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jWJJY+3KSeXUnXNUkM16zPOGex9BU2i/e3OXvhw40lY1ehhp0GIW8cYHUuE93R4RdvIVK6LOYZg9sIxhMTzIDFhZqdQFsKL2bAEuHatHwV8VDm4Y2nhAAxHAS/xtc6m1FL0RLtonK4YYPpKD0nkvX0YwdD/crsUDse528ImXuOwtaT1z3XJOcopzu/1ZUsjytYojmJs6xjI+A1EZR8yKMhzDX/+XRRxnq0IzfeqANFEW4ObRv3Lz4rWxAoeSFuhwt5qtNILXlXpBPBFJ/WgeQS/eCFmuFPl7bDEkIbzujpoPtAyMsVDW14QHUq75Gw9KxJe2pwSMvQJm3ecFmBKu7Ij801dn/SERDHVfVkdqbYAb0hCu7yX6KRDNaOrg/xHN+YOO/rm+J+olPpF8Fl+MLmdZlH9hhK06Odh5VFHnOrQI2rLKKw6qAPmeoJdncjoRTrG+zZFZN65wDnAu26XyklbGx8Jo5GsJM8RML1ji9aqnveIhahd4MjmIx8D0TnwgqPrt0NOT3nOuoK/PDdP7j3akpEYLiI7q8hYbootnbAYw0r5rFWMgqY+hhkuUlSs2G4W768RXsUUu/IxZoJ/WjMifODnYAERFv8QQHMxD4y4axk8TnkTMJ86D/5yX+XMOMtT6nqX7Mheo7BorBU3ay3V46yzaS7goqaEJ/lQwrKNQeUHB9OimU9jr1fuEAo6YaaC3UIO0F2PHkt4ugc5CM4 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: These three functions follow the same pattern. To deduplicate the code, let's introduce a common helper __kmemdup_nul(). Suggested-by: Andrew Morton Signed-off-by: Yafang Shao Cc: Simon Horman Cc: Matthew Wilcox --- mm/util.c | 67 +++++++++++++++++++++---------------------------------- 1 file changed, 26 insertions(+), 41 deletions(-) diff --git a/mm/util.c b/mm/util.c index 4542d8a800d9..310c7735c617 100644 --- a/mm/util.c +++ b/mm/util.c @@ -45,33 +45,40 @@ void kfree_const(const void *x) EXPORT_SYMBOL(kfree_const); /** - * kstrdup - allocate space for and copy an existing string - * @s: the string to duplicate + * __kmemdup_nul - Create a NUL-terminated string from @s, which might be unterminated. + * @s: The data to copy + * @len: The size of the data, including the null terminator * @gfp: the GFP mask used in the kmalloc() call when allocating memory * - * Return: newly allocated copy of @s or %NULL in case of error + * Return: newly allocated copy of @s with NUL-termination or %NULL in + * case of error */ -noinline -char *kstrdup(const char *s, gfp_t gfp) +static __always_inline char *__kmemdup_nul(const char *s, size_t len, gfp_t gfp) { - size_t len; char *buf; - if (!s) + buf = kmalloc_track_caller(len, gfp); + if (!buf) return NULL; - len = strlen(s) + 1; - buf = kmalloc_track_caller(len, gfp); - if (buf) { - memcpy(buf, s, len); - /* During memcpy(), the string might be updated to a new value, - * which could be longer than the string when strlen() is - * called. Therefore, we need to add a null termimator. - */ - buf[len - 1] = '\0'; - } + memcpy(buf, s, len); + /* Ensure the buf is always NUL-terminated, regardless of @s. */ + buf[len - 1] = '\0'; return buf; } + +/** + * kstrdup - allocate space for and copy an existing string + * @s: the string to duplicate + * @gfp: the GFP mask used in the kmalloc() call when allocating memory + * + * Return: newly allocated copy of @s or %NULL in case of error + */ +noinline +char *kstrdup(const char *s, gfp_t gfp) +{ + return s ? __kmemdup_nul(s, strlen(s) + 1, gfp) : NULL; +} EXPORT_SYMBOL(kstrdup); /** @@ -106,19 +113,7 @@ EXPORT_SYMBOL(kstrdup_const); */ char *kstrndup(const char *s, size_t max, gfp_t gfp) { - size_t len; - char *buf; - - if (!s) - return NULL; - - len = strnlen(s, max); - buf = kmalloc_track_caller(len+1, gfp); - if (buf) { - memcpy(buf, s, len); - buf[len] = '\0'; - } - return buf; + return s ? __kmemdup_nul(s, strnlen(s, max) + 1, gfp) : NULL; } EXPORT_SYMBOL(kstrndup); @@ -192,17 +187,7 @@ EXPORT_SYMBOL(kvmemdup); */ char *kmemdup_nul(const char *s, size_t len, gfp_t gfp) { - char *buf; - - if (!s) - return NULL; - - buf = kmalloc_track_caller(len + 1, gfp); - if (buf) { - memcpy(buf, s, len); - buf[len] = '\0'; - } - return buf; + return s ? __kmemdup_nul(s, len + 1, gfp) : NULL; } EXPORT_SYMBOL(kmemdup_nul); From patchwork Sat Aug 17 02:56:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13766929 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 40B89C3DA4A for ; Sat, 17 Aug 2024 02:57:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C9A9C6B03FA; Fri, 16 Aug 2024 22:57:55 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C4A316B03FB; Fri, 16 Aug 2024 22:57:55 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B119A8D0066; Fri, 16 Aug 2024 22:57:55 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 937156B03FA for ; Fri, 16 Aug 2024 22:57:55 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 4E0DC80446 for ; Sat, 17 Aug 2024 02:57:55 +0000 (UTC) X-FDA: 82460227710.03.7EC8C9C Received: from mail-pl1-f177.google.com (mail-pl1-f177.google.com [209.85.214.177]) by imf08.hostedemail.com (Postfix) with ESMTP id 74C0016000F for ; Sat, 17 Aug 2024 02:57:52 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=GXIbwtRI; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf08.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.177 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723863458; a=rsa-sha256; cv=none; b=6pHzc6xElFkLuaux4Xn9c/7Naxgy1BD/1iGgOIdB2i9GmSXFeGJHGpYglp8w4l5uLR4tbW xQO/c56AHp0iv3qH2edADAVmZKgCtjpQlqyPhLgP9rvfM55vIQcRLF1iT2RliC2DEj0rvm Djyz+ynAXzghTpdlQfVRlmHJ5hDfNk4= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=GXIbwtRI; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf08.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.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=1723863458; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=by1YCv/9fNhFKuS7v7AkJGwNY9Y/YBFp3hXhZ5CbnE4=; b=jH0eMIV0MaazpM/u8yRQkO8lPucAanW4osIkoFhHBheKXL2E3yKFwaQyyHb4FM9t4gkVA+ jUWbjAxLTb4/u7LDn0Bw+fdtp2kpSnjKOyFoyyOo/ar23JVq784Qsal9l3TBexY/eqsyWX m9yk2hM73tDQihBRdvIB762f7au5+vQ= Received: by mail-pl1-f177.google.com with SMTP id d9443c01a7336-1fd9e70b592so26244175ad.3 for ; Fri, 16 Aug 2024 19:57:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723863471; x=1724468271; 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=by1YCv/9fNhFKuS7v7AkJGwNY9Y/YBFp3hXhZ5CbnE4=; b=GXIbwtRIL1t43cxP9lcCqqfWvTzBA7+4+pHGVef+TyGCthaINlwFd69bOV5fwkL472 21YHb97ugtHTIZyBKCcrt+OSufiiR7M0Bp6ufkEn25zMeQ6aRwaqZoNeZPl+cWEhsZVw 9GzAbbTgB7jIK+nGbekdO2Kf593zG5HLVVudmU1t0QUGes8xpzcfbq7f73JJXlTRcp7v MfWn9dYDLHRc74jqPzC+c9UkMPBFobvmu8lQcsdtmxAPJyF0JApRPTEwpIgiQANDeNff Zps1/MKXDHrXENE8+7NZ1ciUKQ7CP17X6kOEqM7en0zNXoyQzmzFARUQVpoH6LttJWk9 j6bA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723863471; x=1724468271; 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=by1YCv/9fNhFKuS7v7AkJGwNY9Y/YBFp3hXhZ5CbnE4=; b=Sm6U3EUjNsQCenVh232P5N87rf9A2fGyv/l+Glav59/cnms4FKuIzJp2CglCFaozWA E4pujqR0g/SvWtB6kYsWqxrZZmaKdnuPmrJNzijn+7PsHXKFWxlEdfRpSY5tH8s86mrc 6InOUfg1aCtUpol0o2pkPmMjl9BDzrpQmK69j7mpv7hfYi2rKf6NFU63XBloXimAqJq2 xgWNHBz/RsfzqZqXxoi2dzipvdLpt8Bb96wATe0NKuL//UEqQp67Q9zFbwt6aApjJ0Y3 vRS9s+F115xLgIoLen0DRPB5OWW+6bHIY0EuvzqzGrqbfVrjzCpjYFu/wA7rjIrwilGi AKsA== X-Forwarded-Encrypted: i=1; AJvYcCWT0GLXYOKE1A1xa1tsHcuRrcmpDRx58FA2ptdaFh0CW0T+FNZY07dbwdd4X/IidE9beBTztGVKVVAlhHurdpPM00g= X-Gm-Message-State: AOJu0YwK9d1PM506vlATEyaSiGF59Cp5fccOyevxTjCU88yZze5IrYgh krPy7gB87JlfcGuArZV/VAj+IxO3Nn7+x092SBc4QHCl8gw6uw0u X-Google-Smtp-Source: AGHT+IGmy8Jt8i1r5v72uMgbTh9QfFBoOAd1ReOek6MsKKuMaUCDeTlIuXQ+GXacq0NZtUUeDKoREg== X-Received: by 2002:a17:902:e542:b0:202:162c:1f36 with SMTP id d9443c01a7336-202195ff1e3mr12511865ad.36.1723863471180; Fri, 16 Aug 2024 19:57:51 -0700 (PDT) Received: from localhost.localdomain ([183.193.177.10]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-201f031c5e1sm31801785ad.94.2024.08.16.19.57.41 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 16 Aug 2024 19:57:50 -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 , "David S. Miller" , David Ahern , Eric Dumazet , Jakub Kicinski , Paolo Abeni Subject: [PATCH v7 7/8] net: Replace strcpy() with strscpy() Date: Sat, 17 Aug 2024 10:56:23 +0800 Message-Id: <20240817025624.13157-8-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: 74C0016000F X-Rspamd-Server: rspam01 X-Stat-Signature: fr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c4AtvK1zoSykNH8btUHTTOyS/IwsSnhNZC6wpgbUpkBJrzAODLR7HQD1wXi1EBU+GBqsUQs1Q8JqhcyX69A3DEOdAIwzOvRwPxXtEi4iEGHdZ7YoTx6syCtsLNxvVCPOpAkOJOwjPdP3rWdlAf4Ong2rGcZNGQsgfNLTJDy4q4UdW3+0hsf+2z9JrpRoKynKISh7rwN7yNdrzMG/9fJJASgLbgJEkDI+RIcfRidxkqyy453EKSl3Nm1xBH7wS0K8NHF7yTW/3WylK/AhuTfKucACJmh9Z6jvGSDSqLnzoNl62K8b7gG34eW+mIDXOtlMmMCsNZvYouoXNf2A4UgIYvwBpa8Xluo4cZvO3wOUMZe6v1WsnIEXxmui+6oXRA+R5O2HQ6Mbd1mJ2jkDnlr5L7o/F56niN5cYN6YjTstyDLo0V1yFjdYE/8afGwICUYUeEI9q7YRL2Gd/gasw/d9gxP++y6NHIUQlAG+CM+Bb2josbqLOwuWJpD6pYBuLgjzfE/yAhcx3sgtOX2fEU3qm/Ju85sVYUdZdneJ/DfbkOVcpmoMMu4hvd4+f2gPCt/8gXW3Lx630/tYpem+rQd/d0vV0wu45qiWrg== 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: To prevent errors from occurring when the src string is longer than the dst string in strcpy(), we should use strscpy() instead. This approach also facilitates future extensions to the task comm. Signed-off-by: Yafang Shao Cc: "David S. Miller" Cc: David Ahern Cc: Eric Dumazet Cc: Jakub Kicinski Cc: Paolo Abeni --- net/ipv6/ndisc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/net/ipv6/ndisc.c b/net/ipv6/ndisc.c index 254b192c5705..17f2e787e6f8 100644 --- a/net/ipv6/ndisc.c +++ b/net/ipv6/ndisc.c @@ -1942,7 +1942,7 @@ static void ndisc_warn_deprecated_sysctl(const struct ctl_table *ctl, static char warncomm[TASK_COMM_LEN]; static int warned; if (strcmp(warncomm, current->comm) && warned < 5) { - strcpy(warncomm, current->comm); + strscpy(warncomm, current->comm); pr_warn("process `%s' is using deprecated sysctl (%s) net.ipv6.neigh.%s.%s - use net.ipv6.neigh.%s.%s_ms instead\n", warncomm, func, dev_name, ctl->procname, From patchwork Sat Aug 17 02:56:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13766930 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 DB8E6C531DE for ; Sat, 17 Aug 2024 02:58:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 742FB6B03FC; Fri, 16 Aug 2024 22:58:04 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6F2B16B03FD; Fri, 16 Aug 2024 22:58:04 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5941B6B03FE; Fri, 16 Aug 2024 22:58:04 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 3B96A6B03FC for ; Fri, 16 Aug 2024 22:58:04 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id EFEF3C044C for ; Sat, 17 Aug 2024 02:58:03 +0000 (UTC) X-FDA: 82460228046.07.8EAB9EF Received: from mail-yw1-f181.google.com (mail-yw1-f181.google.com [209.85.128.181]) by imf20.hostedemail.com (Postfix) with ESMTP id 2A0031C000B for ; Sat, 17 Aug 2024 02:58:01 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="nXfd/Odj"; spf=pass (imf20.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.128.181 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1723863445; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=PIeMfVPvNi2mFgQGi33u0WiVQIXgvCVnP/JNmg1PAVg=; b=ib0tAQJh4kIc/YIqJGiki648rURBDDYXw7QCc8zOoZ/8OeY6nA+zW18CJBl5BsSFgpPpvE TjzLIpqlExX3E/tcse8HbEW8Owm4s4hnW2ZIFvoN3756x6uz7LkBevRGaCyrs+aZRLn0qb tHt9VoNB0mvIFGVK+1b9A3KfhF7v+8g= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="nXfd/Odj"; spf=pass (imf20.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.128.181 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723863445; a=rsa-sha256; cv=none; b=DMCmXWGoCRUz5E+KWVuocEX5ET8KHAJ84WJ3wrWB6ZfyOIIC4pgMr8I4H5OiBp6O90NQTR nPbz5OlpfR1h5pJB0bDHQczOSC39ePRrCGuKRaMAZBuVnAZIxNnoyE+yVMbMclAaZOfbYA PtoL7iTwykGjngpxAx65m35uQASx3fo= Received: by mail-yw1-f181.google.com with SMTP id 00721157ae682-690404fd27eso25415687b3.2 for ; Fri, 16 Aug 2024 19:58:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723863481; x=1724468281; 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=PIeMfVPvNi2mFgQGi33u0WiVQIXgvCVnP/JNmg1PAVg=; b=nXfd/OdjAjpT8bw7wx3Phx4wZzuCtf9JwM+Of6bU4NOz9DezWhYSwwllF7aWKzr9gB v/vPcKRrC2qZIR3doiwUhK8quXH59OUORyZVtl7DWsBPDOg61Bl/Qtk3SodHhBNTwqpv OS3cNMWc1b2/nG8/6sC5VIUjdecn/v3lq2fpowxv63iTu5bMH6GQA6LsDTqR0pIAmbLn njbZipbk0ddzGyG5byT8Ktrt/t3kIq3p6XcV/zAsxhtDzzCBX/hJa7D2eTr0q5z0VxxK z7IwhPUBWccM/EM3tvWruI2h8LLjS6Y3jY6P+YaazFN8tzE8ln8Qub/e/U48X+d+cLfC 4DDQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723863481; x=1724468281; 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=PIeMfVPvNi2mFgQGi33u0WiVQIXgvCVnP/JNmg1PAVg=; b=l3PCq2OnY0gwvp5+TrDsP9LmUOBslYXcpM1qPPcc+jDVUJCQ6M61jE4B2sQcPIQD26 dnP/kjU1zPr57b2sCWpK3j3W1lrPu9sUonf+EGTpRPj7S18FYUeeordKVdapC7Ii69TI jiKtZf7fQF4OJbLH5Y3cWgIQkaKGLteDmyRsZ+bIsGG+YpISMblCRk6mqPYEpFngLq7a V8tAAxsJ+BATKlJTOseuakcxsdM1dVBO7TiYWZCoU7oAniYKqrL3XjGjCZZDIN/rzWpz oZ9EEBOy4R5xA03afK8mcAwhzegqywgivylOpwXI5tzjgnAAcy1MT8p5fHhpmyw0SwTn bd1A== X-Forwarded-Encrypted: i=1; AJvYcCWYSuEI9u1nceTFVp1abbcbzxxKdSgJm1Tq5CewPrTvvmv1rVxrTw7LFmqF0dwgYqseCFaLQdEtKbtoLcfDih7xxYg= X-Gm-Message-State: AOJu0YxKNPMcv/UOXCrOwyROwitFz9eD1BWuyF9QGmuC21P958He+xn+ AR9s99dJcfN45ZZJAEbLGEgUZSUBN//tY8s3BWpUCF4JqT/nkb+m X-Google-Smtp-Source: AGHT+IHIEqe/Cmg7s9PimPuB+lj47WHhB9AJi/MthWBlVJ6PrU4Dd47Rx7cTZURpMhIAboN0X1uK+w== X-Received: by 2002:a05:690c:f01:b0:62f:f535:f38 with SMTP id 00721157ae682-6b1b9a64059mr67878767b3.8.1723863481074; Fri, 16 Aug 2024 19:58:01 -0700 (PDT) Received: from localhost.localdomain ([183.193.177.10]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-201f031c5e1sm31801785ad.94.2024.08.16.19.57.51 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 16 Aug 2024 19:58:00 -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 , Daniel Vetter , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie Subject: [PATCH v7 8/8] drm: Replace strcpy() with strscpy() Date: Sat, 17 Aug 2024 10:56:24 +0800 Message-Id: <20240817025624.13157-9-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-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5FiglQ99nvxXwt+upqObedf/J8 uN72h7Qb AKln/PiSYAPPp4r/Q6cvs1UZmZVGl9PSjVRapr2e+Yqpelf3LeXlun91jDQpCheVDJWiK92hja5QuVq3pVt428RsUmG2NRjFOW8j0FcQ/yiW+bEwvtzelzEBRv0H0UquKsXg/Kq9ZbY9kfS401hg7xVRzA9+qAH1eiwCj5nQV+gfnvPvVWelF2K+fWU3SfIAzI1NMm12nP9tmYtVbcmPl8k6OwwH5W7Hz12PbPB4iLsszqdAJERUrAxpZdctCh48u+6t1BioZ/r62JXD04vrQHP8kPhM6YP98mePfyN1sDwBa0iJUO0Z0ugb2gZTUYt608ooOluHcWEANxeOJtxDHZPK04F4wGeYeM4OZPGH6OAFchBATNI7lx74/Y45hWAMYPwG7Fq4sJX9LPR0w8fbChdSlETo0YVqmASr3fZeBPbU3fZU8+KzK7eboLaFFZ5th6S/E6dyUZQeulEfQUa7AodjMcT/9Kyy9iHr24ktTw9JyVcuie+tgzE4pn4InNNobGI29FtzXSlc62VA9/ZjUwko4XOQMO81Jp5x/ 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: To prevent erros from occurring when the src string is longer than the dst string in strcpy(), we should use strscpy() instead. This approach also facilitates future extensions to the task comm. Signed-off-by: Yafang Shao Acked-by: Daniel Vetter Cc: Maarten Lankhorst Cc: Maxime Ripard Cc: Thomas Zimmermann Cc: David Airlie --- drivers/gpu/drm/drm_framebuffer.c | 2 +- drivers/gpu/drm/i915/i915_gpu_error.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/gpu/drm/drm_framebuffer.c b/drivers/gpu/drm/drm_framebuffer.c index 888aadb6a4ac..2d6993539474 100644 --- a/drivers/gpu/drm/drm_framebuffer.c +++ b/drivers/gpu/drm/drm_framebuffer.c @@ -868,7 +868,7 @@ int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb, INIT_LIST_HEAD(&fb->filp_head); fb->funcs = funcs; - strcpy(fb->comm, current->comm); + strscpy(fb->comm, current->comm); ret = __drm_mode_object_add(dev, &fb->base, DRM_MODE_OBJECT_FB, false, drm_framebuffer_free); diff --git a/drivers/gpu/drm/i915/i915_gpu_error.c b/drivers/gpu/drm/i915/i915_gpu_error.c index 625b3c024540..374378ac7c85 100644 --- a/drivers/gpu/drm/i915/i915_gpu_error.c +++ b/drivers/gpu/drm/i915/i915_gpu_error.c @@ -1411,7 +1411,7 @@ static bool record_context(struct i915_gem_context_coredump *e, rcu_read_lock(); task = pid_task(ctx->pid, PIDTYPE_PID); if (task) { - strcpy(e->comm, task->comm); + strscpy(e->comm, task->comm); e->pid = task->pid; } rcu_read_unlock();