From patchwork Mon Oct 7 14:49:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13824825 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 DD7A6CFB43F for ; Mon, 7 Oct 2024 14:49:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 72AA76B0098; Mon, 7 Oct 2024 10:49:49 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6B4596B0099; Mon, 7 Oct 2024 10:49:49 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 52E126B009A; Mon, 7 Oct 2024 10:49:49 -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 30A4F6B0098 for ; Mon, 7 Oct 2024 10:49:49 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id E27AE1A1787 for ; Mon, 7 Oct 2024 14:49:48 +0000 (UTC) X-FDA: 82647090456.24.8A68F89 Received: from mail-pf1-f182.google.com (mail-pf1-f182.google.com [209.85.210.182]) by imf22.hostedemail.com (Postfix) with ESMTP id 0C0E1C0006 for ; Mon, 7 Oct 2024 14:49:45 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=H0wV0mXH; spf=pass (imf22.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.210.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=1728312451; 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=oVEk6SmaURyo42RvRGYBr53delaAcBEjfSHuuD+Cst4=; b=wbXQhrw0vdqKcKqSRLcPerrWpfhRW4zVDe8LFsWXJi93p0WhtQYl/GGo9c6odpULPJIGoo 1+nf8sAPAAV+aLl3Ngs1HPvkCuSk+fZqHDGzAZX+ZApidWaBivM+AqRUc0HkmrYpqii8LS PjsnFxxalybBRuula6J9UZcHOkcOPXk= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728312451; a=rsa-sha256; cv=none; b=7jzAHo76iC7RHrIyLPu8YXWE267cZQ3QOdD/hacJ20y6h8WaPv3YZdiiwr1ybRCIU4Sm3h LFajuCsALiPdMoapfYjKVhyX6XaXefJkdFBalPOyed0Z50tmXREpVjl0lqTZK04yHUoMNn sDkrd8CIPQ2D5axaBPQ1dNs/t3xrzk8= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=H0wV0mXH; spf=pass (imf22.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.210.182 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pf1-f182.google.com with SMTP id d2e1a72fcca58-71de9e1f431so2264180b3a.3 for ; Mon, 07 Oct 2024 07:49:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728312585; x=1728917385; 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=oVEk6SmaURyo42RvRGYBr53delaAcBEjfSHuuD+Cst4=; b=H0wV0mXHUeqRmFbYSLyU1/xdTCOe/EVqoamvRCuIzthsogIEW5wxhDOQoiVMkTLvjO evdlGVZb0qAFLNGFm6O8j6veanOlsnH7zbQf5cDy9z7QJq/w2/W9VoskWbFKIGe9CSwS YwSBxLmgvSc/6Jp6FXU0aCn0OZXbHdgTU7IllQNMmlrXOGxWxmgmSI7YncPzQ5gIxER7 x6LVBLI1DvadWdFoURZ+Hrzz+HgD10zQpV3jowOvC9qRygrs8ekYgRePG3jGFVpNeUB+ YU/0Jm9YOdguNJNtKBav7bAGh4HzeYJe9OBaRAoE3bNYUCB4ln5E0ENcfrjaDicGuOOr 3oag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728312585; x=1728917385; 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=oVEk6SmaURyo42RvRGYBr53delaAcBEjfSHuuD+Cst4=; b=eZfTNUc5avymhPC6NyQz3/DJ97s3dFMtTAJ8x18erNaAYpO27ogZpTfGCrhxBhASeb 0xf5bL0jhIRGG7+FnbdvQ0KfAodsjMfQ3g3q7QAe0y1IQ5k07lRUXc6ypOjivVq7I2vy Jy5QeNEWbAE/nfwnhj/cC/P1u08c7FWKeHmJhk5oMLM6quumjSJoUX5yhfmAtb9gZZ9k UdNTXBXpCKknnIcicTvILdaPHskmSXSVX5s8QehOuEt4o8o207rfp1TaVxqpVj99qdV3 9rhDZ8rNpqHYPOoF0EJAFOmjlHS0FZhFmQBK0rIiVrvptcL2GAln2KFzzTos0YNNb7bc QN4Q== X-Forwarded-Encrypted: i=1; AJvYcCWbUz0eR5Bn4YwtARDklA4BrsocmfIOrZAZtCuF6bTrtZ52u0NKkHV3SWEh9dmzAqBXMxL/5W1hDg==@kvack.org X-Gm-Message-State: AOJu0YzONYFxA47DKMQahK66/PvTocFFs/pyZLP9t1k3IkcKWTKbmaIX 7UnabDolE5d5V4aT/H+rCtcd7SlfG9liL5trpM57xXXruJGCiEZX X-Google-Smtp-Source: AGHT+IFysx44ym7xeNZ29ZQx56P8LK073YLR3FEq6ZYjPgOvQStiRImQUgMUmAdR1iJQTcm5/XW64g== X-Received: by 2002:a05:6a00:2eaa:b0:718:e162:7374 with SMTP id d2e1a72fcca58-71de239f5e6mr18318586b3a.5.1728312584814; Mon, 07 Oct 2024 07:49:44 -0700 (PDT) Received: from localhost.localdomain ([223.104.210.43]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71df0d7cf82sm4466432b3a.200.2024.10.07.07.49.32 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 07 Oct 2024 07:49:44 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, keescook@chromium.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 , Matus Jokay , "Serge E. Hallyn" Subject: [PATCH v9 1/7] Get rid of __get_task_comm() Date: Mon, 7 Oct 2024 22:49:05 +0800 Message-Id: <20241007144911.27693-2-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20241007144911.27693-1-laoar.shao@gmail.com> References: <20241007144911.27693-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Stat-Signature: zeydo3j9fht131hwce3yocpxq64n9euu X-Rspamd-Queue-Id: 0C0E1C0006 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1728312585-389873 X-HE-Meta: U2FsdGVkX1/qsn5P4vY0PdBHEI6nfv94QtToD8101f18NsxL4HSDs0N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h4u8yskULHzwhHyfq1BYb7dCGQSJKSmiELykBcYBvrzBZPR1OF4sUOtATGPB144x3cnsBKpaHWx6A7AEvSAugObs/ICq4HAJP99g/3GypCzGa5My2ZIIpSp5gS1D+20H3EnSR2Fzy/eoaJ4MrgIp3GwO7NzghQHW0/hVvRjRIoZ8GfbzcbjOyPR2hO+cc80VaFecS78Pg61gM2s81qQdg/H00v1qhFxiaNlFIPnoEp98RCk+5wGRP4tTJ5ChT/eWEIfwAZ6bjf9if3r39hiZ5Q5bLq6mx7zADrdFaN4Y+pxl7h1o11xYjCkg/XRe2ZfZuHL8XpZQ7BL+nyb85WluJmoBgmjQUbFP7NLEsmhIxkUZUUuGQToWlTTTYI+V5dcxUiyOnK+Dz04u6gG3/6CMoSUiHVPGmsTaXvwABO6qT7etmcMlHh5BPDDub4q4egadDQZeZ14youcAqOzWRNGaLz4SSUAau4M4aQCqwciS8CFILAwXczTNiFPH6r7piiL8OmzHO+4qXbtIGjWBmyPT3lBBzyAHUuY291QECgPPVdN8WW8wmMw0cWdb60BLiGDPzkv6N2cty9i7U4uyVuB0GXzs9jorMQABfwfIDYrDa2Rac31gg9D1gsB1STiivgHL5oICyU2U3tn8H4= 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 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/ 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 | 28 ++++++++++++++++++++++------ kernel/kthread.c | 2 +- 4 files changed, 24 insertions(+), 18 deletions(-) diff --git a/fs/exec.c b/fs/exec.c index 6c53920795c2..77364806b48d 100644 --- a/fs/exec.c +++ b/fs/exec.c @@ -1189,16 +1189,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 e6ee4258169a..28f92c637abf 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -1121,9 +1121,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]; @@ -1938,10 +1941,23 @@ 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. + * + * - BUILD_BUG_ON() can help prevent the buf from being truncated. + * Since the callers don't perform any return value checks, this safeguard is + * necessary. + */ #define get_task_comm(buf, tsk) ({ \ - BUILD_BUG_ON(sizeof(buf) != TASK_COMM_LEN); \ - __get_task_comm(buf, sizeof(buf), tsk); \ + BUILD_BUG_ON(sizeof(buf) < TASK_COMM_LEN); \ + strscpy_pad(buf, (tsk)->comm); \ + buf; \ }) #ifdef CONFIG_SMP diff --git a/kernel/kthread.c b/kernel/kthread.c index db4ceb0f503c..74d20f46fa30 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 Oct 7 14:49:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13824826 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 9BC27CFB43F for ; Mon, 7 Oct 2024 14:50:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2E55C6B009A; Mon, 7 Oct 2024 10:50:00 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 26DA86B009B; Mon, 7 Oct 2024 10:50:00 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0E81D6B009C; Mon, 7 Oct 2024 10:50:00 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id E1D426B009A for ; Mon, 7 Oct 2024 10:49:59 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 92316160704 for ; Mon, 7 Oct 2024 14:49:59 +0000 (UTC) X-FDA: 82647090918.19.CDFCD37 Received: from mail-pf1-f175.google.com (mail-pf1-f175.google.com [209.85.210.175]) by imf25.hostedemail.com (Postfix) with ESMTP id B4924A0007 for ; Mon, 7 Oct 2024 14:49:57 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=JapusFmd; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf25.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.210.175 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=1728312450; 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=4ED8sTWekie9637+cHBCmQFpLStnm64j/0llImVgOxU=; b=jI7xEZGF9g8/3jbcngJytzLHMynUluYxS2xo0UF2gY+gEwfKTqOzlmFH6TvPA0GZGvHckL vpIfGpGIdsgAYKF2nr+995d52NlP6E2R4SrZh+gxxbhicMpzsUcYy5tfWlAh/TYTtvgiNc nbgbxYTcUNALgjjKfrI4ySxXwgJFoc4= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728312450; a=rsa-sha256; cv=none; b=Gu2xTLmfEML0y+37HEdXqhxDMBV9vfl/8whmfQKu6wjk0jRQIEXSYBLUWV3AbekyIi4XGD 7zfSKece+sTSUTAXJEBS0jmqf1mW+f0gjiMU4y7CVO4bG1nwY2D1Xjrz5G0dDWXDFXv3ce Y66y78twR6PeLou1wREEbmr4ELjNOfY= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=JapusFmd; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf25.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.210.175 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com Received: by mail-pf1-f175.google.com with SMTP id d2e1a72fcca58-71dfc1124cdso1150434b3a.1 for ; Mon, 07 Oct 2024 07:49:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728312596; x=1728917396; 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=4ED8sTWekie9637+cHBCmQFpLStnm64j/0llImVgOxU=; b=JapusFmdsG3uGSdVzwi//ujBKcrm1BuTbYKBVGHMXhJl/fDH4Pfp44ZFn+JeYj/GL1 2SK2ijitgJvtGtyPjURS6tYgrbiysy4h6gMMvY3Bb0rvAllQdp6wHsgNVNRal5q8b5l4 lsdMk22tfsm+fn8y6E41dHUCR9UCEmDnvRmzT6dSwujPnBn/RpcKyPtQI36oc/U6hIzk 2/w1x6vpNYLFco7PDT0FPHz+JI9WIXbdlvU/0RC1cnDmgV4TpFViugimCfbjx8th5RLr qTGzmiWTmsb+r+z58ySn9/mqPSJFDqEWgzoyklx01I7AAriXBGyF3VYcAfZn8kHGAQJy f6ZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728312596; x=1728917396; 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=4ED8sTWekie9637+cHBCmQFpLStnm64j/0llImVgOxU=; b=IaOu4p0REq+v/BWf/eJpSoxVLUIJReaH7fwCbroMbb6/3fgaG5c/7b5n7Q9c+dx7IV mopU3pO2ygBztNEpo7KfSU045FbvHWgpJIz/GrkPJJJZucQG847jPkl2OZhz+08ZRrKD TG1cdjPYrTEIoOQ47rSW2Fv87x05yyh9LM2nob+/bMcQAil+Uc37AIzjk9ix5buQlx0d zNmf6dsDAxWgX/yNCzKk5G5r2I4J1AjBpgUITq8EE3E/qFU4ITseZgmqCNyTUD5wGegm P2bV7cTuzqUb0JzKik65lCzAoZCVvfeLUYh1zFudYlh+ILZT1jJjb9f63eq1ihkTr8Pz w1sQ== X-Forwarded-Encrypted: i=1; AJvYcCVF7n3izfBr7P8JJFbtLT7WXci9RhjkyATZfvwBImDJeAHXWIqIGeNBscxZIkFQooFnGxEOhlsDwQ==@kvack.org X-Gm-Message-State: AOJu0Yw2CV75DJ4n5g/KxXXpRBq931x6VegtNve+IHie9M6VpKAvg9Z+ +jS3cc+6hp0N7x5QDIDuBg2TbCKrmZaF7V0FsRuTLcNcFciDe7VX X-Google-Smtp-Source: AGHT+IFSfr8aGR6hntBNP4HQUskHiw0vm/RzkrWx6VpNUZENgDQ4iLauIOQbi4o7xjraSFHRBCGRjw== X-Received: by 2002:a05:6a00:2316:b0:71e:1ad:a4a2 with SMTP id d2e1a72fcca58-71e01adb733mr6378569b3a.13.1728312596499; Mon, 07 Oct 2024 07:49:56 -0700 (PDT) Received: from localhost.localdomain ([223.104.210.43]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71df0d7cf82sm4466432b3a.200.2024.10.07.07.49.45 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 07 Oct 2024 07:49:56 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, keescook@chromium.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 v9 2/7] auditsc: Replace memcpy() with strscpy() Date: Mon, 7 Oct 2024 22:49:06 +0800 Message-Id: <20241007144911.27693-3-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20241007144911.27693-1-laoar.shao@gmail.com> References: <20241007144911.27693-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: B4924A0007 X-Stat-Signature: otix9wp4a439z1yuc4dg3r7m7mmfjf6p X-Rspam-User: X-HE-Tag: 1728312597-949147 X-HE-Meta: U2FsdGVkX18Nk1HQGIwY0IxxJpGaPL12xjtRXZnhFNa6hDh+2ubW7rWrVvMjvY/BCakZ1N1ttBGZ/hhub7jSFiTBiyu1t2iwJmdNpOQm/YpPu2qR6lbas2fgco6LmWAPKnA2pufumFQ9hGfLIzLDDospDa2+sIX0aY1L/08rOYfYRBIQaQkrvMzwyaPx/UCfy/C4dBhL7tsw5JTUIOS7I2L3zA2HSRqRjNSq9uIVyrtiru1jB2FzKP0TePq8VUcbEGWphceetM1ZlyqVmwIrvkPioez67a2WMFvXOh/uLk3m2bgwUjHWL7US4PKWP+4G2WuI6M60zR8d5za3FhA0FeHrOuLDvcxFvR0nCGV8Ma6s6/qAps/oGCKxXFGwS0NH+AL7inAdC/XbdSy1bZ2PNFph5ZYpS3JnnleCQFyXwMgsqY9DPkuS7xzuNN4uLvgve7UDbU17C/D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728kurtwUn427jZ8qMUGaSQWdvPg== 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 Reviewed-by: Justin Stitt 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 cd57053b4a69..7adc67d5aafb 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 Mon Oct 7 14:49:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13824827 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 2EC51CFB43F for ; Mon, 7 Oct 2024 14:50:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BBF426B009C; Mon, 7 Oct 2024 10:50:15 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B48556B009D; Mon, 7 Oct 2024 10:50:15 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9C20F6B009E; Mon, 7 Oct 2024 10:50:15 -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 787546B009C for ; Mon, 7 Oct 2024 10:50:15 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 31B5AA0838 for ; Mon, 7 Oct 2024 14:50:15 +0000 (UTC) X-FDA: 82647091590.03.54D68FE Received: from mail-pg1-f180.google.com (mail-pg1-f180.google.com [209.85.215.180]) by imf16.hostedemail.com (Postfix) with ESMTP id CC3BD18001B for ; Mon, 7 Oct 2024 14:50:11 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="hj/YfPfH"; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf16.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.215.180 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728312586; a=rsa-sha256; cv=none; b=5RXu0KCJi6BZ62h51x2DvxWwE8EDsZik2nGCzIKbFUjYayFKXxumEoX8jOVqiQ+LT1fFwp I/VUnWxWFLzLfHRT/z9Fz+G6n6T94YYX6YspE2kBa6U50psueEOJHj/7ZWgbUJz7+/W5DD kG7Yoem5qloD8K+GvUCvDci5AnldNHw= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="hj/YfPfH"; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf16.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.215.180 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=1728312586; 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=HIW0FE+/krWpB7pTwf7t2JQaYmJp6IT5nrtRUiMSqvDwBvpZVtABA9j34wseylTgMkL83b ftRQIqmVDQ99vptOZz3hGS57CILX1N8s/BNL7e6XHbMpZhHWYdbqAcYrR3rhBkE5XqkkzA pMfY3MF+n4RbvEHjv25aMA547paaZ24= Received: by mail-pg1-f180.google.com with SMTP id 41be03b00d2f7-7ea0728475dso770351a12.0 for ; Mon, 07 Oct 2024 07:50:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728312610; x=1728917410; 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=hj/YfPfHGjDc3Dfo+dFhsCz/qc1FNLgN9EqOa0ZbgLQL8wcDEAlsZQ9p04u49t0yG4 GvHA+wdnvc9wgJkxV1Ly2rVUnX2t1LvKmkQH7bLsa5JLXRrfO/pvxK9ocZL9LNi9f3kM Yyeq8kkSf8LHwlGrBfvSI2eBB1IcwuHIJpuwDOe8L6b07cQOcJvZ6BHKAlGGfkbjOw8A RLAGMfzlIYe228dU+57uhZnyUL+HMFm3MSqKRUUMuBsLyEjnYsSh753T6lradq0ms2NK ZkAKUmWej0lRFaslKOzqJe3Uc5FNZW+trw7pUkHwpFUVIJyHXBUUJDQBbUwVxyMjcscL +7ZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728312610; x=1728917410; 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=w7r+waMWVIPTArpHoV0rw83VtRyZSIfCmP0BY0XF1ZmeOq7bZoMhFQbapsI980kEZ5 XjFr74BnhhNwSL+C9arkM5bcDhsbUUZl1NjFgd6YejXZgpssuRJ9m7hi/w0QpseSm1QQ Li5BY0Wz2YbMtCLegs5c82e8gmUcaB6QIVn9gNES+f5orQJBKeoeuHUvboWwmvQ1JfRx 2mym/uqXoBvFjMtyIqW/F0X4QYbutEwE/bC3FUKo1xbxTGgZNttGvB2Lfyzce6tlKdn+ leVhPJhwyhb66VMGzHXPEDf9xy7pedZ0X4x3MynnFWnKIWMiS92Waryo4h6CuNBmZcQ6 tdgQ== X-Forwarded-Encrypted: i=1; AJvYcCVZ2FHvDYr88Nq+grd4dXgK/ITCJ3yuQg0iDl2dw+G+erlSAitjXPrMT9I8tBHUVl46jJ3GeHtSIQ==@kvack.org X-Gm-Message-State: AOJu0YyXb8aC8YG0xKT1YWybjzXYRI0mPt5ZmNmLMX8n+xUquuK7/9Uv LgvSmPLspgv6CGQiU9JwTQQ84aKaTKCIhXxcEX0QqwiBq25a3xVR X-Google-Smtp-Source: AGHT+IH1T8Z0gS8IM4ef+l7Ymhr8kCvYXDO1WRSDOYONvg5988rUz94lZcz2t9FfyyE+YN0pNKr/oQ== X-Received: by 2002:a05:6a21:9204:b0:1d4:fafb:845d with SMTP id adf61e73a8af0-1d6e02b14a1mr18448839637.2.1728312610165; Mon, 07 Oct 2024 07:50:10 -0700 (PDT) Received: from localhost.localdomain ([223.104.210.43]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71df0d7cf82sm4466432b3a.200.2024.10.07.07.49.57 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 07 Oct 2024 07:50:09 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, keescook@chromium.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 v9 3/7] security: Replace memcpy() with get_task_comm() Date: Mon, 7 Oct 2024 22:49:07 +0800 Message-Id: <20241007144911.27693-4-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20241007144911.27693-1-laoar.shao@gmail.com> References: <20241007144911.27693-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: pp3z4jouuiggracybz6t7zmo6ix5zotg X-Rspamd-Queue-Id: CC3BD18001B X-Rspamd-Server: rspam02 X-HE-Tag: 1728312611-437891 X-HE-Meta: U2FsdGVkX1/K5jTQCcNrqKeRCZh/saTpNnu7NuYfDra3c2mRz5jWjAA+u3WueG4IKXJ8of3o4sVxUpJuRdgMrp2ucEIi3SNXoc2Fu3dBRPFagVaBtccr9WiAE0XpcluJKZaLTJwE3O5RJuXssyOcUM0P0Mjcu32Qxz7kJYuglHUz3BJwhaA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99u+cXoDCBv9cJEd5uNq289pDcQVwHRuV0CCacwjyxlGTDjsW6mN2zEeNKmxTNNPgRFWP/UAa7W3ZPrdwP0uAowhzF/OCGGn166Xtbu4uYvNLlg5VY67eLQoiWStjEKc1eJW7whIK3k2rcWKIKmwIqL0vKvlRZFawahmk4oFUzA981KBMrq8K3ryU/t0XRqWXApfF4V783J17ZEw3QhC4C1Wn7wCWH22my5fwXReyWrDUxcgZ7tDBQVy0M//WkM3ltPH/2AC1ry1TEpvfc94JD3sqxGf/cFbQII3ptlakDct5qQkF7QDtvlZzIHBpUeQruilVhd2GwPUoTnLjRPirdCP30jQ4e8KnY6EMOjLydCc712slpAc2+EOGgfnH/WqFlUamSg23kC5iOIoi0cO1LuLZz3ZKi6CKw5m3jLlicceW/0BotsV8WWdFJHbrhejJg1heVKY8A8SaRudHzqvU4xfiOqprnSArODL3nSg== 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 Mon Oct 7 14:49:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13824828 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 DF5C2CFB43F for ; Mon, 7 Oct 2024 14:50:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 72AF26B009E; Mon, 7 Oct 2024 10:50:23 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6B3C26B009F; Mon, 7 Oct 2024 10:50:23 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 507CF6B00A0; Mon, 7 Oct 2024 10:50:23 -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 2BD1C6B009E for ; Mon, 7 Oct 2024 10:50:23 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id DEBE6C08CC for ; Mon, 7 Oct 2024 14:50:22 +0000 (UTC) X-FDA: 82647091884.01.F326DC6 Received: from mail-pg1-f172.google.com (mail-pg1-f172.google.com [209.85.215.172]) by imf10.hostedemail.com (Postfix) with ESMTP id 09F1DC000D for ; Mon, 7 Oct 2024 14:50:20 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=Fzllpzsz; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf10.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=1728312578; a=rsa-sha256; cv=none; b=zCj0R5qvI79GG66PARQt+90hKq6n3dyfVXGL2L5UrZUMCHYQkIoMREdB+5Ukx0ltLatg9Z iwxIV4uqAu+KgiImkWjVP0DLrCyJZuAPDiQSoCm6wCiMX9bd4zVLPh6KDIrDFGmqzJdNcb jVAf2fSSMOPYZ6ZvqqF/+rwXHujfLO0= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=Fzllpzsz; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf10.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=1728312578; 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=3pLeLSDr18cy7Ih+bLZ1TS4EM1TNwx5KAK0rHPJ7MeyLxTAq7v5ifLnGcIG9yydjTLwH8w CyDT1iklpS9HEXMGbU9hVyVxF8JUyH4bPnEqn2s43LtP1wHysbiA6wHtsVfQI2mSzAxlSl t/JoqnceXo5DA88Dd4NXQpHgRHvmyj4= Received: by mail-pg1-f172.google.com with SMTP id 41be03b00d2f7-7e9fd82f1a5so1045751a12.1 for ; Mon, 07 Oct 2024 07:50:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728312620; x=1728917420; 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=FzllpzszR/WlNgJ8bF6xIwLnhiCwhvlteVAJu2y4QKCweZF6ITpake5nM2UwPzfYKj c3+NFKWajtyT3WJL1p1b9Xk9cbWoZ2RkM1xcjQibJjONhzI3HpiWMUPM9sOJXixSwaYX qyNFjCNMs7yU+dYgWWl45a4MYiTq29B+5UHcgRrizGkerh+TfI/m2Q63vv4CvHygcbt8 q5Wmt06/SHzcmsAuoJ6qwI0WinsWMEYdTYDCsIu84zZSCDwDVbKXgl42MPlbKdmlV8L8 oJVpcgUD7Zk3W+DXi577Fz/+qtiDPufKdEfZnKLxKjniRGfjFUWSHBPXbM3J+2EigRrL BCEg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728312620; x=1728917420; 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=UvVItg4Y5zw7vup4ndk0SDWFDXa8Er8kCOAt+a5oY3Va0P3iuJ4KNm4u9X3ZO5IgMQ WEoTijKb3dMgqVveBFAcOOrBZKCFk2zDeikQzLmIGb6s33LpX6Yto60hY3LZeOLgEijH vEPvbeWIromaqVwb0YuV+A/rbwAfCcxF/fbBwIse2LGPnUjWi5fvHRnhT5pHcWapzR4r AQ5+e8VxE74iLGZztUWS94vCCQKaqb/mJoSbg8b6JCZNVZVgE5T0Q1vTJLl1OogJPdPs YDw1cVUDAeJMfeHb6VUpsXeUr2D4+aINzU/1yo/PISpGQ7E0G0ZFi5dy/FUHLvQ55EWh RyAg== X-Forwarded-Encrypted: i=1; AJvYcCUhuK3HRM8gqtLrhu39N9RVwS9ewsfa0LU+p9Sjw/aZx3bLOvVsnSa1I8+kpUAUuWUhG6qSUxTCew==@kvack.org X-Gm-Message-State: AOJu0Yyo+63CxujOgjSG+VXVwFaHIPVde5Ktzag+1l3KdSq/hxBBIotM LfpvQpBP9FJjXJzAIO6lD85qH3eCPfv0XTzBQJkCIyReOTTc2HwL X-Google-Smtp-Source: AGHT+IGdJ8333QIHr9VJupVLpNtoPXxXbiV/WsIALKPyiyHXyFn3qrINZ3iUgrsX4T7ZKXd0XBWepw== X-Received: by 2002:a05:6a21:9201:b0:1d5:1729:35ec with SMTP id adf61e73a8af0-1d6dfa27a24mr21138343637.7.1728312619784; Mon, 07 Oct 2024 07:50:19 -0700 (PDT) Received: from localhost.localdomain ([223.104.210.43]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71df0d7cf82sm4466432b3a.200.2024.10.07.07.50.10 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 07 Oct 2024 07:50:19 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, keescook@chromium.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 v9 4/7] bpftool: Ensure task comm is always NUL-terminated Date: Mon, 7 Oct 2024 22:49:08 +0800 Message-Id: <20241007144911.27693-5-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20241007144911.27693-1-laoar.shao@gmail.com> References: <20241007144911.27693-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Queue-Id: 09F1DC000D X-Rspamd-Server: rspam01 X-Stat-Signature: ip6qm7fernqwog5raijcm49kprudgstm X-HE-Tag: 1728312620-174842 X-HE-Meta: U2FsdGVkX1/wabLltT3BjfHj5A5O7ZR5wq3t8MxGlHruGYghgi9iW1C0oZ4bpCoXmMMjj+c3yVvkviPGpkGUYVFQaQ9qMO30ksy50bf60dHqYI2+mfSIBI8Mz5FAnBVphA4sGegzEOF08Sk2sRM7goIwzqyfHFUMImWIiB0+nYcW5seDUJjue3y6uipf8TNOHyiJv4xn9P4jsVuIkt8EeRl9sCcgu7QtqOjGvPLrw83DzkFi95OYn41TacempPqb45vPdZAkJg92EiwwUafTmbuzCpYKPwuyj7T7Rr/78zSm4fVlbkBx3zlb4YqaWqzO5ZAzRsdhdFXJ5IsJViV2tPQfajQQhqssP42AJLCPsaa25R27w3IgNK35lAHi7CBf6GjlWX6W81sjsGfmI52Oo2+LGjNrnnvgksMLoF14dC9ylmFtodJ5OCLIfxIuURRl69SjjWAbjvELOLJ2xgWl7xn9tMCNCUpcszx7dXVcmPovL0ivFrE9fuHjQeBNGn0l7EwVNYDakoQo5ZIVPMEzeMilhZNBrGROvgryo6+DLMgtpFy6OfHf0O4nQQPMX9TnbEmg3IKcTOPbFtmZlGySGTIVhaXRtwAkgwFB59MNc/MpMc8Zt/O/WsKTsuGQhSjzV+kRyqeq+MxjRY8G1UB/jm1X8Uwsuy/mRWFlXpyzAuand9HTgKo8KnkHJwG2M9q2+k3ebfEnmqmScvCK5rRsMnsrcwb6vsx6tPTC8t3xhqK2hr0Xvmnb0gU0oi03r98ogmAbmUYKS+svPX67r1phzIbogjhb+usAoDDPuqwibDX+JR7ByfJfzmpAFZ2RpkxzfOIjq4IEzzfOOVuOveu3Wadw6VQXXjDTUausbTjUPO6Fb8bo3t3xicGUAE+IOK9GiSGDOEUcZca852IQTtuUQpZH8vH+JH+FGIje8L/PugpN8jkXC0ou2dMykq6UxNhyF+eQ8pW8YKQrJRWydRS pTDl8rpa cr2g+YqIKHU3BAwGjs8AWb33AJnmAR8Wx1eg8B+aTjV9vNcF1g/qnNGSKlSEb4Tcas9hI0Jarn2gsAdC4rQH1rheiLoprFK/XXU7JjoEaAJ1EoQOJfoZTVaDp77fxjn0cqJbwWPYdZIMgaNEUD9hJfWvMEouDR2rm24A7qJSwhj7/pmL1Mfcpnxju78nG4fZp11A/gOvCuePyONM4l1lHOaskVU6wLlbybAKnyxtWs4CcnyVoqhHsXXsE8qcctvWCiD5ykdMuvCvs860eqdbjNkJuJksOXZWERowMC/vWYYSfkncMPRthyyW8FtnamJFlhPVLBwJdqHyAh+r1WvhHUmI6ZCKWQ/Su82IGdx1xjGT5PjtgKmbQv4gE5RspWmH3r4KifM2buTtr4GAI5uOQ9oRmmZIXACzl9CuaM581rjfDs4MLTJ8UMnPo7X4tVYX4XQ8xm9OT6GlqkZjCABIM0M5jbqY70nZB+7H0ONls+ezH4/7uneZoeAgWXJv3YmC90VEr 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 Oct 7 14:49:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13824829 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 056A7CFB441 for ; Mon, 7 Oct 2024 14:50:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 83DB96B00A0; Mon, 7 Oct 2024 10:50:34 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7C6516B00A1; Mon, 7 Oct 2024 10:50:34 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 63F306B00A2; Mon, 7 Oct 2024 10:50:34 -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 419EA6B00A0 for ; Mon, 7 Oct 2024 10:50:34 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 0310780766 for ; Mon, 7 Oct 2024 14:50:33 +0000 (UTC) X-FDA: 82647092388.13.2115C18 Received: from mail-pg1-f182.google.com (mail-pg1-f182.google.com [209.85.215.182]) by imf19.hostedemail.com (Postfix) with ESMTP id 1FCAA1A0008 for ; Mon, 7 Oct 2024 14:50:30 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=eSMYY725; spf=pass (imf19.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.215.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=1728312562; a=rsa-sha256; cv=none; b=NChy3KpF9oLy1GwN/qlWOHHCOHeLUqasA9+qc+yX0J247DRhhAXDX+VfHcSf106QTok13P ypLyr4+XvlCx5vKeOWyVEHBV3a0aji/QvaDkO6pit/zdX9MTJa86wI/NkgMg3BAPco3Xdu pTOH0aIJDKsNAOe2kgZ6jmDzt4rRBV8= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=eSMYY725; spf=pass (imf19.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.215.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=1728312562; 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=rOSaWXUvmAwO/V+X4Wf8hE6beLEX5jMwcQWbLUwGe5Q=; b=jydqPwPAnvNl5SOD4XRwbTEmKwR+dZjMmvO5kZzWKOj5m4t2ME3j9pGHXhPv9kgZyHUgWq LmAuKPLt9D+eYqMXW7Cb3O5P9i9rfxua0GHFIJ4jyNcGyMp63TeA57lGQvGNxkfuP1yLDc MuXciAJdMNIoPLJdZv8DFnh9LHNU7es= Received: by mail-pg1-f182.google.com with SMTP id 41be03b00d2f7-7c3e1081804so1925682a12.3 for ; Mon, 07 Oct 2024 07:50:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728312630; x=1728917430; 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=rOSaWXUvmAwO/V+X4Wf8hE6beLEX5jMwcQWbLUwGe5Q=; b=eSMYY725w8kHPzSFFq5Jblt0sOa7en2tp6hrMPIlIDNO173y/kB7tIktG9j6npxIzk kwSIHKcb0Xt/jm6OeD472K9C9AznyR3ckhwuc0USTywEmnL3J4ep1cQLNwmyrundz+LO +Q6N2HjjQRejy2TqVI4csrsuvYFEq93mxHbnW5NRwjr1wxLtN/nElZ3LNr6vaXf4alvv a46egmV90Sm7Afr7YdjZvifdFPp5XscO2R5VsRU2EwFB2Lz0p2WhXS5lV9DnvGNjvM5y F0WerHl5JJmtLcIaax7JZLjpu/luQroZlAiwdlGLGAZE8mdBdqn6HKjhvpLVjwvgTbwt 3QDA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728312630; x=1728917430; 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=rOSaWXUvmAwO/V+X4Wf8hE6beLEX5jMwcQWbLUwGe5Q=; b=rp0wfD3I74e7Ti13gxV/abX4kik+cuGT34kBU+fTd44vwxI0kuxr63Zdgr/jHOeryS zXSBNn2MLFQVqYoJZ6SOg+nMXqt87F8gWoBQCfFSnt4nZRTgTyC1mN6ToW4WwCeiV39r tTZfxspnyb+PW01skRG7mwifkoyGXD9vYve3O2THmThRaLUoTfV+CHRr+VNj74HicZjs bjgkzb7Kv42E1c7dmkBpVi/gy11rjI+l1/5wTFTGgiEO+ja/NtW3kPKTH1oZthlzDFgB vL+Sn9XThW4pehH1eO5mn2CGwkCT3gTPEszacCHHCtxflLbrJCG4APattnQ0UJ4vODIO l78w== X-Forwarded-Encrypted: i=1; AJvYcCVCiOg4lFV/2kTx4n3aA0VqCe+TKR8cqHKHpMWObEsdWwxqc+//yYpaSKJCtTpYnOrvZ9Cynqor6A==@kvack.org X-Gm-Message-State: AOJu0YxhLY6gfFv/PvAPZjeqJhFtjo2GXW3kEhdqJQzUJldHxKFjUhCD FD/EsIHcvTclMnxD2+Qtv8HK4wpUtMmdWQUSscxwUUlJs9pZiLqh X-Google-Smtp-Source: AGHT+IHqOdUcOlcgWNq7VMpvl6yahiIrnpFyiw3ojJd2mp7haZXT77OIrOQnvCwSUS5AAO7Cn/LNdA== X-Received: by 2002:a05:6a20:9d91:b0:1c8:a5ba:d2ba with SMTP id adf61e73a8af0-1d6dfa44e04mr19075009637.22.1728312629925; Mon, 07 Oct 2024 07:50:29 -0700 (PDT) Received: from localhost.localdomain ([223.104.210.43]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71df0d7cf82sm4466432b3a.200.2024.10.07.07.50.20 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 07 Oct 2024 07:50:29 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, keescook@chromium.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 , Andy Shevchenko Subject: [PATCH v9 5/7] mm/util: Fix possible race condition in kstrdup() Date: Mon, 7 Oct 2024 22:49:09 +0800 Message-Id: <20241007144911.27693-6-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20241007144911.27693-1-laoar.shao@gmail.com> References: <20241007144911.27693-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Stat-Signature: dzanyaoiziywp1dodbyyrh5tin8469qr X-Rspamd-Queue-Id: 1FCAA1A0008 X-Rspam-User: X-Rspamd-Server: rspam10 X-HE-Tag: 1728312630-807507 X-HE-Meta: U2FsdGVkX18y8IYzAgfFeXFJjkr1muqbJSIoIewzDevMCWnYnFcWapQTmPlhAsFsKGs/W8IF/DGtDzjYyrpF5xGK7Mapk25amM3wTZ43DVA0HdViopNNQ2RbVJwgA8TwFcAtWjuowJluCeWfH5R/BcpayR51zSYhzAw0rBxeguS4k3QA5zal++L3ZWZQaq8sdfMTQE93HfAHLSbpzB2NsxTBKEYabs04xV7rsWcwdFaB6dcCGJHaFfG0NB95E0Lp6dOnyHYUHTp9JZxDWP5BoOsV9vQRUO18JlMoTiS7YgHXE25FKhkWEssrKCuJ253r+YZO7xx0P8APGt22K0tZKDjtyw8Qb/ZFde6jQKkDe1JOq0kUgV6lkvswL+xy2e5dgm3DSbaJk9zR0mOfrB9go7e7VgVeZOvzT9Kd9cVut34gms8LUYxkWYDoSe7HoVNgQJzMDLzlS8wGSNa/465Y58rmAuMH1UBAFVgbi+0mrm3QWW2EPkkiAJZv3iH9N3JkKuEbEjMnnAib7gdYC21XW04pWPlhJwyEL8ehVKXm+HQTs5TfhP5J3xApWdt8p/1yLA4RxuyCFFBFld6HU7XRaHASr9ydcUP2S4caZkDsLVFcb6nkfFzII1iIXltXpq+HF0/G2aj0jyO8lCQfeDz+Q7sds2KmxDY88rSy2c+7ZCQEH90cGJySLFxpD1M/d2V8bj1uyRQcd5s8VIgEXe+9W1YEKqzYIsjR1z5yU42j8/WBPLi7/sPBzK15YoTwsJyXqQuh7pbkBfLL9qDEUR6fX9Juw2oMidPBn84PRN5DVssMwwC+u5DsAHfCFBaJjBwupa1UBP84inXxAHOw+YYbrI1uF2+gOIbw6LtizNFDOABDszEcj3zJA8B+VbzaJ7zKZ8GynoH8WQgkOA/+r+IzKHXDDdVrqk/5jWCKgqnRNS9F4aAhX+XghYeKpyqlwcMAQAshUzyjtfed6ciss1O 8zdmIIxw K1u2ABRyEa5+u9eYmNB6HuglJu/brlhYdmylGEc43PVh3bOmu+A1jSMv9r+FCSglg6+xlFFzYxKIQdhl/u3ylPpC7YiO8u7nMwElMt3Yj3BoNSAY4E8nXmrEpEL1ccmJNmWXne3L0r0U2e3JGEsgaBsBf3Ca+DcbHBOzIFu9UcF+jyI/NPGpZsULegrHpeWKHYFwb7izK+6l3BUjMT7Ob+zyVZPEYeDwxwjg9lkDqhNsdeRqjX3Vjc6XWDq1U7Ts0BY+J+Q1W7yoJgKJN6hAP6OX8mXDTyvySHP8+44yZ7EVwg/TqzwGq/YJWqnY9fYRlE02rFadCUHUmpjAtdo0vY13inqSon7SsBYW/k0IiwYLbaj739dB3O5paATV7NYPAI4HBY1EvPUra1B9GbpCCYpmYkkFWUum8hlYXNNeqRGb/JRKRknbs1lX/ZVV01KZfSqzcZpvBjwW+Iin2IItCDmNuBcFjlvAR66+QHOasE+Hct3JPnBjdeHOzjSIb6N73c0RAkF3pa/dT8udIcMkAte6y4+RrcQeAxXVnQAheZsSN1Gq6eCqUUgGStQ== 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 condition 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 calculates 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 after the memcpy. It is worth noting that memcpy() is not atomic, so the new string can be shorter when memcpy() already copied past the new NUL. Signed-off-by: Yafang Shao Cc: Andrew Morton Cc: Alejandro Colomar Cc: Andy Shevchenko --- mm/util.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/mm/util.c b/mm/util.c index 4f1275023eb7..858a9a2f57e7 100644 --- a/mm/util.c +++ b/mm/util.c @@ -62,8 +62,15 @@ 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 terminator. + */ + buf[len - 1] = '\0'; + } return buf; } EXPORT_SYMBOL(kstrdup); From patchwork Mon Oct 7 14:49:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13824830 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 A5141CFB443 for ; Mon, 7 Oct 2024 14:50:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 41E4B6B00A2; Mon, 7 Oct 2024 10:50:45 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3A7AC6B00A3; Mon, 7 Oct 2024 10:50:45 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1FA256B00A4; Mon, 7 Oct 2024 10:50:45 -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 F062F6B00A2 for ; Mon, 7 Oct 2024 10:50:44 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id A2026ABE6D for ; Mon, 7 Oct 2024 14:50:44 +0000 (UTC) X-FDA: 82647092808.10.A26F57B Received: from mail-pf1-f181.google.com (mail-pf1-f181.google.com [209.85.210.181]) by imf09.hostedemail.com (Postfix) with ESMTP id BBD2E140004 for ; Mon, 7 Oct 2024 14:50:42 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=TVww5mTh; spf=pass (imf09.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.210.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=1728312465; 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=fBFkTCCgw4nKzS/UQclQt52OlK7datrkk2Sl/mMl9SE=; b=1sZXduMiraRnfPcmMNmIQsDpcNi5j9JyqtXx+w1xKvsz8i/ZGsbUGDMlR0zZNEd3teAozB j4jH3D2nPLtl4BKLhXrgh7fmq9iJNV3AaU5zdQ9z5W7pEb6eMM0ccg7S+9rfcWHTg9sRxa +KWYu/1+QN+wpt6ouNdHho83upe6FTw= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=TVww5mTh; spf=pass (imf09.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.210.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=1728312465; a=rsa-sha256; cv=none; b=4Z0rD/dC5f2PAgYErZ4C36Zuek0wP+IT5KSqlPhZCdZobKsq5rzNXa2B8BPqR5CdmUasSq uUokSt2svyxvm/xWPiJKlFlaB33nZyryTkFILFsarr0eiwHHh+QbPaQbAS7tITILdo5D0t U+X3WCB9QEnmuIxrVEgkaEc+wBr3ra0= Received: by mail-pf1-f181.google.com with SMTP id d2e1a72fcca58-71df67c6881so1727686b3a.3 for ; Mon, 07 Oct 2024 07:50:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728312641; x=1728917441; 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=fBFkTCCgw4nKzS/UQclQt52OlK7datrkk2Sl/mMl9SE=; b=TVww5mTh6AL05q1SKqW9VrY+0XmX4uOWdLPrgoR8D9WnLLwyboTy2eA0Zr4I36r1HM e47tms/macbSVoZdg1bxtQSMt1emUnfdSuZ/NS8JPNDAcAF9ja6hSEgnr2gXrVZbY0uD 0VgoIx3L0VoVtfS1t4nsJmk34jNs7tN1v02BQ95VElG/+8YAEfaSTP+lGJlVkIAqadt5 Eutj4gAQSCRCiZJXR87/Fs7ha/BblbvWtQvQhFyVgEUrCYOdcSK05xEJ9Cwn77Dph8i/ pSzjtZT/CpQg3bGY+6J8t8MHuf+ktqwRortDCuwSvjr/41LqevqF1Uc3HuuoL+e/3FZ5 hPqg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728312641; x=1728917441; 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=fBFkTCCgw4nKzS/UQclQt52OlK7datrkk2Sl/mMl9SE=; b=JTxxu6qw+EYJG5eotVV7fdZDIoy4gibKkHyHN8Gl5YHe2flj+whfLKSPnneDDv49/C XX00PiBjD94DCsL7QeIAZc9TkGwsA8hoIEAK5IimKgb68oAf3zZ6PLighbNxpMZrTA6U eClR6HmeA6gEhPDcvFVnYa2rlmoxKfTzBNz7Coy2/vkgIADNW4EnvlK5ooUYj/DOsytU T3MWGOz90vnGAK+fu9z6XcXjP+rzMpbpxIovkIsNqdYC4hMQnOnEIOJUeWG1qCpbvDTy vtww63RvVSIa2B48oWn5a89I59qVjhjEYlKMgoKn0tdZlwh3iEdjb7XDrvpZTen6Egum pXug== X-Forwarded-Encrypted: i=1; AJvYcCVFkj3/AEGetUwTxBRa5ONMrJrLMb4hyVQpOPwzu8jIgpGT35SJfvYTuCLuZKCVZDv1YzPM77zVcQ==@kvack.org X-Gm-Message-State: AOJu0YwFnGz7eWb8CHccOAtme3icEaFY1Y781OOdYcHVWF3jeQLuvlM3 M8z5KRvq1yOgVgkQEJ/YBnvdiZj4TncGwzqdqp9SiKsw/SmUvM34 X-Google-Smtp-Source: AGHT+IGsy7J8upAtizdqT7cwdExa0Ot6GSfKRh917J35OaiwbZMAM2iFolVRA0oRI0wvfYtn8vWlEw== X-Received: by 2002:a05:6a20:9f8f:b0:1d3:e4e:ff55 with SMTP id adf61e73a8af0-1d6dfa22f17mr20436900637.7.1728312641423; Mon, 07 Oct 2024 07:50:41 -0700 (PDT) Received: from localhost.localdomain ([223.104.210.43]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71df0d7cf82sm4466432b3a.200.2024.10.07.07.50.30 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 07 Oct 2024 07:50:40 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, keescook@chromium.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 v9 6/7] mm/util: Deduplicate code in {kstrdup,kstrndup,kmemdup_nul} Date: Mon, 7 Oct 2024 22:49:10 +0800 Message-Id: <20241007144911.27693-7-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20241007144911.27693-1-laoar.shao@gmail.com> References: <20241007144911.27693-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: BBD2E140004 X-Stat-Signature: 8is9yjwf3a5jexc7s9umwgdo4y86n6hg X-Rspam-User: X-HE-Tag: 1728312642-977272 X-HE-Meta: U2FsdGVkX19RgqlphE5uzo+IpSohQ8pOdeQhM7PMdbsqZl4Ijf+/Bgsb4Brca9Bq98pnCqSnjiss9bB7vrstEKvyoCijyxnSiYsRVZ6V3iJrbsn2TCAzkGJhod5DNL+gLWF+KbCPXYiaw6O0e4maHNo09sV7fKrhq5eA0LwyR2M+PLcrGwPHrpy1eBrPWkYYYEaCgepuxpJfJQTFNNvFWVBf8AC1zKL3GvxS94T7jPO5IUbJ6fUu129LcJgkJVSNtz08AdcVJSHVQRHfWcUcj3/FR+KA2ltyEzbIK6gi/ynkFCiK3sZndZCdEnbjZCUdVQ37szqe9VZUwPbafqy+O4u2PWIJhu+MNzzIWCGglBm5a/3VkHc28105HsrfKmkPiPmrb/ZQ7vt9GieVQ77OeFinioaGtoa3ev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vCBBziMlXuy3n17wjEwKBEvoypbSZ83el4AJc4CZKgzoUuwF4sHvXBfwIHV9+kQdzDsmlc0xqthDMdGAMHBuqzIhlqB0AJ2I6cpkdqlUJQsKKiLKuQtS6u0S6WlaVvOaT8RmGWArDMt3E9w3REBsRZT9aUiOVWR35auvxX 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 --- mm/util.c | 69 ++++++++++++++++++++++--------------------------------- 1 file changed, 27 insertions(+), 42 deletions(-) diff --git a/mm/util.c b/mm/util.c index 858a9a2f57e7..c7d851c40843 100644 --- a/mm/util.c +++ b/mm/util.c @@ -45,34 +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 terminator. - */ - 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); /** @@ -107,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); @@ -193,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 Mon Oct 7 14:49:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13824831 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 F36C1CFB443 for ; Mon, 7 Oct 2024 14:50:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 818016B00A6; Mon, 7 Oct 2024 10:50:57 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 778A36B00A7; Mon, 7 Oct 2024 10:50:57 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5CAB56B00A8; Mon, 7 Oct 2024 10:50:57 -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 3419E6B00A6 for ; Mon, 7 Oct 2024 10:50:57 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id CCC98A17B1 for ; Mon, 7 Oct 2024 14:50:56 +0000 (UTC) X-FDA: 82647093312.10.24F533F Received: from mail-pg1-f176.google.com (mail-pg1-f176.google.com [209.85.215.176]) by imf30.hostedemail.com (Postfix) with ESMTP id EB2678000B for ; Mon, 7 Oct 2024 14:50:54 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=bVKYqzBU; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf30.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.215.176 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=1728312507; 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=7VwBx3mFnKYzOzVHPezqvf6E6t3GSTY/lMuePwrb08s=; b=4Yk3acYxVl2g4IFAkq/m5A/fhknWHw4HXTUMtd13RY0Ai0teudB8Whyrn0lcaO1k58gNC+ 1Dl3EbVhqwoAhG8fwJdiwRaXicT2lXKw6orMr6hwYNipAIHmvw/JkEUA9bDEJyyVLCBh2z Rg/O9I3IONMDxhsqZ6Rxm3sLh1aIGVs= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728312507; a=rsa-sha256; cv=none; b=Vkm1if8Dd5WLDtkBDNLKl4VRaeyQwb/yyYAmW+9etuDe4zHt5R1dw4rZmrJYNkjwFMfCLK IIO1+EZMd5QfFvGrsC3HNCOeYpywqXNrLduvc6egT1wCUHJvrNLEed7NW/EBn6ggeSC/+6 XffLUYt836k8BWJF4GfmZKBOhxOJJoE= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=bVKYqzBU; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf30.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.215.176 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com Received: by mail-pg1-f176.google.com with SMTP id 41be03b00d2f7-7cd8803fe0aso3101988a12.0 for ; Mon, 07 Oct 2024 07:50:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728312654; x=1728917454; 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=7VwBx3mFnKYzOzVHPezqvf6E6t3GSTY/lMuePwrb08s=; b=bVKYqzBUtxOs5xby9W/Yz2BOrYWAr5HIG3gojQxFnksXC18MJnB5NVfjSDch3Q63pY 1djecYzYrTjfURtJDJrX63G3N+5eE+fhtsmYvrGIEVfYL9dn1myGOnP69p2QJPMcxMmS zZHq/OWaPfSfu9jsS7/++TpC547Jg/tTbQNjaj6PN59cw/vCob1ysvPw1z04lkyVw6gV R+z4XXaBxenYIDzzFtTCL6ZJnngyT8Rr+gtxvBb1c0tBLimTZe/sM2NC+6hETFIMrneF 9x2uuFYa3G81yLhAcePH+Ym2wtk/ZdIsWjGPo7mHOMNW+VA2VeTgMDGIjXsawWCYD1cr SAqQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728312654; x=1728917454; 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=7VwBx3mFnKYzOzVHPezqvf6E6t3GSTY/lMuePwrb08s=; b=Sna5vSffNxAY6hIw7tCMqGM7KD1yjXQ9/KlO3mDPgPyt2VOMjCMIFWgmbQkQz4DZ0j RGX3yooIz0Vcy4G9kyV9Aiy7K4sE8QwUFQrPaE3nBecr2AbyIWavp8W//Y7ML69l5+y5 xIgKUeIdOUJiRnLpBjPHQ3/QaNJncn81Gx6Vr4hMmgZ1Ljc/Mmb2QNHXXebmgoyrczFP mUgRO7ZpDnnOkQ8GDEHXgrpZBFdQWGE6y0+hAbtipbAwp7hS1UfNmSt2ZWT55un9Kavg TolR+9/uoCX1bUdvewh1A72qdZPQkzW3qbo6UysalLYUjU0Twj1uYHOwZ869HPQl0y9d zX4Q== X-Forwarded-Encrypted: i=1; AJvYcCXTD5T8ml3wfUZkU5sBvFfC97cbhYAbjDOc75xcY7bON7MR7uptppERJG751pg0DZS5IOsKNDjd9Q==@kvack.org X-Gm-Message-State: AOJu0YwCC/+Yc7vKRuD0xAcWgSkn92y3/3fbpF6ly5zun0R19ZY4R48Z 7fqOn2XOoJ7Rjsi+CgGTREbRMZ5CkcUrp1dB89Iu22+6oZ2aFCRd X-Google-Smtp-Source: AGHT+IEwpXCzalPlyOY9Cdfli1GLzxvFvEupvvujLKDDOEy0H0EsRpGoLeoHLvpBbBHNCnkbeHqgcw== X-Received: by 2002:a05:6a20:9f8f:b0:1d2:eb9d:997d with SMTP id adf61e73a8af0-1d6dfa23bccmr18227823637.7.1728312653634; Mon, 07 Oct 2024 07:50:53 -0700 (PDT) Received: from localhost.localdomain ([223.104.210.43]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71df0d7cf82sm4466432b3a.200.2024.10.07.07.50.41 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 07 Oct 2024 07:50:53 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, keescook@chromium.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 v9 7/7] drm: Replace strcpy() with strscpy() Date: Mon, 7 Oct 2024 22:49:11 +0800 Message-Id: <20241007144911.27693-8-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20241007144911.27693-1-laoar.shao@gmail.com> References: <20241007144911.27693-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: EB2678000B X-Stat-Signature: ocndtj4moty5d9d5gd3ct8wznf7jsjyd X-Rspam-User: X-HE-Tag: 1728312654-773998 X-HE-Meta: U2FsdGVkX1/KtdemWLCOF6TJiAfbdm/Px/iMs4Qj8zMP1ug7CcaPFSjwn97wZeiNHZZHwLA83Lt3LBgN5GKKTJgrQaTQzLWLEfxM4/LlfP0o9OzzHG/u/Lfeal0FrJ9Yeo1ou87tdsvEZqAyO9cuA4dmtXZ1AfC09QrPcMUVvZ5k0oJwBHZAQzC4ZCxThwVN2NLPiECxWSRtbnKCNYPikmwsJCtPqoGPsyApv+wvxB9S/1jy5UvqoQbm91ldtb4rQRKWDZ08S1NNLF88vMKHS8FQ37GDuHxVAgwSJe2Dh0covZ/0vNa3HLCKnRSxsHQPQ3YPpvfB7+Gt/XJJNidNlpCIMADI7o9fIViGWYLo8tLQZrp12wuCV3xKVruJiXDZQ/p0H0EyIqqtObl9P6drGRQRaWRa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vqhZGfgP/yg8wTYx26/FFhrG31tuzr+rk8DiCWYp+4UPfRPeYdXDzOWxDsSSD27z4= 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. Suggested-by: Justin Stitt Signed-off-by: Yafang Shao Acked-by: Daniel Vetter Reviewed-by: Justin Stitt 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 | 6 +++--- 2 files changed, 4 insertions(+), 4 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 6469b9bcf2ec..9d4b25b2cd39 100644 --- a/drivers/gpu/drm/i915/i915_gpu_error.c +++ b/drivers/gpu/drm/i915/i915_gpu_error.c @@ -1113,7 +1113,7 @@ i915_vma_coredump_create(const struct intel_gt *gt, } INIT_LIST_HEAD(&dst->page_list); - strcpy(dst->name, name); + strscpy(dst->name, name); dst->next = NULL; dst->gtt_offset = vma_res->start; @@ -1413,7 +1413,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(); @@ -1459,7 +1459,7 @@ capture_vma_snapshot(struct intel_engine_capture_vma *next, return next; } - strcpy(c->name, name); + strscpy(c->name, name); c->vma_res = i915_vma_resource_get(vma_res); c->next = next;