From patchwork Sat Aug 17 02:56:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13766941 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 15C63C531DC for ; Sat, 17 Aug 2024 02:57:01 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 9434910E86A; Sat, 17 Aug 2024 02:57:00 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="UC+As2QT"; dkim-atps=neutral Received: from mail-pl1-f170.google.com (mail-pl1-f170.google.com [209.85.214.170]) by gabe.freedesktop.org (Postfix) with ESMTPS id CE96610E86A for ; Sat, 17 Aug 2024 02:56:58 +0000 (UTC) Received: by mail-pl1-f170.google.com with SMTP id d9443c01a7336-1fc611a0f8cso22797065ad.2 for ; Fri, 16 Aug 2024 19:56:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723863418; x=1724468218; 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=iSXeW14jggb39wKxD5hCG2hdCk95xYh67NieDdymzHU=; b=UC+As2QTRhhirEQ3GxsQ97syVExufeeydS365Kjg5nsQNn2uu/yfCdGZEJDX6Kr4Pm dbmfbDAJ+DNS8KpcT4PtVAQ2Q8q8/rm4ZTK4nYAiCJ1/85MkP5GoHgyiggJek6bRWNWg SSsEh2lfXNR6KwBZ1rm2UQZbBR+hrQOAM/Mo5NkCfLmv/z1bcAeYIswgeiRwPIiUEAlC k+Csdvv5DNoSV9u3kJ/ZdZoFRhOZJMto4EHEIG/hvmm/7M3cSvgEatSxpLaf2l4MmXxF VNq3vJ+2Eoy/vbNzSRqLuVnxeQXH0yp7/sfSOzzFiy/F6RwMm+5nVd0gleOu+20uURtD OxoQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723863418; x=1724468218; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=iSXeW14jggb39wKxD5hCG2hdCk95xYh67NieDdymzHU=; b=c9MieojngKOeiruy7DrvGsP3Xz2bqv6510IN7knM8ddrCFhgU7ccLDzr4y9LQ1XIjL xVn2R1Y2z8OWBaC2zE20NCzD12+qdiDqFKICZV7fDenrIimVWso0bm4c2xvwjLngpOSv 1BCibt4RInrgtHty5DXEG+xN/NJbrHzEDf9qT0cOCOvMbNjQ2W4YX6F0PYEHm+3GoBUO K8bQYFPB5ECP23bq6HpQU/jHRLXcMSx545Ayrp0cL3K1Nfp9Us+9B5j+hzDJCxiDf7eB tRnagtZanApx+zrM8KhsE4KkpzM3DKe3MVDcefE7y2JxtNw2s16SKUumtVtWC8hA/1DH xdGw== X-Forwarded-Encrypted: i=1; AJvYcCWRifEbq4ymOK0ziqsnb8SWFJh+WWWQ1Q0y5F+oGH99KSWiQJis97oim1INWonQ1aovAjJLqAbLpAmIgVi44CxREz5f6MFHJiDZSGPp5p32 X-Gm-Message-State: AOJu0YznfXra6LOZ46A4qlzUGIzy2RgYEKFX5kZgLkP3M/FguETu/zaA qNppb+gKcBrfKD544OfuYISr2fKERj1xaItb5NXu2ZPY7CaKSK/4 X-Google-Smtp-Source: AGHT+IFVf7h2xaEBLW2CupHL7L7C8Pe40CvC/Mw6eEkJQnE3Swhf1+Ors/i9DDP0sM5dBvy2264FSw== X-Received: by 2002:a17:902:f54b:b0:202:37:f721 with SMTP id d9443c01a7336-20203f454fbmr48859335ad.57.1723863418122; Fri, 16 Aug 2024 19:56:58 -0700 (PDT) Received: from localhost.localdomain ([183.193.177.10]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-201f031c5e1sm31801785ad.94.2024.08.16.19.56.47 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 16 Aug 2024 19:56:57 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, alx@kernel.org, justinstitt@google.com, ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, catalin.marinas@arm.com, penguin-kernel@i-love.sakura.ne.jp, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, audit@vger.kernel.org, linux-security-module@vger.kernel.org, selinux@vger.kernel.org, bpf@vger.kernel.org, netdev@vger.kernel.org, dri-devel@lists.freedesktop.org, Yafang Shao , Alexander Viro , Christian Brauner , Jan Kara , Kees Cook , Matus Jokay , "Serge E. Hallyn" Subject: [PATCH v7 1/8] Get rid of __get_task_comm() Date: Sat, 17 Aug 2024 10:56:17 +0800 Message-Id: <20240817025624.13157-2-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240817025624.13157-1-laoar.shao@gmail.com> References: <20240817025624.13157-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-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 a47d0e4c54f6..2e468ddd203a 100644 --- a/fs/exec.c +++ b/fs/exec.c @@ -1264,16 +1264,6 @@ static int unshare_sighand(struct task_struct *me) return 0; } -char *__get_task_comm(char *buf, size_t buf_size, struct task_struct *tsk) -{ - task_lock(tsk); - /* Always NUL terminated and zero-padded */ - strscpy_pad(buf, tsk->comm, buf_size); - task_unlock(tsk); - return buf; -} -EXPORT_SYMBOL_GPL(__get_task_comm); - /* * These functions flushes out all traces of the currently running executable * so that a new one can be started diff --git a/fs/proc/array.c b/fs/proc/array.c index 34a47fb0c57f..55ed3510d2bb 100644 --- a/fs/proc/array.c +++ b/fs/proc/array.c @@ -109,7 +109,7 @@ void proc_task_name(struct seq_file *m, struct task_struct *p, bool escape) else if (p->flags & PF_KTHREAD) get_kthread_comm(tcomm, sizeof(tcomm), p); else - __get_task_comm(tcomm, sizeof(tcomm), p); + get_task_comm(tcomm, p); if (escape) seq_escape_str(m, tcomm, ESCAPE_SPACE | ESCAPE_SPECIAL, "\n\\"); diff --git a/include/linux/sched.h b/include/linux/sched.h index 33dd8d9d2b85..5f1c8a58bb76 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -1096,9 +1096,12 @@ struct task_struct { /* * executable name, excluding path. * - * - normally initialized setup_new_exec() - * - access it with [gs]et_task_comm() - * - lock it with task_lock() + * - normally initialized begin_new_exec() + * - set it with set_task_comm() + * - strscpy_pad() to ensure it is always NUL-terminated and + * zero-padded + * - task_lock() to ensure the operation is atomic and the name is + * fully updated. */ char comm[TASK_COMM_LEN]; @@ -1912,10 +1915,27 @@ static inline void set_task_comm(struct task_struct *tsk, const char *from) __set_task_comm(tsk, from, false); } -extern char *__get_task_comm(char *to, size_t len, struct task_struct *tsk); +/* + * - Why not use task_lock()? + * User space can randomly change their names anyway, so locking for readers + * doesn't make sense. For writers, locking is probably necessary, as a race + * condition could lead to long-term mixed results. + * The strscpy_pad() in __set_task_comm() can ensure that the task comm is + * always NUL-terminated and zero-padded. Therefore the race condition between + * reader and writer is not an issue. + * + * - Why not use strscpy_pad()? + * While strscpy_pad() prevents writing garbage past the NUL terminator, which + * is useful when using the task name as a key in a hash map, most use cases + * don't require this. Zero-padding might confuse users if it’s unnecessary, + * and not zeroing might even make it easier to expose bugs. If you need a + * zero-padded task name, please handle that explicitly at the call site. + * + * - ARRAY_SIZE() can help ensure that @buf is indeed an array. + */ #define get_task_comm(buf, tsk) ({ \ - BUILD_BUG_ON(sizeof(buf) != TASK_COMM_LEN); \ - __get_task_comm(buf, sizeof(buf), tsk); \ + strscpy(buf, (tsk)->comm, ARRAY_SIZE(buf)); \ + buf; \ }) #ifdef CONFIG_SMP diff --git a/kernel/kthread.c b/kernel/kthread.c index f7be976ff88a..7d001d033cf9 100644 --- a/kernel/kthread.c +++ b/kernel/kthread.c @@ -101,7 +101,7 @@ void get_kthread_comm(char *buf, size_t buf_size, struct task_struct *tsk) struct kthread *kthread = to_kthread(tsk); if (!kthread || !kthread->full_name) { - __get_task_comm(buf, buf_size, tsk); + strscpy(buf, tsk->comm, buf_size); return; } From patchwork Sat Aug 17 02:56:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13766942 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 EA1E7C531DC for ; Sat, 17 Aug 2024 02:57:09 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 6C02210E86B; Sat, 17 Aug 2024 02:57:09 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="GUvAZEFc"; dkim-atps=neutral Received: from mail-pg1-f182.google.com (mail-pg1-f182.google.com [209.85.215.182]) by gabe.freedesktop.org (Postfix) with ESMTPS id 050FB10E86B for ; Sat, 17 Aug 2024 02:57:07 +0000 (UTC) Received: by mail-pg1-f182.google.com with SMTP id 41be03b00d2f7-7163489149eso1923234a12.1 for ; Fri, 16 Aug 2024 19:57:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723863427; x=1724468227; 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=GUvAZEFc6SdD6ySO8Cx0ScSknc33ljM5HESR76xP8HEne0I0+1bzrhnCxDLEAaJE37 TuMMcs9VOaiZiKqIYtPqiZ8TODZBKAIVUaA671hJu16LDejvKwva6pzjX8Y5K2mQQkqE H5NuaXcpG3lD5pKFzc/6EiIwI3IU3mGaPxkE/6rU/Psy6CLBJ3E9MH5R2zu9f5rjRnnt XR/CDZzFIlQiC6QCur4t0U3gKdPTCySs9KfPFVL8ruEZ92/smRacQ/mORg2Qbvb6OmTF 8W8o8VWuDd06FRUJbz6rcI46hJxipEO03zjlFEmYCLKAwttq8D1D5KZK0CztCPXbbOwi 88TQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723863427; x=1724468227; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=+RcIZVYUDMrvydSLTObUn9UWjAdkDM4CrmA64YTz3G4=; b=UN/T8M2e4qxu1vDBtwgtSP09T1cIABSuHDb7qszorzf7vrtnM1HU7FYYVX0Us4Y5uk LZBPlMoO2i3gDXFkPBD9ae42Zz5i9yMOCwB2XEFDrHflohbGtLPizLDXSJrGRe1mixyB f5zCU+akj5ebE1XGqrxm3lW94MKA0mc3+m3jiHFLQwTjjSR37K3Y7bFJl6ekc//N0+yV 8QjyNxOXjfCGquK1UOJhpf98FCaJlb64G9/Vr4+/LV2Kr1Kobj7lLPvU4+NOgMP9E4cw vviMIId39mn14WGnDjX8++0a/y+8zRvkBC/od5VTOFHQb9LnLZvdAe1qTbEsyq3ABAA/ U3BA== X-Forwarded-Encrypted: i=1; AJvYcCVWEYh2Oca5qrbghL9GIW/U0SGiLWVrE6pD14bU/rs2InBS7sXALuDMZRVRhbX2RNlTXhQW5+e1SPBK6lrknr5cUxaXUG5C5v1dcHH3oajm X-Gm-Message-State: AOJu0YzIp9tsPcWv0Pkj1YNkmzEOwhNoMvQWcff54C2tZElcbG1fyp8W H6T40Qv6a8uGAumBgFm0gq5EMi6EmkVM2DRNPh71AXTJVHi2i+rD X-Google-Smtp-Source: AGHT+IGloZlxC12N+ArnDbFHNJc7Yrc4w00KFUrCatU21ag0p56SeEdkfwKvEveNvVnkzBGuU5AKpQ== X-Received: by 2002:a17:903:32c9:b0:202:bc3:3e6e with SMTP id d9443c01a7336-2020bc33fc8mr44372525ad.33.1723863427201; Fri, 16 Aug 2024 19:57:07 -0700 (PDT) Received: from localhost.localdomain ([183.193.177.10]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-201f031c5e1sm31801785ad.94.2024.08.16.19.56.58 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 16 Aug 2024 19:57:06 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, alx@kernel.org, justinstitt@google.com, ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, catalin.marinas@arm.com, penguin-kernel@i-love.sakura.ne.jp, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, audit@vger.kernel.org, linux-security-module@vger.kernel.org, selinux@vger.kernel.org, bpf@vger.kernel.org, netdev@vger.kernel.org, dri-devel@lists.freedesktop.org, Yafang Shao , Paul Moore , Eric Paris Subject: [PATCH v7 2/8] auditsc: Replace memcpy() with strscpy() Date: Sat, 17 Aug 2024 10:56:18 +0800 Message-Id: <20240817025624.13157-3-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240817025624.13157-1-laoar.shao@gmail.com> References: <20240817025624.13157-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-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..e4ef5e57dde9 100644 --- a/kernel/auditsc.c +++ b/kernel/auditsc.c @@ -2730,7 +2730,7 @@ void __audit_ptrace(struct task_struct *t) context->target_uid = task_uid(t); context->target_sessionid = audit_get_sessionid(t); security_task_getsecid_obj(t, &context->target_sid); - memcpy(context->target_comm, t->comm, TASK_COMM_LEN); + strscpy(context->target_comm, t->comm); } /** @@ -2757,7 +2757,7 @@ int audit_signal_info_syscall(struct task_struct *t) ctx->target_uid = t_uid; ctx->target_sessionid = audit_get_sessionid(t); security_task_getsecid_obj(t, &ctx->target_sid); - memcpy(ctx->target_comm, t->comm, TASK_COMM_LEN); + strscpy(ctx->target_comm, t->comm); return 0; } @@ -2778,7 +2778,7 @@ int audit_signal_info_syscall(struct task_struct *t) axp->target_uid[axp->pid_count] = t_uid; axp->target_sessionid[axp->pid_count] = audit_get_sessionid(t); security_task_getsecid_obj(t, &axp->target_sid[axp->pid_count]); - memcpy(axp->target_comm[axp->pid_count], t->comm, TASK_COMM_LEN); + strscpy(axp->target_comm[axp->pid_count], t->comm); axp->pid_count++; return 0; From patchwork Sat Aug 17 02:56:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13766943 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 4922BC531DC for ; Sat, 17 Aug 2024 02:57:19 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id BCA3010E86C; Sat, 17 Aug 2024 02:57:18 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="AEWcDuYM"; dkim-atps=neutral Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182]) by gabe.freedesktop.org (Postfix) with ESMTPS id 42E0B10E86C for ; Sat, 17 Aug 2024 02:57:17 +0000 (UTC) Received: by mail-pl1-f182.google.com with SMTP id d9443c01a7336-2021c08b95cso1042235ad.0 for ; Fri, 16 Aug 2024 19:57:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723863437; x=1724468237; darn=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=AEWcDuYMzY1Qu4yXSblWx87XwW+jf6GRlGCoHLof9bzhUNPkviVSnvIiiAyn+f7pa5 FG3cIg9Zt8KWaTPmX/x/vL9JMJvhwl82o62qgvg3OubWplQNWuZnS/74G40VwBSkRW0y 5JFBQhH7t7FmkFjWfnTVFvVpPUWgcUQ+EUJzu7heaBg9rJ1+AaUiJTnk5yX3/AZn7Un+ BTV41dmjeSATiLmzmZal3cDf39nV8fpTwFrGkJADe03/6DXtWQH3XPbBeQZqwGSgJzjM riP/3gBXEEFIt7M7iEPCLpjlLCGoec0lYJkRKY7qQvVRXLGiINyOTetwXzQ75EsefqdD pVog== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723863437; x=1724468237; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=mIIMsSPqVp5AusgTbRbdrgF3qEr8ccJBENe3BbQ0UBw=; b=TC3bzv15T0PBVBKUI5x6mkHyuIFCxr0MqNd9mlKbZy/hEBHv0uYBbwN3HtKFIpFo1I hxnFYcdhIJC1Epv7PoihdiddFXhi7jYL/i6awdzjT0r/4xDmlPinFOCaYZjXFI9/dqPv E+8o6MsRwcfC6Vjcpz9phODEZv8X01LKUBV/++GW7QL5ncGaFN6f79Ag7e6dUZQiV1D9 ztrvE/RRz5ZzrGPjmRIyHzG3HUEZJULTukrGg0QXDGjwDEhkJVQA8buCaMCY+Angfevk IuQZHqdJQMs0+0rtFcIhDmtAzstiYnlaRo9mA5WMXQxk+c0XAp1MkyfOgKZPZpl3SWVB ulyw== X-Forwarded-Encrypted: i=1; AJvYcCUNBZW/Gh4EZ6xWNfd6aMpPgIMfQkY7jy90OvEV67iKLdvByE/cGIMB4UbTTHKKHBDxju52H8jUWCP1EjPeYpPjOtaYRXEjnNmnBtB1xNtc X-Gm-Message-State: AOJu0YxMKeGku5DBYW5broYo4bodaZ2bdiPOP/QvQ98cIVHujuoh4Rng EscPSLWgfuKP8QiY11YSXmUV0DNosVT8XPD8th4oSg+cyz/4TwTx X-Google-Smtp-Source: AGHT+IEzug8bsagNMxwK7nm29ecXIwIfZ9FH+TLEi6IcVkbHtwlg4mlyU3SBLgFEVqycM3+Rn/pQ8Q== X-Received: by 2002:a17:903:2303:b0:1fb:9b91:d7d9 with SMTP id d9443c01a7336-202062963e7mr80764935ad.26.1723863436587; Fri, 16 Aug 2024 19:57:16 -0700 (PDT) Received: from localhost.localdomain ([183.193.177.10]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-201f031c5e1sm31801785ad.94.2024.08.16.19.57.07 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 16 Aug 2024 19:57:16 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, alx@kernel.org, justinstitt@google.com, ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, catalin.marinas@arm.com, penguin-kernel@i-love.sakura.ne.jp, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, audit@vger.kernel.org, linux-security-module@vger.kernel.org, selinux@vger.kernel.org, bpf@vger.kernel.org, netdev@vger.kernel.org, dri-devel@lists.freedesktop.org, Yafang Shao , Paul Moore , James Morris , "Serge E. Hallyn" , Stephen Smalley , Ondrej Mosnacek Subject: [PATCH v7 3/8] security: Replace memcpy() with get_task_comm() Date: Sat, 17 Aug 2024 10:56:19 +0800 Message-Id: <20240817025624.13157-4-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240817025624.13157-1-laoar.shao@gmail.com> References: <20240817025624.13157-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-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 Sat Aug 17 02:56:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13766944 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 4ED64C3DA4A for ; Sat, 17 Aug 2024 02:57:27 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id CAD5310E86E; Sat, 17 Aug 2024 02:57:26 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="RBK4AKAg"; 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 1FBCB10E86E for ; Sat, 17 Aug 2024 02:57:25 +0000 (UTC) Received: by mail-pl1-f171.google.com with SMTP id d9443c01a7336-20208830de8so10213245ad.1 for ; Fri, 16 Aug 2024 19:57:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723863444; x=1724468244; darn=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=RBK4AKAgNTP+Ejk0iV4lMDCUwuE9ErX2swKTB4Lbi3iTmalmafc3ACN1wbzn2fpead aJedxKO40AldhilCZwJf+GuNfF94Vz+j/cDNweNzMr/ixLGtzbDOIPzs7Lb9PUVL5mLp chd849gSRfQ9BZNf+eLu5q0tUkkJL05eBWhvxWxySt0DsoLByaHF6X0zHK81QMUGLKpT i8BirKm5gqdB35LSb9oUJ2drMPji/k0yAswZwxrzN0t4s45aXt2VuIA/LJVE+xLCxGh2 ey2h3WqaGlZvS68oB9a+UJ/0oCOSYqGuZ3stTZuC33O4Ym8yLpT5jxZdY8mCVsrf1tRy GndQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723863444; x=1724468244; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=QQdkC9XISn7BWPlTE5/iE7aUj/L2jeLonKoaBsbNpS0=; b=PgKaGVoa6AUwL4Mi8yTGCRPZnrXwjcW8Z2qLcyDxCb6p/AF6VOgUJUMip7B+lDiKRa 60uN+kih0eZafntRST4uSt3Uk9ggr+kjLQGd1Rl4zoylufWcULj3xi9Zl/fyjAML2kAB 4xChdM+aiGIIKWSc7cVLBqKwKMSVf5Q91oHuhLdZnemksrMUmQi31EbjWHng7SGg+JAV l1XG41oDCYn/cChSK5O/EzSPadApgmK4nR5s5AnNR7MpMC6LpYIXGvBNCbnQO7KDJMoL lXnv4MVQbqXFlqLLSxVmOJTG6c1E8yj/Sa8QVG7IPKqKZi4JyBubATOV/H42M0PpRiRu YkSg== X-Forwarded-Encrypted: i=1; AJvYcCW0I2IMDpAd98SEcOvi5CM7/mTqFfKaR/aZS9BH6V44pQoQZ70XicqvLvFrdLNxmVIzGX83AkIBW4w=@lists.freedesktop.org X-Gm-Message-State: AOJu0Yz0CBMYdqeDqHi2kbqXgHi9JUeQHziqr2GlDcQCdv0eUR5w+g+5 o6yzuHg7HDSKe3VEFkhWLlWeL7Zo2WUGenllEL/2HYEnGO9O0mr/ X-Google-Smtp-Source: AGHT+IGimEn6HMsvkqgHYM+kruqPW+JJG9kJGgyigEFWYmxee2uabObu84uvkyN6MQRp9Qx9ywJBnQ== X-Received: by 2002:a17:903:2281:b0:201:febc:4366 with SMTP id d9443c01a7336-2021969a77dmr17762915ad.55.1723863444544; Fri, 16 Aug 2024 19:57:24 -0700 (PDT) Received: from localhost.localdomain ([183.193.177.10]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-201f031c5e1sm31801785ad.94.2024.08.16.19.57.17 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 16 Aug 2024 19:57:24 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, alx@kernel.org, justinstitt@google.com, ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, catalin.marinas@arm.com, penguin-kernel@i-love.sakura.ne.jp, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, audit@vger.kernel.org, linux-security-module@vger.kernel.org, selinux@vger.kernel.org, bpf@vger.kernel.org, netdev@vger.kernel.org, dri-devel@lists.freedesktop.org, Yafang Shao , Quentin Monnet Subject: [PATCH v7 4/8] bpftool: Ensure task comm is always NUL-terminated Date: Sat, 17 Aug 2024 10:56:20 +0800 Message-Id: <20240817025624.13157-5-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240817025624.13157-1-laoar.shao@gmail.com> References: <20240817025624.13157-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-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 Sat Aug 17 02:56:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13766945 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 90D31C531DC for ; Sat, 17 Aug 2024 02:57:34 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id F416510E86F; Sat, 17 Aug 2024 02:57:33 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="asglrlLN"; 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 E3F5010E86F for ; Sat, 17 Aug 2024 02:57:32 +0000 (UTC) Received: by mail-pl1-f176.google.com with SMTP id d9443c01a7336-20203988f37so14318365ad.1 for ; Fri, 16 Aug 2024 19:57:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723863452; x=1724468252; 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=asglrlLNvBacPRIFu+lqBEpGMHf2o3I17lXXv4N41jpU+2dioLUEvRD4CuQQivP4Zt JtawP0L9I7Yq/A7Jzo+nQYNbPLApITmyeMzz7rxxB6j7lKkp9jNUw9W9YloceiJBwsnw Ka6RTV7FVMtxpLXHrRBd8d9IDnSkZd/Zb5HZ1Id8oCLfW/2ZSYKGgMYbpBGpSjAccPRU 0J1WH+Qz2c6YeNuAX9efZnIUYjB/VIDMmsayOFa+AJ3/v1uwACmiKpw911RVbAFq17JA 7J5UbJVYPgayPniPJKeffq34T1KaXHRlRyijm9ZAspGJtD+a7tnCIlb5h9LShxHpHwmx Vjhg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723863452; x=1724468252; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Dg8K6fIsZQLFiS7Xpg/1YZlm81yjN4oHJWyrWmInMGw=; b=u1f7GiEStEh7+9MWofHl8+9/fkf2ueNanSfhmvoA3MaekAHKbPseWylNCVUqpKB3Cv mZHZiGZ6ykjvOVh3QbN+TZZtfjGntav1dS88F+ZjlqxW87IuJuzjiJiTdUrtaZ8YUYSA D2vPalCHni9Su00Z0orNoEmARRYVJwZJ2dGmZXGCXVcBBsJ7Oa9YqCP1/IodnhNPhRuk CGiGJvGu01kx37++qq9iba2fCnLRNbe+7unQ27cb1F0fbH1JHex1SLvdqHJXCS8r0ezn ebvmy7rc0PUMEytq5Spw5Ky04DtkRMWLQQyLqi23R7Ezan5LHMDbMxG9vwyOTnpETMI2 WBrg== X-Forwarded-Encrypted: i=1; AJvYcCWJHgrW6bAca567+lhkdZ4RuvkkEV4Pjp5K6r+/BpVHImIRe31jHx0+ko2U4MvXhZGiaXDgIwxudj8=@lists.freedesktop.org X-Gm-Message-State: AOJu0Yzhqzbw674eTsExhIcmhQZVOT3xxlQ8wml8VTFwt7+cA9zgToOD Ybl1tRNoaN1tZV+ca1+itaePH45RXux7h35SHMqG2e9ML/aOtEBs X-Google-Smtp-Source: AGHT+IF3ZN+4FrJiR5tCQknEVTwkbUbAx80SeY+AiIkjp+qgoruwIjB5dCv+k+zHBrTId5++7+R3wA== X-Received: by 2002:a17:902:cccc:b0:1fb:7b01:7980 with SMTP id d9443c01a7336-20203af4193mr61360745ad.0.1723863452450; Fri, 16 Aug 2024 19:57:32 -0700 (PDT) Received: from localhost.localdomain ([183.193.177.10]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-201f031c5e1sm31801785ad.94.2024.08.16.19.57.24 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 16 Aug 2024 19:57:31 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, alx@kernel.org, justinstitt@google.com, ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, catalin.marinas@arm.com, penguin-kernel@i-love.sakura.ne.jp, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, audit@vger.kernel.org, linux-security-module@vger.kernel.org, selinux@vger.kernel.org, bpf@vger.kernel.org, netdev@vger.kernel.org, dri-devel@lists.freedesktop.org, Yafang Shao Subject: [PATCH v7 5/8] mm/util: Fix possible race condition in kstrdup() Date: Sat, 17 Aug 2024 10:56:21 +0800 Message-Id: <20240817025624.13157-6-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240817025624.13157-1-laoar.shao@gmail.com> References: <20240817025624.13157-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-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 Sat Aug 17 02:56:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13766946 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 73BE3C531DC for ; Sat, 17 Aug 2024 02:57:44 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id EC51310E871; Sat, 17 Aug 2024 02:57:43 +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="TRX92LVc"; 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 E449710E871 for ; Sat, 17 Aug 2024 02:57:41 +0000 (UTC) Received: by mail-pl1-f179.google.com with SMTP id d9443c01a7336-20203988f37so14318835ad.1 for ; Fri, 16 Aug 2024 19:57:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723863461; x=1724468261; 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=TRX92LVcPpudXkes2Rmuht1UVapKsKc9JCYjln1Xp0MIYfy7e4XcQybLnrZGhuR8tf 1AwJnSuqxUZszsy84/eV7/etxB5yK+p+tfpbat4jAgfKolFODgg7L4pWuDQq0TrlKcy9 xBt4MIzYnAPoU7kS5TqUex+sP7L3ibwEbQN/ZR5nEUP4Hc7nvI2XRm2GWBbFUyFudgd+ 9cfQRime4MCMnL7loZv542Y+5kfF5k/+PxbjuFxaytRGl6iwuz1f223nrkzXYmNbkJVu GeEavQZnSx11ktNDFUW4W9rI6PjYpSKN/BcpmPMA+d5+t4QzWvOo3CSokLHhn4Syboud dtHQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723863461; x=1724468261; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=mY4VRCvd9L4i067FYJCdAQbgb2obGf5r9hy8hsyaNQ4=; b=BFQQiA6e4tzPOBOZOfJusy5jv6RHsCjc5b+QFVlI6YZIvIgncAJWsn7/cArSNZVRzc 9gNIZWSOBhFm1Tuj9LFvKi4ECZSZ+RGLOW3LhrBEpuSCHiXMifsWvK5k27QeIy7OTNDM /ZKHPUO3NSw3aobyjWHD1ApXhJCs7lshPDKVdI7omNtOwhoTUjbEktEQqMvG0KfLO8le SsaENStxEefqbi6nTYG5cm5CJKAV+0xJfEpx7nY0AbIWU7a6/GAYEWusDeHJVd5ELroJ eI5K1M65DNuCSW9eeJu1EVu3YOceN+4FtS0W9nX6eptHgDAWzxWw84WDvU1PoNwsSRtQ PiPw== X-Forwarded-Encrypted: i=1; AJvYcCVoSU+4apc1ArU1DI6wLuOMar+hKiJEsMhIMQDymWQAsfcxZTYi8wteAWmZrNUvkR2fr8hCEd8Tfb9K/2/oJjoO86CR/xQMrvYo7hLccPgw X-Gm-Message-State: AOJu0Yycxg2ZQmMwmLPYENJTnR9d8GaFEXOv8dB3xh4hXC60+tkeDKGU wUjCPGiMJGiS7GGFInSL6Y7IAJZ0pPyilOOufOPV9nbAFRvsf8LI X-Google-Smtp-Source: AGHT+IEL7YdwJrnU7j+3S0kOHwdlHJumXPs1OdWAsSouquvh+afE8cHfhlG2TFnMxzOYJRDIWJjJ4Q== X-Received: by 2002:a17:902:e549:b0:202:100f:7b9b with SMTP id d9443c01a7336-202100f7d6amr36403685ad.35.1723863461337; Fri, 16 Aug 2024 19:57:41 -0700 (PDT) Received: from localhost.localdomain ([183.193.177.10]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-201f031c5e1sm31801785ad.94.2024.08.16.19.57.32 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 16 Aug 2024 19:57:40 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, alx@kernel.org, justinstitt@google.com, ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, catalin.marinas@arm.com, penguin-kernel@i-love.sakura.ne.jp, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, audit@vger.kernel.org, linux-security-module@vger.kernel.org, selinux@vger.kernel.org, bpf@vger.kernel.org, netdev@vger.kernel.org, dri-devel@lists.freedesktop.org, Yafang Shao , Simon Horman , Matthew Wilcox Subject: [PATCH v7 6/8] mm/util: Deduplicate code in {kstrdup, kstrndup, kmemdup_nul} Date: Sat, 17 Aug 2024 10:56:22 +0800 Message-Id: <20240817025624.13157-7-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240817025624.13157-1-laoar.shao@gmail.com> References: <20240817025624.13157-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-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 Sat Aug 17 02:56:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13766947 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 E5CB4C531DC for ; Sat, 17 Aug 2024 02:57:52 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 5BA7F10E873; Sat, 17 Aug 2024 02:57:52 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="cj4/4Wme"; 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 A12F110E872 for ; Sat, 17 Aug 2024 02:57:51 +0000 (UTC) Received: by mail-pl1-f179.google.com with SMTP id d9443c01a7336-20208830de8so10214715ad.1 for ; Fri, 16 Aug 2024 19:57:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723863471; x=1724468271; 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=by1YCv/9fNhFKuS7v7AkJGwNY9Y/YBFp3hXhZ5CbnE4=; b=cj4/4WmeV3a+2pOojcJLa727FWDjWjKNNb4rFrx4sdebeu48Mif6WvpETswwn1AtW7 9s9rVTKjjysFHDDErYXc12A7jHBP9B5n+EefrTRUdsmgZs4sJqd7VpkdaVI5g0YkGPLC i6qk+J4DZ32KEBVskMWtSGjz5Hyan5SE8omhgpA+Scl2Lw+BZ1TIzE6WsNat7/Aysxpx eIED6Atfi81EjAQtvbFvdydCalLaqBmZ8z1Ad2hwo0jNG2tCkXnaFngz9jVYUNbBuRcK nXCPiVumuMtQBGZTkr5L+fN3BNzh1MTItbUkN+CLrDZUjSQcUeMSEqMZCER1ZL2hfgQI aMYA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723863471; x=1724468271; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=by1YCv/9fNhFKuS7v7AkJGwNY9Y/YBFp3hXhZ5CbnE4=; b=hx2O0csVIbH8GgeFjdS3zZSsegBNyciRtU3vbOPcwEQddLrOcrfYbyyWpiNOYYXj0C peb+PyYdJeokGnG1MYIeUgsA7OZ7IwLPe92JpPG50IOyiHOjj/yDgJtl+013tLYRmkFk J7GAI4PVxNeXicPueLzkw+GEyYJ5VPln5uyzdTpGEgcSPXb6HkohmQS3NZBQtw9RVIuY PQQMI+fCpytIjt/91UfUN0QGA5ccnDy4sZN3XVpZSFtDKvBhOZwf0+jJOQFdXwfsDpSk PG9MpLyICHjLVX+1I2x+K2WRFe0HG7EVRlGJYf+i9Mv7zKX9iGcaC3DHJJdi/Gfe8Fj/ pIVQ== X-Forwarded-Encrypted: i=1; AJvYcCU+9lVeIXqT7OQnXM67xozuvmXW4rwm1hshVoMntfYdX6hPXFSaEy3p9EuwPqqIbSYtro3Wg8suPUQ3MuD1lQxmWAXlK0FafjpKL5wfwGAK X-Gm-Message-State: AOJu0Yztld2T1vF8XC1PSuno42Y0uVd1/1Ob0rzLaKUv3A0TS7jcF3hv QPHcUCJxDSSPKh9HlvhbSXQySGTLR5dHaWQQn5Cz3M1Eqg3l/rxT X-Google-Smtp-Source: AGHT+IGmy8Jt8i1r5v72uMgbTh9QfFBoOAd1ReOek6MsKKuMaUCDeTlIuXQ+GXacq0NZtUUeDKoREg== X-Received: by 2002:a17:902:e542:b0:202:162c:1f36 with SMTP id d9443c01a7336-202195ff1e3mr12511865ad.36.1723863471180; Fri, 16 Aug 2024 19:57:51 -0700 (PDT) Received: from localhost.localdomain ([183.193.177.10]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-201f031c5e1sm31801785ad.94.2024.08.16.19.57.41 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 16 Aug 2024 19:57:50 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, alx@kernel.org, justinstitt@google.com, ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, catalin.marinas@arm.com, penguin-kernel@i-love.sakura.ne.jp, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, audit@vger.kernel.org, linux-security-module@vger.kernel.org, selinux@vger.kernel.org, bpf@vger.kernel.org, netdev@vger.kernel.org, dri-devel@lists.freedesktop.org, Yafang Shao , "David S. Miller" , David Ahern , Eric Dumazet , Jakub Kicinski , Paolo Abeni Subject: [PATCH v7 7/8] net: Replace strcpy() with strscpy() Date: Sat, 17 Aug 2024 10:56:23 +0800 Message-Id: <20240817025624.13157-8-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240817025624.13157-1-laoar.shao@gmail.com> References: <20240817025624.13157-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-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..17f2e787e6f8 100644 --- a/net/ipv6/ndisc.c +++ b/net/ipv6/ndisc.c @@ -1942,7 +1942,7 @@ static void ndisc_warn_deprecated_sysctl(const struct ctl_table *ctl, static char warncomm[TASK_COMM_LEN]; static int warned; if (strcmp(warncomm, current->comm) && warned < 5) { - strcpy(warncomm, current->comm); + strscpy(warncomm, current->comm); pr_warn("process `%s' is using deprecated sysctl (%s) net.ipv6.neigh.%s.%s - use net.ipv6.neigh.%s.%s_ms instead\n", warncomm, func, dev_name, ctl->procname, From patchwork Sat Aug 17 02:56:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13766948 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 6C5DAC3DA4A for ; Sat, 17 Aug 2024 02:58:04 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id D9CCB10E86D; Sat, 17 Aug 2024 02:58:03 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="nZE9NjbU"; dkim-atps=neutral Received: from mail-yw1-f180.google.com (mail-yw1-f180.google.com [209.85.128.180]) by gabe.freedesktop.org (Postfix) with ESMTPS id 3E10F10E86D for ; Sat, 17 Aug 2024 02:58:02 +0000 (UTC) Received: by mail-yw1-f180.google.com with SMTP id 00721157ae682-690404fd27eso25415697b3.2 for ; Fri, 16 Aug 2024 19:58:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723863481; x=1724468281; 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=PIeMfVPvNi2mFgQGi33u0WiVQIXgvCVnP/JNmg1PAVg=; b=nZE9NjbUUKVza4vQuG3s9V/ohiLeNNO8QohzHPNQx621jwxXglj0g6Q+vRToQvt2iQ a3MKYWjFHYYxQQ6zmF9SEHTAIpxa05HJO/apRZ8uGBB9zQFL/NIisneGqznIJ0F2/C1H QOvWvdmRyf9hs6bJTM2xlT4ogX95+31w/bgW3/qwokItEfbSkq/Sib2Ecx+5JQRstoQW 2zPV12eR4M/JavLQB41IB4kXCMoJI6cObaaUhBSYk1Yzv5qzswxUmL4a0MgXG3oae3BD zXiI0s46c5X4AdvmEg8puiXqjxX76kbx2Xj33my+kcU8yl5lVs0cr6ft3dT9p5ydPWrD dYuw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723863481; x=1724468281; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=PIeMfVPvNi2mFgQGi33u0WiVQIXgvCVnP/JNmg1PAVg=; b=FMk5qihkVALpv6tiJlXPrtczbYJgEyrf+hH8YFQERjNbDgo+64a3IJhhvxSyLhXiA+ UcmUHbU+SKmdcBURncSKRjLQKyIgF3eogyLdn9KhonUwDTog0xwT87YRKIf6RQ3YSV6z YAdXmzG4tYK4fDdebpL9XE/qgtR+VqXFak/IIEqRoyvGe7R2H3VXD6trC3e+VdSrpD5j uwcoB6pZKs4FqNT4OjQu0aeWYODSzc9HtyEWaqGsls1zq3p9pYYFeG4wBhWsx3tmEn9B OtnDnqSSv2D7nbVHitHOupk2nQTc+A8cmAUHUT/p5CzA8FIKDRmryb6z7aTL3MrP2y06 WCIA== X-Forwarded-Encrypted: i=1; AJvYcCXY9QmzY6tc97Nsg/bqDWVOeWoUVG4WKZfGj1yKumk6OIvYNJqM7LsNhPScgq4Col4EASXuk3bMvhvFKADXYZGqLh/L3SaG6DlZnCArYVEm X-Gm-Message-State: AOJu0Yy+iGjLyJMFQ+w94kkgod5SDMEU7b4ugoLUU8j+zkvmKDDKu30T zIpHpLNQKN3pYD5C0BYd8/fLqWIoV5RIsHjaTogZT8dTv1ZxuufP X-Google-Smtp-Source: AGHT+IHIEqe/Cmg7s9PimPuB+lj47WHhB9AJi/MthWBlVJ6PrU4Dd47Rx7cTZURpMhIAboN0X1uK+w== X-Received: by 2002:a05:690c:f01:b0:62f:f535:f38 with SMTP id 00721157ae682-6b1b9a64059mr67878767b3.8.1723863481074; Fri, 16 Aug 2024 19:58:01 -0700 (PDT) Received: from localhost.localdomain ([183.193.177.10]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-201f031c5e1sm31801785ad.94.2024.08.16.19.57.51 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 16 Aug 2024 19:58:00 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, alx@kernel.org, justinstitt@google.com, ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, catalin.marinas@arm.com, penguin-kernel@i-love.sakura.ne.jp, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, audit@vger.kernel.org, linux-security-module@vger.kernel.org, selinux@vger.kernel.org, bpf@vger.kernel.org, netdev@vger.kernel.org, dri-devel@lists.freedesktop.org, Yafang Shao , Daniel Vetter , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie Subject: [PATCH v7 8/8] drm: Replace strcpy() with strscpy() Date: Sat, 17 Aug 2024 10:56:24 +0800 Message-Id: <20240817025624.13157-9-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240817025624.13157-1-laoar.shao@gmail.com> References: <20240817025624.13157-1-laoar.shao@gmail.com> MIME-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..2d6993539474 100644 --- a/drivers/gpu/drm/drm_framebuffer.c +++ b/drivers/gpu/drm/drm_framebuffer.c @@ -868,7 +868,7 @@ int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb, INIT_LIST_HEAD(&fb->filp_head); fb->funcs = funcs; - strcpy(fb->comm, current->comm); + strscpy(fb->comm, current->comm); ret = __drm_mode_object_add(dev, &fb->base, DRM_MODE_OBJECT_FB, false, drm_framebuffer_free); diff --git a/drivers/gpu/drm/i915/i915_gpu_error.c b/drivers/gpu/drm/i915/i915_gpu_error.c index 625b3c024540..374378ac7c85 100644 --- a/drivers/gpu/drm/i915/i915_gpu_error.c +++ b/drivers/gpu/drm/i915/i915_gpu_error.c @@ -1411,7 +1411,7 @@ static bool record_context(struct i915_gem_context_coredump *e, rcu_read_lock(); task = pid_task(ctx->pid, PIDTYPE_PID); if (task) { - strcpy(e->comm, task->comm); + strscpy(e->comm, task->comm); e->pid = task->pid; } rcu_read_unlock();