From patchwork Wed Aug 28 03:03:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13780382 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 7D0D4C54749 for ; Wed, 28 Aug 2024 03:03:47 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id C76F110E461; Wed, 28 Aug 2024 03:03:46 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="PoxgFd3i"; 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 5DD1310E461 for ; Wed, 28 Aug 2024 03:03:45 +0000 (UTC) Received: by mail-pl1-f174.google.com with SMTP id d9443c01a7336-201fba05363so52067995ad.3 for ; Tue, 27 Aug 2024 20:03:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1724814225; x=1725419025; 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=5dwC82d7KQL5pIEF6T6/bNHtBQAMogTq8DRZCJ210sw=; b=PoxgFd3iipq9e+nDYWJkg49/p7t+F1Bjb9PY77z5xJ2f+AqBv4Wz/fWGm10SRUgdcQ yWnPPmzW6zC8/s4k4HIsf70IC3Sl8/jXv2X8qgKBg8Ur9I0ogAGJ2Vv+ZxVQf59rCoIL pMNlJP7F/pdVPZ5f+zZMRHDxWdqr54wX7ZPDW+QM15nJyGSEpimhxOVAjf5tyO/nVyng Wl1KOzp4cBQRwTGddhy0Cpgc9B/h/YDyxyCFe66U06J5ooQOLCzbHWG9iOxo47pBemMy BaSUxur6q/pbBLRQ7ABMP9jbjN85MkLSBvibdk+lM9Y0NHHrja8AQ1sOsDuxUZ980uly hOAA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724814225; x=1725419025; 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=5dwC82d7KQL5pIEF6T6/bNHtBQAMogTq8DRZCJ210sw=; b=MVXCan+uzyQhX4RxQq2ORFsVko9oDJEW4wKpncBF4+K8HobzuI6UTlKzseCsl3ZPOE sjOSzJiW+zFR8Chn5JUuWgHYnnspTE7HedQTYLvQLfM9aL+0KSrUlc2k5A3EtIbFUkIR cC1W7Tv0x3epWLMTDfc8LJtHha8dO8zw+IyRlugetPGICPfWfTi0BShyfycBnXP6tdzy Si4zDMvrc3CqAi0djguYbEW1UUQq9SxxyYbCE5Ztse2IF8Y/qZsxQBo/bRu8t90saVQj rNmI+vUcPXrytYzN4WSEyXOfy78BcP3M1JEIf3VC/69SpYgfpQR6etCc2j//dHkLFUyR BGBQ== X-Forwarded-Encrypted: i=1; AJvYcCUOuDQGWV3WX+GZeEUbcORvqZXuaaEHyYIMcRHNmuLImsMtoqF+bFmBXu7PCrgAHILHiNjLaeiAxwg=@lists.freedesktop.org X-Gm-Message-State: AOJu0YzSM1YunL8zcPfjO+KhyeaWGUh8jDf32tmtnUW7U/VACqh/nl8M 4g5yPw8VmjrPRFGF/qA/8P3ch7o4e6meqj2F+CwIExjxYGfUhu0n X-Google-Smtp-Source: AGHT+IEc8gy1oBP9Tt27kWFVV2/sdUfpEnu0CJVUYvciPK7kLGNgdrFqezJmS8xsximWyUWY+TcSOg== X-Received: by 2002:a17:90a:d490:b0:2ca:7636:2217 with SMTP id 98e67ed59e1d1-2d8440a87edmr695434a91.2.1724814224691; Tue, 27 Aug 2024 20:03:44 -0700 (PDT) Received: from localhost.localdomain ([39.144.104.43]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2d8445db8f6sm317977a91.1.2024.08.27.20.03.36 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 27 Aug 2024 20:03:44 -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 v8 1/8] Get rid of __get_task_comm() Date: Wed, 28 Aug 2024 11:03:14 +0800 Message-Id: <20240828030321.20688-2-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240828030321.20688-1-laoar.shao@gmail.com> References: <20240828030321.20688-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 | 32 ++++++++++++++++++++++++++------ kernel/kthread.c | 2 +- 4 files changed, 28 insertions(+), 18 deletions(-) diff --git a/fs/exec.c b/fs/exec.c index 50e76cc633c4..8a23171bc3c3 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 f8d150343d42..c40b95a79d80 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]; @@ -1914,10 +1917,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 Wed Aug 28 03:03:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13780383 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 D7324C5474A for ; Wed, 28 Aug 2024 03:03:55 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 17B5F10E483; Wed, 28 Aug 2024 03:03:55 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="JtmC6sVn"; dkim-atps=neutral Received: from mail-pg1-f176.google.com (mail-pg1-f176.google.com [209.85.215.176]) by gabe.freedesktop.org (Postfix) with ESMTPS id 23F2910E483 for ; Wed, 28 Aug 2024 03:03:53 +0000 (UTC) Received: by mail-pg1-f176.google.com with SMTP id 41be03b00d2f7-7cd830e0711so132432a12.0 for ; Tue, 27 Aug 2024 20:03:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1724814233; x=1725419033; 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=+RcIZVYUDMrvydSLTObUn9UWjAdkDM4CrmA64YTz3G4=; b=JtmC6sVnLeisAoyF1pElJc9+h0zKHZ6C7LvUyv8cMQvTMXgOSf0buzbxe3WJeKi8OK rnVUid8smeEViKstRxecDSNClSbSH7H8Ecv695FhwrO0ZPE6eFLUgiJLFjTdsMW7Ismr b9LGXEtUV/WGBQBa2XXdvadgx3dyxQ5n4vACOvLGjGRohlZzQOdMC4JcO9yo+vXo+Cdq wP1y8gJs+lsc+c0rtJa8wDxQdFbDCALR4Rqiya5ZHVW6hTuRmE+WF8KwLFtWqZ60v8aS TMWTpdZKdsQcvxraxikmC4ULIR//cRyHavWvaawORKetKq21KKSCLbf7LCrbznCN1S7e e5PQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724814233; x=1725419033; 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=d5XjT+6FyR8CyVckrOpwhnxJQZw47xBkzGH+O1aTFykxBeo+Wk3A0vEn8Z8b1WqG/p PSBQrjNS9Ol22DAgDmPuw2qEmNWv4ctgyxw6z/17+tJi0UgoBSz74fDj3hZrlyeezqia TqCCA+crRPHHGCHqvF+NOsPDwDSHG9Exig5/K6qya6Ohace94m7flYJB7sJPb3KtIYmx 8o8GJxHY4racR7Cyu6/m7epSyKnAYuA8k14iDqfdspX2dvgaOV1Q/zUaj+s5KaZ1WXG3 qU887pn6pXbUuudsVov6ebXWsxJlV8pKCAl/0GrsLRxJDl0rwISt7yoXzslkc35mXHE5 /RQw== X-Forwarded-Encrypted: i=1; AJvYcCXt9sXFUUgU/5IPS3dUZEyZznz8xbrxe/ykPGCXNs6441MzpU0der7CIfFH9BU1dKHEaQ4m5Jbh4iE=@lists.freedesktop.org X-Gm-Message-State: AOJu0YwXKcDZ6dKPixgg08/e0WNslnFYZF4kDgaGkRkcR6e7VO5G0tI2 +4KKkrjIgvQZVu8atRqBejzWrt7la5g/OYhXcpTdaFAjPhQq+FBF X-Google-Smtp-Source: AGHT+IGFzzoi8lpwMf6Iv4C1DEmxZSABK+5V2UtAilXgBtIiNQOvP4ALnfkUbY0O1vyUXWVYARLCDw== X-Received: by 2002:a17:90b:d83:b0:2d4:91c:8882 with SMTP id 98e67ed59e1d1-2d843c839efmr1207327a91.11.1724814232595; Tue, 27 Aug 2024 20:03:52 -0700 (PDT) Received: from localhost.localdomain ([39.144.104.43]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2d8445db8f6sm317977a91.1.2024.08.27.20.03.45 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 27 Aug 2024 20:03:51 -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 v8 2/8] auditsc: Replace memcpy() with strscpy() Date: Wed, 28 Aug 2024 11:03:15 +0800 Message-Id: <20240828030321.20688-3-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240828030321.20688-1-laoar.shao@gmail.com> References: <20240828030321.20688-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 Reviewed-by: Justin Stitt --- 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 Wed Aug 28 03:03:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13780384 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 4C571C5474A for ; Wed, 28 Aug 2024 03:04:02 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id B8C5B10E486; Wed, 28 Aug 2024 03:04:01 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="jaVEkhiH"; dkim-atps=neutral Received: from mail-pg1-f181.google.com (mail-pg1-f181.google.com [209.85.215.181]) by gabe.freedesktop.org (Postfix) with ESMTPS id 7146910E486 for ; Wed, 28 Aug 2024 03:04:00 +0000 (UTC) Received: by mail-pg1-f181.google.com with SMTP id 41be03b00d2f7-7c6b4222fe3so3832187a12.3 for ; Tue, 27 Aug 2024 20:04:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1724814240; x=1725419040; 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=mIIMsSPqVp5AusgTbRbdrgF3qEr8ccJBENe3BbQ0UBw=; b=jaVEkhiHFkThKbYjOe8DnHXaYD6XuOYkYpP+LNAKleCcznczay9TkATtVDVdxCh7/z mRR3nY5GSg18SEDBw+k1ZICR0PeF+w6hlY6srtVSfUTp0vHizdWN5pJDX2f18DsooNBH 2heu5g8kdW3KK7+ybgdMlo7yNqyXr2WXR6yuVtrWyIgT3swaQI7g49yd7LvrI64wdPHX ZUfeyxHTJ2KX+Tex7ezfwwiLrNid/KJ4Vc+kdZeOgHmyVVARcFrjxuQtNvCCzAyo1TaE y/+Z4gu845JD/QIYg2mfKCV19AanjzhVjcFZFMWfPhBNgsErCsh6ieNGcILsBzTe+sKZ 7wkQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724814240; x=1725419040; 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=d1CDoiplAXUeAeEe2kL2A4TOXvUirf2aX6kn4uCmgHbQN6CMX3ighjJARRTNFBgv4N +v4WpVsNpVVY1Q84SIVs4EqTSTsZuUgoAwcasJj8EAOXTRasiHd+Lcyxul1Jryel5jRB l5iY5kBovN9nvIE0Vq//Q9v1t3SwopQY6OFabSa30VLH5Uj4TFFRPo6WgYG5k34chYb1 aHCZ5378CM6WsNhLx1n0qw/DvLG1ZKDgYAzZ21pej5C53CSxXW87piYt2hIXEoFiQa08 EZ/L0cCFPPAPxHRDHg6GmkT0WGwCNY66IvzZ/n1SP1CvRldd6Jt5BxSIInnzWLY8j4ad TXoQ== X-Forwarded-Encrypted: i=1; AJvYcCV5t+aq/vt20qx51wBdcsjAu56kSdWBVO7qMShZfy283U3EfXxswp4BLkP+hw3C3pkw/7lnUGK0VRc=@lists.freedesktop.org X-Gm-Message-State: AOJu0YyoT2543FCd3PIvIMdfFYO5z2iWDA6DeG9RyGRLkynvcJkEp5Ew i+aVJ8nAWyxlJUrzFrLk7h1CHpdEPsRzkX3zmE14jY2OLQhOaScT X-Google-Smtp-Source: AGHT+IFERm607QheW54BofDpcXniBv+8JM5KHrTSMUx7woXR1463qkdvSKbT6nvldL/lDZfqmA0dvg== X-Received: by 2002:a05:6a20:43a5:b0:1c4:c7ae:ecea with SMTP id adf61e73a8af0-1ccd286f499mr543329637.11.1724814239886; Tue, 27 Aug 2024 20:03:59 -0700 (PDT) Received: from localhost.localdomain ([39.144.104.43]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2d8445db8f6sm317977a91.1.2024.08.27.20.03.53 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 27 Aug 2024 20:03:59 -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 v8 3/8] security: Replace memcpy() with get_task_comm() Date: Wed, 28 Aug 2024 11:03:16 +0800 Message-Id: <20240828030321.20688-4-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240828030321.20688-1-laoar.shao@gmail.com> References: <20240828030321.20688-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..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 Wed Aug 28 03:03:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13780385 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 CC33DC5474A for ; Wed, 28 Aug 2024 03:04:07 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 44D8310E488; Wed, 28 Aug 2024 03:04:07 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="Uq2arMJ6"; dkim-atps=neutral Received: from mail-pg1-f175.google.com (mail-pg1-f175.google.com [209.85.215.175]) by gabe.freedesktop.org (Postfix) with ESMTPS id 7131410E488 for ; Wed, 28 Aug 2024 03:04:06 +0000 (UTC) Received: by mail-pg1-f175.google.com with SMTP id 41be03b00d2f7-7cd830e0711so132540a12.0 for ; Tue, 27 Aug 2024 20:04:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1724814246; x=1725419046; 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=Uq2arMJ6Bk8aUPb6qUPqWAkywJJIq/EfBN19I5jq0xbZMPqfXJoomcB3UmZnPaYLV6 BJBmBAlMPhz8TK3FGXJYz+W4I54TSJLFK8svLAPi6JD+sdyskp321A3BgeyUmnD8DHWx gZGLc6wRs/jaKT+qg2/OnllyjhJrM9a1kyzkfTDYX+sumfbDw8qTlecmCopS3z1sDYvO 1O/4NaX90EQbmsmKvFPy53f+r16i7cvgRZgYFkCn3CclAQrDTQJ5PbrdIKYehHJaKQpS xbUhBXmOp/9Im77l7LVz2zL5cv3/EKoHDljSZWV4wJnoDPGttK7G7lYwWL3/y+ZGB05W 730Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724814246; x=1725419046; 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=cqhgCedpHFN4Q+eSy6dB0aWirIadsUnvIS2dqXWJzz1/wBqPL2CuHnzSIKFS0GCN9u mXDFnGe8/jMcMh9QMkYBjNh5O2H1U1/b7rIGAj4TU2T12+6He44hIinJCi4Mht2nLti3 pIDZ2Jma0bYpcJB+H2V3hGTw0hQ2Um6sorcRz1XI4MkwWans5Y6RNCfDJ7t5GhAUjKPt XAvt1sHInsKPtGVLBYyv6rcpDGVUScvHQNjJRXt/F9E5Z02vFET97rcoS5xHLzMx38im vposhHmNuXctA2rAk4708u4THYFYbb3MG5CV8Dm0QDWtCYFA6pLydqgk1gHR/4EYZZzM 3M6w== X-Forwarded-Encrypted: i=1; AJvYcCXFh/ZB7E6N6T09Fylua+/Pm9f5+zClgGdU4wiNkl9sK5Ln88XqnwbFNuGoSZWanQSH2i6RqnJwy8E=@lists.freedesktop.org X-Gm-Message-State: AOJu0YwIrM0XH8AVxXgGrO0t149sPx6cNlmlUFp0FCBCB3ZWKnkfEh2F LMgcxeDutdqT4Ny8vyDYJ9TnS85MZYeWgrnShT1ltF0D621c44KD X-Google-Smtp-Source: AGHT+IHxaydVqwv10Tfh2ak5vjo3hT38eMV2JYlayqlM6cQqHxMPAOmu6Mb/QjPRAxpmFPgYrqTvlA== X-Received: by 2002:a17:90a:304b:b0:2cf:f860:f13b with SMTP id 98e67ed59e1d1-2d843da1884mr1305336a91.17.1724814245904; Tue, 27 Aug 2024 20:04:05 -0700 (PDT) Received: from localhost.localdomain ([39.144.104.43]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2d8445db8f6sm317977a91.1.2024.08.27.20.04.00 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 27 Aug 2024 20:04:05 -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 v8 4/8] bpftool: Ensure task comm is always NUL-terminated Date: Wed, 28 Aug 2024 11:03:17 +0800 Message-Id: <20240828030321.20688-5-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240828030321.20688-1-laoar.shao@gmail.com> References: <20240828030321.20688-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 Wed Aug 28 03:03: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: 13780386 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 9A843C5474A for ; Wed, 28 Aug 2024 03:04:16 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 05A3710E489; Wed, 28 Aug 2024 03:04:16 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="OSo/qUtX"; dkim-atps=neutral Received: from mail-pj1-f52.google.com (mail-pj1-f52.google.com [209.85.216.52]) by gabe.freedesktop.org (Postfix) with ESMTPS id 6937A10E489 for ; Wed, 28 Aug 2024 03:04:14 +0000 (UTC) Received: by mail-pj1-f52.google.com with SMTP id 98e67ed59e1d1-2d439572aeaso125416a91.1 for ; Tue, 27 Aug 2024 20:04:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1724814254; x=1725419054; 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=N9R7Mhbk+Se4PIDhpm0/p6nrldMPUtb+oC6vNd91DMo=; b=OSo/qUtXq5sDYASXf6yDXQgYlmudbZtHQZN8rwh06zuoNlQlr55rTHsmdSBgjN/1vh wgaYWrSOJlHEl/fYbkOGCldbL3GqU8jGntqI7yFbmcAZ+M+ihxweiAzE95D7Gj/WuxyC RJfroQ5KbVf79GEqAcrPyDbapyY7Oh77B1Eebjfp6DLzNKHzm62n1JzMC12RoQKaKtzH KQvjuKDSZ27c3pooApB2zyGikMXf2KQYijWzM52i6ws3EGoecSFtVx4H7Zn3v/cLQBUS rqQVAtbG7K4HG38Q5BQ83dIF2zzRKyIhOX2dvuY9O9E7aUZvOSfxGlP7Xuug8NnBVliK y0Ww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724814254; x=1725419054; 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=N9R7Mhbk+Se4PIDhpm0/p6nrldMPUtb+oC6vNd91DMo=; b=aivnTFKh7o2sPfz0JQYAD9XJN+nYvQNABT1rKq2mVwBWC7C9CWE+SRyYIU+pDF4+ZS uuiIc2iGPQnGAas9axJ5mPaauWHyBvvozVecfz21pNLpYH+quDHIoRPXRKhhtDph7/I+ LTq+pgnmTSz0XB8WOzQaDlJNDhG+Nydjze0PoVX6YVavgvdwLgJ0L3RXYoPiUkL5L/so JgCtxtFZgvUQMldFl4MUtuvwsZZ6dEElLv4+WUtLw75LTIX/iK4eB/BFy2ZrzeagTmCj iHTT+Ci3NB3Eq+4mCMKC8ezR7JVdp40ndcBt9DhQR0gsbomNALPDLJgom6im7bzXt4bY r2KA== X-Forwarded-Encrypted: i=1; AJvYcCU1huqqsFPuVQOVFlXEz0YLFyavU6cNy7qMX2C+RKDBw4Vz/nupusgE+yfI25SNW97I3scC4X+b+f4=@lists.freedesktop.org X-Gm-Message-State: AOJu0YyiRZnRFoiWG3jIbs2d6LKotpDbtgj8B/GhzPqoHWWzewFmyAAW 4cj1Cl5/cDz7k2nYbsPosw1D91nYAoJuxf7SKd5+NRYyNZgys3JV X-Google-Smtp-Source: AGHT+IGCtw+zPOY+Vs3gCNaMxterxPo1VEjCCML55T5/9brSfwlu9kiROVQ6nSIbi2XhsLWhhe27AA== X-Received: by 2002:a17:90b:3b49:b0:2d3:d79f:e8b7 with SMTP id 98e67ed59e1d1-2d843c16e68mr1209703a91.5.1724814253885; Tue, 27 Aug 2024 20:04:13 -0700 (PDT) Received: from localhost.localdomain ([39.144.104.43]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2d8445db8f6sm317977a91.1.2024.08.27.20.04.06 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 27 Aug 2024 20:04:13 -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 v8 5/8] mm/util: Fix possible race condition in kstrdup() Date: Wed, 28 Aug 2024 11:03:18 +0800 Message-Id: <20240828030321.20688-6-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240828030321.20688-1-laoar.shao@gmail.com> References: <20240828030321.20688-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 Cc: Alejandro Colomar --- mm/util.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/mm/util.c b/mm/util.c index bd283e2132e0..9a77a347c385 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 NUL termimator. + */ + buf[len - 1] = '\0'; + } return buf; } EXPORT_SYMBOL(kstrdup); From patchwork Wed Aug 28 03:03: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: 13780387 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 208D9C5474A for ; Wed, 28 Aug 2024 03:04:25 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 8893C10E48B; Wed, 28 Aug 2024 03:04:24 +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="Gq16KGPy"; dkim-atps=neutral Received: from mail-pf1-f178.google.com (mail-pf1-f178.google.com [209.85.210.178]) by gabe.freedesktop.org (Postfix) with ESMTPS id 0139810E48B for ; Wed, 28 Aug 2024 03:04:23 +0000 (UTC) Received: by mail-pf1-f178.google.com with SMTP id d2e1a72fcca58-71423704ef3so5001094b3a.3 for ; Tue, 27 Aug 2024 20:04:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1724814263; x=1725419063; 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=O1UnhBnsG0EZ9945TsUyg6NQ1s52ltgN7fNtAXZRUfE=; b=Gq16KGPyoiemwHAiCfAWhVcjwZdJ511YnZ5u+YY94qZLfsxIetDzP4icF0TGwTWR1N DaVUjeP4H+yGIDVZLGHeo9QzbLfp2FcAouvR/IoFdWBvUuA6aqJrzuPPo/c/hZ7aM9YX DOgJAkcSDs6EiXdvt/bh/ekzvECK+ZyVN3FEDsP4xENPgcV49I+iPvE/rFboqi/wwdMo /E9/ZHxqSxxD6AACdDw2coNnBq5pGtcT0s6NoAwvxH1UMHLYl7XOzRFDhJXV/FCRjDXM 7fEIDdRB+x2GQawhbpzB8DiAjj9uJ22+Iu/5ZYK91AxADd6BnoaE8ZA55lSZHdIOwXoM u+FA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724814263; x=1725419063; 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=O1UnhBnsG0EZ9945TsUyg6NQ1s52ltgN7fNtAXZRUfE=; b=iQzD2f9FQPpr/8+YvY+/M34wiwBH1MvthbJKJrhwtGH3vLNoJ/aH6Rf3gXOFVTH0FV J7VXXIF7s+gENQdAD5KSzIjzTK8OSBKOWk6H3tGt2FjONrKTaDizp//k8uML3h56FDMl aX2pRXKDpibnYNwPSYI/yltGLdpM5Mtql1NcZG2w/S0svau1lI278ynKOI0W20+hT2yN WvbG8w6+lNLSNroFR6l4VtHlUGWLn1ENtQ8CQrCJhjGYkVE3vSkROhkn+5O3dPPmGwMY SNYTT/Ir9AJFIcpM9sa+QjMUH0RAjXj234SGntprZ0WMtF7GTbd9L2D5UOHkfLnWTunw ahZw== X-Forwarded-Encrypted: i=1; AJvYcCUAiSwEpDlkuG0BSqIGmea5LtXdF/iBe8drYXWtLbHwBsnDtmbsiRkpAvVEk3Eova/F+7fIcEcK7/o=@lists.freedesktop.org X-Gm-Message-State: AOJu0YwAXc2NbLBagAsyMYaE8xJ0EcB6mUgHe2evRaAx9LX8/UAJyAKS m2RvYHdJCA1dS8ropX2gdsHYwBY/AHrcZFyC59MnKxmDKEyx1Sxc X-Google-Smtp-Source: AGHT+IG7QBffnYc0dCLvaTOX5KXK0mJkKlpaXmWfgPhMMu+561lRAfU2Q11Tqx0ug2HVRiltuCcmIg== X-Received: by 2002:a05:6a20:b598:b0:1c4:b931:e2c4 with SMTP id adf61e73a8af0-1cc8b4bd8bamr18005852637.26.1724814263473; Tue, 27 Aug 2024 20:04:23 -0700 (PDT) Received: from localhost.localdomain ([39.144.104.43]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2d8445db8f6sm317977a91.1.2024.08.27.20.04.14 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 27 Aug 2024 20:04:22 -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 v8 6/8] mm/util: Deduplicate code in {kstrdup, kstrndup, kmemdup_nul} Date: Wed, 28 Aug 2024 11:03:19 +0800 Message-Id: <20240828030321.20688-7-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240828030321.20688-1-laoar.shao@gmail.com> References: <20240828030321.20688-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 Cc: Alejandro Colomar Reviewed-by: Alejandro Colomar Signed-off-by: Alejandro Colomar --- mm/util.c | 68 ++++++++++++++++++++++--------------------------------- 1 file changed, 27 insertions(+), 41 deletions(-) diff --git a/mm/util.c b/mm/util.c index 9a77a347c385..42714fe13e24 100644 --- a/mm/util.c +++ b/mm/util.c @@ -45,33 +45,41 @@ 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, not including the NUL 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) + /* '+1' for the NUL terminator */ + buf = kmalloc_track_caller(len + 1, 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 NUL termimator. - */ - buf[len - 1] = '\0'; - } + memcpy(buf, s, len); + /* Ensure the buf is always NUL-terminated, regardless of @s. */ + buf[len] = '\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), gfp) : NULL; +} EXPORT_SYMBOL(kstrdup); /** @@ -106,19 +114,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), gfp) : NULL; } EXPORT_SYMBOL(kstrndup); @@ -192,17 +188,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, gfp) : NULL; } EXPORT_SYMBOL(kmemdup_nul); From patchwork Wed Aug 28 03:03: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: 13780388 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 C7DD5C54749 for ; Wed, 28 Aug 2024 03:04:34 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 2DBD610E48D; Wed, 28 Aug 2024 03:04:34 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="Clu3aEuR"; dkim-atps=neutral Received: from mail-pj1-f53.google.com (mail-pj1-f53.google.com [209.85.216.53]) by gabe.freedesktop.org (Postfix) with ESMTPS id 1CACF10E48D for ; Wed, 28 Aug 2024 03:04:33 +0000 (UTC) Received: by mail-pj1-f53.google.com with SMTP id 98e67ed59e1d1-2d3d662631aso4815059a91.1 for ; Tue, 27 Aug 2024 20:04:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1724814272; x=1725419072; 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=XVdwfZoyZWde9aSN6zhMbNNaJxIyi5X3Dr8kaez296Q=; b=Clu3aEuRhnzMdUMy8wOj/duRMoZ/1e3LMsb7fx7HZno1QK1kPCAdggQpLgNLbt63oA KIZxnMzKI3uZYjkUbJcP4Ynet3nBCPYBThOJBIhVAaKjbBB5KkfhvYpqRcxGy18zTr+8 3HoEjaZZuHDPGr11jvGO5sdmaVLwRrUpwhzfohix5k3dQKs2l0vKbzbqxGp/XZw66Ir2 yUlBqD8inTk71l+c+fmF+0ZdofKmR6wHYhWiRNlDQPihEJFs6AiPhoRiL3RtAzBNwaFE oTYp2GFO9bRfjXtvX4Vk8TteNnb/Cx5BgS1Yg2OwMucRnc7RAsaUZWI8GnAxiPy+2en5 Q2eA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724814272; x=1725419072; 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=XVdwfZoyZWde9aSN6zhMbNNaJxIyi5X3Dr8kaez296Q=; b=Wi9PZn7AT6k2FCnhUvVdo7GmwayM+pZpvp4ohneYJuGG0/MXkCXyfNHv2FV8vyH733 7AKQ0Ss+vEJZS9vdy+zAO6Oj5SrmdSZJ7OLJRn5TF1wWrNDijvttBvSVxPmUB4kWR/fE NyJp8RHO4AupFowRsyUSBwNwVNqbKoNq0Sbm/EC6UwO41kN/i6M/PrUibOB3xCvyMoWW b8Y6HttMSDFqg2w6rBlZmCF0Y4VGLiLB92kIwbhc0RYTz4Sq43Lo3pT5IUzIGIb1HXMT jtD+zrKxarSd+9/Fk/Ubu9xyYPcvxywezETvp5EiXE3GAhM9x86E7LBPMg/2LSX29931 0AJg== X-Forwarded-Encrypted: i=1; AJvYcCXHujmJDbnh6dI0nrbT5c77wsIaZIBSVQupzvF3XXkfpc22cnIWDKtWia7lQqQGEN3+bCOv4Q2Z0Wc=@lists.freedesktop.org X-Gm-Message-State: AOJu0Yx3wKDAgFrjrr793Y7VMLaDCn3UJsSLMiZH1aqw4m+TueOhD2H+ 1X1Cp2gu+fVBZb/2n0aC/qjHTHmNiTTjhakyBfUf56Tr0eyiTb6X X-Google-Smtp-Source: AGHT+IFb9q9yq9VVSwLC6AkI1/p1bhkyyRxy6O/7jYlaHznbpdfEJAgSG3Tzmhmlf5xXUEBbcGAPmA== X-Received: by 2002:a17:90b:356:b0:2c9:61f9:a141 with SMTP id 98e67ed59e1d1-2d646bf605amr17374556a91.16.1724814272564; Tue, 27 Aug 2024 20:04:32 -0700 (PDT) Received: from localhost.localdomain ([39.144.104.43]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2d8445db8f6sm317977a91.1.2024.08.27.20.04.23 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 27 Aug 2024 20:04:32 -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 v8 7/8] net: Replace strcpy() with strscpy() Date: Wed, 28 Aug 2024 11:03:20 +0800 Message-Id: <20240828030321.20688-8-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240828030321.20688-1-laoar.shao@gmail.com> References: <20240828030321.20688-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 Reviewed-by: Justin Stitt --- 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 b8eec1b6cc2c..cf7c36463b33 100644 --- a/net/ipv6/ndisc.c +++ b/net/ipv6/ndisc.c @@ -1944,7 +1944,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 Wed Aug 28 03:03: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: 13780389 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 63200C54749 for ; Wed, 28 Aug 2024 03:04:45 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id C756E10E48F; Wed, 28 Aug 2024 03:04:44 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="bpRdcJBS"; dkim-atps=neutral Received: from mail-pj1-f45.google.com (mail-pj1-f45.google.com [209.85.216.45]) by gabe.freedesktop.org (Postfix) with ESMTPS id 5E1FD10E48F for ; Wed, 28 Aug 2024 03:04:43 +0000 (UTC) Received: by mail-pj1-f45.google.com with SMTP id 98e67ed59e1d1-2d3c05ec278so4580393a91.0 for ; Tue, 27 Aug 2024 20:04:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1724814283; x=1725419083; 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=8OBLeQ9F541pwxbn8dEubiWANdxJtGv9ekHxWl5Hwac=; b=bpRdcJBSlhJmZS69VbnxID9zxEk6JZ1HQvYPd7wxoh160hIdyogKlf1u2/JyxBvD4d KZnAda0U52dvGPO2WUpgWC+aieUvkiioGAKM2Jnyjw+U3wYKAe0cXJ1aZo+/dfx+iyD1 KTidGArq6utLU+/rscMo7pfEp2a6TliYbwx6Ctl7zqGkhFm/p4EaeFdvtohM6RZWVem5 tdZuXgmzfpLSsPy8EYeaDObkrhTCuXmq8Q0vavk5KjPlrs/vmz5t+YF6h16Yl5hWuP77 VBXPiCaNZh9UR1Hpoav5vfh+WSsi9eUIhwkCHkPkzRIERrtLX91ZvEtbf+mx+IB4JBuN NazQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724814283; x=1725419083; 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=8OBLeQ9F541pwxbn8dEubiWANdxJtGv9ekHxWl5Hwac=; b=qy72+q7EqgRYcia3PBpGfixnxW36fP8IIGgaKHv77PcODdAw9j3rfeu9mVom87mhJO ekEvz7FUdIHNhxcwfv1DxA/8Mg+cMiHsXqnacmmOLftY6hVOB9bzS7WoE3STxF+MdwCm k15vapd58Brlv2Ibir5uE0dmdq7AakKCs9wBP8Y+BVegqJU3bxIlwkuAmiylUt8UuSHL qd4sML4T7F+ZgC5Faj2yLLbR61T0yngc6GI3vkmk4aCVBEB1fyQoCv9KX88rd3KFEcwQ H1C3TKIAX3M2zWJJ8oc6Shj+NIKw7Ld0jxaudta2z/o44Uzcd1sIbw3ypfrawmi14ar5 KIPg== X-Forwarded-Encrypted: i=1; AJvYcCVoSWhQ8G0V9/CFGJMpLsbXDSoROC9Ss+Vcat1ERSZTwXm5+c35QLL5LQFq1XRy8Ja1UlhKgeO+KRE=@lists.freedesktop.org X-Gm-Message-State: AOJu0Yx5t+QcKOp2lldar9Jg5xLK1COzRAPbJy08r5z1x4GvOWoDKzMq GF8ij8YDX5uPCgcfg5+p9p7QD5+r2ZOPYDflKhJLuY1qy+CG1wrl X-Google-Smtp-Source: AGHT+IG9WCG/sJqfIUmD5L1QkWASIgz28eaxuJrS0TfQtB7xTFo8bkGI6Rj777tc/95CgBuyWlEpcw== X-Received: by 2002:a17:90b:390e:b0:2c8:e888:26a2 with SMTP id 98e67ed59e1d1-2d646bcd147mr17296354a91.13.1724814282792; Tue, 27 Aug 2024 20:04:42 -0700 (PDT) Received: from localhost.localdomain ([39.144.104.43]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2d8445db8f6sm317977a91.1.2024.08.27.20.04.32 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 27 Aug 2024 20:04:42 -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 v8 8/8] drm: Replace strcpy() with strscpy() Date: Wed, 28 Aug 2024 11:03:21 +0800 Message-Id: <20240828030321.20688-9-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240828030321.20688-1-laoar.shao@gmail.com> References: <20240828030321.20688-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 Reviewed-by: Justin Stitt --- 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 96c6cafd5b9e..afa9dae39378 100644 --- a/drivers/gpu/drm/i915/i915_gpu_error.c +++ b/drivers/gpu/drm/i915/i915_gpu_error.c @@ -1412,7 +1412,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();