From patchwork Thu Jun 13 02:30:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13696493 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 7FC4CC27C4F for ; Thu, 13 Jun 2024 09:27:00 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 0D3CE10E9C9; Thu, 13 Jun 2024 09:26:58 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="jhwipbGH"; dkim-atps=neutral Received: from mail-pg1-f175.google.com (mail-pg1-f175.google.com [209.85.215.175]) by gabe.freedesktop.org (Postfix) with ESMTPS id C31D710E960 for ; Thu, 13 Jun 2024 02:31:11 +0000 (UTC) Received: by mail-pg1-f175.google.com with SMTP id 41be03b00d2f7-6c53a315c6eso393872a12.3 for ; Wed, 12 Jun 2024 19:31:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718245871; x=1718850671; 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=Zt6Zo0BQwE9mYz3Q0zCJOWUGA/XKEFxfC7Idotg6GZ4=; b=jhwipbGHqhe0gib78A5WHgcMsExTtIxJCyvJXMyyv82LZ/qgojA5OXKR3Hg8u6FYMC vO5GICsi0Otf+hPML69UHGs21GvCLA9iBBoj/k4RBFZrnTc0kQDfrWAerTGr3AhmqhCn MfPBW5LZSkOVYuA3boXQiBy1AoOPqxHldk4JEfnh9NRQDU8BukshGaXJ9Z7566n68lkx dkJkQ6tcfueO61u/aSsMCPMRqHcU3X5/+rQ3u1kkQ6Z0E+Kx5ghO+AdvA7G3JOgE2/Ek YcuyhYmvrx93ko15K/FymO3IHDt3v/mXBSejLSWjl1S8vTIzc1NZpRjJ40Kb9CG5evHF 6skg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718245871; x=1718850671; 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=Zt6Zo0BQwE9mYz3Q0zCJOWUGA/XKEFxfC7Idotg6GZ4=; b=PNJlsNlH4jqIgRWG0s9a+YnZ49L0pxkneUhulBaCh5R2nhXiM0gq5mYyCYuFoyDfaZ TENP0jG1gVsbTxJerINrhzBwXY80ERtH0+dSzgxLK9RfTAu1OgP00UbKb8dHzljRN7La hYjTrotJhOwRTBOz3vZruESg4AuBxKFZzePXvmLEwykYYhJunlfetd/2oIB1XRiRRupd amSkjq8IQm1p2x33iRFtFAEd5tc7/Vx4FrBC9FpvcT908teHtoBCAbb6w73URfBqKMNE GdiR4OW7pLZfFUzu8pN5bXKAvAhecHRK1zEyLDrqSx2Wzwl6f2vG7sG1qDRNMvOhXJdI ZvDw== X-Forwarded-Encrypted: i=1; AJvYcCW+IrMWAuyjwGQZlo8DWTFVkJnJ38+6vHjfAQAnyHy2u9zybpq7/d9Eq+DYCaDmerwVX2RPxjwPKLQx11bScSoudhtL0882zJVEfUCXyP/4 X-Gm-Message-State: AOJu0YxPaSq8QtRa9Li1gKdi0EfPjFj1YwIsf2BDYjU1fa0iXxptYgDL S1RmRWcoVXb7wzTAxaxGxeNpj/vKT5BHbzKVx6YNSkzc6c2ReutO X-Google-Smtp-Source: AGHT+IGepHvi+lKqMo1GYaife0L6fUJVRPiktMyHkXeHwR+7tr6qOJAeLHRN5Lin69VRxAqus5DeJQ== X-Received: by 2002:a17:902:ecc1:b0:1f6:6ef0:dae4 with SMTP id d9443c01a7336-1f83b5f3fffmr43616545ad.32.1718245871101; Wed, 12 Jun 2024 19:31:11 -0700 (PDT) Received: from localhost.localdomain ([39.144.105.92]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1f855f4d159sm1755695ad.289.2024.06.12.19.31.03 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 12 Jun 2024 19:31:10 -0700 (PDT) From: Yafang Shao To: torvalds@linux-foundation.org Cc: ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, 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 v2 01/10] fs/exec: Drop task_lock() inside __get_task_comm() Date: Thu, 13 Jun 2024 10:30:35 +0800 Message-Id: <20240613023044.45873-2-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240613023044.45873-1-laoar.shao@gmail.com> References: <20240613023044.45873-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Mailman-Approved-At: Thu, 13 Jun 2024 09:26:31 +0000 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 40073142288f..fa6b61c79df8 100644 --- a/fs/exec.c +++ b/fs/exec.c @@ -1238,12 +1238,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 61591ac6eab6..95888d1da49e 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -1086,9 +1086,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 Thu Jun 13 02:30:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13696492 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 397B8C27C75 for ; Thu, 13 Jun 2024 09:26:59 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id E753510E9C6; Thu, 13 Jun 2024 09:26:55 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="WrxJFv32"; dkim-atps=neutral Received: from mail-pf1-f171.google.com (mail-pf1-f171.google.com [209.85.210.171]) by gabe.freedesktop.org (Postfix) with ESMTPS id CC18D10E960 for ; Thu, 13 Jun 2024 02:31:18 +0000 (UTC) Received: by mail-pf1-f171.google.com with SMTP id d2e1a72fcca58-705b9a89e08so480717b3a.1 for ; Wed, 12 Jun 2024 19:31:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718245878; x=1718850678; 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=j1cpGZH9ekrwOk4ggaybwp6iXeGg5lCIICWIGBwiSB0=; b=WrxJFv32gohLUCv/zA/KhLg6jimn/yBSTPte2T09Do4aanr/WZHTRdRIBS0uamq7PM pHMHLTvuz6OmTlP+F33ZF4dX2Lo1yzmn5G7NJ71eS/K98vS/LdGDkhoo0HrkN4kR92lr dVx6EL9aZBR4c5o9l+BlQ7D3xtO6lUVzK7tbC1nkNSEM58JpszP5MSs5QR2oMERZt1xE HGxcB4YjuZSicfX6ohLb8WextZyyGnur/3ijZ8O2XiKKcg0BTdJn/UgeKVoPOQie5299 8Ep2dTgwOBFbdbIeso402SNBYU1IDaYwlrz1kzaHyMRN/YDCADkIDCkrcjW6pnup2kl4 H0dw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718245878; x=1718850678; 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=j1cpGZH9ekrwOk4ggaybwp6iXeGg5lCIICWIGBwiSB0=; b=RY04MghA+nkj3KqZ1UriEQJl0c0h6eZ3W3HPetziUPVbk32pIa33VhYtgouvlWyRP5 Ah0XbTtPdv+l5otXxiaHqMLmoomNX9CME3TSmxObAG0nCYWD1mbbaDZaPkMOkkuzbpWC swr/AhDoRmL6XS32CBVgPPDs6fJ0wyjGoQRNEKlcFF/dQisHQC4MSNCaAAdsaXHC4C42 KxbhwkulmTmzSISWVzPNwm1kdAiavNjTSxFJcakQg6vFDSqSSvICYT9Hbrtwq2rKBUUH +w2dQQrGltTfak87+8WqM2nts9s3rEV1RMjryOQIpojwH1A4KIPfw7Bo/mUpA3hCSo7U pFqA== X-Forwarded-Encrypted: i=1; AJvYcCWJov17EbfWxN5TeH+gsSmDElhIx7x5njfYvTRqcRrsR6DeA8qwW+ZVyXyd0JkUgkxK2M2WhyiYy6cF4M3ckRK0H9Ty9cOOq+Ecjvj2NxLK X-Gm-Message-State: AOJu0YzHa3yII9biHb43/TtbmpBPjCO/RpzIA6lt3qfDfrcMyjFC6pAJ pkDk/SubF4+uEVDZRe57r4iiwr+R1Pn5+w9c9I/PbNiPoDPLr3ww X-Google-Smtp-Source: AGHT+IHxE49dVlqeBIraoeODhxKMszGj4KYKBCDFAoiQKvyi2cHrcHMBy4aCqKmXdfgNqTCtDPqmtg== X-Received: by 2002:a05:6a20:3948:b0:1af:8fa8:3126 with SMTP id adf61e73a8af0-1b8a9b773c5mr4249289637.6.1718245878051; Wed, 12 Jun 2024 19:31:18 -0700 (PDT) Received: from localhost.localdomain ([39.144.105.92]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1f855f4d159sm1755695ad.289.2024.06.12.19.31.11 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 12 Jun 2024 19:31:17 -0700 (PDT) From: Yafang Shao To: torvalds@linux-foundation.org Cc: ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, 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 v2 02/10] auditsc: Replace memcpy() with __get_task_comm() Date: Thu, 13 Jun 2024 10:30:36 +0800 Message-Id: <20240613023044.45873-3-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240613023044.45873-1-laoar.shao@gmail.com> References: <20240613023044.45873-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Mailman-Approved-At: Thu, 13 Jun 2024 09:26:31 +0000 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 Thu Jun 13 02:30:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13696488 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 B4508C27C79 for ; Thu, 13 Jun 2024 09:26:53 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 39B2210E9C0; Thu, 13 Jun 2024 09:26:52 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="iGPLNBTc"; 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 416C310E960 for ; Thu, 13 Jun 2024 02:31:26 +0000 (UTC) Received: by mail-pl1-f171.google.com with SMTP id d9443c01a7336-1f4a5344ec7so3676335ad.1 for ; Wed, 12 Jun 2024 19:31:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718245886; x=1718850686; 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=nQwUpm7TadSQyuNlGOfh8JWtJNghSVDBcHtu3NLgdvg=; b=iGPLNBTcZKkvrleH1jBmCWqma/g+/WHNAXNdmqaokoN87UjQ0OL9apPr/Gq0mG3jlT grF3DsonNMD/MxJz+5P2CMzJSkTO4Vx1PAiMAnBSHcbA0OS0RSOJTir0dozCfxHpURO8 /QEgNzZa81IWkTxsEZF1UjuVtiFHmDDwguqbQLWKz9dZaeE4BMdJa+ATbIcmpnHdyafG VrnHZaWVeqEf8flGS0GcRoNQtHLmhvlqJNVtec0y/DKSGA4i0GFvloqGNYZ0ZiEbY3mw pM8Vg7rIpA/GFnFn0891N6hGbyVPp2FAokFdjNouuULpp/eo4vWezsH+QVxCH7x9dyZC n80g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718245886; x=1718850686; 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=nQwUpm7TadSQyuNlGOfh8JWtJNghSVDBcHtu3NLgdvg=; b=w7MzyaiQk8P8TK+bIrQVluGvM5+fT6qSA3jfFuQKjt4hIFmXjR1YncDFlUT9u0L+ys X1aq8z+GSNsvgLY0EtKjojzSk8kPeBNOT6WNjBRC2l6a1NQJCIU0bwfiQp9ARpf+Mtnq 25l9zo8dHIfYOLm9FUpsE9YnwlgZ7GZK1fHLNVsaumqeI1Xlfw6QJ2Yjrnifz/m27BPa FqrFl9yOOEkvZXH2TvlRzw+Vdg0zk9ttWOrYVfghXXTVmA4v+Ri5rVF0nX4V9Z8T5YM9 2teBsVy2CsZMQA5Or+2tCJotX4zqstDG+bq/9XU6GMoXusl/iyd+fpdGa6VQY9oTnWgH oDYQ== X-Forwarded-Encrypted: i=1; AJvYcCUAlsLwkRDkPr+56eP2WcdBDfE7VJ6rzIrS92jgh+NyJ7hEt45V8nGuzUmYYkO9h4K0zQcsfcNIncN4VYQvHpriX2HpBXietPxhJDLTCpWC X-Gm-Message-State: AOJu0Yxsnrct2PLdCQkeEFDEC5TjLD6FgETquDTAAy5c+2qPITruzQDr Pfuf0LfShq+LMci/ai5AdszS9SlNk9aVX2vmypUgTEyMdfgjqwxM X-Google-Smtp-Source: AGHT+IGMO7B1LQEga8czanZyIFvbKD26XnoiSA9hTxuO951zhYSmXnFwAhLQKN8VIXez5ya7Dmvfhw== X-Received: by 2002:a17:903:40d2:b0:1f6:ed6d:7890 with SMTP id d9443c01a7336-1f84e2cc0b3mr21158875ad.16.1718245885659; Wed, 12 Jun 2024 19:31:25 -0700 (PDT) Received: from localhost.localdomain ([39.144.105.92]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1f855f4d159sm1755695ad.289.2024.06.12.19.31.18 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 12 Jun 2024 19:31:25 -0700 (PDT) From: Yafang Shao To: torvalds@linux-foundation.org Cc: ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, 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 v2 03/10] security: Replace memcpy() with __get_task_comm() Date: Thu, 13 Jun 2024 10:30:37 +0800 Message-Id: <20240613023044.45873-4-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240613023044.45873-1-laoar.shao@gmail.com> References: <20240613023044.45873-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Mailman-Approved-At: Thu, 13 Jun 2024 09:26:30 +0000 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 Thu Jun 13 02:30:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13696495 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 10D81C27C75 for ; Thu, 13 Jun 2024 09:27:19 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 4B1C710E9C5; Thu, 13 Jun 2024 09:27:18 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="k0wi33/I"; 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 878A210E960 for ; Thu, 13 Jun 2024 02:31:35 +0000 (UTC) Received: by mail-pl1-f181.google.com with SMTP id d9443c01a7336-1f6a837e9a3so3691165ad.1 for ; Wed, 12 Jun 2024 19:31:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718245895; x=1718850695; 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=c7QH8g7nY2INvVrL4NU75ftNhN6FL7HL04IiCAjwJmE=; b=k0wi33/IvQ6Ogl6coTEfiLbKddo1L7hBeFs33Mw7XaqyfIghjMfd/zy2oFNkOgJARx GrvNBtxGoRehySfYg07gJN1qVmVHDLi450zI2qbqSGYezsrahFDm9qY8f1WzQzKerGwX u+4eJLa/uhwKhsOWN/4ndRaH22ZQcYt7jybHvwCsZ3oMghdxtyEWKvGO/rFbA8fn4GqR 3ZfiY3jRbOD+yIRIpYvOt7PlslJDQFChkeZFbLiRQ4dFlL/ZVlUFRvKJ8jdvGiT/JTqj TwgSaOaoOpaji6LDJtAIhspnlBBCJ9raZPtOlT+j1VAGVqTPsG8QUeS/3QKgPkYkI36L o4LA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718245895; x=1718850695; 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=c7QH8g7nY2INvVrL4NU75ftNhN6FL7HL04IiCAjwJmE=; b=msi2rUdu7osfWR5xAHRL9ykI2TditUBtGYx7Hmx6grx2BZyvVIJHqNB6W7PBww5TbJ O1UQlilYk8qclxUURBGLsayfW2ny/3f1el3Npg03F4KyU2WCBxJDbao2sewkBIADYYpf jHFXOtFlFfdO8gu+hkbK0iWvQ8ERIny4Ecwjzk1lMS84yidamMfhQEkeMrbFrlIanIN1 ImWPKy7HWxM9PhsDr8yFB4gQTP0top/A7L3e/SIiB2H2V1gQcEhbgofexIhsPmvZaL42 oX48zm/jTBVGcUmPgqTxjH1IK8Q1DN7tHkXQdeJn8s6x3HxWPhv9XiLCcY3il9lw3HHp ve9A== X-Forwarded-Encrypted: i=1; AJvYcCUeTu+IT5Iko5HtTwjOdRAC0FcGyX0mwt0nRLZf4yfjP2hpnWwg60qJwfyQNZPOYQnoocQ+tLKN7jAAUkqJG+SKfoejZ6jDsghDflrDCZkz X-Gm-Message-State: AOJu0YwIt7IlgNspv6/HHuLgIBXHv8GZkfaghxKMXKzGwmu56FPgwIK7 g64Soh8N2liRnB/U5KzxwhHDtrahGhv2i8zJDVIRa/tH27jHfN6X X-Google-Smtp-Source: AGHT+IFO0RiQEuS+ZZx5NhUKFkro4pSWALq4fQpBCAuUBHe6UDJpEGOEOlucSptFPDnQxh/E7HkfRA== X-Received: by 2002:a17:903:2283:b0:1f7:2051:18ba with SMTP id d9443c01a7336-1f83b565dbbmr48444305ad.14.1718245894762; Wed, 12 Jun 2024 19:31:34 -0700 (PDT) Received: from localhost.localdomain ([39.144.105.92]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1f855f4d159sm1755695ad.289.2024.06.12.19.31.26 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 12 Jun 2024 19:31:34 -0700 (PDT) From: Yafang Shao To: torvalds@linux-foundation.org Cc: ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, 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 v2 04/10] bpftool: Ensure task comm is always NUL-terminated Date: Thu, 13 Jun 2024 10:30:38 +0800 Message-Id: <20240613023044.45873-5-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240613023044.45873-1-laoar.shao@gmail.com> References: <20240613023044.45873-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Mailman-Approved-At: Thu, 13 Jun 2024 09:26:30 +0000 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 Thu Jun 13 02:30:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13696490 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 25CFDC27C79 for ; Thu, 13 Jun 2024 09:26:56 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 839B810E9C2; Thu, 13 Jun 2024 09:26:53 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="ZdLDQtOU"; 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 8E18410E960 for ; Thu, 13 Jun 2024 02:31:42 +0000 (UTC) Received: by mail-pl1-f170.google.com with SMTP id d9443c01a7336-1f6f1677b26so3885875ad.0 for ; Wed, 12 Jun 2024 19:31:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718245902; x=1718850702; 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=Ei/8xwKAZ1rE7ZNdSGJjlup34y97vbtFvnVfEPyjJ8Q=; b=ZdLDQtOUJ0agHZEJOv2j8d9QZqBT/LuQu434BHlJtEM3uGmfAgREyKequWRInlDI8I DdYgP7ljdJ7Fr+GvLWyxl1p5WYLoYUdwJE9edRwn1yQkx108zbB1hKMkIZItvXkWtD7M 3/j2OVp56Qb8VxZTbJLWlOAuLCGLsz5+hnPmEXxmGQnjLOTriUQW0zJjLNwg8PFIj6vA NnixNejEOiWc+tkgfjQ5xFwESYEBgbyLn50kyjAZDCSodI/SugpGVU6ryLHjcXNlsf+F kTRtES5p6HIgIDViPspD0kay7dlSQDDloRZL69q2H0iLWWNcYBMi2iZF2acVfmOhO3kM fvdw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718245902; x=1718850702; 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=Ei/8xwKAZ1rE7ZNdSGJjlup34y97vbtFvnVfEPyjJ8Q=; b=ZokcTLM2Bo1W4YKfGU8LOZw/4KeM2FZ6fkzAxCGHInjTsc6SyY/eDlG1P/VmfZC/V+ 9UsUGGnuVu6wj5H4XcLZjKk81z9kJcJcvpJu/SyemqaXLQXuESp13kGO89Wrupo8TSos 0BQlBTiMxUDTIoMhr7jVqkzKMlZpnQXF2tnh0VPiADBoVxDbg4eJC2SdeVSaeYwol1ID mPFighvAzXeHF9S8EOX0QQlyrLjmKREjPsP19p9J2WdGj/4B5Ac6YUODlHEUby9bebFi D3cEmBSW6haa1ekBjP20GdQV29Z3EN6brs0IAcGwkUF3ydzMhWo2HC01QkY5qwvwgrsH iqRA== X-Forwarded-Encrypted: i=1; AJvYcCWH5eilgIhpAUnGIihE7bFnGg9czYPx75xl+c3/DMEw/oGqFCCYUSCdBJXLDRWSWhBQCIR/VXiyCnvY1Ph4h3FO2O1VEeJ0gr6k2gXek89O X-Gm-Message-State: AOJu0YwIYQ7HmZ5M7C8rRJscwaL1n2KcX/YW+2+GlPDO5jIwKQjF4iM5 9JCwbI2ycZBWbfYD0b5G47zph3XjlqWzKRFS+zAJoW2SVorsv/4t X-Google-Smtp-Source: AGHT+IFIaVarDVsj1gEbMfPNFvaRxIMZj6V1FUrejF3TL24xnCXwEw+tDOS2CgmUK4jtVsLBUQ40Ug== X-Received: by 2002:a17:902:db11:b0:1f7:1d71:25aa with SMTP id d9443c01a7336-1f84dfbb4cfmr20547645ad.6.1718245901789; Wed, 12 Jun 2024 19:31:41 -0700 (PDT) Received: from localhost.localdomain ([39.144.105.92]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1f855f4d159sm1755695ad.289.2024.06.12.19.31.35 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 12 Jun 2024 19:31:41 -0700 (PDT) From: Yafang Shao To: torvalds@linux-foundation.org Cc: ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, 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 , Andrew Morton Subject: [PATCH v2 05/10] mm/util: Fix possible race condition in kstrdup() Date: Thu, 13 Jun 2024 10:30:39 +0800 Message-Id: <20240613023044.45873-6-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240613023044.45873-1-laoar.shao@gmail.com> References: <20240613023044.45873-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Mailman-Approved-At: Thu, 13 Jun 2024 09:26:31 +0000 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 | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/mm/util.c b/mm/util.c index c9e519e6811f..3b383f790208 100644 --- a/mm/util.c +++ b/mm/util.c @@ -60,8 +60,10 @@ 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); + buf[len - 1] = '\0'; + } return buf; } EXPORT_SYMBOL(kstrdup); From patchwork Thu Jun 13 02:30:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13696484 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 25DBEC27C4F for ; Thu, 13 Jun 2024 09:26:39 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 0F0D010E9BF; Thu, 13 Jun 2024 09:26:35 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="A8wGD3NY"; dkim-atps=neutral Received: from mail-pg1-f170.google.com (mail-pg1-f170.google.com [209.85.215.170]) by gabe.freedesktop.org (Postfix) with ESMTPS id A6D0810E960 for ; Thu, 13 Jun 2024 02:31:50 +0000 (UTC) Received: by mail-pg1-f170.google.com with SMTP id 41be03b00d2f7-6c4f3e0e3d2so377934a12.1 for ; Wed, 12 Jun 2024 19:31:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718245910; x=1718850710; 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=rI/qiwYumUf9WdznWs8PjjNV1aSEYVRYeuHEKQmKc7Y=; b=A8wGD3NYYYL0h4lyfDQOcrX8EgfLep3e/sLLjNw5RO2vUQyA7BtsrWyO+BD1fHG719 imMc8ddnbbUNKG3yjxQQ5GhRoEgAIbsEMB2Tqql5Gepr5gnSuRz8S4SiwGQ0TyDHDQzv 3+HF8C8qCs0arBOEziYRz2ZVDkBF3sjwr0TtnTwkh0wzmYVaprfCkSCcR8tM8m/NVx9b 0Z05WkG5ANYfh3+D1YiTxtrgtUFuqaaMCaEkzb5MLTbCpaHStXbcIyGtYL41EiyMmN5g 0y5HaeeqHdX2HoPiMZhvzB2be5Bh32Ox9E3R4q2wxF/oV6092rHQHr1mquTdKZiNejCR Qt6Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718245910; x=1718850710; 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=rI/qiwYumUf9WdznWs8PjjNV1aSEYVRYeuHEKQmKc7Y=; b=BV6udoB5iE0oF+R7Zoab50N9+pvpWeDf6LMdc+VQkWFg6EurhYAbJN0hPauNxQj1HL Bg2d6rJIkl3lZzGruOmnx/34ZT7jqKl7sus6hHJa9VFfwHRpFn+GfxyFWrb8dRGUcEXl tu6Oa86l27EKHwXDkbcPLIsKh23zWU1TDtm2easaaxM6riEVagF5U35mUIaK7Mi6WJQF STKVLFfPLIy+KlKfly1BgwHPMA3AOkpXHLjH70JAvtbkZAzR8t6GQsZkjWFWKkv19XFB h7M4GR7DQsb3ouQtmS3RlogblFknT43v5mOtnXQhsdwB9Of3xVpomKjcB8B1NnfJAmvJ q8OQ== X-Forwarded-Encrypted: i=1; AJvYcCUHz3Hv/ogxmILkL0JE9howmh3QepZsCOWK9z9Cr1Ym7nk2nozkTncU3j2ttMBlzmVU4QJmBJquSo3SB5ycspjtkxjOAxN9uBVr9ToA+VXs X-Gm-Message-State: AOJu0YwetiobHmNds3wGe5Zjw5xcHI3uFxwlDhst6b4dahIKcQb++qEz 061lhZsMpEpPd9bCWFnQWysGIVw/O2ntMvzpy2o+X85KxOGxllb0 X-Google-Smtp-Source: AGHT+IEHZPxKX/RP4SBm4cAnIghTfWMy7FOuVWWLSjXVUtL6OKmdClkmrYCnnZGc6EFhsF+GbPMHHg== X-Received: by 2002:a17:902:d2c9:b0:1f7:22bf:57f4 with SMTP id d9443c01a7336-1f83b730b3dmr43536295ad.55.1718245910002; Wed, 12 Jun 2024 19:31:50 -0700 (PDT) Received: from localhost.localdomain ([39.144.105.92]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1f855f4d159sm1755695ad.289.2024.06.12.19.31.42 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 12 Jun 2024 19:31:49 -0700 (PDT) From: Yafang Shao To: torvalds@linux-foundation.org Cc: ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, 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 , Catalin Marinas , Andrew Morton Subject: [PATCH v2 06/10] mm/kmemleak: Replace strncpy() with __get_task_comm() Date: Thu, 13 Jun 2024 10:30:40 +0800 Message-Id: <20240613023044.45873-7-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240613023044.45873-1-laoar.shao@gmail.com> References: <20240613023044.45873-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Mailman-Approved-At: Thu, 13 Jun 2024 09:26:31 +0000 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 Cc: Catalin Marinas Cc: Andrew Morton Acked-by: Catalin Marinas --- mm/kmemleak.c | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/mm/kmemleak.c b/mm/kmemleak.c index d5b6fba44fc9..ef29aaab88a0 100644 --- a/mm/kmemleak.c +++ b/mm/kmemleak.c @@ -663,13 +663,7 @@ static struct kmemleak_object *__alloc_object(gfp_t gfp) strncpy(object->comm, "softirq", sizeof(object->comm)); } else { object->pid = current->pid; - /* - * There is a small chance of a race with set_task_comm(), - * however using get_task_comm() here may cause locking - * dependency issues with current->alloc_lock. In the worst - * case, the command line is not correct. - */ - strncpy(object->comm, current->comm, sizeof(object->comm)); + __get_task_comm(object->comm, sizeof(object->comm), current); } /* kernel backtrace */ From patchwork Thu Jun 13 02:30:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13696491 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 9053BC27C4F for ; Thu, 13 Jun 2024 09:26:57 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id C1E4910E9C4; Thu, 13 Jun 2024 09:26:54 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="QQiiG90p"; 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 E9E6B10E960 for ; Thu, 13 Jun 2024 02:31:58 +0000 (UTC) Received: by mail-pl1-f173.google.com with SMTP id d9443c01a7336-1f8507aac31so3954265ad.3 for ; Wed, 12 Jun 2024 19:31:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718245918; x=1718850718; 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=MqSWr/pfZwtTIRxMBQN6sYuDk2R2VvioPDgEPjsCPow=; b=QQiiG90pLTi+L8oP4+dBjRDwp7hilcdeeO4rh6yxnB+ksG80laAdwSiKSuu9YSgu4c 9s6+J7GwEWrE3CRgSSNTkPWyr3LVKn+qmKKVfk2OYVX5/uPg4d+NMnMqjwD2YGgJ1L9B ixteH6PuRJczORQeth97AmjdWeWuaOsvINGMvX4ahLglARiyWj2Wyxa5Obg514dWGBGP 8WUqeOa+zilAVU6t1eN7XluSlgnY5XQfaGyWzNhtAJW7Qzy5/4GoTUN/Dpiprtgbjiar absWIgZUQ31GVAD0P3QQVHL183GupzgXWZ+v0B+rY3fIKf/W86V9Ouig0v0sfFCd3kXB V/NQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718245918; x=1718850718; 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=MqSWr/pfZwtTIRxMBQN6sYuDk2R2VvioPDgEPjsCPow=; b=EQRiCf9K5PUno65aJrGbU7jVzsH1uCMcnY1RJRAbFCccj8u7p6qtL8pKJhtHti8gDk e1DGGJR+uiXb8HGQKXwr5uUudZcDEjMn6TmbsTgFoh3WBGJvDK90m065+Qmxv/aj/6D8 2l1kLbCmr0X/8x/XjJGPDst+FQ6Lx+do97bzudJDVWT5JYz0qUQsWLUhoHe2d0zeVV27 Odry25dVnQZsdO4ubtugRmZXYK93JYVUPDCyWuxb5v7I+51E0kjuUWx4GyJqseGqLl8j Nc1wUlUp3vewhjFOELVnFEWYSwp4BxZAyqxcWf+vta5VDzucsUDjz9AeO5hn/x45zisK A+nQ== X-Forwarded-Encrypted: i=1; AJvYcCWPCLpVcfIq+N8XQSFlouOsjJblQGn2RpGlyAwf3UaG/X9ARFJJ9qYnpuBMvz5GFxSAAy8iDhPgLDNaT2u6bmXF7gRIF0B9gUdOTIROlFTL X-Gm-Message-State: AOJu0Ywsm0f/rWaD65x/PRaOTOvHqnnfHnu05Gun65fi9otpl5QCJIGQ opPQJrUNKFZ5MSVjLAg4wVarMbbqV+nKpX9GCfVn+NUEi9srn7qx X-Google-Smtp-Source: AGHT+IF+JYjBn0OjZmgrQWU2wgsy8ofTmlVYqxM2KmLwEfyu1c8K7LtQBzzKH0FH1zCzrNJ76ASDQQ== X-Received: by 2002:a17:902:d4c6:b0:1f7:1893:2587 with SMTP id d9443c01a7336-1f83b55b817mr42002945ad.14.1718245918153; Wed, 12 Jun 2024 19:31:58 -0700 (PDT) Received: from localhost.localdomain ([39.144.105.92]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1f855f4d159sm1755695ad.289.2024.06.12.19.31.50 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 12 Jun 2024 19:31:57 -0700 (PDT) From: Yafang Shao To: torvalds@linux-foundation.org Cc: ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, 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 v2 07/10] tsacct: Replace strncpy() with __get_task_comm() Date: Thu, 13 Jun 2024 10:30:41 +0800 Message-Id: <20240613023044.45873-8-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240613023044.45873-1-laoar.shao@gmail.com> References: <20240613023044.45873-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Mailman-Approved-At: Thu, 13 Jun 2024 09:26:30 +0000 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 --- kernel/tsacct.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kernel/tsacct.c b/kernel/tsacct.c index 4252f0645b9e..6b094d5d9135 100644 --- a/kernel/tsacct.c +++ b/kernel/tsacct.c @@ -76,7 +76,7 @@ void bacct_add_tsk(struct user_namespace *user_ns, stats->ac_minflt = tsk->min_flt; stats->ac_majflt = tsk->maj_flt; - strncpy(stats->ac_comm, tsk->comm, sizeof(stats->ac_comm)); + __get_task_comm(stats->ac_comm, sizeof(stats->ac_comm), tsk); } From patchwork Thu Jun 13 02:30:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13696494 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 D13F8C27C75 for ; Thu, 13 Jun 2024 09:27:01 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 5D0FD8926D; Thu, 13 Jun 2024 09:26:57 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="ZwhPghxH"; 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 63FAF10E960 for ; Thu, 13 Jun 2024 02:32:06 +0000 (UTC) Received: by mail-pl1-f179.google.com with SMTP id d9443c01a7336-1f4a0050b9aso4929595ad.2 for ; Wed, 12 Jun 2024 19:32:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718245926; x=1718850726; 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=HQT31JSkonb7Xmqn+k1uqrjgngeoHr6IFKrX/+KN6Ok=; b=ZwhPghxHBurUUIuCXnYUltvOD/m08/tgeoDoSLYqL6O0gn7rbjeKS4VAmRpkSJLX7z rf2HdBl9AGJGryY9gFn1a/l+2tPtdlDLvRWvGrQxf8rmNEmjVn0uviOi05ysxmcqZx2V XziEieb37GU+Arnctec3XQbhGUdA6GLYrS4lWj86VNwo0WpHYWYLTcD0Ma4+byCzbRjR PrwnbKtkh3nnTf+qojxwpZUKwIfP20vf6SpFuDUbKIxbFCMoKQQP3xUwuYOwao5HbzPO Rz84bLAuz4g7mA6PuIna1FezqVufaWrOOf4CFT5fPkam708jJZPdC8N5A36C7GuiJyjy ROzg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718245926; x=1718850726; 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=HQT31JSkonb7Xmqn+k1uqrjgngeoHr6IFKrX/+KN6Ok=; b=PDTzDQDuwUVAOsAca5qVheaqxfJFNjS7vMWvW6URRoCUX9OUktVJcllWHDti5HIN5h 5ocX6CXbHmeIj70sc+QsYGXD8tIEyJzz2A6HUPBHj/W/wpk1aTZZ11iX9IdWnbXu5NSo 17gUA6bL4nxbfzR1R7ywleat+fWGLdvGkXKhu5+4gnB6QMMwWVfG/jV7QN5eNK1TOwXE OiMkvpw8AVmgpvwnnkUZfoDQRz8/3VvyLcefwGsfvaKVLI/AzqRxrwcqJ2cJ3o+CsrPa a+WsQ191WJz1pF0kjimNG3FSfi06y+c0Rya7EYUEULZXgLrEqhSlpnFnGeJmc6CEYgK4 Rj6g== X-Forwarded-Encrypted: i=1; AJvYcCWxJGgjepLVo8QMK/Cgg1mOTL8+/UfOumKFyo7UXJ+7zzQE07R6rb3JGlz5k/BsuBBKmcIad7UAIHYEaV6FySO+Onz46M7fSLHRAwLyS2V5 X-Gm-Message-State: AOJu0YwQeZ8n06V9OIj6IJfmGPa8LOAGxRzLPv+yEnw2qOOSKHNZPwQM If7/bbxkbQGT4tFbB9WRgvztsyL1eRHkbij+C6Q9lvPRvl+ggfkR X-Google-Smtp-Source: AGHT+IE/Thj9zy8OzNEgltY85A2YMlP/9PBe6RK31xV6C4QSBrLzPw+agMFJyhDUlBI6xydC6ujXaw== X-Received: by 2002:a17:903:1c2:b0:1f6:89b1:a419 with SMTP id d9443c01a7336-1f83b5583a6mr41814045ad.17.1718245925584; Wed, 12 Jun 2024 19:32:05 -0700 (PDT) Received: from localhost.localdomain ([39.144.105.92]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1f855f4d159sm1755695ad.289.2024.06.12.19.31.59 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 12 Jun 2024 19:32:05 -0700 (PDT) From: Yafang Shao To: torvalds@linux-foundation.org Cc: ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, 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 v2 08/10] tracing: Replace strncpy() with __get_task_comm() Date: Thu, 13 Jun 2024 10:30:42 +0800 Message-Id: <20240613023044.45873-9-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240613023044.45873-1-laoar.shao@gmail.com> References: <20240613023044.45873-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Mailman-Approved-At: Thu, 13 Jun 2024 09:26:30 +0000 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 Cc: Steven Rostedt Cc: Masami Hiramatsu Cc: Mathieu Desnoyers --- kernel/trace/trace.c | 2 +- kernel/trace/trace_events_hist.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c index 578a49ff5c32..ce94a86154a2 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 Thu Jun 13 02:30:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13696482 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 4114AC27C75 for ; Thu, 13 Jun 2024 09:26:34 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 2264E10E242; Thu, 13 Jun 2024 09:26:33 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="j26kbFOV"; 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 23EE710E960 for ; Thu, 13 Jun 2024 02:32:16 +0000 (UTC) Received: by mail-pl1-f181.google.com with SMTP id d9443c01a7336-1f6559668e1so4960675ad.3 for ; Wed, 12 Jun 2024 19:32:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718245936; x=1718850736; 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=wLV4tdtvKwjUMxvLd3MjEerWvvi0S3KYqORy/5kgs/I=; b=j26kbFOVBEMkD+7ZSOT95JukQsT8YjCJht6UEoar2RXsu2jGPkKc7LVNqlPFKrgNJr WwQfhmGjay1hbSDI6wtGnNYkNvCRQY3/aqujOpEaC3EK5VianReETC8JchPOr8hdqRS+ 1G+Xg+xgodW7h4Dv2fBDpuHEtzPKU79L9dVLmbkb5+d69/8xHRVgxOUTpq/9u/h1lKXQ bR/GL4XV7Uz6EcaKWH5noCtI0O8Wn6PJylzou4LUwHJXzQtLQgrBpNnIfkVRrJKJitOV WwbVXPyFTkgk78+sve7JWv7vC4AkADQr50tz3dvgkHph3AX3m4+73HQ/l+Ndt97C5IEM TnIA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718245936; x=1718850736; 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=wLV4tdtvKwjUMxvLd3MjEerWvvi0S3KYqORy/5kgs/I=; b=LvwwA4ZRmi9ff6ilZoldtV/c3zMfPH+zN5GPDMquDMwuYtE/TPi/bundrxfH5GnQcT dKj/k4D2N2QvGTamt4RD/euDam//vjU/7yRR1gHv0GFhiNktD7qUUZuunmQ/hy2EWwNL DuJCq9qdXZiu8lxa4PH49DO2wjL5jXg7y/yEjHEdwjb5/ankD6k1Bswxo5DeX2kKedk0 9yYMpJJNOz4RANx+sIDcABVVOFwABAyE3pFTpBP4BaKttuBcH5wCKrN30wp2N4wThcfn bPPPnqfNWh1w+lH+jCO3AeWKv49mDsZ/dtAePFKrx6arQ9oLvL6osihbUbQpUExUIBdu 9kLw== X-Forwarded-Encrypted: i=1; AJvYcCXogyg0XK6aTaDK9hru/3UaTVDuLiZwPE8nJRfQTCKEBzk1vPFGOCIb2yH9zrolFGJupNmu1/nuhpuDtG0rDAijy9ZXZ+OFuyxPAba3DDx1 X-Gm-Message-State: AOJu0Yx4Plm+hpxKsDnNesfvcDY2pmFkLEhJbhnNNoIaWqQyU47vKiZD LowptuQ0NXGoIrsv4DJW00FXGC60EH+6frctagdtT+wXaW0ErJA8 X-Google-Smtp-Source: AGHT+IHU8nljO/XpjX7VaEB7PAOC5fyOplOexRJH9EcW/3foBQqPSTf9j2DyRFgOTFj3wqtthRwoDg== X-Received: by 2002:a17:902:d4cb:b0:1f7:3763:5ffb with SMTP id d9443c01a7336-1f83b74d134mr37873815ad.59.1718245935611; Wed, 12 Jun 2024 19:32:15 -0700 (PDT) Received: from localhost.localdomain ([39.144.105.92]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1f855f4d159sm1755695ad.289.2024.06.12.19.32.05 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 12 Jun 2024 19:32:15 -0700 (PDT) From: Yafang Shao To: torvalds@linux-foundation.org Cc: ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, 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 v2 09/10] net: Replace strcpy() with __get_task_comm() Date: Thu, 13 Jun 2024 10:30:43 +0800 Message-Id: <20240613023044.45873-10-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240613023044.45873-1-laoar.shao@gmail.com> References: <20240613023044.45873-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Mailman-Approved-At: Thu, 13 Jun 2024 09:26:31 +0000 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 d914b23256ce..37fa3b69df45 100644 --- a/net/ipv6/ndisc.c +++ b/net/ipv6/ndisc.c @@ -1942,7 +1942,7 @@ static void ndisc_warn_deprecated_sysctl(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 Thu Jun 13 02:30:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13696483 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 A8469C27C4F for ; Thu, 13 Jun 2024 09:26:36 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 9B01110E9B9; Thu, 13 Jun 2024 09:26:33 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="T447+X/l"; dkim-atps=neutral Received: from mail-pg1-f181.google.com (mail-pg1-f181.google.com [209.85.215.181]) by gabe.freedesktop.org (Postfix) with ESMTPS id 8114A10E960 for ; Thu, 13 Jun 2024 02:32:25 +0000 (UTC) Received: by mail-pg1-f181.google.com with SMTP id 41be03b00d2f7-6e57506bb2dso450723a12.0 for ; Wed, 12 Jun 2024 19:32:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718245945; x=1718850745; 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=ByPZqEdQZBBtzQ6Ut5Ro8SR1Vw4SYts83oZvgSR+UR8=; b=T447+X/l+FtIDTCxe1g4qLqexAo8lb+eRf0EcD/SitfwzUxWjM0XOCwXpPPP0Ys2Aj hc8DtcIBaj6U6Zp4n350UaJqj7GwmRUn1RAu8awjh2cQ9TH3OnABbny1zlqgDRVZ2b9K C+OKtqypba5TL3nZ5V2jFAVq+5aAATKRJSnvZKnMDhZ7muB3FtL+UeBG+fwvJyFL+t9t wx0d5HFIyPlm7lGV/cqO0Yf13IRAVyUSZd1evZmgjuyzJtswbj+WAWXJlGbuf2KBUiNv lau2x1i8ivPbVsBXBmlTqmqdsRSaxbfxHScwmgr5yr6cospV5PUuqxvz/8itAdPP5+V6 Na/w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718245945; x=1718850745; 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=ByPZqEdQZBBtzQ6Ut5Ro8SR1Vw4SYts83oZvgSR+UR8=; b=e82YCy5las9YwZFa0ovJyAJqfmg/7nCTd4jhbAh12lFoSOa0+aAdLNB1WZQQI4BXNp yuTpIoHGsymJEPsYhHu9NSUOL4CjDLph0vo2oRmYuZVnD84d/sWeIUdnk0UUgQUaw3Bd cqYzwlWCQh1h1W0sw9Xl0+V9oBMUJsI0qTXc9nyx5R7oRxcbpk9EtKJuchp4mvN0+CCJ w7XCAVMWK2c+DsywRWomRAkATKRb7mP5CYr+DokgKV182wIlQVCjnl24svyf8gSslzSm REI/fn7TNbt5cIqnMkakrYpUMPC1vBPOnaVbfTeUnB0RSVYx4Nv4UeTZlc6zQa5d1Btm Bi4A== X-Forwarded-Encrypted: i=1; AJvYcCV6Xaah7Oee1Dl1ruYpdU+t4DMbn7jO5fedvyuJkkJs9hNYSXMpfZoUhCklGLUmrJBBu45qdxoTnokwrZHhOxFzg6Vk/kNyCFeFh3uxqCz2 X-Gm-Message-State: AOJu0YyhfezunDrr1OXnCofob7qNHfK90gpRRTf7a+fyyFEL/RdhTzkE EMpBUsQgeE3FnDmscYMfrqO/jk/4NZz+YpgQzgtZBYLnWyt9ZAXU X-Google-Smtp-Source: AGHT+IE+I63Tcz7mU3dbe4p80REUOFhGXlU1vxQyrrx4b613sOPf+IM/heFPON96TqCsfc2pgVpt6g== X-Received: by 2002:a05:6a20:840c:b0:1b6:c527:7e42 with SMTP id adf61e73a8af0-1b8a9b1eb09mr4050153637.3.1718245944827; Wed, 12 Jun 2024 19:32:24 -0700 (PDT) Received: from localhost.localdomain ([39.144.105.92]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1f855f4d159sm1755695ad.289.2024.06.12.19.32.15 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 12 Jun 2024 19:32:24 -0700 (PDT) From: Yafang Shao To: torvalds@linux-foundation.org Cc: ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, 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 , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Daniel Vetter Subject: [PATCH v2 10/10] drm: Replace strcpy() with __get_task_comm() Date: Thu, 13 Jun 2024 10:30:44 +0800 Message-Id: <20240613023044.45873-11-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240613023044.45873-1-laoar.shao@gmail.com> References: <20240613023044.45873-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Mailman-Approved-At: Thu, 13 Jun 2024 09:26:31 +0000 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 Cc: Maarten Lankhorst Cc: Maxime Ripard Cc: Thomas Zimmermann Cc: David Airlie Cc: Daniel Vetter --- 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 625b3c024540..b2c16a53bd24 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); + __get_task_comm(e->comm, sizeof(e->comm), task); e->pid = task->pid; } rcu_read_unlock();