From patchwork Mon Aug 12 02:29:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13760005 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id C87C3C3DA4A for ; Mon, 12 Aug 2024 02:30:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5DF896B008C; Sun, 11 Aug 2024 22:30:40 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5676B6B009A; Sun, 11 Aug 2024 22:30:40 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 408EE6B009E; Sun, 11 Aug 2024 22:30:40 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 1CA766B008C for ; Sun, 11 Aug 2024 22:30:40 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id C6E6FA0739 for ; Mon, 12 Aug 2024 02:30:39 +0000 (UTC) X-FDA: 82442014998.17.ABD3999 Received: from mail-pg1-f177.google.com (mail-pg1-f177.google.com [209.85.215.177]) by imf06.hostedemail.com (Postfix) with ESMTP id DB0C8180003 for ; Mon, 12 Aug 2024 02:30:37 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="ZA6/VzQk"; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf06.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.215.177 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723429827; a=rsa-sha256; cv=none; b=Lt96Ci7eGehHeufSLOGHMVfJTO1Vy4N4lmyLrwwRKz7Qp61M/Syk5jkzoCaSe/xOaDZZ5w Sp455hNWo0k0PXPpoNe8rbzJmVhQt/evstWKm1THPCopjTt2qkFCQQvyXbEixt7ZO82Ri0 LSKV1kNnNUy142vWrknXzrCqwo07bTE= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="ZA6/VzQk"; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf06.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.215.177 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1723429827; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=khhjXbPZMt7GBHDJwocHjgjVouiC9Zx1GPF6Kpp8feU=; b=Z6NtKY3vCGF03/RFaJ6QXfsqd2IcYfgQgKCJ/m0pT/lCvDSOCfkqo6XPf9RB1c0NyZ48CP S3DovmoByZyZZJynURS/Pkpfm4vunbvLhGCIpSKy/cUVJCMSBI95DQS67wDLNGGCCZMxV+ RGOiYNq8mhx/m7CevUjj/8BYwdoWbHg= Received: by mail-pg1-f177.google.com with SMTP id 41be03b00d2f7-70b2421471aso2305311a12.0 for ; Sun, 11 Aug 2024 19:30:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723429836; x=1724034636; darn=kvack.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=khhjXbPZMt7GBHDJwocHjgjVouiC9Zx1GPF6Kpp8feU=; b=ZA6/VzQkXhj5WgMdM8vYnujGAIj59aqsZWf3KtXVZ5/22IKdGjFlBWPfg5b2AdFlim W7t2NXXA4+WgwDk3keQmh4IejKmfKfvmVUUN5S6xnaqgpftjCslyXScp+pWQwIa+V3RM aZQazBGwlPSftmI2Dw33D3igI7qeDuIaB81Qdm0KZy7VHG7j1OWKP/pKDJK3v3/JoWu8 YnZQP73+YHPh2dpC18i5yBabKo66FRCnUnTXcvmOMsLy3H7WClpLoYQj3OzjYk0XyGT7 H/dBwZq6W13p3UaIserq1MG7UBjKDhN9zcoh5iMJfOcJ4fB0ubVLNs9fx5pa+WZVZYEj 03JA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723429836; x=1724034636; 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=khhjXbPZMt7GBHDJwocHjgjVouiC9Zx1GPF6Kpp8feU=; b=Qyfmcaw6qlGrwYBERxdBTtjuNGD4KdJULNNJZj6YpURQS5TqUsZgwKPR0avscwnQ9C 3aCbZ9b/eVyLWKxm2Y5uN9JB1ZR8k1XT/Qv2Rbb3RMoZBiMMtUDCdtqwCJyEMk0y65wS T0MjvE2epx36iuL5y4st0ubtU9vCLxYZSDWgNlBHYQpS1cU1kZhnkKOaKGsq0xncaBa0 m2PFojCmZlUure/EN+EkcMVD6t+Z2AzZl41B2a2b2JnyUm5JE432IBKsQHj3LaHNpfdv ZWaMps5lkNZiTOqkT1zp+OsvBE7gm3YFPNkN+pGSrU4Y8EnirvVOigZ0pF/br5+svA6P UhBg== X-Forwarded-Encrypted: i=1; AJvYcCWlPa55DqhtR+X5OtXHTDPnVoMFAAczR1i8TKQ77SdYQYWBSiMainuGCtxJX0eD2sZSdrEorOwKKUTbP2ufEEtYtTM= X-Gm-Message-State: AOJu0YyIsrCc7Dkt6nW83O1GUgpRVbrT86rfwzzTUeP5c8cFLE1LGcnZ gtadv2Thg/oSc/Vp41HF8hRzneDzQ6oCRJegPbmzPiMDgCuJuaM4 X-Google-Smtp-Source: AGHT+IHZ0gPVVBMqlR8AM+LuQyCNFAmVaej1h0WqXQaGCWBBgBWQLJgV4NwTDuyMDp98EgrIiW/RCQ== X-Received: by 2002:a17:902:ce83:b0:1fb:3474:9500 with SMTP id d9443c01a7336-200ae56b418mr67022635ad.27.1723429836312; Sun, 11 Aug 2024 19:30:36 -0700 (PDT) Received: from localhost.localdomain ([39.144.39.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-200bb9fed69sm27884765ad.188.2024.08.11.19.30.04 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 11 Aug 2024 19:30:35 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, catalin.marinas@arm.com, penguin-kernel@i-love.sakura.ne.jp, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, audit@vger.kernel.org, linux-security-module@vger.kernel.org, selinux@vger.kernel.org, bpf@vger.kernel.org, netdev@vger.kernel.org, dri-devel@lists.freedesktop.org, Yafang Shao , Alejandro Colomar , Alexander Viro , Christian Brauner , Jan Kara , Kees Cook , Matus Jokay , "Serge E. Hallyn" Subject: [PATCH v6 1/9] Get rid of __get_task_comm() Date: Mon, 12 Aug 2024 10:29:25 +0800 Message-Id: <20240812022933.69850-2-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240812022933.69850-1-laoar.shao@gmail.com> References: <20240812022933.69850-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Queue-Id: DB0C8180003 X-Rspamd-Server: rspam01 X-Stat-Signature: yrug9te6dh4nhuok67a1droinjsiu1g4 X-HE-Tag: 1723429837-381223 X-HE-Meta: U2FsdGVkX193RdqFsWbTj4EmT5I5dRjJz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/StCuJxph2p+UkhgsmgKP1CEyxW/183RtWUUoX70ph1JysdJJuB8hFf4bz29rTJlUpYMYdxp+EZOQqNkTZlTZpkx4eoMGOwyk2ZoGY0xmKwIs8cbSGQzBswA+lewf6UkBfSx2Vm4/idsX0PxnGZiiM+TMyliF10baE882wd5x1a/WTwtmToSoeMrq5WuqX1feDG3z6xTaUAIiTT7NLFMEy7ceYwikatWoVq8vnRAy0keMhDFTuXWxvP6yQInoTwjqN1qKnpFd7f8+Y1uZB4TkQO3h3wCG+g49qWRL9eaOd1HI0T157AzcTXmHnmB0x17wqT4Nj7tqMO9OpdZT6seVFaVTM1kE5cOtUjNIlYUJNEsNJP3P59MhG6dQT+a7IXyerzdShwGHDgFMYF5276M7yKMZk0ALp023IvT1NoDdUdJ6yGJRZ5PRqjESB2CrpcS9FWNtNQTF7QYpqvOvNySBq3cADNYDhQJhM5WXz8JEO97z8rQWFiXStNfeK3+ezX8y4EK0RQi9980yI84VOFI4l4QQKrbzENeJoGyVmIle956cMsopvMyxLgQJbQb6OumQZOHOarXqbCWIaMF+PHb60uLiUFfU1YHO2p+poUzbpBK3N9mQtC0Z2YURNWF/9WbRn7M9VPC+I0j/5MPsC3cYLxGppdRb6PysrFwVOfz8I8a3ouNM6Tg2S1m2wsuggFVk5Tf6zTuUQ7tjzCVG1OxmAzY/x0u9cj X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: We want to eliminate the use of __get_task_comm() for the following reasons: - The task_lock() is unnecessary Quoted from Linus [0]: : Since user space can randomly change their names anyway, using locking : was always wrong for readers (for writers it probably does make sense : to have some lock - although practically speaking nobody cares there : either, but at least for a writer some kind of race could have : long-term mixed results - The BUILD_BUG_ON() doesn't add any value The only requirement is to ensure that the destination buffer is a valid array. - Zeroing is not necessary in current use cases To avoid confusion, we should remove it. Moreover, not zeroing could potentially make it easier to uncover bugs. If the caller needs a zero-padded task name, it should be explicitly handled at the call site. Suggested-by: Linus Torvalds Link: https://lore.kernel.org/all/CAHk-=wivfrF0_zvf+oj6==Sh=-npJooP8chLPEfaFV0oNYTTBA@mail.gmail.com [0] Link: https://lore.kernel.org/all/CAHk-=whWtUC-AjmGJveAETKOMeMFSTwKwu99v7+b6AyHMmaDFA@mail.gmail.com/ Suggested-by: Alejandro Colomar Link: https://lore.kernel.org/all/2jxak5v6dfxlpbxhpm3ey7oup4g2lnr3ueurfbosf5wdo65dk4@srb3hsk72zwq Signed-off-by: Yafang Shao Cc: Alexander Viro Cc: Christian Brauner Cc: Jan Kara Cc: Eric Biederman Cc: Kees Cook Cc: Alexei Starovoitov Cc: Matus Jokay Cc: Alejandro Colomar Cc: "Serge E. Hallyn" --- fs/exec.c | 10 ---------- fs/proc/array.c | 2 +- include/linux/sched.h | 31 +++++++++++++++++++++++++------ kernel/kthread.c | 2 +- 4 files changed, 27 insertions(+), 18 deletions(-) diff --git a/fs/exec.c b/fs/exec.c index a47d0e4c54f6..2e468ddd203a 100644 --- a/fs/exec.c +++ b/fs/exec.c @@ -1264,16 +1264,6 @@ static int unshare_sighand(struct task_struct *me) return 0; } -char *__get_task_comm(char *buf, size_t buf_size, struct task_struct *tsk) -{ - task_lock(tsk); - /* Always NUL terminated and zero-padded */ - strscpy_pad(buf, tsk->comm, buf_size); - task_unlock(tsk); - return buf; -} -EXPORT_SYMBOL_GPL(__get_task_comm); - /* * These functions flushes out all traces of the currently running executable * so that a new one can be started diff --git a/fs/proc/array.c b/fs/proc/array.c index 34a47fb0c57f..55ed3510d2bb 100644 --- a/fs/proc/array.c +++ b/fs/proc/array.c @@ -109,7 +109,7 @@ void proc_task_name(struct seq_file *m, struct task_struct *p, bool escape) else if (p->flags & PF_KTHREAD) get_kthread_comm(tcomm, sizeof(tcomm), p); else - __get_task_comm(tcomm, sizeof(tcomm), p); + get_task_comm(tcomm, p); if (escape) seq_escape_str(m, tcomm, ESCAPE_SPACE | ESCAPE_SPECIAL, "\n\\"); diff --git a/include/linux/sched.h b/include/linux/sched.h index 33dd8d9d2b85..e0e26edbda61 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -1096,9 +1096,11 @@ struct task_struct { /* * executable name, excluding path. * - * - normally initialized setup_new_exec() - * - access it with [gs]et_task_comm() - * - lock it with task_lock() + * - normally initialized begin_new_exec() + * - set it with set_task_comm() + * - strscpy_pad() to ensure it is always NUL-terminated + * - task_lock() to ensure the operation is atomic and the name is + * fully updated. */ char comm[TASK_COMM_LEN]; @@ -1912,10 +1914,27 @@ static inline void set_task_comm(struct task_struct *tsk, const char *from) __set_task_comm(tsk, from, false); } -extern char *__get_task_comm(char *to, size_t len, struct task_struct *tsk); +/* + * - Why not use task_lock()? + * User space can randomly change their names anyway, so locking for readers + * doesn't make sense. For writers, locking is probably necessary, as a race + * condition could lead to long-term mixed results. + * The strscpy_pad() in __set_task_comm() can ensure that the task comm is + * always NUL-terminated. Therefore the race condition between reader and + * writer is not an issue. + * + * - Why not use strscpy_pad()? + * While strscpy_pad() prevents writing garbage past the NUL terminator, which + * is useful when using the task name as a key in a hash map, most use cases + * don't require this. Zero-padding might confuse users if it’s unnecessary, + * and not zeroing might even make it easier to expose bugs. If you need a + * zero-padded task name, please handle that explicitly at the call site. + * + * - ARRAY_SIZE() can help ensure that @buf is indeed an array. + */ #define get_task_comm(buf, tsk) ({ \ - BUILD_BUG_ON(sizeof(buf) != TASK_COMM_LEN); \ - __get_task_comm(buf, sizeof(buf), tsk); \ + strscpy(buf, (tsk)->comm, ARRAY_SIZE(buf)); \ + buf; \ }) #ifdef CONFIG_SMP diff --git a/kernel/kthread.c b/kernel/kthread.c index f7be976ff88a..7d001d033cf9 100644 --- a/kernel/kthread.c +++ b/kernel/kthread.c @@ -101,7 +101,7 @@ void get_kthread_comm(char *buf, size_t buf_size, struct task_struct *tsk) struct kthread *kthread = to_kthread(tsk); if (!kthread || !kthread->full_name) { - __get_task_comm(buf, buf_size, tsk); + strscpy(buf, tsk->comm, buf_size); return; } From patchwork Mon Aug 12 02:29:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13760006 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 273F8C52D7C for ; Mon, 12 Aug 2024 02:30:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AE9126B008A; Sun, 11 Aug 2024 22:30:48 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A70A96B009E; Sun, 11 Aug 2024 22:30:48 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8EA386B009F; Sun, 11 Aug 2024 22:30:48 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 657806B008A for ; Sun, 11 Aug 2024 22:30:48 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id E9BDEA7851 for ; Mon, 12 Aug 2024 02:30:47 +0000 (UTC) X-FDA: 82442015334.27.9C504C3 Received: from mail-pl1-f181.google.com (mail-pl1-f181.google.com [209.85.214.181]) by imf11.hostedemail.com (Postfix) with ESMTP id 2A2214000D for ; Mon, 12 Aug 2024 02:30:45 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=l9J+LFrZ; spf=pass (imf11.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.181 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1723429812; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=zoIFivrPmimQFCGo/41HPdx35H3kdQIF/qhwBf2evw8=; b=TbDInQNhjwMiibIyZrxUDvKdTudJZibau5ze3TJwQ9brLNkYz+EHDQmWWNmxlYdhgnvE8N s8BwMGsWo1T6NSZKFMYyIFpETx5UZD5+5EpTk70CSHh/rWk7XEY9QVkyaiDem9c7iqNMbb f+7AGY2EWmLf4Z2h3hUMIXpAtKVA6DQ= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=l9J+LFrZ; spf=pass (imf11.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.181 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723429812; a=rsa-sha256; cv=none; b=uVngYNLyUqC4Y/1Jbr7/nRVE70mTZj3u7VEHpcl4o5wow0sz9SkXnUYjUVte220vmN2MUg ht9n3ScULyK/TMOjZerrDBbYPCSOZprjrouP8I3KSheuw1XqZbanMrC2PSk9Jd2Gaekpm4 HzEJFygnlXfx+2uaJY1LhDCWF/gEc+o= Received: by mail-pl1-f181.google.com with SMTP id d9443c01a7336-1fc65329979so35794475ad.0 for ; Sun, 11 Aug 2024 19:30:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723429845; x=1724034645; darn=kvack.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=zoIFivrPmimQFCGo/41HPdx35H3kdQIF/qhwBf2evw8=; b=l9J+LFrZFoitmaU45R/u2khvseWwXl4HILgsKQt4XyYgv6KtA3V33Ft1VGbrmkWD5q FrsyJvrhHmENjdFLmCM9dUSevvv2MuRpwlslOgVAZePGCYw6Z0+b5jBbHR9CniNIWd+Q WbVYZtIlE1XJeh3lFedxPC+BW66v+lGz9nWzRHsKv426hjtGqX2clRIiTUcqRsN6TOOk EbQqsVp1YUHiI1hKiV6HzTY7w0QtGt2KBUY+4ewUbvS+YF2EEv6rqAdhpW69zh1w2chT hYimQyHffKJiGEZD7cDd65IJXKHdAIcS9tyAIYKEAQXqHcgC0x+cvrSlzlYrGdKhXyqH f7bQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723429845; x=1724034645; 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=zoIFivrPmimQFCGo/41HPdx35H3kdQIF/qhwBf2evw8=; b=tkAfy+XTSbI+O0qzlZmcefj5Hzt4JWcVyQAmg+OGahg+pEsWN4JIB+czmmTSq+E8cn A6zBryZXo+HKLcgT5lJX0NcmrPG4aw7plZ4M+T/CiPa5NUvQFO1QtoLEeUEbN9SU9mgu 3F5avlP5tFC44vrkPlmBPQ0xLGiFWHc9roRBgN6tBiXQVrv9j23Gvc3+a+vinCKxZlXP UIW/Os9hpjzt1jqmQEzQJ6HuR43lVX4EArQbxxnr1lDWDdWBpUCPlzO1zHnnAhoLVOGE dUKO8OC7pGFliTxV0ZlyQi/w4isQshoNieajPXYr0PnEcG9q6WjjNM7+M34XNi0sjFBV dQUQ== X-Forwarded-Encrypted: i=1; AJvYcCXl4/w9xN/3XoWGxQK9dfjeSpBaZ7WPbc+Xlizrt4FLBCA/yIHViePj0YJ2o2RPMtS2fXN2B48ikZRJNbNZqQ33JLE= X-Gm-Message-State: AOJu0Yyn1iDd4bRR/l3SNtpDS4Isbec44ehKtDRpFC4RfkNhOh0FG9BP Q+0XZOsLILlHnUA1CmBbFUuxCKMcWssgrImT9FDtmYgCWb5ZZO6G X-Google-Smtp-Source: AGHT+IHsiTPfsy98RJjC8wpBNmaXvnlNH5fflWZLUy6tj95HlXDW+LhDZRUML9DQTQa1nKZJvXQLvQ== X-Received: by 2002:a17:902:db05:b0:1fd:a0e9:910 with SMTP id d9443c01a7336-200ae5e811amr99536005ad.62.1723429844807; Sun, 11 Aug 2024 19:30:44 -0700 (PDT) Received: from localhost.localdomain ([39.144.39.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-200bb9fed69sm27884765ad.188.2024.08.11.19.30.36 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 11 Aug 2024 19:30:44 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, catalin.marinas@arm.com, penguin-kernel@i-love.sakura.ne.jp, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, audit@vger.kernel.org, linux-security-module@vger.kernel.org, selinux@vger.kernel.org, bpf@vger.kernel.org, netdev@vger.kernel.org, dri-devel@lists.freedesktop.org, Yafang Shao , Paul Moore , Eric Paris Subject: [PATCH v6 2/9] auditsc: Replace memcpy() with strscpy() Date: Mon, 12 Aug 2024 10:29:26 +0800 Message-Id: <20240812022933.69850-3-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240812022933.69850-1-laoar.shao@gmail.com> References: <20240812022933.69850-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: 2A2214000D X-Stat-Signature: adzqyqgmfkj78im6b8f3qw9thcom5qde X-HE-Tag: 1723429845-713314 X-HE-Meta: U2FsdGVkX19C9IBfGcf+thDRcreoAkOJ1HxGwolywlG9QfK8FmmkT4qM8+UM5+tRLIejvw5eUQz5yd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Jc/2ATFWVvUj0kwniZCAjirntFpCBjux0lwouR4ULK98nZJQYNEwbU+kRavIs9ISUBKNKqICk7zKJhyCoz6tPGlqLDVdY2k8agtSJiTxotSmG+u7dUIWoxysZtAjfyBg16uAtREL5+bZtXZ5jzjot9Q9S8KvfKxkMOoIbU2Xt+gqHLSD2r+rbByAIUIrTkOToIBbGUY1hhz+KiB1tQfSis3vcThN8SlnsKKrepBPTDXSn+faxJKBHjUfxMBR8TcWV8vBU1Q1jCiBz9Hs3Kt3MOas75CuobEY3kBY7kqFNlxJ9NB3Cgnrn1S9NWQAmfBFSw/tIOVKxSlYMIjqcX9ZpIMieRNREIJq3a6E3/4+o2ayfHXML2KsdzYrixG1862y7zysE2iNVUiR0AfO06Yg9uLqmJu4xbgoZ77MQE5JUYoptY+xYmsSoX26WHLgGogz+FDVeLjLI3XTUg= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Using strscpy() to read the task comm ensures that the name is always NUL-terminated, regardless of the source string. This approach also facilitates future extensions to the task comm. Signed-off-by: Yafang Shao Acked-by: Paul Moore Cc: Eric Paris --- kernel/auditsc.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/kernel/auditsc.c b/kernel/auditsc.c index 6f0d6fb6523f..7cbcf3327409 100644 --- a/kernel/auditsc.c +++ b/kernel/auditsc.c @@ -2730,7 +2730,7 @@ void __audit_ptrace(struct task_struct *t) context->target_uid = task_uid(t); context->target_sessionid = audit_get_sessionid(t); security_task_getsecid_obj(t, &context->target_sid); - memcpy(context->target_comm, t->comm, TASK_COMM_LEN); + strscpy(context->target_comm, t->comm, TASK_COMM_LEN); } /** @@ -2757,7 +2757,7 @@ int audit_signal_info_syscall(struct task_struct *t) ctx->target_uid = t_uid; ctx->target_sessionid = audit_get_sessionid(t); security_task_getsecid_obj(t, &ctx->target_sid); - memcpy(ctx->target_comm, t->comm, TASK_COMM_LEN); + strscpy(ctx->target_comm, t->comm, TASK_COMM_LEN); return 0; } @@ -2778,7 +2778,7 @@ int audit_signal_info_syscall(struct task_struct *t) axp->target_uid[axp->pid_count] = t_uid; axp->target_sessionid[axp->pid_count] = audit_get_sessionid(t); security_task_getsecid_obj(t, &axp->target_sid[axp->pid_count]); - memcpy(axp->target_comm[axp->pid_count], t->comm, TASK_COMM_LEN); + strscpy(axp->target_comm[axp->pid_count], t->comm, TASK_COMM_LEN); axp->pid_count++; return 0; From patchwork Mon Aug 12 02:29:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13760007 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6069BC52D7C for ; Mon, 12 Aug 2024 02:30:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EB8CE6B009F; Sun, 11 Aug 2024 22:30:55 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E41C76B00A0; Sun, 11 Aug 2024 22:30:55 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CE2D16B00A1; Sun, 11 Aug 2024 22:30:55 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id AED0A6B009F for ; Sun, 11 Aug 2024 22:30:55 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 6C4C44071F for ; Mon, 12 Aug 2024 02:30:55 +0000 (UTC) X-FDA: 82442015670.10.F26D4C5 Received: from mail-pg1-f172.google.com (mail-pg1-f172.google.com [209.85.215.172]) by imf09.hostedemail.com (Postfix) with ESMTP id 8A8E6140029 for ; Mon, 12 Aug 2024 02:30:53 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=cXJSeflR; spf=pass (imf09.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.215.172 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723429818; a=rsa-sha256; cv=none; b=jap9Z/C7n0jRCU8mATF4no6aqP/wBubke1guFbkNWg0HhUTO7EBhkyDyYscEwkUjypcg3X KzLi3BS1aY7ZUBK5yCaHpXcb8d7T/6DJ+qPEec7Tr9qtLBDKBtFTSIzQY67BPXrjVjcFFI 8vKqPM3MuDlYJC/OfwpqI2xJo/qJKCU= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=cXJSeflR; spf=pass (imf09.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.215.172 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1723429818; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=9Po06JAJWHpcwq2qZykUbq7VkC01v3704cEIBIqD0N8=; b=X3hklOmnuYNhb3CRpbUKNwlVAgkCxgIlwS3g7ZOU0OJN/1UKwB2Kyl9jm1TPbbwQMJI70B S0x+Xv1Pxi05gKuSEWifjsllEa+xO5Zl86UF5iGrt5b4vjfaH1TV7B0kPB9SoZq9nAlOIm SRQo4V2fuZpMu/Y5C1ib8txpJZY5APo= Received: by mail-pg1-f172.google.com with SMTP id 41be03b00d2f7-7a1843b4cdbso2697125a12.2 for ; Sun, 11 Aug 2024 19:30:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723429852; x=1724034652; darn=kvack.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=9Po06JAJWHpcwq2qZykUbq7VkC01v3704cEIBIqD0N8=; b=cXJSeflRRaA3NS8mwyTxRre8au3rea8Co2cAxUzPQchuoTvc7tD1O7ghP7Y8408aPI iphdO2HzDwb/7RCqDP7f50Tj4wVtV4FlDkSIiJlpn5PQsZAABKyw79ohJGzJJ3D4kDsH 7SGJgulaHJ3p0RMmzfKcaSOyCvE9U0Z+OjgvUaDp8Skv+VBdncrLA+NVogAsp7d2bRNm kDfS79eYO2s3oTcYJowdPNOL26BUsAh1RplMHC0JRIAMVRnNv4lfsvAMpcxlZ1mmnuDt gN11p8bGrrq5awCJze/piO4xHdIC7jhHBTst9QbF8cy/t2VAV53jlgjAC8RrP87IrcbP sNsw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723429852; x=1724034652; 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=9Po06JAJWHpcwq2qZykUbq7VkC01v3704cEIBIqD0N8=; b=XSgKmD70pu4K2QPbrZDALC2lnMrmh++KMVXBTImrHQW6qNIo/c6LQ+DGcT2CX5qbjC HE272bYqLg+NmemarBuxSrImXsGh3wU1Z6H4a7salClJzwWFNyqQnwwv5ZzM/EiyF2Vi 6w8PDmanv/Ug1d+VdeVVXuHXTyG9R6HlhAVXPNI5Z3GvKT+przo/baOmeQ/VwI5WRc1x 2DO2pd25KzMBWXMbEipLIx+IKCrUmOU5E4o2jTP6wJmPYJ0EjiaVa63YiGJ+yb02H9tT vvNqMnHQiKDjZTqCOqkrRd8Yglxl5pYl8IWNn4PqlmPjcsu7e5w7O0cLYCQwHFp9UkcV 1hVQ== X-Forwarded-Encrypted: i=1; AJvYcCVXrV6ANbsCDBw0DWneosdz6LQjqF6Ik6ObyXWIV03tgREpGfs2XhfxnYpEqpw8xHIoSEl8iFqtujIa80BO364OgEc= X-Gm-Message-State: AOJu0YzsSeW2gRMMjInGOADZIDgjiU7uNA0FrqqnwfUFTtN7aD4e1ktt 6eho12q98kIRGI4NerZmYrzBJ7P3ZvQ5pOv9Rn+HHeXj4zIuoUsR X-Google-Smtp-Source: AGHT+IE1xZMlHs3Cuw2myWpVVx1UE/beU7sYLAE8S60uxm8E0L6tTVwuNzgTetxBMMEWFVehNVy1XQ== X-Received: by 2002:a17:902:d2c8:b0:1fd:8eaf:eaa0 with SMTP id d9443c01a7336-200ae550a83mr99817585ad.38.1723429852169; Sun, 11 Aug 2024 19:30:52 -0700 (PDT) Received: from localhost.localdomain ([39.144.39.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-200bb9fed69sm27884765ad.188.2024.08.11.19.30.45 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 11 Aug 2024 19:30:51 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, catalin.marinas@arm.com, penguin-kernel@i-love.sakura.ne.jp, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, audit@vger.kernel.org, linux-security-module@vger.kernel.org, selinux@vger.kernel.org, bpf@vger.kernel.org, netdev@vger.kernel.org, dri-devel@lists.freedesktop.org, Yafang Shao , Paul Moore , James Morris , "Serge E. Hallyn" , Stephen Smalley , Ondrej Mosnacek Subject: [PATCH v6 3/9] security: Replace memcpy() with get_task_comm() Date: Mon, 12 Aug 2024 10:29:27 +0800 Message-Id: <20240812022933.69850-4-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240812022933.69850-1-laoar.shao@gmail.com> References: <20240812022933.69850-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Stat-Signature: zt9zzodqbx1ewjjcyofd8yg6iuws83xd X-Rspamd-Queue-Id: 8A8E6140029 X-Rspam-User: X-Rspamd-Server: rspam10 X-HE-Tag: 1723429853-627405 X-HE-Meta: U2FsdGVkX19J9k1+dsPOkj+eUykHnC8G8N0QyGRIH7J5mwLXQb1+sqpnwv9A12Oww/MWIB+r9KPtLaVuK2PJzBsxpAD4ODn+HfNNYJdK7pIM/5RGlAT9MPAsRRy+AuGOrlFxiA/F8ujBBsz3xg6UAefB/kwFEmaiuTn5LdBt1u7JtGHP5vt5noLlB2fz/Arz4DGk39rDhqsnsm4+Y0Zbn5slqEcSqV4lKLvKqvB3KW0SqW2htMSzKbe8hDf8awXg9uJFSoMkHQ9eUqrRExD+sy7Q4xSToda8RS/TLqa2gljF/r/Be7iQPvhc2BUODW43EBhz5cRHmUahNr0QU5gMsKiZE//u9+5oSBtCnuHwIkAYlLbBdCb+IuaFmvSCp/TpdASHRcS0J9/989TU0TegrOISTF0hyDEFp6WoItHVkJNbFqKjK0Ri7+n0uUxHUl8Q3/CIObpcsn13uhjP5QuVCZW3uyveCk2uNPfk1lXuvhnvMmDHu+mTGSUkaw83ao8vgSR+fCSwyaq7jm350Uk2mxOsGYQiLw1MIGpPzq2QLVnEJ24l4HLP0hPsCjmv0X1kCWTq7Io19dlMFY/H3DNx05pbDmvOUF3mdF2ebicvHZyDptowH5/tG2eoYv76IEMeSZ01EcwOe6Ij1iOyemC+c58PZIoOHUFRHFBRy0n09zoDHj4vxZEjALLkDypO/+reAJEFDfXuboJNDlTa+bgyWIIshSR5SOKHYrbSvl4FlUWPQKteT/+pZ8FD6hnp4qgRoigGQDjtDgxF/X7DS+8aFVupcXnAZ0dl/DDL+KcYnDNvqJnjek4kxSJHAaW/4sDpaINiUg4+5t3SiIChsg+is8MI5dcMqYZh7uVCM1yuqslq6grlm7iZOrqjY97p2JYQUL/ydmv/ajEUHJW/8569q32yVkw09hCxZPdkA0XKp37XcAsauhNzaSS/fvHvYQhPQNOOkU7Bx1zlehYgIVa fG7V9ocZ QffdapLq1e5/P3jHBT5zHiTpOTDuZVUH+QFE9dc4lB5sO8dH21gwD+PBckE6b/nkxoeTNnc5O7iJX2j48lMRr/Uh7BSIoMCcncrDui0oUr5aZGucB6S6XJ2/0IpcyfHcfk379Kv0WcukRuiwIT0JH+UHCHnS+nJHbdX5kpqhyVHUuRkxIObfUUv7dJT8Lzj6rZ9lQ+KZomnwWqpJzWXn26+KtDgdzD6Ayf6m2SBneOAWLPrN7cSLQpW5AMsPu3MrChukgYGfoZkUU1eLZH/wgReEz5BpHsWUw8SPSqjEXxBsgluaKRUn3iRmGLeqHo+oCOqoOCpJthTbH7eRXDXd1fkhK3Gh4DuRmgq9qN8HwgMezyqqgknH187/RUnbCgSilV/iZlIJlnrbmCrvJHKH2FCtLiTiNJ5cp7qL6D3jxRebar47qHebtINzzkl8jwWMjyd5syi56IEJh6n2U8vIboETYGTMY7SHn57kBJkgG38o075J9aF53T6pGDfwtFXkpsYz+hU4Af39AgunW7MUi9uwC79TuVW6McEONPf2M8+ME/0sCsfcR9x54N9p7uiPSXnXC7IUw3MBjpIvJW4MVDJk/zkeKw6IIBPDla3Hd09g3+d36KB64+8c3YGtGdbI6VgVIuzGoDjo4JbkBToIJ5VLz9KpVpev1e2IFasNpINkqgxEYoKzrukIFY3QWEjHWNQaOGkaK2L2zk/PW2RSUhkkySfPXNSlO2yRL/5MyxzsrL0ztfWm7mUX8ohwD3HWm6osp X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Quoted from Linus [0]: selinux never wanted a lock, and never wanted any kind of *consistent* result, it just wanted a *stable* result. Using get_task_comm() to read the task comm ensures that the name is always NUL-terminated, regardless of the source string. This approach also facilitates future extensions to the task comm. Signed-off-by: Yafang Shao LINK: https://lore.kernel.org/all/CAHk-=wivfrF0_zvf+oj6==Sh=-npJooP8chLPEfaFV0oNYTTBA@mail.gmail.com/ [0] Acked-by: Paul Moore Cc: James Morris Cc: "Serge E. Hallyn" Cc: Stephen Smalley Cc: Ondrej Mosnacek --- security/lsm_audit.c | 4 ++-- security/selinux/selinuxfs.c | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/security/lsm_audit.c b/security/lsm_audit.c index 849e832719e2..9a8352972086 100644 --- a/security/lsm_audit.c +++ b/security/lsm_audit.c @@ -207,7 +207,7 @@ static void dump_common_audit_data(struct audit_buffer *ab, BUILD_BUG_ON(sizeof(a->u) > sizeof(void *)*2); audit_log_format(ab, " pid=%d comm=", task_tgid_nr(current)); - audit_log_untrustedstring(ab, memcpy(comm, current->comm, sizeof(comm))); + audit_log_untrustedstring(ab, get_task_comm(comm, current)); switch (a->type) { case LSM_AUDIT_DATA_NONE: @@ -302,7 +302,7 @@ static void dump_common_audit_data(struct audit_buffer *ab, char comm[sizeof(tsk->comm)]; audit_log_format(ab, " opid=%d ocomm=", pid); audit_log_untrustedstring(ab, - memcpy(comm, tsk->comm, sizeof(comm))); + get_task_comm(comm, tsk)); } } break; diff --git a/security/selinux/selinuxfs.c b/security/selinux/selinuxfs.c index e172f182b65c..57e014ff3076 100644 --- a/security/selinux/selinuxfs.c +++ b/security/selinux/selinuxfs.c @@ -708,7 +708,7 @@ static ssize_t sel_write_checkreqprot(struct file *file, const char __user *buf, if (new_value) { char comm[sizeof(current->comm)]; - memcpy(comm, current->comm, sizeof(comm)); + strscpy(comm, current->comm, sizeof(comm)); pr_err("SELinux: %s (%d) set checkreqprot to 1. This is no longer supported.\n", comm, current->pid); } From patchwork Mon Aug 12 02:29:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13760008 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id A65FEC3DA4A for ; Mon, 12 Aug 2024 02:31:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 36F8F6B00A1; Sun, 11 Aug 2024 22:31:02 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2F73B6B00A2; Sun, 11 Aug 2024 22:31:02 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1BF336B00A3; Sun, 11 Aug 2024 22:31:02 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id EFD5B6B00A1 for ; Sun, 11 Aug 2024 22:31:01 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id B1C5080635 for ; Mon, 12 Aug 2024 02:31:01 +0000 (UTC) X-FDA: 82442015922.03.1EA20C7 Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182]) by imf05.hostedemail.com (Postfix) with ESMTP id D739F100004 for ; Mon, 12 Aug 2024 02:30:59 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=Ds2BGLJb; spf=pass (imf05.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.182 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1723429825; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=QQdkC9XISn7BWPlTE5/iE7aUj/L2jeLonKoaBsbNpS0=; b=QxZg9dPaWsziTaIkrilUlseK0Vur9RcZhP7emHPG49t6jsWvKQNzXW8VUFaqF13tZnjqkF hZXVfajRh69rCMdLQ+gFRCgJkmt3jDidhUXtVWIgzs45HhIp0qQMpAh8Hejcu2BFCPd/nO 1zo1D5rITVoBACVulAmoJjPTuBzuHuw= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=Ds2BGLJb; spf=pass (imf05.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.182 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723429825; a=rsa-sha256; cv=none; b=RUbuXDYnR3/nCUXYluv1hCcWjbZZbSY3nK9qK7q5VM2zLy3C0hHxc65YBNw91HEcuqsPbT vZ+tCVf9Am2nQOlH0KlTy4NbDJP8tXzxdzlBGJ5znX+FEujwT3vCsdw3p6+JpUd7ChZqrp 5zFcs8K3PM+Lw6lmsd8nXciFhja2lD0= Received: by mail-pl1-f182.google.com with SMTP id d9443c01a7336-1fc5296e214so37046555ad.0 for ; Sun, 11 Aug 2024 19:30:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723429858; x=1724034658; darn=kvack.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=Ds2BGLJb0IpS4+BJL2Nto+KoCDhAyST05SDG6iVZGZcflcQom9LgXxm6A89XM+W82v iYxdq2gSe4qkqKB+hNMPNfFSxtsYbpmYXrVr4BwZ8JvWSHZX+2I8/vNrqG8scO2/EIsv VynbcKuhEAf3peMMNjNHQ5sMA4JgzhfzYYrfJinUhAbvTdKZqXmiqEvwOqhZ4DNQLQPc WfQm9TMvW4lHEHGoPjWU6tJLjSlKCeTRHnAfVA7u0hChHeWCYaPg2xn/pnFIgbl7ygUI 8AwE3nJRJDi8TzSBua8gUj3GfWcg+K7zUiDWlDvyOpOQoG9zU8k+4bseU5NKxiJYYG1+ dc1A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723429858; x=1724034658; 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=QEFrIuUXtlWgHCNvQFF8vnLdaARfo28T536nb+VqCgC+SYj9xp8MsbkYviSbckd0fa 7a5Zs0Ee4txgZ+qi7pWIb358lVR2ARf7v9XpfwdUpuWHDwNvM+EvosrzOsRkFl7i9XpY tFqNDZkpUzEhq+U2yPyFKDWYpTEA2hlvah5hbzE6pZWaX4QvnCMAcZmiSYCkwHlyb5QY ZmozaHWSyJvHAi4E2RxvWENWsIBi8wNvI6L4yYhzY1KlK3CBA5+qYe6S20Z+WOHugF2F H/Q+J1f08XpzqtA1lPLqXwouSXPhzRUtG5xD+Q8D9ypZXvI9e3s1w137G2uO9SbI8Itx +8mQ== X-Forwarded-Encrypted: i=1; AJvYcCXgQNUzMY+aEMqSIanASo6Oo31MauEUHQpRIOgmoOrBsFhQp/0R8ZGjQVyQLV3otDSOdrz7/DSdclpnpu9qmMd+QkA= X-Gm-Message-State: AOJu0YwD94C6lXeHwT3xLQB/2SCLIX0AiRQpwkEe2asDgFDKRXlW9x+k ThIwHVJRLpORDTNe3dyVbqwu2N8UBgoikzfbX7IvURHROwLU2z0+1r44KMbwkJuE9Q== X-Google-Smtp-Source: AGHT+IEa9lv8MoV6zk/9zF8wCzjKsRZm15PR0yRidIjcflgd8FgwO2+8+6fP26BlJQqb1WyA3rzQJQ== X-Received: by 2002:a17:902:f54b:b0:1fd:d7cd:ee53 with SMTP id d9443c01a7336-200ae540e2fmr93875475ad.28.1723429858561; Sun, 11 Aug 2024 19:30:58 -0700 (PDT) Received: from localhost.localdomain ([39.144.39.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-200bb9fed69sm27884765ad.188.2024.08.11.19.30.52 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 11 Aug 2024 19:30:58 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, catalin.marinas@arm.com, penguin-kernel@i-love.sakura.ne.jp, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, audit@vger.kernel.org, linux-security-module@vger.kernel.org, selinux@vger.kernel.org, bpf@vger.kernel.org, netdev@vger.kernel.org, dri-devel@lists.freedesktop.org, Yafang Shao , Quentin Monnet Subject: [PATCH v6 4/9] bpftool: Ensure task comm is always NUL-terminated Date: Mon, 12 Aug 2024 10:29:28 +0800 Message-Id: <20240812022933.69850-5-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240812022933.69850-1-laoar.shao@gmail.com> References: <20240812022933.69850-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: D739F100004 X-Stat-Signature: mw6zrfigpj1mp9f5fp7mztu8yok4y19t X-HE-Tag: 1723429859-986138 X-HE-Meta: U2FsdGVkX18v2rawAogJ0CSHbaITeA/niv/xQ8XQSuDepLxkKiRWWNzQPRDUVwfdklMr9bX1/xCF/A2EHfp7vwNBeunDQ2bUwSruaIp9/FwdzJWIYMQf4dtjpxcgpyAU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BULbYHEm5rj4JIBP9IMvm4s/imsj6NC/ghzxYka5Xmwg3F7pL5E04Hxmf6Zw3XhqXFWYJkxUKjzK2vSUfH5OXyr6IOJiE1Go0Vo26TdcGfVQZieqNh5LrTBBL5OOE0ag7k/0taINtNpHhrBrc4XuhRxLJ2ClRQGs1Ii+9LgFzU/sE8bDMLHCTVjogs9ZJVVJKZ412MRxXgf7HS0drirE/7R3QjRUYX1i63aah3ETzqpAWqMk5+eXEgvIjCwsJKiTlwul2FLoCrTQfq1X99C571RETDt1Gd45Lht+uAhE6vmKULP1RY8beRJypIbKiXBzbnbKMDbabQBb4uz9V7PMMa7/upscdhNdZth7WP8n0RfnAI4YUALCLdEIUcQGPA== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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 Mon Aug 12 02:29:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13760009 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 053A1C52D7F for ; Mon, 12 Aug 2024 02:31:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 850E06B00A3; Sun, 11 Aug 2024 22:31:09 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7DB706B00A4; Sun, 11 Aug 2024 22:31:09 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 653D96B00A5; Sun, 11 Aug 2024 22:31:09 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 44F926B00A3 for ; Sun, 11 Aug 2024 22:31:09 -0400 (EDT) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 079D4A0739 for ; Mon, 12 Aug 2024 02:31:09 +0000 (UTC) X-FDA: 82442016258.02.1DAE848 Received: from mail-pl1-f180.google.com (mail-pl1-f180.google.com [209.85.214.180]) by imf26.hostedemail.com (Postfix) with ESMTP id 32700140005 for ; Mon, 12 Aug 2024 02:31:06 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=cnARXHBB; spf=pass (imf26.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.180 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1723429813; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Dg8K6fIsZQLFiS7Xpg/1YZlm81yjN4oHJWyrWmInMGw=; b=2DUzkMhyHLf5+JrhC+6JLsx2vdnwT2+QMr4WrA5Sk5iJuQcgUfmhzrz73DSmJPwZF7XzSP VDlz2ruG1++66QJ+r6GTXWv7l29Jr+bg2cMaqHLMKgr9nxf87hi1LpS8ZAnMDolRBbOPgx nWQQvOsJFqN+rVcrlyZT/I7f+wle4a0= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=cnARXHBB; spf=pass (imf26.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.180 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723429813; a=rsa-sha256; cv=none; b=r7gfngwQYnsOxYjtZIN9UokOQB0ZxzaeRRpIfSgvhrmIU3loF5idtfodhqodcq/K5FPxMA mrL9XE53aNlaKMdZ0FcPz61fJytQg40uR/6Z40A7m4HQbiq7/soCvxW5F+uGKL05eTRDm2 g7oNcPaJ2dPs7NgpbZ/qN4HgphewSeM= Received: by mail-pl1-f180.google.com with SMTP id d9443c01a7336-1fc60c3ead4so24420115ad.0 for ; Sun, 11 Aug 2024 19:31:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723429866; x=1724034666; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Dg8K6fIsZQLFiS7Xpg/1YZlm81yjN4oHJWyrWmInMGw=; b=cnARXHBBe2yTSF0G5pH50C0c9gCmwGxjOLkGvmaKQm01lMr8YWrTArZGlSXMohwO3p vv7+rUuYs49AYsij+L3l+Jf4XnsETu7dSzEglPfRmNSb8QWoQgqPRxpemxCVjoUBB6E3 s3vLjdL/UDUdpqI0YSzRj7NlpUjBwC05e7bsF+8G+HeFSttHjWxmfyNOBgaZkU09cFxU b2dkWVhYzcTjlh7wTb4oxc6SfJdf7iyaWWbIAtFZ0sQZOMFYmB6sgCi8Jt9PT0fWza4M ZwLli1HzC2qoY25HqWnkwxj/PK5IOwJsotsPpkX7xb+QHwvDrv1r5s0bo2wMp3WXXqoG K53g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723429866; x=1724034666; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Dg8K6fIsZQLFiS7Xpg/1YZlm81yjN4oHJWyrWmInMGw=; b=AxH419R+8SZRmENUsIE1zRS8+C399QLG2h28l424GZVuIdOhN37Qs43keZbqLm1OKu z14APKJ4KXLf38W016nxKejHuDF+94VBml0RANQCPmMoAVrw4h4fv4CdOyFI9E0n/NZu pJ3SRB7wQleXEWqetr/knzFhYAiq57IGtgzPL7w4Y1uMOEhT+ZoY9yS/f0EKAKqFOhwI oZwRfADHzaDsmTlQjEVKpCoUHjncekgoi2T88+ax0U7EDW8I2rp/be5Z4UhkXnwqWYmC OzBMTxQi+J/stKP0eXifyl09xqOZr1tnGJ4nPVFIM12SOFtm+Kr5JwVjSHTJPSzJPvQ1 pJRg== X-Forwarded-Encrypted: i=1; AJvYcCUVPF+7UxrExkytqOK/gasMhfGkR3ctu126jeqWH3n0Yy3NLIQjwLIEh69zfomQjGml7MPV79CSo2plHCY1UIEPF18= X-Gm-Message-State: AOJu0Yw2X7u1T98ey8ohZrH0xfEjfOgUuCN3hCm2Q5Y1YY5VF4YPNxr0 AVoct9jElJ5dCryFYPXstDtricTKNkSYhQE2ihgDusiXNivIY2gF X-Google-Smtp-Source: AGHT+IGpTCkxya5k7tDP0eeWIg02IZrGkBb+Xs7WqcLI3e7hWnKKPF02puCEAAGLT4aj9e0Erpx/iw== X-Received: by 2002:a17:902:cccf:b0:1f7:1655:825c with SMTP id d9443c01a7336-200ae5a8934mr50792845ad.36.1723429865812; Sun, 11 Aug 2024 19:31:05 -0700 (PDT) Received: from localhost.localdomain ([39.144.39.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-200bb9fed69sm27884765ad.188.2024.08.11.19.30.59 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 11 Aug 2024 19:31:05 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, catalin.marinas@arm.com, penguin-kernel@i-love.sakura.ne.jp, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, audit@vger.kernel.org, linux-security-module@vger.kernel.org, selinux@vger.kernel.org, bpf@vger.kernel.org, netdev@vger.kernel.org, dri-devel@lists.freedesktop.org, Yafang Shao Subject: [PATCH v6 5/9] mm/util: Fix possible race condition in kstrdup() Date: Mon, 12 Aug 2024 10:29:29 +0800 Message-Id: <20240812022933.69850-6-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240812022933.69850-1-laoar.shao@gmail.com> References: <20240812022933.69850-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Stat-Signature: ryezu8md51bhci3c4x86jdp5hipz415o X-Rspam-User: X-Rspamd-Queue-Id: 32700140005 X-Rspamd-Server: rspam02 X-HE-Tag: 1723429866-646697 X-HE-Meta: U2FsdGVkX1/mbfdTnC5iIKUeDMHxnPWk88Dn96571ZrzzhAIe02A1Nlny0+6RSvA/zyx2WO2j6YosS/q8DsM6ozm9wP6DvtAsbdLbFH+k5GBl8iJ4FHLXWGIjnMcYtA1hbYL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iu0nbH9BkU7BN/cYAknOOyQSpU+N9y1nvZqiVqvxWpaEoGDfyGRJ9hwtDROrXkEWFCgfisF62Pfmn+Vst3eoWJ3Sj9AeYjJ1oD3RrABsupAi5UtgBYrygFg+8pVVz26NZOdKBPpTzCaieQC8WZcGVnLRlUyQbQHGNjBoCYPt5XQ09siEuNA+FySB1RvJd0HZcQ0oaiWHzTE9LFmulvQud/Bt2Zw4+2C9M0lMK/J6FQiF00rDe7juaGhFeL8sJoi6zy8TX1jMCHmm3eeYgGtvY2a0prOtVURssFSsqM+YKms2QKUUP8PPALGEi9xdrsM7DNbVV+JQ9ay/+t6PWViE9sSzHpiNZ2Lzk2juMCK1Zk7KVCQmzCD3nwtJL3dgp8856Hwwa3DSzBKWjcA== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: In kstrdup(), it is critical to ensure that the dest string is always NUL-terminated. However, potential race condidtion can occur between a writer and a reader. Consider the following scenario involving task->comm: reader writer len = strlen(s) + 1; strlcpy(tsk->comm, buf, sizeof(tsk->comm)); memcpy(buf, s, len); In this case, there is a race condition between the reader and the writer. The reader calculate the length of the string `s` based on the old value of task->comm. However, during the memcpy(), the string `s` might be updated by the writer to a new value of task->comm. If the new task->comm is larger than the old one, the `buf` might not be NUL-terminated. This can lead to undefined behavior and potential security vulnerabilities. Let's fix it by explicitly adding a NUL-terminator. Signed-off-by: Yafang Shao Cc: Andrew Morton --- mm/util.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/mm/util.c b/mm/util.c index 983baf2bd675..4542d8a800d9 100644 --- a/mm/util.c +++ b/mm/util.c @@ -62,8 +62,14 @@ char *kstrdup(const char *s, gfp_t gfp) len = strlen(s) + 1; buf = kmalloc_track_caller(len, gfp); - if (buf) + if (buf) { memcpy(buf, s, len); + /* During memcpy(), the string might be updated to a new value, + * which could be longer than the string when strlen() is + * called. Therefore, we need to add a null termimator. + */ + buf[len - 1] = '\0'; + } return buf; } EXPORT_SYMBOL(kstrdup); From patchwork Mon Aug 12 02:29:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13760070 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6A286C52D7C for ; Mon, 12 Aug 2024 02:31:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C700F6B00A5; Sun, 11 Aug 2024 22:31:15 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BD1616B00A6; Sun, 11 Aug 2024 22:31:15 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A71D86B00A7; Sun, 11 Aug 2024 22:31:15 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 85B5A6B00A5 for ; Sun, 11 Aug 2024 22:31:15 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 3527E160778 for ; Mon, 12 Aug 2024 02:31:15 +0000 (UTC) X-FDA: 82442016510.19.ABA307C Received: from mail-pl1-f175.google.com (mail-pl1-f175.google.com [209.85.214.175]) by imf05.hostedemail.com (Postfix) with ESMTP id 5C25C100007 for ; Mon, 12 Aug 2024 02:31:13 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=mzNa3V5F; spf=pass (imf05.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.175 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1723429819; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=mY4VRCvd9L4i067FYJCdAQbgb2obGf5r9hy8hsyaNQ4=; b=PQtBz2E+Ey9ef4jNlDo45Bbfr9uECTrqZncYj6mR/qCGh5i5jmKffgjdSz8m3w5ec5+YiC LxlhBhMHExJOCC/XInXjXgW5cnVgBEM6yYrVX698pFpYFtKBZsuqnVx6h5VtAFTTZVdej+ Bkzvq7otEyt5FJKzzbBFwMevwRcHPVA= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=mzNa3V5F; spf=pass (imf05.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.175 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723429819; a=rsa-sha256; cv=none; b=B8oCf+DhMcLbBGhFrxT35fGPxtGVoTTkaE/VoDBCwKmQesLfpSaPMSVVS57/uxQXsbCt1I ZIBhWyMCPJRRnVTToXne/1GnO6dzaN6gX0BI/+U1CAxK1Aqf7TnmAHJulpPr5GDbKpU9VP G6qenwusDCnPhfeImVmO4VZ8ZjrTKUo= Received: by mail-pl1-f175.google.com with SMTP id d9443c01a7336-1fd66cddd4dso37874385ad.2 for ; Sun, 11 Aug 2024 19:31:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723429872; x=1724034672; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=mY4VRCvd9L4i067FYJCdAQbgb2obGf5r9hy8hsyaNQ4=; b=mzNa3V5FX1bH1l5FCvC4j6KcB2rI5vZSphxoqkF0xsJ+teaXAT1CzIfoNJlbF/aFoD PDvIWGJS6eJsBKkJmDbVUHfzbUpmbd0fJXJty/PkA1gcDDMpwOH3mGi/z+UPni5gfh6k 2wa3RV7qkA/yuqPz665acHQn/eGdNRhg2EzAJXY+bQnH2xNoJsf11+z+L7+CcNlo2LKb BsNz+H/d4VBtzolVUaD3XBFp+DVEfeDX/0Fvv37FtqTIGBXxioEToHJe/2pZ3NzhQJVc EGJhBlaXyj0+rvDDX3Ag+tHkq09CH1zZpJjYwy+Fzd5+2EGn3mcP3CutwKpobzSZVBn+ wdVQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723429872; x=1724034672; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=mY4VRCvd9L4i067FYJCdAQbgb2obGf5r9hy8hsyaNQ4=; b=i4kkOXmcQxWJOavPuESuYGxao420IRafuxIVOSCeFPqvqf4pF8lMzbMfhK62zKXwns kkeVG7SurhvLKu0AQFz2hgDjQWgJxR7wiCPDFKBpF9uATtnPKzefh3xylQ93zkYM+HZP XuChdY+hI5yDrHf3ATv42StvSQqcLOM9r5DFpt4JYESNpe5L0GfhnKP4XSwYRWY7M2zf uI7Mq22SogjHrQxd73k8H9EaqXQ17g2SEH9SsxhAwsVlpTd6BktUaiH6cltmKADIhFbv VM6HzuOtxQd/1b+vSTGVoFqxBBUoa5odZ/Z0B8lDV6fm9/I8sXqtr2W7t0AZ+ZebYjrM gNsA== X-Forwarded-Encrypted: i=1; AJvYcCXiy1WKpoqkkU/8V7paeO1rv4MBs54oarqlPbwzOkkMB5koSXUeGPpNLpVXE0obJkOhY2XtDtAG0waTsQR62RdgWQ0= X-Gm-Message-State: AOJu0Yy75sF6kTA5Da9UrYO/0aCMRSjJPO3/EP5vHMmMIIpU9+FkIRZp 4mmJVv+FMjhO74+F2eCUmuk69TEIB3hwWjs07Ji0qmkVbKzkYJ7I X-Google-Smtp-Source: AGHT+IGDYbe0RTpouvyRH8j57LNI0ygI4N2QpIwMJo/CclCqBa2C0H4JAnlCXTMiS2Sh5vz84U44EQ== X-Received: by 2002:a17:902:dac3:b0:1fb:48c6:a2b0 with SMTP id d9443c01a7336-200ae4dbc8amr93864745ad.5.1723429872026; Sun, 11 Aug 2024 19:31:12 -0700 (PDT) Received: from localhost.localdomain ([39.144.39.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-200bb9fed69sm27884765ad.188.2024.08.11.19.31.06 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 11 Aug 2024 19:31:11 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, catalin.marinas@arm.com, penguin-kernel@i-love.sakura.ne.jp, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, audit@vger.kernel.org, linux-security-module@vger.kernel.org, selinux@vger.kernel.org, bpf@vger.kernel.org, netdev@vger.kernel.org, dri-devel@lists.freedesktop.org, Yafang Shao , Simon Horman , Matthew Wilcox Subject: [PATCH v6 6/9] mm/util: Deduplicate code in {kstrdup,kstrndup,kmemdup_nul} Date: Mon, 12 Aug 2024 10:29:30 +0800 Message-Id: <20240812022933.69850-7-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240812022933.69850-1-laoar.shao@gmail.com> References: <20240812022933.69850-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Stat-Signature: tfosdift4m4infjjxn5odbupqz4mr46z X-Rspam-User: X-Rspamd-Queue-Id: 5C25C100007 X-Rspamd-Server: rspam02 X-HE-Tag: 1723429873-429955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NEC5S3SUmTWJHfvLJy4LmUrtHDGy9TfwOEkeflwotntIj2C4kTVMWsG1zPWoOIjZ0TXfGsuPZ9XdmU/lyhCEOpsdai1VhJ9jTDs8WFfBhi1KGA2nJMV2KBbgSEdGEsj2QZPiILcto4XFUSh7vIM83P5PGeuO+9ZiJhjg64ekeE8ZJ7n9O/SHxXvKY6BYFHpJcGyQ5jlqrTuQUEB/HgGacDilUzE2IkFd+507tnFUALpFyJklKI94b+qIAaG/xKMefhLIn5EcPJYATtDSrGw8kWMYLP/r+Ha2yOGsOJx46ZxJzEt87MXmw7U4+wPhxmTMI95zcdbZBIXbBQBkLhFNWhLWo3SDY30Nsdc9gMcAQtLsSLe8Co5Dx0NnQDiQlIknekyK2PqJtfY4eTt1bC/HPXVkJ4Y/X8Wwkm0wlJc50l1xRNuRqghgpDRJZVik4uLn+vHdpo+XZOrVviNEZBPpxnuksFnEDTLRDVGyKTvGpSqil9dMQmsHnrFdH/dZPsHlvVCG3Fbb4/I/TDtt/DTwyD96zF0k67ovHp5xS9rL9xcu/rPc8JphpRZQLz2IuNKblEBDtrecQrOdwYKyloE2S5fycQ6N07M/S0TpLHRf8HHVYFXPmIbgTgxLKykuJvyb7MZaBgRAnL1b3jRVfwVl6H0LOxEtYIRSgErLlmARYTi79aQF0TAbwQWUa9Vn3bS0V21 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: These three functions follow the same pattern. To deduplicate the code, let's introduce a common helper __kmemdup_nul(). Suggested-by: Andrew Morton Signed-off-by: Yafang Shao Cc: Simon Horman Cc: Matthew Wilcox --- mm/util.c | 67 +++++++++++++++++++++---------------------------------- 1 file changed, 26 insertions(+), 41 deletions(-) diff --git a/mm/util.c b/mm/util.c index 4542d8a800d9..310c7735c617 100644 --- a/mm/util.c +++ b/mm/util.c @@ -45,33 +45,40 @@ void kfree_const(const void *x) EXPORT_SYMBOL(kfree_const); /** - * kstrdup - allocate space for and copy an existing string - * @s: the string to duplicate + * __kmemdup_nul - Create a NUL-terminated string from @s, which might be unterminated. + * @s: The data to copy + * @len: The size of the data, including the null terminator * @gfp: the GFP mask used in the kmalloc() call when allocating memory * - * Return: newly allocated copy of @s or %NULL in case of error + * Return: newly allocated copy of @s with NUL-termination or %NULL in + * case of error */ -noinline -char *kstrdup(const char *s, gfp_t gfp) +static __always_inline char *__kmemdup_nul(const char *s, size_t len, gfp_t gfp) { - size_t len; char *buf; - if (!s) + buf = kmalloc_track_caller(len, gfp); + if (!buf) return NULL; - len = strlen(s) + 1; - buf = kmalloc_track_caller(len, gfp); - if (buf) { - memcpy(buf, s, len); - /* During memcpy(), the string might be updated to a new value, - * which could be longer than the string when strlen() is - * called. Therefore, we need to add a null termimator. - */ - buf[len - 1] = '\0'; - } + memcpy(buf, s, len); + /* Ensure the buf is always NUL-terminated, regardless of @s. */ + buf[len - 1] = '\0'; return buf; } + +/** + * kstrdup - allocate space for and copy an existing string + * @s: the string to duplicate + * @gfp: the GFP mask used in the kmalloc() call when allocating memory + * + * Return: newly allocated copy of @s or %NULL in case of error + */ +noinline +char *kstrdup(const char *s, gfp_t gfp) +{ + return s ? __kmemdup_nul(s, strlen(s) + 1, gfp) : NULL; +} EXPORT_SYMBOL(kstrdup); /** @@ -106,19 +113,7 @@ EXPORT_SYMBOL(kstrdup_const); */ char *kstrndup(const char *s, size_t max, gfp_t gfp) { - size_t len; - char *buf; - - if (!s) - return NULL; - - len = strnlen(s, max); - buf = kmalloc_track_caller(len+1, gfp); - if (buf) { - memcpy(buf, s, len); - buf[len] = '\0'; - } - return buf; + return s ? __kmemdup_nul(s, strnlen(s, max) + 1, gfp) : NULL; } EXPORT_SYMBOL(kstrndup); @@ -192,17 +187,7 @@ EXPORT_SYMBOL(kvmemdup); */ char *kmemdup_nul(const char *s, size_t len, gfp_t gfp) { - char *buf; - - if (!s) - return NULL; - - buf = kmalloc_track_caller(len + 1, gfp); - if (buf) { - memcpy(buf, s, len); - buf[len] = '\0'; - } - return buf; + return s ? __kmemdup_nul(s, len + 1, gfp) : NULL; } EXPORT_SYMBOL(kmemdup_nul); From patchwork Mon Aug 12 02:29:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13760071 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 07586C52D7C for ; Mon, 12 Aug 2024 02:31:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 914776B00A7; Sun, 11 Aug 2024 22:31:22 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8C4566B00A8; Sun, 11 Aug 2024 22:31:22 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 717E66B00A9; Sun, 11 Aug 2024 22:31:22 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 4D6746B00A7 for ; Sun, 11 Aug 2024 22:31:22 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id EFEC51A06FE for ; Mon, 12 Aug 2024 02:31:21 +0000 (UTC) X-FDA: 82442016762.05.183695A Received: from mail-pl1-f178.google.com (mail-pl1-f178.google.com [209.85.214.178]) by imf18.hostedemail.com (Postfix) with ESMTP id 21C971C000B for ; Mon, 12 Aug 2024 02:31:18 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=LeBGyDP8; spf=pass (imf18.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.178 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1723429809; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=qdEfItmwefU07w76SDkXtsO/LaxPCVZAR+StHmo8iTc=; b=0iKw23KWXPvjaYADxCQWjth1270DUWVf86QoURQARTH6RwfyAK+QWrSpP9U5VAmNEensHV UlStYvo8p6GxDv0BK47wzcJxU1uMcfCnJLHNCPf7V2bRfIISYycG6JBG10eqQeIIes3O9Q 6iBqUOYIVkanl5rifXZ5AaVp9B+D9+g= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723429809; a=rsa-sha256; cv=none; b=VfRnhblCA//jMvwcNUHE1vcA39SDYyu3MK0qWlBHBbPCniHdf9kPnrmzKnEUi/wR8VjbPG IiTBfgctCxpYhXw67aAKooeO2iTwpKWQS63lnQrAVa7CxWASz30zG5CB4qKSMGqJA9lyXK 9IMx6r+ZSCVooYhTpOK+frkBR/2YurE= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=LeBGyDP8; spf=pass (imf18.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.178 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pl1-f178.google.com with SMTP id d9443c01a7336-1ff67158052so23753615ad.0 for ; Sun, 11 Aug 2024 19:31:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723429878; x=1724034678; darn=kvack.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=qdEfItmwefU07w76SDkXtsO/LaxPCVZAR+StHmo8iTc=; b=LeBGyDP8wnyxtC0RWqBmCDnhPd3f61hWoPX+sWFttXZ2QH4v/MvPBX8cN/zCrePlXt 3vm/It0kMd8zGXlhci238m3lncNnMxbCndvNM5FPLbk6BGnvyAXNrrJsipC8trpYG6n9 p3d3qB2fQWm0PE1WvArpxJlnjQJDbtN066n7+DRrD0Vmuq0G3BkJiOlHd01k8BkcpzQc Ee4D/dNNqnqGZM8Lqm2ujSL4wSeE/gdCgJ5wSV9rkkdzv+FoybApBFIkbdOTI0tE5oj5 9cajW7jj62moCWhZWTMPw0Li28wmI2zq+XXmsahVkA5jQQ/x9Pbb9WQIP8d/OVl7Twps jpFg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723429878; x=1724034678; 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=qdEfItmwefU07w76SDkXtsO/LaxPCVZAR+StHmo8iTc=; b=DfqORa0l7BJChXtRuy1DMn3HmLYzeM09XJ9Wu/pC6mt6WQdU1+k6WV2IC+lSinFFHx luB372l3AMX+jH+uHVtqAtgkH6X6Yqh7gbFps1msFc9lkyGj7uliPGDBFCxHeKmk5SFC iwfKzINmDVL52MOd2JDxXZEgSseH+0ONqMQ/ETbOHAcm3B+nkkBoUHg7+UaoYczjyMVE AQYhR6k9qnuIV5NY4XmtweFKQs0oon1Js9LOg+tOm4UGgN/wi4UQswH5ThJR+XsVmMf5 nhrn758o6ws5Vk0/DsNYB+dML71Gzds5oSTFEg2R0cS/KvD4mM77liT/B8raX13M0VOx whSA== X-Forwarded-Encrypted: i=1; AJvYcCUPu+3uzzHy6ssPZAcG7uQO3LbA1GhI2JvgCR0SNJ1KyTYt1bpFV6/xiqQ35uzbEM14DZhg6U+a0S1aDa8BL3FHXCU= X-Gm-Message-State: AOJu0YypDyCixcv9UNMAf2Ycpv5uhVZCj2hfO/VgARvYjtLO4fcISl+o G3qAipBeJXxQjgyCBUWdZu7L0+UwslcAzjcBFiWseEAttmsCKDGE X-Google-Smtp-Source: AGHT+IGvMeJ1WDvZRyKICOlWBdf489ybDgp4ZGppLiFztYYS3nDRhT+W8sx80+j2UnrjElUuwI0I0g== X-Received: by 2002:a17:902:e804:b0:1fc:57b7:995c with SMTP id d9443c01a7336-200ae4dba44mr56315805ad.7.1723429877770; Sun, 11 Aug 2024 19:31:17 -0700 (PDT) Received: from localhost.localdomain ([39.144.39.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-200bb9fed69sm27884765ad.188.2024.08.11.19.31.12 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 11 Aug 2024 19:31:17 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, catalin.marinas@arm.com, penguin-kernel@i-love.sakura.ne.jp, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, audit@vger.kernel.org, linux-security-module@vger.kernel.org, selinux@vger.kernel.org, bpf@vger.kernel.org, netdev@vger.kernel.org, dri-devel@lists.freedesktop.org, Yafang Shao , Masami Hiramatsu , Mathieu Desnoyers Subject: [PATCH v6 7/9] tracing: Replace strncpy() with strscpy() Date: Mon, 12 Aug 2024 10:29:31 +0800 Message-Id: <20240812022933.69850-8-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240812022933.69850-1-laoar.shao@gmail.com> References: <20240812022933.69850-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Stat-Signature: jw3uacf6xmbeh5oi9rxour9zstebnz9y X-Rspamd-Queue-Id: 21C971C000B X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1723429878-931416 X-HE-Meta: U2FsdGVkX19Um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kx8dcrDjYcU2RzAw2Y4yBvCQ8ccEWSkS6klIaPR4xqIPh1weIVuSqbbqaEZzdOJBZHfURzMBI1rF8vDq+loTCAevDqFLFopA6aKFRpsXbdhA/X9y8BEKwU3VTZjOyTJji775nFgVxnUP7PhbQAxRY9Vuz0aSwtHth9SCy6Z9IPe3ehyLcXuqAz8/UL1xzgCynxkQGcphJqlMMbPhSvjUcWoqIm3X5Eekpm0CJeS0UVKRwEMBRWCa1nE0DMvsThPFtlCDfevG+7lAMyXto3vIGE1mK4oyUSSXWXgvwVlpN6/IWzQ0sE594vZi1q2CSHeUUX8ZjccQoU1lMdS7KEq94yPNYVnKaU/DgLhBRiPCiJBE3a/at6T+YCZrlM2bK8Dx210WAbC7Rwprz6yjjSDJQczI7wipz7wV6Cxc5ydO7s8bMoQ1oYFr9cIRn4Qb+2/xvpdbyKSKKibvwl5RcSBFJGLqkfkkSWVChYMziwHu7zYLfQJt/RBOVzv3ETDHrpJLCiLBnyclFiny3B6G3GqZr4deGfpn X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Using strscpy() to read the task comm ensures that the name is always NUL-terminated, regardless of the source string. This approach also facilitates future extensions to the task comm. Signed-off-by: Yafang Shao Acked-by: 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..1b2577f9d734 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); + strscpy(max_data->comm, tsk->comm, TASK_COMM_LEN); 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..4cd24c25ce05 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); + strscpy(comm, task->comm, TASK_COMM_LEN); } static void hist_elt_data_free(struct hist_elt_data *elt_data) From patchwork Mon Aug 12 02:29:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13760072 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 21731C3DA4A for ; Mon, 12 Aug 2024 02:31:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A9C1D6B00A9; Sun, 11 Aug 2024 22:31:28 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A24006B00AA; Sun, 11 Aug 2024 22:31:28 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 876DA6B00AB; Sun, 11 Aug 2024 22:31:28 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 652C66B00A9 for ; Sun, 11 Aug 2024 22:31:28 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id DA4B5120370 for ; Mon, 12 Aug 2024 02:31:27 +0000 (UTC) X-FDA: 82442017014.25.689CAF0 Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) by imf17.hostedemail.com (Postfix) with ESMTP id 13B2140016 for ; Mon, 12 Aug 2024 02:31:25 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=VNmplp7a; spf=pass (imf17.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.174 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1723429852; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=lQq52x1okLoFlEdSdcWpS9opM5nCjcuUg5Jqdy6Utiw=; b=YVJlM7fQHzp5VodFCC0jrUTD+WrgJalSvjF4dW6rHTtafRFaLufFt4QORiPShCBZFQkDNg qqT8KE4yEcAqgqpPlJGOH+G0MC2DLrlUOjeKOIaP7pvyEX2oVco++4Gz7lBmIaefGmbQGa a1KXcQ0abl589Eg9fSp8OqgEDtLVKyk= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=VNmplp7a; spf=pass (imf17.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.174 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723429852; a=rsa-sha256; cv=none; b=CmRA/bA+zYeaZeur+vKp/stYJA+X9H9TLi8HElQ14J9M5QX81eabK47Xj3gOy2ErX34kFE B070wXCu5xEU7r0UeBznjcqFp4nXfiGCbjKZgExgRhPRTMhkKL8tbeIk/tWlEMkaQuxnCa 6ylZJ9PchM80TXhKk59LX3A0MwOxNgU= Received: by mail-pl1-f174.google.com with SMTP id d9443c01a7336-1fee6435a34so26516265ad.0 for ; Sun, 11 Aug 2024 19:31:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723429885; x=1724034685; darn=kvack.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=lQq52x1okLoFlEdSdcWpS9opM5nCjcuUg5Jqdy6Utiw=; b=VNmplp7aE348rug2qSwHJ98aI2+ekjUB9Zv+e2fd3f6xPV6Fwbmz82x+sEe+ArslxA KQn/gv4jjzfS3YS5elm21s1d5wFvddULMFCcshdz9ajBCQ2ih5skiRUib9Kud9/6WtBE HQSDSXMVpGCHsbT03U9o732SJ7VAasZu3u/o9+CcZRpdyvaxkHX6buIWt3Xz8poMUmod sS1+9IY4zTGWeUYKYvdE8+fxdRSBIWIBP1D2RAcSOd9FkJU0McUKXQMfDuOnjT8TuyE9 GLSkK6qXvxiMT07leJx1tiT8I/rqS8hy+BEn1brn6z/CPEljm32p0+h5ntb/TYfe23va zjBQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723429885; x=1724034685; 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=lQq52x1okLoFlEdSdcWpS9opM5nCjcuUg5Jqdy6Utiw=; b=ut2GuBcGSOYbP9sxb8iMpUxomcO70VtnlZ6k1XBnWexA3Uekm5ehZuGY9k0HHCRg7V STAdgieGLgx6Jvp6g24pbgvMSbEzzhe4bMpCCAWyrcRlnAyAMeoQqQyVkqo4XIdQuDs0 vjGiP2rfEewA+LoAAP0yUEfCQBwuFgY+Bb766EOSFO2KGY6fbbc1wQkJDag54aR6ukls Y8qfwIpRHSawblJS+FlVxytWtyuTa9qed6X0czg7ct9a9fHT5ypBchWoUPJQXXpHkOdn XoW+7qNtWy+cx/Y3cJNZaeiM6OTy5VGrLz/VxZBDatoI5gl8lpLeB1EadeVBh/3K4xsE mHGw== X-Forwarded-Encrypted: i=1; AJvYcCWLN+yqvG8K+jxlys1ipbhR3mzc7m6ZB2aZOY6kse3LqRqOVZpz0+M8rjQqUV5dy06avo6djsBxdBtIa+Nk0HiLQjE= X-Gm-Message-State: AOJu0YygSyd/FEoGBzzEIinse3DklPCpH8MQ8oKjaF3P0Q8RgyoaWEJF dyn7Sgk/hC/sMCD662h7GxM/576UTCHjs8l4BmIvNKKT4Mkpt1hM X-Google-Smtp-Source: AGHT+IHaetHTBTCym7N0t424xRy6qtmLiFL/7ctUucHwtwIF7cfl/lI/MF8VrMeOi3ytURGCyHANzQ== X-Received: by 2002:a17:903:249:b0:1fd:7664:d891 with SMTP id d9443c01a7336-200ae5cfb6amr57289655ad.44.1723429884796; Sun, 11 Aug 2024 19:31:24 -0700 (PDT) Received: from localhost.localdomain ([39.144.39.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-200bb9fed69sm27884765ad.188.2024.08.11.19.31.18 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 11 Aug 2024 19:31:24 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, catalin.marinas@arm.com, penguin-kernel@i-love.sakura.ne.jp, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, audit@vger.kernel.org, linux-security-module@vger.kernel.org, selinux@vger.kernel.org, bpf@vger.kernel.org, netdev@vger.kernel.org, dri-devel@lists.freedesktop.org, Yafang Shao , "David S. Miller" , David Ahern , Eric Dumazet , Jakub Kicinski , Paolo Abeni Subject: [PATCH v6 8/9] net: Replace strcpy() with strscpy() Date: Mon, 12 Aug 2024 10:29:32 +0800 Message-Id: <20240812022933.69850-9-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240812022933.69850-1-laoar.shao@gmail.com> References: <20240812022933.69850-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: 13B2140016 X-Stat-Signature: nbsxix7hfgjsjj38obuedux9tuecpnkr X-HE-Tag: 1723429885-382333 X-HE-Meta: U2FsdGVkX1/C5HlkxM3YJWw6qI9Vsf2sqKYMT7DNrXrqjh09zSX5iOTwmPaXqz5AUZBynMqvrCO6N4ryzZKJSXewSNql0r+5KhE0GZ5cjHpt3T5ZJKGdCQDKMjGO6qKJ+gTeBG2ci+lMdwoyvrBneD+/9n5g8i41r6wgDbyiIsJU8BiJDESXIn7s94v0qt8P8KM3XxDtfSLQyYd0vF4xR0tjQ5Y60l10kSxrKYig1NL1vv4RC4erpy7GeqNTJLW0NahGEZxHH/ERSp7Zwn/yqdr7ihdw0i6PCRt1sqtfB6350KG2K8nLKt5Z4U51m2Y9DQggOEVQTpzPDbi2gpOW5vKir31uBmrCrtoZiv1Wt8cX5C6Rz2NzSPqXRSAGMhpfkCMj8kVAUClCDThnFTO0pqChU+IUQ6KfKHjBUkmYnqnikQf0x4RwognYAQkm3Rro2omYyf0+T71gBtiuiui9xn3YYmwtZusL001mJ+591OuqdZeMh1QGK24PxQvGBoiz9OWMKSiRyd3FcrswE9MrD1zS74RPLeomOJr+nCDm3uIFxjQhwERDlrSxm2MPtW6XIClZcpWSecqyO6CIvlnzun4617OiG6GcNc+V51XJKCwC6pZe9T3/0wlR3wuAbj+oRHLf8sTZvTcoLWaKcnzx4jm26gx52n8a6sJ8pX8S5e/gd+hSzwck+rtlCQhNZtkPh3fS0+0i55r1LH/t+kSpPD/eSEjOoMUCs0hvJJV5o5T1qY/NjnkuzmGG20vKSoTZ9/bll/4c/Tksv+LAks5W/OPTUQbElJgYXkcuNlKKIUj3ZJ1QLSZza0jlpKWkLSAlL2QZ4WJipuhJ1ctRy4TnoxKDA0VI2s0Vg/BijTBvHLkt+w2ueRMznnesgnMH7c7fwuqhFALgKUO3oHSHJiRAGrhFoS9xp+WWRpgyNotWBbgbu4jM3XCd53+vBvWqGpZnQ/I06aBgNXjD9zvQwQX s7DYUwMY o+ToCRinGVlZNiJ3CAvABnMoa0qsx0gplBK9rTwcz1/9wTyTFpwPUriYx8AB5y8rDovoW+RD+UZJRrIIv+Cdl1W23OCVYJostDWaHz0ggBHUKj3aoWLE+1Zu31yg9V40iCOCSSHZWpQ0MmGkbdQKWOPfAcXqBw6rZCMocMyOV3pRMgOLZ/MKYSW2KLupLmeT9tYiIWcv4qXtN370aEj4tYSdPeTOx7JrNsT78Gsd+G8GMOQt/M4ZoTT52Ntl5UN0lp58bVM76mpRf6HCzF/1K6umNhsr4dJm29s/j1cMW2qORGg0+naKI4nDodwtRqWnpA6+iQmzOS0IUdOf1hmLnAsE3i0zpcdifXAPl9kA5YtOokuGCvlkkRl03spU9WGsnl4NTZDphJIFlrw5xdtc/p0AVDLacHS/+fqyIUoO8MAICaoM1OPT9iBlckGAChVWctm9tAAUkd3CUG+zOdxjQJv3JYfvG2zMDt/VFacW0iwAeYIYqDJYMtCbx2bxtK4jr71ehHBtOG3ASLbwcFwddv4Pl5wLXjcF47Mpa5I6bjsio0uLn3o2jhnsBBcbQq6ubK54npqVKYKeyhFzQAWKKzJaS4snfuJxhoOkpBU2/S5gKbURSgt129X+1Fg== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: To prevent errors from occurring when the src string is longer than the dst string in strcpy(), we should use strscpy() instead. This approach also facilitates future extensions to the task comm. Signed-off-by: Yafang Shao Cc: "David S. Miller" Cc: David Ahern Cc: Eric Dumazet Cc: Jakub Kicinski Cc: Paolo Abeni --- net/ipv6/ndisc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/net/ipv6/ndisc.c b/net/ipv6/ndisc.c index 254b192c5705..bf969a4773c0 100644 --- a/net/ipv6/ndisc.c +++ b/net/ipv6/ndisc.c @@ -1942,7 +1942,7 @@ static void ndisc_warn_deprecated_sysctl(const struct ctl_table *ctl, static char warncomm[TASK_COMM_LEN]; static int warned; if (strcmp(warncomm, current->comm) && warned < 5) { - strcpy(warncomm, current->comm); + strscpy(warncomm, current->comm, sizeof(warncomm)); 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 Mon Aug 12 02:29:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13760073 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8C675C3DA4A for ; Mon, 12 Aug 2024 02:31:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2ABE96B00AB; Sun, 11 Aug 2024 22:31:38 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 25B416B00AC; Sun, 11 Aug 2024 22:31:38 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0D6356B00AD; Sun, 11 Aug 2024 22:31:38 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id DDC0E6B00AB for ; Sun, 11 Aug 2024 22:31:37 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id A5B00A73A6 for ; Mon, 12 Aug 2024 02:31:37 +0000 (UTC) X-FDA: 82442017434.09.A62FA49 Received: from mail-pg1-f176.google.com (mail-pg1-f176.google.com [209.85.215.176]) by imf01.hostedemail.com (Postfix) with ESMTP id D40C240009 for ; Mon, 12 Aug 2024 02:31:35 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="KjcBx/34"; spf=pass (imf01.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.215.176 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1723429841; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=zZu3KZG3iOINXwUvnWcZKbey2aLH0BWohtl6uQ29mP4=; b=6dgpg57vkTqdfZcP7g6Z7w+6ba/NB2eNELP2YTIqEDlixmLz2niKKnW3MxqcnqwT4Bi+GX P+ujqx6TZIOEHrcWCkB73JkP/Vn9nTw7pO9zXvhCfbgpOKSWEjAfMrg2T4EphWpuy44lvB 0Nx55pcF2WIAKYCi+3QQYEl+I1M/r5M= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="KjcBx/34"; spf=pass (imf01.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.215.176 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723429841; a=rsa-sha256; cv=none; b=tY6xRx9Q4uuuJQ2ld8YU6OUe9+fWXzSa93nDMz+kq5DeyjNjIuqdhgUt0GHrDZddAS3Qb7 Bu6xGH+ftmaeNeQXsdUaTKxh4d61T2N7I4sHJDa2eagkhVKg2PaotSEr5HFBewvyavJPY1 wPGDhUFo7KYHei3o1bmVKbJJVknURJ8= Received: by mail-pg1-f176.google.com with SMTP id 41be03b00d2f7-7a115c427f1so2437243a12.0 for ; Sun, 11 Aug 2024 19:31:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723429895; x=1724034695; darn=kvack.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=zZu3KZG3iOINXwUvnWcZKbey2aLH0BWohtl6uQ29mP4=; b=KjcBx/34zbGQAtPz9V8GBVk3OgIOl4yMoXCaJDBaz5AWXuPybqMJO9RunUmltwZmah 8ZIHvcz4Bl1D0//mLUBT+KSgwf6hg6SGnElAGVgqSkWlYSGfDwXikQZfQAJjIlXEZqz5 lx/o2hDCkkxbx1vRsBNYLFJEorq5L96Vb3iRL/TgNlEmeDtnmKYrpYGWUXxrFrcGTleM l64rC56Gd4baimGVmoE5tEMhdurn3O3hlHSOory4PFYBHfkNm0YzguRyG5Ue434Y8Gci 9yeUTICT5FdB2Bqp7RdhxJJD5LXpDJB/w1DomO5Um2JNEbh7sjFNvrT6J44vC43KZKis 0q6w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723429895; x=1724034695; 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=zZu3KZG3iOINXwUvnWcZKbey2aLH0BWohtl6uQ29mP4=; b=OnqkH0ZcxVe7xMd8/XKPFfYHVFjKg2uMGMJNPZ9UcYZPy+Zy4y8gaiS9xfinRjgpWb z44C9Qj1zDzXrgUZZDHZOU5O4XxsP/v3VIpzXKDURuZddDfL5X8dhWcxlh4c8D4IP6VJ 0c+Sp/VY2RMEZGObiF3iBi2uI6iFYP4g015ZiKc6c8XMA42MzyIxqF0RkptRXTyFAtkG 4HHaBXaUGQ3NJ3MQYO2ov0tTSIXvj+igyJg8jxqaGHkvF4DVpRsnR8SM1GvS+/Zt7Ubs u0kRrwb5CCj/Ows+dT9YeZS09QQQIrikbIZtASOAsl3tAWCWl/+6xnEYRGyCAHxvcrXM dgKA== X-Forwarded-Encrypted: i=1; AJvYcCXv43S5BkOOO+JvszVUIURnsXAL7nyYISQqk9VYAdcdsABTlsRioGbxk9K9BbBR5zpIdnGbIN4WZwWR3BKoAmYil3o= X-Gm-Message-State: AOJu0Yz4RQvDdzxj4osobXuf0AyNYl0ekroBx6A5rJ9RhJAjTmC/3qvp rqjTVb/KRkBPMJKRRrDUH3F+xPFBtBMSUH7T9LG1yaY6Qblwx6ojNmYqxd6FDYdxsw== X-Google-Smtp-Source: AGHT+IHdX1dJ6B1gxMt4DoIS5gsxO+K0m5bSRRhjzARSl7z6wL1zfkA/bgMgo4cA342XfY8E/QA2Rw== X-Received: by 2002:a05:6a21:b85:b0:1c6:fc56:744 with SMTP id adf61e73a8af0-1c89fd26229mr6526380637.31.1723429894568; Sun, 11 Aug 2024 19:31:34 -0700 (PDT) Received: from localhost.localdomain ([39.144.39.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-200bb9fed69sm27884765ad.188.2024.08.11.19.31.25 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 11 Aug 2024 19:31:34 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, catalin.marinas@arm.com, penguin-kernel@i-love.sakura.ne.jp, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, audit@vger.kernel.org, linux-security-module@vger.kernel.org, selinux@vger.kernel.org, bpf@vger.kernel.org, netdev@vger.kernel.org, dri-devel@lists.freedesktop.org, Yafang Shao , Daniel Vetter , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie Subject: [PATCH v6 9/9] drm: Replace strcpy() with strscpy() Date: Mon, 12 Aug 2024 10:29:33 +0800 Message-Id: <20240812022933.69850-10-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240812022933.69850-1-laoar.shao@gmail.com> References: <20240812022933.69850-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Stat-Signature: 9yq6bp9zgw7k3ts8in1cdfq69ebmuwzf X-Rspam-User: X-Rspamd-Queue-Id: D40C240009 X-Rspamd-Server: rspam02 X-HE-Tag: 1723429895-513726 X-HE-Meta: U2FsdGVkX1+rOaJzjzQXwYqZsoUm8RdBJHhAfMrKFx0zzyE6+130Kx5eBDwabzadRIhVWoGRZ8i1TTJBvIGK0bjLXAcd2QuBkTJ2fGdPGroJDxCRAKCF9w2Pgh3VhPWU+N+TTeJoAMfVS3mZlF93DYcfojIGfm3s82cdWXJ+iIPiExgmthT12gKkv5DzAUi51JVolcYktdm7PthMwJiZt3Wgo6r0VSap8gvlzavnjlsjlOsiFPTwFHsW+S5rbJW3xC54D1XmNy/1qiqMCuUdMG+Bj3s76zNQStVdrjmbfWE48pl89HrtrGRQEFe6N7WfDLtrU8tCHT3JYxzM+CD495Qm0xoaXewrwPhwH/LDysv+XUzl3VAh9kb93VL6xIAKvz/p6mEEm3RN1t6TMcgCEBK7V11elm7k+1JuRJStaCjYxlsERkQef4LksnumptLUPPBVsOdQL/dSX0q0ZTm8MqGL8iTa8/QsenZqzWdajagSIYqn0QgYRZXDSoEPNhZmsQMA7pUax900xINoEr+o/T4HDVjkbjmZ1Pu/J1d6gMusNJGrwA2t/BXRTPOcc7TdFl3Pbwd73+VS7qawIl5vnjaO8WcE6Yl5m2cA2XCIwbU4QobUhucyyZc7t7Has471rfKe1OLGM8gc8VGd31Y55x5RynFlxLUEFxm7SrG+Ze0c2vC2NZJQSBj09V+AbVA3K7fCJ5ufDkTmeqbHTuJz8ZOFXrBrY2RGpj6tAh0FTwuay3SzV2PGis7jZxl3DRVruNvbmbfvbuqfSTVGjzqTcFJ1Hcu8ARv8o0+kXEkBLR1/lSa288ang24KR7nWkY9DkLc+8jctxXrIMKpc48pHwvXMKqDOvt+NkBEtwXRT7kIIBKitMN+VtLdZrCEj0b8sfEFZkkKSE95Iqv5X2q08mchtWE9dk9zhyV8M3WM9vJ+tkyloKwZoG1SrupjEJAGbX0n+5OXayAUMwNdNktJ o9wWkBj8 up69BQNdkT2weNIUk2DLoEY518VlF7YjXwfOszja0ZxI1V/ySlcB4tptcaz+dgQqJkt6JRfLVS6wAKMF29PmU/C+JiwhUyRBgsUGzdNh+7iZrY4EFNJpv6ix6sNdyt4MMYs7qWF3xZbeUpbE+PxbAzazJ22YeVlvF/jIjA9RS84KPHG2I4mItcwjmjymQR1e4uqMPhaqWkQG2sHXj+WfjtjNtp5+dR0xRlXygTB+KcaQq180wQ9bKwroP39Iyk+FFpg3+eC/l5FAv5K0KE3nf7cp85GQ54Qry2+g4CTlQj9W4X4VGhGlHTvFlclNjHJSuX/n72T65EQo2QIBJy2IXXgZhBRBz7Q9j8f5fg2HbvojKFg9csh1VPyfH1/4ugfyxAHLxQ2b1qpKzKT8Fc0kXhfOr9e0uu35WVYTnAue0D7aREfqUEpr1q9KZJXkFkLxSmWF67VVL4/7z2WnfgpuN5xBR+Fljdi96f2m+66JJPlVmHXTcO7roBPnd+KBFc3M8o8RKJmeyKP6cKcasKVSj3n5SjdGa9wkyjEfM X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: To prevent erros from occurring when the src string is longer than the dst string in strcpy(), we should use strscpy() instead. This approach also facilitates future extensions to the task comm. Signed-off-by: Yafang Shao Acked-by: Daniel Vetter Cc: Maarten Lankhorst Cc: Maxime Ripard Cc: Thomas Zimmermann Cc: David Airlie --- drivers/gpu/drm/drm_framebuffer.c | 2 +- drivers/gpu/drm/i915/i915_gpu_error.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/gpu/drm/drm_framebuffer.c b/drivers/gpu/drm/drm_framebuffer.c index 888aadb6a4ac..71bf8997eddf 100644 --- a/drivers/gpu/drm/drm_framebuffer.c +++ b/drivers/gpu/drm/drm_framebuffer.c @@ -868,7 +868,7 @@ int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb, INIT_LIST_HEAD(&fb->filp_head); fb->funcs = funcs; - strcpy(fb->comm, current->comm); + strscpy(fb->comm, current->comm, sizeof(fb->comm)); ret = __drm_mode_object_add(dev, &fb->base, DRM_MODE_OBJECT_FB, false, drm_framebuffer_free); diff --git a/drivers/gpu/drm/i915/i915_gpu_error.c b/drivers/gpu/drm/i915/i915_gpu_error.c index 625b3c024540..97424a53bf9e 100644 --- a/drivers/gpu/drm/i915/i915_gpu_error.c +++ b/drivers/gpu/drm/i915/i915_gpu_error.c @@ -1411,7 +1411,7 @@ static bool record_context(struct i915_gem_context_coredump *e, rcu_read_lock(); task = pid_task(ctx->pid, PIDTYPE_PID); if (task) { - strcpy(e->comm, task->comm); + strscpy(e->comm, task->comm, sizeof(e->comm)); e->pid = task->pid; } rcu_read_unlock();