From patchwork Fri Jun 28 09:05:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13715806 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 D6B7DC2BBCA for ; Fri, 28 Jun 2024 09:05:31 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id EE43910EBC3; Fri, 28 Jun 2024 09:05:30 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="EOKJRzAy"; dkim-atps=neutral Received: from mail-pl1-f169.google.com (mail-pl1-f169.google.com [209.85.214.169]) by gabe.freedesktop.org (Postfix) with ESMTPS id 525CB10EBC3 for ; Fri, 28 Jun 2024 09:05:29 +0000 (UTC) Received: by mail-pl1-f169.google.com with SMTP id d9443c01a7336-1f9ffd24262so2091245ad.0 for ; Fri, 28 Jun 2024 02:05:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1719565529; x=1720170329; 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=/dYktotwfBu6Roz80A5YCXaIPxsklnKpN0JVw9YLBJ4=; b=EOKJRzAysVAMU7eicxAcrFaX8gyHXE81P3NBrPv9yFblilP7GEaT3+BjALpZTjFhyZ ZWodpg3VKXLEJOF2u1Lwrmkv8OjFFTx8EoC1gGUmHlvWEmgLWLnR70wvzvT24rDl+b5N CIzCGqKoMxKEjTyOLVdOYjUC75VpllyXlT5BsDFbv4iCFj0aoeotuAtMxoKdUxEcJGd7 eSzU1ZhXBIGttDtLsSwN37TPvvx/jvJy+p4C986kmviByBWedhkBZ4RGqD8D7TX/dL5Q IZQZkfxnznPS3FWSrgWuMy/Vy+Q8aPVGRPYHEdZ76S/hJGtZyFfVEmsOkz9VEBcYxL4J EKiA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719565529; x=1720170329; 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=/dYktotwfBu6Roz80A5YCXaIPxsklnKpN0JVw9YLBJ4=; b=uj8qO0ndw1pp1xr5CFSrcOZLgrpffeydi8ChQqHn46V3tBp4L8P1ahaBqfHzIK3y92 yMe1HoZ/JG7nqvBlVSU1xT948rxzHSkPskXeWhRL2S33G9SIdIj3YwOlGWo1I2Ack4Fw iExL/dY6BW5I7krUACcHoSj/rmwEYCGmyEGXzYyZ7M+qJq0YoQm9YIGP2GcI6Ao8+TOX MXnUaTPhES5cWMDfLvC+eB4XSC4hMFrcLaucVHFxoaN1iZvv59a/42Md6QnfqerrO3jo lHm1NPOJw5QXVo/rjZIxEyjX80VPvdMbA4dZC5jjwZVPxQa1ImnvxvqvTIzIq+axhQgL z1dw== X-Forwarded-Encrypted: i=1; AJvYcCWG/Egx3wZGUTLARbVEIIe3KC5Io3MIMgR+j2rycl9XvNH4HB4wIYYwe5Vb0RNXPiPss2wbzAQpmMjbT7NZdW9NTYgPkAwph7l7iXVPbVFE X-Gm-Message-State: AOJu0YzZCx8gBZcwTdbZfx4tKCxMvjhNyJaiqLAz0gG9JGKcXXA/6Ty4 Bvg+kQJm1tkNcI1fOqqmlt3lc6mxBln70MkC74+gMCiKHMYUCar8 X-Google-Smtp-Source: AGHT+IHq12/wr4f/48x7wfiSbegh8HPUA/2boo4oAyw8ihJMGtAi4AloztlVccxiTIt0WgGf0DVrww== X-Received: by 2002:a17:902:eccf:b0:1fa:3f05:20b2 with SMTP id d9443c01a7336-1fa3f05244emr164534885ad.65.1719565528645; Fri, 28 Jun 2024 02:05:28 -0700 (PDT) Received: from localhost.localdomain ([39.144.106.153]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1fac10e3a1dsm10473085ad.68.2024.06.28.02.05.21 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 28 Jun 2024 02:05:27 -0700 (PDT) From: Yafang Shao To: torvalds@linux-foundation.org, laoar.shao@gmail.com Cc: akpm@linux-foundation.org, alexei.starovoitov@gmail.com, audit@vger.kernel.org, bpf@vger.kernel.org, catalin.marinas@arm.com, dri-devel@lists.freedesktop.org, ebiederm@xmission.com, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, linux-trace-kernel@vger.kernel.org, netdev@vger.kernel.org, penguin-kernel@i-love.sakura.ne.jp, rostedt@goodmis.org, selinux@vger.kernel.org, Alexander Viro , Christian Brauner , Jan Kara , Kees Cook , Matus Jokay Subject: [PATCH v4 01/11] fs/exec: Drop task_lock() inside __get_task_comm() Date: Fri, 28 Jun 2024 17:05:07 +0800 Message-Id: <20240628090517.17994-1-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240628085750.17367-1-laoar.shao@gmail.com> References: <20240628085750.17367-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 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 Fri Jun 28 09:05:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13715807 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 C24A2C3064D for ; Fri, 28 Jun 2024 09:05:37 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 1234E10EBDA; Fri, 28 Jun 2024 09:05:37 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="Ytv/HBaT"; dkim-atps=neutral Received: from mail-pg1-f176.google.com (mail-pg1-f176.google.com [209.85.215.176]) by gabe.freedesktop.org (Postfix) with ESMTPS id E430D10EBDA for ; Fri, 28 Jun 2024 09:05:35 +0000 (UTC) Received: by mail-pg1-f176.google.com with SMTP id 41be03b00d2f7-6eab07ae82bso242474a12.3 for ; Fri, 28 Jun 2024 02:05:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1719565535; x=1720170335; 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=y+o2QcP3zzShPD5bpWdvqtRKk4SGVf/ncCjWPDHjjaM=; b=Ytv/HBaT4FVlaFBv/h+OKw6vdrUlhS4kV4E0VIAJoWb4NVHT8HbAAXDdcMdreuP5rX eL40X9yfS8Z5zKjjHcta4o2hRKQJ4uls8ioUh1Cy0e9+yYgGTFHrLM0VERCbvJWiGyXU CcznM36NoQkyH046teNnQI//Sq0exlooREpVPoSqHlj4nVjMsXGqT9AcIWxsI+BRpntD 8Y4Gvzh6ZP/1ASRGGltvtOMJ/fZn0UNM3RHzliKGQ9WNt+96Aohqfxblbm6CsVD4ln9U d5ryi2cbtobT515A8rA7WencaKBMxndmuGlHrvzVAOe+df4cOuKhhn8bdgsFkMILk+h/ 9mTw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719565535; x=1720170335; 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=y+o2QcP3zzShPD5bpWdvqtRKk4SGVf/ncCjWPDHjjaM=; b=O4dy6uwCFeNUHtnR1vIHWblrgnes6giGRi9rY/cgb00zSA4lj8ApZnDwEf8c4c2jI/ upUnkAjyJ5PGKGU33w2DGnx0PFA+jyse9Pz46j+2Nf5MkOFR3gVR3PskyOxtD21oa3JY +dlNO/qDUCxzWgEafGPXNH8iStefc+ES+EIoC4Trw8aBU3IwZ4KMtEnorOO6RbwR6etM HUt5CKfe3nVQhMdY6ZeXmT/a7kCimglCx3Nl96eOqSpzybcSmi3nzfhJGyTbT0zpoTES DrmwGIE8gl5noO/r+k4DV570502F1tbSjLfBT3HF33S5s2vQbgt55BWqs+5QbLmJveLK 4m/A== X-Forwarded-Encrypted: i=1; AJvYcCU171N364bOGqCBD7QvC0zf43KH5YAJRXh8rofILmTEF2Q+JMtL3cio8R3DBHhmLACPZKASkaGXMvWrKzAmt7VJ/QAlaN4EgfQ+ADPRMkAV X-Gm-Message-State: AOJu0Yxx6jRnsKPDJG7CoB50xXtAcLZ5eVOmABCNCo8/IY64K5rdzbpT rngXCN1zr4KlZV2EYMqajY5QtURsfClTKZIRw0ynkKTjFhaAZ7Pv X-Google-Smtp-Source: AGHT+IGiL1nE/JPCYxh777n4qtTvEfwnEp1By9NjDz6oRHtLGf4s+XwALk3c6cvWzb1jiZh3cdfwuw== X-Received: by 2002:a05:6a20:c325:b0:1bd:2267:b45e with SMTP id adf61e73a8af0-1bd2267b4edmr8264153637.50.1719565535182; Fri, 28 Jun 2024 02:05:35 -0700 (PDT) Received: from localhost.localdomain ([39.144.106.153]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1fac10e3a1dsm10473085ad.68.2024.06.28.02.05.29 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 28 Jun 2024 02:05:34 -0700 (PDT) From: Yafang Shao To: torvalds@linux-foundation.org, laoar.shao@gmail.com Cc: akpm@linux-foundation.org, alexei.starovoitov@gmail.com, audit@vger.kernel.org, bpf@vger.kernel.org, catalin.marinas@arm.com, dri-devel@lists.freedesktop.org, ebiederm@xmission.com, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, linux-trace-kernel@vger.kernel.org, netdev@vger.kernel.org, penguin-kernel@i-love.sakura.ne.jp, rostedt@goodmis.org, selinux@vger.kernel.org, Paul Moore , Eric Paris Subject: [PATCH v4 02/11] auditsc: Replace memcpy() with __get_task_comm() Date: Fri, 28 Jun 2024 17:05:08 +0800 Message-Id: <20240628090517.17994-2-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240628090517.17994-1-laoar.shao@gmail.com> References: <20240628085750.17367-1-laoar.shao@gmail.com> <20240628090517.17994-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 Fri Jun 28 09:05:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13715808 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 41F7FC2BBCA for ; Fri, 28 Jun 2024 09:05:44 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 61B7810EBDC; Fri, 28 Jun 2024 09:05:43 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="GeK6eqJl"; 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 934E910EBDC for ; Fri, 28 Jun 2024 09:05:42 +0000 (UTC) Received: by mail-pl1-f170.google.com with SMTP id d9443c01a7336-1f70c457823so1557115ad.3 for ; Fri, 28 Jun 2024 02:05:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1719565542; x=1720170342; 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=J+uQG4RKAtUTljj+c6PuEzvl8DtV0722F0vMFtL07WU=; b=GeK6eqJlXmgdMgo4oWKeeYJCqI+FSRPpR7UK2Sgc2huxYeVfcCV5YUiwytHmDKRknk U3TdlAKvNcpAwWswOscLsoYOolglMNDRZKED4WZfIL/4YT2cDsjv4X0+TurKHnutG5VM eO7dLbLHBRe3y9sKGHNR0N5Ro7SfGx2Q4O0BSU33LaSqaQWKqdJJcbEV98eN0L/GTGW+ WyeNDaPju9zkPU9or7MmJ0pudYFJIxKYPbPWD1zyeMOf+7NaOCbEKpDu6YJ/BcxrOMNv Zc/F3d6ffx1Q2SL2KGXYTz3vI/EPwIzq3alZ8tfyuSIOE7mgQ+AEYlKSGUBNnEU69Og2 dqtw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719565542; x=1720170342; 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=J+uQG4RKAtUTljj+c6PuEzvl8DtV0722F0vMFtL07WU=; b=FkAe06AGWCj6Lf84b3i4b1gy67UrW6KvRlh6U1PAsjfZSx3FYaLgBFOOYK2bK3oN0F xCiBFBQWQ8GNV+KpdoPlzqpft2x9ym4s7hzCkyXPvU2LJYChISSmOnTbAMTuVsPQ4L/P nHuNDESpB3r9v+/w/9q8YDTRLtK85wXAFuucbQqCyhIzlBzwcEQgUoDUMXWvSabb6umv cUat5XDSSB0CN+Q4HV8XGWu2pPnaotGqpLzfuJwK06LBbJEOtiYqkfpG5iPYeVgpN1ou IlrlHY3oC3QWW1uKNALzgFkWKg3HmlGnEgdsYSV8hHXNkhXqyTyNqyD/ifrqLmk6kQ+B XpKQ== X-Forwarded-Encrypted: i=1; AJvYcCWYHW4j8nHYSzmLm6bpGRkFBypIZehjoKjL0l4pAKEQsjMfc6Cdjtlilz4uvUKn99/e7g7QJ3duX1t9gYmIReqz/U2e4OKYKv5rb+gLeFL9 X-Gm-Message-State: AOJu0YxDpOj5qJ7iGY76AU17PM9PSPPpi9q3IMP7WvLmYdcndPjox2+y ZDHbA2UskgR2yA3QBquYjbPeKRhfntJ4pJcndKWygJKF1HtfhOZv X-Google-Smtp-Source: AGHT+IEiCe46L4L+6u8pFkskIn0tSNW3yZf9XlVCW3W2rC8VIHwBiWsCsriXmS70kmQMnPpYjn9ORg== X-Received: by 2002:a17:902:a3cd:b0:1fa:a34e:8819 with SMTP id d9443c01a7336-1faa34e8cb9mr39730485ad.60.1719565541990; Fri, 28 Jun 2024 02:05:41 -0700 (PDT) Received: from localhost.localdomain ([39.144.106.153]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1fac10e3a1dsm10473085ad.68.2024.06.28.02.05.35 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 28 Jun 2024 02:05:41 -0700 (PDT) From: Yafang Shao To: torvalds@linux-foundation.org, laoar.shao@gmail.com Cc: akpm@linux-foundation.org, alexei.starovoitov@gmail.com, audit@vger.kernel.org, bpf@vger.kernel.org, catalin.marinas@arm.com, dri-devel@lists.freedesktop.org, ebiederm@xmission.com, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, linux-trace-kernel@vger.kernel.org, netdev@vger.kernel.org, penguin-kernel@i-love.sakura.ne.jp, rostedt@goodmis.org, selinux@vger.kernel.org, Paul Moore , James Morris , "Serge E. Hallyn" , Stephen Smalley , Ondrej Mosnacek Subject: [PATCH v4 03/11] security: Replace memcpy() with __get_task_comm() Date: Fri, 28 Jun 2024 17:05:09 +0800 Message-Id: <20240628090517.17994-3-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240628090517.17994-1-laoar.shao@gmail.com> References: <20240628085750.17367-1-laoar.shao@gmail.com> <20240628090517.17994-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 Fri Jun 28 09:05:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13715809 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 C7DF3C3064D for ; Fri, 28 Jun 2024 09:05:50 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 2807B10EBE2; Fri, 28 Jun 2024 09:05:50 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="BB25ohRC"; dkim-atps=neutral Received: from mail-pf1-f170.google.com (mail-pf1-f170.google.com [209.85.210.170]) by gabe.freedesktop.org (Postfix) with ESMTPS id EFBBC10EBE2 for ; Fri, 28 Jun 2024 09:05:48 +0000 (UTC) Received: by mail-pf1-f170.google.com with SMTP id d2e1a72fcca58-7066f68e22cso281571b3a.2 for ; Fri, 28 Jun 2024 02:05:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1719565548; x=1720170348; darn=lists.freedesktop.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=QQdkC9XISn7BWPlTE5/iE7aUj/L2jeLonKoaBsbNpS0=; b=BB25ohRCQsuGfI1AnsZ5DpFBR3cEWxuKXqHOPum7C6zkC5F6JesTU9RKiZu7zf/BGE 8CgtvQlP4Lz0vZSQ94vf1175tQmwY6mXZdhSGyl7owZXBlzAqZbpJLXCEn+JBJo+KaFB 0wBJAwlMSDmhSpd9U0bb9a0pHmeTkB2p95dqCZoc1G/5PrXVGi/NjhyKG3Q5gCSi4RMq yRKUuHKglJcy03rY5SpsHBjfdjeSz/UberQBbPuiN8j+zzW30yOhNds46kwbwZ11liZY Yb3n3VXonNeNldo5VwuOroF87Lpfb9e8t+9wox7xZTST3xrMmCYV8h1qHCHDqiM1VGPx fNkg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719565548; x=1720170348; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=QQdkC9XISn7BWPlTE5/iE7aUj/L2jeLonKoaBsbNpS0=; b=uB3z6tJbEUgvAYddGIXeYvILxSr5LcUTU+Q9RDk1wBClAqKiXaE6Lmoor32dnwTHPQ PC2s+zC38qpPM7oyMQ0n/lT/zxbSEOyr8afkbENm3yeqofNNalPPdZnFfSsEK+9urtnE VIIFQYeqTnhjfP3oQKrjvvNGQAarPP9NsyMFd8g/++GfReNrEvAnWpcXekFx4wX6wGr/ qxF2YZ/xmHF4H2n9czNP+Y/q5RmXsbFji7UTC+NzdzCctqqzEC4DkZ9fHcFpWIJCZNz8 Y8H5xK9SZDsh81g1DFIvFgkFoVi68fv5x71f47iYq3FdtErOsJE2g96EniOk26mRXddr y0GQ== X-Forwarded-Encrypted: i=1; AJvYcCU7AS0JtSgQdnWYyWfoU6VxCGqVMCNQHcPjeUVMVHKtlMYpXUo/x8PFVNUf3en0XiuVNY729llXNmt59W296sAUM1/c32Qb1mCJHJWDeA2/ X-Gm-Message-State: AOJu0YyTywlq7Ivrw6W9cZXJz0vRclSglqTVqDZ6DfruxfPUpf10F6rQ +Nh3PLubadt2k6f52ct4fP21vp6ZxNT0eboR0QODEyDyP2ZEXINc X-Google-Smtp-Source: AGHT+IFMoCwH/epxCGvG8TCVrNgBjmP2Ft1l1xXAwKN56cpOIayJsWQPU41hiw8mZOfUPZaXevc3Mg== X-Received: by 2002:a05:6a20:6a04:b0:1be:d9fc:7f03 with SMTP id adf61e73a8af0-1bed9fc824fmr4655522637.23.1719565548310; Fri, 28 Jun 2024 02:05:48 -0700 (PDT) Received: from localhost.localdomain ([39.144.106.153]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1fac10e3a1dsm10473085ad.68.2024.06.28.02.05.42 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 28 Jun 2024 02:05:47 -0700 (PDT) From: Yafang Shao To: torvalds@linux-foundation.org, laoar.shao@gmail.com Cc: akpm@linux-foundation.org, alexei.starovoitov@gmail.com, audit@vger.kernel.org, bpf@vger.kernel.org, catalin.marinas@arm.com, dri-devel@lists.freedesktop.org, ebiederm@xmission.com, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, linux-trace-kernel@vger.kernel.org, netdev@vger.kernel.org, penguin-kernel@i-love.sakura.ne.jp, rostedt@goodmis.org, selinux@vger.kernel.org, Quentin Monnet Subject: [PATCH v4 04/11] bpftool: Ensure task comm is always NUL-terminated Date: Fri, 28 Jun 2024 17:05:10 +0800 Message-Id: <20240628090517.17994-4-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240628090517.17994-1-laoar.shao@gmail.com> References: <20240628085750.17367-1-laoar.shao@gmail.com> <20240628090517.17994-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 Fri Jun 28 09:05: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: 13715810 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 515E5C2BBCA for ; Fri, 28 Jun 2024 09:05:57 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 864BB10EBE3; Fri, 28 Jun 2024 09:05:56 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="H8iwIQgw"; dkim-atps=neutral Received: from mail-pg1-f169.google.com (mail-pg1-f169.google.com [209.85.215.169]) by gabe.freedesktop.org (Postfix) with ESMTPS id 46BC010EBE3 for ; Fri, 28 Jun 2024 09:05:55 +0000 (UTC) Received: by mail-pg1-f169.google.com with SMTP id 41be03b00d2f7-70df213542bso217607a12.3 for ; Fri, 28 Jun 2024 02:05:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1719565555; x=1720170355; 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=8rSpj4+mjUH2uIm/J6/DitPwV6C8fJ6cAywoYkpJrFY=; b=H8iwIQgwP/h7uhisnZqMWNiOwvHT4XnCfybHEkXP2LbLhdchpHr0pG5WtxQCyGv8yj qrnnKoOXmE0KCHu964J/yLpf7FlesfioF8DpfTQyW32OJxUYBowqb2sGsLJKo6jKoytc 0MpjMmR3OXCIaFf0b+rDGXVdnkSy5TeopH/rDl702EosOG0vI0WNBEgVFKCpekqh+X3n +TWi31hnsAl7Jf4z1OhoTs9E4yufyU0XSK/wGq8hIuBYnRT9+asq1DdCcODD9D7MdSlj Rr7n220ifGYqYVN3wi80CQsHfSYXunsxgvZg/N6x2jDO5vDCDVvxWesY/opUZQossuNW FHbw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719565555; x=1720170355; 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=8rSpj4+mjUH2uIm/J6/DitPwV6C8fJ6cAywoYkpJrFY=; b=Z2DLduKvprfWdgP+M3amWReny9ECSjbw5aCYzurqt4+hOanCNgX1NTng+iIZtD65lp GSuhXtIlOJ1hCE6nCOMawfWh+SZbdO/4Qu2HJeGnS1wVkqVQbrq+Xg6TeL4b5AkzkTVr HuIfDNVS6zKhlt6lZjcrfwBxwOKvSD3JBZ3NA6VhYCnyFMvNBUpsWov+4rDZxTZXEq6/ 8hcDc3xjNIGyY2jfnSa7uJcId1u7CgwV3vS5RIxBj1NVcTROs3zEDV/tfBw3qgzkB9ZG YLjE2FzQCW84Ai/9yTo/zySB1njyPQFR0XrGfG+G4kRHPHWFQreYUMldTMxPZKM7b7S/ M2/A== X-Forwarded-Encrypted: i=1; AJvYcCUJpLm6zeIYFN5kOaCSfXIUapZHN7gVdFqzODWWNcIRdUS9f9lnNiT09+giQZgvLbsUuE88dZmVgtznFadxihHACIG8WwabVadO2GHZ7kNP X-Gm-Message-State: AOJu0Yy464Lb5Xpjk5ndWWH/Q3d3eFdZAmByyCjs7Es6mxfkv877W5UO jAabtEmXockhwfGxZb9qXaDUfH/zdRVEIAO3Byog8PTyry38c4h3 X-Google-Smtp-Source: AGHT+IFlYRl3xHVXsC7P9mtN40+ZKznkLN9wN0U9RRVR76hQVjzS9rrWerayfKdnpHLG4c4CVUtojQ== X-Received: by 2002:a05:6a20:4c16:b0:1bd:22fe:fcaa with SMTP id adf61e73a8af0-1bd22fefd3dmr9032557637.51.1719565554658; Fri, 28 Jun 2024 02:05:54 -0700 (PDT) Received: from localhost.localdomain ([39.144.106.153]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1fac10e3a1dsm10473085ad.68.2024.06.28.02.05.48 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 28 Jun 2024 02:05:54 -0700 (PDT) From: Yafang Shao To: torvalds@linux-foundation.org, laoar.shao@gmail.com Cc: akpm@linux-foundation.org, alexei.starovoitov@gmail.com, audit@vger.kernel.org, bpf@vger.kernel.org, catalin.marinas@arm.com, dri-devel@lists.freedesktop.org, ebiederm@xmission.com, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, linux-trace-kernel@vger.kernel.org, netdev@vger.kernel.org, penguin-kernel@i-love.sakura.ne.jp, rostedt@goodmis.org, selinux@vger.kernel.org Subject: [PATCH v4 05/11] mm/util: Fix possible race condition in kstrdup() Date: Fri, 28 Jun 2024 17:05:11 +0800 Message-Id: <20240628090517.17994-5-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240628090517.17994-1-laoar.shao@gmail.com> References: <20240628085750.17367-1-laoar.shao@gmail.com> <20240628090517.17994-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 c9e519e6811f..41c7875572ed 100644 --- a/mm/util.c +++ b/mm/util.c @@ -60,8 +60,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 Fri Jun 28 09:05: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: 13715811 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 B8A74C3064D for ; Fri, 28 Jun 2024 09:06:04 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id D5E2410EBE4; Fri, 28 Jun 2024 09:06:03 +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="ltRUuNMX"; 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 2D9BE10EBE4 for ; Fri, 28 Jun 2024 09:06:02 +0000 (UTC) Received: by mail-pl1-f179.google.com with SMTP id d9443c01a7336-1f64ecb1766so2080985ad.1 for ; Fri, 28 Jun 2024 02:06:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1719565562; x=1720170362; 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=hxss6iSMiIilMwCNJtHyPtBSL44EBghXiK4U5v9w5a8=; b=ltRUuNMXnYKIgPY2VoFcTXP3SxFRDBC71WF3YhmS5E5bzfBnufXdabx1V6vbyd3QfB +DBC9ZegdP8/U04N9y13nsfmK3xk36BFLpFJjkUHTlL2jidYSR75zN8T6u4A8W2GN6o2 a2hhxmkf4wTvStntf0ciT/1p0TUSKFKsF/5jhcgqbKdiuJxX6kHe8CF82IU2UK784YYp 2coBb3hJspOlRIW6xFfW9iNrxQi3WDyHV4tefiho2xFQeI32WHTpesg+n1pv4dhFIrac o0Y5hf+r0qusZKa1ECxY1CZQOCmlxzpIgJpXcWGqiP/sDgNEijyXNaiLnPyN6lRsBYcr NY8A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719565562; x=1720170362; 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=hxss6iSMiIilMwCNJtHyPtBSL44EBghXiK4U5v9w5a8=; b=EtBQHKo228Kcr4EPB8Lf+kYWMJq4tJ4IyK8YjKhJXSGtWtXFgWoJA8+x6uz+Kv65jd UpKnQSu9BIZjRcMHp/ui4mcFk43rVORiEDQuQc3vmRSnmliZguu1w5M38BMVzBLWCycS suo1Fepi6L/u1zcHq1qO//Kz1nfQqU7rv1kqwS8rtwkbWjAaTc07E7cL0q1BdERL7g2z QQZ9XdsLxU6eFEs8m5hOBH62waheEIRU3h71buD8Vt7QkyduX9oE3oYrYaM06v8fYlta tTx0d102NXtimoIYmgOQsvMFCF3FODLIU7xgTOuqyqC9bqakW0ZmmioIu10UcXx3rK/Z GTQA== X-Forwarded-Encrypted: i=1; AJvYcCWUzmZsqeftkiAygGVu1PPbTPaeTAJu4JpYkb+XQ6K3oN1y1YeVxVgEUXlFeyTQFp228berTeJpHSa6RzC3hDNyXAXr8XBDFh851LiyTEvg X-Gm-Message-State: AOJu0YwhYd9ZRm63OsNz61qCKm1WAk1/LNOWeuTctoarFn+lKJJSs86o SUhTmd8miAQytz5DlT+CZZs/CGpQscwaVZzhAb1aVWz4rY7ngjau X-Google-Smtp-Source: AGHT+IF41G2qRww4DdF/h1fHsQyMDqy+U0zSGeUJQI71N0qwJDXNmrrmWplbD4mTRfu3C38uIRvv6A== X-Received: by 2002:a17:903:18d:b0:1fa:2001:d8ff with SMTP id d9443c01a7336-1fa2001dad7mr177652065ad.52.1719565561588; Fri, 28 Jun 2024 02:06:01 -0700 (PDT) Received: from localhost.localdomain ([39.144.106.153]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1fac10e3a1dsm10473085ad.68.2024.06.28.02.05.55 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 28 Jun 2024 02:06:00 -0700 (PDT) From: Yafang Shao To: torvalds@linux-foundation.org, laoar.shao@gmail.com Cc: akpm@linux-foundation.org, alexei.starovoitov@gmail.com, audit@vger.kernel.org, bpf@vger.kernel.org, catalin.marinas@arm.com, dri-devel@lists.freedesktop.org, ebiederm@xmission.com, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, linux-trace-kernel@vger.kernel.org, netdev@vger.kernel.org, penguin-kernel@i-love.sakura.ne.jp, rostedt@goodmis.org, selinux@vger.kernel.org, Simon Horman , Matthew Wilcox Subject: [PATCH v4 06/11] mm/util: Deduplicate code in {kstrdup, kstrndup, kmemdup_nul} Date: Fri, 28 Jun 2024 17:05:12 +0800 Message-Id: <20240628090517.17994-6-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240628090517.17994-1-laoar.shao@gmail.com> References: <20240628085750.17367-1-laoar.shao@gmail.com> <20240628090517.17994-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 41c7875572ed..62a4686352b9 100644 --- a/mm/util.c +++ b/mm/util.c @@ -43,33 +43,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); /** @@ -104,19 +111,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); @@ -190,17 +185,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 Fri Jun 28 09:05: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: 13715812 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 BEBDDC2BBCA for ; Fri, 28 Jun 2024 09:06:10 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id D2DA710EBE5; Fri, 28 Jun 2024 09:06:09 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="JGAoNL+i"; 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 3FB1E10EBE5 for ; Fri, 28 Jun 2024 09:06:08 +0000 (UTC) Received: by mail-pl1-f170.google.com with SMTP id d9443c01a7336-1f9aeb96b93so2005795ad.3 for ; Fri, 28 Jun 2024 02:06:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1719565568; x=1720170368; 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=K/nz1JHR4z8p+ajw1D912vGA5V6XqUcGmzMFKN4JA2M=; b=JGAoNL+iLG88iga2MnXhU/Sb9h2/Gba7urXhuSEtu6hIqQzh56Ac455L9NLy99wXpK UFGSYxsonYqnZykq8wJq3Rg+X48PPQ5BYpE2GTdB1c1JsPC56F8yR7eKA4S/4aQljVcW 7FxoRc66/aTmosjiUuU7oUWqgGPUS0vPy6sfpDQYcFnCj6ulqbN+WvBwlNqSPrebWy4T 6TcjU1MngYyFkoELzfZFPeD8CMmRXh2+mIOJNbJUS+eqFj5tdotoLDY8WrrYPd9yN8rv z6fLsHcQJ+2jqo/TYswLOkAadzpe+JfUfLzDFh66LJAkxXSVLLMvFfenG6kJEFJPk5et SHOg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719565568; x=1720170368; 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=K/nz1JHR4z8p+ajw1D912vGA5V6XqUcGmzMFKN4JA2M=; b=Vx5qDSjx90/SYUXPM4dgrdNItqULyxrL/oBcSJa7eU3TEadKA5G+5FwHh1loAlV4W6 xiHQneofftKSo+nIwafz1CYHrYf7wbbS0kwdwntvUhJ6sUjXSXZN376butwnvqiJYw4f Ky3/dxrrVzjfNEh4S2X3tl6dMNXo/XZ53Gzvsqd7Ge0mKLIUpAEzRxXDFesJFdRUxu05 fiXuL2n3Jjo4ARvPTZ7/Gox0zEF5oOPDk6HCwAqIvwa6ttS7ru3aFCqKLXZVWh6qgxU+ aiMiKRtvOAK5kXfCfCFnsQOM2Foz1MPodqyPSuO1cKQUcORCiu7ABawUSN4lIqAcgvP4 kpdg== X-Forwarded-Encrypted: i=1; AJvYcCUIKvO3FynngbC+f0g67xZY/mjvbumxILJja1sfMyPZmsGDi9xatt1WFRlqdhPr9Sb0AktaghoxXeWcMnL1cYtPhRo74VdDoao7quJF6JVN X-Gm-Message-State: AOJu0YzzS8kmhziCCtsbxs2RMqvtR5QZc+gf5iJ0gN2pIbFDU0I5cTOh 29VLpUEmcKrwHvjewNlcsWo3XtBIXJKlAr3mbf7+qh/YIC1Cv3ZD X-Google-Smtp-Source: AGHT+IHdHlMsiSxDneCpl0Z+k+h/q/UIRrZYdoSU24sjr9+9ved5pPcYJylJrzwg3G+ZaraRZ/+k4g== X-Received: by 2002:a17:902:eccb:b0:1fa:ab7c:a483 with SMTP id d9443c01a7336-1faab7ca748mr37580565ad.5.1719565567694; Fri, 28 Jun 2024 02:06:07 -0700 (PDT) Received: from localhost.localdomain ([39.144.106.153]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1fac10e3a1dsm10473085ad.68.2024.06.28.02.06.01 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 28 Jun 2024 02:06:07 -0700 (PDT) From: Yafang Shao To: torvalds@linux-foundation.org, laoar.shao@gmail.com Cc: akpm@linux-foundation.org, alexei.starovoitov@gmail.com, audit@vger.kernel.org, bpf@vger.kernel.org, catalin.marinas@arm.com, dri-devel@lists.freedesktop.org, ebiederm@xmission.com, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, linux-trace-kernel@vger.kernel.org, netdev@vger.kernel.org, penguin-kernel@i-love.sakura.ne.jp, rostedt@goodmis.org, selinux@vger.kernel.org Subject: [PATCH v4 07/11] mm/kmemleak: Replace strncpy() with __get_task_comm() Date: Fri, 28 Jun 2024 17:05:13 +0800 Message-Id: <20240628090517.17994-7-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240628090517.17994-1-laoar.shao@gmail.com> References: <20240628085750.17367-1-laoar.shao@gmail.com> <20240628090517.17994-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" Since task lock was dropped from __get_task_comm(), it's safe to call it from kmemleak. 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: Catalin Marinas Cc: Andrew Morton --- 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 Fri Jun 28 09:05: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: 13715813 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 8273DC2BBCA for ; Fri, 28 Jun 2024 09:06:16 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id DED5610EBE7; Fri, 28 Jun 2024 09:06:15 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="gqNPdjw5"; dkim-atps=neutral Received: from mail-pl1-f172.google.com (mail-pl1-f172.google.com [209.85.214.172]) by gabe.freedesktop.org (Postfix) with ESMTPS id 7D8FF10EBEA for ; Fri, 28 Jun 2024 09:06:14 +0000 (UTC) Received: by mail-pl1-f172.google.com with SMTP id d9443c01a7336-1f9cd92b146so1820135ad.3 for ; Fri, 28 Jun 2024 02:06:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1719565574; x=1720170374; 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=aN2V+duoRLP5mjQclolxXRJl1PN7YZIwA2PLtFFVKQo=; b=gqNPdjw58LEAnCrVHduK2HV0ue4uaWQBNb4wQvYdO+D+BBb4YQ9q+9dukjc0mq+qY5 7UHqi6rb7EChKq9P5InanIaPEnk+AG7JldClBwkcjJcXhUi8TsRxn76QqpA66r8VJbR0 6jChEymIz7XntjPD85mcM7OIajEwFSgBVm6nGdiicUu1RiE4kvvuOINJ62KjCDgzDFBG TFX5jWpsxapuzrhabjmFmG/Rd3CsX9AClHy2xXReRRB7ImvMiiIN9cmuDzyvjfIs9G81 jubt+Wp+D9ox6vEW/o69v9FJsyyhnWHJvVnBBV+BTe5/n+CzVKV8bSXX4P369AridYr5 4Syg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719565574; x=1720170374; 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=aN2V+duoRLP5mjQclolxXRJl1PN7YZIwA2PLtFFVKQo=; b=TlnHpJavokIOE0W4sfUcA7+KkZ4+TFoEjNsYpSmVd18xgGLNEGqb7deZ3bQnySxOcV nsFFmwLA69gUWetBKmr79ew+bh7f6uMtEsvbmjYcfzN6gu9N/0bu1EAfjM3yBLocboPz oy7FVgLZfG5JD4+StaNTfF/YWfh1QUw8t90NKL3w4apfZncD7m3dujuuUFtXkmlxOj5w KJ9U4x6upDbUgwihDUKz4bAWCpd/EjOKQpPpgbnY4lOoxrwQCPKpZXsF3CI7EQkXX9CX GVmFH81qjaxM6cpssyFq7hcNw1FzGBhWtOvCFlla0OtC/RBTXLvxgkzbuT+PTAMjTvJS IqYw== X-Forwarded-Encrypted: i=1; AJvYcCW6UGYf+5OLZqEMfDsK2qPRJ6IVXCODzd99M62SHPQNljdPrw4fhEl+ywgWIEh1B/roLWj3WUbgIpOiPRslUWt5DYM2YmwcZX0o0499D2Lz X-Gm-Message-State: AOJu0YyhIho/SxJpkA0ZnXPArWXQOECbm72OpoNrBeIl6UL1OLkhEGNE udI3seFL0WNmYOpp306OP/aFngYhookJKpXb7zl6HlCdhUt8FIW2aBYVAEzoeFQ= X-Google-Smtp-Source: AGHT+IH8hbTH/mkdkSjyBT5baT0IFO1FQw7wMYLskc8y3Zcc6iSLCmGElb9qtOP7mNAm10MFemyYoQ== X-Received: by 2002:a17:902:d50b:b0:1f9:e7b4:5def with SMTP id d9443c01a7336-1fa1d3e0157mr181052415ad.3.1719565573956; Fri, 28 Jun 2024 02:06:13 -0700 (PDT) Received: from localhost.localdomain ([39.144.106.153]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1fac10e3a1dsm10473085ad.68.2024.06.28.02.06.08 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 28 Jun 2024 02:06:13 -0700 (PDT) From: Yafang Shao To: torvalds@linux-foundation.org, laoar.shao@gmail.com Cc: akpm@linux-foundation.org, alexei.starovoitov@gmail.com, audit@vger.kernel.org, bpf@vger.kernel.org, catalin.marinas@arm.com, dri-devel@lists.freedesktop.org, ebiederm@xmission.com, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, linux-trace-kernel@vger.kernel.org, netdev@vger.kernel.org, penguin-kernel@i-love.sakura.ne.jp, rostedt@goodmis.org, selinux@vger.kernel.org Subject: [PATCH v4 08/11] tsacct: Replace strncpy() with __get_task_comm() Date: Fri, 28 Jun 2024 17:05:14 +0800 Message-Id: <20240628090517.17994-8-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240628090517.17994-1-laoar.shao@gmail.com> References: <20240628085750.17367-1-laoar.shao@gmail.com> <20240628090517.17994-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 --- 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 Fri Jun 28 09:05: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: 13715814 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 6300EC3064D for ; Fri, 28 Jun 2024 09:06:24 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 4E91C10EBEA; Fri, 28 Jun 2024 09:06:23 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="ayIoKq+9"; dkim-atps=neutral Received: from mail-pl1-f178.google.com (mail-pl1-f178.google.com [209.85.214.178]) by gabe.freedesktop.org (Postfix) with ESMTPS id 1986610EBEA for ; Fri, 28 Jun 2024 09:06:21 +0000 (UTC) Received: by mail-pl1-f178.google.com with SMTP id d9443c01a7336-1f9cd92b146so1820615ad.3 for ; Fri, 28 Jun 2024 02:06:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1719565580; x=1720170380; 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=/Cv3tjdGG97YCwgOlXgEBsrvlHrrgiTFYRDjMKYVYxc=; b=ayIoKq+9wcLiXatP0UP/0cvrU19FoFvMq88R/fdA/q2RL6OTQsnQHChGKqEOdBd/Li ja+h9EXIjTApswYR72xLIxTBr8P7XjA3yioJ3EX0Xocw1X1OXDZzOJG7pAfb7I5OpyFD MQadkb/QYfASXornLkwot5iD91G/8tLVeppkRNz41G2whpkO877ACR8Ik5Lh1TQH4aJi IJZU1kEefAB/nSfNGhTDchC1WL79jGAogHZOECdsTWNFyTMmE5tCSKlg+MS2Anscihcl ePTXb00YBat0/hPynCOaWfrBYBs6bgG+7g846veQlSwJBgKypInnt64DANGmbtSQ7nvJ 0/IA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719565580; x=1720170380; 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=/Cv3tjdGG97YCwgOlXgEBsrvlHrrgiTFYRDjMKYVYxc=; b=I9rNlxBjISg5KLN+tfjcZQgzxjdgqnr2QskeAB0VNm1tWuXkcaZM2IZCSTLw/fSAy3 /jC//CBWfmkNrG9JXUPpJFlxYv7G/FT/gr4OIq08qWvOI8bIXhBMn9r6yx1bkvQIyofl 3PSrUwLdJO42Bew6kjBxVgfGWM8Eid83MUZX8VO9hutAYdVD6wynZVzecuf1ohVIgP00 9tsXZ3KpcncXYlsd+ObPqScc38Kml+ICMMxjKtYp5PrZhojZldrcI9dAX1dsLSohsApz aS1yyJNqbL03E2LuIBD5E/yjgkgObioftOgPrPT3jSpd0yzW67G2o7r/pOjKEoX4FvmV SCKw== X-Forwarded-Encrypted: i=1; AJvYcCVQmBvA9FgIgAAZQL9EfF9V4UQdSrV5ScaXMEgNVXr/umGc8BlzIa1WQXRse0I1MszYo2S4XfsDKpMt5wmtTVPn4sFnd3fL6iCtE8N+RtJ7 X-Gm-Message-State: AOJu0YzAyJwLjGRyiAAbMEXTi/KxdX3HYs2d5O9dOG8RoVLcK/GK3Ey1 pdjK8qHy4y96e0mdZImNd6OqdTNEhxPDGAit0+8XdkgH+ow2lEVp X-Google-Smtp-Source: AGHT+IHxho2IY/KPtiPSOQ+4V3CqX1HJNyhpaejcbnnpb1PpTk/59mlf95gQnvz1ll6XSDBM+JJ4SA== X-Received: by 2002:a17:903:1c3:b0:1f9:9cdd:dc76 with SMTP id d9443c01a7336-1fa1d667d19mr200814765ad.47.1719565580537; Fri, 28 Jun 2024 02:06:20 -0700 (PDT) Received: from localhost.localdomain ([39.144.106.153]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1fac10e3a1dsm10473085ad.68.2024.06.28.02.06.14 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 28 Jun 2024 02:06:19 -0700 (PDT) From: Yafang Shao To: torvalds@linux-foundation.org, laoar.shao@gmail.com Cc: akpm@linux-foundation.org, alexei.starovoitov@gmail.com, audit@vger.kernel.org, bpf@vger.kernel.org, catalin.marinas@arm.com, dri-devel@lists.freedesktop.org, ebiederm@xmission.com, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, linux-trace-kernel@vger.kernel.org, netdev@vger.kernel.org, penguin-kernel@i-love.sakura.ne.jp, rostedt@goodmis.org, selinux@vger.kernel.org, Masami Hiramatsu , Mathieu Desnoyers Subject: [PATCH v4 09/11] tracing: Replace strncpy() with __get_task_comm() Date: Fri, 28 Jun 2024 17:05:15 +0800 Message-Id: <20240628090517.17994-9-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240628090517.17994-1-laoar.shao@gmail.com> References: <20240628085750.17367-1-laoar.shao@gmail.com> <20240628090517.17994-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 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 Fri Jun 28 09:05: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: 13715815 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 2E045C3064D for ; Fri, 28 Jun 2024 09:06:31 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 39D4310EBEC; Fri, 28 Jun 2024 09:06:30 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="I3ljRlru"; dkim-atps=neutral Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) by gabe.freedesktop.org (Postfix) with ESMTPS id C3B7A10EBEC for ; Fri, 28 Jun 2024 09:06:29 +0000 (UTC) Received: by mail-pf1-f179.google.com with SMTP id d2e1a72fcca58-706b14044cbso318680b3a.2 for ; Fri, 28 Jun 2024 02:06:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1719565589; x=1720170389; 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=YS5JkKLpH7rBBlezu3RsuBopuhXkHEJwrCmaySM0BwQ=; b=I3ljRlruVY6B2fYVOlLjINeswQfEtpv4FknxuUrFDFNThOGgncfnDsledGS3g+OU+X fEFqcD6VwmMxrTMo1VMmN5+dWKD0Oq8kjGbrFPmFQtEMk3+w+LoLF7Vj0P8LkQPNfF7B eqpuPsOlizRfvijEs0BW0wYPOUgCy8LNfdaCZ4WMHR3FURPuVDsE98em78D1o0xf6XIL +tN4IlCVJNCpzBpAqmSffnSikXPl+qJfa/AKKcPVAfR5S/C1r2237oRngxYpNe/MWE9I N1tMpqY4Oo0xmu/FkVUkXE6eGpq1vSyUAFKieoVRNdslrqGowv+ln27PoifaYtqjbqSZ vTzw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719565589; x=1720170389; 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=YS5JkKLpH7rBBlezu3RsuBopuhXkHEJwrCmaySM0BwQ=; b=ePm8Ga2UFF4okkyuK7w+aw4EjgRK6ERI5uM5e5bMVfb22+NVqhG+4VcVVqQ5eMgMyk Z8MoUuLV86zU8t1kxUwgsrkzX72JnuNJWgnwPlb5RjU7bLzR7XxeUDHG1mZkHsAemj/1 tC82qCcHE1KOuNVybw63jkmsmY1Qus6sA19VNR80JKPpduSaEVb/LN+aRCLbFfOCsH2+ OKgF9KEvW+YSX/qC4/34oLF0mDQi7VH8ZeXmyhtFRwiPLPrOCzTs6cQ6aSDQhMLN/It7 T9q2ORUMO8kT5hUVDUlJcbOnNIuux5p68y12gpZikTWs7QdX0R9gz87AU8jsNiy0HXwT Em9w== X-Forwarded-Encrypted: i=1; AJvYcCWRmrtXmQ/9ubh05eF8pHQIOn+pEeQZRQ9Fvxt7EWHEUxob1YCQ0IG+9G9D1b1eFDmQ2aGfoz60xQ7J0bOX/bw6yRnMGXser7pJFfK33fi+ X-Gm-Message-State: AOJu0YwWU8mwdsjCp+M3tLsARM+oR6PYvwUnKU2vKSGIPLg8IHJnXcJO w0/hT1O3qCTbIvueARfFkbLAdTwa4KemQM5+fxDG7JuBY+UnRdgR X-Google-Smtp-Source: AGHT+IGOKlpBVO+d+BglzrA1U38WRrINDjYkTk3SrZCioD4n2rLQ44T0idrxtclcQs0Zl0I35xoQFQ== X-Received: by 2002:a05:6a20:b391:b0:1bd:2200:23f5 with SMTP id adf61e73a8af0-1bd2200244amr8943793637.39.1719565589157; Fri, 28 Jun 2024 02:06:29 -0700 (PDT) Received: from localhost.localdomain ([39.144.106.153]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1fac10e3a1dsm10473085ad.68.2024.06.28.02.06.20 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 28 Jun 2024 02:06:28 -0700 (PDT) From: Yafang Shao To: torvalds@linux-foundation.org, laoar.shao@gmail.com Cc: akpm@linux-foundation.org, alexei.starovoitov@gmail.com, audit@vger.kernel.org, bpf@vger.kernel.org, catalin.marinas@arm.com, dri-devel@lists.freedesktop.org, ebiederm@xmission.com, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, linux-trace-kernel@vger.kernel.org, netdev@vger.kernel.org, penguin-kernel@i-love.sakura.ne.jp, rostedt@goodmis.org, selinux@vger.kernel.org, "David S. Miller" , David Ahern , Eric Dumazet , Jakub Kicinski , Paolo Abeni Subject: [PATCH v4 10/11] net: Replace strcpy() with __get_task_comm() Date: Fri, 28 Jun 2024 17:05:16 +0800 Message-Id: <20240628090517.17994-10-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240628090517.17994-1-laoar.shao@gmail.com> References: <20240628085750.17367-1-laoar.shao@gmail.com> <20240628090517.17994-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 254b192c5705..10d8e8c6ca02 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 Fri Jun 28 09:05: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: 13715816 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 7C2C4C3064D for ; Fri, 28 Jun 2024 09:06:39 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id BA18010EBED; Fri, 28 Jun 2024 09:06:38 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="XclwU8xk"; dkim-atps=neutral Received: from mail-pl1-f172.google.com (mail-pl1-f172.google.com [209.85.214.172]) by gabe.freedesktop.org (Postfix) with ESMTPS id 1D98A10EBEB for ; Fri, 28 Jun 2024 09:06:37 +0000 (UTC) Received: by mail-pl1-f172.google.com with SMTP id d9443c01a7336-1faad409ca7so10102205ad.1 for ; Fri, 28 Jun 2024 02:06:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1719565596; x=1720170396; 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=uws88M38Us6mZwthahlWUK7fM6+CMa7f0lHdNpc5/ec=; b=XclwU8xk/mftjG1++58n1d7G6wgj8L/2+wfmO4SVr5ZPO1vlebBmK0/NSgyXz3aDvi UFfReis9g8xUgbtfcCNkpskXF7rG0jHC+Bb3T3W2Dus+COSgqs0PcU0BbYdvfD89HxJ6 ia7Lk3VTPF9FsucbvRd759mPHTxHX7hHIsVJmEMb5e8PMgLK6g/2D146IvxXWYGikaHu xUBdncGbAjfcpxyGWLLewTpWQgIv44MpSd7zILg4/8fRp/i5mk1KvOs7j1Z693TKLMsg /m9aBPwSB73r7NNKUC5aRGrEcdQb+e8/FiG0ioPbWQ6trR1iK2NunnrqpzBGsdDT4+jA gf2A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719565596; x=1720170396; 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=uws88M38Us6mZwthahlWUK7fM6+CMa7f0lHdNpc5/ec=; b=uXfeESHIUHkzMBMyiQsDn0D3pr7JbCH4orXGMY3aVlr1RT8THcujzWurKEnxiWK7gn kM71SrakzzFUYcrlcHQdbTOVyL4VVFZPKGF+J3/WJgJb4//4tn6ktDySmQqShC/SAG8p yolThHk4/WvmAKxDSjpRTLN4WH4PNSvbpdkUFbWuqORuZYYQ9TMjEFp9DzOIvCDkKsx4 HvfAjUJJ/iqhX0n6w1OobvN2p4/tdK6Z2vCw6SnWu2aIPcyrXuimIGdSi10/7BjOCjFq MUUzIXn0/GZ/3zteenmof0xY83gJubzxFPJTPZINTeyTletqLsreZV8ItX5TvhcQJgY+ /P8Q== X-Forwarded-Encrypted: i=1; AJvYcCXKrIYqH0OeLWSfCEk0U/c8l8dIrFGrONq+YoR46UC5zUtUrB7NUNW8/R843K1RRkzYKTu2ENJwwVJiT3cQ9nTrYn3Dt12H1ew5SMV6gPpa X-Gm-Message-State: AOJu0YzHVOTKHlgSUqjDoSWgx7QRTmpRyQXXPo6ZMxwY3C6l9aDZt1XS FTnsW/we2L1JEYWz1M/pARzqMh0WGf4/Uq2QhFu4tLLDPTDnGWvK X-Google-Smtp-Source: AGHT+IFavdIVLnysCQZL38ewD7/CaBekji3/EpakpMEZSkmUhyVQ9iiqTWsp/YgBhmvk5fMqdYz2hg== X-Received: by 2002:a17:903:244a:b0:1f9:e3fa:d932 with SMTP id d9443c01a7336-1fac7e975ffmr17035345ad.9.1719565596545; Fri, 28 Jun 2024 02:06:36 -0700 (PDT) Received: from localhost.localdomain ([39.144.106.153]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1fac10e3a1dsm10473085ad.68.2024.06.28.02.06.29 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 28 Jun 2024 02:06:36 -0700 (PDT) From: Yafang Shao To: torvalds@linux-foundation.org, laoar.shao@gmail.com Cc: akpm@linux-foundation.org, alexei.starovoitov@gmail.com, audit@vger.kernel.org, bpf@vger.kernel.org, catalin.marinas@arm.com, dri-devel@lists.freedesktop.org, ebiederm@xmission.com, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, linux-trace-kernel@vger.kernel.org, netdev@vger.kernel.org, penguin-kernel@i-love.sakura.ne.jp, rostedt@goodmis.org, selinux@vger.kernel.org, Daniel Vetter , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie Subject: [PATCH v4 11/11] drm: Replace strcpy() with __get_task_comm() Date: Fri, 28 Jun 2024 17:05:17 +0800 Message-Id: <20240628090517.17994-11-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240628090517.17994-1-laoar.shao@gmail.com> References: <20240628085750.17367-1-laoar.shao@gmail.com> <20240628090517.17994-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 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();