Received: by 2002:a05:7412:2a8a:b0:fc:a2b0:25d7 with SMTP id u10csp181319rdh; Wed, 7 Feb 2024 01:15:30 -0800 (PST) X-Google-Smtp-Source: AGHT+IH/g7yjFtRaOzfvV3i+heUxtVCl/P816BVrJtB5A6isNnF3tOxtSWBrqf9CDMxBn4kDUOTB X-Received: by 2002:a05:620a:5689:b0:783:e9ba:7d2f with SMTP id wg9-20020a05620a568900b00783e9ba7d2fmr5306648qkn.48.1707297330344; Wed, 07 Feb 2024 01:15:30 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707297330; cv=pass; d=google.com; s=arc-20160816; b=rMHVp8ErkoZlZZkHylpDApQbRU0nCZZU3MUqojKiCU0KU9aa2/Xnf6FquCzc4z3QJi 4wXN4FUc+fZYGGnP7wAmwS92r+tysw5v0MZ1AHtPclwfMvrxQYLmr/v3letkWhOHmGxh hO16ZkVF6EkPVv++aAqlGCZTTCnTKgBruyQz2aM3UT1JU0NxB6xp1Hr656KBu23xWEJ1 TwnRLXFf7iPuIsSGGGqzQd8hea3AQ2LUGzHzTm9u0q8gzD8AwjGAdRM4Hz7BNItUKkfZ LALfyYnaGm9vLZDPc8YzjCxSg0j7fghgXDjIkTP6SQIcHnCW8yuT5T2JtwhpTnP9U3lE QeTQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature; bh=qycfQY7WpATGS5KSFOYCUWWSWXpjxhLgPHolg3EUGiM=; fh=mkknl564nSSZZKlX7pXXx4bsIzv7GfbCE4WczDiz/UY=; b=vqBYURR8bZ9/yuD77tkyns4GTZs+TloHqHUJ2KXmBQmnoe1DuQudm23/zQZT8txYcY Opxk+qsUU7ed7Tgv+ZautIXsQmL/HDROxFC4kY4pjryvY7Hdn4rtQPe/jEAyHhBC0nB3 IUl6DPZu46OjkOiBNotS3Iry1dXLQCg/iaweX73gaOTYS+9jbo4TGRryVjgr/a0r8VaJ 70nXTycUBIqcKyEOq1BTXN3ndxYlnt4CpHtlC3SfZ1FKYR8io875Th7d2Ea3VPpg8gMm EOJ5gbvLiof1hT0s1WKo0K/1ieVtIVRoJbSJwMMO4AXNH5sbVl6ZjgtDd8K5aaGVE/2T VL8A==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=nwiXccck; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-56178-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-56178-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org X-Forwarded-Encrypted: i=2; AJvYcCXSetH9Icg7QQF9YlM+/3FPRExYSttdnw8YEsaDuj8GbuG5t+U7OSyYkg1CBQnp+AKyeR7Vu8t9nz8r/i7iH80uYI1JGiR/OEJZJU2RyQ== Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id de39-20020a05620a372700b0078591d41ebcsi783822qkb.46.2024.02.07.01.15.30 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Feb 2024 01:15:30 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-56178-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=nwiXccck; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-56178-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-56178-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 1AA0B1C22C8D for ; Wed, 7 Feb 2024 09:15:30 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B63AA20DCD; Wed, 7 Feb 2024 09:15:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="nwiXccck" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CAAED20B21; Wed, 7 Feb 2024 09:15:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707297325; cv=none; b=VAUuAFlASaFpvsLA8n7dT+E1BK+Qx8CijqlsO1kvP4TfDP6TjuLnJCP1Af6hUVhXq5RHYJXiMP4GouEcWczsyAq8ByQ4P0sz1K+sqRS0xMVblRIrXc4r47TFa2bfs3nLW7PN2tJDMVW+s64SKeIxbrCMQvEwO8UJ4FBylMtPQXA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707297325; c=relaxed/simple; bh=PdzsS0hD82qvdEpGRHg6Fb2zmu189QNDYRg81/jndD8=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=iL+Bmag4J1dpq3QXI3F3Cz6c0sZCXYamepbnqq3ZVt1xb6u2gNKsIA4bULjrVMWNu6tF5+G7m3YnzglDs3a6AMHH4S+LAegaySARTk0HSvjhyFzEruRL7/AXUXFvlGfgIj7o2cB5iJHTy9AohtA2BcR5qFisnfghAkneYzf+xi0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=nwiXccck; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 98072C433C7; Wed, 7 Feb 2024 09:15:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1707297325; bh=PdzsS0hD82qvdEpGRHg6Fb2zmu189QNDYRg81/jndD8=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=nwiXccckQ8miYKkauGuR2oS8cquNdxFMONQmEFEHnSoDA6w+ute/EcJBTF+VK+VW7 k9jlBTxNUhq1IHlridQCSTXM1ijvkGF0b2lX4WTyj9uVeRwYl0AbruO6xpQ7x/eOwu Mt0TGaboGNc5las8H/5jxwdJMycwdl0q46gNZLzUx85Onwne02Uo6DIsNnD5nOWXDm P+dsWU5S+Q26FlshAKGKP5o/6dAEmV44ZatEk3dzB7mB9Nc2I0GjD/uCi6YwSep7hS Lzd0hWWFtDraFDO/KphLjumT1IxSVKmWoLhgyMDAGL/nUbG8+t0CqlXUkSm/OrZLBG nOwKVdlHsHHIw== Date: Wed, 7 Feb 2024 10:15:21 +0100 From: Christian Brauner To: Tycho Andersen Cc: Oleg Nesterov , "Eric W . Biederman" , linux-kernel@vger.kernel.org, linux-api@vger.kernel.org, Tycho Andersen Subject: Re: [PATCH v2] pidfd: getfd should always report ESRCH if a task is exiting Message-ID: <20240207-vibrieren-waldarbeiten-30eeade05203@brauner> References: <20240206192357.81942-1-tycho@tycho.pizza> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20240206192357.81942-1-tycho@tycho.pizza> On Tue, Feb 06, 2024 at 12:23:57PM -0700, Tycho Andersen wrote: > From: Tycho Andersen > > We can get EBADF from __pidfd_fget() if a task is currently exiting, which > might be confusing. Let's check PF_EXITING, and just report ESRCH if so. > > I chose PF_EXITING, because it is set in exit_signals(), which is called > before exit_files(). Since ->exit_status is mostly set after exit_files() > in exit_notify(), using that still leaves a window open for the race. > > Signed-off-by: Tycho Andersen > v2: fix a race in the check by putting the check after __pidfd_fget() > (thanks Oleg) > --- > kernel/pid.c | 17 +++++++++- > .../selftests/pidfd/pidfd_getfd_test.c | 31 ++++++++++++++++++- > 2 files changed, 46 insertions(+), 2 deletions(-) > > diff --git a/kernel/pid.c b/kernel/pid.c > index de0bf2f8d18b..a8cd6296ed6d 100644 > --- a/kernel/pid.c > +++ b/kernel/pid.c > @@ -693,8 +693,23 @@ static int pidfd_getfd(struct pid *pid, int fd) > > file = __pidfd_fget(task, fd); > put_task_struct(task); > - if (IS_ERR(file)) > + if (IS_ERR(file)) { > + /* > + * It is possible that the target thread is exiting; it can be > + * either: > + * 1. before exit_signals(), which gives a real fd > + * 2. before exit_files() takes the task_lock() gives a real fd > + * 3. after exit_files() releases task_lock(), ->files is NULL; > + * this has PF_EXITING, since it was set in exit_signals(), > + * __pidfd_fget() returns EBADF. > + * In case 3 we get EBADF, but that really means ESRCH, since > + * the task is currently exiting and has freed its files > + * struct, so we fix it up. > + */ > + if (task->flags & PF_EXITING && PTR_ERR(file) == -EBADF) > + return -ESRCH; Isn't that a potential UAF because we called put_task_struct() above but this is exiting task->flags afterwards?