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: 13760018 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 gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 45915C52D7C for ; Mon, 12 Aug 2024 02:30:39 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id BC23110E034; Mon, 12 Aug 2024 02:30:38 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="S0fg+FlI"; dkim-atps=neutral Received: from mail-pl1-f177.google.com (mail-pl1-f177.google.com [209.85.214.177]) by gabe.freedesktop.org (Postfix) with ESMTPS id 13E9410E034 for ; Mon, 12 Aug 2024 02:30:37 +0000 (UTC) Received: by mail-pl1-f177.google.com with SMTP id d9443c01a7336-1fd640a6454so28753125ad.3 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=lists.freedesktop.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=S0fg+FlI0UXdFlN20vRHB1DgBy16DMexjcuFyLB/b8GqNCwyCQHgOYRF/sjsfbEE6p 3joR6Krva8fFMu30A70Bj+R5rq6x5KKGRiHIypJ8lEOaRDrrEEBpw9bT1KihwuTYPo2c QcI1pYzf/2DqstboOj+1GxIgDKO/7N/iCXJbjl8Zt7u0MP7EuWhMAapbsNy2ybSeVzWz yBct81woZCrlrgyJOLwJcnzsOR4TjXWNEbykv12rUnN1aOi77fdsAVpe5iAmn9Nrm4Ng rEQbBhKhEeyUutwGzgoQZQ1vjQV1L6TPAFVk0jwYUMdpUlGqHh5hSQbh95TMDwfue397 eIkw== 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=bKn5GlcQAFXtZ/esdTlAXBPp3G8olk9r1TNGYdDpHy4lH2UAgOwvT1GLtidcdG1pvj POggsYwakws0FrU8gviPZtpc4WHDS0JjNQCyCohAI3/OjsNG/1DHmwsoE88iMGotiQkn kgeZhuAuWgpdDcFs55XUR9nimd9frpZwImnpZMcIE+e3/q4zUWair9mEywxbPIaoWTIj hBMWVJfEZPj3Z9CfCL7Kw+RFYGyZCdRTVYLXhx96L5sYxMG25gYQQGcneU5KYesLyDpO qXqCMb5OVgfh4I/fsl5Oh71F3IPkn8rs0BnMeD0KSvcaYVNgfm/8J5VbX7tCqh93uCcA 6dvA== X-Forwarded-Encrypted: i=1; AJvYcCVG/1HLpNj6sl4RCpkJOhwP4GmokmvUtJ0VgLFrfLhhxzoBhw+VOEPFP0DH9bNq30m2fAxU+SaEC0uV9wLB9R5Q3aeSDuCW2pI9R3h5mKqC X-Gm-Message-State: AOJu0YwT2Do42X3E+fpkNVAyfvQLcKqU3w5F6Llk0OYD7Ro5+lfl05Bd xNQob3jjvl4iLrWMwUI+XsaOsiVeDZPsw1PnuabXo8tQyHB70CIi 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-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" 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; } From patchwork Mon Aug 12 02:29:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13760019 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 gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 82DBDC3DA4A for ; Mon, 12 Aug 2024 02:30:47 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id BEAED10E041; Mon, 12 Aug 2024 02:30:46 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="Wv8yqc6X"; dkim-atps=neutral Received: from mail-pl1-f176.google.com (mail-pl1-f176.google.com [209.85.214.176]) by gabe.freedesktop.org (Postfix) with ESMTPS id 77D9110E041 for ; Mon, 12 Aug 2024 02:30:45 +0000 (UTC) Received: by mail-pl1-f176.google.com with SMTP id d9443c01a7336-1ff1cd07f56so32429255ad.2 for ; Sun, 11 Aug 2024 19:30:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723429845; x=1724034645; darn=lists.freedesktop.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=zoIFivrPmimQFCGo/41HPdx35H3kdQIF/qhwBf2evw8=; b=Wv8yqc6X1WKOxvA6ltllDgk36l3v0wujOxLXytPt9E8+dPv/0XlzYLcTMlb73bptee hj7YPwFzW6g30OmlyXuVjDE9a42Zp0mvnk7OwUWx231927jD3n04ERcGdaQkV9QF8s5y FbZ8XWcyrDNStt1/7/b3jhXGodrm0VcFYDRDlxE3s3PIQ61CucrfesadxGjj84k8vSep WFpmZoj1EeB/nQFak/M6EF4DiCBHEL8YmYaN/m8E2iYZVSOKFjkQ5MJPt45VTNQLxoNx XQ9RYux0czrpn5OKSxduFkPFsP4Q4Gmhc8uQ7pwQk8lBmy61a+B4U8gcsUVej9EnoYFE rXvg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723429845; x=1724034645; 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=zoIFivrPmimQFCGo/41HPdx35H3kdQIF/qhwBf2evw8=; b=dnZjauwQQ/GiO/vK7UtS8nUFHvsL2Fv1gUMhfEAOX2ptOpDPPRLjSwY7u9SovC048/ Wair9Yr8rxmg48LT/k1Xh0yvb0mo+NEz04asP6LOefCbmybuJKO1AYQkKJnghLqy9jBk UJG+NSo1jgkbZEXHX6OTrsql+ud4yaZLP38JTpFcy03bDBxFpVBWL4r74e8f1/fmIxqn 9FIR9F65TRWPSVAYi3qjwKrt4oXMuq87DECbLXNqtyEZb28LNaHECl4eVOv5eDbEAakh NXZjaOI5nMdqdDrXYZub0h0N0rntCNMQ3mcPO4t57lM5WHhL3JaSXObjTpqEfDF0Mgo9 a7gw== X-Forwarded-Encrypted: i=1; AJvYcCXYERJ8yM0pdk56ESHTYGnbtc+q3bGDyVtc7/LMB3dnc06+enzojIc/ZgdaxOxodPAhplTBVWwhyHxZtzFn/P/70eiUOYcu9Y6qu+B67wD6 X-Gm-Message-State: AOJu0Yz7QrSSa/hwEkABKTImzyfX+wS1qscm4WuR6KsO7diQjcYGw9Ju enWQsNHLJN99M4WRBKANm4Tzml90EZlRxCxJ8sMOuP+jwdc2vKJh X-Google-Smtp-Source: AGHT+IHsiTPfsy98RJjC8wpBNmaXvnlNH5fflWZLUy6tj95HlXDW+LhDZRUML9DQTQa1nKZJvXQLvQ== X-Received: by 2002:a17:902:db05:b0:1fd:a0e9:910 with SMTP id d9443c01a7336-200ae5e811amr99536005ad.62.1723429844807; Sun, 11 Aug 2024 19:30:44 -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.36 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 11 Aug 2024 19:30:44 -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 , Paul Moore , Eric Paris Subject: [PATCH v6 2/9] auditsc: Replace memcpy() with strscpy() Date: Mon, 12 Aug 2024 10:29:26 +0800 Message-Id: <20240812022933.69850-3-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-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" 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..7cbcf3327409 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, TASK_COMM_LEN); } /** @@ -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, TASK_COMM_LEN); 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, TASK_COMM_LEN); axp->pid_count++; return 0; From patchwork Mon Aug 12 02:29:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13760020 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 gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id A03FBC52D7C for ; Mon, 12 Aug 2024 02:30:53 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 27D5E10E04E; Mon, 12 Aug 2024 02:30:53 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="lICm0Htw"; dkim-atps=neutral Received: from mail-pl1-f176.google.com (mail-pl1-f176.google.com [209.85.214.176]) by gabe.freedesktop.org (Postfix) with ESMTPS id BB47610E046 for ; Mon, 12 Aug 2024 02:30:52 +0000 (UTC) Received: by mail-pl1-f176.google.com with SMTP id d9443c01a7336-1fd9e70b592so28944095ad.3 for ; Sun, 11 Aug 2024 19:30:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723429852; x=1724034652; darn=lists.freedesktop.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=9Po06JAJWHpcwq2qZykUbq7VkC01v3704cEIBIqD0N8=; b=lICm0HtwazW/d/A81JxiuVlkE1A263U/bJiqTu8G8TVLtmFpgm1ZqEC2/o3Tfa48ZN I+ywP/vTNtwDF8Bri5zPyPwfyPgR0HK7Ja7Qy2bZWwH5VHL7Aiu6rJLMX0csfQoxtJut 3ujo/DndKtFHMvCYlF92LJ/IOzNrPIkBAy3ifhKOsUf/Na4CjFbfr0hw6Bmd7KySsVoW Yy2b/H4zah7drRW6q0bbIYNCpF7X1TPm/GMCPsdZQcNFNxubxwa67scRfxdFTnRlAt6y cLb3u8el0xwy15+jfzFz398+JmPCoBQxX10wD1Iv91BpP5h0YjLUe0VKz8cCk1DaFE/l ZDJA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723429852; x=1724034652; 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=9Po06JAJWHpcwq2qZykUbq7VkC01v3704cEIBIqD0N8=; b=ktEZnlTHr/fQDt6Z+/Ne+Hzyy5hCMG9fh9KoGKKuzGy+p01OosuUn8FbvZT2d9UEk+ mSTiwqN+sUX8nd7ytErKVKHexz2Y0xiPGdyqEARdglqmEjl8e6sWaubnzI3tpMTJY9Qb /6ygQH90c2MpmuR103YSwqyO1AYJ8QkoBTQ6NzPLEWVEz9F/MVpj6kM7LDycfMWZGwIB FGUtUnGh2/9wlFQ1TcH9YgpUL0pwdJX2jdjA/C3IvhXd11BLONNnqaQTwVcT8kAGCMGv zVYOaaRPtHyVdJJ4HesmEJuUVAxziH32zj4oWz34Pw62KwYI9W6sdq0qJORTYDI1w9XE SA8Q== X-Forwarded-Encrypted: i=1; AJvYcCWmINrcv7GaasKveO65NnRgElAKMPsl5x4PXbFtl7jtiPQd1R/xD8QA+4eEHnt2dtr7+Tsu9goTOHhKSQoCk3KQrmz3y1OqI3dgx/N8wmno X-Gm-Message-State: AOJu0YxwnCOi+oH4KK8uKuzvb1hvo7r0wMPXJuJQCb97hLp0Qfi5G+Rm RHIlp2R0Rl/z/lRpcxJkLwnBOP7ohWwb+IhcVJmmBPqeuF6ysWCm X-Google-Smtp-Source: AGHT+IE1xZMlHs3Cuw2myWpVVx1UE/beU7sYLAE8S60uxm8E0L6tTVwuNzgTetxBMMEWFVehNVy1XQ== X-Received: by 2002:a17:902:d2c8:b0:1fd:8eaf:eaa0 with SMTP id d9443c01a7336-200ae550a83mr99817585ad.38.1723429852169; Sun, 11 Aug 2024 19:30:52 -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.45 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 11 Aug 2024 19:30:51 -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 , Paul Moore , James Morris , "Serge E. Hallyn" , Stephen Smalley , Ondrej Mosnacek Subject: [PATCH v6 3/9] security: Replace memcpy() with get_task_comm() Date: Mon, 12 Aug 2024 10:29:27 +0800 Message-Id: <20240812022933.69850-4-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-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" 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..57e014ff3076 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, sizeof(comm)); pr_err("SELinux: %s (%d) set checkreqprot to 1. This is no longer supported.\n", comm, current->pid); } From patchwork Mon Aug 12 02:29:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13760021 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 gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 5F305C52D7C for ; Mon, 12 Aug 2024 02:31:01 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id DDBE910E043; Mon, 12 Aug 2024 02:31:00 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="DY+jNv+j"; dkim-atps=neutral Received: from mail-pl1-f179.google.com (mail-pl1-f179.google.com [209.85.214.179]) by gabe.freedesktop.org (Postfix) with ESMTPS id 212C510E043 for ; Mon, 12 Aug 2024 02:30:59 +0000 (UTC) Received: by mail-pl1-f179.google.com with SMTP id d9443c01a7336-1fc5296e214so37046575ad.0 for ; Sun, 11 Aug 2024 19:30:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723429858; x=1724034658; darn=lists.freedesktop.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=DY+jNv+jBrK4rC1PAHUO/6IgbHMJCk6v0hgplTXBDEYALNbwSbPAkfr82kRrzh4Llt uKoQDkUVLY75Q+aRWjfjzRTNtZe4hupbqGl1eGrlE26jO7Az/p4yndnw9VV4kVAOGHU3 mcXlaYSL4VPyzi5Dtkyf3qfYZbZAepr7USYZYAhwvZLGfVmO/0NbsPTs7fzQ/eOMRJDh 4a3RaqRAzWxGZ+lII1AIOjj51Vy0zXjHsVoMpsh5zs3SBGpQR2voGJ5nbsbFdTAj0ymD JSVcF310/7JbML84vgQCG9l7f7VLNDuiRnb5jDGtDecikJUgYXaXXWbZV4q7osHgVuAs XwtQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723429858; x=1724034658; 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=JXxMmmPoN6QuBlSEiuCE2P9NAQwMWb0bSY0C6iTvtHKVEGc4po4+v5eZoALHOOzItu 52rM5qD7h3/NRizxQ+38Jip35wXi79JiU2OTlRRXUXQeSK0ikEov2PSCtxRnUV5aPReF 8YMzxGhcYxaVw53cJzee+EqJuZuLDp5pTMwdH4qMh1OCMst7dHeHiYstm6GRDzk+94Sf 06aKLdjWLmLrHBP2JskizD2Olp/DGr8zsb05rK5DVKoTp2+vCAucOpTmdKRe5mXw8dWX q+QIYITsdhp0l/ngZcJFGqyS8W7Ax6Zm3PoAi0EgXNqssz052VbCGfMRWkOqqjXrvVHt mJGw== X-Forwarded-Encrypted: i=1; AJvYcCVaa6Thj1KfNe0jl18v85FOYkDjmwuclkFJsF7MkgsjZAaWIkcjKS9eKrJsFN+FJNS34fiwqbIkRTenDrBN9D/neWrD3m0O1rrtRfz0IUH1 X-Gm-Message-State: AOJu0Yw0BONLW5PsKKqH7GIbgUuuIUYVW/2fzDScD6L826yP2eFADXZw ud7yYxrrqMoxMvBHCs6A5Zxsj4FhuqqanZZK1Jp57QztWucssYqH X-Google-Smtp-Source: AGHT+IEa9lv8MoV6zk/9zF8wCzjKsRZm15PR0yRidIjcflgd8FgwO2+8+6fP26BlJQqb1WyA3rzQJQ== X-Received: by 2002:a17:902:f54b:b0:1fd:d7cd:ee53 with SMTP id d9443c01a7336-200ae540e2fmr93875475ad.28.1723429858561; Sun, 11 Aug 2024 19:30:58 -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.52 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 11 Aug 2024 19:30:58 -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 , Quentin Monnet Subject: [PATCH v6 4/9] bpftool: Ensure task comm is always NUL-terminated Date: Mon, 12 Aug 2024 10:29:28 +0800 Message-Id: <20240812022933.69850-5-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-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" 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 Mon Aug 12 02:29:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13760022 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 gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id C6B2DC52D7C for ; Mon, 12 Aug 2024 02:31:07 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 5458C10E045; Mon, 12 Aug 2024 02:31:07 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="GVcrQ7Ke"; dkim-atps=neutral Received: from mail-pl1-f181.google.com (mail-pl1-f181.google.com [209.85.214.181]) by gabe.freedesktop.org (Postfix) with ESMTPS id 7D3C310E045 for ; Mon, 12 Aug 2024 02:31:06 +0000 (UTC) Received: by mail-pl1-f181.google.com with SMTP id d9443c01a7336-1fc4fccdd78so25765335ad.2 for ; Sun, 11 Aug 2024 19:31:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723429866; x=1724034666; darn=lists.freedesktop.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=GVcrQ7Ke8FKjAzjNy2ts4M20MWHF7VHqtir/iVsrxwiiNS9Ikw5ttI6i4s4Lw4dWSQ nLA+FM+6239tzMnBzkhAfrZwjuGqrWyY8sq6fIRx+EaJAfZM58UbPL9ZUvW4NLJ4nkc9 tVt6haLTWauf9B5pp8fmsHR3WHUbCMyLGluCjlv9yuMOswMC8SfWRPNvSs4LK5ON7Ddz rb5LmSLl08R8Gzr2IsFskwObk98TKCtxCmxwcEJ1PbbAhITrjyKH653sj5SohUIy+P3S HeiSN9Qt7E/9Ky7mle1y3FBPCaoqmX4xVIPcnvt1zcAHI34N2mSCOTalg4fsuiygVvUB XGUA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723429866; x=1724034666; 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=KzuLasdK37kUs8+sfx37Hu1syT06dvujNX37kT1nByewEykzWJz8mqxUThMykWQSBG qdRfBoZ8HvPLcJxFw0gOsdkPC7RLRZumJrxWMmK5Eu1CCNhvQZAj7rIeq0wRd1/bZshd XRuIz1JSr1qbeGvrZo1Av6j7d7O0YiJBjhMMO8+dRWY0gfQJ5y7+QV6NmD1FaM3p6C8X L4NK+B7e+z3h7tUr0YYEkZd02xSZUX7CqgeRwzeXMl8oydcZW/eeElGfQw1xDed6lV4k sivt59aUlcPGKAbTV/be0ziOVFixGbe86BEbTEjbQYRtmK0PzYlF1L7YPLS9d3Q5TP0J yCeQ== X-Forwarded-Encrypted: i=1; AJvYcCUIavtq5asB5o9TLMM+/34nU+5jVU6isU+zXBU4pCdMF3fULaMPTRLki5Nh+3Gw104xfzR7g2lPtAePsfMZ6UrM5OE0IMZrngUxkmYriPrs X-Gm-Message-State: AOJu0Yx5GJs+ndHuerLham7esluaM62nC8Ak6gVeJNGjejqje9By+b63 zOztGkJjMSM5lQmzn/1HdlSUuauWPwXritu3OB6vmRyBbCXKz9Wx X-Google-Smtp-Source: AGHT+IGpTCkxya5k7tDP0eeWIg02IZrGkBb+Xs7WqcLI3e7hWnKKPF02puCEAAGLT4aj9e0Erpx/iw== X-Received: by 2002:a17:902:cccf:b0:1f7:1655:825c with SMTP id d9443c01a7336-200ae5a8934mr50792845ad.36.1723429865812; Sun, 11 Aug 2024 19:31:05 -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.59 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 11 Aug 2024 19:31:05 -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 Subject: [PATCH v6 5/9] mm/util: Fix possible race condition in kstrdup() Date: Mon, 12 Aug 2024 10:29:29 +0800 Message-Id: <20240812022933.69850-6-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-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" 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 Mon Aug 12 02:29:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13760023 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 gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 62510C3DA4A for ; Mon, 12 Aug 2024 02:31:14 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id CCB9610E046; Mon, 12 Aug 2024 02:31:13 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="fMMK63Ju"; dkim-atps=neutral Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) by gabe.freedesktop.org (Postfix) with ESMTPS id 9F25B10E046 for ; Mon, 12 Aug 2024 02:31:12 +0000 (UTC) Received: by mail-pl1-f174.google.com with SMTP id d9443c01a7336-1fc52394c92so36128185ad.1 for ; Sun, 11 Aug 2024 19:31:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723429872; x=1724034672; darn=lists.freedesktop.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=fMMK63Ju/1ww8Icml5ZqNmJaWEY5mjbCYeUzuY0LLHrA+rX7/IR8v3y3WokaPcTXuj ZdH1fy/kHyN/DndLPnOPgD9J2M67v1SDbs++u/FXeXy2oz4/pf3UpcBap7uELwweb6J+ NO21TAJogE2lFKUlj1srfByhmsNfWi91cJXIz0dSWQ0Oc30+9TyAATSByk0GNmj60xK5 ZtZ5b1eK0pqmJeKTyWgHVilFnJbLf+pgNkY7Ch4k6/faTVKN+e/OUaRT1c514rfzDBBb 6FtHz0bsndFCaOMhVJMfDBf6M0OICz3eyq6sJ1rPSGeex4unN9SWrJs+jfsHnNcikv8Q rjPA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723429872; x=1724034672; 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=sXBKxI3Bk5dGJKcT229b3sIsOLzRgsRLAqurFOfSpmwRH2PGT/AGNES8MqEvs/EfRs AweIVlQpPrFRhXepqZ/GIvpj/aLBhUi7RqC4Y16fzsZgtWjQbwCQmSoAJdDH6hHvexu4 Je8q9lKWDGQ7eF1HIQSJTM/cecPOi0T3vjKbxV9V+Dd1E87ZtLIK6Ai5khXrlXHOPqJP dHU1YniP4l4aqsQsYoEIcZ7DNhto4BQXUy0NzhB89VrQFlgNWKtVUhzyRh9gjuA4cCbS +x5hZEIKgmKxkHQ+pOzNYxfth7svnScXbtqQ9uQmkJNnpKYDhfZg2aNBeej/TUaTfj5G 6ZnA== X-Forwarded-Encrypted: i=1; AJvYcCX4w6l0iIq2J13XGmh/aB/t6ZrC+tt6IqkQJ6FXTG3WrJGTcbgcZbNQKsi+VS8qp2CFFWleIDEf+uwj3hcqaBGvEmgOnfOSexfqriwjIQKV X-Gm-Message-State: AOJu0YzXbm7gtCIcsmzocaTLzWU/yGPCZf0YSrfGT9rtmnKkE4Fm5NmR 8Ggo24S0F4MD+1uEnIyyd8nZNH8Xs3Hisd8mJCoqQu+5udnWZYhN X-Google-Smtp-Source: AGHT+IGDYbe0RTpouvyRH8j57LNI0ygI4N2QpIwMJo/CclCqBa2C0H4JAnlCXTMiS2Sh5vz84U44EQ== X-Received: by 2002:a17:902:dac3:b0:1fb:48c6:a2b0 with SMTP id d9443c01a7336-200ae4dbc8amr93864745ad.5.1723429872026; Sun, 11 Aug 2024 19:31:12 -0700 (PDT) Received: from localhost.localdomain ([39.144.39.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-200bb9fed69sm27884765ad.188.2024.08.11.19.31.06 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 11 Aug 2024 19:31:11 -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 , Simon Horman , Matthew Wilcox Subject: [PATCH v6 6/9] mm/util: Deduplicate code in {kstrdup, kstrndup, kmemdup_nul} Date: Mon, 12 Aug 2024 10:29:30 +0800 Message-Id: <20240812022933.69850-7-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-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" 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 Mon Aug 12 02:29:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13760024 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 gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 98514C52D7C for ; Mon, 12 Aug 2024 02:31:19 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 1580010E04B; Mon, 12 Aug 2024 02:31:19 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="KwlWjRqT"; dkim-atps=neutral Received: from mail-pl1-f171.google.com (mail-pl1-f171.google.com [209.85.214.171]) by gabe.freedesktop.org (Postfix) with ESMTPS id 6928510E04B for ; Mon, 12 Aug 2024 02:31:18 +0000 (UTC) Received: by mail-pl1-f171.google.com with SMTP id d9443c01a7336-1fd9e6189d5so28754955ad.3 for ; Sun, 11 Aug 2024 19:31:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723429878; x=1724034678; darn=lists.freedesktop.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=qdEfItmwefU07w76SDkXtsO/LaxPCVZAR+StHmo8iTc=; b=KwlWjRqTNw+vBV9AtrZSR1TPKWKGiCLqPajOUZWfpW+KtHN34OyUaFYJYM7E1YsQvD iNn9neJXJ1mIQ6PfVoeXDoD3jCW0LSsvpQU/wGKwnSKCdxP9HAkgdc8HfrX2nSEPj6dv QBicDgQykGfjZHM5JpCkpxiwGZnHfxuld5h8Ni/D3De21bkY6Ctkzf08RmU74n9ppoW7 UX6qglUYSeMQMuMHgNt5ED+nqdW9dbnfsto6OhBi6zw3yNOk9JQ8s+S7vdOo1OtT6K+p de1hQ9IXejjov1o7TFLRyX8uvFWQ59l4jB6tjz/8bAIpjpH49swyMKjJBwuCSxYC6FMg wZMw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723429878; x=1724034678; 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=qdEfItmwefU07w76SDkXtsO/LaxPCVZAR+StHmo8iTc=; b=RFVKO0MXkSqvT//3UExyrt45za7hLNnt5c226xF7OgnzUaT2PglGxasaObL6aP0vV9 yAsFJWjiFc27iibHPL32QeSA+ULSrNREyiKf/seuyDCLZGJW+/yP1llXcp1XFwdTDcXv uDLOeZzIMdDWC4Qs18+gEyFpd99yaLuPpSzr9yIOPTFwZ1DCFh6Xvqck09o9++qBHFM3 GvOoHavKY/bn8GUugO2PfpiD64L8iXnnqHvnyiTLcob4c67CjJ3KrxtPMJWZqabKkiEo ARIRBgCpdodsJE4hd/8N8F/gzEFsg8RmKdhYskY1Bzyn8obLU2kjDdYxSfjOT5SqkAOP bUrg== X-Forwarded-Encrypted: i=1; AJvYcCXC2HeuaIGxLWkulie3ahtdQWYwEWS03MDiL+Dl4FHle+xIc/gdYKhG+U4+ObIQ4RPYu4FeZQFljCuZa1ADcig2/R3GYgI24w8DzfzvtgTr X-Gm-Message-State: AOJu0YxhyJ6pRHad2agR2F3bikG0OEZQ4TfsrTEITfLduiMjK2jLNpkk k6BprC7kln5ORJsderRO5ZP+VvUXGqI53IEdtmm7Egd05rF11Hzj X-Google-Smtp-Source: AGHT+IGvMeJ1WDvZRyKICOlWBdf489ybDgp4ZGppLiFztYYS3nDRhT+W8sx80+j2UnrjElUuwI0I0g== X-Received: by 2002:a17:902:e804:b0:1fc:57b7:995c with SMTP id d9443c01a7336-200ae4dba44mr56315805ad.7.1723429877770; Sun, 11 Aug 2024 19:31:17 -0700 (PDT) Received: from localhost.localdomain ([39.144.39.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-200bb9fed69sm27884765ad.188.2024.08.11.19.31.12 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 11 Aug 2024 19:31:17 -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 , Masami Hiramatsu , Mathieu Desnoyers Subject: [PATCH v6 7/9] tracing: Replace strncpy() with strscpy() Date: Mon, 12 Aug 2024 10:29:31 +0800 Message-Id: <20240812022933.69850-8-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-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" 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: Masami Hiramatsu (Google) Cc: Steven Rostedt Cc: Mathieu Desnoyers --- kernel/trace/trace.c | 2 +- kernel/trace/trace_events_hist.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c index 578a49ff5c32..1b2577f9d734 100644 --- a/kernel/trace/trace.c +++ b/kernel/trace/trace.c @@ -1907,7 +1907,7 @@ __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu) max_data->critical_start = data->critical_start; max_data->critical_end = data->critical_end; - strncpy(max_data->comm, tsk->comm, TASK_COMM_LEN); + strscpy(max_data->comm, tsk->comm, TASK_COMM_LEN); max_data->pid = tsk->pid; /* * If tsk == current, then use current_uid(), as that does not use diff --git a/kernel/trace/trace_events_hist.c b/kernel/trace/trace_events_hist.c index 6ece1308d36a..4cd24c25ce05 100644 --- a/kernel/trace/trace_events_hist.c +++ b/kernel/trace/trace_events_hist.c @@ -1599,7 +1599,7 @@ static inline void save_comm(char *comm, struct task_struct *task) return; } - strncpy(comm, task->comm, TASK_COMM_LEN); + strscpy(comm, task->comm, TASK_COMM_LEN); } static void hist_elt_data_free(struct hist_elt_data *elt_data) From patchwork Mon Aug 12 02:29:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13760025 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 gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id D9C2EC52D7C for ; Mon, 12 Aug 2024 02:31:26 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 5AD6810E03D; Mon, 12 Aug 2024 02:31:26 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="j9Ufc7Pg"; dkim-atps=neutral Received: from mail-pl1-f177.google.com (mail-pl1-f177.google.com [209.85.214.177]) by gabe.freedesktop.org (Postfix) with ESMTPS id 515EC10E03D for ; Mon, 12 Aug 2024 02:31:25 +0000 (UTC) Received: by mail-pl1-f177.google.com with SMTP id d9443c01a7336-1fd90c2fc68so25809985ad.1 for ; Sun, 11 Aug 2024 19:31:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723429885; x=1724034685; darn=lists.freedesktop.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=lQq52x1okLoFlEdSdcWpS9opM5nCjcuUg5Jqdy6Utiw=; b=j9Ufc7PgM8RLwnWVCv+2JM/eNcDY08TNnKVlBE8HODIVAHICy0W0hEDR3LoXIZ8dbR qNHJFpazZKuZUL/oRcK+/v5LA2xw7EjDsPJnDu4gMaSvmXcPNYPFVx4NW9KYXcSHh6QN V5UXAiocbVxgfRkdcdbsMmpz0KY2J6wQ2X8Cs3RYsejaVGMYCNDHHqwXdODy0fY29Upc 9tnVfLhR0Cx6PIX6en9m83I1o+a3n1c9ER9hHmWXZnslu/uTbDo8xO3dbVE4qbzH1g+C F6H4oCP4wrahey1oVdpKwGlQVkHUUo+Rfp4uwqRI7u4biiSkRiaHKuP/X6u4Z156/7V+ s/jg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723429885; x=1724034685; 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=lQq52x1okLoFlEdSdcWpS9opM5nCjcuUg5Jqdy6Utiw=; b=YTuHpEs5OX1nmOkqJ2HXQmJsbzkp9qr4wjEVXT64uGLXtv569R8EWFWv2TII44oMRk 8+ZUEVa0MV74zslBCCfohDGiUcF1fJMwtPuKs+oHJVe6oZFWahCbNbUOLI3V0qTEl4EE JSXqMQSqAFM9BD5EbhB5bD9F4pVqBwnPZhfrSONj0S4juFRPCPlljfEdi+2WgA/UE43R 3gKefBKEuNw1qLnHj/N8bhjavho4qqCB4kvq+MWOegWucMWdXa2IpRlTdpA5HF189UMk x6UPXftI5OT/d9PVSdvn4gfNh5P+NWNtlvpeoy9/QIEX9EhLpooQq4WajYSiWAO2TXZI V2dg== X-Forwarded-Encrypted: i=1; AJvYcCUUEHprmGOea6MvNHf9g5dxBhLbeGxgOLKZY9XTwu27OjFqeHIcXSjuh+7OnRxODiJnyi9+E3UBZy83kvpbgdOJ8s66somO7ETs8QJu/2JJ X-Gm-Message-State: AOJu0YyJpWPQiaFyu7phpfZgSRVQpVgE/06+qCqGrbQO44R1U7/D3TSm t+zPCoWMta6MVe4kUOFJgZ/9E4Gm9juLrZJh3bsJMWJRAJcKzGr6 X-Google-Smtp-Source: AGHT+IHaetHTBTCym7N0t424xRy6qtmLiFL/7ctUucHwtwIF7cfl/lI/MF8VrMeOi3ytURGCyHANzQ== X-Received: by 2002:a17:903:249:b0:1fd:7664:d891 with SMTP id d9443c01a7336-200ae5cfb6amr57289655ad.44.1723429884796; Sun, 11 Aug 2024 19:31:24 -0700 (PDT) Received: from localhost.localdomain ([39.144.39.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-200bb9fed69sm27884765ad.188.2024.08.11.19.31.18 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 11 Aug 2024 19:31:24 -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 , "David S. Miller" , David Ahern , Eric Dumazet , Jakub Kicinski , Paolo Abeni Subject: [PATCH v6 8/9] net: Replace strcpy() with strscpy() Date: Mon, 12 Aug 2024 10:29:32 +0800 Message-Id: <20240812022933.69850-9-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-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" 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..bf969a4773c0 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, sizeof(warncomm)); 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 Mon Aug 12 02:29:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13760077 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 gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 8BD6DC52D7C for ; Mon, 12 Aug 2024 02:31:36 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 1126C10E062; Mon, 12 Aug 2024 02:31:36 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="Pt5/YOzo"; dkim-atps=neutral Received: from mail-pg1-f169.google.com (mail-pg1-f169.google.com [209.85.215.169]) by gabe.freedesktop.org (Postfix) with ESMTPS id 299BE10E062 for ; Mon, 12 Aug 2024 02:31:35 +0000 (UTC) Received: by mail-pg1-f169.google.com with SMTP id 41be03b00d2f7-7a103ac7be3so2282194a12.3 for ; Sun, 11 Aug 2024 19:31:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723429895; x=1724034695; darn=lists.freedesktop.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=zZu3KZG3iOINXwUvnWcZKbey2aLH0BWohtl6uQ29mP4=; b=Pt5/YOzo3Lgb2BbosUJ0du67+B/lD7nanvHTxPf44S6HrldejiwHKrP7DFSVVbw6Zd z6rNxG8H7dnv02WOAhT+CgdIgS1RQE618QA/DVlVvNsnmD6I1HKbyzV5yvzPqANAy0DU ut6cKvD1934TQVCHlrZba0g+QZaowFjW7jZXo2/z0H5dHwbO11uSu/y10YiBMG8TPl0N 2o4F6l54hdASolS/1XENhONPv1yLh4eTI5I7HSasduxrwTgRkFEHJTj1cj/wiJiggsLL N1VKLozHnoVW8BkTI3fmlGrwrffPtMQAu+xFFqwBvKmDe/Hg5gPbJ1LonXTcZwwkbEN4 4JhQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723429895; x=1724034695; 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=zZu3KZG3iOINXwUvnWcZKbey2aLH0BWohtl6uQ29mP4=; b=Ncs++8qnoxkqCvQ3xeDjwdc83UcS90fBf6s1/H6rIt57FQ59ByQNAI1udcVqlp13dC 4ww74bJaOwT8DL/VImj6ZA3wjJUxZ8ujzi+Q+yw873ReunIBBagcFz4USrq3Ai+3JdnY nDzW3eoiRaTGDuzAjEatBUNTUPQmyAkXjyrrJ0lmba+uBUQcAAD2ca8usWimhYDfAnWP PsPezhdMBKAeGwK6GD7oCxCRkjMlFU8SIFLF1gigOUCAmHRbLgW03hrGT2mMTx/pWyjB VODXpAzNl+dbf4vP1RKmsUAkNMPf1p0r70CiAQXnTjqUGz1b0HIlnJ/Huwf1rNpqvcDc 53rQ== X-Forwarded-Encrypted: i=1; AJvYcCWg9goIZDbQPXSbNE9qSRv+Mf/Lk2DMDfdDNYMQ6J78JX8eShXVutPCwOZ3doQba9cpAnQcUbYUGvfggvGO+f8UcE1ndKV6UR5chxHaXhJt X-Gm-Message-State: AOJu0YwXVibeRt4aR9vLmspGCGgSBI//jg011Y9Tt3DL2tN/qJub5ifu kHvOMm1eEeDJWTrQd6PF5AEvEbuh+DiJI36wTVmt7TfTJHrKPiZd X-Google-Smtp-Source: AGHT+IHdX1dJ6B1gxMt4DoIS5gsxO+K0m5bSRRhjzARSl7z6wL1zfkA/bgMgo4cA342XfY8E/QA2Rw== X-Received: by 2002:a05:6a21:b85:b0:1c6:fc56:744 with SMTP id adf61e73a8af0-1c89fd26229mr6526380637.31.1723429894568; Sun, 11 Aug 2024 19:31:34 -0700 (PDT) Received: from localhost.localdomain ([39.144.39.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-200bb9fed69sm27884765ad.188.2024.08.11.19.31.25 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 11 Aug 2024 19:31:34 -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 , Daniel Vetter , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie Subject: [PATCH v6 9/9] drm: Replace strcpy() with strscpy() Date: Mon, 12 Aug 2024 10:29:33 +0800 Message-Id: <20240812022933.69850-10-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-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" 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..71bf8997eddf 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, sizeof(fb->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..97424a53bf9e 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, sizeof(e->comm)); e->pid = task->pid; } rcu_read_unlock();