Received: by 2002:a05:7412:2a8a:b0:fc:a2b0:25d7 with SMTP id u10csp187504rdh; Wed, 7 Feb 2024 01:30:03 -0800 (PST) X-Google-Smtp-Source: AGHT+IHzSaqPenIkvZ5VcXBVs9U/btB7jHqhTuvf/IyjeiYEZDN8zcxxt3/nxD7M+82wQ+fcCV4x X-Received: by 2002:a05:6a20:1451:b0:19c:a16d:ca79 with SMTP id a17-20020a056a20145100b0019ca16dca79mr4692167pzi.15.1707298203568; Wed, 07 Feb 2024 01:30:03 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707298203; cv=pass; d=google.com; s=arc-20160816; b=LqQcnIlZLekI2mfdfkMDIzcj8c+Ltz1YjVTCMX1GeKrR2cnlPPCocLMFKSrTP8jSls BzK0rL8nZce7/PK2thPV8TSkfF7IY9KXaTfhMkbtKeC+d3K1Y+UswIgfoeIgm05IHB8E HaoW79KylgXQ08waBrCYQUSf85/c5XlDZlM/cZnlf+oitARZJym4R/qdJIb+VnGyGXf7 IP7qIDIV1OXMQVf3WDCwcqcX6beMdhWMQK2PHEhNqxm5j8gHy2Q993vLN7shLE+kQBLD zxHRQA1TjJd42MW25IzNkUSbjsr7uu+lRwffITaZFJJUKtgKs8DDj+uc5CYhiyQdJTyI M6mw== 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=5hKkth+9vDGqSAQyT+7ZBgY/ZHUIPcG1abCUGa70dB4=; fh=5K+HwuE+zwv8A9DaYoLuv99IM/AzT1c3Slnrokp7jSs=; b=U+yCkQU9OigoN3vJOuJp6lqEp341hFT13l9HOBsei/KGTUU3Pgpf8qNBYos1CR1yYg UInkyBA6rkh82ih6qh1ykoVf4p9Hy9vc739e93/cnVpdxjZjjWuQrn2v8Pvai4NwO1YO tPk7F27gydaMYC27qB8p3I5/c25IiuEwSQCpinkg1C0TeHPDxOraRk1N4SfNpizMoSQn dyZQJnCT3SvRPf8xeCGK5MzoupdndbFRJPdSglmypVO7wolHgpYCU8XT74baB4Vhpc0a tHK9DhxLcVHhIda3P3CB8agDI9zWo5kiZI94iY4pmlKI+E9nZo/5WvCO3fCKStw/dGAU WtJA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ViONNc8y; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-56206-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-56206-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; AJvYcCVNOihZ/9oS8ObCFqBXRdGyzJDwia+rMYz2qjESiucVGgLSCNKAsH4hWSXiJGjYE/fmArlFn+DH8CQfHVN0wjJqpR5h6/KM/PcabhqnNw== Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id q5-20020a170902e30500b001d781dc0f70si1097102plc.591.2024.02.07.01.30.03 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Feb 2024 01:30:03 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-56206-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ViONNc8y; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-56206-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-56206-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 3ABB92834F3 for ; Wed, 7 Feb 2024 09:30:03 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 13160219ED; Wed, 7 Feb 2024 09:29:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="ViONNc8y" 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 3423020DE5; Wed, 7 Feb 2024 09:29:55 +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=1707298196; cv=none; b=kzKgcMGl79S6Q3z7aKv9CZDm0e3/oi5yvA+71BFMSP7X86PFNlFOvK9adJEDl7ljPyL6odJluMp4RIphDWneY+eWIg5VErYsOGxrh0LH3yAIPskQHgn1KNz9RY8PyHSz0IqPkzqgWx2fa4NLtQ8aTzwXOZB3jsdiGynNr6QDntw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707298196; c=relaxed/simple; bh=AqL1GrUDNFS2//C3MYuy0qq/ymNdLTzPmwXANRzTwmE=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=U1uS6OGva7gDFScRm6DwtzzSsiB9NBR7OlBAyeS2pJ7tQ+j+hTT10LWmRnwXsMyXsBj6l9OoZaxj0j7WR9UKgpiu76QhwT0//xiwd0y292r3rnWXuJXwypaNdf1XfDDEx6E7MVUMldZABVoEw3feQVq9Lnqs80TOA94GwxzenpY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ViONNc8y; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 30C2EC433F1; Wed, 7 Feb 2024 09:29:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1707298195; bh=AqL1GrUDNFS2//C3MYuy0qq/ymNdLTzPmwXANRzTwmE=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=ViONNc8yJsOV+qb8Z9cQNugPfNmJOGxrbuKKUoB11Lwbgr8NFXR+GkiTaufnc6Fqs mx1n+KjKGfh0vStzchhZZmb4c9w89GYGHBfOJKNwiXwlaYmZaHH80KMqOL8zkDZNGX D3kG2bBp5wLl0LfMhM2DraoyKVppeY1PsHQNwl9WynNzAZp+ivYNvejTU65SPvSDUh 4zVa7rBmhATzUjse7dmsC2rbk15BZLjZkKlop/nboB2pWiuF7r1eJ9Fjr7X9ntjWfd YcNOEHbkzqcx/iRXfQjUxWLE2wkMFWUNUkfkBx4CU0ekVJgUOtk+mXPVA6boZVhtBb +mTWppJv4SyIw== Date: Wed, 7 Feb 2024 10:29:51 +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-daran-fliesen-6039a2e36f39@brauner> References: <20240206192357.81942-1-tycho@tycho.pizza> <20240207-vibrieren-waldarbeiten-30eeade05203@brauner> 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: <20240207-vibrieren-waldarbeiten-30eeade05203@brauner> On Wed, Feb 07, 2024 at 10:15:25AM +0100, Christian Brauner wrote: > 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? s/exiting/accessing/