From patchwork Sun Aug 4 07:56:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13752547 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 BA833C3DA64 for ; Sun, 4 Aug 2024 07:57:06 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 22E3610E090; Sun, 4 Aug 2024 07:57:06 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="GChRmBzt"; dkim-atps=neutral Received: from mail-pg1-f173.google.com (mail-pg1-f173.google.com [209.85.215.173]) by gabe.freedesktop.org (Postfix) with ESMTPS id B008610E090 for ; Sun, 4 Aug 2024 07:57:04 +0000 (UTC) Received: by mail-pg1-f173.google.com with SMTP id 41be03b00d2f7-6c5bcb8e8edso7082454a12.2 for ; Sun, 04 Aug 2024 00:57:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722758224; x=1723363024; 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=3hHqe/dVMoVlxAlcEkx88W0WqBv09P0jej1HBEqhafw=; b=GChRmBztxcXqlVfhPFx4rpkj2eikeXj1RjMOPg2vHwCWnXTKdPETeDmjeuC/8HpXnZ tysbzpZch16FXJKQBkoS5/bOshEf5F/9OPlLZAQ9Cx8TnTkJO0IAMVEQDOSYAJXH/tD5 v0EDNO+iD10VwXWaX280Ssasix2RbHSz36MamxgY7+RPvclQ1FnXqkwBJ1P4PQUguSkE LjC6Laz28+agHyADSK9lyT3dHmN39SDYmzRBkkoecU8xhfqXcxmkSMR909vhLiRFR7X/ JyoS6WLxJWZZi7EaMwanviICfXniSw2RR+qFFNMh+UyfzfbJ5YWsTsZySIgnOsDmluxD vtwA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722758224; x=1723363024; 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=3hHqe/dVMoVlxAlcEkx88W0WqBv09P0jej1HBEqhafw=; b=VQ2C1jzbjfw/bBk4sXOJZZLOj/81pDb6J2qaVEy2v3kVSxEdgWqfrbov3ZXxSxtHaO XASLzuqaca/gqQHH1Q8JWR2a6PF7kkXUvTC8jsEleUNJa+/LGH5lp71QCVRnob4a9ogD J9V0ZODOtSEL6yDtAvT1Hf/i5mS8QPpYqUJOavltOEZYuHlhXbIMANb+E59jnq5fg5my AWQzNT5gjU+Ug9JrVhZ25b9L5dGAa+fvp/k2p4fD+1BYitJRN4YMWiuPi20HKL35ZxOF E9EqX02igKyAPDTzDA5vA91OwFRgEhJMEik9RD10eo9dW1j/B0c97/SwdZlW2Vnhh8VW mgTA== X-Forwarded-Encrypted: i=1; AJvYcCVjGPUfkKqq4fFqY8ZxUd2wk7/QeZww00NGiPerChSkiZ4hAXVeXHn5LIyOfJbAhrTROrqz+YRX6BTS4yR8pZF1ST11+lGBJPySnqHL27l0 X-Gm-Message-State: AOJu0YzU+ENhJBTruTce3Fv8BL1x9ElXo+K4cFwT/TvG6kA6NaB7EmIz s3umHb6bjJv/thjof/AGAZONvt2X0SYlo0O3kIS7loVpib539FU2 X-Google-Smtp-Source: AGHT+IGGd4NHuT97uC6xOQQvHv4GaxRo9+Vf+VhHCShMz8IgX/0qP5KyCL4xcS6MgRK6pPd/R/ObqA== X-Received: by 2002:a17:902:f685:b0:1fc:719c:5f0 with SMTP id d9443c01a7336-1ff573e75a3mr134700565ad.49.1722758224055; Sun, 04 Aug 2024 00:57:04 -0700 (PDT) Received: from localhost.localdomain ([39.144.105.172]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ff59178248sm46387605ad.202.2024.08.04.00.56.47 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 04 Aug 2024 00:57:03 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, catalin.marinas@arm.com, penguin-kernel@i-love.sakura.ne.jp, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, audit@vger.kernel.org, linux-security-module@vger.kernel.org, selinux@vger.kernel.org, bpf@vger.kernel.org, netdev@vger.kernel.org, dri-devel@lists.freedesktop.org, Yafang Shao , Alexander Viro , Christian Brauner , Jan Kara , Kees Cook , Matus Jokay Subject: [PATCH v5 1/9] fs/exec: Drop task_lock() inside __get_task_comm() Date: Sun, 4 Aug 2024 15:56:11 +0800 Message-Id: <20240804075619.20804-2-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240804075619.20804-1-laoar.shao@gmail.com> References: <20240804075619.20804-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]: 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 Suggested-by: Linus Torvalds Link: https://lore.kernel.org/all/CAHk-=wivfrF0_zvf+oj6==Sh=-npJooP8chLPEfaFV0oNYTTBA@mail.gmail.com [0] 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 --- fs/exec.c | 10 ++++++++-- include/linux/sched.h | 4 ++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/fs/exec.c b/fs/exec.c index e55efc761947..6a0ff2e3631f 100644 --- a/fs/exec.c +++ b/fs/exec.c @@ -1195,12 +1195,18 @@ static int unshare_sighand(struct task_struct *me) return 0; } +/* + * User space can randomly change their names anyway, so locking for readers + * doesn't make sense. For writers, locking is probably necessary, as a race + * condition could lead to long-term mixed results. + * The strscpy_pad() in __set_task_comm() can ensure that the task comm is + * always NUL-terminated. Therefore the race condition between reader and writer + * is not an issue. + */ 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); diff --git a/include/linux/sched.h b/include/linux/sched.h index f8d150343d42..71002f0fc085 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -1096,9 +1096,9 @@ struct task_struct { /* * executable name, excluding path. * - * - normally initialized setup_new_exec() + * - normally initialized begin_new_exec() * - access it with [gs]et_task_comm() - * - lock it with task_lock() + * - lock it with task_lock() for writing */ char comm[TASK_COMM_LEN]; From patchwork Sun Aug 4 07:56:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13752548 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 1E4FEC3DA64 for ; Sun, 4 Aug 2024 07:57:22 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 9C34610E096; Sun, 4 Aug 2024 07:57:21 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="e+dS8S33"; dkim-atps=neutral Received: from mail-pl1-f181.google.com (mail-pl1-f181.google.com [209.85.214.181]) by gabe.freedesktop.org (Postfix) with ESMTPS id 1CE5510E096 for ; Sun, 4 Aug 2024 07:57:20 +0000 (UTC) Received: by mail-pl1-f181.google.com with SMTP id d9443c01a7336-1fc5549788eso77190695ad.1 for ; Sun, 04 Aug 2024 00:57:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722758239; x=1723363039; 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=9th/bKhMjvRAjGGVHBeVqs/CIYRU9eynsJIFdR021yc=; b=e+dS8S33hwek7kR3Ecrd4g+gst+Vewt5vHhwwlI5O9WcIhHALc4VtdCc0yvNFNPfWR M4NVPZz+TAXDAEfyXtxNpUKUHpi45SDVJC1REhanvkSMMS7QgebpHZbMVAyAJ9nwT+sD peTTHCAs5yC0Jdlt+F5FmCJQ00OVDxMPiQve56KtL3CtCcfMVCkL5HyJ27VaLWXmdlv4 qZZYkWbNSeOxro+G6LvCVr5nWY8BTtrKRzW3ZDHx0r2SSsc7yfwOI2i3RWhdGfVW6988 4m+p3EZg+UpSYcLtceyyh8iaG67BLON/0dTmW3KcrG3qGlz2YFQK6PmNKlwKbwISp7I3 y1gA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722758239; x=1723363039; 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=9th/bKhMjvRAjGGVHBeVqs/CIYRU9eynsJIFdR021yc=; b=fl3VooHVn0ay6wjY+dyU6pcjPdBdCHiOpwEt5NfRBo32mzGJ0S1+a6oGCyAcxrZuGb eRhbVBdUUMvYvsioqa+f81saUBTJ27bpu0oPvku1hU+/fKR/RmcRUwGUc20VtvRLQexl QLpY4H7GbEgOIxS/dnzROW5mDHoyJU5UF60Hv3fO7W6peMtbUYirPn7+SjNW9XQ4xIHp bUM87lqgs3Y+8VS4a1ZOjOu7qnes0Lsp+Wdben/olkBOi/cDnuLrRB3A6VXBrduFemRH iiO+acEwwliM9GOpElEVQR1faJhJlgK158Jss7H5AZREy1jZ8RE4C+/c4dq8rBNPaBWm it1g== X-Forwarded-Encrypted: i=1; AJvYcCWwiv7wHgLD+E75ZF6t+vS3fPrZXEkzpM4XWvVi6dyPV+oPdaPy/4ZdjYXxG+zQXozvrgN4hUoACYTqURN8lXWaSjaoG4b5GMeQ4Nto7Tpv X-Gm-Message-State: AOJu0YxEw5juJV3u7It1FpuAZmcrE2cHg2wixuJnxOwurTwm2mveX3ZB HE+oewo++A0iKRd0xeIF7hx+p/TFjrvWHjpH4ZEGtaFVYwjqK7NQ X-Google-Smtp-Source: AGHT+IFtTPhQAQmKp5sRT0b26x8NVcEvgcuc+FRvXvXgBRo6DjcIq8upCUyDVHbaNG1PlsqP6YWdLQ== X-Received: by 2002:a17:902:cecb:b0:1fc:5ed5:ff56 with SMTP id d9443c01a7336-1ff574a1977mr137301625ad.61.1722758239497; Sun, 04 Aug 2024 00:57:19 -0700 (PDT) Received: from localhost.localdomain ([39.144.105.172]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ff59178248sm46387605ad.202.2024.08.04.00.57.05 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 04 Aug 2024 00:57:19 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, catalin.marinas@arm.com, penguin-kernel@i-love.sakura.ne.jp, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, audit@vger.kernel.org, linux-security-module@vger.kernel.org, selinux@vger.kernel.org, bpf@vger.kernel.org, netdev@vger.kernel.org, dri-devel@lists.freedesktop.org, Yafang Shao , Paul Moore , Eric Paris Subject: [PATCH v5 2/9] auditsc: Replace memcpy() with __get_task_comm() Date: Sun, 4 Aug 2024 15:56:12 +0800 Message-Id: <20240804075619.20804-3-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240804075619.20804-1-laoar.shao@gmail.com> References: <20240804075619.20804-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 __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 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..0459a141dc86 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); + __get_task_comm(context->target_comm, TASK_COMM_LEN, t); } /** @@ -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); + __get_task_comm(ctx->target_comm, TASK_COMM_LEN, t); 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); + __get_task_comm(axp->target_comm[axp->pid_count], TASK_COMM_LEN, t); axp->pid_count++; return 0; From patchwork Sun Aug 4 07:56:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13752549 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 DAE9CC3DA7F for ; Sun, 4 Aug 2024 07:57:36 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 6139010E0BB; Sun, 4 Aug 2024 07:57:36 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="J3XbPaWS"; dkim-atps=neutral Received: from mail-pl1-f173.google.com (mail-pl1-f173.google.com [209.85.214.173]) by gabe.freedesktop.org (Postfix) with ESMTPS id 9C2D710E0BB for ; Sun, 4 Aug 2024 07:57:35 +0000 (UTC) Received: by mail-pl1-f173.google.com with SMTP id d9443c01a7336-1fd66cddd4dso87078425ad.2 for ; Sun, 04 Aug 2024 00:57:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722758255; x=1723363055; 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=B0KTkwxi3yTBFtdP01Ghiv9wnj3oEVaVdo6Xnw/VoxQ=; b=J3XbPaWSdSykVlchyZecodt4yDfXuHe+hJ/0TQVlIjpjoxwNCsVM6rPY4xvaMA82Zd Fm3dK9d/rvkSELczag5wQiHfG6b8tV4F2Ig+qY8BRYThKvE/5+e60NBNGNGkRUlLjYd+ mQj4d4wJAdjr0S9RXlf/aSIaVdqicDQ6VJJDGHs8RS78ZO5fXytFCyjB8Pf7QXSub6K1 Az+R2SPqGmLxLgA30y85kGHAfUfdO220dVLLQvy+riunPVIzWfA9dUs1jDRnplEYJN3D 1muFTqA4vJQzb2liEaHhPpwLkS+W9ER+yN5mktGSlhCM/VOgGwT5egevt7PLZP0ZJPY3 o9uw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722758255; x=1723363055; 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=B0KTkwxi3yTBFtdP01Ghiv9wnj3oEVaVdo6Xnw/VoxQ=; b=jffVwtyhQ+KTGd5isc0J6OKiynbpWoXJIEId0W4Wqd91XW0qnp0+tRxdoJSQJl2URH Sr4m40cb6YU2DBNtyHuHAld1u9MtnaD8K1Jfz6ebhskSqLJi8LrmG8+t5XCN8sCl59p8 3oOdBqeJuA5K+OUDuAUHvRQpzviCitK36NtUH3FiRib/wfrxc8OMMvigqRGFPUxZ+J6p KVFYBjwB4I4rzAMXjOxmuTq8TGn5fmqngtE4YhArAiH6W2qNYokxzKIMaLtRmEHzwvc3 tha+KizOJJ7HLp5OCaj3Yo95TRt6e2XwR4RcLKj3RPN1Ym4hsCkNBtGyR1dx/EAmWTOU wFbw== X-Forwarded-Encrypted: i=1; AJvYcCX2FeDM2R0qgVJM4wOAxG1Fv9lXUjNuzRU2qWNGP/+mtBCEwDwGX0B92b06gsPTu/DUnx12cRlKw8xu4nmCVsH/S5eqkjewDacAQQ/bZcmX X-Gm-Message-State: AOJu0Yz8Abesdn7DPSqGG/fDG1UoEWpCmE8JMNqnR22hfozplVwhjQzk 7g6e1xeFxJgW0I/rS48/7CundqaWjZ/UhUJQuHkhwlcLnTMbtT93 X-Google-Smtp-Source: AGHT+IH6wosRQHFK1XrZrMQ/1/OazY5atARBRVgGVQBt8cXkjOG1KelkDNxLA3xQ6gKV/rMpiZ54sg== X-Received: by 2002:a17:902:aa42:b0:1fd:96e1:7ffe with SMTP id d9443c01a7336-1ff57456f1cmr80659485ad.55.1722758254963; Sun, 04 Aug 2024 00:57:34 -0700 (PDT) Received: from localhost.localdomain ([39.144.105.172]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ff59178248sm46387605ad.202.2024.08.04.00.57.20 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 04 Aug 2024 00:57:34 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, catalin.marinas@arm.com, penguin-kernel@i-love.sakura.ne.jp, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, audit@vger.kernel.org, linux-security-module@vger.kernel.org, selinux@vger.kernel.org, bpf@vger.kernel.org, netdev@vger.kernel.org, dri-devel@lists.freedesktop.org, Yafang Shao , Paul Moore , James Morris , "Serge E. Hallyn" , Stephen Smalley , Ondrej Mosnacek Subject: [PATCH v5 3/9] security: Replace memcpy() with __get_task_comm() Date: Sun, 4 Aug 2024 15:56:13 +0800 Message-Id: <20240804075619.20804-4-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240804075619.20804-1-laoar.shao@gmail.com> References: <20240804075619.20804-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..a922e4339dd5 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, sizeof(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, sizeof(comm), tsk)); } } break; diff --git a/security/selinux/selinuxfs.c b/security/selinux/selinuxfs.c index e172f182b65c..a8a2ec742576 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)); + __get_task_comm(comm, sizeof(comm), current); pr_err("SELinux: %s (%d) set checkreqprot to 1. This is no longer supported.\n", comm, current->pid); } From patchwork Sun Aug 4 07:56:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13752550 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 66777C3DA64 for ; Sun, 4 Aug 2024 07:57:52 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id E4F0B10E0C2; Sun, 4 Aug 2024 07:57:51 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="OvfJXiNh"; dkim-atps=neutral Received: from mail-oi1-f179.google.com (mail-oi1-f179.google.com [209.85.167.179]) by gabe.freedesktop.org (Postfix) with ESMTPS id C4B0610E0C3 for ; Sun, 4 Aug 2024 07:57:50 +0000 (UTC) Received: by mail-oi1-f179.google.com with SMTP id 5614622812f47-3db130a872fso6646666b6e.2 for ; Sun, 04 Aug 2024 00:57:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722758270; x=1723363070; 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=eQQ3mmtZRuc+UzgaTIbXRCgMjfWu2etwx8M84uu2mcE=; b=OvfJXiNhf5NsIEHT/+lw4HvUUjNzmKk+HmYLFCvbktUr+/iKnKVmSn/f/qN2NHUWe9 UHq82TiLt9N08Q6+FKA+Kt6hyKtO67f9sJm0izl0mh1WS52v87ptb8lPzxgfnyyGpfkA knpJjrTkt6ikrEdpOhF5YdnwOvSKjXuDiFWN0Cs/ME1ywVGsRlXlhYdASQiv3b6VUGIQ GB4KIb+kkK83BxAnHnTE+HvVuB3PHhHNO9HF4KjSNG/mX9QbqwMVOHbXnxSPMTY0RmCg u3iF23+SgTAOAnqpUBo15a6hO/Do47HsFz1upc3U/bxoUrSMtkfYvj2NG5sNlDuT+A+x NiHw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722758270; x=1723363070; 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=eQQ3mmtZRuc+UzgaTIbXRCgMjfWu2etwx8M84uu2mcE=; b=naBHVOV74kxzcJ5ShhYaPZWSzJZHoNRRSmbDLQ6pJaLISaD6G1a8tQjvTZAnZHj5v0 pTGP8QG/r4gOooGR8co0EDGmBS5AmiU8QZiAVkSjoSK8CNcy992O0nVd2rZgQKnjCMzC Rgzzfiyxn1nGtwplxGHR6WIQbwBI4yXech2zcngk64Z/03xpFloBVGfsUyzIFONViBV2 jGz+7gV7z6KDDA40XDPjPXInE08jQkFQF1xjmBXVTu7IzEqQ8/xUJUh5u5t6DnNkuq53 5y1w9CLOVVxDSJRx8zTWiVnCubW8EslUEmOk9tB66bCPeRCnoRx/HRTRhIJSLffLI7Kj M3eQ== X-Forwarded-Encrypted: i=1; AJvYcCXb2RQMzkgsUQgi4AFhzjBwu1+iK2XWARR6kRyMB9rLMU84uNT+VQnwFC7VxXBjJwQUhs8DzjazTMcvS+gzeqv4SVZn9mluzP3HE4Z2csM5 X-Gm-Message-State: AOJu0YziFmAVmpel9ZAffsMOzgo4udS/E2khvTJ5m2z06iUfSqFYfOtN 8T2DgFD2cby/Of+NXomxoH3svBxkrxMNAN9iMKK3Bc2/PRu95kJz X-Google-Smtp-Source: AGHT+IEnK16jBONqPu6EI1AggsrrDpNbP1fkcELTlG1wIUqkHXvdRtx4NsDiXlOwAOqcDTPz63EQbg== X-Received: by 2002:a05:6808:159d:b0:3da:ac85:3bd5 with SMTP id 5614622812f47-3db557ef9bemr11482423b6e.8.1722758269943; Sun, 04 Aug 2024 00:57:49 -0700 (PDT) Received: from localhost.localdomain ([39.144.105.172]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ff59178248sm46387605ad.202.2024.08.04.00.57.36 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 04 Aug 2024 00:57:49 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, catalin.marinas@arm.com, penguin-kernel@i-love.sakura.ne.jp, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, audit@vger.kernel.org, linux-security-module@vger.kernel.org, selinux@vger.kernel.org, bpf@vger.kernel.org, netdev@vger.kernel.org, dri-devel@lists.freedesktop.org, Yafang Shao , Quentin Monnet Subject: [PATCH v5 4/9] bpftool: Ensure task comm is always NUL-terminated Date: Sun, 4 Aug 2024 15:56:14 +0800 Message-Id: <20240804075619.20804-5-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240804075619.20804-1-laoar.shao@gmail.com> References: <20240804075619.20804-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 Sun Aug 4 07:56: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: 13752551 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 916AEC3DA7F for ; Sun, 4 Aug 2024 07:58:05 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id E389F10E0CB; Sun, 4 Aug 2024 07:58:04 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="Jra3C+K4"; dkim-atps=neutral Received: from mail-oi1-f170.google.com (mail-oi1-f170.google.com [209.85.167.170]) by gabe.freedesktop.org (Postfix) with ESMTPS id 810CD10E0CB for ; Sun, 4 Aug 2024 07:58:03 +0000 (UTC) Received: by mail-oi1-f170.google.com with SMTP id 5614622812f47-3db1270da60so6745490b6e.2 for ; Sun, 04 Aug 2024 00:58:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722758283; x=1723363083; 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=FVkI7ws3sH8SytGBGHon98LlMkqoO/ooAo5HpqnjZFY=; b=Jra3C+K4300j2uVF4CTupJStLMn2jSgRYHSdrhs3F/2Fm5lcZc2s5BK9fqTIYdw3kC VF7STgAbf2TkUk/c4R/jeSPRLiTxRjrQYy2dAdLbhXlOBd1G+8G7JM2L5DkgFnmChppc KD/C2xey3aJLRosAyHvKxvWP1649nANm8GKxujAuA+ejXo3mR27nKPqsqOYvVdtt8K5H /6I+f6xZEp6hlQw6PDpjdEdhPDkMKN0nBz3AiCS1Yd5RAecGYNF4fs0GHdwTvVwFVyqr JeTIQ3NcDlZTzE3u0sxo+3pZn7zBUXMGeshEvnc7zcZ2AdSYhbra1aP7Nx4mT6k3/ght C1/w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722758283; x=1723363083; 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=FVkI7ws3sH8SytGBGHon98LlMkqoO/ooAo5HpqnjZFY=; b=Ur1xvfgwMtnu9CSrmrtthpihouw70t0v9HDC4LLyWQsRlN9USJcj6435sYnIKhSRrB Zse9VWYLI5kyOXaDZZ/v+klCU6YBKFqXeLzndrqOOZ5yzjqBGG7lruGr5nFsbT05y6Co /cU+/2NIRDvYl0VGIo31E++WxZr0uVmCaDYp1cFeCA0A7A32BebreAykPYX0eSV58ArO oeNnleKGCIUD0/mQ0fkf/ePmlDF+Y3S3KSeSeijJ7CpyGDzUIqMSrAX3aQOfS7+fmj9G oJ+yS9LPFRUc+ixM7Grxv7ibFpRY530kC5pPOlaZ9sW50GfhnaGoDYpKTVX3sG9Mfpnq 9X4w== X-Forwarded-Encrypted: i=1; AJvYcCUibeIlb2CivxaIQOUiy2V1HxkVNgcleapI6hlJjjGAtwV9hO7CQ/c4GxsyxvxhwCtqkjGBbfOsBfX3qL+p8p+yRZ6U0uFyJ5BprmrVWlCL X-Gm-Message-State: AOJu0YxvrGsoSDy59U/J9IDLIIkW3VY3K9arS4Zw+YRIr1/2B3LWsy9p nb8FRZlr/giuGn/O+rSbyxwR274XpU6Tia1wbcXdKEvNdMqXqB0r X-Google-Smtp-Source: AGHT+IGvj96nN6G8dNTQwO1gCnAi0LCFIOOEmeeyoUDneJo+nlGiP3jzVP2As4CYicNVTrPVKHFDvw== X-Received: by 2002:a05:6808:16ac:b0:3da:e587:3e1d with SMTP id 5614622812f47-3db557fc2cfmr11867023b6e.4.1722758282670; Sun, 04 Aug 2024 00:58:02 -0700 (PDT) Received: from localhost.localdomain ([39.144.105.172]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ff59178248sm46387605ad.202.2024.08.04.00.57.50 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 04 Aug 2024 00:58:02 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, catalin.marinas@arm.com, penguin-kernel@i-love.sakura.ne.jp, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, audit@vger.kernel.org, linux-security-module@vger.kernel.org, selinux@vger.kernel.org, bpf@vger.kernel.org, netdev@vger.kernel.org, dri-devel@lists.freedesktop.org, Yafang Shao Subject: [PATCH v5 5/9] mm/util: Fix possible race condition in kstrdup() Date: Sun, 4 Aug 2024 15:56:15 +0800 Message-Id: <20240804075619.20804-6-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240804075619.20804-1-laoar.shao@gmail.com> References: <20240804075619.20804-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 678c647b778f..912d64ede234 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 Sun Aug 4 07:56: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: 13752552 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 3985EC3DA7F for ; Sun, 4 Aug 2024 07:58:23 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id BA7A910E0A0; Sun, 4 Aug 2024 07:58:22 +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="Jq/rr7Tx"; dkim-atps=neutral Received: from mail-pl1-f173.google.com (mail-pl1-f173.google.com [209.85.214.173]) by gabe.freedesktop.org (Postfix) with ESMTPS id 442A110E0A0 for ; Sun, 4 Aug 2024 07:58:21 +0000 (UTC) Received: by mail-pl1-f173.google.com with SMTP id d9443c01a7336-1fd69e44596so33007395ad.1 for ; Sun, 04 Aug 2024 00:58:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722758301; x=1723363101; 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=bS0UkRamrVL5XCR4Rnj9cLPm9Cl+fEpiyHi/lwqSnvY=; b=Jq/rr7TxlxrNR0ucv8scxKPHIpEH2dUnNMdUsq/6ErJ7rpplt2IYtMw+K9aekprA9e m/kUW+x1atZYfik6H90y6y2pw/kmYzpS1urspaURcga5DmqR49h0cAlvecFokiP1mzcj oXqp+hxhBD0yezMbCkdcfJ0OeDZ3jaxcep+ud6zgOxrzD8cneZE4DFe7wOpbTZh6f69h G5Ca/SVy6KT9TUGRu7y4VkJfSI1PocJ52+OxlMCeosQyrVbsuw4RQRID4hNEYadHbSIf oJDBdJQb64rROnvUYqMK39zZUUACverOS5YPrWOw3FEKEwlgzEfX2VmNFSVYzjHwpmAa 6jig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722758301; x=1723363101; 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=bS0UkRamrVL5XCR4Rnj9cLPm9Cl+fEpiyHi/lwqSnvY=; b=Pv262d1QnfYeuUjdl6dMrY2tIXT3j60srEVLnhOSCOsSBJw8rZA0vYMKhZRsX2dj0U LFgv7fiNADSFi1qQYHVkaTgX7ZXIklB3+HcLRKcFidmZvAGJoINgYwprjU0yWzm1PO5B O+rJMIEzlr8f43oERuuTpC5XPnVZkFffnaP0/y65oDTHDEic5CX16g+M8SQytFjzPSxs 6JNBpjU+GfhPbGpLMdzhgxGJhkojQsl35x5yq3NqDznaxvWy3GqpDqxGWSLeOu9Skdf/ QRSTZJMz+CQ9IXzswBd0FFgzAmXGeA5sBuCQJc041agTh7DZF+xt7Z+sxZCF8+JWeapF hjwA== X-Forwarded-Encrypted: i=1; AJvYcCXH9rEOinFarfRKvl0E56YPyKLbnUMtFA622CkCO4ChqIUsDpXDyRglidk6dmO4XCe2Rd6VPX/zthQpWXsQpZ5nSNWcNRVasnkcEXySBqVc X-Gm-Message-State: AOJu0YzKRivY3VzbGnWbtX94R86kQHF7hHXR3oomB/6nYvIZVXubr26Z uyyABY3xAHnIhJi4EZmmJWx/gO2vmYu7vSHFUlfFzPA3p7LmTscY X-Google-Smtp-Source: AGHT+IEGtCQCVMnQCKfJ0+BIkhT35+DpDH9a54ylBrsi080HAExdi6zY8c1ZJxWy2ZwMLSFkL3dmww== X-Received: by 2002:a17:903:1cb:b0:1fd:7ff5:c673 with SMTP id d9443c01a7336-1ff523ef0dfmr156564885ad.2.1722758300691; Sun, 04 Aug 2024 00:58:20 -0700 (PDT) Received: from localhost.localdomain ([39.144.105.172]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ff59178248sm46387605ad.202.2024.08.04.00.58.03 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 04 Aug 2024 00:58:20 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, catalin.marinas@arm.com, penguin-kernel@i-love.sakura.ne.jp, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, audit@vger.kernel.org, linux-security-module@vger.kernel.org, selinux@vger.kernel.org, bpf@vger.kernel.org, netdev@vger.kernel.org, dri-devel@lists.freedesktop.org, Yafang Shao , Simon Horman , Matthew Wilcox Subject: [PATCH v5 6/9] mm/util: Deduplicate code in {kstrdup, kstrndup, kmemdup_nul} Date: Sun, 4 Aug 2024 15:56:16 +0800 Message-Id: <20240804075619.20804-7-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240804075619.20804-1-laoar.shao@gmail.com> References: <20240804075619.20804-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 912d64ede234..2c5addabd6f7 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 Sun Aug 4 07:56: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: 13752553 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 917CCC3DA64 for ; Sun, 4 Aug 2024 07:58:39 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id F0C0210E0C3; Sun, 4 Aug 2024 07:58:38 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="W6nivcY/"; dkim-atps=neutral Received: from mail-oi1-f174.google.com (mail-oi1-f174.google.com [209.85.167.174]) by gabe.freedesktop.org (Postfix) with ESMTPS id 4F05510E0C3 for ; Sun, 4 Aug 2024 07:58:37 +0000 (UTC) Received: by mail-oi1-f174.google.com with SMTP id 5614622812f47-3db2315d7ceso5601710b6e.1 for ; Sun, 04 Aug 2024 00:58:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722758316; x=1723363116; 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=hHFsexRAvS3+Twdq3ZGpnUTD4jZRkYjFMffxIhD4B7E=; b=W6nivcY/2Qn+yajjHWxxeoDoVl86XQIPqaylEk7zhrgp21pxOyPcdIYo+9H7KSvgVi n72tBXifE0oGcwlMOrke5w/XIWUku64SRTLkOqmpIMc2AXvGm0MkiMHtnvYD/YiKr+hR M94OWbVwtKRH3tz71CUrWo+6p3sh+hZykiPFb+5RtalMVhkLYY8mqJNpy8b40jfnxA6b fgv2sq4PldCO0Q0nKTueN4CRCvbBYsvDAMEijNigu6fCGeNgU0++h2ZwPRYQfK0tW+/a ZzToltCDgqyR3ut1GhIy6WR3BswBkFO2zcHyQeNI+/kqFbbBDCx7XY/9MTZ1MSOYRzxz Kghg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722758316; x=1723363116; 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=hHFsexRAvS3+Twdq3ZGpnUTD4jZRkYjFMffxIhD4B7E=; b=izcwOX0AnxmzO/vtL8Qukfe+8+1lGKcbuOVmjE/izQVp0CCQMd+e3IOsAwf453Kw+G hexUGs1DHzZjll4ZbyJJwMsCjeofRfzvUqw3aRYuja80b1yqw8va8sGp8BGAnhrp1uqZ cA2eeOIzZtOAPv+QetjZinqXdrSAZLqlgNJQVYkq85iX4hles8AnKd/aBr/o966Xbc3t KClWz5ZcadSlb8yW9WjsgWK8bYSgpwIcLzsudWGuF+6yq5UnQpAy/QtuZttZWAGnQ/Xd IHD6kkd9roUn3jbvy7BGMklLYgtt0kS0V79/gh9aSa9Zk/qU7gfZVYTEOXlA8AuS9Y8j 0PEQ== X-Forwarded-Encrypted: i=1; AJvYcCWj7hKIgvdXhsYr/S5MGdf5x+LEWCQnVw/TvrhcrSmVdZLR0h1SoIDM/lWzF7OfyC5rJX0T4ngbUSs82VXhia2fRGP1LoAUYILpU8dWT99A X-Gm-Message-State: AOJu0YwbLhAVF+jd0tb4lRiyv8vvixnDRbTj1+fDQh76FPQ1ly7nmQlh yogN10i8TrGqQWF7zTqJVDYrjYM6AqMskhzjNqWdZ5OziuZoZBKA X-Google-Smtp-Source: AGHT+IEQdsxKAJTw4i+uNNPkbgE3NiIW5Ir2yWohAneqMp4vDkArZBqW84U9DpBffvNg6sRs26e7Fw== X-Received: by 2002:a05:6808:212a:b0:3d9:2562:7541 with SMTP id 5614622812f47-3db5580f273mr11989986b6e.24.1722758316365; Sun, 04 Aug 2024 00:58:36 -0700 (PDT) Received: from localhost.localdomain ([39.144.105.172]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ff59178248sm46387605ad.202.2024.08.04.00.58.21 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 04 Aug 2024 00:58:35 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, catalin.marinas@arm.com, penguin-kernel@i-love.sakura.ne.jp, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, audit@vger.kernel.org, linux-security-module@vger.kernel.org, selinux@vger.kernel.org, bpf@vger.kernel.org, netdev@vger.kernel.org, dri-devel@lists.freedesktop.org, Yafang Shao , Masami Hiramatsu , Mathieu Desnoyers Subject: [PATCH v5 7/9] tracing: Replace strncpy() with __get_task_comm() Date: Sun, 4 Aug 2024 15:56:17 +0800 Message-Id: <20240804075619.20804-8-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240804075619.20804-1-laoar.shao@gmail.com> References: <20240804075619.20804-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 __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 Acked-by: Masami Hiramatsu (Google) Cc: Steven Rostedt Cc: Mathieu Desnoyers --- kernel/trace/trace.c | 2 +- kernel/trace/trace_events_hist.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c index 10cd38bce2f1..985d2bf2bbc5 100644 --- a/kernel/trace/trace.c +++ b/kernel/trace/trace.c @@ -1907,7 +1907,7 @@ __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu) max_data->critical_start = data->critical_start; max_data->critical_end = data->critical_end; - strncpy(max_data->comm, tsk->comm, TASK_COMM_LEN); + __get_task_comm(max_data->comm, TASK_COMM_LEN, tsk); max_data->pid = tsk->pid; /* * If tsk == current, then use current_uid(), as that does not use diff --git a/kernel/trace/trace_events_hist.c b/kernel/trace/trace_events_hist.c index 6ece1308d36a..721d4758a79f 100644 --- a/kernel/trace/trace_events_hist.c +++ b/kernel/trace/trace_events_hist.c @@ -1599,7 +1599,7 @@ static inline void save_comm(char *comm, struct task_struct *task) return; } - strncpy(comm, task->comm, TASK_COMM_LEN); + __get_task_comm(comm, TASK_COMM_LEN, task); } static void hist_elt_data_free(struct hist_elt_data *elt_data) From patchwork Sun Aug 4 07: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: 13752554 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 E33F0C3DA64 for ; Sun, 4 Aug 2024 07:58:54 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 6549D10E0D6; Sun, 4 Aug 2024 07:58:54 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="FHT5ay+7"; dkim-atps=neutral Received: from mail-oi1-f175.google.com (mail-oi1-f175.google.com [209.85.167.175]) by gabe.freedesktop.org (Postfix) with ESMTPS id 444AE10E0D6 for ; Sun, 4 Aug 2024 07:58:53 +0000 (UTC) Received: by mail-oi1-f175.google.com with SMTP id 5614622812f47-3db130a872fso6647051b6e.2 for ; Sun, 04 Aug 2024 00:58:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722758332; x=1723363132; 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=Iy8s8HUuCpYYzqYVYDLibKnXKIG+UtFNyRBbbBdMpkI=; b=FHT5ay+7zz7IIMKjZC7MdQWmxuO/zD/q3PKmJ1H3xMsm0yv0nQLna6PdCSu7AMlrSh joJ8MUzR5zEZxfnb1I6fpPx604ETzd0eSnm+AMrR9BMLuv7VuKKrg5cvpyIMn5WtZ9DO md0iOXvZESIMT3eFual0nm60oek+FdM0c8/AWRce/h4B0Z0dWqexY6vy73SfFVYfyY/+ a2SWt3uRQYZsygdmxk6dYSKnlva8xcgLa5CrfdQoW8R3o1Wl+l35orGVeC342hdfckeK CFcGPeNKDW2R2z3Jbr/qq6uFy4ol6FHI+p8r1OKZS9cop1dDneGMZfVe+OFFodCKPGrG LFag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722758332; x=1723363132; 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=Iy8s8HUuCpYYzqYVYDLibKnXKIG+UtFNyRBbbBdMpkI=; b=EXkOKwthoynKsxH14s2RN3ahgdXn28QJJtXlPeIWtFYihfsBbuQABhQCTBkW7Cvalp kIV3Onz1dsEpbbM+DyaoBWozMaRgcaqh7AIwMhvotSEnTh5JV0LzszVsCqQzyhA86mQv xsG/MdKDQacXIpwujNWMcYABj6L/umIO1n+NppoEDACKxo5cI8FEN3+8NUgk8sDOoexW 0Mt7P3OULGnCmsjTwH5udayeaFwPTb4wBwUqQQf/Wd74TM27CM9fkfn4KlDdsZDxcUQx YJi3LD0nicEsrVEdWhhx4ZvmWZYl+U23ryWs388uF9X4itGtCiAJEEOwx2hxxtTvdlVR WK6w== X-Forwarded-Encrypted: i=1; AJvYcCWsHwvEC3moV0gQtmnUyoYXB5zF6JO3qHHetVxeIN2QMVAdnJLZ27PO0b8Da0nHSSsJO6E+XHE+23k6a75b722mlg9ymlRLQbsHRcJ154j7 X-Gm-Message-State: AOJu0YxMdPo1tvtxv/9QJ5XQll7D/7rc6E4rSFPqBqS+Sz89b9BQ1oKX +foDQLPX/btxZnQgMwFolzeSSBEXWKN3+gBvEAvOMLcWezA2+eFq X-Google-Smtp-Source: AGHT+IEAdxuBVNyLOuAyS4g3ON7ORD29wxk4r6CpWYYml3q2XeTU12w4OJdy/NnFgIlYXJGGtsOOlw== X-Received: by 2002:a05:6808:1984:b0:3d9:9e78:420c with SMTP id 5614622812f47-3db5583270amr9335216b6e.38.1722758332352; Sun, 04 Aug 2024 00:58:52 -0700 (PDT) Received: from localhost.localdomain ([39.144.105.172]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ff59178248sm46387605ad.202.2024.08.04.00.58.37 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 04 Aug 2024 00:58:51 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, catalin.marinas@arm.com, penguin-kernel@i-love.sakura.ne.jp, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, audit@vger.kernel.org, linux-security-module@vger.kernel.org, selinux@vger.kernel.org, bpf@vger.kernel.org, netdev@vger.kernel.org, dri-devel@lists.freedesktop.org, Yafang Shao , "David S. Miller" , David Ahern , Eric Dumazet , Jakub Kicinski , Paolo Abeni Subject: [PATCH v5 8/9] net: Replace strcpy() with __get_task_comm() Date: Sun, 4 Aug 2024 15:56:18 +0800 Message-Id: <20240804075619.20804-9-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240804075619.20804-1-laoar.shao@gmail.com> References: <20240804075619.20804-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 __get_task_comm() 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 70a0b2ad6bd7..fa3a91e36ba0 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); + __get_task_comm(warncomm, TASK_COMM_LEN, current); 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 Sun Aug 4 07: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: 13752555 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 62DD0C3DA7F for ; Sun, 4 Aug 2024 07:59:10 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id E184D10E0D7; Sun, 4 Aug 2024 07:59:09 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="aCJCLNtG"; dkim-atps=neutral Received: from mail-oi1-f172.google.com (mail-oi1-f172.google.com [209.85.167.172]) by gabe.freedesktop.org (Postfix) with ESMTPS id A5F5710E0D7 for ; Sun, 4 Aug 2024 07:59:08 +0000 (UTC) Received: by mail-oi1-f172.google.com with SMTP id 5614622812f47-3d9e13ef9aaso6621587b6e.1 for ; Sun, 04 Aug 2024 00:59:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722758348; x=1723363148; 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=leoSzvc1irTIRY4t+RZAVg9wXL8E4KK0Ycetp1tzeI4=; b=aCJCLNtG1YUZpwXslrWttIeBQOacOPfNc8/UlTxa7mbxQB8cg37XYei6JcpQ2EBRkQ Q07NSkv3r84FNZMH4LX5wzwW4m3HrWzNawhQtPuzCpH/9RpNIilfi8FBN4yG1Gjl5ld3 MB6G3yMZkfZq0pOXtt42m2aL92rHXv9uvlHfNiVWqXKVWH9K4MTzcpi6emJiG/W5K/Hn 0bZrkB0PAVNF2dqB05vKFikAZGPTwPDy7Gd7LzgIi5hROZ1jGO5YcMWDeX7oebE/wRJD uU/8M/dJA5B6/fupxeNY/UoOOojcQWoLt4SJEM5E4fxry8MzaBs/rkwTAMdVK1mUR2gn jemQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722758348; x=1723363148; 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=leoSzvc1irTIRY4t+RZAVg9wXL8E4KK0Ycetp1tzeI4=; b=R+nSlTlY/U5Mp06TXERrZyLqhdqxblRnqgl4mOMyrFfYfdoCRVIWF5EeNhBsvdAZbK 4yGE7tmon3wxJRL/hQ5VIqgwUbGvNqguW+Nr68co9ZdmPAC3jXPacwUqdcSlvzSBJZkv FKLHpIKoHZCdk6MBVUhG+LApGXsy1qZFMknAAx80Z3TtkNdlnPxwXlivu6xqodKJBdEr PcjvEQRcDtYaIaCENhofhY7j9rwxcMnsnEMC/ouQgCtMVVsk9iz8Go26ucH/CaHHBjoJ d7JCQUJT8mgpvnPn+btTzMCSleJVHd8uCyiV+I0FDxIeynB5Kn0amjuJlhZQsbisMhjr yAng== X-Forwarded-Encrypted: i=1; AJvYcCUug9NZLttWKRnGXHEUc8sp1A6G5pP5KAz9QaWTZN8DIowq43ZAnsTFT4cRKdUpgMa/cya5X11ncmCpKqTxUVyCaXBXzhONq5IN6V9sq1xO X-Gm-Message-State: AOJu0YzpQOAmk/olz0kNKVSKj6KGetZs3QkuWCki0ZDFuOBbHjFwq9qU i+lcpBJHLtF9XyeNPvT7rdKwXN91bR1n/RGx3WLHOpoVjy6hqmhR X-Google-Smtp-Source: AGHT+IFvxFD9i5t49STUUYBZ5sy63mPUTCqzaYfVU1LOK5V4jv+UubPbJD8tYCgFeYVMewc026EG+Q== X-Received: by 2002:a05:6808:10d5:b0:3db:25f6:a62f with SMTP id 5614622812f47-3db5580f909mr13121118b6e.28.1722758347849; Sun, 04 Aug 2024 00:59:07 -0700 (PDT) Received: from localhost.localdomain ([39.144.105.172]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ff59178248sm46387605ad.202.2024.08.04.00.58.52 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 04 Aug 2024 00:59:07 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, catalin.marinas@arm.com, penguin-kernel@i-love.sakura.ne.jp, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, audit@vger.kernel.org, linux-security-module@vger.kernel.org, selinux@vger.kernel.org, bpf@vger.kernel.org, netdev@vger.kernel.org, dri-devel@lists.freedesktop.org, Yafang Shao , Daniel Vetter , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie Subject: [PATCH v5 9/9] drm: Replace strcpy() with __get_task_comm() Date: Sun, 4 Aug 2024 15:56:19 +0800 Message-Id: <20240804075619.20804-10-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240804075619.20804-1-laoar.shao@gmail.com> References: <20240804075619.20804-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 __get_task_comm() 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..25262b07ffaf 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); + __get_task_comm(fb->comm, sizeof(fb->comm), current); 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..163457a6e484 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); + __get_task_comm(e->comm, sizeof(e->comm), task); e->pid = task->pid; } rcu_read_unlock();