From patchwork Sun Aug 4 07:56: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: 13752524 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 BAD8AC3DA7F for ; Sun, 4 Aug 2024 07:57:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3318F6B0085; Sun, 4 Aug 2024 03:57:08 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2BAE76B0088; Sun, 4 Aug 2024 03:57:08 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 15BB76B0089; Sun, 4 Aug 2024 03:57:08 -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 EA0D56B0085 for ; Sun, 4 Aug 2024 03:57:07 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 6B1C11C4B9E for ; Sun, 4 Aug 2024 07:57:07 +0000 (UTC) X-FDA: 82413807294.01.CCE3406 Received: from mail-pl1-f171.google.com (mail-pl1-f171.google.com [209.85.214.171]) by imf11.hostedemail.com (Postfix) with ESMTP id 7A35440013 for ; Sun, 4 Aug 2024 07:57:05 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=V3eg5rR9; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf11.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.171 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=1722758158; 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=3hHqe/dVMoVlxAlcEkx88W0WqBv09P0jej1HBEqhafw=; b=a4v+9RKO06+Iz77QuO2pzocx2sqouo+tHtWEeHTBvVGRtT++1PFHmgXHWX/YSyyVbNFdYY PVRMkVbeEpNce3gnXgUaxR93JtbkRoTaKgikV8DldodTvonrFhc+QbrK6xsR5Ak6q9yJGx VIWzrNze7JGgh9XYxIrUdpI/3SDn3kk= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722758158; a=rsa-sha256; cv=none; b=ka8h7z/v90MZrk2DpmcuufsZ1XNoVPXn8c3NvgjR9yG4e6FaSBrN7T+r9geZIqELuDm+bV nMPmSOeTIRMpfiIUrWdMm9KTMkd1HbODAqB2nNGd97DbWhf/OAnEJHe4gyxMaB2wJu2h2A AqDaJNZa6vu5psIxYJc7wqhakkW03Ps= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=V3eg5rR9; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf11.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.171 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com Received: by mail-pl1-f171.google.com with SMTP id d9443c01a7336-1fc65329979so84365885ad.0 for ; Sun, 04 Aug 2024 00:57:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722758224; x=1723363024; 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=3hHqe/dVMoVlxAlcEkx88W0WqBv09P0jej1HBEqhafw=; b=V3eg5rR90/9pvnaOESiHD+NQclDf2oGjAsCnGcdC/zufrx8FX7jkq2nsJeAjFknyFd Ty/rxAJkDdxr9KVIARocNTSNSQUlRZ6+o82iV2mJvTIQBb1Xpkk0s9WHU/FPDvmy5r4D oxA5TxkWY6kj0lIDRAFQ1r8XuspCcnq87vxOAWmViQLRwIFDWxXJ/QztVOf0P81uD+hJ 8inBUwRrmyqvTzqXmtVoVSv+Okkf5mVFRpgUO+lKLlAdvD7I2BE2M58qi4EsiRkbQdZd aiVOFe8C4y+6IBELl2mQZGZvcGz3sRe8Ki6EP35tjHR2m8Lr48TsfswBP3UzNzqd4Tb9 0ruQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722758224; x=1723363024; 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=3hHqe/dVMoVlxAlcEkx88W0WqBv09P0jej1HBEqhafw=; b=YocaeUg+PKace6N08c0p0lJfyR/LIihcR7onruY1nas4wCsYbqzA5XLmzzccSNKC+Q F6FIXjFx1xWqO6WHi/QH8APxc2P/kSESA+/wMK1/9LrHPE1jkvkGbiu8g8NJzZoGBbTF cKMqkKcUmAcOT8bMZTdH0p0t6eCB5W5fxO4o+bkd+s6b6ECciMzzSdNg7E3b2HIf3thD 4il+U8zuzyIfgontACKITm5I6YMutHmevvvCjbZHa2GXHtup5OLRwxMD0Ohq7Imc9MCB ubELXnwf64iuYGVn4HQKlnErO6Opvj6Zui3iE0fPlDhvnU2ZpuWUjQadz3mcFXKTiz8/ 9eWg== X-Forwarded-Encrypted: i=1; AJvYcCWjQecLp7P+bELhS5HYPIfidI5NgSFD8gctWMqzmW0COWw8MGkH5Jegd1b6NntkcCIlMosdoaxbqjQ80ZYDaOEjdtk= X-Gm-Message-State: AOJu0YyMEAtAOlPT0DWWQGYiKRWJO87CoGXKF2iCuE2MY8i/Jai1+Dvr ch/pgAZ7ae/1cpIyyX7HYpaGL4lSTAwT47EUed9YrmvJ963gJjMP X-Google-Smtp-Source: AGHT+IGGd4NHuT97uC6xOQQvHv4GaxRo9+Vf+VhHCShMz8IgX/0qP5KyCL4xcS6MgRK6pPd/R/ObqA== X-Received: by 2002:a17:902:f685:b0:1fc:719c:5f0 with SMTP id d9443c01a7336-1ff573e75a3mr134700565ad.49.1722758224055; Sun, 04 Aug 2024 00:57:04 -0700 (PDT) Received: from localhost.localdomain ([39.144.105.172]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ff59178248sm46387605ad.202.2024.08.04.00.56.47 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 04 Aug 2024 00:57:03 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, catalin.marinas@arm.com, penguin-kernel@i-love.sakura.ne.jp, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, audit@vger.kernel.org, linux-security-module@vger.kernel.org, selinux@vger.kernel.org, bpf@vger.kernel.org, netdev@vger.kernel.org, dri-devel@lists.freedesktop.org, Yafang Shao , Alexander Viro , Christian Brauner , Jan Kara , Kees Cook , Matus Jokay Subject: [PATCH v5 1/9] fs/exec: Drop task_lock() inside __get_task_comm() Date: Sun, 4 Aug 2024 15:56:11 +0800 Message-Id: <20240804075619.20804-2-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240804075619.20804-1-laoar.shao@gmail.com> References: <20240804075619.20804-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 7A35440013 X-Stat-Signature: ywfcqnczpmg7yme4ogc585kxkkc4iidu X-Rspam-User: X-HE-Tag: 1722758225-451335 X-HE-Meta: U2FsdGVkX1/cdKx2Tqs/MKiSTCF1/pspPS97Bb6BK+zcosBcf9w9Irg7ynHFVnVD1Dk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3iP31uy4QZHMo/Mjf0grZKx+aD/EYbDHaGufS7QX2fIdS0e18mJoOzSF0mL3zeTbR+EGTgVXiBzDg7az3KCWurck0SDbMIKNgHQ1+WMEK23/tX/CqgglDAtSYS+K2rwb/8bVVFq1PcuugahCQCP+ft2tfeXb/t3IeTybrS+DNMOPrponEApN6jEwRVcNVUfb9KwPTFQ/yEV6wuegnIxiVxlXtEcAha8QLvzfIo3J+W01+hfH1mTayjQVMEHt+3F29h7OVw5RBH/xqcsT4RpkRWY2yIBpgbDaGBjWmyDTliul8vBIr+4CNhBslsCvRCupA0cH14y/jTakPtJUbrj1ceA0lEx0sSEzeUQwHoLPncFnz7YjWnfdY1QZaoLKOWd/Q3QLVuUHYDH6u4b2FXfNoTnAKtw5LTvEn2MfnFQ5NUthS6zEhnv2JWHln9OwYmI1Dn4sk8GGQZj5ZIV+0HUc7J1w7ikrGHAqmWQkj7JbLRNXBuNKJ9urja5Ca5DkIvcRs1X9TlFTeWapCj+7NQGT9apm8MZBSp8XJWttEQxEbKEzlC1PAxYQwQriY/YV+rXWbOAu6w/82hVWtGXbW8MrPFh3mz1HHzEAKIOXrcqBVX8qOgOtnh2sz6xDkB5xsdygCYGMkb6hZ06URzikx4qkQ= 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]: Since user space can randomly change their names anyway, using locking was always wrong for readers (for writers it probably does make sense to have some lock - although practically speaking nobody cares there either, but at least for a writer some kind of race could have long-term mixed results Suggested-by: Linus Torvalds Link: https://lore.kernel.org/all/CAHk-=wivfrF0_zvf+oj6==Sh=-npJooP8chLPEfaFV0oNYTTBA@mail.gmail.com [0] Signed-off-by: Yafang Shao Cc: Alexander Viro Cc: Christian Brauner Cc: Jan Kara Cc: Eric Biederman Cc: Kees Cook Cc: Alexei Starovoitov Cc: Matus Jokay --- fs/exec.c | 10 ++++++++-- include/linux/sched.h | 4 ++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/fs/exec.c b/fs/exec.c index e55efc761947..6a0ff2e3631f 100644 --- a/fs/exec.c +++ b/fs/exec.c @@ -1195,12 +1195,18 @@ static int unshare_sighand(struct task_struct *me) return 0; } +/* + * User space can randomly change their names anyway, so locking for readers + * doesn't make sense. For writers, locking is probably necessary, as a race + * condition could lead to long-term mixed results. + * The strscpy_pad() in __set_task_comm() can ensure that the task comm is + * always NUL-terminated. Therefore the race condition between reader and writer + * is not an issue. + */ char *__get_task_comm(char *buf, size_t buf_size, struct task_struct *tsk) { - task_lock(tsk); /* Always NUL terminated and zero-padded */ strscpy_pad(buf, tsk->comm, buf_size); - task_unlock(tsk); return buf; } EXPORT_SYMBOL_GPL(__get_task_comm); diff --git a/include/linux/sched.h b/include/linux/sched.h index f8d150343d42..71002f0fc085 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -1096,9 +1096,9 @@ struct task_struct { /* * executable name, excluding path. * - * - normally initialized setup_new_exec() + * - normally initialized begin_new_exec() * - access it with [gs]et_task_comm() - * - lock it with task_lock() + * - lock it with task_lock() for writing */ char comm[TASK_COMM_LEN]; From patchwork Sun Aug 4 07:56:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13752525 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 DCC36C3DA7F for ; Sun, 4 Aug 2024 07:57:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4432A6B0089; Sun, 4 Aug 2024 03:57:23 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3F3616B008A; Sun, 4 Aug 2024 03:57:23 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 294146B008C; Sun, 4 Aug 2024 03:57:23 -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 0BF0E6B0089 for ; Sun, 4 Aug 2024 03:57:23 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 84B43161813 for ; Sun, 4 Aug 2024 07:57:22 +0000 (UTC) X-FDA: 82413807924.23.14F4F06 Received: from mail-pl1-f176.google.com (mail-pl1-f176.google.com [209.85.214.176]) by imf19.hostedemail.com (Postfix) with ESMTP id AA9BA1A0008 for ; Sun, 4 Aug 2024 07:57:20 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=EQ+i0aBI; spf=pass (imf19.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.176 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1722758211; 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=9th/bKhMjvRAjGGVHBeVqs/CIYRU9eynsJIFdR021yc=; b=bD0DuQkC/gV3JXmUYBPo8jiWLY1fTUBRkUKkv6WiunsdSrt1l4q/8bAQw4/2llcMUu/kqb z7PpfGUNb/j2ETplHHoR8aDCAJBIv3bgL+TlbDwmlJV7QrJGOWCphRcW+wExXWXwhc/MbY ZTd8MrcM3RdJabHEJc2akDpVsEImBig= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=EQ+i0aBI; spf=pass (imf19.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.176 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722758211; a=rsa-sha256; cv=none; b=nFNdbPaz0ton/em4XP5EYyVLAQJd7fUM5bjq0F3dWdgM/Vcb8U1+1xZ+XG2a3XWH6DbSmx LkVMAQk30jearxKTNJK5cIPMEIvWm/VwOmI5+dMnsTBsAE6OetByi6BCS2FscL0eedqi5u mOyM3abRej68P6Cu0T9Ks+1ingXLUJg= Received: by mail-pl1-f176.google.com with SMTP id d9443c01a7336-1fc5296e214so78016975ad.0 for ; Sun, 04 Aug 2024 00:57:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722758239; x=1723363039; 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=9th/bKhMjvRAjGGVHBeVqs/CIYRU9eynsJIFdR021yc=; b=EQ+i0aBIpjvGm7kqRzV45IDG2ujpUc9wXUHTyq1SRa07QUFJ8uN6r6+Zi+p9RLQEE4 uma/CIH5FHYCjq+GT26U/93uVBDMdtsBqf8uWCdV97mGBaFQExRHnSe/H62/nPn2WDcM y3YkLWSv+ob1eSBwFGxQTb7TNTX8gHZBke0ejDgefqLP+7KIUZ6oapu7ppBvuvXCW+dp +fqagZHFF/3vVScYmLFmM5/a67oMwT+ruVyLlkqOXCUTaqhondfSa+DlVU/fvyp2GzjS XgQb6qb4vPGcnLbk2EtKlDWp+4/o5tGwXyU2p7/+EurartbEUwQ51tzZ9hH6NrLKhlZQ nB4A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722758239; x=1723363039; 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=9th/bKhMjvRAjGGVHBeVqs/CIYRU9eynsJIFdR021yc=; b=CzI0jBWAOh9FZ602Ahyhg2y4DUMX70Gug/CkIkL3PJLaLDfzKAj8X0WJyiR73SUhdS DFZmWaRbeWZDWtOIYh7dd4HeGHlloxglihUHImIA+sFuEEXottqACmXMI02IaojQt+s6 gvKan9MmjfSc3GpAkAtjVCTcQZPqUA/WU2T7D2+4XEeofIaniEGKdkPE1AxyCR33kpey VsCSr5YPT8wp/pUp5TA26B9IGLj83bmVZdNS5Qax1QjoK/Q2NGVwxLfskcYBkadfPfTz bDfPV3CNyvnsZHw1nOWd8bFJsGRu+3PqO7W2AmEtrMMWVGdu460iX9pmQewiMeG9JtXY YhCg== X-Forwarded-Encrypted: i=1; AJvYcCWI4nh7HELAcWQLoHn8Rxpk9ubCFvrXka7MVCrOxQDFyKqAhWkAvh9U5UD8g7D2K9cmgzc8ymtJCkR13QKDHQwveMY= X-Gm-Message-State: AOJu0Yx10D7Ml5pFUfDfvB1i+DvVhmQ1nCsrxPyeiDFiMCETYQ2yAyk2 jzmhGyLmdoNZ5FEMyWYO08RI3yPh1qAsMyhAmRT8L5QUSuxoROgMOdRIq2f+OPo= X-Google-Smtp-Source: AGHT+IFtTPhQAQmKp5sRT0b26x8NVcEvgcuc+FRvXvXgBRo6DjcIq8upCUyDVHbaNG1PlsqP6YWdLQ== X-Received: by 2002:a17:902:cecb:b0:1fc:5ed5:ff56 with SMTP id d9443c01a7336-1ff574a1977mr137301625ad.61.1722758239497; Sun, 04 Aug 2024 00:57:19 -0700 (PDT) Received: from localhost.localdomain ([39.144.105.172]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ff59178248sm46387605ad.202.2024.08.04.00.57.05 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 04 Aug 2024 00:57:19 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, catalin.marinas@arm.com, penguin-kernel@i-love.sakura.ne.jp, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, audit@vger.kernel.org, linux-security-module@vger.kernel.org, selinux@vger.kernel.org, bpf@vger.kernel.org, netdev@vger.kernel.org, dri-devel@lists.freedesktop.org, Yafang Shao , Paul Moore , Eric Paris Subject: [PATCH v5 2/9] auditsc: Replace memcpy() with __get_task_comm() Date: Sun, 4 Aug 2024 15:56:12 +0800 Message-Id: <20240804075619.20804-3-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240804075619.20804-1-laoar.shao@gmail.com> References: <20240804075619.20804-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: AA9BA1A0008 X-Stat-Signature: exfazimso4yoxt9ekbis7wpy94se9z4t X-HE-Tag: 1722758240-76243 X-HE-Meta: U2FsdGVkX1+tYa2lXEjD2jmPa01tHstHyc1cDVRR7iCrtY4Plv44H60ma1lJM0QbR7LrvaxU/uXunUIriuBkQDlys9awKyHy5eBTfvPy5F2IEj3e6VQFfhLQffIoU3SAbrn2F08654g3kNJRDqJQLVgBenht/oJNhGLzDUneN/Tesg2Oys8Teu+LxPKdlM7fpdHevYbpO+jp4x2SIk6o0NjoiJbTlNXOp84UCF2JPGwLawRw2I0esKok4WWOhNG5Thwore+V5EC9ra7ChadN3aoJqi0JiLKjnuuHktdEpRutv/dkfhQkhbIb6RSfva1t9cw9hdmNvSk/YusxQS1DKvN7mnFnUQDbcUFuVh/BeUT4Y4v0Yzs5WWMIC+5SQa/A2/J+If28iKsCOEWczlunaDhMnEXRYt1ebaI57TY37dNo4dz4VYhRJntR5QbXDu+/cWdbpQDA3e4YFCuj6iW3n9eqvqwa8OiXpmvmsFh/aOtomkNLLdDQiZtgIEvgTl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osity: 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 __get_task_comm() to read the task comm ensures that the name is always NUL-terminated, regardless of the source string. This approach also facilitates future extensions to the task comm. Signed-off-by: Yafang Shao Acked-by: Paul Moore Cc: Eric Paris --- kernel/auditsc.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/kernel/auditsc.c b/kernel/auditsc.c index 6f0d6fb6523f..0459a141dc86 100644 --- a/kernel/auditsc.c +++ b/kernel/auditsc.c @@ -2730,7 +2730,7 @@ void __audit_ptrace(struct task_struct *t) context->target_uid = task_uid(t); context->target_sessionid = audit_get_sessionid(t); security_task_getsecid_obj(t, &context->target_sid); - memcpy(context->target_comm, t->comm, TASK_COMM_LEN); + __get_task_comm(context->target_comm, TASK_COMM_LEN, t); } /** @@ -2757,7 +2757,7 @@ int audit_signal_info_syscall(struct task_struct *t) ctx->target_uid = t_uid; ctx->target_sessionid = audit_get_sessionid(t); security_task_getsecid_obj(t, &ctx->target_sid); - memcpy(ctx->target_comm, t->comm, TASK_COMM_LEN); + __get_task_comm(ctx->target_comm, TASK_COMM_LEN, t); return 0; } @@ -2778,7 +2778,7 @@ int audit_signal_info_syscall(struct task_struct *t) axp->target_uid[axp->pid_count] = t_uid; axp->target_sessionid[axp->pid_count] = audit_get_sessionid(t); security_task_getsecid_obj(t, &axp->target_sid[axp->pid_count]); - memcpy(axp->target_comm[axp->pid_count], t->comm, TASK_COMM_LEN); + __get_task_comm(axp->target_comm[axp->pid_count], TASK_COMM_LEN, t); axp->pid_count++; return 0; From patchwork Sun Aug 4 07:56:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13752526 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 A9F50C3DA7F for ; Sun, 4 Aug 2024 07:57:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 09A946B008C; Sun, 4 Aug 2024 03:57:39 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 023746B0092; Sun, 4 Aug 2024 03:57:38 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DE0316B0093; Sun, 4 Aug 2024 03:57:38 -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 C0A186B008C for ; Sun, 4 Aug 2024 03:57:38 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 42896A0D1D for ; Sun, 4 Aug 2024 07:57:38 +0000 (UTC) X-FDA: 82413808596.12.728DBCB Received: from mail-pl1-f179.google.com (mail-pl1-f179.google.com [209.85.214.179]) by imf18.hostedemail.com (Postfix) with ESMTP id 644A91C0003 for ; Sun, 4 Aug 2024 07:57:36 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=Ty+gIr8c; spf=pass (imf18.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.179 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=1722758226; a=rsa-sha256; cv=none; b=pF+bFnd7Ht4ZjAP1/TvmSkFbuwIBkiXx93hjcjMtCmSIXDyzb4hpZRjuWF8IEmwSGrgNMu xvAI4BdlqtByDw2a7pByZvSPoHS+Us4IADp1stxKbYubrzZs6VesKNDu/ts7/HUyOFrGcW OyVH2qLBJidNLp/su5HSW0z3AHuTmDw= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=Ty+gIr8c; spf=pass (imf18.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.179 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=1722758226; 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=B0KTkwxi3yTBFtdP01Ghiv9wnj3oEVaVdo6Xnw/VoxQ=; b=4G18E1lyXRaqNgN+6Ep6Xwl/0xmC3r0ZDOa9uZnrQp/jETKzQLtwfNJ2Xhk6lOFFtBwgjM wFEtamAmpp7qMSIbH4gM2KAZo7zevEH7idlRdPyjBMaDQBEao6A624TOUOMbuvHNL2Pbps Vkd31R5myTHFPKeKSjNzX8xX8OSjgjg= Received: by mail-pl1-f179.google.com with SMTP id d9443c01a7336-1ff4568676eso39992765ad.0 for ; Sun, 04 Aug 2024 00:57:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722758255; x=1723363055; 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=B0KTkwxi3yTBFtdP01Ghiv9wnj3oEVaVdo6Xnw/VoxQ=; b=Ty+gIr8cBeVYiQMepJ8Z7nC0NA5l5AzR6jSkWc1w1buk83pTJ32IZj7WmMU82RWZYF 910qjrf0+rBwFCbh1ZsOxY28v/UC5gkcABb5iL91TQxfgyRtP7B2QKoEhfLzjaqlrHwD W8Lhii1CZklbgJSc0iOp/r4+apDpSA8v8ZfXJeOwxdvQROghvwp5fmhhJWk24e5xi9rj 9LdRXE0buQxdAykKj78Y9m0x8HXkfM0lv69MYRm8pZmOBitLPGEf77YsQmSuvteB8uGT 6xKqndjhJTXC/QB6qbrk5VhcKYAgMaJJkH5vBy0ZxKBlkeaWM/h9xuF4wagF7cLMMomm cr4Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722758255; x=1723363055; 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=B0KTkwxi3yTBFtdP01Ghiv9wnj3oEVaVdo6Xnw/VoxQ=; b=pYC80lDUxkC+pi4zzNg62nnhlQDW05SNLeGlQZv1Zdd+xIxS67i2nT1c5D8tVMBF3B i6PG0BiJeOGlQStS0PUlQL8qTBmJ8nlviO+HrOZRhAwUbpHH3tO95NPsT5Y+LvMId0d/ AGoQGuLYyCwsPtyl0MzJ+uLgg6uGBy5BJLwGk5t6xg2S8gi2x0W5CXv3YQ7+SpaHtSS0 QUEh4DuGuISCrUNx9zPGleLAVhNHWwjYPFvj6DbOaMer59JcHxfB34RD3VB9QDjfEfYI PO5jVtUkho5KcPFPyEK6Uy6h+CDN0VHB3vOr3IWLxdFY5/RDYgsVUnJO7+9wBf7+vyXn j3UQ== X-Forwarded-Encrypted: i=1; AJvYcCXs6z43YQMVcKG//8HIGXyZBo2IRdgYRio2HNsCF9JiAHq3+ZRC+zCix7sny4iWe+KZXywcandP+v+jSeV23ZlO6oQ= X-Gm-Message-State: AOJu0YxcYz4UCmo429Azqvygzx6UHsPiDthSZmK6tYrdn+rYbywCEOTV QfyqLu9Qa8VU19CBYJLTIa3sg/JWO+CavviD94cWAX5bFHaUFMPE X-Google-Smtp-Source: AGHT+IH6wosRQHFK1XrZrMQ/1/OazY5atARBRVgGVQBt8cXkjOG1KelkDNxLA3xQ6gKV/rMpiZ54sg== X-Received: by 2002:a17:902:aa42:b0:1fd:96e1:7ffe with SMTP id d9443c01a7336-1ff57456f1cmr80659485ad.55.1722758254963; Sun, 04 Aug 2024 00:57:34 -0700 (PDT) Received: from localhost.localdomain ([39.144.105.172]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ff59178248sm46387605ad.202.2024.08.04.00.57.20 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 04 Aug 2024 00:57:34 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, catalin.marinas@arm.com, penguin-kernel@i-love.sakura.ne.jp, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, audit@vger.kernel.org, linux-security-module@vger.kernel.org, selinux@vger.kernel.org, bpf@vger.kernel.org, netdev@vger.kernel.org, dri-devel@lists.freedesktop.org, Yafang Shao , Paul Moore , James Morris , "Serge E. Hallyn" , Stephen Smalley , Ondrej Mosnacek Subject: [PATCH v5 3/9] security: Replace memcpy() with __get_task_comm() Date: Sun, 4 Aug 2024 15:56:13 +0800 Message-Id: <20240804075619.20804-4-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240804075619.20804-1-laoar.shao@gmail.com> References: <20240804075619.20804-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Stat-Signature: knz1fsywnt8b1oqmcqu11srnnopa7w83 X-Rspamd-Queue-Id: 644A91C0003 X-Rspam-User: X-Rspamd-Server: rspam10 X-HE-Tag: 1722758256-706878 X-HE-Meta: U2FsdGVkX1/Is/910/k0xPj+OJeVaTb3Ur5a0W58s9EVnTslcA60chcBZjDVYc4tGtKE8+9Xp6c9HKlVUtg7F1fgFA5xYkvX/WhWDfSenViGl3CjT+3+JXkeeLbN7q97/If/ExC1cxcpzAo43UBXgNo6r6w7RtjTx1nGxMIvoPg1g+zPXgx8mpvlu7h55QC6kdCdECqx7HNJlIzDbYPrW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tp47FMHEsMHOud3DFze/a5pfihnx6Un4tXiGR1sQkBclCj11X2JJP2dyrwe64mEH8Q18ZcI9DzjmD2PiUj9gKBMsF6AFL8B7JDj1dnhqi2639U+Yo/NToh0bHxLHY5pjcM+/lQISfxHQCYk4jwwvkwH4Glk5XF3WqdFuPJ5ZmvcPBAOgILDdeBkfSh3KvrCaxQQPMe7PR/XLV6ydk6Vqp/lSpfu3cQ96D9IreCJS27MfhQWpMBFgr0rEX85rb8wQJr6dsE9agL7Hse2FAmDwUMTJNc+a+a6tsT5yWXSfGzOShkKE9kHPY6wiBLDl/qRy3Cc3HpK+Ut0MvyF9jsYqk6/Z2y/hvEgA6X/pBulug0ENlsXMYlt2caAXoINx22IQtybbcBLOhpQ6NScvMx4/i+epiGeKcrw7VNTsj+xzKjFRQzNSBjaXSewy1js0LKdY6HDdxIxH1AzAg+jO2m1BMy0g 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..a922e4339dd5 100644 --- a/security/lsm_audit.c +++ b/security/lsm_audit.c @@ -207,7 +207,7 @@ static void dump_common_audit_data(struct audit_buffer *ab, BUILD_BUG_ON(sizeof(a->u) > sizeof(void *)*2); audit_log_format(ab, " pid=%d comm=", task_tgid_nr(current)); - audit_log_untrustedstring(ab, memcpy(comm, current->comm, sizeof(comm))); + audit_log_untrustedstring(ab, __get_task_comm(comm, sizeof(comm), current)); switch (a->type) { case LSM_AUDIT_DATA_NONE: @@ -302,7 +302,7 @@ static void dump_common_audit_data(struct audit_buffer *ab, char comm[sizeof(tsk->comm)]; audit_log_format(ab, " opid=%d ocomm=", pid); audit_log_untrustedstring(ab, - memcpy(comm, tsk->comm, sizeof(comm))); + __get_task_comm(comm, sizeof(comm), tsk)); } } break; diff --git a/security/selinux/selinuxfs.c b/security/selinux/selinuxfs.c index e172f182b65c..a8a2ec742576 100644 --- a/security/selinux/selinuxfs.c +++ b/security/selinux/selinuxfs.c @@ -708,7 +708,7 @@ static ssize_t sel_write_checkreqprot(struct file *file, const char __user *buf, if (new_value) { char comm[sizeof(current->comm)]; - memcpy(comm, current->comm, sizeof(comm)); + __get_task_comm(comm, sizeof(comm), current); pr_err("SELinux: %s (%d) set checkreqprot to 1. This is no longer supported.\n", comm, current->pid); } From patchwork Sun Aug 4 07:56:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13752527 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 CC613C3DA7F for ; Sun, 4 Aug 2024 07:57:53 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5A4AD6B0093; Sun, 4 Aug 2024 03:57:53 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 553776B0095; Sun, 4 Aug 2024 03:57:53 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3F4656B0096; Sun, 4 Aug 2024 03:57:53 -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 1E95E6B0093 for ; Sun, 4 Aug 2024 03:57:53 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id CA2AC1409AB for ; Sun, 4 Aug 2024 07:57:52 +0000 (UTC) X-FDA: 82413809184.09.69691B3 Received: from mail-oi1-f179.google.com (mail-oi1-f179.google.com [209.85.167.179]) by imf13.hostedemail.com (Postfix) with ESMTP id 01CAA2000B for ; Sun, 4 Aug 2024 07:57:50 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="e/r2oSEx"; spf=pass (imf13.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.167.179 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=1722758223; 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=eQQ3mmtZRuc+UzgaTIbXRCgMjfWu2etwx8M84uu2mcE=; b=QrVrSLEZwMV9IaTO396EWonU4GLbKNXR8p/EfIAVUF08EE98DAweJsX2ngmjYgFMPAhNIk gKADYoQCcXzPeeAH0/hCZ6RHqW5DZechHpcsqH9BWjcmclIlipoBVQMxgJkZ0JnGGweFJW yFSasZlk1LRFauHRzVZG8JbHI8DxwrA= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="e/r2oSEx"; spf=pass (imf13.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.167.179 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=1722758223; a=rsa-sha256; cv=none; b=RMQetB5xmBdwPGTIIYEOVo1q2p+DH0DVjS+ol2TvunSvkg5smHA6E2VO+3kH+0iYl1vl8D BacU+E4kKI0eUDeYOlFADkJKk+pB5OWoRsiktjSF7f3VuCVOAE4xK4XGyEAIW9Vph35Uhy G2Sy/RPY9kBAo2i8EcylUIkLVQHzfxU= Received: by mail-oi1-f179.google.com with SMTP id 5614622812f47-3db16a98d16so6095528b6e.0 for ; Sun, 04 Aug 2024 00:57:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722758270; x=1723363070; 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=eQQ3mmtZRuc+UzgaTIbXRCgMjfWu2etwx8M84uu2mcE=; b=e/r2oSExR5s8l8M5qc8Y9+aFEzZ6mKz3n1LKtgPjNemgqqaF7WCDnNccbdaF4mj5v0 9hUiENNCh9w9SZaF3ApAKsz+Avrje0lFTl5zjtZDPpBdAXDCYTni6C47JCOyhuQIsF2o QQZA16OOab1J2/41vxcNdn+nss/YYiF70bLKk5MM/pbdv/LqjtSxHpLVailqJQbN5rCQ 1fF2ruHNBeTpJFgMhbvMAsNS76JsHKPXwkRddwArGIUcvQju6xum2yNjHQmb5pQZjbvb ifZ8gRzzew68tNKo9Yq8yje2Z4mD1ucQn8VIxUcMXRq7jPNLf/WKny81NEW64yZwBExq a+Bg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722758270; x=1723363070; 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=eQQ3mmtZRuc+UzgaTIbXRCgMjfWu2etwx8M84uu2mcE=; b=XQhX5QVwjfcmoCfPFfj+jp2ga9PvCg0gYpYsDUVVKcPCP4TnINHLPlpm0OycKcjjbb 8n2MpamBLPVuFuUTaLEuu93DCCt3Qq0WCPm1Na6cPsTy0kbEQYYZ6oNH7Hk6bjBuPbxf QULsvcJpltKOO2im/U/ysYpeJcC4jgl+Nxj3lOKVxyjzb2AFIrEtKdeMLhCi8WCjm4wq WkdO2lDJxkZbrUleepmDKFj/oVr1yfeitnaxHkUyly4qZ0No5LSAnCuKnslu9IzY75uB PIcn22i4J20bKEo6YLCCVl+ATp6J2XYVVNFUt0xVAbABZfRH9SnzC+vZ9XTZ8LtuSMWo pkdA== X-Forwarded-Encrypted: i=1; AJvYcCXJNWf8Wo4NtmJy6B/Iscz9D6Kxio7ETUdH+qvEnzq1ccGt2Cf6w1bKwk5blwDuqLz9kprr1iFtsi7iuvZgKKmQZnw= X-Gm-Message-State: AOJu0YwHkg/w7dsqOL+K9QPCdEd6iwsaBffU7DU6RO1Sb9J+c1PmFwto IZjiiBuoqFEyJEvJG/z9yHEGO1Mbxq2iCP1PRB9Te0xrOMSPB+m/ X-Google-Smtp-Source: AGHT+IEnK16jBONqPu6EI1AggsrrDpNbP1fkcELTlG1wIUqkHXvdRtx4NsDiXlOwAOqcDTPz63EQbg== X-Received: by 2002:a05:6808:159d:b0:3da:ac85:3bd5 with SMTP id 5614622812f47-3db557ef9bemr11482423b6e.8.1722758269943; Sun, 04 Aug 2024 00:57:49 -0700 (PDT) Received: from localhost.localdomain ([39.144.105.172]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ff59178248sm46387605ad.202.2024.08.04.00.57.36 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 04 Aug 2024 00:57:49 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, catalin.marinas@arm.com, penguin-kernel@i-love.sakura.ne.jp, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, audit@vger.kernel.org, linux-security-module@vger.kernel.org, selinux@vger.kernel.org, bpf@vger.kernel.org, netdev@vger.kernel.org, dri-devel@lists.freedesktop.org, Yafang Shao , Quentin Monnet Subject: [PATCH v5 4/9] bpftool: Ensure task comm is always NUL-terminated Date: Sun, 4 Aug 2024 15:56:14 +0800 Message-Id: <20240804075619.20804-5-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240804075619.20804-1-laoar.shao@gmail.com> References: <20240804075619.20804-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Stat-Signature: 4p4xfayueu3pof8t55csdbxh5dui6dmz X-Rspam-User: X-Rspamd-Queue-Id: 01CAA2000B X-Rspamd-Server: rspam02 X-HE-Tag: 1722758270-620888 X-HE-Meta: U2FsdGVkX1+2sOXgl++ar7tLe4whIMD1Ca4WqKaEcaiGfp8TTLANpiS477sGtKUIogcIg4Y3nqf9WbJnuXiTbJbG9vtBgIEN41Mr4tUFZIHXzXNNeQta+Hp85Y+jj9BaiUpN1R9pvSr7ktq+uuf7XOmJc2KlYaBjSBIALeMFO1m520DfKpJVX42A+NF9upYrHYVyZ9innddcdzWgYoqUzMaMUEFZEgRULk7pkjkeQPfuoRDKlc6ZrNhv6rsPVep3V6upH16aZpZxbXxpJsPfeFJZe1jtLRrSxeNUc03/QAlI2KiYrIHsEDei9QFtfQ+eklkjtye3XCTyIY1YANhPyeA5cHyRR05zwUaD1d36R3JanDPQW3Lg3XqXhggmtm6GINJfhvcGC+HFapPFh7ZxN7SA9PPgUSUSFJfvqCMI+W3xROvh+qH4TiBy35Wa7x6gKWX2wtqEBOH5Fok+BGm9vE+LfR6N4BDsWsileqBoFqOVer0Q3vQwE93ww3XNgSF7hUMP2BgW18eflFphz6/4VgaSfatoFiS9XKNyd/vJYY89qgDf5lE59+2+E1+Q+W6RiK523x2X+wKOmSzs/JAn7JVDnevqn/TC9bwavsOLFEfbBd3OXydz2pPL6a9lFDw6+FhQNCDh4UESBrzXptZ61d0/y/JacyCrnV7kbyziHqpDYO0jFUGhlexET8dFHpjm3jNhFHrQFK+fIdY1X9W9XUR0OXewAc2eJLGXYDvBRildx3fIepYxMgmUBwEYls7GSDaG4ycLNfo3lTiiZOOEyOuFhpA6+Bif0jyvCajiK9WkUoNaJ+IPTmJ5SyMQrp+Vx9CDOvvCAEvtJMDi6v2irh4enh1DzpTh+mIvYL9z3zyOuJIhdSNij6CUPCQD8uxneQjnWTORk9Eo/jFMIzQxWOVcWNbRRsR696A/VWGb06nTfd7gPNN0SWYUkMcyeWqRDKSVPEYs2PpYIeCDgGi 4MMwpvdJ 4FjiovVDyF0PK3FpMDZ7Jq7BMxVcldYKqzYGlISc1JjMuoBSz7ruK44PJCCUlldULBa/NWuaNS5foCy5B6uqt33paNydEAEdszKdgeC8FcylKayi4Q967WJQc+9KtzaVFwtSAnIvzlnQO7xBDKmM8wtjdQ3HGiq41m2F/tGKQxhXvHffFTgRbtCfRhlrCNAShdixt8gab0fNiJn9w/Zt0OK3WT44EacxOJ37YV4NH8VLqDZLhmUxDOTKwQuEPCQcDJkoQj/zQVO67CAXowjLWUMBLzb8bYrOC89l+zsGF79aOfJt7FqQcDzOMNnt2colxPmpEoHFAEDiSptwW1zVYL8Ld2RHYe4aQALwZvBBSlH6dUFzB75tloTVM3noks+PnVsEpe3RbTeS6ONag69fKljjgcdyH3Tcg5TgHMuoja3FFg7CEMin6sM42y+Z3zs/b/5kYh6qhR/P/XHrzdvpTDYuknXGDSOJ/4EkFUDJtFK6YZZIEqYRtWLKLSv/q3EsFhZZDRE+9wJlAyn8= 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 Sun Aug 4 07:56: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: 13752528 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 6F96FC3DA64 for ; Sun, 4 Aug 2024 07:58:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EF3416B0096; Sun, 4 Aug 2024 03:58:05 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EA3136B0098; Sun, 4 Aug 2024 03:58:05 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D43E86B0099; Sun, 4 Aug 2024 03:58:05 -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 B247D6B0096 for ; Sun, 4 Aug 2024 03:58:05 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 72CB2C0AB3 for ; Sun, 4 Aug 2024 07:58:05 +0000 (UTC) X-FDA: 82413809730.19.D73FA9D Received: from mail-oi1-f176.google.com (mail-oi1-f176.google.com [209.85.167.176]) by imf10.hostedemail.com (Postfix) with ESMTP id 9C8E9C0011 for ; Sun, 4 Aug 2024 07:58:03 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=fIbaSsGm; spf=pass (imf10.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.167.176 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1722758276; 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=FVkI7ws3sH8SytGBGHon98LlMkqoO/ooAo5HpqnjZFY=; b=gEU3FW74f82DzoxWj6dLLpr++CxMLq4mgGkygXNyvnOu/HurSe/EjdQsA718xGMHbJXFFW 1N7hvEO4Zlbg51u/W0K6wbE48rCOT12nXzr+HVP3ZDBt+SR7TxJw2dlWD4dhFC2OWBvyFZ 3T3X6TxofeIbrsEghT4qpJMkwQ49Yqg= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=fIbaSsGm; spf=pass (imf10.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.167.176 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722758276; a=rsa-sha256; cv=none; b=B+q54ckJ+jgZtcEH24p4kbMgLpynYOzK189s/NbxoMvIa8kq6etyrG0n5K6Fi3L+sMbWGb 69Xqw6tDZu+msXTP1WHCdPvG20dxeKH5Te/FkGP1eSCD/ImsCcT2fBtqwP7kUHGCeR/RY6 tsiNuKuZ3rozfiXtSIji/UCATRLzJE4= Received: by mail-oi1-f176.google.com with SMTP id 5614622812f47-3db35ec5688so4702195b6e.3 for ; Sun, 04 Aug 2024 00:58:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722758283; x=1723363083; 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=FVkI7ws3sH8SytGBGHon98LlMkqoO/ooAo5HpqnjZFY=; b=fIbaSsGmU8c/Z8GIPDQTrXmm0C5fAPk3tF6laYxFWletzoNY5u6TVfY62mJBnU5sf5 FbUMhDJSyt7ctf5FelQItDto9VNW96mir16uxFVNgWnueKKSgTFpbSUaUmCmKdRUCRAH sWfyV6/b8Ezkp2Ao9yleJNmq+2n8Zs5jceInvZEO+JOKnGuG4mOdQ0+CjAeLETWDCbRJ vsreWnqbn+0FqWqBD+DePqaSo8/5R/TW/lSXUTtervFyaGt8+QGdXpc0PZo9aqP7QZc8 gbHAT8FUxbodal7Yo/P+qruWl54Ctk8qs/FpGOhS/SVEXVnEKJaWJsdnxRW24TrlKHad Sj7w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722758283; x=1723363083; 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=FVkI7ws3sH8SytGBGHon98LlMkqoO/ooAo5HpqnjZFY=; b=SrICW1xWJr6TmL3af3Btp6Rbvzns6oh5zSmfAWUm4UqPC28nXhx0jIKQywVPHYOJK0 FUhXZydMWEh0T1r2GW9Kmu6CQlQyTPqE1Athw+Rajoms7QYUzgMJkBGvYjbs86rRGltJ Gkw3t1tRTwW9pghOLLBBK7mhg6+SuqcepZYkQmTMAxQLJ3yE1iPtPX29JO9rZtFXRhXp xXVi6bo9tYPnFku5G5fDZQusHgW1DnWTXYHO/T+yKBr+OyKq4wTqinyrFDw+sSKFpO1u lDlvyeMKY/gSL3vmT1IhOtThuqxIchACm8ehgm7iXUVMHJAovHbD11fd9RkLzaWnYA0s W9JA== X-Forwarded-Encrypted: i=1; AJvYcCUU5FjaKYx86772pbN0Z+9HLy5HPJF0QxPit3yuj/MiuyyKMcKvWgwH5yaoqKEHRpiAf7pszFWelKBX+eW6ktLnSVc= X-Gm-Message-State: AOJu0YyYGFO+yWjsrUDs4vCzhxm8EfoTU/SK36KRnrTOIi9t1xpPIrnh wlKpm1vyx36LnKKhC6bsG2EDtHggurKXA2KcdVRhORr4hIMp3Aw2 X-Google-Smtp-Source: AGHT+IGvj96nN6G8dNTQwO1gCnAi0LCFIOOEmeeyoUDneJo+nlGiP3jzVP2As4CYicNVTrPVKHFDvw== X-Received: by 2002:a05:6808:16ac:b0:3da:e587:3e1d with SMTP id 5614622812f47-3db557fc2cfmr11867023b6e.4.1722758282670; Sun, 04 Aug 2024 00:58:02 -0700 (PDT) Received: from localhost.localdomain ([39.144.105.172]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ff59178248sm46387605ad.202.2024.08.04.00.57.50 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 04 Aug 2024 00:58:02 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, catalin.marinas@arm.com, penguin-kernel@i-love.sakura.ne.jp, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, audit@vger.kernel.org, linux-security-module@vger.kernel.org, selinux@vger.kernel.org, bpf@vger.kernel.org, netdev@vger.kernel.org, dri-devel@lists.freedesktop.org, Yafang Shao Subject: [PATCH v5 5/9] mm/util: Fix possible race condition in kstrdup() Date: Sun, 4 Aug 2024 15:56:15 +0800 Message-Id: <20240804075619.20804-6-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240804075619.20804-1-laoar.shao@gmail.com> References: <20240804075619.20804-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: 3gt7zgfo5swq93h7khuadri98wezwff6 X-Rspamd-Queue-Id: 9C8E9C0011 X-Rspamd-Server: rspam11 X-HE-Tag: 1722758283-408754 X-HE-Meta: U2FsdGVkX1/joAweLuEsxJdJb74iEzFuJ2FaJOUTf4smXEem0ogGQM9SoSyewPOH7drGY1TUmpUULA+s1IlYjMJJFFGOaXansKbEoBKQvFjbJCVRu7UGQjLMHmt4RjNxXCYcRkhOYj6dDPDbbxuvDoSWvNrDvm6rU7+O1OAZx/c5DYr93ssfdEqwvxcrXknmpzmm/bnEHffec6FjVmumDyxDh83HbE1t6SE/j5v2qSWMOm1IBq/KFM87xERc6uf7cJ4wfPTz/wXh0nvgO6GC5Z6uXqslzvOcuHj5jHKjrInXpsfneb/r4s5bQFuR8bzENNzLzjIGqlGJlnmQVXtWe3Q/9W4qeOw7xCyLBEY/KgYSNjNfvP03eKVpnATI7E+d2pK95wE1XvACYpTPTX7RS7Yl0bU+zfOcAZPJfvOXbhqmWEXPg3kDrY+BOSoXbEnIA4k/BhUxZjMvFihQRWLgeZHfjT7XDDrVrjU9rO/V0vp2sDoQDJZmO/SPNrrkCcHduBd/74HGi0WGkGZaukpfR6VaJ+vL6EkakHxD+xMcPPHUWKPC+Df7WxADf1OqazZIZFH853te4GpWgGqDAzJHv/pjyPs+zjJkmnpeiaMc3zo824az0k/kw8eYRycODDOOSxT8/dPJ8S2/bFdVmB6cSgoFYpLvMpbMRHL8wSN0aatQ3hF28Enurx53FpJmLwMpUQi7FhJLETDl1SyD20uE3pnRvob1CG7Cb/1TfUx+CqXC8y7TQGeMiWSyqbyH9vFKE4ncFyXzyoATKIguE/7Tj1OzRs7coNVABKB5HhfXP5Z0gEigWadNLqFQVRkuwt+Pn4DI3/76qfAXVXgbMG9iM1zDORj6Md4NNDVfBUpbb6h37D8QhXbM6+aKoXIQklQriA2XS4r20aTfiPCpqPj5H8/QppnOBEZOUl/OGFQ5Su3gF5ZdnYt8rK1ZaG4iY1zYgdpr7EK8pCSVwUzPGyE ydNrjBeW NyeiuHKARRYcI2pz823BuIeCfgddPn78zh4JXDmmi//gp5gyRDUHTGq0JWvnDBB1FRIcuq5gxHAQRpQ1UjvGdbjiNWt1o1hBfMoz6UYGg8Q0WT5XadJyyufumx5pL3YzrAsJh+Az79nDqdtQHHAVwgLzfLBx7s8jZYTtnl5joC6jHQM4q9NaR3LL+fO8+QSPJABAZx3eYfpokRipS1q/s5h5/pfeWJou//MvHMA5D3PKn3vBcsR+Za+NQGMWWpVaycaBi9qz5qYmFkk/rq0NQ/X+Q6WFZPW/grXMsO73dp2jDKnsJXfK2+3mVue0mZh2UQNlIrnMETpxNFsf8JysP2wxdENfOsglpUquQFSC8bb+lCJlwJ1MEC/joWwJkYBpaIkGVrzmIef8g6/mDnJQ1/4zd6ndxRnCMQmn558GMFzZz0s8BikKDD6yy3vDit9YoIIX0gS91jbxWsOdMpo18gx7cFujUdAdKr5ebV73EUiqWVaSSPH7HKEtb0fOPXCYgaifilhzOeAPTIVw= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: In kstrdup(), it is critical to ensure that the dest string is always NUL-terminated. However, potential race condidtion can occur between a writer and a reader. Consider the following scenario involving task->comm: reader writer len = strlen(s) + 1; strlcpy(tsk->comm, buf, sizeof(tsk->comm)); memcpy(buf, s, len); In this case, there is a race condition between the reader and the writer. The reader calculate the length of the string `s` based on the old value of task->comm. However, during the memcpy(), the string `s` might be updated by the writer to a new value of task->comm. If the new task->comm is larger than the old one, the `buf` might not be NUL-terminated. This can lead to undefined behavior and potential security vulnerabilities. Let's fix it by explicitly adding a NUL-terminator. Signed-off-by: Yafang Shao Cc: Andrew Morton --- mm/util.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/mm/util.c b/mm/util.c index 678c647b778f..912d64ede234 100644 --- a/mm/util.c +++ b/mm/util.c @@ -62,8 +62,14 @@ char *kstrdup(const char *s, gfp_t gfp) len = strlen(s) + 1; buf = kmalloc_track_caller(len, gfp); - if (buf) + if (buf) { memcpy(buf, s, len); + /* During memcpy(), the string might be updated to a new value, + * which could be longer than the string when strlen() is + * called. Therefore, we need to add a null termimator. + */ + buf[len - 1] = '\0'; + } return buf; } EXPORT_SYMBOL(kstrdup); From patchwork Sun Aug 4 07:56: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: 13752529 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 CD96CC3DA64 for ; Sun, 4 Aug 2024 07:58:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5F2B86B0099; Sun, 4 Aug 2024 03:58:24 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5A2126B009A; Sun, 4 Aug 2024 03:58:24 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 442C76B009B; Sun, 4 Aug 2024 03:58:24 -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 25B066B0099 for ; Sun, 4 Aug 2024 03:58:24 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id D4E7514186D for ; Sun, 4 Aug 2024 07:58:23 +0000 (UTC) X-FDA: 82413810486.18.02E6D09 Received: from mail-pl1-f170.google.com (mail-pl1-f170.google.com [209.85.214.170]) by imf14.hostedemail.com (Postfix) with ESMTP id 0BB8A10000A for ; Sun, 4 Aug 2024 07:58:21 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=FQ43AOAd; spf=pass (imf14.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.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=1722758241; 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=bS0UkRamrVL5XCR4Rnj9cLPm9Cl+fEpiyHi/lwqSnvY=; b=lqHpLh80MUWZaJIxsPaTygFYapSoDr/kbKXmXNA1cmr537zeeSiuI8s1BFYhKSWet+n4Qq Ql3RUaF82HTVvZq90nlFlHQGiFieJKoGADCFBO6Jslnm14rUQwraZgB+sQJi3oYGb6cXE5 Jx80AtxoCotGRlG+oyAVCiDQ66C84Ak= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722758241; a=rsa-sha256; cv=none; b=LtQ68eYMzD4CJAym/+mJ8xWboJncN3SYeIpydo2ddeqSEWJfXGTX+Gasab9vTHmnwHbuRP Q5H3407R38wR2H5AL4nLNwm7qILSzgcnWj94jzj4THCRf5Zg5ItIxiYsIyPnqnmD9bAAVS P5EpqqsqLSpyNgmuFhIgXAdzMqoqG8o= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=FQ43AOAd; spf=pass (imf14.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.170 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pl1-f170.google.com with SMTP id d9443c01a7336-1fc56fd4de1so32922355ad.0 for ; Sun, 04 Aug 2024 00:58:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722758301; x=1723363101; 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=bS0UkRamrVL5XCR4Rnj9cLPm9Cl+fEpiyHi/lwqSnvY=; b=FQ43AOAdpi2uzZotkoLKrRsUk0l/+gIn4UlYSXtC/AEC9RA4167dfCGZjteICzIG5z oXePdrhrMAhyFPumUacyEjjKR9IvFLgSlAMPxSN1/1XsEkdJYF4I7B8fxOPWFxRHT8GK nM6EKnAhQVuyshHBR0XoePVda9GN0HHWIx3h03xTHLCmMhU7wAWCdfLs3D60y0uwP8nq mmthlUA/pCuxmNNVmK47bux7URkRzYlUGQLHqB/qFDaOSXGhh0KlsyClbiBwYmQlbTb4 6LP2p4vsUl7yo72LvD3N564yuzDDdrAYFr4MJ3+yBIoNYYlAKwly7xdTpsdwPsGWWQiR y5Uw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722758301; x=1723363101; 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=bS0UkRamrVL5XCR4Rnj9cLPm9Cl+fEpiyHi/lwqSnvY=; b=GtHrIGdNFBrxNjJNu3fzs8W8YAg2TyxQDp7hqUm6tg66Ay5nZhNi7MBz2UJIrcZZe2 VP5F41W5vg1GA/UHwjrCzJDMrZVJ420VTChITO8I7VUbYNnUuzmXZMk/nE5DWGyacy8M azpLC7sFhvbqEnTzyL0zPfd2Pzq0NwDOBWbGG0y9ZjQh3HFKvb2Z7jy7n/M5HVntGyV5 CYCM3QByh4tNOlJbGnDtSCWqf8AgxOsdRB+Rj2olz3mcR94G5DjrOyq2QnnRoQbVnJv0 DDEccJhkLgf+z7tgsyAabm3x0veRj7uUsTWJazjsjc/Tz+GQEakPvZbeuxl3y1ddxc6S OQ/g== X-Forwarded-Encrypted: i=1; AJvYcCUQ2usXKhFJnpEkfQOOTYI67mTBCIZSXrspF1K5WGsYmFzCQmdDm6lSkoo7DBqHlnFdTpRUyqgEecy51G4Ze/yOWd0= X-Gm-Message-State: AOJu0Yx5+/IbWzVj1Q4KibfOR2uvSiwOSwN0/pUAe9o+MRGOz5LyIq1Q KAS+rV1u8m1OKXB/tsrG+AhLIUyuHjFN0Vj/8mXThnAOem84Jcxpc1veg2acI1w= X-Google-Smtp-Source: AGHT+IEGtCQCVMnQCKfJ0+BIkhT35+DpDH9a54ylBrsi080HAExdi6zY8c1ZJxWy2ZwMLSFkL3dmww== X-Received: by 2002:a17:903:1cb:b0:1fd:7ff5:c673 with SMTP id d9443c01a7336-1ff523ef0dfmr156564885ad.2.1722758300691; Sun, 04 Aug 2024 00:58:20 -0700 (PDT) Received: from localhost.localdomain ([39.144.105.172]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ff59178248sm46387605ad.202.2024.08.04.00.58.03 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 04 Aug 2024 00:58:20 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, catalin.marinas@arm.com, penguin-kernel@i-love.sakura.ne.jp, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, audit@vger.kernel.org, linux-security-module@vger.kernel.org, selinux@vger.kernel.org, bpf@vger.kernel.org, netdev@vger.kernel.org, dri-devel@lists.freedesktop.org, Yafang Shao , Simon Horman , Matthew Wilcox Subject: [PATCH v5 6/9] mm/util: Deduplicate code in {kstrdup,kstrndup,kmemdup_nul} Date: Sun, 4 Aug 2024 15:56:16 +0800 Message-Id: <20240804075619.20804-7-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240804075619.20804-1-laoar.shao@gmail.com> References: <20240804075619.20804-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Stat-Signature: tpzggihyu4ruqr3u6zt14p6wq8zx1h4g X-Rspamd-Queue-Id: 0BB8A10000A X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1722758301-513349 X-HE-Meta: U2FsdGVkX1+lNGn23Y7wTbHIF2i3acuvfmtCNcbnrDaKxM9KQg+PwW8+di+2SIFHKn47N0Q8Cigr1W7QSisiiubd7QzTSw1mwkb8YGy645O3uYLL39EjTTSvJWPRbgrvQAafd0q+jIshdSe6KSsqd+ZCA0K2ABX38kwCojPklj/vC1FQxRCbEilkfOSDaYAFeTqc3Uqx8Rrcm/ssYxptOj1Q+RnxTHDDEZAkm/yVy5bSC3dmsjT3qEqmuw4SwYKU1Zq6O3unJbAOE7ubjqj5U/3WKjKg2ws8e4S9lYfL69fCZW5s6Z3kRU0xBS+/MkS0s0gXh8n9NGwqIwNN6HszV/hJrwWu/NANQAEdfvuKx0UOEaAVaeoNwo561R3Cb/G27GORsrjfWv5UUrtnA1H49e32wv86ynwjF9H5o/bhd7NECgsLuD+zlO52PDxlKeo/6EsJ/qsFEc/PirPtNEyhObBgvw2px6jmtrZVDaP3JtP/yizQIbQceZpz8ZOPk6jArDylOeWx/zO6dOfHkCSahBQYYMnHmRpTVoNQkml+Bj9uqQFayfoG3ERUBr12+uJgsK/roMQ9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91JxKwb0UwUI= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: These three functions follow the same pattern. To deduplicate the code, let's introduce a common helper __kmemdup_nul(). Suggested-by: Andrew Morton Signed-off-by: Yafang Shao Cc: Simon Horman Cc: Matthew Wilcox --- mm/util.c | 67 +++++++++++++++++++++---------------------------------- 1 file changed, 26 insertions(+), 41 deletions(-) diff --git a/mm/util.c b/mm/util.c index 912d64ede234..2c5addabd6f7 100644 --- a/mm/util.c +++ b/mm/util.c @@ -45,33 +45,40 @@ void kfree_const(const void *x) EXPORT_SYMBOL(kfree_const); /** - * kstrdup - allocate space for and copy an existing string - * @s: the string to duplicate + * __kmemdup_nul - Create a NUL-terminated string from @s, which might be unterminated. + * @s: The data to copy + * @len: The size of the data, including the null terminator * @gfp: the GFP mask used in the kmalloc() call when allocating memory * - * Return: newly allocated copy of @s or %NULL in case of error + * Return: newly allocated copy of @s with NUL-termination or %NULL in + * case of error */ -noinline -char *kstrdup(const char *s, gfp_t gfp) +static __always_inline char *__kmemdup_nul(const char *s, size_t len, gfp_t gfp) { - size_t len; char *buf; - if (!s) + buf = kmalloc_track_caller(len, gfp); + if (!buf) return NULL; - len = strlen(s) + 1; - buf = kmalloc_track_caller(len, gfp); - if (buf) { - memcpy(buf, s, len); - /* During memcpy(), the string might be updated to a new value, - * which could be longer than the string when strlen() is - * called. Therefore, we need to add a null termimator. - */ - buf[len - 1] = '\0'; - } + memcpy(buf, s, len); + /* Ensure the buf is always NUL-terminated, regardless of @s. */ + buf[len - 1] = '\0'; return buf; } + +/** + * kstrdup - allocate space for and copy an existing string + * @s: the string to duplicate + * @gfp: the GFP mask used in the kmalloc() call when allocating memory + * + * Return: newly allocated copy of @s or %NULL in case of error + */ +noinline +char *kstrdup(const char *s, gfp_t gfp) +{ + return s ? __kmemdup_nul(s, strlen(s) + 1, gfp) : NULL; +} EXPORT_SYMBOL(kstrdup); /** @@ -106,19 +113,7 @@ EXPORT_SYMBOL(kstrdup_const); */ char *kstrndup(const char *s, size_t max, gfp_t gfp) { - size_t len; - char *buf; - - if (!s) - return NULL; - - len = strnlen(s, max); - buf = kmalloc_track_caller(len+1, gfp); - if (buf) { - memcpy(buf, s, len); - buf[len] = '\0'; - } - return buf; + return s ? __kmemdup_nul(s, strnlen(s, max) + 1, gfp) : NULL; } EXPORT_SYMBOL(kstrndup); @@ -192,17 +187,7 @@ EXPORT_SYMBOL(kvmemdup); */ char *kmemdup_nul(const char *s, size_t len, gfp_t gfp) { - char *buf; - - if (!s) - return NULL; - - buf = kmalloc_track_caller(len + 1, gfp); - if (buf) { - memcpy(buf, s, len); - buf[len] = '\0'; - } - return buf; + return s ? __kmemdup_nul(s, len + 1, gfp) : NULL; } EXPORT_SYMBOL(kmemdup_nul); From patchwork Sun Aug 4 07:56: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: 13752530 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 45269C3DA7F for ; Sun, 4 Aug 2024 07:58:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D6C2C6B007B; Sun, 4 Aug 2024 03:58:39 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D1C6B6B0082; Sun, 4 Aug 2024 03:58:39 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BE4E96B009B; Sun, 4 Aug 2024 03:58:39 -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 A13F36B007B for ; Sun, 4 Aug 2024 03:58:39 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 379BE417FD for ; Sun, 4 Aug 2024 07:58:39 +0000 (UTC) X-FDA: 82413811158.24.D8CB360 Received: from mail-oi1-f171.google.com (mail-oi1-f171.google.com [209.85.167.171]) by imf20.hostedemail.com (Postfix) with ESMTP id 67D341C0006 for ; Sun, 4 Aug 2024 07:58:37 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="U6gL/zyq"; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf20.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.167.171 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722758310; a=rsa-sha256; cv=none; b=AXaS1vu2OCnptGmBg0fi3nDkIHSsBMLvZbDd3bAZ3OwAUKCGMTrbcdYWG62Vn1KEW96F10 eVjCpC8UQFdrVKc57GiX4BaCORZbcHOsHWcInaB2w29t8zluJpZjfBJmRCt+UCRMR/o+p6 tt3YRpBuSMYrnbApazY5TDUVRYB8WuI= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="U6gL/zyq"; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf20.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.167.171 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=1722758310; 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=hHFsexRAvS3+Twdq3ZGpnUTD4jZRkYjFMffxIhD4B7E=; b=SbpFW50Gb1jZmGEilrvaB9lGVmgrAH+x4ArLyH+ZJJKkALJjGwBxHOS9r/MGnejSirZSy/ oOByRHKWGWlz+hrUU0wUHIj3GHCUE500071wlJNyEo609CP13gmfXWpV/1SSfl083y10Sg GFmKqK7LwV9zBVfkivOCrSt+ygDDSu8= Received: by mail-oi1-f171.google.com with SMTP id 5614622812f47-3db2315d7ceso5601711b6e.1 for ; Sun, 04 Aug 2024 00:58:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722758316; x=1723363116; 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=hHFsexRAvS3+Twdq3ZGpnUTD4jZRkYjFMffxIhD4B7E=; b=U6gL/zyqChBa9+LUSmX+Kpjze3d4WCaT9MmnCgyMlJvebfqsPHFSuJGwRzrGKHHutf CYFD9uxbozHSjlxg8gQCc7zc5xGe8sqiNEEZTdSyel8NEErEugQYv+nUlNeULqtGMpLS +f5VGXtxx+ZaZHTPWDaXe1wGzJ9MGrMHrGXopsAlqUXsUwcFW83w+d0JUBPOjRC5A3Lb MYy6eUm1i0tBlHnyYPUmX7V23qWxlOsYe/HF/wwdQ0RLJLEzjh0uKZRHQIDOL07hkFKx PI2WO7cOVW1c+bGOJ4EeD/AEACAXnOhhQzOqURKWo7yhu7nVnrQnt3ZC/MNtOP3ZpMnm LC3Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722758316; x=1723363116; 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=hHFsexRAvS3+Twdq3ZGpnUTD4jZRkYjFMffxIhD4B7E=; b=t+a70MahEzD3XWCs6DGrZDNQ6lQ1DAMtKe7qEvnB3c+JrnJ11vcinNuLOnmWebJozJ t5XNkqOvDRt+cOXmqAs4aXvtCxjwj1/f5VVvVeVd54BQmMFfkmggb3ATO6q+WqDEIOF1 MBTY9eR1dc97I5iHjWlgGZj5JqSb42aE2cBl7ObJG/akhqbTh34wyy+Y8yutZfAMxblw 8Us2Wi2MXgJMTaqP67aAwxL9x+h2bKvW3/TCsZeHTDgHjv+nRybGVq1aTQKcSmmVuVd1 zhhdfU33EzLkYrgpmqwfktGDpFEMQN6rsygLzd5gTO2BihHNu2SGCR1VnMOyU01WcBKd +new== X-Forwarded-Encrypted: i=1; AJvYcCWsDNGXXFfZhbQTIYYA7R58FRXiPvjpCF/o0+BQm3m6+i5a9RoIE9wUA5jKkiIHDXVNlMdvd4x4m8bCotvkr7kHCxs= X-Gm-Message-State: AOJu0YyNeNgz73elDLauWXHrna8Umd0ksAY8tBtfdiNKzi/cWNioTZ+p aoe4BttNXJMVvr/Aq7TYmJgp2jA1zR9JPeZcIwToJwJX2F8DevSG X-Google-Smtp-Source: AGHT+IEQdsxKAJTw4i+uNNPkbgE3NiIW5Ir2yWohAneqMp4vDkArZBqW84U9DpBffvNg6sRs26e7Fw== X-Received: by 2002:a05:6808:212a:b0:3d9:2562:7541 with SMTP id 5614622812f47-3db5580f273mr11989986b6e.24.1722758316365; Sun, 04 Aug 2024 00:58:36 -0700 (PDT) Received: from localhost.localdomain ([39.144.105.172]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ff59178248sm46387605ad.202.2024.08.04.00.58.21 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 04 Aug 2024 00:58:35 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, catalin.marinas@arm.com, penguin-kernel@i-love.sakura.ne.jp, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, audit@vger.kernel.org, linux-security-module@vger.kernel.org, selinux@vger.kernel.org, bpf@vger.kernel.org, netdev@vger.kernel.org, dri-devel@lists.freedesktop.org, Yafang Shao , Masami Hiramatsu , Mathieu Desnoyers Subject: [PATCH v5 7/9] tracing: Replace strncpy() with __get_task_comm() Date: Sun, 4 Aug 2024 15:56:17 +0800 Message-Id: <20240804075619.20804-8-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240804075619.20804-1-laoar.shao@gmail.com> References: <20240804075619.20804-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Queue-Id: 67D341C0006 X-Rspamd-Server: rspam01 X-Stat-Signature: u7cpb9eqfzuq7sg7y7tq5ar3yhrku3w9 X-HE-Tag: 1722758317-522491 X-HE-Meta: U2FsdGVkX1/yvY4/ojeNBg/vmIId8Z0Oge+xleIdVNV/2hCx4xVX8yIwtYPZe224Y5G7PzxfLkREbdUHsYXOk/R3B+LmNE5Msj8RqUlUHytAjNFiVexYNcFNWlfi0+kmtM/qj6lM82WyhX9JPRCbs3//6N6rsSdVCyStLvdgzavqAUxFDtU3j1YtIH2rW9PXIP3+Oo2IJXu6g+5tc5V+Hyj/bmS1SmESjp0kz5anTvFIvqnZUjxtx5X59gqmmxmESNxyKVAB71BPgbiIrYZYooQC17Es1seHjEIvhZ7vGKgIW7KBNbs4SPNW8K8ilMRcfPYHTMv8jQNVfDvXB/XC5fY86FmgWLNurDjOEYFTfAJeu3wdMn+fufx12XkerApLsL4N9Nb9DOKzwfQr6bQo0M92vaclQq/1r+OnAeCb/qBkTaQolrbM5oEacZ8ZtvsWg307ilKUX+6uSUYnOw5r2p9nLtC2n2eoMq2AOienSuARBoBCmetIR9awCab2k8hDQ+tkNYIUQtMqQHxnAanpTKbE5kmhq+wEt8YcH+DX2TtsmFecYCh+CDaNDixWylL0ev4MAhZym6nDZe+5+6jnCFxYuOTN1WFKRhdwLaqWOZ601eVTn9WGb6OkE5IBZPGyiXsbFaSeu7yADoE+w1PN7HyGibrICBBTN7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k X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Using __get_task_comm() to read the task comm ensures that the name is always NUL-terminated, regardless of the source string. This approach also facilitates future extensions to the task comm. Signed-off-by: Yafang Shao Acked-by: Masami Hiramatsu (Google) Cc: Steven Rostedt Cc: Mathieu Desnoyers --- kernel/trace/trace.c | 2 +- kernel/trace/trace_events_hist.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c index 10cd38bce2f1..985d2bf2bbc5 100644 --- a/kernel/trace/trace.c +++ b/kernel/trace/trace.c @@ -1907,7 +1907,7 @@ __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu) max_data->critical_start = data->critical_start; max_data->critical_end = data->critical_end; - strncpy(max_data->comm, tsk->comm, TASK_COMM_LEN); + __get_task_comm(max_data->comm, TASK_COMM_LEN, tsk); max_data->pid = tsk->pid; /* * If tsk == current, then use current_uid(), as that does not use diff --git a/kernel/trace/trace_events_hist.c b/kernel/trace/trace_events_hist.c index 6ece1308d36a..721d4758a79f 100644 --- a/kernel/trace/trace_events_hist.c +++ b/kernel/trace/trace_events_hist.c @@ -1599,7 +1599,7 @@ static inline void save_comm(char *comm, struct task_struct *task) return; } - strncpy(comm, task->comm, TASK_COMM_LEN); + __get_task_comm(comm, TASK_COMM_LEN, task); } static void hist_elt_data_free(struct hist_elt_data *elt_data) From patchwork Sun Aug 4 07:56: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: 13752531 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 41510C3DA7F for ; Sun, 4 Aug 2024 07:58:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CA9886B009B; Sun, 4 Aug 2024 03:58:55 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C58DA6B009C; Sun, 4 Aug 2024 03:58:55 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B21296B009D; Sun, 4 Aug 2024 03:58:55 -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 904726B009B for ; Sun, 4 Aug 2024 03:58:55 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 38466A78A1 for ; Sun, 4 Aug 2024 07:58:55 +0000 (UTC) X-FDA: 82413811830.19.7FC4235 Received: from mail-oi1-f169.google.com (mail-oi1-f169.google.com [209.85.167.169]) by imf04.hostedemail.com (Postfix) with ESMTP id 5EDC440003 for ; Sun, 4 Aug 2024 07:58:53 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=RI0L91HE; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf04.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.167.169 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722758288; a=rsa-sha256; cv=none; b=ASYJNR1FYH2jG5e2/sSLlC6bKbez76aC78FrfHsW6SoBoQCp2G94/JC+lmDG0W4Mt/4MhG /OAfsJsIpAUdp0dl8zPP3uT21rCJPthUFLzUiEruM3PwF+GxhA13zWCRvt7LRr4/XibxB8 aXjb8izigwnEHRKJfps4FQ6eqoatKeg= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=RI0L91HE; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf04.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.167.169 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=1722758288; 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=Iy8s8HUuCpYYzqYVYDLibKnXKIG+UtFNyRBbbBdMpkI=; b=H8oa7BxJnCH/5Fi8Ym+IZGz4A2hRBWaUHLHEDU5AIr/hMD00omxjEr8Ffz7h4N85PDz8KI OKvtGMMmvl1OvjkOBaOtRciGoV5CZn+SUO+759evgXGU3iNS+V7R0Mgv/g3wX0Bo/FmplR f1uUBwE3KfAEprIpLbVpxyO5pKEdNNA= Received: by mail-oi1-f169.google.com with SMTP id 5614622812f47-3db51133978so2542890b6e.3 for ; Sun, 04 Aug 2024 00:58:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722758332; x=1723363132; 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=Iy8s8HUuCpYYzqYVYDLibKnXKIG+UtFNyRBbbBdMpkI=; b=RI0L91HErNasqIouNBMx8JGlTh1wR8wTuTD1wkqrLyVuukwf/5iX9CeRrtO8kX4atz JwS24LeoELH5eCjq0Sau3JN9HAatK165slgsdSzYytuyQ5U/1HruflOqUcBqXIaJ9rc+ IUb4ucu4ROXKTqrFezeMqb/+NwBB7mTKXc5vRxtjEh7CguClzvhmRrlOLOGfUkCYu8qb 5eXlj917Ko4iCZYQhQhf4CBhd7n3MhCJU+fEPBTEkLWPoiLQGRFUA2KXZpl6xQM+qSKl ahgUiUVXHYZippWzgKYTSfVlz9D6yKT0Qvr6x797yZae/lepNYf+++tt1xRQmdR/kOXX az4g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722758332; x=1723363132; 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=Iy8s8HUuCpYYzqYVYDLibKnXKIG+UtFNyRBbbBdMpkI=; b=pWIELjGkxK9/Z0UrxLKZa4BHmUdHkT1W/8YT5CTtsOXKGSK6P1oDr0qw3o8DuBTmpX O4usv4XBJgb333jXoCMds5nsBMW8yd2Jd3RvnaPYLqtDda7mwSU1i0ZaJg/knowBcG3g 8x2LmjkSWWci7jiV6gKFWi19iUKSpTPs5FdgvfZxvpXktuERDu+2j1TPaX32titjLgTo 9OIwaPoPfFEZhfuP95jcJS9JNPzX90MqyfYdjx//M9HyEG0OJHOeuNgb9k+vAVH6O9Wk K/EIhBvVNA9pIRMZGNuBZ8DkRDR7QxSL5fXFhnchi2vkOg5SStRGTOGJh7+hYTZwqr4I e6aQ== X-Forwarded-Encrypted: i=1; AJvYcCVHx3Ecx3+QwjT73IcWGeILVS5SI1sv3J074nXliOwSIh9eqeeY9zzgFpzkqZa5lidRhDZfBA5aRaCLjYOePmDTK/o= X-Gm-Message-State: AOJu0YyoPar6BCUbCTeJpn6YxbKzGrEDs3ruFX5CkeRxu1KhG2haNeCJ QqjtpjgChTgjozKeN3TRkLYZKIaQQ7dg5xCTw75d/N47C2ZSu2qf X-Google-Smtp-Source: AGHT+IEAdxuBVNyLOuAyS4g3ON7ORD29wxk4r6CpWYYml3q2XeTU12w4OJdy/NnFgIlYXJGGtsOOlw== X-Received: by 2002:a05:6808:1984:b0:3d9:9e78:420c with SMTP id 5614622812f47-3db5583270amr9335216b6e.38.1722758332352; Sun, 04 Aug 2024 00:58:52 -0700 (PDT) Received: from localhost.localdomain ([39.144.105.172]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ff59178248sm46387605ad.202.2024.08.04.00.58.37 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 04 Aug 2024 00:58:51 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, catalin.marinas@arm.com, penguin-kernel@i-love.sakura.ne.jp, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, audit@vger.kernel.org, linux-security-module@vger.kernel.org, selinux@vger.kernel.org, bpf@vger.kernel.org, netdev@vger.kernel.org, dri-devel@lists.freedesktop.org, Yafang Shao , "David S. Miller" , David Ahern , Eric Dumazet , Jakub Kicinski , Paolo Abeni Subject: [PATCH v5 8/9] net: Replace strcpy() with __get_task_comm() Date: Sun, 4 Aug 2024 15:56:18 +0800 Message-Id: <20240804075619.20804-9-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240804075619.20804-1-laoar.shao@gmail.com> References: <20240804075619.20804-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 5EDC440003 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: n3udrj7cdg3zmh5kstp35ioh6izpund3 X-HE-Tag: 1722758333-566946 X-HE-Meta: U2FsdGVkX1/HTQREqpwPdhLsCO2HkPWv+RTakterr2o6SaWWnljkpQMijCKOKHXiJ8580zkl12qXTdPo5c9hii3M28xVvgq6KrKWq7CjuWdwMND/aqqfeuReenb7oR+d73tgPGdpTLenNQIsj3qEW5zDnwGWV5jzdxHNFWyJ3hk0bId49NUFKXpuVIOC/ErGAMB2F1RKk27t06LDzZpmSrx2VcSKDSWKsZaBVCovIq/Ifo77qwZL024QdngeR7DuNgBdi69gwE0bPwiDUk9i1cNrOCN4dVxjZKc7GUNBZWWcePsP3xAUqTRjlmQWu3hbvOJf0KypxVcs3mt4bneNLxQq6e0Vtxoh5Xq6d8rk54uUIazf6ZYbmk4A3D+/4UBlsjc3Y+M6DFUkRk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7ZkwnouaWXSzBrWAjufOPB5gwGBmYTooYSXuoFPTobSIpk4Tqc1v3e+rOfGrOw4iPMkcNlb7mXBKIw/IocszEy87eLfuZuHnAEwlqwYKqij8Jw0JHWie4mfHpp2iZi3A7ubNCusiOcdSjllMdmemid2yhhmLiChYUtTdaPJPLgzOHMZFIPLuR+AurrPpcTIoxCA 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 __get_task_comm() instead. This approach also facilitates future extensions to the task comm. Signed-off-by: Yafang Shao Cc: "David S. Miller" Cc: David Ahern Cc: Eric Dumazet Cc: Jakub Kicinski Cc: Paolo Abeni --- net/ipv6/ndisc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/net/ipv6/ndisc.c b/net/ipv6/ndisc.c index 70a0b2ad6bd7..fa3a91e36ba0 100644 --- a/net/ipv6/ndisc.c +++ b/net/ipv6/ndisc.c @@ -1942,7 +1942,7 @@ static void ndisc_warn_deprecated_sysctl(const struct ctl_table *ctl, static char warncomm[TASK_COMM_LEN]; static int warned; if (strcmp(warncomm, current->comm) && warned < 5) { - strcpy(warncomm, current->comm); + __get_task_comm(warncomm, TASK_COMM_LEN, current); pr_warn("process `%s' is using deprecated sysctl (%s) net.ipv6.neigh.%s.%s - use net.ipv6.neigh.%s.%s_ms instead\n", warncomm, func, dev_name, ctl->procname, From patchwork Sun Aug 4 07:56: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: 13752532 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 8B7D9C3DA64 for ; Sun, 4 Aug 2024 07:59:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2711F6B009D; Sun, 4 Aug 2024 03:59:12 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2201D6B009E; Sun, 4 Aug 2024 03:59:12 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0E9636B009F; Sun, 4 Aug 2024 03:59:12 -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 E393A6B009D for ; Sun, 4 Aug 2024 03:59:11 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 95E86C17FC for ; Sun, 4 Aug 2024 07:59:11 +0000 (UTC) X-FDA: 82413812502.18.1A8C3CF Received: from mail-oi1-f173.google.com (mail-oi1-f173.google.com [209.85.167.173]) by imf16.hostedemail.com (Postfix) with ESMTP id C8293180007 for ; Sun, 4 Aug 2024 07:59:08 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=apYQnpGS; spf=pass (imf16.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.167.173 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=1722758269; 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=leoSzvc1irTIRY4t+RZAVg9wXL8E4KK0Ycetp1tzeI4=; b=Q2wGt7BPy8QhNRn2bVdSm+eUB8e+3w2b2umIGfXHH7Ki50uqGTw1d7Sg5X+PSCU9JhGfqN 2713RZFXqntgLpiClx8iRG2QwGA6Fq+CJ0ypvkjge1UQ5vKLDWJlqizZ33zclPmZqiCulc 0BHYUZtedp+2TaTXfLRQDq5v2r/59SE= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=apYQnpGS; spf=pass (imf16.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.167.173 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=1722758269; a=rsa-sha256; cv=none; b=wx0CKscJ50JLSeQZCRhTr7StsH1cbE6lodFelm4mG0Xf7dKzb0eEjIygbVUtl287qqkOYW uaslbsKpJtQTZZ4Lw9hztxyU0PM9t9b01ybBGhRcdLiAU6Q6N8hh4p5GjY4YK+NiPqnXPe CYBhJLLNUVmnTR+J3ZAj3dmiHH8ZyT0= Received: by mail-oi1-f173.google.com with SMTP id 5614622812f47-3db16a98d16so6096104b6e.0 for ; Sun, 04 Aug 2024 00:59:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722758348; x=1723363148; 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=leoSzvc1irTIRY4t+RZAVg9wXL8E4KK0Ycetp1tzeI4=; b=apYQnpGSA5jfg54raBitLnSpMP5DiLjO8KGfkpYSVUra5c0dQ3NVjT7hWtLQhWgiEO VkVjM3tebvAAzCcez1ZsYHBJOP4u3Du2Dmx4JpvZ3fAU0RQa5HI74XeW+wVmMcs4mzM0 0Sg0OK4ahdKtDBgz72ds7/X3tPstDqRMIJBgi+8j8w1me4WcWNLbIO4+sraJp4LChvr1 h9mAd10pnC85vE5SaXJ7YQigE8a5Dbthq/hI7XsR4/HBlU14dMrMeUiifgTZMLLwnfKV 3uPW0WHyKcJpnaQUKMAw3prflNG7EzfSflfblE17drtwU7mbLvao7OnP6v6jMn1LIRVD 42sA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722758348; x=1723363148; 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=leoSzvc1irTIRY4t+RZAVg9wXL8E4KK0Ycetp1tzeI4=; b=OE5G0zl/7T/DwBLAgRdOxnAI817JlRwFP07232JQyKzYlJ+VfziNdht0fjligfFxEX JqlDDo012egs7/7oNbx0LfePyNlALLGeJZEwJUdDTR1NB1wLRYewjlt8vr9ZjNeh7lSi a9KNc9dhbI+uW9OJuYq5uaGkKBspDDJrluqEDefF/0lE1YleM/592e++uAvMd8n2vyTQ lVz56g/T1ikVPspAEG3XsXCBnW9VINrNcjeYQ9Cs780XfCvjkf3fokkjw7YDx1aCglrL 0gp2yUiFVMXHGXBD4Xb2TWEMWlmYB8pLGoJ7TNGSh6scf7PmWJQ+0OUySp71yBgocJO/ qwPA== X-Forwarded-Encrypted: i=1; AJvYcCX895FvkJwnRAl0aVe7FuRv0s6+m1tAPILX+iVKP43FALknbUirtDxolERAaGChSuHP830d4FYkrgSQOUv07CztPms= X-Gm-Message-State: AOJu0YzhVKmhTA5wNTvoLON5/Py0supnwcYvrt9V1hbiBLwkLvf57/b6 RqwuMEp8STfjY5LW/PhkPsJFkmiSfXFACY9m/Mu2k4spDrxdSAbe X-Google-Smtp-Source: AGHT+IFvxFD9i5t49STUUYBZ5sy63mPUTCqzaYfVU1LOK5V4jv+UubPbJD8tYCgFeYVMewc026EG+Q== X-Received: by 2002:a05:6808:10d5:b0:3db:25f6:a62f with SMTP id 5614622812f47-3db5580f909mr13121118b6e.28.1722758347849; Sun, 04 Aug 2024 00:59:07 -0700 (PDT) Received: from localhost.localdomain ([39.144.105.172]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ff59178248sm46387605ad.202.2024.08.04.00.58.52 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 04 Aug 2024 00:59:07 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, catalin.marinas@arm.com, penguin-kernel@i-love.sakura.ne.jp, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, audit@vger.kernel.org, linux-security-module@vger.kernel.org, selinux@vger.kernel.org, bpf@vger.kernel.org, netdev@vger.kernel.org, dri-devel@lists.freedesktop.org, Yafang Shao , Daniel Vetter , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie Subject: [PATCH v5 9/9] drm: Replace strcpy() with __get_task_comm() Date: Sun, 4 Aug 2024 15:56:19 +0800 Message-Id: <20240804075619.20804-10-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240804075619.20804-1-laoar.shao@gmail.com> References: <20240804075619.20804-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: C8293180007 X-Stat-Signature: j99z44i8hx4sxa8gfygt64trch4gn1rr X-Rspam-User: X-HE-Tag: 1722758348-98511 X-HE-Meta: U2FsdGVkX1+rzHLXXaQQUpoEOzw0M3xAFSzUDTtmAxL7eNzRTOg5QFj2OUoaOX8Rl0AXZ6W9Z9o7GeOuUnQc1jIsAxdT/iCFIgYkfi6fB+g+HPRirdAXK2yCUF8Z4AKn1D4Hgr5d0CcoCM7R0ZrUhbBsxqv6kRTb2P/iewHXZqDA74mUIG0UsWiuVj2cdSWsT/NYEZi/zCrBh9ufgL3xicpoGAPMA0qvAXXUPQ5lMqx6F3X8tVWUeUNv9chG9+xfshU03vLBfUBGam1XLkv80AClV33HP5CyLu4BAO2Jy1lTgZpQtrJm0c8VCn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LP+WGIv2CsgwINR5ivNqKZAytLDlT/dB0ILoN4AzCOfEMHwp5Dfckg7kSAnpEq1hTqegHJ74vmaMYrtG3dk3BgDJHC04XRwjmfCRAzJqVbJiYx4r8UURR+6oRfUeiI/3Czrt3owu0iw1Y6OklKDO1NTG6dRmDGsN4+k/rOTRdwa/xHeYe8OGdg= 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 __get_task_comm() instead. This approach also facilitates future extensions to the task comm. Signed-off-by: Yafang Shao Acked-by: Daniel Vetter Cc: Maarten Lankhorst Cc: Maxime Ripard Cc: Thomas Zimmermann Cc: David Airlie --- drivers/gpu/drm/drm_framebuffer.c | 2 +- drivers/gpu/drm/i915/i915_gpu_error.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/gpu/drm/drm_framebuffer.c b/drivers/gpu/drm/drm_framebuffer.c index 888aadb6a4ac..25262b07ffaf 100644 --- a/drivers/gpu/drm/drm_framebuffer.c +++ b/drivers/gpu/drm/drm_framebuffer.c @@ -868,7 +868,7 @@ int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb, INIT_LIST_HEAD(&fb->filp_head); fb->funcs = funcs; - strcpy(fb->comm, current->comm); + __get_task_comm(fb->comm, sizeof(fb->comm), current); ret = __drm_mode_object_add(dev, &fb->base, DRM_MODE_OBJECT_FB, false, drm_framebuffer_free); diff --git a/drivers/gpu/drm/i915/i915_gpu_error.c b/drivers/gpu/drm/i915/i915_gpu_error.c index 96c6cafd5b9e..163457a6e484 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); + __get_task_comm(e->comm, sizeof(e->comm), task); e->pid = task->pid; } rcu_read_unlock();