From patchwork Fri Jun 21 02:29:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13706692 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 E36BDC2BA18 for ; Fri, 21 Jun 2024 02:30:50 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 729E38D011F; Thu, 20 Jun 2024 22:30:50 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6DA1E8D0111; Thu, 20 Jun 2024 22:30:50 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 57BCA8D011F; Thu, 20 Jun 2024 22:30:50 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 32A698D0111 for ; Thu, 20 Jun 2024 22:30:50 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id CF8671A081C for ; Fri, 21 Jun 2024 02:30:49 +0000 (UTC) X-FDA: 82253317818.16.5A87957 Received: from mail-pf1-f173.google.com (mail-pf1-f173.google.com [209.85.210.173]) by imf01.hostedemail.com (Postfix) with ESMTP id 05B3B4000F for ; Fri, 21 Jun 2024 02:30:46 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="G/tQ1dRD"; spf=pass (imf01.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.210.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=1718937037; 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=Zt6Zo0BQwE9mYz3Q0zCJOWUGA/XKEFxfC7Idotg6GZ4=; b=pLcgwh53bH29iMo4EFs4XkMCnjjQQHTrbsxykxempg+olLyAQ6ykgUjNsjBcfm3iMjcd2N M9yqjhJNI8B6dUgSnNTF6kfBIKammHsrp00jpvkOcxCpupGNNQjpcWl0n40CoQNDGiW/vh hbGWdnkr1AeYuG8ocomNoz9IU1x0KPU= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1718937037; a=rsa-sha256; cv=none; b=P2pgMW7KbI4TRCP9eIlj6u2HFkO7crwPzN5MaML461IOFk0fX2YI+40vsefv/pPkYG06rN fZ0pV/Adbo0j9B42MjS9jsMAU8rjv1Kx2J2eEQmjYiE0lHxZxZm6m+amb7kGQCdepdogXj JMdmVDDyIsSkma6a4yAhKlHubLLhTEA= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="G/tQ1dRD"; spf=pass (imf01.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.210.173 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pf1-f173.google.com with SMTP id d2e1a72fcca58-70436048c25so1557847b3a.0 for ; Thu, 20 Jun 2024 19:30:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718937046; x=1719541846; 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=Zt6Zo0BQwE9mYz3Q0zCJOWUGA/XKEFxfC7Idotg6GZ4=; b=G/tQ1dRDP9NgmrPantwEnY3Ed/N9d4jjfymGEGhSRQF1BBsM57As1mDVJByDnybDmY 5DIITEpPbkpj+96mqKG3E8FmAH/Jd9ITkEVL+wgivgEMmRA6y8K/7vu7+EGiXkAoAa5i lQrYHEnXQjlpoLC69o07kkKhw+jLf30+tIYkcNlhCVmyhsgfWAUMishW8JmV2OBs/Q33 YFbsLVED05V4+m4CbBgIshaYIQEQqjI9xyFDHWVO5a3Ax6xzok5rmlL7BSFUp81r1rp6 +qhGR1+v8b9S7pY+rjABbRfD4y3GhROCwQBilq2jl7TLrUHHzlqlDTjtfL3ybaOZfa33 GNYg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718937046; x=1719541846; 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=Zt6Zo0BQwE9mYz3Q0zCJOWUGA/XKEFxfC7Idotg6GZ4=; b=BFL8OlBCBTVV3MaMxf2LoBUhidV3bOS2OgvQo4y4eXK+JxV5Phl2WjBHRsPjdU9pPc H1+yjK0Jmz1U9Ds6beEscpXUTWJ+/qk+kDFAei5JySwANuRNH5P53SL4kECofHFwqwW5 4B/bgNrgqCPwlvxu2qFQRXD5T2D4FqWC5NFw51gHCuCNSzXeMQnnqTymJwv/EHTxfbCe eEeztTc1wX22QKxNy+T+9rzIvx2QdN3E/YXSUrAs/I11Wb3l1IYoX0ypiz6DBjnaqwfg d3MwTdjJ/OQ8e13oRsiVW9c+cDInM1ncE9+wcrTk3lwt9KKLsv9kCwgAgQmMhN0UM9jL pafA== X-Forwarded-Encrypted: i=1; AJvYcCWbXs5hIyhHLd0ov4Q/jI9Um7nlq7WEkwG7mZg5TqjU/DS4zo1nO39GqgA3pzdRQ6IafAiCPPOVXnC+h8xOjqXO7tc= X-Gm-Message-State: AOJu0Yy/IhJV3J5fdJRI4+sveqvb3qTjyKaTqBh3YNN2RZWqAXXf+qF1 VI5Pwxri/uIA0lYcYjjc3kJpgFqQJFvAEI7kTZ7Ujwy63aSXeSvgB7CaRKsF X-Google-Smtp-Source: AGHT+IGtfjPn4RgUkASb+Xbc5n3FAl++/e74fQb3jmqCcMo9NRTzTfAqzNgZkCzFDeDT/EYbemTCfA== X-Received: by 2002:a05:6a00:22c5:b0:705:ddbf:5c05 with SMTP id d2e1a72fcca58-70629c4204cmr9525016b3a.11.1718937045659; Thu, 20 Jun 2024 19:30:45 -0700 (PDT) Received: from localhost.localdomain ([39.144.105.24]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-706511944d2sm332488b3a.70.2024.06.20.19.30.34 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 20 Jun 2024 19:30:45 -0700 (PDT) From: Yafang Shao To: torvalds@linux-foundation.org Cc: ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, catalin.marinas@arm.com, akpm@linux-foundation.org, 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 v3 01/11] fs/exec: Drop task_lock() inside __get_task_comm() Date: Fri, 21 Jun 2024 10:29:49 +0800 Message-Id: <20240621022959.9124-2-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240621022959.9124-1-laoar.shao@gmail.com> References: <20240621022959.9124-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 05B3B4000F X-Stat-Signature: cwbsaifitnq5he7qfcqim1kxrbc7oc7o X-HE-Tag: 1718937046-944758 X-HE-Meta: U2FsdGVkX195hpjrficwqcDbR0vatW6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EMvHOAqThQg7XXJf6makH9Mqq1pcKBa+XDDoBLvoGHLAd49v/GuxrxH9QojAy09kEaGk141+T+p0cTwcxqSVVEplujEoU3z7yOxZ8YRmS+Ux7i3/ZE0kxkswhrd4bSUPl/AVJBo+ORIOt50Zf1NDnNGZyecub0VLzEbGL67AOJZ3Pm+LSREwgZRcbISo5peX4nM+lSCl12UIia301Trkd7WvZSZHKY9EPU3/X7aIPJofkvAXL6oP9yqKoxTxyqU39/YxuF1wTz8F5iZMekzPltSr3SLF/pjbCbgj5Y4jNJJAZ3OXkhBQ57MPs08Uex7RzYnL7AEYEmk8IMbdnUJ+G8NOKUtG0+0q4AM5/TQfly7Hu3ETOQ8Vw74jNUWENKWkLgn5ZDstYyS8MDByRlxrxh77aBhICZTVRGVO6lw09E9tSM0yHncN9pY+FH4uzSdZhcJNukbXXSBajov+jVwZRVj8jO0Zc/NdZmm4WVH6tLj0yOFxsrLBKqpAQidG098iAxjFnymUk29hTfhWcEORKRLf0amKurutjvjADRy4FV+e/A0D2KDGrcwaG3rV5HrvAnX8/f+UtOJfbD/+gX4Zm0RfGn3IJduVI/CkXmCDADq9JkOuWIFmBXO1SW7VaDK82OmbU40g/zicFY48JomGhEKKB9hNSXJbFfxoUWm9HgiugRpHEMjL/uCAaYf3WBVNFz8JNa+A= 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];