From patchwork Thu Jun 13 02:30:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13695897 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 2A85DC27C53 for ; Thu, 13 Jun 2024 02:31:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9D1346B009B; Wed, 12 Jun 2024 22:31:14 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 981466B009D; Wed, 12 Jun 2024 22:31:14 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8225D6B009E; Wed, 12 Jun 2024 22:31:14 -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 61F566B009B for ; Wed, 12 Jun 2024 22:31:14 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 18B841C2DF7 for ; Thu, 13 Jun 2024 02:31:14 +0000 (UTC) X-FDA: 82224288468.12.30BCD66 Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) by imf16.hostedemail.com (Postfix) with ESMTP id 415DF180002 for ; Thu, 13 Jun 2024 02:31:12 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=L0DY5MrU; spf=pass (imf16.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.174 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=1718245871; 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=By5oteG2vYKNiXXwbcojQ0i5z1H7WpE2IA1E0X7Qrhm7JPh750n544xESTdmaqXO11kpam mHaJHW0xkzyLPOdgJPZPBpCsKTJS1SAGJE4Cz90vS7GQT/ODUuOntAlCIVbFUfl1cUMbA6 NuybAPNcKiZ7zt+r00eCPhHVaUyD1dk= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=L0DY5MrU; spf=pass (imf16.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.214.174 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=1718245871; a=rsa-sha256; cv=none; b=pUAc0LZ+9tMMDTHUlVJpySFY6W0vkb0GbcxfC/wQwxrRmwApB3STptSS3tVflGnoywC/Cy nfuT+2/8HcqQi88hu8Ou5JGDOUClReJky1JKPsYsi+yB+C7v2Gskd6SNEQTlCVyt6u9nVp BM/m5eOtlIkgFIfv6OK54ukJmP97VqA= Received: by mail-pl1-f174.google.com with SMTP id d9443c01a7336-1f44b5b9de6so4604295ad.3 for ; Wed, 12 Jun 2024 19:31:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718245871; x=1718850671; 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=L0DY5MrU9z3Wag7NqA6NVfzixe8rQeCVEmPRG59qihT5d9cSIh9MEvwwTESkccjEVS rmUkc8T5Xn6vtz3oBE7bRd8Kg0x7JPIq6ymViUs2f89q8hyKyNpixWTj9z7ff4yMkbIb dEedt3qS97wn3ijRfU+8q+6rUavX9Y09zZhVW42Llq5RwvmluoCsKXqN7/6EMvu37uG3 AGHOnYW/i6FMtcLbA83Jk0jXjGd/Mj48v0whq28VxqjXv+ForYFKlbfVfIqZ6Qy5jWAN buou2xWU6FkYcyVdseVRrpLpXHP3/Re6J1JGzweqB9FhdgP8EaZy35yQMC3Tytk75GAD qWyQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718245871; x=1718850671; 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=A2savAgI75I1r/hz8cZYIJeAA/bxA7FKfDBiJDpbzbhIt+kvA5hBv/MQqYMbfK6eDa W0A72c0aD4jtQHcXYa8rD5qsUGuyUYOyFRr6yhL1RSb30gdcHZf/ETn8+6XRCXY7nkuH 4wm0ooAyZYrKSpGxGV63fB+7wpVYSL7rLL6xPJz8LF03hwlmUoarlbFoKE1w+Xs04QyI eiiWolXyXZ8iQrxi9sX1q075H7kZnmyseB7+jyD0clejf2b6+Gdph9Y6wHoRWvaIiaQC VaTxxdSJjJgXuhlWZQF4Gih3O6YkPYXuCRhsFlqrnNeV77q4PN3SmlcZ15AVWKkvgHrT jHMA== X-Forwarded-Encrypted: i=1; AJvYcCVXDdo2VzUfjaUibawcS4v4DXr37N6+PO150fJ0hFjGd/fVMM6BrUnfkfPHKw2Mj8pZTca67SMRA9QDa21WimU/ODA= X-Gm-Message-State: AOJu0Yyn9CyZC3JK7YfJT0GqRfh/AFgySQhmaSXZlmB1AvlmiHdlnE2R 0I5L4eGL0KUHI8cB81ga/dP2HkflJkxU2VIwcC5e1+YV1VQ0pGXdUs/bQVLW8X8= X-Google-Smtp-Source: AGHT+IGepHvi+lKqMo1GYaife0L6fUJVRPiktMyHkXeHwR+7tr6qOJAeLHRN5Lin69VRxAqus5DeJQ== X-Received: by 2002:a17:902:ecc1:b0:1f6:6ef0:dae4 with SMTP id d9443c01a7336-1f83b5f3fffmr43616545ad.32.1718245871101; Wed, 12 Jun 2024 19:31:11 -0700 (PDT) Received: from localhost.localdomain ([39.144.105.92]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1f855f4d159sm1755695ad.289.2024.06.12.19.31.03 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 12 Jun 2024 19:31:10 -0700 (PDT) From: Yafang Shao To: torvalds@linux-foundation.org Cc: ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, 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 v2 01/10] fs/exec: Drop task_lock() inside __get_task_comm() Date: Thu, 13 Jun 2024 10:30:35 +0800 Message-Id: <20240613023044.45873-2-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20240613023044.45873-1-laoar.shao@gmail.com> References: <20240613023044.45873-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Rspamd-Server: rspam11 X-Rspamd-Queue-Id: 415DF180002 X-Stat-Signature: frmkb3h9z3zhrzsmfojpfa8u4fejq3ip X-Rspam-User: X-HE-Tag: 1718245872-57740 X-HE-Meta: U2FsdGVkX194RrxsQ/bIHI8kLlxXkp6uba0uAj2SAUtfausn3ZKLSPmiJXJeZEdNGPawrBDaqzwbTTIlwUE1O7rE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eYizW4INtjEs6HwIRzGEJ3bP45OpRQws6KEYLfrDWcj9vB3jgJ6ocEdtMJ2jeNv/KDsmFnw7m9Uo6U+w80x6PXyJA5AD5lqrnVzQAiqt7qD2aUIuySY19jH9o52BWiLAyU5CgYolE0nr2NDxRHOI7vARUheAwMU72MxjNVxiEpMqNvDqed/o8hhtWFVufv6ybEdRfGVoVcQWBXMtPwW8HPC75/cwYHIQIHSnDqraLQ7M7vOqXrrxMCxkpAKV2nsWSbT3xOT35DmVU8RM0KnKer2+g65XE0txehbpgxGPueCCWbGamT0hQPYHJLcuqk8qKkUrqzy8/Z4IQ1pyEHM7rPm5CVN5Jrf+GMBY0C/ak0PNFvmXn9R531bU9vULZL9RViNm+LXPI9O3vPRqlkXgtwLB/SD0lRyCwRKuqE9OcrUNFkF5YPBBoRKyhwM6YF8tcHTZsS7t0s4AU5P64N2v+lTru5fdDTHj65VA+0hvuQRKKQM4scWJB4dQim+mVOkMfjUmYyWTfcRryQqyPpk6TCQu1UJiOEVd2y7hB/qTU06T3IMqpmPw9rkzObk0KYKQqhXIDp/CEc/qetsltS9+gF07f1YvLiUHXfenNETZTHij47uVnN6KJaLsreM= 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];