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];