From patchwork Mon Jul 29 02:37: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: 13744220 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 7B52CC3DA49 for ; Mon, 29 Jul 2024 02:38:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 02FB16B008C; Sun, 28 Jul 2024 22:38:42 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id F10546B009C; Sun, 28 Jul 2024 22:38:41 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DD7E36B009D; Sun, 28 Jul 2024 22:38:41 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id BDFC16B008C for ; Sun, 28 Jul 2024 22:38:41 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 22AF5402F8 for ; Mon, 29 Jul 2024 02:38:41 +0000 (UTC) X-FDA: 82391232042.28.43E2195 Received: from mail-pj1-f41.google.com (mail-pj1-f41.google.com [209.85.216.41]) by imf09.hostedemail.com (Postfix) with ESMTP id 4BCEB140008 for ; Mon, 29 Jul 2024 02:38:38 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="Vl/Ix9uj"; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf09.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.216.41 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722220679; a=rsa-sha256; cv=none; b=SUhoTiPRyreR7gg1FIMlBnWC/nG8FWNgo9GMEzrhUNFnLRKtdiVArRBh4KI0TvbGIUZBs1 a1FFwQ6kws3fi8Rrb0iYOBW/QF+gz+/aeieqs7wix6akjgwb24on04/n6VKgmsAspvRXKg 8dMelhNDz25+/JwBO5+nXxNFfYdJQso= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="Vl/Ix9uj"; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf09.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.216.41 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1722220679; 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=/dYktotwfBu6Roz80A5YCXaIPxsklnKpN0JVw9YLBJ4=; b=ttsBvGFbWpn/G1teB1skJRsy0iabQN7Uu443hYqFqm8jiPkfh0jTzYnKu3dc9nf2y15oQ+ w+R8xl4Frd9xcu6VF74xUd2o2HD3PkarU839+Lee2FRrWq9z2PBDnBhTwXLb5u0R6jq9xD sZ82pDm6xG8voQKk5cohKdjyor/ZO6s= Received: by mail-pj1-f41.google.com with SMTP id 98e67ed59e1d1-2cd5d6b2581so1625809a91.2 for ; Sun, 28 Jul 2024 19:38:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722220717; x=1722825517; 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=/dYktotwfBu6Roz80A5YCXaIPxsklnKpN0JVw9YLBJ4=; b=Vl/Ix9ujRR4hHZhdUqmkv9+4IabdlgZ1UFR/3Zf2ul70L4AZ0n/YuuaE1cTAr/aBss dClVpriMJr+rMmIS/b5LThd+iFv+SHMDRwYGiAj5+fXsTgV/ZoZB/7SQNZTCyvPT5rrI 6pY1gYeqcpjo3xL3bsvT1haOwl06vVDf17VDsENpiU7AstHNb6EIqU8aNVl0HMcrCHHi c0LkyaXagSFVPm0Vwaixmsadx3HvT1f/L3LtE5gViDIzTpQvRfIZbxDCU1XBLyqqRQJT W9PM5OGT8bfrjOfZZmzvBkA4Z3YL1EeMGI2JXmdqh1ZflslRjoUlPAJqPHuXgAhCh4Se I0pw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722220717; x=1722825517; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=/dYktotwfBu6Roz80A5YCXaIPxsklnKpN0JVw9YLBJ4=; b=p9MT0T3+6EJYFtgXq98ELIRhFVl3Xk0mycUHnlvkigPRlVp47NLC+wngKNGPo4fTZr kRkpgxaUXBZV9b7DTYxVEen47QtTBMPkVw1HA7sVVX+1/7Bmi9YQOs6nNQ2gzkPfQi0V TaqcCWrcYxoUK2DcmijTFpAvYCslfXWE7x8tocR30J3YsADfBQebGE/cxFRKKANVY/Cb OIoI36aZwceNa+YEd1FmJfkZ1nMWj6ONbulD+M5H8E9HQKsgSpfxxy/PItHZEJ+S+ZQg 6rUieIXzbTNzRsLOn56eoSSEuYhn+Hc5qvwiOZRGPGJLIW4fodtDX5dVz1igooKlpXrO T/Zg== X-Forwarded-Encrypted: i=1; AJvYcCW3PZ8qPaxPneb238+e9hVjkk08Ufw4UyjzMBqpkfp/oUUm/UoYBsJjE4hbQuM9M1LiQgiLzHYL8NVd+ux926uizNc= X-Gm-Message-State: AOJu0Yw3f6aqBMh4UVxSvK9lZKRN5Lx76tmJ0j0CO+4Gu3nX0gN9V+Ok J7pphwJ2ql4DC4o16BJ1HZRyHsw1TWaDGa5kvQ7VmVTTwjk/zS3S X-Google-Smtp-Source: AGHT+IEUvSCxvTt2iFtiQZnOEMmTj53JiCANDbCPnBaNHXCCebMev5gTq4Ke5agmpYOP2sLQoQ29tA== X-Received: by 2002:a17:90b:3793:b0:2c9:5c7c:815d with SMTP id 98e67ed59e1d1-2cf7e2275d2mr4251165a91.22.1722220716985; Sun, 28 Jul 2024 19:38:36 -0700 (PDT) Received: from localhost.localdomain ([223.104.210.31]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2cf28c55a38sm7332247a91.10.2024.07.28.19.38.23 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 28 Jul 2024 19:38:36 -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 v4 01/11] fs/exec: Drop task_lock() inside __get_task_comm() Date: Mon, 29 Jul 2024 10:37:09 +0800 Message-Id: <20240729023719.1933-2-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240729023719.1933-1-laoar.shao@gmail.com> References: <20240729023719.1933-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 4BCEB140008 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: b13mctxiwh4gqgghxogeu9ob57x6ozoe X-HE-Tag: 1722220718-673015 X-HE-Meta: U2FsdGVkX18qXY7QVGyLejdR3w8H9OfTJ3969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gFsclJ94MBy8BqmesXNiluGWcQzotIULrBMC99Z9zo5mrucTlXz3TgC6EGowcT1Jbrn3I18F0RnkHS0vZRCqsZ6eDwN1NEzXZQxNKhJjYBeF8oM3KKbCvTgIBK33nvvvWl6+qSI3qwJL1EnOCyyOuDvXUG3SCh/V7wNdqFp3yYIr0tMWf8CIxL8NAYNVnw0xDuauaIn2PiA66X6jXJYu2Xrol5t6dIOCcHPvPjtfw+BgW5iYKu7SeodPAq2KcdcJgT/NuXnnrK8HrFXcHmKwlUZhDWlw/hJbqAk/f/EKvTi5Bw4gxudgelADLosZO1nzHpbR1IWqdzcQ5zqhaVI3taa7Rk4o+0BxnWaOQL6NJ2N5Eb+6he55RY/brlDY5KwhHaAeNqJIec+LuT/31bWAPw63kSGpP8QsOuXdP3rBfb0p3hfhGG4CeWTi6mwc+yq6YnitUVnyFBgkDHXsb5LcvtS8nAxOhXkTrfci9CKgdW5wgIOFkV9IaG6gjn0ycUq1oQCA9g19KbXEew9vCqgkA/ceHVZov4zs6i0AR6/hy4VZ4B7fBA6jy/WzIJAnxBMqPxnhU8a8PAExJmwdV/IMzK8JCE42RSyAo/LYwlMziiNPm5jFGeuExk+RRMiF4etCfyiQCg9UKYkC4mjwH8AxlN7aA4KIgKeGFwLriciuY0J9rIQuW42TNhCWoi9igomIO2YcM= 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 40073142288f..fa6b61c79df8 100644 --- a/fs/exec.c +++ b/fs/exec.c @@ -1238,12 +1238,18 @@ static int unshare_sighand(struct task_struct *me) return 0; } +/* + * User space can randomly change their names anyway, so locking for readers + * doesn't make sense. For writers, locking is probably necessary, as a race + * condition could lead to long-term mixed results. + * The strscpy_pad() in __set_task_comm() can ensure that the task comm is + * always NUL-terminated. Therefore the race condition between reader and writer + * is not an issue. + */ char *__get_task_comm(char *buf, size_t buf_size, struct task_struct *tsk) { - task_lock(tsk); /* Always NUL terminated and zero-padded */ strscpy_pad(buf, tsk->comm, buf_size); - task_unlock(tsk); return buf; } EXPORT_SYMBOL_GPL(__get_task_comm); diff --git a/include/linux/sched.h b/include/linux/sched.h index 61591ac6eab6..95888d1da49e 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -1086,9 +1086,9 @@ struct task_struct { /* * executable name, excluding path. * - * - normally initialized setup_new_exec() + * - normally initialized begin_new_exec() * - access it with [gs]et_task_comm() - * - lock it with task_lock() + * - lock it with task_lock() for writing */ char comm[TASK_COMM_LEN];