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: 13715765 Received: from mail-pl1-f169.google.com (mail-pl1-f169.google.com [209.85.214.169]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6DBA914388B; Fri, 28 Jun 2024 09:05:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719565531; cv=none; b=ROhelnBm1HolS3LTdc0nOjg1J2z3FBkuFPncqBQy+wEFVuC0tgNqI0GqbTg4p72Vdv3bsJR4xvkuhUNGAw4KYEqChyszUdxO9kUnN+EBBe6iqWdtmYGwAJyhO2I7bi30X3nrO0HtmUi1KuFtq9XeXWv86mAQBiQBXc8LO25XEgs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719565531; c=relaxed/simple; bh=YDO2GJm6IOYrfxyYfmn9qJYNq9J5YDMBpKRsJS71FYw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=afBivkFsdnybjUJkdKKC9SMJ+x+GfQ8OftuM7JnB+0AY2iRDGDR6plnDIb5ywgfpoYA56rgjai/bCmsfaSfXpE1XaZ3319XMF4DgX7MbghihwL02UoGMCWC9VTwqyLxXnJRxgxUuA1HBdchDROJjVr8O4CZFUqnEd//uyNaudl8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=aemOUw9l; arc=none smtp.client-ip=209.85.214.169 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="aemOUw9l" Received: by mail-pl1-f169.google.com with SMTP id d9443c01a7336-1f9ffd24262so2091235ad.0; 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=vger.kernel.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=aemOUw9lzghK2nAEzwinfvhLc+6EJuE+gNfslNNSRfMNAty9dSETrgZSlUk6fDMvHq KIZ15tFFFJryKvCSkJh56XILlBaNqMiYK70730FLbIKbBGzzFLIq5r1hMGZn9iiiRY+6 H9zn6AAS/CaOHNKDaa6i9C15ANu5RVJukw8GmBOOKyd7r7r5S5X/WfzqA4vCckIL60i4 7XrBxTLVLs1aFVmyPYAQPDfA1ACBeT4I4B45DskYFpiseSEetHuVmblQpn/uVnI0W7mS 3tTcsdvAMdAQ/OFTtbiMWnfNA33ipypqLcwwp7PNj2Z53NZuCsEQPtvUGQqQRtn/M1X5 ihjg== 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=eTwoS5k1k6yDFH/1fShi0qN3R8UdlIHP4VqUlxSHCxQLjFzcKEVeTZ4R6H/tesNGlV 76Z46RMVecMUvrCzf5eV2RB5Fupa4/qJbNx5C1HjfrUFeP97H0EUAJ2Qe/GRVZULbU68 AjR/JAWQ/63jh0DnCQq/Y0NMcJOUqTspW9t0E+p94qRsOhOtRgBJ2WpZ1q+EHO2GHCH3 eib9MvxmWU/3ffDQs/7iTfFqz9+iZwo/KneECszINP6RDRGPnGb903rXmfcWAOUAYhkR hRAQCon/etp82+/qT+6t2x4Qfo48Gp72b2STj1Y3M+Lx5s78sQ1sf6MJCVhmjlojXnoL Qldw== X-Forwarded-Encrypted: i=1; AJvYcCXjR8//uT6kbiqVdp7iex6gLgkpztKYQztdAIdNuvZGf6jPASv1ZLsg127okx6I1ztl/Ab7k0OWR6mYkRffCzp4zP34tEeq9y/lHarX3Ll07fY0EI0bThKs4PMbh9BjQV+SHGoMGWqRcAnumBg+xHYgTrDa8gN/VX+61G0tfNukMzu/qbz/aODBgR3NtvXsuH8C7tdxI+RlfA6DapuY6yGybN48UaQYOcH4Jj2imq1QNpeoNiQumG2tizJE8dhC3vO+E7L6Cr0zwSwNevtzzBd9Sk7O/axmTLHfVYS2deWEV6YKCRrv+PFVK7kM5nVJ8AIw51iLXg== X-Gm-Message-State: AOJu0YxSTyC4U4okg/Wz9TJVF3NjNjtLtN5VISoPJf1E2jaUTSVqWShE R21Sk6oQ801d46QRzYzwx624zb6IIgpeXJCRrzG5Bqy+wzS4I56YpnL/emQvyEc= 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> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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: 13715766 Received: from mail-pg1-f170.google.com (mail-pg1-f170.google.com [209.85.215.170]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0F55C14F9D5; Fri, 28 Jun 2024 09:05:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719565537; cv=none; b=H2wSWMH/4XgWwCs1Led8Z9U9sLEP3IpObzq3ysv1gqitritx/iQfQS5v0d8iTlul6kEMmp1KDR5qFnf8urXd1CG5en7VOq9FbriLYoG7wO17sR9SM5yL2QwxbRv1+W/VkLOvvZ5e8pIDeJJR1qtnjkRR/nMZSox6xw5WYg41Yho= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719565537; c=relaxed/simple; bh=RhhRTzGzjqJG2IuRKewfcKjpBWjGGob4kNd4UpTBjcc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=TibJQtwZZmOmbYmFG/H5TYKjnbkqAweH7NHFfYydOp5vB+wjYi2i5/lj1oedJNuX1YdmwB9uArK1hKA47pWpmjU5h2Z2U2+NOA0hajflb1v1wOvx4dCUIXeysC2ZX4o1PTcC9889nidqehjXyI52po8YP1KhjECLLUBbj8dzI8c= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=LxnF5IEL; arc=none smtp.client-ip=209.85.215.170 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="LxnF5IEL" Received: by mail-pg1-f170.google.com with SMTP id 41be03b00d2f7-6eab07ae82bso242476a12.3; 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=vger.kernel.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=LxnF5IELEJI/qnkaXz7mYZMz3A1D4qMP46Garx/pteNIn9li8vwwlpoUxbAltKhrsX 6Zu7PnZj7yjZYWyAEtOZwv/37eYzXSnx2o+IWKQ4Sbw/S6YSIEL7XPIpTINEHZ/E9+MO ZuMq6m3XAH3vNB5xKy7IAmK49VM0e3ZvSgw5GG35Lyolf4LZ4cLG+cHOOhrmw1GNfShF z9p6Hewa+kPU52WIO88VCDG+4d1Lh9A2lVi5v9+xcj2I4NYSGV2y5CJH+RFAwNkfyyjm tK6hQEFJl8s9forSjDNdm0mPt2Y5pwgSLgPchxYsgIHYvfkWBhLGVidfh1+FIHmmYjSU PVbA== 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=r68zthh3X+j4uUhuPqq7MSKYl9A4WyNfMdxdU7Ju08Rk7yQzRKd2kGRs2TsHK5NhOG oxMVWF2tK2WzZnQGimsrMJ0uooQNNMCJBe0/MFyWDs1M+xzcjU8AoAEJeL7n85YgWW7X jXperA+PKE3q/jZm8vq9SQk/HFAUDlLTxpl+OdxE4fzqFGiFy6OrAucymgtDsxMQEYU4 WBKZAdv9sfpKNmuLJBOeVgEJwlWLwkg5SoltK3xyHOE6fd6xkR9QhmAtNpDTwKQzfpQU lxbaTnrav0F7edBMIJ5y/I4/IEln+VzDdhavSEPnvbmv5NpEFheKBRV9oF7VjG9SOfNk bsFQ== X-Forwarded-Encrypted: i=1; AJvYcCWu+4NuKgLboBVzPrQ778gSSLA54E4CfQCBEdptQNgH7qsRcq5ACcxC8diw5yTwxYQSpZ5kP3CJ7WtbpJxPXNg1y9s4wWfibB0gifKtfAt89jeOANb1PFi8pjAgcB9xmYotVX/OObbsKdvKsaJ83Xrv6WOFMfMYpgas6nCJT5M/EyiR1ohIL02AsTkxlveBPsyD8a9Z6irYNZl6HsW278LVuuJZVTXgmiT0T3QVCv8VYpOqIw/6PJ3mOEPKN7azI6HTbgjmDsnBR33P2V4R0fv0tWI9Nl4/OzsEYtdkkNk4sKxzUYqKNdtjPYf3o1ZzBMKkp4BZKA== X-Gm-Message-State: AOJu0YwYZnHzmt6YYk1QBlqPJ/O7cR8J3WYE164JbusknWdBT5XnV1Sn I3cZAGcTXnOKECgVzvIGvz8Yg4lWFsBfI472VvoD0T7YBsBEmIfZmMPBqjXVntU= 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> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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: 13715767 Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BEC7013FD9C; Fri, 28 Jun 2024 09:05:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719565544; cv=none; b=ewNaPDOkRNgp0yLp2xxDU+vPtbXWU7Iqcc5NRAkjA6N9YWwZrB3Q5HLPoGb0poR5/hHX6vZCTjrSWeqX2z8MS9OnhT6zvW9QWFbaeYX+Qk3s9Makcl7YDB84HY+4vmT4He8OGyt05zaa2tfikeRfi8lK3SFYMPC0PPwnaJKAW40= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719565544; c=relaxed/simple; bh=FkchoV1pKKhU4iF2H0UnfhLXdjU/OqthurkRTQdk9Rc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ovaLBlf+luMeuMPAJbRKWmhZ1R99I2NjwvkOJhz5sTOspsomCN9asxijuTJ9qCU/vVfmvEUO6vuW4D1YWndbTTotg+QNABrIlcwUEYvXDkhD5xAJkUBxTc2vO7nm2f3B91C3/YhYvKD0+7L7759UMXwASTybofFxp7DfgMWB9Js= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=fFo3oWPk; arc=none smtp.client-ip=209.85.214.174 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="fFo3oWPk" Received: by mail-pl1-f174.google.com with SMTP id d9443c01a7336-1f6fabe9da3so2232635ad.0; 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=vger.kernel.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=fFo3oWPkGNgcH9wn9coWFJSsc7w/nPhxAi1rlFcGhBUXvUOLycYhHYE3ymiQHjjGW9 XlBrZ1YvwwJ1g+A7MT38DWS43HWHrDlDEezq4jBMxXobGVbUNKtjE536G5yeo3YACIk9 LgLqHwncd8rHI1OxGZriU64/27596tu+X1jWEWWAW0lcJl1CjFwhh3J7QcFbU1KniKn8 P5u8aTRH1xPr/kIU6mIoHAO9S+XDSlTpcEBlg/zNWTbJnjQGInODkdeJVOLMAB8PzD5t I3F4LnISxZf9bX4/THK44n/COlP1/X4XpeTN1oyN+6HkGC0sXqmOqbmcv7IG34Qeyu4S aotQ== 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=RQj0clfgzD/HQaI8h3EQbIL6GSP2tokr2A04eft/5ZuWHsfhciRnxTEpJf4U8tjzml ixUs6J9fzOGc3KLcr8VR/JAbsWITkctOKs8/1j10a5bzdFI6W6VpHs3Tb3yWXs7c54bY ijDLqIqXJ3sAVzJ4AAjuDRlN/xoJ1HUBstf2uT0aAwwDSOXP5PqFONLY/5kj/HzYRSGN FVdehhtwneTiRgD6fWzvBSAdl22gpPA4yxaX1lD5rryzqMwFTOZWnECaZH/FSbUUFLNW pd9jBFd+sZ+PLtxVTZDLy/lurJnV0r4dR+CWm1vZF+UhpDUMRafnzWKJYROBWFSYxcSq 1r0g== X-Forwarded-Encrypted: i=1; AJvYcCWXotgOz4FNmN18RRhK9JTBhhjFrVcCgZuZAEEIVTZknSrAkHhZj26gNH6SfS9isN3jIIik5GfntMr3hjE/4fbDfkD5L6Vd06vVt9tP8BPjMUbrCrtvpHyJhdo0UAlmI93NCgKs5l1C55gDVXrbdh31uvGK8xk3Cmj9sGw2r4J9DYJ97IVUVnk5HhNkFxyBvcshzpeazr2e8Ls115U3ySq/1jzIizrYhg36vk7rbITje7+lG7psZzI0VzZ2rWNdeEPLkT0FH9NgHy6jlwL/sZezlftxOmQm5bl9l/qD9zzsxF+R4OzKeLvNXcmRasQefM+FjkGF0w== X-Gm-Message-State: AOJu0Yza4QsNV8brvp3Bd3Y3m3EZMLEAotovwThzksLdH3swkDaf2iQZ UCzzbOwr8ny0gtsyIf4HfWIm+TCqpbkvElXMEUKgsCxQomIFBSno 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> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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: 13715768 Received: from mail-pg1-f178.google.com (mail-pg1-f178.google.com [209.85.215.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1EF3913FD9C; Fri, 28 Jun 2024 09:05:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719565550; cv=none; b=LJ7sAvZaeNmnqkq7Lc+ZBO06US4jDJm5I13RjiHQu37pETwNoF5oFOeZVbc9SwBgPp8Es4vG7iYwVcu8yYywbfPCDIfuX8ClMtv7tyeB1AuH0Tc6TMu3NERD0LdOJlRZ2zufAaX7E0Slqb1JEfZ0NQHb9PizjhWuGYUGQNGTrTw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719565550; c=relaxed/simple; bh=56ESQtaoBbBqZU/saKgTljkr5U/isylgWUhSQpUUDZM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=M03Q6QZxGCfN7JcrGvBKx0wkHd2j8FZFRAv36tQ05X9P4Apo+nyebGbA6/B2ES5xoX3g7vu+kHAJclbaPgGSKGqZG/CEhGyvKEjgXAyRMuN9L0HyKEzSbOAjSCTjojFuOW0gcyWiMlsdIFpBQ4elg74M+e3upjynubl6SjQHBl0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=CBAiivsm; arc=none smtp.client-ip=209.85.215.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="CBAiivsm" Received: by mail-pg1-f178.google.com with SMTP id 41be03b00d2f7-6e7e23b42c3so215745a12.1; 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=vger.kernel.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=CBAiivsmAqatmPO8Mr+bbLPhjY6pNO10+HWKtWPdNbYcB0z0Ar9r8Z6xT838uTzZHz 2u1OLarW77kbSC5GFi1ubd8/e044PKRai/T7Ia4xa6+T6WDa1/kni3NAv6iswFFGpILH WEeCnfw8YJW66GCbiLED6RAq0tsnPb5J4CkSLYVfewXS4t7fkN4iBEO0GnTCS+xCTRzb 2XCrqX69nw6lr/Dbm0WjgKblSKa7INUYeqTqY0WCfRcFQD6s5rvjqsIDz2L7vjbD9yk/ GgqMd4Ha7kVn1aNuKKv29nsZAowEDpJgaWn9eXDF92ToqZCrjNBugp+8Uc8QCjwl0hGL UGyw== 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=uvlLqjBqS5Wzlg9ktXWQ2qIGV4Mb+rHJjFcjlI+AAr9gWLhA12a68qaDgTvGQ1vJxm 8CYMOM9yq+oK97AJ5LroRyGXuC4NgBIkjlS7l+gU6PW/bKSFFAi3aazPT/RVFWaNoFjg 4xyy/fwap2Onc7dQd8ZUAqf2yumiX14V7E57Val/UtFDe2JNhkW/CpWvUmMPbEMMrA3S nYTB+S7NJE25X2JmfRduQzvu9Hl9XqdiYwDMvYMjPFmTb3GgsJwa27BT6zDH0qI7pq7e c1O/H5bPp2mqPb2tx+t4Z0TDuRy2b0P1Loko0ZrN8bygaR6zV8qdyAQvb2ej5dICncKo ahIQ== X-Forwarded-Encrypted: i=1; AJvYcCVDjaNFRs+JDDudWMVY6MO+X2rffied4mth85cCavMqV+HuGs6+SXkqgddXlosd/h65SqkpGIgaqa3DscgrrI6j0eidBzs3jSzueHCezngKDnmOqwAY7pnzbkN/9bmDyJfskys71n761icVT56wQxduNOpPwxGBpEdIXCgVSDfSXrpRccJ0dCzMGh82mJTDD2Y9ibUees4sYH++b3SEYHxVIJpayt+3wH4WoURTlZuQESYKWGy0iTKPIiLK/UPsq+0tmXvNVfzdDAdbTHlRbFkiB62fVHUky5csiSnTEIXI0VRbP6u2firci5PNobGONnbu/rGetA== X-Gm-Message-State: AOJu0YzQvHI9H8Wg5lpJ0TsOwmwXJSthbdCh76f/HfFrj8lFBXcnwpbC cOT1Ewnxsm14pfpG4V4f1NECeslHWBDyMmHBbMni2nb4VcrsZAFJ 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> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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: 13715769 Received: from mail-pg1-f175.google.com (mail-pg1-f175.google.com [209.85.215.175]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6FE2F14F9FF; Fri, 28 Jun 2024 09:05:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719565556; cv=none; b=mX9piUg9d+ujaWCaxTf3RXDIyIkfeK80DAoreN5sgT7kNZSTxuzxyEm8jLbE4RDLVZoH6cgVrzZRUpzN4HvZVgRfUMOuZnnnF8eYWBKkA5E8kJ5XdLCnAqKZAwDx79B9jgbgRUTr4DSyTLm2FbA/F5pKiX0U64Ut4JMyXw14n0c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719565556; c=relaxed/simple; bh=LGO/jG7Tv89MIg13raRY5z42pAUoAbxhcnib171V4yA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ldMfVM/lhdbOTmmxvx6s+8+8AI0AJhLngGujmwoTe0Rm8gFb6VbykZPXEZ+cH9wAMRC8nHjkFlX3RY9uGx5DyPKtN2sON9u6fUZrCq6IvWE5rVnQag2XOBVg8Q5fSeDosomq4/VUr/2vsokVmbieMDGHrqfy+aSAkUhyhjq8wxQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=ilGcRyhw; arc=none smtp.client-ip=209.85.215.175 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="ilGcRyhw" Received: by mail-pg1-f175.google.com with SMTP id 41be03b00d2f7-7226821ad86so233933a12.2; 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=vger.kernel.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=ilGcRyhwdv9aN6E5OCpfdwXYSIFUnL47k41DOtkTPo95zHMkDg+Amb92wt/kccMsbC FJ+9oGQbv1rCQvpVU7z71VMs4Momb+4KY34bMSUugstGdR8/J/whqUOxXBZGVhRJaBkm kdzlexvwLd5X4p4I71refgnwoev3byv//TZBrW8st3glKRrFcQg8dT0pUtvz0Cnc0S00 oK17CwT9VVaguCJqAjh3bgxZtqrOS9+TSoeLL32LAoMam5j52+4C2J10xACXqbIoIt1F 2a1rUymeU2p/ejhqe2/YsBdoitZ63MoZS8UIyanPRxHaWh98oyHsa6FN/1QkVX7GV+71 fsCw== 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=am8hw9xbShRwhBUfC3OZkOdNHfvWdASu9yAuwsY+iU1ezYB0xJ1yrMgG4Jh2Q/NHRG wGO8w2Q9MWJDFtm++omR8NgB/Kb0jq0nAQdIsKv4RkKZjwQpv1SKn8S2zL3oyOr3/9uL wqfiC9GUpO6pIxsTTxDJi633Kqgun0io5weUQcnfB/MONnCLcRVD29s+GV4qbMx1+I1g fewZkeqSbnwAUofkVcLaJGe1i2C6swrdWWRJOmXydnbSRt5k9cJ4/1jPHZ1YGbaJJLqC zMWS4xzF9pvBeIf4gIDRky02hTLgEcG0VICHW2jGS0xCLXQ42df/LD+p8/Xt4h06GRYX lPyg== X-Forwarded-Encrypted: i=1; AJvYcCUQmag/HJABDbu2rf7vvE9NOY3q6hbwbmggfXK3hAViJWAt8553WBogcm6MRKGtRXyxj1CMVcnGpby1rETukgV3xlUPi7Q7gwhdV0ad1q6RisDixn6ElSHwRa9zulLK1VGnaeKxQAr0KDDC3ZSu2g2sfCXG4iNoIsZ4BE3ls6ZZAVAhAPhW9LUYz1dIwutLnnXKVcgA4VOFr1OsU+nzcSbAvcqKJHC9WGipvu/tQHON3cJ0SvudoNQi0vw/v7ME2yIsBvU0//FFLXa7rUUrV1s9OdZtDf7k1bYDXZE+HM/nL7oo//Yrf46jq0Lqfv7cHogWH8s+vQ== X-Gm-Message-State: AOJu0YwpMofFmef1EcEAuQx2eVNieluoRWb8kPgfHmW6AcaKtzvgu15T 3GgCUnb62pocxjGGOVbG4dUKigO7NgEel9VL0S5ZEDzpLT0Gxkbw 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> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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: 13715770 Received: from mail-pl1-f180.google.com (mail-pl1-f180.google.com [209.85.214.180]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5BDAE14D719; Fri, 28 Jun 2024 09:06:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719565563; cv=none; b=f+YBkjdlG3Ey7shtfRnUDYi8oNa+1w0IJ12pZkevrFAv7ze+59K+n8sJH0lgdagj6oGQa2fY2FFmC79qJYtLVx2tnjwSkKFmYIgiO+IOL5RLxhZcjwbmTypmY8IF7c0M+4/tPBQOK8QuRAzZRUEkQDO8mkZ4lXKlc7v++I1fmHw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719565563; c=relaxed/simple; bh=inPPFHuZm+7fEeywe6ogK4qmICCbgmhVjJQRSqf84EE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=XdANDqR/z4AlTPnfGLFJNQE7BbO4oS5sWkASbYe0Ic9z0wZ9hXVqTUSW9ZPqut/0fy9Q4atYq0EHitaVs3OAbzqkr8FRVX2k1DoHbcFnUDxI7Msw5CYGFrKRfzQcXiqBNMcAxDVYQjENSDQ38f6FPWzQBDlsByMBXZvlVpMFXRw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=jSIx8kUG; arc=none smtp.client-ip=209.85.214.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="jSIx8kUG" Received: by mail-pl1-f180.google.com with SMTP id d9443c01a7336-1fa78306796so2162135ad.3; 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=vger.kernel.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=jSIx8kUGU6V0zcqoDiuJBB56i42/XOJ9rcF+cwaz++3wR3U/UgbFjI5BvFczQE5FDY 8jonScrWgXhStel2Wv5v1tT7w/o92QLN61x169ha2IoZLnrnXSQp9hk2/JO7zSgc/h8i tiPgwvN9n0eOoU4XrLYgo1qeOwjCJX6heE5XHUW+e2aFUu8mHiqBT1YxqcW0AJXAFSfF wALr0NgazWiAqP3PqiE3FcnCJqmC/U/Pc2Qv4pVgGcgRQ2MVJPlFSrCV6HeFI9omXxCk yl3e2Zon/mtZHuk7XFTGroo1nhPl3rtF56Wphnefyd3xCQgD7sCzeWDwbABdhF3O5QOS atLA== 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=YWbuu+fBV4BcUVBCYvt60muB00187ChI285PuqaWo/tOAxYBZW0b2n9tJV33/moMxW 1Snv4oG5PLJ1TMAnsFj/YCh8dYi3kQ4Ed2g6Ef09R/T89NvpVbUmp/+UNKQnaC4XHJjb sNFjAO4rtFdOh4Ijn4ROQMkYxzQd3Jpobz8QuEu/+1pFUSEovZn/96ioXPx6S3johqjk MDz+AFJBssW9Ohgh77VCqCNyCbniBQPEX+gsDf5JPfmWGHvU9MpMBgC3CRHS72ASScQZ KfGxXKC1TidYbhrsMEoUsmxyFE2F22xVb0wyCudWVua4eQjZ9kzMz3H6Yb8JGZ64Mx1i bcBg== X-Forwarded-Encrypted: i=1; AJvYcCWQQaR6k5Dyw3uWSFK/F3afFMVj0cb7HZqhNHu1A6KyRKC/uRQkwoo4panScCgSP7SkYPAxu/DiFl4zoNfnQOa3hckNA/cRRlWWb7yJ0B+yiiFuCC3hDjDwZPlu2qUI8NO+N+R3V4pFd5eHFHanyjU6YjNq1/o+1Eocs3uj0+mnZ4ky5dwBq/PqFdY1tR5hRBeyExPm7xQHwc7GavBLWfdnjRtAsJNa8UB2//YG94xqts1ChpyEHAoG5eIzMpC3t2KgobZQH0hra6x+6T8g4g5J9kNZ3qSroIKfACubLV9eu7M4+mJrqqYMoP/9QHT9K5AQBy8siA== X-Gm-Message-State: AOJu0YzefTmfZkiF34Np8oTV5RYd9YaieleUB9N3S1yovJ2MYr/eMUB3 7uzTB7xvtZ5Il79y6FnvoQ19CF5aUP3tcMECgTtr2jLmwAuSYC8T 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> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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: 13715771 Received: from mail-pl1-f178.google.com (mail-pl1-f178.google.com [209.85.214.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 70CF7155747; Fri, 28 Jun 2024 09:06:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719565569; cv=none; b=FElxpa5CwhT6F0EEVE+3wS5tYwFYuQoBIXNHEXCGvcM5W1JDpK79dcDtmEYqGa6NrP9Xybj7eK7+qA3f1QmW8ZnNdK22ccwNhRssrKI44DIFvD1sRxTyV4ZrUIEo0XgoxcS5omyj6Lim9XHOz/6/vtDztGlreY/uQTJp9ve1dvg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719565569; c=relaxed/simple; bh=bu1CYcoPH46NrrB7Vm16hCPiv2fWyMUG+bX0JU8hjFk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=TkIvQrttU/wN210nTvh+/ZVV4f1nrPAm2nrle/W+SeoW8LKgRpLC9cefGaTnfg3WuGjeJuM6EMrSCvCkY1AqxDV9L65tCZUjChDoXCCM8uJWhGZADEYeu4zaVq0qIkP6IVziinoHvESqOAPYo49B1czBsd+RXu3J1zwXCQC32RM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Q7HoDE6G; arc=none smtp.client-ip=209.85.214.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Q7HoDE6G" Received: by mail-pl1-f178.google.com with SMTP id d9443c01a7336-1fa07e4f44eso1913455ad.2; 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=vger.kernel.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=Q7HoDE6GAbFzH26ArtsU8PmiWntFEdSDalJLzI25IQCfEvOlS0kfdnE9GENY9AdOOq U7FCiMmF86pHrTA3eEKdvvWweZTrzQPU4rNCGee4vU5xr2BcmpxhBM/lqSYAY5O/muaZ iFNykK7vQQbEo6m3Md+epm8n+LuOC6aR47mNaIA3iD7wWTrVWz/dienEiVi9Y/F7Ee6E 57t1T1lME8WZAFC4z9Ctpp98oopKbSkJlwF4IXyHs7orpNwSzTYS8Yki5T4gSGE0rlS3 I12C9ZyOOA2tNSSvWx0+h7+lax4scJrS/QVNyDj2TXDdkzfkfOG+PsLteoWtbM9+c0mL CUCg== 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=H8LRy6BKdoHHMHX2coVtrLR+p/wUFsF+fnAQ3fH2euZTkSGAdHpHnSefbXc8nYxmze 2kbZ21akJPbPWsvvKdSBnne15ck13cKkeIubpB1cm+AZCSuDPCZBtksUHq7oZ7vdHRSD //tzwBsQC5UarBKyMiAYEqCzAmQjIqYzUN6wIrjzqfgAP+gEZt+PzJWLL7wedoIIF/7j ry5fwdGG0IoBB0Qy3cYXDAatf15wehYJ1kMnEkmMGATjmEYsytXG4OC0OYwn5sYYom1K J3MFzacSRN+RTd7ju/vt91DBb0sM/+qV6M/V027RZhnqyASWTwCDJstgD2uSJAEF3wa5 G2Mg== X-Forwarded-Encrypted: i=1; AJvYcCX93kwKLhzYQKUKLhSfI5405i/8h92I84OMiTJuSeR6rrmCAAgcSmh13/k87+H6K1BAsoIsQBSnJH5YYDHNiIee7uPkKh5cTC9vuB/KUJMaoDSI/zQWm67pwUJfXVE13ji/gPEIBUzgiRTef3Sba1yxausjJCzLSbsgjxEDZihETOOglLWlvVIW3J1thx31eZXI0Lp/kyb2ZOTVcr4tMPT/3cQU7AMzbwOVZsYW5F4QXbzbluxswu3DYfhoQfyU7V7Olp8Svx+iK2TUFDTl6sx8AQ/7tnCL1mfd1zHAkVmqotiyH/AN9rzX76eM6XLr32LQqVdUMQ== X-Gm-Message-State: AOJu0Yz93plRc/ZCfKtewK/kXKsbcCQXDKcDJLi9/kzuTsVFJUKVtwFk d0KlOxuZ/ZCODsNwWjqoryJ/OCexByWsYds+Ih/6usJT+nrQuvLz 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> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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: 13715772 Received: from mail-pl1-f176.google.com (mail-pl1-f176.google.com [209.85.214.176]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A5C1E14F11E; Fri, 28 Jun 2024 09:06:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719565576; cv=none; b=IPztj0NXuhZDegbrVFQzVy8nFotjSTi3JP3bNoDbVZY4Kh/wc82vc9/WvAnMl1g4Qh1DDJ1sXdO6+cInKSYmmZapuFBRLP75xaOwtfP1i7KBwN/8/oGxrVPr4pPT+atVKKIbCG8RExL8UpnAxn8bujVVd0YazH5nGlgS1NNTT4s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719565576; c=relaxed/simple; bh=TGYLyUCIjyALT4hs8afmO6j0YAO8SFsfcw/1VlLlPhw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=UIx25xf/lJDz36GxmB4Fql+7rOwPRX2XdSh+miiY1VIxPNUJLHJYq85gdQx8/TnPVPWiFmqtmy3gNmWVHurmR+FSyJlOkjMdq0zmMPDqQDeD+BQU4E970DD4nzxIB4A4XPVDY8qN36CTNgOD6JW00eZ/6ax0LisVBVMEDArOotk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=hS3gq1YA; arc=none smtp.client-ip=209.85.214.176 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="hS3gq1YA" Received: by mail-pl1-f176.google.com with SMTP id d9443c01a7336-1f9cd92b146so1820125ad.3; 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=vger.kernel.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=hS3gq1YAOcjThymW8mmS00o9oPQwacKMt3QVqkjVtPH7WF5LReVafW93AG57JDD05e 5ADo6t7II4CUL6jVKKpNab2nT8CRGk0KY226rialVEt0N6uGjZ371onancryoz3qUy4M xzzw9GMNzdgZM1b6JbnBRyK85nDNknAZHqVfqaN8ebTF0LdBSsNXACoJNiEZA0CceYRS JkRZIJ40xvl9d9x4z/VyU/Ur/rzcu+NmgG6hxoa1N+q6aSl+AHwLNoFwa4J/5O5P3l+X K3Uzxip85ybasGhrC3uAk4Ra7Fo++GhAfB7aT3WCt1iJfZYUkDN6pzmQae8YQzNb2mG0 aUMA== 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=kmzoJFMIi9FLvWFJCCBxtXOg3kRTnOimM8YfUyX/Fe4vbt8gX420QgwszqMA6pXXZD haB/ZVH2COjA8TLO99COPuyUJOCUg2kx7CZhSnaCFzhEEP3LMHhgV1t/pltR/Ehym/53 9QCsHsZl9mBwo6oRwK8MbbcsW2x1rSGeVWn9BNpeJq0AIMybGhhy0C3wSt4Ei40HFx1y dJmbPKI7JmCVMfL6jik2W3szkFF+g7/EO773xFS7oGqmgVU1R7sej8ENpbTfVVcxMvNo fUTY6Uq5lJZ4J2zmzgeG+tQESioVdKVFh2E6/Kzmou7g34p2GkI0aofyeEX8rrhu0Zbv nzQw== X-Forwarded-Encrypted: i=1; AJvYcCWCq86wSQ5PY+iytlUmFV0X2AxfNWjRX3bN4SKNCoVn7hDN/V6KFrFs7oOvlM8nktUkWxCqlQc5iMoynFjdhvS9rtKM6dwjmI89vHTnB8XOM3vBbn9vtthHdORAuFpA2At4ZvwDRKwJ54qK23kL3XvG0LM7r5Tg5O0y/PjUfCylNlbZL0CTOtEfNDCzCyS0BMj4gaHSU4uyvWDr7Nu8w7vNmJ8REI1huZrYLFgKtSEJgLo4tMJGt3ND8addwCUOg7QUgWTNVNQhXSQ5D/FkhDvVZPz0xT0bNjTmjff3eOhH00dm8jcyc4550DxpNO70Vjwl59pdtg== X-Gm-Message-State: AOJu0Yy1+L/SBXEOC5l7M2sjmKcIBfkPzgrDIK/bfN0xyppaq4vnmCs1 uZvPDYN1hLbBuut6Nn3wSt5zoymbEv+67e0qcEKRbL7PK+PA2Eg+ 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> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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: 13715773 Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4043015217B; Fri, 28 Jun 2024 09:06:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719565582; cv=none; b=t+SC6v5b7baA9692ZJHl3nJrGF67hKgmt5Thjvf16Yc25Hco+IEyYtNBkBTPlhNN5lLKhA6WYzqY5gnVLh/otnPbv1TvWsFS8Y4mGWJYhqA6XVqjcEWKtZtt/KZVZZuBkhkQTZzoGgu/tTZAgBYZX2M3wKysHOPpr6zbAbX1UBk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719565582; c=relaxed/simple; bh=MRhgjtwGX2vJNzrN2ayxMYZ3geY9fsSR5HOs6exTklc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=GGVoWfJ0U0G396+NdH/SJYomypF3lYnTIZfGsw2BzT71djSAMlTrmraSn+ODxUYkRSQC+SiH5Vk3PvzrSBu6G2HSHot7uc8EFp+HDT9hq0XAyRqFlY8vbqVfykZ86Xo2BM+IeEMK6gprZFATbYIH5G0m2hETszYBSsAhJzU6LYc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=knfkixXQ; arc=none smtp.client-ip=209.85.214.174 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="knfkixXQ" Received: by mail-pl1-f174.google.com with SMTP id d9443c01a7336-1f480624d10so2276965ad.1; 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=vger.kernel.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=knfkixXQ3TW2vjRvM84nBeVPHhGrsITUdG1E27kw0EKFT9nNcrNMNA0D5D8xacuDQt CsvAmp7DXAcKadMearvGc9t/pcgWwEhW81wkn4dsIFRjHl+YgI1YGBvqIY/aHG91FUt4 +WczwFqoDNlxa4hKcXCLbLKQCHM+lZvPSGG/KF+BX7XRho8d9qsr8raoLKr+mDp/B+Pz aL6a3ioBRTo1PfmjnC8hBqAuMzxV9Bm9Vm6mCwrPAxiBURb945XE7M5VCkcWY40jDyyw Ql4c/NdAHOH8ApMGDLIdTNRPbtnXWLCSena9pQTA8mep5A9YcVtQy1XzAdtpvBRdO0FR SZSg== 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=kQdCQPFe3CPjXcXsrj40btdnNzWVpS6D84Ycdy9WxKu8J8ifuyLRYDj674oJiZEUvx o1ORYyZIJl3r5A2N9NiBRL+/k/Uuen/pZJr5GL9cm5Yt1+68oObE545zWWSyBDNiCAKw XjYURAYWGnfZ/m8ccpn6FWKUAActv++jhb+10NkHRHp3O6i/1YGQ+jIKhWbpyvRfX7/D hJmzD26Z46AbbGs6eVKXxW9PL/Yu7ZyMxuQPfCfOQ5zFjSwBsu7sUZwOWhawsWBF8gbd mTwO9lzJRw5dtnu+QptjNg1l1tefGbrxusgVclXzOh6NLje2rvZbDJzUoVSUUGFU1BA+ /gzQ== X-Forwarded-Encrypted: i=1; AJvYcCXFolFp2bDjNCKliK51kggPiMH5lpOOUNknOmomj4DNJymhWMgi0lMnnqood9I+cGSuddQfuqfNECGMwhAKCAU/gG+5D5F3yxxOtB79oR82jEaLj2w1ifxoHVcUUk0dwI7whdRNytXUva1eKBSRC05BQMBSQddJnoR1af+SJ/oCFQTwA+MgTvecYVFriGWvux/DTHVAnAe+bVDP2f9nSQt680TAArvSevCvzO7BJZVK1eceFzyF/+e79GN5yEeyIhozDp5LKzdEvEyFRAmLREYPVAiJ2hXHtkoxfEwAz4daERrPyxi49VYFg+xF0KZNckS4FK0FVA== X-Gm-Message-State: AOJu0YxMaX4qshIwvZOysFIyXNUekBgVCHhW7GVAErU55QJx+vFN4/0C clQ2+nLyqST/sEmx9VLzBI4pXm+7xzxheyqoNqdUTgfyxePPqBGN 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> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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: 13715774 Received: from mail-pg1-f181.google.com (mail-pg1-f181.google.com [209.85.215.181]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D828D14E2F9; Fri, 28 Jun 2024 09:06:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719565591; cv=none; b=WUdV3MzE3BmFP493N9LwWuPkbv55I96g3PUdoQ+/v2r7X2TFkppPckk32x+x5qEEElLo5XJdBuu9fg2BMnNnMM96Nc8x2k+fXgKxaWN/eP36rtmqP653g8we58tza8StrI2QEkAfDrNpLThNx9w81xgCZOwqTBsqbQ5mK19YXrY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719565591; c=relaxed/simple; bh=wcSkD7vIjfQlJo/ird14KPUKFpdk4Q6HJbv4EbwFBc8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=OjFApsmyE5uqaNae9m5xFIHXFfpQMsT8a5PIuIZHwboVRICkTwSSUuQcvAWZjNp1AdIMzoAugpZCGQwFWQN4eH8fv9VeHbqccbIt8d7KXAlrighQm93ftcRcuaFXKQcANjsDBDeqZ2/isQtFMkvCzpZHVb8Ei4qk3qFcKe5lSt8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Ie4YukQd; arc=none smtp.client-ip=209.85.215.181 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Ie4YukQd" Received: by mail-pg1-f181.google.com with SMTP id 41be03b00d2f7-718b714d362so230193a12.0; 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=vger.kernel.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=Ie4YukQdo5yG0/exL8gnrHKq13FLKvw6mFz70plGXXVsRZJkOzxoCPh+WYcIMB8U24 Eta5ts/AoyEKaNjmH1f4tj0zwBnGn8WqLOf420W0zXg1x1lN7CZqGBqWVKeYF5XK15XD BkOE34jOs/0HUg3wZcMlEIyjuaZYrTxGAJQRxtiXH/oiVt62MO1rn6Swsia96ugi5rl3 t3vdLvSAUyoiBCacM1a2Y743brwC5O5H1roJ/nAxrH6izwN8P4pZvREsVsFmtB4yhfvR pfFKoSXzkTo//KXZgBXvckVhpO9ysOp3KZib86nIAaCzseQ/knzfgMjclqBiMZfeD4Nu GkFw== 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=SvHoA6G8S2FQPi2YQ9AzDRGKrF/Mxt0oLsoRpkCoaOx5cRS9It6kG82e5wtoni3lRu XKU5225J0xaH0CrJPiEI0zumtYq+UnsbCw3sMf+STTBrhpB6/eJQtjJJ7RWqIiQTf2Ee TNWO+t0PiwES1FpD4jV/W7L/j426U63BomyJVbbv0wR5uMlz34KrCNHSGkV3beFblml+ d0n2ra67Wsjmnm0g2uGvaHQbcmLzV08uk0ioOr7hfuKELw427501pjVZWF6KtBl7QE7b N7K93JICvFHHfGOF2ctAytxhPDL+AYxNaQFq4BnNp5vsh4PrB7HIunXR3jgZmCCZhByu ALXw== X-Forwarded-Encrypted: i=1; AJvYcCVm7pzmre3nkRjbyxTynSUj+Lmq5uR7XDtMOiC7ygdNiLC8+C9htgwjodVCYhAIwecFghZ5hGSxVsAUjSqGgKQ8SX9nztiiBabq85afj0pwr2XxCswhWkpFAtTopcB6oIRzfmGDaAOLO0Do7vqrJ2M0fGxDkaE774QUfjkw+UWEZqeFL/2KfeXhvmaxQxdqgb4yNkHnncnXbST5SCWVwcmjWi1AwrYV1pg/BxKPoMI96iFJ5bbGlG4Whq61N71/uh+VlashJrHiVeFeWiIE4bk42rptb4u3VaSeUhU//4L/JYUMX2vT+hwC/ktjg2kBUIbMt0N4Nw== X-Gm-Message-State: AOJu0YyZdwhGMDk2mNk+z5q9C5/ueQMhy9MzqOn7suj7vdMMsfFLQTeX SVKJ30npeg0ekoEDtmlzW7Tq65UHt9+tKFr8kDNRbkM+CF2dkNzA 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> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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: 13715775 Received: from mail-pl1-f178.google.com (mail-pl1-f178.google.com [209.85.214.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6616914E2E3; Fri, 28 Jun 2024 09:06:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719565598; cv=none; b=S4Vpm5hZQGboXCkSmbF1OG+DqlmybGP3f8Knf4DNlCT0XbA8YgnNj9LNs+fYzGlAm36smi2tJ2dSYfK0nL95ICExdZuKDU5aj1BLwsMyIgC+8oGYhgTcKEaQxU+kO92GmatbAb4bbUxDUcQInQpWa96UAFRVlWvt5sqbYa02EPU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719565598; c=relaxed/simple; bh=KFR6O5apdaE005aia/eSIy/aDnoPWMXgYn13tfLkB8g=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=EKRfcSsidmOIy6o9QYj676pssnQcSPNpF+5xmJwnzFXvVN+YMaqyW0kLXLczpjC9cC8N7nLiRyE6yeK9NZX79A68wRJ2iZL0UW4T4AM1fqgqFtcOVnAxSgWtMLWONuraGXsdgszA4M+Q/t/T+C62eOfOJgEmfBY9o63CWtaHCn0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=XXPZhHWP; arc=none smtp.client-ip=209.85.214.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="XXPZhHWP" Received: by mail-pl1-f178.google.com with SMTP id d9443c01a7336-1faad409ca7so10102195ad.1; 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=vger.kernel.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=XXPZhHWP5QwN57uWYOpdmk6Gaux3myKKOf3nc9dvHIPRKclvO1+9Bv6SRs4YWb7fXo 99EJL8IXsa2NhD6TX9su8uf2+VXgFECijWlmsYBqEkQC3V4qk7sBRah3P9K0lQoY08qR FRMkgJn677sRYGlrGnajHFdUnES0aNh7IShdXt+EBd4d0AwJD5dWIzi8spLy7SHjZPPz lhSQpPjRJhRh2XdMNQ/ZOfUt4MQ0JdHtyreGG9HPT3aPYcg+sakQ0VUlPj3twOvSiPKb H+rhtadKLFnNIPb/r34KiYaHYtwn0ErFhTd8mAQ4vOZUnL43BJ9s79G3bQ6hKgtbK6dV OZnQ== 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=U70QX8udI8MELd5FeBLkaMmLRTUDQwJ+HGSVw2w3rOVeL4gxykvXAG636bi6XK/ipy Uz5iyzCRcllVrDNRVtBQEXfhBLeauvOnyl5SQKf6LCXK+z84MH6KH8yz6tiL5zZPuQ+W vUKO1HiLe6ODqTrsYY2cLpdUmFryN+iTOKqHa+QU/h4QNA+MU5GVlsg+aRdBO4WVl/XR GduIoGC0Cnn3owemaG36ZRFLDu/Lbcb8r1KWertNPtbdR2TDQcb2IF2DH5LZukJqNR4e 0l7zdJuj/p61WztsnkYijsXdrTz6x9tldcTiSpBljfut1FTPdY9VqL/uOCihrJujhWS0 7k4w== X-Forwarded-Encrypted: i=1; AJvYcCV1GP4aohZ1kgG3orBrbhNMaf6kI3Cx4ta4OQT20zzEXOk40ls63sluWp1QE0mm4VHCbnWuuByiiHmsSxSW7kTP/GwzpaLPmWzFLluOYxsW6XgtnGsAFf+cGPsSP7G79VE5eB5x0+AI4mQJHD51cfgWteCOw1n2m4NzN2ruqdC23WcqHok4hMvYdihEMFnKSYMo7lP2CHrWQ8bU5XuquYi6mGNMZSDTXKgI8vC0+0Uz2RtCctXOhxM4eC/WnmsSg849BVYoVLhggiea6YOcnQnx9BsPGmuoArKSnQBMoIBFswo1BBWO+NNUNnmUNvqgdN5s/q+7+g== X-Gm-Message-State: AOJu0YyujdauGEAtCS0+BhclUwfeWFn0NvbDEs6EV7KY3SXwmWSpyL29 deWd3CozyCvRoYLBFZHiha0cGmHJNWl7wL9LtyMN2/yXZezq6+wObRpE8D1kBLg= 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> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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();