From patchwork Wed Aug 28 03:03:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13780361 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 5A051C5474B for ; Wed, 28 Aug 2024 03:03:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BDB736B0085; Tue, 27 Aug 2024 23:03:48 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B8CC56B0088; Tue, 27 Aug 2024 23:03:48 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A2C0C6B0089; Tue, 27 Aug 2024 23:03:48 -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 81D006B0085 for ; Tue, 27 Aug 2024 23:03:48 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 35D0D161C04 for ; Wed, 28 Aug 2024 03:03:48 +0000 (UTC) X-FDA: 82500159336.14.A526C03 Received: from mail-pj1-f47.google.com (mail-pj1-f47.google.com [209.85.216.47]) by imf15.hostedemail.com (Postfix) with ESMTP id 466E9A0012 for ; Wed, 28 Aug 2024 03:03:46 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=ESccrBcF; spf=pass (imf15.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.216.47 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=1724814157; 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=5dwC82d7KQL5pIEF6T6/bNHtBQAMogTq8DRZCJ210sw=; b=ZEoC7t7iqjVdMgTIpiIOvI9NNAwIZwubAiRUPn7IbM4Ygkmq7RkQeA/Yj9CPKf/kief1xZ c32+DO6sUQJC8VWdwizhjyrqfCCr33r1hj+RXWZhknsqonlcdTMcjR+wWhWqChU3zeikM9 /DpWgCA/NvBadnjDw9yw9kaIwn0S8oA= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=ESccrBcF; spf=pass (imf15.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.216.47 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=1724814157; a=rsa-sha256; cv=none; b=VyMeEfWjCw4EWOP+NPMVU0/q30Wn8GyPs0Eg1yJqwzMx5YNotxT6wDnVBgFlYuOwRUDSLC CGxD7KQRvkYViZQOUgr7sfdoi4u6opsU5RSQgvo7161IXzPecCk4iaLv0Glfj30Yd1bF8n DzFtTFhtRyybsveA+tX0Vt1PJWbmGfI= Received: by mail-pj1-f47.google.com with SMTP id 98e67ed59e1d1-2d3c0d587e4so4422115a91.2 for ; Tue, 27 Aug 2024 20:03:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1724814225; x=1725419025; 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=5dwC82d7KQL5pIEF6T6/bNHtBQAMogTq8DRZCJ210sw=; b=ESccrBcFEqusxKKnJZJpCXzjNxttA03uTQZWwCaHA+af9BpTC2KCYvUAnA0EcC3b5k dsDoan/QTu1sgou55TMBE/vFgn8pcEn7x6FS9+Vd7H4829kAufmS0hyvRFx4O0uecmq6 cA1pY3Et5KR6TwQpMkVxnOS9kBEUc1Ipdl+mmEX6+rMGj5EmL88h52cCnWxFvuRFVKn0 uZ/YZw2xDOHJQhIpUSg0viouHAKN4SxYkb+RG44YS8m7R/jDKRhtkC42J6H5lbehu6BP uS7qagEA5Uz+TvqdbawzG0Yyf5W5ukmD5fdkKL3ayPw0mPBIjPSImZxwlSEM3+2yDVwS XEBA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724814225; x=1725419025; 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=5dwC82d7KQL5pIEF6T6/bNHtBQAMogTq8DRZCJ210sw=; b=KrtOuBl+Oae00kzl11bYyktdFhThL9p/WHfrc2g027cipEdWhAvhraNCnJM2X8a6We Gy+vf0c6KAwGujRu+/Z+Xs3OpivqOvN1TUdBNgm2wfxkF0JeE0XeEpwa5acuEljCjFZz jXZOxfSgYhoy3hwgWZ4wlquxQPBI13nuwHxP+RPYe4nrVbn1sVOhi9SJURjF71YhsR8M /wiyui8nHcG5Ll4Hd8TlVRO/dWRpH46+5KV+kYcQgZg0nBCZauUOowNiOPYoQ3V6Tl1d 9fUhWLsiyIifihSJuSftsgHoV5k02/D27jBVFXl3WUS2N1tPWScyNr+/ezAmmUQJNX6/ OBQQ== X-Forwarded-Encrypted: i=1; AJvYcCU8eyUXkfA8m64/pqTJBTPgqDND7zoGjRV8PpehP2PxdJfWujiDqZPiUBhvUzl5h+pSGE0rzDUZPA==@kvack.org X-Gm-Message-State: AOJu0Yw2fXW1VbaHsFtSvilAjJBpju77sQgEV41qlP7xZUaD+bp/7wT+ ZWN5gUVawM6MVl3LUKf2xgH6o8yAg8OxHTj8rOpdd9q1GwKXYhSd X-Google-Smtp-Source: AGHT+IEc8gy1oBP9Tt27kWFVV2/sdUfpEnu0CJVUYvciPK7kLGNgdrFqezJmS8xsximWyUWY+TcSOg== X-Received: by 2002:a17:90a:d490:b0:2ca:7636:2217 with SMTP id 98e67ed59e1d1-2d8440a87edmr695434a91.2.1724814224691; Tue, 27 Aug 2024 20:03:44 -0700 (PDT) Received: from localhost.localdomain ([39.144.104.43]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2d8445db8f6sm317977a91.1.2024.08.27.20.03.36 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 27 Aug 2024 20:03:44 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, alx@kernel.org, justinstitt@google.com, ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, catalin.marinas@arm.com, penguin-kernel@i-love.sakura.ne.jp, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, audit@vger.kernel.org, linux-security-module@vger.kernel.org, selinux@vger.kernel.org, bpf@vger.kernel.org, netdev@vger.kernel.org, dri-devel@lists.freedesktop.org, Yafang Shao , Alexander Viro , Christian Brauner , Jan Kara , Kees Cook , Matus Jokay , "Serge E. Hallyn" Subject: [PATCH v8 1/8] Get rid of __get_task_comm() Date: Wed, 28 Aug 2024 11:03:14 +0800 Message-Id: <20240828030321.20688-2-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240828030321.20688-1-laoar.shao@gmail.com> References: <20240828030321.20688-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Stat-Signature: 1jyr4h95k6fbce4fd9xhofh5g6gmwoyq X-Rspam-User: X-Rspamd-Queue-Id: 466E9A0012 X-Rspamd-Server: rspam02 X-HE-Tag: 1724814226-343525 X-HE-Meta: U2FsdGVkX1+zVs8YZoSJf5YmJu4DjBgAHSFT3Wqr3KEmMAcSAM3441rnZd57p6y6UurZu8AGtZtQpHWXZAMjBJZ9tA3Tuj2PpdEy1mfcCeXc4BkgH5fwLzSWFUmMKuXnUvxR/wcuOVLEphcTb28rzsIcX4k18aSbT4K5MLILABkI9JPI0anb/JvWTLKuuUCD4nmdy0sX3rq1E08ObKlQ152Q2CFcoqA3agTcLGKYoETty82MqmECC1YHnp74EvAS5kgbzM4MHpUZG5JZ4KTrXfjR9HSWsv+vWNGq4epJr5t3IH5TEP4bclkcHpBDtDFdgplONG9Wq/+DYiiMzeaXRQ85s4uFMnrGFKtp0tbDyEvD2a/kF6QyLi1CXUbGMyHWMRYVD2wXIp44zL1InTJmvK/Qr89SW89ziIkpuxfrbpEDAf/+TSWZYAWEgoS4CfiJNrKuPLE1P7KAe73eDd6wx1qsaq7Y9gOwimoXbcSXo5sBihNBEWJh++JVsQRer4jyvQhjxXBAg0R2bXaVi0GTXMQeUDJes0pOyLDuRTFt292Op7AF110lVSe3qisFERQo1W4ctymyDA3HxwKhROHKBAPv8HdyHm+dwueRXuKmMsL+qdcf4nQ3x1Z1xsuK/FTigkD+w5+2gf2+RxGYAKsj0JvCi3HAbJ52x3E+AV9Zaoxcn6K5U0FtgQiUUvKVj9wPDpM/Wf4F04HJdDVIvHRrzet+rolV9IDgINshixi1ZVZjB2CcNiX8IOa/r21boKknmxnvMWoPhNE2GpIju2mHAcYzyaiQ7hFPEVu7noQ1BJjCH/2ZcJ54gO5rT4kFvb0StkQt8xyqqFJC8lD/T+gb3a1uFTI9avTIAJwbCwlMCvBuatIMHnDDoXoPOyM9bzU6XJ1fToQj9YUcqju4IMawmSJr4XuSbvx1jI1m8oSlvpC4PR2eK4Rt1qJjGm/flQQiGcaPMQ3gIZlvvMuNNGI 7bDdBk+z Rd4jU2yDZIm8JkxgtSEMNOIbP9D4Xgqk6X7l3TdGhO/+wnkdnn7X18m2Qq/0JRyX4GkskypApxSEB/Pk8O2TqeF46kQnvXd+aUq4oS/4F9YQCpcRkJ3Beo8dVUecsHuG5Q/MDrdampwu+PI94HB+TbL+26/QiAw8azQOM7xWL6hvRg8vhaXwFcEeazNGPcy0llFGgB7kkkHHFTPA7gO6r0SpTWInOhADjqKFCDbQnGB/gslBiB3RU5hf1bwjgd6jU3tZUKj/uj8Wh6BI7h8NKButl2vwmLnIQGLZktO64lW6DNhYRPPrtaQT+TnjZMXtOtAE1k2n63Rurx1cx7Z2YkOytINE9Tjz9UMzFeiBTNeXXSVZswxGIU6MZKDzpqAKB19EhlbDwSnIQUT4PgA5wG9VcVslTlrQa8PBeQvvS+2BdLt66Di24484bJITqUFU+Z6zBVQuPUarHauHaa2VoV4eOfdjZbUrbUVTnQxucU/yHR6SmN4H2ugVFGw8Grs/7SGXAz04Lq9/cSf5DKfzzHIGvF1mNWo1aq0/tMIaVT0vWinQAn3/78iFM/j+lInRuAOaeFcj9MKsZGyxm1mue5Nsi/OC1IbGz8S0NwXoZ+wJwz83oWKIKa07iemOhk8d+DKu6e/VfUWBXKldGAcK9j8m/Dh4rszJ//2rCCv6oe4uQVhq+CDXGRcQNAirDPAChSS1FSF60JkuhPIEnhT7g99qqUHITSfdRZfLhKXixp2JAr7+XchrpBg89lwIl3zduvq7jNBIUbCrg1vHz9IbdpZODfXZ9GjaLvsRD2Jj2PBSuKplmf1H96rG01sL20w0CkYnXY1yhp/wJp58rVhMx7xbN1j4AU2XwualLA4uMzD/29toy0wtChR9HZ+r6D/Ba+g4k 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 | 32 ++++++++++++++++++++++++++------ kernel/kthread.c | 2 +- 4 files changed, 28 insertions(+), 18 deletions(-) diff --git a/fs/exec.c b/fs/exec.c index 50e76cc633c4..8a23171bc3c3 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 f8d150343d42..c40b95a79d80 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -1096,9 +1096,12 @@ 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 and + * zero-padded + * - task_lock() to ensure the operation is atomic and the name is + * fully updated. */ char comm[TASK_COMM_LEN]; @@ -1914,10 +1917,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 and zero-padded. 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 Wed Aug 28 03:03: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: 13780362 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 E7F36C54749 for ; Wed, 28 Aug 2024 03:03:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 80A876B0089; Tue, 27 Aug 2024 23:03:56 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7BB036B008A; Tue, 27 Aug 2024 23:03:56 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 65B916B008C; Tue, 27 Aug 2024 23:03:56 -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 44FAE6B0089 for ; Tue, 27 Aug 2024 23:03:56 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id C9D7F41C34 for ; Wed, 28 Aug 2024 03:03:55 +0000 (UTC) X-FDA: 82500159630.14.2EC1C02 Received: from mail-pj1-f52.google.com (mail-pj1-f52.google.com [209.85.216.52]) by imf01.hostedemail.com (Postfix) with ESMTP id DFF9840008 for ; Wed, 28 Aug 2024 03:03:53 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="kt/MiHTr"; spf=pass (imf01.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.216.52 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=1724814189; a=rsa-sha256; cv=none; b=YNmBj52OmFy4AYwLsKe++xyBrWiVco7fMd/DlnIVYwZvXF2zIYr5yOV7IyHWKgH7wFoQGc 2BQjwohtayIk+p8O0OD9FwkSbT5LOyVlqKFWrm1XjLV+qfOCVZev0tElmeHhXk2Eqw15yH SKwW6j/W7alLWBqY4JITGy0v3HNQTSo= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="kt/MiHTr"; spf=pass (imf01.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.216.52 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=1724814189; 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=+RcIZVYUDMrvydSLTObUn9UWjAdkDM4CrmA64YTz3G4=; b=bAvAFcXqwploidrGPjM3yjKHBUq1Id14cj7qHNDBvfYzqh8d48JKgy/vy5nftMr2OmMX+l /xgitCq/KURktMa0by1u2yaCuFyRMob8NO01hpSLU1e3rt/Mrwis0KwP/2M5v0+L7RxrEv YdN4H3c6cnAx/NtDMpEfFvFgT2xfmTQ= Received: by mail-pj1-f52.google.com with SMTP id 98e67ed59e1d1-2d3ec4bacc5so121223a91.1 for ; Tue, 27 Aug 2024 20:03:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1724814233; x=1725419033; 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=+RcIZVYUDMrvydSLTObUn9UWjAdkDM4CrmA64YTz3G4=; b=kt/MiHTr6JssVuwpy9NTa+CyfZrQBTEpfYQSNuPeIH6Mh3JH+THEISnEHZl3Fhqiq3 W2CTrH4jVBAf2xKbBdSwQ7QMWh4ugybWvQIw6fX2mLjoA4nQ9xnxpYj34K2KK/9CfCst 7TOYq4fH6897OQligXQ90nK2dvuh5dfrAlAXbpIv+W/4bx9JcDLg3Sr4SzdnscJ3AtdO TLt9at9PeUJd+yQCgvh/X1qVi1pQ6Ad74COWV6dXQiUAWeZw746bQ/9FY8uWR+y2RDA3 ec7duagmeGFWvHxq3ay5hAkMMJK2UHlPsLfdZ+p2tMu5jdeeDw/9bsVFFHz63NTpI0SJ DgQQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724814233; x=1725419033; 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=+RcIZVYUDMrvydSLTObUn9UWjAdkDM4CrmA64YTz3G4=; b=NYGCm035T1kZMWTb24zJySm9SIv4ClAaW2LKodQmiQvjPaDn1qFbLYnjxPqCGJclDd LonuTFQYpGZOrKhpx97/RAGGVRuTUDvWGMzTmKtVpaOHTzdiAU5Wj06Pt9jPD6UALq+B HWOp9C6cKedcuI/mK8MEtrCS6Gs3/OR0A60T+iyw7xIwLOR+GRsnBCi68h7CLYE7qWDS bHtu0RpxVt4mrGj3uxWEHrf+K3i8tkeBwqfRpYt++C9d6s9MNqfAef0oC3/qzUUHrjfi 8jMxV1/gMBMYghzulmR5vu37Li/F0K9OAkkMrPa9IUcz5ncUIkNW9cXbHOarqK29360j PqDw== X-Forwarded-Encrypted: i=1; AJvYcCWyxhKJvZjxn2JfuYfUIaPQsDKi9h+npyDR+Rwqgd+AdWyjya0YVCL7voGhIZF1KquCdMOZwYmNGg==@kvack.org X-Gm-Message-State: AOJu0YzV7ptCPD4hx4kfcMrhrUN3W1n6hFGur5DuD52Oy3IE36b09sA/ viV4NKrD/wyMXw01Kqj6VXkeDqtBIGDqi6OW2iXu3Nu6jvkDaSMS X-Google-Smtp-Source: AGHT+IGFzzoi8lpwMf6Iv4C1DEmxZSABK+5V2UtAilXgBtIiNQOvP4ALnfkUbY0O1vyUXWVYARLCDw== X-Received: by 2002:a17:90b:d83:b0:2d4:91c:8882 with SMTP id 98e67ed59e1d1-2d843c839efmr1207327a91.11.1724814232595; Tue, 27 Aug 2024 20:03:52 -0700 (PDT) Received: from localhost.localdomain ([39.144.104.43]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2d8445db8f6sm317977a91.1.2024.08.27.20.03.45 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 27 Aug 2024 20:03:51 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, alx@kernel.org, justinstitt@google.com, 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 v8 2/8] auditsc: Replace memcpy() with strscpy() Date: Wed, 28 Aug 2024 11:03:15 +0800 Message-Id: <20240828030321.20688-3-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240828030321.20688-1-laoar.shao@gmail.com> References: <20240828030321.20688-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Stat-Signature: dzgio59eq5entpfxz7xhf6ibnwg14cox X-Rspamd-Queue-Id: DFF9840008 X-Rspam-User: X-Rspamd-Server: rspam10 X-HE-Tag: 1724814233-589490 X-HE-Meta: U2FsdGVkX1+H6Kml3uluIL2po771jUruaPHHZSQLYestqbUkRBT+dAqzBVOyhVUNDkfdh9du0X2RMt7PyJXCU2EbcgGC8KQRuVA2vX6jiN2h9oXYXl4MFUrq29CMUqrJzgTAQUHNwAF9SSbnhGrAoxtchafY1hdIT5fZc6syKwilERi3f155QT7StGE6gzCz8kPt8VeLyyOU82Px+ZBeRVY3ddQrW88b7cHLo0/5+wwMuI5oojyTVqhUCdps0SPba8pPPoELOD+0H54f+602ueghP86OIWS1KLh2QKrIUuL7Q1TWLYGoGj1c/6XHi9Dpy04uDG0J3JCumc+oeM2gPgse9+gzm89dVvUc4c2qQnHaoN0uz1og/S6HlRE3XD7zj1mHi4BsLEcNViVPsUbXYWeBTWLyL/Np6E65gMD8jDEhC8YtPc/uQ98sf0qLitcrR4sJQORHwlrkGr+7yxDUxS3xB1VM1XL2ZPyr8pD2KeGwe0ddXoMaXzXVYrHLzH8shr3VFQqYj3TjH0XZMZNoZrPfg/N/Yq1vdfDJYxmylNCMEnevsLOHsmuAzRJOtTeSL5vn7/OqQ0VdRbfVHj9+Wyn7vufwiRWsDaXoHx28ONX4UYF2C3R+3y486m5Lbi4kw+DuBhHp+hE8A08570k5Tsf4x50IiHUG2K3obMfoMasyq4SgiGK7vDCsD6wBZ+CQTJqBtoOHPupIPTlsarldUIOnZNa2lHB0+ZY5/Psa02R3t/Wlt8PMa7NbTvrX8f0MRA8GkYUTGOtseq0ILWilazVmqzq0FxsZpvFHk979nt+VWWw6Seo76L5odWXnQeMhvUn5qLr68LR15bi5SMeCwJ6JGNFYZo0qYXYz3sz/H26v6p/FqmZxPSJYhMSLAJ121PfD2QFKRZ/XX8Dt4FXLCrRum36wDGGH1VSMjfv1j2peChJOCwEi070I4UbkJK70OGxsZI35ksGvieuYmjo Un6WNPoS QinaKiXZ+07h6FW8E8ELTOD8piHQdC/ygOxBuQsLJKtd7qNrhMvU3JKwrVn63s1hqh8rKlh5ktd2gBTZU1Cbpo8KRzeCJaF0eo3EshEbJZXPMY6i7QzjMaAj8a6pn8lZ6sU8bHqeDUe2aqSzm5Zkq3s66HoylJnPFg4y2zazAazcpAJT//mTQUcl4tHGmCW8eH/slytfMqX+Gj/kqZBQ+3qcENqYnU2bpDzjxMxfKUtFF2K4hsiG+Y6M1TdMQ8LTug7gbsM4b3AcTYaySbqN5yILLPglhZ4Fz9FRbrGvoL2JQ9POW5jeCp7Kzd8K0IYLQWsswQ4WLCaGTN2/Bf2x/Jpa9+k603LS6JxcPuBmlLANLHTqxX5TMZ1G7VGHOghX57FHykqdsxsZDEXlzG+WutfYwHtSV0jvoDPL5hXhSNTtpeDF3lT+3wftkdKXsTDTrsRwqBXwCGhAooVwgcxbrFjUdsBmqmG//bZS7rBtWp0zI4MXKUUVORRfkYNEOFY61VJzDKXusxHQZLPFBmyHJvjx8W43mRPyDRV02AzHQ0lxnsoRVTQTy/Xyth9e2OdBSLukdgAyGbOVqTjc46/1TaFW+kqhuxEGHrrQiJySP69rKbftkCOQ0/m+Fw7XRekNGcy3N 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 Reviewed-by: Justin Stitt --- kernel/auditsc.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/kernel/auditsc.c b/kernel/auditsc.c index 6f0d6fb6523f..e4ef5e57dde9 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); } /** @@ -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); 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); axp->pid_count++; return 0; From patchwork Wed Aug 28 03:03: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: 13780363 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 2EEA7C54749 for ; Wed, 28 Aug 2024 03:04:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B3F4D6B008C; Tue, 27 Aug 2024 23:04:04 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AEF446B0092; Tue, 27 Aug 2024 23:04:04 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 96AF26B0093; Tue, 27 Aug 2024 23:04:04 -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 770296B008C for ; Tue, 27 Aug 2024 23:04:04 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 223BEA1C6A for ; Wed, 28 Aug 2024 03:04:04 +0000 (UTC) X-FDA: 82500160008.22.EA549E1 Received: from mail-pg1-f172.google.com (mail-pg1-f172.google.com [209.85.215.172]) by imf13.hostedemail.com (Postfix) with ESMTP id 48CEB20008 for ; Wed, 28 Aug 2024 03:04:01 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=KxmUC5Dn; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf13.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.215.172 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1724814221; a=rsa-sha256; cv=none; b=ba75/U7XUGqhKQkArjqsXauka7mjDh9E+rbCjT8l2nYcw22TkWhzVjV89c8VfbNTAV39cr i9ldFUKKEIpzyO1Hgz0F2YH1CG67zUqvC4xMD5TjPYq9gmucgW5UVxX3XJGLgc3OxL3uvI OtDY7XGD+u3/JJ8z475i905ePjOkoOM= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=KxmUC5Dn; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf13.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.215.172 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=1724814221; 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=mIIMsSPqVp5AusgTbRbdrgF3qEr8ccJBENe3BbQ0UBw=; b=eqVZbm0xWLEe3RDkJSmdHA8I3WIsCx2GF8exyOjBWDHCIrzggS/YTYXu568piqk7vo4y+8 lQwxs4aWKWsjVcXPMpH0CwroEWPDNl90nzyD3f4jwzWILC2cJ+xNo+sabjtWE0Z0Y2XA9a 9i6x4GuSsSr2pdpLEIxrpU188GeSEQs= Received: by mail-pg1-f172.google.com with SMTP id 41be03b00d2f7-7c6b4222fe3so3832188a12.3 for ; Tue, 27 Aug 2024 20:04:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1724814240; x=1725419040; 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=mIIMsSPqVp5AusgTbRbdrgF3qEr8ccJBENe3BbQ0UBw=; b=KxmUC5Dn4MqF8EYaOUfL/dnJfsUlz8bphkdGBmsguUKfbowe0R1992gy/kxBEFAXwk CzA5fsRhrKuDWqumdnh2U3bL3X56cmHaZLqntVijaacRWG4cYgWdqLj76lRDdd518Dh7 P7BSfQBDYKOK/re1qJkkjeAlffiWbqiVmDIKDXlC8B5BPtLBRe2AcpxS15fpYqTveExu 4W2Z2Lm+qilwJ55CNJVHzkpd3gAOsIVSiIwBLg3C3cHf03Hu4WL794HmYpu4nBkKYVwv EvHU29BZZe/rNTOEoIQop4SFJTuEDisXRBowViqgGKTJ97v14VfQKVeD3+E/dmy+Luc2 IAkA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724814240; x=1725419040; 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=mIIMsSPqVp5AusgTbRbdrgF3qEr8ccJBENe3BbQ0UBw=; b=M/rGSA89ZVydBs13r5j/lCyD2LY2GUfPh2dH9kbK5Dk6oDV44+xUZWJ74VHFQByJPO F+V0hXUaWwO2S26NQKqdkXBigovWHxkSs7NQfAyOLqEOtmuAMujzFhMixJuCLJ9soNcy zRQ9tOzQBwBZdiLH6ObqNG/SECmC4WZU1SDAziBGg8+bKvWOOFnfvlzgnC486xrOSFyP Dwr7q8u++EGwyyyh0XqFHjJKH4JdHKnmhaDNfxbY0hU52AEvkZ6oy9CF6stw/9XB4I6E hFhTfQDrbnKejMCDjhZlVwOlIkLdgUmjSm/mSI2B7UIZdz0zVP0h7K5T/XI4hrLVB729 bYjQ== X-Forwarded-Encrypted: i=1; AJvYcCWvf9VKKjm7yB6mZG7C3ORM01zWTkO+fPG/0tHClZ6Uu3ouE6sd8QXv61P0sbmaivG7ncqASx345w==@kvack.org X-Gm-Message-State: AOJu0YymVaF6EeozuclhPZljk9xbXUF1Z7bEdnPAmfCdrgsA3qFUx7KZ xChu0GU77qFCbsiYDQOK1rVOdDB1ra97CxpLYcyLqvzJ2/75P2wt X-Google-Smtp-Source: AGHT+IFERm607QheW54BofDpcXniBv+8JM5KHrTSMUx7woXR1463qkdvSKbT6nvldL/lDZfqmA0dvg== X-Received: by 2002:a05:6a20:43a5:b0:1c4:c7ae:ecea with SMTP id adf61e73a8af0-1ccd286f499mr543329637.11.1724814239886; Tue, 27 Aug 2024 20:03:59 -0700 (PDT) Received: from localhost.localdomain ([39.144.104.43]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2d8445db8f6sm317977a91.1.2024.08.27.20.03.53 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 27 Aug 2024 20:03:59 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, alx@kernel.org, justinstitt@google.com, 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 v8 3/8] security: Replace memcpy() with get_task_comm() Date: Wed, 28 Aug 2024 11:03:16 +0800 Message-Id: <20240828030321.20688-4-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240828030321.20688-1-laoar.shao@gmail.com> References: <20240828030321.20688-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Queue-Id: 48CEB20008 X-Rspamd-Server: rspam01 X-Stat-Signature: tmm7pejs51jt119r7wqy89anfogkn7ne X-HE-Tag: 1724814241-31900 X-HE-Meta: U2FsdGVkX1+vaEGdMP4v11ESr2gsBNWu3e/cVDRvq8K/P4An9X11tQwgoSMd3w7lk+xVCLW0JW3gzjGIeGoUwMclIIZ6eA5XX/HssodIP5f8SKSgZ/XJaHVyByhkhAjP7XKKTIZZprdlSkrhcPGMoAmEZ+cL9HFxnJdNLus59MOM2T4HogeB0xgzBcr2HWUbsvO8ZZXRBPJYgPS8W+EihlW7cBWzCSAmbBcfoaTyZhJhwzgMvsQ+whhLV0uo3hebXE2ngkY81aMGEEVBgKJ9CyJPPrPpiUaW4Mij24Mhai/8LQWhgGJcLFOg/Fm4ZFi+OKQKdkNSi7bKjy0nUV58y3ecTLGT5xzAWTZM2gS4sjqRfMbsm+XMZ8anATa23s9wpjpLJdhAtinOfP1/4FMVF6IzuTt7aBNt/hFaKR+HjSM6GcXshB93HRjdEpCIhRRhkhmUNC3FxH300VLEbLYUJSAMDpNSAUlhj0Th/I66Fn8jf/Q2kdd8glBtcHF5sbwFEHQUhsPmD02exp2IOVBzpOIvj+wPvJxcAQesUwMZQMqy9TXwz0k8lwYoX5DwyKuCCH2ckq3qfeszzHOs/YaxKCW1baz4F/cjcYBxG0hJUdtCCX6oO5HMrr2+Atsx/BcFiG/GiY070sprSAL2bhLkKIIhdJNgkTDVwGWHO7k5pH+EaZoGKIw25pxvtbwO6WHDH4evlt43of3MQZwVIy3sy9E+DhXc1kNi/XQnAhYs9Lt/ayTJYne4dRUt1lauovXfFiH+KIM9xkECOUYt78vw5+Pvq5ChK+zgNNhMqSa/8VmGpCfVEnb/l+CsMxs60zAnCCWbr2yzbscEOQwgbzb2I6BL3PS75rLYesIuccVLZU/S6USMuFML6LFxq3YLVJ1jb4nMhVwOPmArexd2gUTHfDxtlZlH2DpQU8SxeG/n3wM8idgMbB422723Igi+RRrT81pB9vL2mIoL50dRK5l eNyu8h4F 48oNLkmy2B40tVmluRkPWTdTPtiNnRfQtxTOCrb38sKxNpYvNS7qyGyMOl9zuKLmXmkSWkZLtZ4jdGt5Q6DfKSrPMJd6U/cVp0Leu2Z/+r9RU+jHUvzz71++yfnqO3ySuEvhcT4FLTdBRqyFQCvZgMIrjnnkGIE4l/X0/+FYw4+2xRxfWoRpCLS+2qkS8gZQpBUIvN3ZjPIwgDubcpbrRf7C329P5dRM7fngQmVX23OIk3y7ISvKfaDyS83Vp4H8KMdE/NvR2qRI/o0eU6WlnKmvK+XFbwmOxvVk/mvXRLgxYaGgwynclpDKnyblT9DlHVFPoBS6P/deOX0NSs5xTBWolMMjfOKArNVAA0IqqmwHki/cG7u6Txy7CbM4VoQ2WEuDpcqPfi7ltZaYRj/YZuRJVVsAdEZJMCnxkhky883bFrfsVudaLM5Z5oYI8+VZZxWwkOpQyxcMffTh2M4DVu7LhU5rSD+QzDVsFpDWU8Wwr4Tk1UEU3PI8B4M6LfWZINel2/TAvJe0A47JmTAlIDD57TrXLll63IWvV+6GZzrEoLru16jIZtGnALbDgVdDMGhoAn8HiphQ9f6yhGOmXgTZmGmrjaEcWEEUxF3IT6P27QWsfB+rhovbS8Hx2z9qQHG+vn/snclDHAorQwyGJrFiBhWCi5BVUd4tvhQEAOGQGqNUYMjRlXZJWDlN6qGvNRvgMbFCqrgqnSSZsnabdj0lteQVz74dg8fl5+le1BDtosO7NaMoo2KbmLwPAwtjG4FUfr7E6VvcPsgYx/1aWWWSaCPhccFmSpYTBeKmem8E6QuwGoG5P1Dv7Q77yWMyVJgYbdHT6hqr/htjGVwsmcZMNDytxbNjOyXpBK1B4Mc/RpmY= 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..c9b05be27ddb 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); pr_err("SELinux: %s (%d) set checkreqprot to 1. This is no longer supported.\n", comm, current->pid); } From patchwork Wed Aug 28 03:03: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: 13780364 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 51480C5474A for ; Wed, 28 Aug 2024 03:04:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D3C246B0093; Tue, 27 Aug 2024 23:04:09 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CEAFB6B0095; Tue, 27 Aug 2024 23:04:09 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B8C6F6B0096; Tue, 27 Aug 2024 23:04:09 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 9864D6B0093 for ; Tue, 27 Aug 2024 23:04:09 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 47867AABC4 for ; Wed, 28 Aug 2024 03:04:09 +0000 (UTC) X-FDA: 82500160218.19.6A2D623 Received: from mail-pg1-f169.google.com (mail-pg1-f169.google.com [209.85.215.169]) by imf29.hostedemail.com (Postfix) with ESMTP id 4B9C5120009 for ; Wed, 28 Aug 2024 03:04:07 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=AnS8Nsc6; spf=pass (imf29.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.215.169 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=1724814159; 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=vpiNNmhZjOZpEcQiz6887y+/+b9ZWD+7jKScF/e6RUNZc2hC4T0Xr2lf/GwIC1jdfPPqd6 t8n7s1reuau70tYuPXgsCq+5D3RWFxHA6TSRgYlc0CWgCwrqwksx/ol/324l8OYq7ezhTC HXOVT0L2Fq58pKaw7JhBUTvaaDgzIvo= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1724814159; a=rsa-sha256; cv=none; b=B1YHwuC60ukwSjeS2EB+XnvznwpYKqri8e4cuV0nLUCE1mh4Hwlmp0lG+sMp2YsRSGqDF+ 8Egr7hid5wJvPqTNizyq+TzdlOBxzeLZrvsrXfjFL0F+8HHB+RvbB5UM/PLBWo9ecsKvTk Bszvn14V+Cw+S1eXuRIs/znLm5vHiCE= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=AnS8Nsc6; spf=pass (imf29.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.215.169 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pg1-f169.google.com with SMTP id 41be03b00d2f7-7cf5e179b68so124244a12.1 for ; Tue, 27 Aug 2024 20:04:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1724814246; x=1725419046; 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=AnS8Nsc6f1XjW1O0tA8USxF/ziGlhmwT5jxqnLgUmtryYneSahTfi56xHXdSq/dTqE eeR3zamZHX406XHlmMOk3sMZE9L79snkIRbZTwwVTv+yJ24D7x7XoCTdAeVSoFdNLGv7 PXOzzh5RfVFgJnV3zPYFfBrIbA/ybbcVebZWzvZk2ydz+0A12ZH4l9CViB4unIg6oHjz EI/wscbJN5doggqg5s0nW8MEJ41UunxGPmsHUNCxQfMBgHnY8pv2lNpNNw7JH1k8ewAS DW6s6Sj5T28BoMF7BbnDZe7scAUrl/6deQz7FD/4P+QryExcb9hyyC23lfuOMqQ9vGYC xZaQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724814246; x=1725419046; 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=NHHEOKuVjQCNqFTDTDwm89JoT00cGGYUoT6ULhK5xppZ7imMgXZKzXsnWDiXy6aEwW WKJtwo9j6t9Mnhc1PXn4CCI3NHH/Lf9BPLEFOM6/QMw06SAvzhrn5Xv9Q54f0JeNDplz pvMyf6cgqfArYS52msJB6USnbzDXcKCP2AIXvcHe0wcOIMQ+ZnwzsF7lZT1rbf4KCfyN 46YqKsBYVlcYeHi4kCdbiNEbOSybpt9iX4ApMzCpQ32ZX4qtGLgNV4TikeTZO4TCezCM v0qTJO/cdGirDCl/HKHrmhgvFdPe2qZ5NiTLspk2jAxB71v8MzxrsXmiQGa4PNMRjKEg J+/A== X-Forwarded-Encrypted: i=1; AJvYcCU/tuJQjEk+2fD14Ux9hwcsClhew2A2oWX17Yr9SR/NjxIzeFc8ozcq4m1gG92L0ytnE7viYWH/XQ==@kvack.org X-Gm-Message-State: AOJu0YzZiBuR5hJ8DSrMdMFMxjtWQHl4Pha32mC/SO1b/378zKv5DwDO Oie65vEe1fMlWJlY/947Jm56FxkCBxdw4GiE6uim957SRpbRAfMzp2tcIkEI X-Google-Smtp-Source: AGHT+IHxaydVqwv10Tfh2ak5vjo3hT38eMV2JYlayqlM6cQqHxMPAOmu6Mb/QjPRAxpmFPgYrqTvlA== X-Received: by 2002:a17:90a:304b:b0:2cf:f860:f13b with SMTP id 98e67ed59e1d1-2d843da1884mr1305336a91.17.1724814245904; Tue, 27 Aug 2024 20:04:05 -0700 (PDT) Received: from localhost.localdomain ([39.144.104.43]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2d8445db8f6sm317977a91.1.2024.08.27.20.04.00 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 27 Aug 2024 20:04:05 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, alx@kernel.org, justinstitt@google.com, 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 v8 4/8] bpftool: Ensure task comm is always NUL-terminated Date: Wed, 28 Aug 2024 11:03:17 +0800 Message-Id: <20240828030321.20688-5-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240828030321.20688-1-laoar.shao@gmail.com> References: <20240828030321.20688-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Stat-Signature: trbga8eazx4fsuqcfg13fndjx39yk5co X-Rspamd-Queue-Id: 4B9C5120009 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1724814247-662396 X-HE-Meta: U2FsdGVkX1/2uV6UPFtyAVUzZs0nMEiPJ3lqwk8cUNMdzl614RkOJ6JCvqw/RBO/ptaUKOq37OzeDSc1XGZc5LnY89veA5AFXjvmTFQD6WiRZ+FH0VC+UkIcSkAPLnkVf0j/t9I8UGSTy17uGkr5Av+hi8dqozqDdMrbj5gWo3HKT8P8nfgBOSfrCEcVGkvdgMdUx1G+lhe9SqBkHTPfgA8OEjV01M66Zto6LHbRyG6jfxjeWK3xSW81yQhm2nMGL/5plCsrTQAUMzzBoJYUG3s4zL+cLV+qpGrrGZCor9kwAkByG2Q+X8zz5fe2x47WhssjWXr1G1GBMO69TMpnro0YpBwrAPHxFZXqDFH+aj+ZYf+tCMFjsh6KAQUDnB7kJNc67fQxfIyEE7JA40EKuhqZ8qc/qmaharNSy3WDuguGQsANEaTKNeIHHEzklhdFwLYPzW4a+uwRHBv6iDB5bvC7yKkwSGPMgpBp7HJLVsnj53LEl/MOBKASRREXdW6RrgFTadqp0yty+0GqXGTUOk7b+hJj7uPPpuQtSi1Og/bK+SfRm5nAhofHH4+0kwAqaQyeJz6DCxSHZf5nk5kNFhcmu9NGUxzyL6KIzfLOMJQTO53yuT+Tao0WFHFqOE09KxHKE1qxKH0TEhyawPY19vtu08rceHitGAfyg83SKE1HmHuurb9W6ezBCrJ8uFfRVImf3MGZd+7p5PtNHXKHpFqsAQ1MnUYgWNADbBzoscgaI3nCzD3+E/SKKVSitttRxqdphzoHGyZIsetTmP89OW8a1/Z3vqdZm8yUzt5MUkwVEDCTXpwFwzSe33DO4pwxPTiZOQEseQ33o4d0+0n1Bjmthes07m9uQqWmUp4tTq1TnucDeO4p/y5OkkvhDy6YhZomwygTtCCz7XhOorfKC2ELlsthf0g8rDkuEl+Os2fwKg55x5ysZ0uLy51Sw3LmzrfWrKZoVHUo+CD4Viv P06sZI+m tYi9u3bwX9z4Jipqou1OD36XC101Fb/JfzsCld0w4K7V5mg2ZKgzrm+TyhptlNTc5+V1rlgtDWmeQ+UiQNSVkpY8xPLwWs+U9JmUF9V2dI1KWxJqKTdULEM9fp/YpQQSncRdFho2HuGgSV1gRhp0RvUJCapHxe+HIPKQGPHryAsXzjO6iX1b9pBRWotFbjsr5ZLqaJ3cHpXtnsAlPQBC0ULu8k4mNvAke6OQ2//eNMdQ8+I7AT4nIa2IAzBeSo6i3Auo4MuAfPiDJu3dkOIyOfuUcI2od8fpvJGWjvWgJVQCzZuR5IHpBLle/7rB/ps64iMA8+b+wj6nlWbJffcXivAbegxMMbjGsy+N1DnmTUc2LpB4syZFyckhLVWnuP01PRXQ9oNbph1SZmNTcNp1FCPNeaSTDK8WJqjQHcKcCzArWMHHsYiN+/u3Uof6Ag5B/wWNbdSTbG9UfiUcw5La1EojgoU72Avz2Ynv0c0N8RVmb39VhfNyhW8qEcC9gG/7a5BtzO9T6xDGFr1JRuJbM+RVr56hZQP0v4zWiElny1Hcwtsj5PWeDIPDge4080PIsc0qZ/eGJNUFnPSONs3OAucXFsmXsq2ap1sQFPfnHFDwQLZI= 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 Wed Aug 28 03:03:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13780365 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 1D696C54749 for ; Wed, 28 Aug 2024 03:04:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AA2E96B0096; Tue, 27 Aug 2024 23:04:17 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A53436B0098; Tue, 27 Aug 2024 23:04:17 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8F3F06B0099; Tue, 27 Aug 2024 23:04:17 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 6DA016B0096 for ; Tue, 27 Aug 2024 23:04:17 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 162EC417C3 for ; Wed, 28 Aug 2024 03:04:17 +0000 (UTC) X-FDA: 82500160554.29.293A06F Received: from mail-pj1-f52.google.com (mail-pj1-f52.google.com [209.85.216.52]) by imf28.hostedemail.com (Postfix) with ESMTP id 31350C000F for ; Wed, 28 Aug 2024 03:04:14 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=nWJijizF; spf=pass (imf28.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.216.52 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=1724814211; 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=N9R7Mhbk+Se4PIDhpm0/p6nrldMPUtb+oC6vNd91DMo=; b=a+KCTX1OgYxOyNTyuUH0dxGF379lFRAK+W30T6dx66zw0p3AqMqlC3FyRpcMr0kiCDOrXE dxrd1jmt1yn7EMNpOySS/ZmJpq+qywR60TsPo9CEoMer8i7Qiq68nF3mNKcGDrppivJPB1 3SaHNGltNXvBjw8SQWVLNE2j981gB+U= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=nWJijizF; spf=pass (imf28.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.216.52 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=1724814211; a=rsa-sha256; cv=none; b=zXJVAD+z4g2IOb5LN5cf3aDtcFUXDmqU4ktwuJbqYLn7zY4I7vTWpkC08KmLsrn8SS8YOL pnVLZc4EjB40vEwqRkr0UwkaTZGmsS3Ecy9JSmyC7ZZLiEgwaXIIeskihOC+/4g+BlPb/C +JVo06Mm02QI2m5DJ1nnqqmc69RH2QY= Received: by mail-pj1-f52.google.com with SMTP id 98e67ed59e1d1-2d439572aeaso125417a91.1 for ; Tue, 27 Aug 2024 20:04:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1724814254; x=1725419054; 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=N9R7Mhbk+Se4PIDhpm0/p6nrldMPUtb+oC6vNd91DMo=; b=nWJijizFIpeNW6buGLZ4BTUitwruLES2cpnYe7PNgpysZAJBaUtO8knFCHL19KNP9S N3gCZ36GWHWguIx7ZSehQJ9niqaL0qzjhmJJmZSqJ/gU7+5bm5ydNDgy+lcUYerWK3a5 ZqUeyJOJTXAXAU5X/co/QPQ3sEfwNSNUCpsavOYdV0GXilt456Ism8+Nh7kL5j5AO8LK BOTgf9B007CExhRJ6jzyipqqrbntIyXVS11tiXEp+XhnO+1snDhebay6VletHffUcUZg fB7ng9OSvqM23ZyBOBrn8gjn440Mt8fd1sJHdK/ttSwoFdLiW4gPdqz3VoVp0Szz12+p S5tg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724814254; x=1725419054; 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=N9R7Mhbk+Se4PIDhpm0/p6nrldMPUtb+oC6vNd91DMo=; b=EfZ3bR8cYT8LxehVcv2JO1Fj5eWZQBgH+V9xAjqxUBMiUdZFwkWcByCgXZC1ZorNdj wRNItoRp2u/YCKUbODy56MtmBKpybSSRX+a9cCAEiKCKiNXSZFACWM4/tSjr/jpKEUMF 5JZCoQaObT7TLTmYRrGBU/CrYG+5BMYNiPqge+ZVFbBuLWlGG/CZCiQBb4d4sQr+8J/4 5DK+LUsOulXIPFy3aHDUUBJClq4f3b81fujs7Tv1L53vxOxRunSImTH3hAzdhbq67q/R dCC8XHqPOL4SDqxersI8EANTGdno1j0GFBeWlwXaHRQhLB4tAAUWf+tRV3k6fxNCkBbv QJ2A== X-Forwarded-Encrypted: i=1; AJvYcCWZ6LwhjONa66giTTdCMkLHUWSXQ8BJy6IdAA8Br8tUH426roovKHS73UZdyyFJ2owG0Q8DzesWow==@kvack.org X-Gm-Message-State: AOJu0YytVMz7K5HHH7A6r4/QYOe71LfayPjOumf3jQJ4eR5qzOXU27vz GOWJUrJmz/jYUJ4u1FkWkj/DEfRU111+SdXN8ZsJDQ7VfNSQtfNb X-Google-Smtp-Source: AGHT+IGCtw+zPOY+Vs3gCNaMxterxPo1VEjCCML55T5/9brSfwlu9kiROVQ6nSIbi2XhsLWhhe27AA== X-Received: by 2002:a17:90b:3b49:b0:2d3:d79f:e8b7 with SMTP id 98e67ed59e1d1-2d843c16e68mr1209703a91.5.1724814253885; Tue, 27 Aug 2024 20:04:13 -0700 (PDT) Received: from localhost.localdomain ([39.144.104.43]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2d8445db8f6sm317977a91.1.2024.08.27.20.04.06 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 27 Aug 2024 20:04:13 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, alx@kernel.org, justinstitt@google.com, 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 v8 5/8] mm/util: Fix possible race condition in kstrdup() Date: Wed, 28 Aug 2024 11:03:18 +0800 Message-Id: <20240828030321.20688-6-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240828030321.20688-1-laoar.shao@gmail.com> References: <20240828030321.20688-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: 31350C000F X-Stat-Signature: sbby79ruiujn1t4a1yqqcm6k51iykdqe X-HE-Tag: 1724814254-311345 X-HE-Meta: U2FsdGVkX18NPBjYVaNwBb9C9bXgrKY64oEUzqWWIi/1BBhH5xEX9AD7r2QnhIeLKWf0Kk7QZVXXlDZUAHqhsDwQV4fiQLQtrXKBDdvnWIOyrSFihSIbO1GjnCW1WLkirRMlJ56T6/Bhd4zEXbTJgMQxfd60IgoPKNIhHsUxmebwFZ3bykoKUoSvVgpdx8L/aBF62HpXBXyN6R4EPl5BlH7+GKHEF3/Yfspl/TdTlP3VTVAyARJJcQEREhIvZbJY372bDgp848jD6MVbXqhUYNWYs7DvhaCzhhQb5XDMKnoT1CXetR5rtiFvrOYuXCTx8s48I9swc4FIfU5D5JOgVJUGopUElLC3sOlaUXDpYjlcqE0bLwQ36F8Atg5hovV/dM4jEMwSZspf6SzbQSbrl1CAUvitpzidzOdNvo0MUfavl7uozeFSA6lVXM8n2MwMjzGyof2DYV8LKUh4zFlQL8prWCr5wV1njkDYxn92G2HWWhMOFivelm/yrsVtQ/BmweIaUIJgnmIAxHaB/UJbmiQJMfbEPPJp7D3lkOG1eB/7eMM3yyg2JjLp4HnTDg1+O9jR1v9BQRun4uD3WDjrj2BJEf7RvoxTPNucKbH3/NROvzh5F01ticvE5v9ujm4qVIiCniXR27aDa9xYKG0YAuF3FURCdBxhgryA1Jt3LmcFB7/JMncNfDgJJ+qShXZEfNgfwnJqo4DMBMpeCDd+CK8R7HQHNP6HNzbexf+5lDGslySBwd7X8E58VFr7WyLmqszsynr9c+aHKCsZ2ziqKHu0x4QHLQ9m1MW4OAm0uUw8jsiUpKAQEU6G0L3OIjKUqTtg5mAEmUioahWUEDE64Lu/+xzGUmpZkJLK6dAimYKvr8iaZj0RdvYaJsnq4dDsH1s963lF+hcYujEa8GLNvml4suQKo5ngIY81piy0FaMTPGW1A1ZVdPb2mbyfdgCn1R+DSkbgUfEMc7wC+U8 DhKUQmlP tx4amqm+Rqc+gleZBxT79EDmybw55EJvieO5qzXSBfnBK+V190xx9vPqDXw+F0qejFZ3yi/FbqYrK2hFKWBFtNNiZxC686Kwlgc9BZjj0eXI9rKjsqrO+0fYjKT1Eo3S0mC+dAzaxFbueZZ+fAhqR8Xp+pOnNkhxLrrMVV8VtF8uqP/EiW/Y4AxElfTU7lAvORp1TwxB6YcoQVJ5RUicJAn4o9LjXF8Jnn5Ie19n/4SmsE6l/EGRe51KnUCtpz081LDDt53xRWOba2qcmsWjVA4bu9NLr7e5ehfeumi5UgGQMbL1kUhmRQ1Jei/wm+WDRPtTGXAjd+ONb0P/Yuy0nBhsUilIISK5XgNHW5mIdpx3kFNtprKt2OJsZPNBsg1QAIWJ6XB3Vdn3NSH/vCMUf5sy9VZqiFLpGPPsYSIKu6Jx+bdPZZp789VdymBcEFQWiyBSVTnpSIWDA0IKlg6zz6BtW7IrqE8abnJI1nrbrviy3jId//GH54ti77ahfzPLDeE23SqKqfmcDIhCu1VFEEH4iWFO/8NpWyBVOYgB01heLPdrzOCo2Wys6If1AdFabNwrc88tf/HNWaiRTgFHs36YTQAUXd9q+JMpB 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 Cc: Alejandro Colomar --- mm/util.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/mm/util.c b/mm/util.c index bd283e2132e0..9a77a347c385 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 NUL termimator. + */ + buf[len - 1] = '\0'; + } return buf; } EXPORT_SYMBOL(kstrdup); From patchwork Wed Aug 28 03:03:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13780366 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 AE6B6C5474B for ; Wed, 28 Aug 2024 03:04:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3ECD86B0099; Tue, 27 Aug 2024 23:04:27 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 34E3F6B009A; Tue, 27 Aug 2024 23:04:27 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1EF196B009B; Tue, 27 Aug 2024 23:04:27 -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 F3E206B0099 for ; Tue, 27 Aug 2024 23:04:26 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id ABFA581D35 for ; Wed, 28 Aug 2024 03:04:26 +0000 (UTC) X-FDA: 82500160932.01.0D2494E Received: from mail-pf1-f170.google.com (mail-pf1-f170.google.com [209.85.210.170]) by imf22.hostedemail.com (Postfix) with ESMTP id D0B94C0006 for ; Wed, 28 Aug 2024 03:04:24 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=mVEbTKA5; spf=pass (imf22.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.210.170 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=1724814178; 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=O1UnhBnsG0EZ9945TsUyg6NQ1s52ltgN7fNtAXZRUfE=; b=EFN+H9sWuI87FXQaWgiwhxOnqMOIsIPIqRn+/db44+cY7rchhkmFwbNjqVQaGG6tiSI82K 5xZ9FFbvKuBnoc47laxFJRD7f9WExBnLFbolKdcisA0ZU8IEkaxRi0szhwoGqOs1H+GCt9 6CdJC+C2/vq5mOiCWJfzHLjxIO8Mu50= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1724814178; a=rsa-sha256; cv=none; b=4gNdgn3UI9Czv5hW1lhZMoA+WrrCn+9rMv7fHL83K9JLOjdpXH7D8pB+iGlt0633l7tZQN oBLU1DNG6wHnz+deKs3+Ybt0Vg4tPCmoyhAjifbibfklyuXr1rBoP9bn99f6EZ1SZw5NK0 tYfULEd9mnq8qnSfOGf0H7fHya9BOBQ= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=mVEbTKA5; spf=pass (imf22.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.210.170 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pf1-f170.google.com with SMTP id d2e1a72fcca58-7141285db14so5369406b3a.1 for ; Tue, 27 Aug 2024 20:04:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1724814263; x=1725419063; 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=O1UnhBnsG0EZ9945TsUyg6NQ1s52ltgN7fNtAXZRUfE=; b=mVEbTKA50OMV4HP2AhdaOLCkL4dfComRgHyJjymixwgMZW4ZUuw7YhNU9D5u2KszwF eh5UxDplWzBvbomllLKpEW2Low4VmGvscDbvgCE6kKHcybKRWphJEH53U4XiHF8gckdq B5rpHs8rrjdMROuDmUhqDRBaDkC0j/my5JSBYRJU3VYrANBQ4riSxMxzVyHM3CXEYSpD +0+5Rn7HAnIenuw4fes1A5RNnI4NG68DZ/SnEo64aVDa22ejroWuS4uYFPSd4mCWzspQ f3WFIVXOXoYJ8dnFdIg+2VZyKvE/SNlEcJkjz0RfMXrSQCN1v4OYFA63Vi6IOpr8VrH8 /LIA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724814263; x=1725419063; 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=O1UnhBnsG0EZ9945TsUyg6NQ1s52ltgN7fNtAXZRUfE=; b=Ke7Bt0eiqhjCLS3xgQRuMp+6dsY5WLAiEQK+TVRWdkfhpquRFfQK4osRldSoh2nMMK ADn6bAdhkBoV1KFGwHyq9gPov6aaW255NIEA42YGyJSnQcXsnaxpOFL1ZsK3sWREqK4c 3s6XCwvcna056MwKYRh1eZ0TLrrwS/zEXLy7pl+VrnYuhqJ4NaikBSb+iPXv7XzUnVkR ZkKFgqUSPoMKaPyfAEle3KSZ1qzprKnNOaEo0HgB/PkJzLn4tPIZHrcGZiQ3pNZHlbzw UmKc3vwcIxxc+m/RTuitnqKm7SypYrr9L0Eh481uEQ2OSKE4Nj2Y3hVXtuo04RdXG/eH 2Xiw== X-Forwarded-Encrypted: i=1; AJvYcCX58F/I8Ch3zuibZ7RMy1VumNqeAffM/S07tW8AWFPKFLSMSUHwnfrkRzl379HthgNG5zvtfz/I3w==@kvack.org X-Gm-Message-State: AOJu0YxqaBcbTfnV7ilscdu7enQg71GFXnnlrKsItplrhQfsLQDTw1Jq 2JkgnbXArxYt+Aoh883A+tma1zRC4WNf0/2SmWDzFnVkK9LtqHOq X-Google-Smtp-Source: AGHT+IG7QBffnYc0dCLvaTOX5KXK0mJkKlpaXmWfgPhMMu+561lRAfU2Q11Tqx0ug2HVRiltuCcmIg== X-Received: by 2002:a05:6a20:b598:b0:1c4:b931:e2c4 with SMTP id adf61e73a8af0-1cc8b4bd8bamr18005852637.26.1724814263473; Tue, 27 Aug 2024 20:04:23 -0700 (PDT) Received: from localhost.localdomain ([39.144.104.43]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2d8445db8f6sm317977a91.1.2024.08.27.20.04.14 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 27 Aug 2024 20:04:22 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, alx@kernel.org, justinstitt@google.com, 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 v8 6/8] mm/util: Deduplicate code in {kstrdup,kstrndup,kmemdup_nul} Date: Wed, 28 Aug 2024 11:03:19 +0800 Message-Id: <20240828030321.20688-7-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240828030321.20688-1-laoar.shao@gmail.com> References: <20240828030321.20688-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: D0B94C0006 X-Stat-Signature: wnkug5f598eqat1pyk86hhe7uedk4m1z X-Rspamd-Server: rspam09 X-Rspam-User: X-HE-Tag: 1724814264-810844 X-HE-Meta: U2FsdGVkX1/J+S435fIOy8Z7L9am00qdTPW2BNP/+wBQ3AU0/c1lrXg4XAiDgn9Ie7FYyHX7/btWyJirgS40cKRPno7GCLFS44gqZ3ycPf468zWa0kFLHgUasTAjBHXF/RBQQ9q5R+EU5eoOtraSnxS6Q4Tyd+g6KWKTW6tReOZ/YT6PhMluwDsfk3Zywww8WS/yGFGVDRCQ4v/eYkTSPNElaBlwdDL1CCXvhkJ/bGgg5y5sX9pS9HfJsMsV4do7C2G73bLhOHBCkbsg2sU90s+8vY9ARXswgb7Tkeh0eabF9DcbczKJNGcIqzEEMHopNhRB+zZ1UjQCEiA6IYkQUzWkQ3KtOftkweNNjdfKqml7ddjjppLc7+UJnRFIWA8Cc25KyE5z1BiwzlaLh8Ssns7p5MZeB5LWYsMjjf1G4VK+pWmiblc2fuNFnRCEqdwNQXc7tfECjVDa0TMqa4xLBxSuGf0kwWQGfzHs2U05IMua8FCjgbyR3YgBSiNbvsU3RnYOnzsMZHerFCqKa+G76EhW91/MWTIkvj99xXFXPJXaJ+INLKj8kE5fnrjVACR6nzyAegunk3U4ytLgx6lf+OBpm701akNtFN115GRcKp8bowC8Nx1nPX7LhTcZ2PqOsXPRXXhTh4qwGQWWi4bVqWwvSzkRkR7ZrCvO4uG5lpZfcfTSBmNrDwPjq1hNihkub0PUn7LAPWatpJI8GepwehfPOzjZCeNZdzdph0OpSox3ErqwPNoYVVtM9iKJr5B9ghdtSRgJK3gH8Hy8kHPCrkAhoanuhaGaURt3QsqTrDris6pYd4/JqmH+q8WxwtThYAzxgKoIirwdjSHjQ25hbmDUx+YSzYko0k+7JMvxBDqo9JaTXXsm7etiFtIt05CI42yY/PV+i2uwnYLZ46QfIx6OPW+yRJzHtGMx1H3cOT2zsoFXkj+XvqkTqZOmSGScEGWn6+8hB19GildEp+c AgyDNpMu Ko2vxfmVIC1uTER+i1jUtJkY/ebZQOoiQ95kozUYW3hJlipClPHoOgpAOpJiOi50IrkwWsS+d5hID4cxLPJpkXvdMhcwhLOkeDguviUz0WMcFVVxr41VdxGb+LhOpEW6V9r3SRzLH/qfp+8GVRtCWaj9p5La188g6BL0f7oE9A037R9zonqwsaKi1HSVZlqDO7zxu8yPuGiGCnkw/dQqJGVmpm35/EClPHACHotp0uglAbTBgpHaKhWAZ/3Li+tFfn+oVQRMYVi0MzFWL07ssDSJQXLxP1aSwZDg6ZTfkdwxemcVPFRCzNuJnrWhLoYxybisCYSYpQMRK8y/hZ0J0ceic0d/KueBYSEWZoCGkANPkca8nIMSolalUTYTXiVm2uJ4l9KCv0//TkvsSSVbJUw+h5sUpSR3UNE5TtCefWeb1gEoHE3J/qZ6aj04ULyIFTXug3BmjYoXALT8UANHU71ni/lcDTp+P7LWqhseEI2zoFtne4tK0Zc+XOnghVkpmJ/9Re9NKUf/uC0eS/qXuXjMuuoYXOmjckPQT4rg7NZ51K92K0xrgAQWQfaD0kdEv5s7wvhZ60vzbnjsXjxnmOCjAO52Dr1NvxHJray1+0EY9wYs/Rc87kapv1Lh8EnhCYsYALWLMuKqtGFSLeR+6a7nm5gz59taT1ODVWSKbQc1x/bpES7dxZSJWFDc2qn2JGc4g5j9Vt+t8w9M= 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 Cc: Alejandro Colomar Reviewed-by: Alejandro Colomar Signed-off-by: Alejandro Colomar --- mm/util.c | 68 ++++++++++++++++++++++--------------------------------- 1 file changed, 27 insertions(+), 41 deletions(-) diff --git a/mm/util.c b/mm/util.c index 9a77a347c385..42714fe13e24 100644 --- a/mm/util.c +++ b/mm/util.c @@ -45,33 +45,41 @@ 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, not including the NUL 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) + /* '+1' for the NUL terminator */ + buf = kmalloc_track_caller(len + 1, 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 NUL termimator. - */ - buf[len - 1] = '\0'; - } + memcpy(buf, s, len); + /* Ensure the buf is always NUL-terminated, regardless of @s. */ + buf[len] = '\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), gfp) : NULL; +} EXPORT_SYMBOL(kstrdup); /** @@ -106,19 +114,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), gfp) : NULL; } EXPORT_SYMBOL(kstrndup); @@ -192,17 +188,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, gfp) : NULL; } EXPORT_SYMBOL(kmemdup_nul); From patchwork Wed Aug 28 03:03:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13780367 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 A4C5CC54749 for ; Wed, 28 Aug 2024 03:04:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3777C6B009B; Tue, 27 Aug 2024 23:04:37 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 327F46B009C; Tue, 27 Aug 2024 23:04:37 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 216956B009D; Tue, 27 Aug 2024 23:04:37 -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 031976B009B for ; Tue, 27 Aug 2024 23:04:36 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id B37F141C13 for ; Wed, 28 Aug 2024 03:04:36 +0000 (UTC) X-FDA: 82500161352.26.3126311 Received: from mail-pj1-f53.google.com (mail-pj1-f53.google.com [209.85.216.53]) by imf11.hostedemail.com (Postfix) with ESMTP id E12584001E for ; Wed, 28 Aug 2024 03:04:33 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=kCuY5PXA; spf=pass (imf11.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.216.53 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=1724814186; 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=XVdwfZoyZWde9aSN6zhMbNNaJxIyi5X3Dr8kaez296Q=; b=2P3/QSXE+R/cM0PHH6NLbX250N/E3G+9ASStirDcy9/kphMwhDxc0WZNj4RDBfXJcnfrjb cNzKu7XsY90fosnGKboMnyKbZrSWmwxUhMYe5rNL+3KEyR2qt7qPxZ4hRiDTKUW3vabIgM pxBfBRTc1yh5ZN19K4vKnZtXbCMN2z0= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1724814186; a=rsa-sha256; cv=none; b=FuzBdiRLup8qSXtCFDo/l6gs3lZZjArQfnOH9kEGEJ2/CsW8mT9QPHcqE6bFZ+8PlydiCY JuRzAz2kWh+tmCPc1REi3zy3GX34t9X/rkEs1kOUy/p8+sdrt09/66mr1GRS8I1zeP7ODL p56PJDAUrLHIJQk7Djc/YsJmWVPR05A= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=kCuY5PXA; spf=pass (imf11.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.216.53 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pj1-f53.google.com with SMTP id 98e67ed59e1d1-2d3d58d6e08so4808787a91.3 for ; Tue, 27 Aug 2024 20:04:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1724814272; x=1725419072; 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=XVdwfZoyZWde9aSN6zhMbNNaJxIyi5X3Dr8kaez296Q=; b=kCuY5PXAZdOBq/4Gng8+CEFf7qzXFtWVG3991RkATH/dIVjo5obSR1lyh9w/Q+5eqD 9z3hf86UnxJaxLVi6JpHvm3u6zmOhQZhrZm3VG0IlZTTmXI/q76i6wkuIN+Kbmsn7cEr 7CyukcXOPbFAdMWS9XQo6Z/CzoiY/fV9azW2KfpYdmg1rxz5gtbQdtRpW1sLf24wYQSQ bDwDuRDGxbZ0f68udr+j4Ezfe+7Jiu1NJ6q5K1FCYpVp34OMGRA1dtm1CYqcYZv+vX9q XVy1ngDTyrlEePJr+Ph8WkPgKpuYpwSZ+PTINirpXvoGOOSQsPv7EtXFQgmtCTd5cb9x MjLQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724814272; x=1725419072; 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=XVdwfZoyZWde9aSN6zhMbNNaJxIyi5X3Dr8kaez296Q=; b=rPBnnWqckcTlNbpa0dPk/9gAMDeyJss8aMldAukI6131TTAsY2yokDkUkIuuRXX7js yY7OWOgtq8CkR90fi5j758DKSM/tRFe3yvbE91TAXF6lWhc2pHnrgClhMlgPSXd8GVh+ ZaYmJGsAa67Qp43d1iA4Vex1MKMY44lDTTlBecirGw/SiB+9fBqArGlq/eiUZfacnnmf YtND0QeKrjYdsPMIVbIdC5Bzy+wP7ZkmGtzArEhCvd1IekrBe6/BrQaAIhK6DIIrHMni uzLJ8hvQCjN0s4m2RMTixPnC6yW28xqtdXYc+Adwmb+XbzbweHSdrQoUEEjdAXdRVuMo lNpg== X-Forwarded-Encrypted: i=1; AJvYcCWtYMOEY0DEQX7ZfAFXFOEk4M2sdDPBT1IvM8jFQcg3RY4bBrDPJcXTS1zznmSfJDT26ktaAaWI4A==@kvack.org X-Gm-Message-State: AOJu0YycGk7LcjJrsyQ4yB82ZlzqneQoiZC62dRdXv3O39ATO4HlobYJ /83iOboneTJggtBm7LbAqWvuZ4tn/ZP7STezJIHYsWJCnsvUSoWT X-Google-Smtp-Source: AGHT+IFb9q9yq9VVSwLC6AkI1/p1bhkyyRxy6O/7jYlaHznbpdfEJAgSG3Tzmhmlf5xXUEBbcGAPmA== X-Received: by 2002:a17:90b:356:b0:2c9:61f9:a141 with SMTP id 98e67ed59e1d1-2d646bf605amr17374556a91.16.1724814272564; Tue, 27 Aug 2024 20:04:32 -0700 (PDT) Received: from localhost.localdomain ([39.144.104.43]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2d8445db8f6sm317977a91.1.2024.08.27.20.04.23 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 27 Aug 2024 20:04:32 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, alx@kernel.org, justinstitt@google.com, 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 v8 7/8] net: Replace strcpy() with strscpy() Date: Wed, 28 Aug 2024 11:03:20 +0800 Message-Id: <20240828030321.20688-8-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240828030321.20688-1-laoar.shao@gmail.com> References: <20240828030321.20688-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Stat-Signature: cpwrn6ugrdji91gd7oftpxdibywn3hp3 X-Rspamd-Queue-Id: E12584001E X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1724814273-227454 X-HE-Meta: U2FsdGVkX1/7ViPCmWvOHboMAxINPJWeIx6NCunaMhZLYVsNoKymu7BvGGOjGeA8BdRh6Rv62yrTqkr4CmhsiIoPlIKtFlY5hcCOAMsc3Y9oboIwb7SqPk9xu4ukr5hoKgRbsikCgd5qjRYDYY3j7kSiodfKEqBFOjhb9JuGr8RigBD66txCcfeoQo/hMZ1xY4h7rGzCyB2OiW3i6sRF0Td2MIED/72GqipeSEU1HzErCsXTsIdwZ26MVEZj+fRONLbFKopfYKoy4FWQvOKDHx9wLJM6lrX763/wTOYTfecv3hhWiR8lYKQtkI8+2/zut6z2FyDTQM+cXVqUMEEZLMyHsWJ65RTT3N7g2EEWLxkCZ9l6INEYDmafA/0hM99jgAeioRpOK3cuqVUKXfN+lhM5Nkxid2gVqzvfK+ueA3gA3PgAgO1PYG01MFHYzZFwatB9Sx1XmpJnbZsMknXCUwtQfyfarWEoCnd+N6F1ssePp+oBK0ijlYjzh+hrpYzhfHfHT6bROpZX5F3JFyA92TKeaSI0cEQG7RVsoL1zohGRKAytvURCcQFihrXZBybspXdV2YXAyHguXPx/bhNuQ09BdB2jVrxGFqZ7VLdiMgCoIBGB2D8b+t2jkiPYTbFHMY0nZ9weLzmf+0EacJGBfikCf5iFJWrSzBE6JwE35ZUsmZkvMqokivRYb1cUEb+96waBD3CLjuYcrqXm3pPEo4JJO2bDFRUzjdTojMZSh04usLdlB0JheysCODd9C7hQsxRvgc8AbjN688KwYA7w35roE7QTHKdpNpTD6jzt5dQYmMRpPq8SZyQDLoOO4j/EBzVLukLDeRCJOCseXKP3gEe3fOgkyHZWRw+4whBrqmT8oA5o6UZAd6WcFo395KUKsohT/oPRO4txXt6/Y2V9d9lsfPs5HpPg+PFQz8Hy2j1qL1I+UsPnFwP2CZV72KBl+sgCLG++UId+Zzvgwm6 P5BaMX3J V59hnwmfkRA/hB2oWss4cJN7H5MGGUXzZb65EHtl9+8PDTVR2vEXQdL00qMAPzcOzHJUrbFjmio0GfK2N/E9uP1KkqqBER30R7s0cbVZ4Ci6zlt1r4UZDP5yfyCqP4oOGWsr0mSRuuwzzp2KiSPEjaaFpACtS+vw5A9yNCY6UQjH9Q8ws53haLA+Wx72LMjKHx051Y6WQBBxwHmZIH52fdpmfAMUA0kbe9z96pNqZqo278/Qhm+9tK9CV8zqihUMxqH6lCfCUHBWcchG1mDrGoqSId8X+Hm0KbocmJbcrtTdNQnA0xoLuXkfnayDzRC8l2yxBa+bI2WzfOqXvph9rp2XFxEJC/j0MVbU8X0KW+tIrYt4dUKwD3pNNPz8mN3vAqf8+G2VeiFTwBfN3wse2oTTbFOLhc75sD2Uqw1vSuNU7/76TyBPqhZRCpzUVXBQTo/OkGAOj9FkF/uEDcd1oBM+MAmqrMTmZdGGueGuJtd359KRdVAc9w9jnKtavZvuFUuxfHphlXdy0CJZ058v3UMEVGqMF0j6XBd10UJbm3WuAX4ryN3AZOpMwcBjkYrp9yPxTYrsoUjX5vGLw3kNrG+bzWCWXlddFASrcv/+wWxfBOE6vJDCPnOPVwrKwWwQEncSvFxGmo8ey/MKyk/F09vwMPjrn0ZWH/6cV8WkylsBIP6tVbIIiVamd5sHExh+EvBVD 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 Reviewed-by: Justin Stitt --- 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 b8eec1b6cc2c..cf7c36463b33 100644 --- a/net/ipv6/ndisc.c +++ b/net/ipv6/ndisc.c @@ -1944,7 +1944,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); 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 Wed Aug 28 03:03:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13780368 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 1EB6AC5474A for ; Wed, 28 Aug 2024 03:04:47 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 944926B009D; Tue, 27 Aug 2024 23:04:46 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8F4576B009E; Tue, 27 Aug 2024 23:04:46 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 794BE6B009F; Tue, 27 Aug 2024 23:04:46 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 53EBB6B009D for ; Tue, 27 Aug 2024 23:04:46 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 0A2B4AB37B for ; Wed, 28 Aug 2024 03:04:46 +0000 (UTC) X-FDA: 82500161772.16.BC1C240 Received: from mail-pj1-f41.google.com (mail-pj1-f41.google.com [209.85.216.41]) by imf24.hostedemail.com (Postfix) with ESMTP id 34F8D18001B for ; Wed, 28 Aug 2024 03:04:43 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=kW3C+vfk; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf24.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.216.41 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=1724814186; 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=8OBLeQ9F541pwxbn8dEubiWANdxJtGv9ekHxWl5Hwac=; b=d/J9jJwA+/tYGo8Fno8EHbsTojLKhTeYmhlT3vjCc1vh1g4mpoVgSEJrchrGZfHKBTrhBY y+xZaBjiE3WSKNfghtTutvQEfTdZ6ExfE1TAW6QqDajNcH0MGErAtBGabH4YMWPzzMQq5v bRLE9ja9c7A5u3Bn0GRBUx/RzakZzOE= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1724814186; a=rsa-sha256; cv=none; b=TY0jS3WpZu/UWmXt1FC2nVAk65oMj93hECbBqd+hIEs1a1y3VNYSR6hpBPH0Qhj2sXn2Da /X07umywbRmhKqtk11eC0NykTN18t6RkJPELf3XEKCP0COrVl93uTpwDfUhbAy6dfHfbFv T+52h3bmbSlH8jdqtMnQqEiwVbsnWwk= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=kW3C+vfk; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf24.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.216.41 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com Received: by mail-pj1-f41.google.com with SMTP id 98e67ed59e1d1-2d3c05ec278so4580392a91.0 for ; Tue, 27 Aug 2024 20:04:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1724814283; x=1725419083; 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=8OBLeQ9F541pwxbn8dEubiWANdxJtGv9ekHxWl5Hwac=; b=kW3C+vfk+e+vDXeWiqL40KaNZfpjfEpgBWeYnx6ofSSqcuqhh/FK29DT1K4wKMFO6U nSTiJfCr7c9x1K/VB+dOp/BefJfz1SlPcFWZb+I7EcCBvxrMIbwhZaI/uHcK7ioL9uIm eHR+rxam7wo8/ao4XdDyt/kNIwtp+TMrr6WoNyjV/R4ApV6bjYelQSTCkTkHyKboNvlr CBQ/eB3FFjS2m+tZWpdmAA4AQVITIkkwTQV7W1l44Ejpw56TB0fS7NdOWbn6MyCPQV7b EsRzCranrgTTHbbhF5b9Kx+NRCevE3anKRtgbeu3bWvpSZzzwCu9ShgNLkxNxMgkMi14 7uZw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724814283; x=1725419083; 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=8OBLeQ9F541pwxbn8dEubiWANdxJtGv9ekHxWl5Hwac=; b=N1woHjOeVj4keqaugdxpXUnFD/Qjo10DSKZGg7IxDUjAroCLMb1kzerz1iDT/YE5VL n7xV8modctrW70G7cOsJiYBSZ9yvAQKEK8hTglB3ks6E+kUN+WJofo+BxL/sZRB8rYiV UjNJy43Wiszw168YmUomtmwWqXWAG7IF5kXSbfsTOQNSLyMH6si0WWf1nk4xvKQ4KGld bq8le9trflESMoi9o2/U6CfI3vYl1eOLKoVz0bkHWrZUygmubWPqvOTq5ZNK63BddmMT cjsUnXz+HGl5hct3YpwTmmNXtn+FtjezATkUApUsIMGzeCpzwJ2CjSNzdGKdRnrEhck7 R2zw== X-Forwarded-Encrypted: i=1; AJvYcCUd8n348DazZB17nKQolPbQEiUzMBYsgxEqRT3qFt6ZxiqglyzntPfzfZvl5Oz5wdgdj3KP1TQ6AA==@kvack.org X-Gm-Message-State: AOJu0YxsMtg1A5y8YpBHOpbmB25mk2WbotvPYWfrt9Bgg85LQUb1Znho uRu5YcZwrzx7EwEp8S6/Jg6dLCAAeFh7UjBghloBLCweq2FtxZ+Ows5amfwG X-Google-Smtp-Source: AGHT+IG9WCG/sJqfIUmD5L1QkWASIgz28eaxuJrS0TfQtB7xTFo8bkGI6Rj777tc/95CgBuyWlEpcw== X-Received: by 2002:a17:90b:390e:b0:2c8:e888:26a2 with SMTP id 98e67ed59e1d1-2d646bcd147mr17296354a91.13.1724814282792; Tue, 27 Aug 2024 20:04:42 -0700 (PDT) Received: from localhost.localdomain ([39.144.104.43]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2d8445db8f6sm317977a91.1.2024.08.27.20.04.32 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 27 Aug 2024 20:04:42 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, alx@kernel.org, justinstitt@google.com, 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 v8 8/8] drm: Replace strcpy() with strscpy() Date: Wed, 28 Aug 2024 11:03:21 +0800 Message-Id: <20240828030321.20688-9-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240828030321.20688-1-laoar.shao@gmail.com> References: <20240828030321.20688-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 34F8D18001B X-Stat-Signature: ttg7dox9umqibfy3fzjsaibxx9gseeex X-Rspam-User: X-HE-Tag: 1724814283-197491 X-HE-Meta: U2FsdGVkX19C0FkFinjA5I2kqc9+z/tVqfhgWY2pGACUtIPJAhMPxLfuGda0vHGR5C3FTEjeu2VLOobYES2UIkFr2Hd8eyaK2ViEsXTWWVfzRh46jz/e/uKOPijjE+GOds/8NcJuPbVaZKmSdu9Yb9Zjb83kjfLY6vVuD1ktlDi5Ernc4t+kczxIRFrz27YMe9r4uVi28VoZLZX2h4JHwYMmo3u2b7M5llvvm6Zzy2tOUMqFqAEplh50j6UGw65z8TQBjnlWHp8jjapigURWn24h2SykCV5HyWucWxejXwJOFR9J9ejAWDFHaxgV3GQQYRKWoDO2/F+SCKSI+gAEOwlvxO2ea41LjxcVOjmGwj8vB1UviKostWtLCPCNrE7mGj4AhxGNRQCRoLeUB6jFedXq4jGke4aio0NmD3Q3at+UHZN5bDxwlkz2GGwb9/TP1/5gR9iXTXDRg1x+RfVSOEZo4hoQqjSoQGCabdmDzppctZtxxNk2lqXlWzbNAmIBtGqxG1oUC8NNB5UqdSk/wOdL9FVf5qIUD/zturSEhD7GeUiudAY0eYzdDDMKz/ELbOaen1WqotHn0V5wmln4CNUoG/IOCb4rg69lKbifCul+mNWfEMX8FLQSj6R4ZUjl5Wf7uHOIo9EvxQwHBdJHEFw/AlywheEuD4eAuJSDzq6AG+PR5v2T1He+uNT8uOprBDTq47O6N5li3nMp8AGuLkLf5oua6KBcDinAF4yNLMUl4Uqf5a6Bru0b7y6XlaLZV6yq6vpqgaDOR7xLy9xsOzELXKSyE8fjRILqjaRXaqB0eSkqLxu3Z7kqe2Z6qVuIc2HyT0t7nt8JFcz6LiTsXUJR0Zgkr9jQImTD7hlPRO1yd27sy/toglOVAvyeQhsxMHN+1+wWrD8SjICtCT5iI6eK1hez/rl/LRMCcrdWmedcDp5hO2d+BTHaTT85Mo3vrbDEFtUeBYSP6bnj2w/ SrhTZ7r3 BbihjbIj6bRgCcdSsO2kQQQzum2qjSqtTvYa8HRXI9fLN24VeAe7fyBRRzuDq9qBi0inuzYWkPsAQEgHizDH+ggeeCplzcGvbh1KWDWsksQxPY3IDuihd6ER3ARo8s4GG8a6MbO1M/wrNuntj64MK4mSYPcymhtt8sNEQOF1RoQFcy75qXArTmjAk9zJuJznc6NtcmCLO/uA0cY/GwqupdlQ4/Plv866zq1NC9go2hvQqseD0w/bRjbgLtq2HYj41n2Xs8o81IB+SCBIzaiG8zRSz8p5tUl0VFgssfiqWq8vXL7eAJDKhagoEWLooXnBM5dr+gtF+S4l/2yhZx+tcKUzxOrsdTWA7hsp6LTXTVwLb/KozDZtvlLa3rumEgKHBPqkQcTOPdJKN9X66haIj3GLr7whFnNdw2M2b3mY6yifGSkjws3rMfNILfI1QNDx0QAb6LpC/bu9N9gxFdUcprDigAF23GI+kIvK1UQhlWhQKzeumQfKwX8jeRy5KWmBJjA05WeqkErT9dAl32/0lPQ9s1ZcvEZWH4ap15zN1aWYrBN6L96l2eSpbmxCxSzjcDc0I1yMiA0BhCXwYfbRatmWPPxmifs1EB6M4BCJZ6uotiwQ= 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 Reviewed-by: Justin Stitt --- 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..2d6993539474 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); ret = __drm_mode_object_add(dev, &fb->base, DRM_MODE_OBJECT_FB, false, drm_framebuffer_free); diff --git a/drivers/gpu/drm/i915/i915_gpu_error.c b/drivers/gpu/drm/i915/i915_gpu_error.c index 96c6cafd5b9e..afa9dae39378 100644 --- a/drivers/gpu/drm/i915/i915_gpu_error.c +++ b/drivers/gpu/drm/i915/i915_gpu_error.c @@ -1412,7 +1412,7 @@ static bool record_context(struct i915_gem_context_coredump *e, rcu_read_lock(); task = pid_task(ctx->pid, PIDTYPE_PID); if (task) { - strcpy(e->comm, task->comm); + strscpy(e->comm, task->comm); e->pid = task->pid; } rcu_read_unlock();