Message ID | 20240206164308.62620-1-tycho@tycho.pizza |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel+bounces-55331-ouuuleilei=gmail.com@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:168b:b0:106:860b:bbdd with SMTP id ma11csp1667217dyb; Tue, 6 Feb 2024 08:43:49 -0800 (PST) X-Google-Smtp-Source: AGHT+IFNXA8rJMKY6Xs/MfdrC/HQoRyBJ76wfERXaLROszM9O9l66ivq5tX6KIZnbu0koeLcS8sD X-Received: by 2002:a17:906:6d6:b0:a36:600d:8911 with SMTP id v22-20020a17090606d600b00a36600d8911mr1888581ejb.10.1707237829349; Tue, 06 Feb 2024 08:43:49 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707237829; cv=pass; d=google.com; s=arc-20160816; b=iz8Q8wJbIdGDDMoUIKbYjX9hLTbNXuv3+K8+/avJVRRk1FTqP/TXAMRv1OiE6H+K3k CQ4uY+/EUe9ErbX1D7TvbcTJ7Gt+47a+E8YK6AiMKKbKzAODrTX9hKUpHCLe8/NNZzw7 jd/UqSA0hAYbse/+XaX+tQHL/nXqaUgnvQm5ka1hJeUVXH1eH/nMWvR9gE9OSac2/kxR yMBnNY0eOM/4psX++iSYXFVz+3boqGdC3UEGWf7nU8NwCVbtHQWnpoCR1fYpBWqd1UHa HOR+ZCPqPMEfCJPtgk29FQRQlaFCe9a5H7ocANqGzryijVOACpss2uiphub7BWeWFabv xdfQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from:feedback-id:dkim-signature:dkim-signature; bh=P4gQPZry16ZhlVFQebz1H5tj4IjCcocpT+0mBjF4AJQ=; fh=QBJ/ptM1QKuE3FQrjVRmaXU77OOOmluQr80PCUdr9pI=; b=Cw/jstN/SDiPCHvCIOUqzHyGx3K5bZwZ6Vz4t/s6lDLqq1sl/0Njytxf6gCyK9p/ag XAiQdO1AXueTf+60HnUbWQ758SzKqnqMtAb4OOID41HOAGx/KUqEtdtkKNgsBCbb6Qi2 1pb+v4+0v50YACPgQcWvk2+MRFp4uEb5bksyTK/dCExi4wSxNE93m3oYiCKo8cmBCwYz B5dLN+tAKGGUqK19OS99BVZmaoES7HhT+ewymnt3gEOZIh0Ug3LyL/8ZlrvE0jyJ1joc 4I15IwygqwFzY5L6Ep+ATk9/2zoTO2mMni2g7rnYKn35RNCJrDsNfM1OtH+90UqxQkIO GhSQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@tycho.pizza header.s=fm2 header.b=nu9Rh70Y; dkim=pass header.i=@messagingengine.com header.s=fm3 header.b=fPeHNZdV; arc=pass (i=1 spf=pass spfdomain=tycho.pizza dkim=pass dkdomain=tycho.pizza dkim=pass dkdomain=messagingengine.com); spf=pass (google.com: domain of linux-kernel+bounces-55331-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-55331-ouuuleilei=gmail.com@vger.kernel.org" X-Forwarded-Encrypted: i=1; AJvYcCVVRVrK0zP+8ZQPZCOvnhxpc3eQwRQ17oodQCYKh7FT9vRGsGLLh4kblRA1bNczGblSRTHxB3e4I9kvYWFpLTa366qzVg== Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id le8-20020a170907170800b00a366c968e68si1294708ejc.196.2024.02.06.08.43.49 for <ouuuleilei@gmail.com> (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Feb 2024 08:43:49 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-55331-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@tycho.pizza header.s=fm2 header.b=nu9Rh70Y; dkim=pass header.i=@messagingengine.com header.s=fm3 header.b=fPeHNZdV; arc=pass (i=1 spf=pass spfdomain=tycho.pizza dkim=pass dkdomain=tycho.pizza dkim=pass dkdomain=messagingengine.com); spf=pass (google.com: domain of linux-kernel+bounces-55331-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-55331-ouuuleilei=gmail.com@vger.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 am.mirrors.kernel.org (Postfix) with ESMTPS id B99B81F2653C for <ouuuleilei@gmail.com>; Tue, 6 Feb 2024 16:43:48 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 651706FD7; Tue, 6 Feb 2024 16:43:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=tycho.pizza header.i=@tycho.pizza header.b="nu9Rh70Y"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="fPeHNZdV" Received: from out5-smtp.messagingengine.com (out5-smtp.messagingengine.com [66.111.4.29]) (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 ADC44523C; Tue, 6 Feb 2024 16:43:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=66.111.4.29 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707237806; cv=none; b=VDL49X5OPQYmt3kjCHWfoIIvMSvnnAOZKtS5Ji9SdJ/H2dBMFCFtyleaV3FcYOUmr1Wrjv7GrugQZLKMSYAkMAEY3NmMx4rQsrhaWYxbkeiGyxboKwRxqeBalJBM2SLKbAonKk/A1GMaa5zmk9o9K603AGJCkxHUcemCU9pByZc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707237806; c=relaxed/simple; bh=H3sA3HP6yiXiZD2YbLYrofwBQrLibAkYudoYFsj7hBg=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=KIyGxvjsU9uPLrA9IHf/0K/LSW7OLZikr8aSFjxuC8IobzG0D4Jr4tVbC9GFF1courJWrssYT3YV+WJioZldQwpMvRsZ1g5hBVacyxv3kZO4UAtI1YOdVQ6YNAPazCTvpI8xlJ7UrxC5FTyZoCAb0GlPVFIKR8vSSxlfs+DvrqQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=tycho.pizza; spf=pass smtp.mailfrom=tycho.pizza; dkim=pass (2048-bit key) header.d=tycho.pizza header.i=@tycho.pizza header.b=nu9Rh70Y; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=fPeHNZdV; arc=none smtp.client-ip=66.111.4.29 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=tycho.pizza Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=tycho.pizza Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailout.nyi.internal (Postfix) with ESMTP id 8B6A45C00B1; Tue, 6 Feb 2024 11:43:22 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute5.internal (MEProxy); Tue, 06 Feb 2024 11:43:22 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tycho.pizza; h= cc:cc:content-transfer-encoding:content-type:date:date:from:from :in-reply-to:message-id:mime-version:reply-to:subject:subject:to :to; s=fm2; t=1707237802; x=1707324202; bh=P4gQPZry16ZhlVFQebz1H 5tj4IjCcocpT+0mBjF4AJQ=; b=nu9Rh70Yppyf9WYoGytHNSIIjAypsbQkXswHO 0qi4/ncq6DXSkhvqWelFxV1Hxd3eUtQp5bGah/epE62g4bDAdBCVwWB88TFVfZKM s0mtwm/YgDrzuOeAC7A8MoucFNIf/TfeVaTDIEm2+50ynI6LguR5a8f6kTyd8brs ViaopX0LgEQwWPfjY3sI+fR/VQGW9n9kINHy0yA+4OzpDhpakFpmnSNknrF5LwWK 0k7ra67E4Py2H7JZUlcVY9SWSvDmNciU0oMCX9REkrWNQum/2qGPPZ51jyF2OnwB eYAArbmPGN8ughBDCXQ5ZKaGcFMYbYsR9RT0pfE4495wAL0jQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:message-id:mime-version:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm3; t=1707237802; x=1707324202; bh=P4gQPZry16ZhlVFQebz1H5tj4IjC cocpT+0mBjF4AJQ=; b=fPeHNZdVzy0/UatbEAy0/AoIwZPpCQOmJZ7Nawyy3jgu fuQ9RvsZOaM0WI51JoQxCc050gqWa24LKOtNQUMlcOEYkPxm9WdkMO6RfGJLSVKb 2zhW462CYVKcNCw/3Fo5ZJ/BuU3S8dPZNIKvhZ3ga7gFqwuflg7CeOR22pKqh0Nc SXbeyevpsyvTq68eXbu6CVYviZ6HqcnTkt25GvoGyoYFUasvLv8Oz/SIZ1rFqDoN Z9dq78YzOYTWWL6ObR3F9pr0ktyS8AiQWaxs26wa2qOpbvrhNR9ojXkNvpfH7M8k 5J1L+Uvf32300UvzPI5kTpfMNYX+fHLdYYmxR+6unQ== X-ME-Sender: <xms:qmHCZczodrSt8kITMBydlvvQvXHcZQjxVxpXaE9r4N_A0IjNqMu_GQ> <xme:qmHCZQSDznfm6sRm9d1JBg7AoZJM8-OG0BhTo7WpzejjA4uf2LlvsOWaIfVEhgfRB j3zRHPHhrhj7nfWIUA> X-ME-Received: <xmr:qmHCZeW4s8Ptd7ih9j4Ovo3xHNGsK_KcaJCKIv7_V-UZZc-sdwLqvc7TDe4vxJ7ERL-oRKxt72TUPJk_r7yP2t5xRoNhcHt8sUFKheqd_w> X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrtddtgdeivdcutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpefhvfevufffkffoggfgsedtkeertdertddtnecuhfhrohhmpefvhigthhhoucet nhguvghrshgvnhcuoehthigthhhosehthigthhhordhpihiiiigrqeenucggtffrrghtth gvrhhnpeehfeefheelfedtgfejgeehleeifedvgffhueduueehheeuhffhhfethfeivdeg geenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpehthi gthhhosehthigthhhordhpihiiiigr X-ME-Proxy: <xmx:qmHCZajjohnMDCG3XO5QVHOrHbuQiXbBVDR1xnrmCXDBrlPKn6DvSg> <xmx:qmHCZeCiBw4PqDCzyTZDVIxhGW5iEOAmgbi2gwB8jpBODiOKoe2d-Q> <xmx:qmHCZbI1_LzThlKbK40yV1mcVYqORMimLbuOwKY4LVKXbv0RipDm4w> <xmx:qmHCZQ0Z4GfJlD-FUl_pdFtntO1Zzwmfowz0IjOEbG5GfMdWz5bBZA> Feedback-ID: i21f147d5:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 6 Feb 2024 11:43:21 -0500 (EST) From: Tycho Andersen <tycho@tycho.pizza> To: Christian Brauner <brauner@kernel.org> Cc: Oleg Nesterov <oleg@redhat.com>, "Eric W . Biederman" <ebiederm@xmission.com>, linux-kernel@vger.kernel.org, linux-api@vger.kernel.org, Tycho Andersen <tycho@tycho.pizza>, Tycho Andersen <tandersen@netflix.com> Subject: [PATCH] pidfd: getfd should always report ESRCH if a task is exiting Date: Tue, 6 Feb 2024 09:43:08 -0700 Message-Id: <20240206164308.62620-1-tycho@tycho.pizza> X-Mailer: git-send-email 2.34.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: <linux-kernel.vger.kernel.org> List-Subscribe: <mailto:linux-kernel+subscribe@vger.kernel.org> List-Unsubscribe: <mailto:linux-kernel+unsubscribe@vger.kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790168614076916516 X-GMAIL-MSGID: 1790168614076916516 |
Series |
pidfd: getfd should always report ESRCH if a task is exiting
|
|
Commit Message
Tycho Andersen
Feb. 6, 2024, 4:43 p.m. UTC
From: Tycho Andersen <tandersen@netflix.com> 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 <tandersen@netflix.com> --- kernel/pid.c | 2 +- .../selftests/pidfd/pidfd_getfd_test.c | 31 ++++++++++++++++++- 2 files changed, 31 insertions(+), 2 deletions(-) base-commit: 082d11c164aef02e51bcd9c7cbf1554a8e42d9b5
Comments
On 02/06, Tycho Andersen wrote: > > From: Tycho Andersen <tandersen@netflix.com> > > We can get EBADF from __pidfd_fget() if a task is currently exiting, which > might be confusing. agreed, because EBADF looks as if the "fd" argument was wrong, > Let's check PF_EXITING, and just report ESRCH if so. agreed, we can pretend that the task has already exited, But: > --- a/kernel/pid.c > +++ b/kernel/pid.c > @@ -688,7 +688,7 @@ static int pidfd_getfd(struct pid *pid, int fd) > int ret; > > task = get_pid_task(pid, PIDTYPE_PID); > - if (!task) > + if (!task || task->flags & PF_EXITING) > return -ESRCH; This looks racy. Suppose that pidfd_getfd() races with the exiting task. It is possible that this task sets PF_EXITING and does exit_files() after the "task->flags & PF_EXITING" check above and before pidfd_getfd() does __pidfd_fget(), in this case pidfd_getfd() still returns the same EBADF we want to avoid. Perhaps we can change pidfd_getfd() to do if (IS_ERR(file)) return (task->flags & PF_EXITING) ? -ESRCH : PTR_ERR(file); instead? This needs a comment to explain the PF_EXITING check. And perhaps another comment to explain that we can't miss PF_EXITING if the target task has already passed exit_files, both exit_files() and fget_task() take the same task_lock(task). What do you think? Oleg.
On Tue, Feb 06, 2024 at 06:37:22PM +0100, Oleg Nesterov wrote: > > --- a/kernel/pid.c > > +++ b/kernel/pid.c > > @@ -688,7 +688,7 @@ static int pidfd_getfd(struct pid *pid, int fd) > > int ret; > > > > task = get_pid_task(pid, PIDTYPE_PID); > > - if (!task) > > + if (!task || task->flags & PF_EXITING) > > return -ESRCH; > > This looks racy. Suppose that pidfd_getfd() races with the exiting task. > > It is possible that this task sets PF_EXITING and does exit_files() > after the "task->flags & PF_EXITING" check above and before pidfd_getfd() > does __pidfd_fget(), in this case pidfd_getfd() still returns the same > EBADF we want to avoid. > > Perhaps we can change pidfd_getfd() to do > > if (IS_ERR(file)) > return (task->flags & PF_EXITING) ? -ESRCH : PTR_ERR(file); > > instead? > > This needs a comment to explain the PF_EXITING check. And perhaps another > comment to explain that we can't miss PF_EXITING if the target task has > already passed exit_files, both exit_files() and fget_task() take the same > task_lock(task). > > What do you think? Yes, you're absolutely right. Let me resend. Tycho
Sorry for noise, forgot to mention... On 02/06, Oleg Nesterov wrote: > > On 02/06, Tycho Andersen wrote: > > > > From: Tycho Andersen <tandersen@netflix.com> > > > > We can get EBADF from __pidfd_fget() if a task is currently exiting, which > > might be confusing. > > agreed, because EBADF looks as if the "fd" argument was wrong, > > > Let's check PF_EXITING, and just report ESRCH if so. > > agreed, we can pretend that the task has already exited, > > But: > > > --- a/kernel/pid.c > > +++ b/kernel/pid.c > > @@ -688,7 +688,7 @@ static int pidfd_getfd(struct pid *pid, int fd) > > int ret; > > > > task = get_pid_task(pid, PIDTYPE_PID); > > - if (!task) > > + if (!task || task->flags & PF_EXITING) > > return -ESRCH; > > This looks racy. Suppose that pidfd_getfd() races with the exiting task. > > It is possible that this task sets PF_EXITING and does exit_files() > after the "task->flags & PF_EXITING" check above and before pidfd_getfd() > does __pidfd_fget(), in this case pidfd_getfd() still returns the same > EBADF we want to avoid. > > Perhaps we can change pidfd_getfd() to do > > if (IS_ERR(file)) > return (task->flags & PF_EXITING) ? -ESRCH : PTR_ERR(file); Or we can check task->files != NULL rather than PF_EXITING. To me this looks even better, but looks more confusing without a comment. OTOH, imo this needs a comment anyway ;) > > instead? > > This needs a comment to explain the PF_EXITING check. And perhaps another > comment to explain that we can't miss PF_EXITING if the target task has > already passed exit_files, both exit_files() and fget_task() take the same > task_lock(task). > > What do you think? > > Oleg.
On Tue, Feb 06, 2024 at 07:06:07PM +0100, Oleg Nesterov wrote: > Or we can check task->files != NULL rather than PF_EXITING. > > To me this looks even better, but looks more confusing without a comment. > OTOH, imo this needs a comment anyway ;) I thought about this, but I didn't really understand the null check in exit_files(); if it can really be called more than once, are there other cases where task->files == NULL that we really should report EBADF? Tycho
On 02/06, Tycho Andersen wrote: > On Tue, Feb 06, 2024 at 07:06:07PM +0100, Oleg Nesterov wrote: > > Or we can check task->files != NULL rather than PF_EXITING. > > > > To me this looks even better, but looks more confusing without a comment. > > OTOH, imo this needs a comment anyway ;) > > I thought about this, but I didn't really understand the null check in > exit_files(); I guess task->files can be NULL at least if it was cloned with kernel_clone_args->no_files == T > if it can really be called more than once, I don't think this is possible. Well, unless the exiting task hits a BUG() after exit_files() and calls do_exit() recursively. > are there > other cases where task->files == NULL that we really should report > EBADF? I don't think so... If nothing else, sys_close() dereferences current->files without any checks, so I think task->files == NULL is simply impossible if this task is a userspace process/thread until it exits. But Tycho, I won't insist. If you prefer to check PF_EXITING, I am fine. Oleg.
On Tue, Feb 06, 2024 at 08:25:54PM +0100, Oleg Nesterov wrote:
> But Tycho, I won't insist. If you prefer to check PF_EXITING, I am fine.
Looks like we raced, I sent a v2 with PF_EXITING, mostly because I
didn't want to run into weird things I didn't understand. I'm happy to
fix it up to check ->files if that's what you prefer Christian?
Tycho
On Tue, Feb 06, 2024 at 08:25:54PM +0100, Oleg Nesterov wrote: > On 02/06, Tycho Andersen wrote: > > > On Tue, Feb 06, 2024 at 07:06:07PM +0100, Oleg Nesterov wrote: > > > Or we can check task->files != NULL rather than PF_EXITING. > > > > > > To me this looks even better, but looks more confusing without a comment. > > > OTOH, imo this needs a comment anyway ;) > > > > I thought about this, but I didn't really understand the null check in > > exit_files(); > > I guess task->files can be NULL at least if it was cloned with > kernel_clone_args->no_files == T Won't this give false positives for vhost workers which do set ->no_files but are user workers? IOW, return -ESRCH even though -EBADF would be correct in this scenario?
On 02/07, Christian Brauner wrote: > > On Tue, Feb 06, 2024 at 08:25:54PM +0100, Oleg Nesterov wrote: > > On 02/06, Tycho Andersen wrote: > > > > > On Tue, Feb 06, 2024 at 07:06:07PM +0100, Oleg Nesterov wrote: > > > > Or we can check task->files != NULL rather than PF_EXITING. > > > > > > > > To me this looks even better, but looks more confusing without a comment. > > > > OTOH, imo this needs a comment anyway ;) > > > > > > I thought about this, but I didn't really understand the null check in > > > exit_files(); > > > > I guess task->files can be NULL at least if it was cloned with > > kernel_clone_args->no_files == T > > Won't this give false positives for vhost workers which do set > ->no_files but are user workers? IOW, return -ESRCH even though -EBADF > would be correct in this scenario? OK, agreed. Lets check PF_EXITING or exit_state. Oleg.
diff --git a/kernel/pid.c b/kernel/pid.c index de0bf2f8d18b..db8731f0ee45 100644 --- a/kernel/pid.c +++ b/kernel/pid.c @@ -688,7 +688,7 @@ static int pidfd_getfd(struct pid *pid, int fd) int ret; task = get_pid_task(pid, PIDTYPE_PID); - if (!task) + if (!task || task->flags & PF_EXITING) return -ESRCH; file = __pidfd_fget(task, fd); diff --git a/tools/testing/selftests/pidfd/pidfd_getfd_test.c b/tools/testing/selftests/pidfd/pidfd_getfd_test.c index 0930e2411dfb..cd51d547b751 100644 --- a/tools/testing/selftests/pidfd/pidfd_getfd_test.c +++ b/tools/testing/selftests/pidfd/pidfd_getfd_test.c @@ -5,6 +5,7 @@ #include <fcntl.h> #include <limits.h> #include <linux/types.h> +#include <poll.h> #include <sched.h> #include <signal.h> #include <stdio.h> @@ -129,6 +130,7 @@ FIXTURE(child) * When it is closed, the child will exit. */ int sk; + bool ignore_child_result; }; FIXTURE_SETUP(child) @@ -165,10 +167,14 @@ FIXTURE_SETUP(child) FIXTURE_TEARDOWN(child) { + int ret; + EXPECT_EQ(0, close(self->pidfd)); EXPECT_EQ(0, close(self->sk)); - EXPECT_EQ(0, wait_for_pid(self->pid)); + ret = wait_for_pid(self->pid); + if (!self->ignore_child_result) + EXPECT_EQ(0, ret); } TEST_F(child, disable_ptrace) @@ -235,6 +241,29 @@ TEST(flags_set) EXPECT_EQ(errno, EINVAL); } +TEST_F(child, no_strange_EBADF) +{ + struct pollfd fds; + + self->ignore_child_result = true; + + fds.fd = self->pidfd; + fds.events = POLLIN; + + ASSERT_EQ(kill(self->pid, SIGKILL), 0); + ASSERT_EQ(poll(&fds, 1, 5000), 1); + + /* + * It used to be that pidfd_getfd() could race with the exiting thread + * between exit_files() and release_task(), and get a non-null task + * with a NULL files struct, and you'd get EBADF, which was slightly + * confusing. + */ + errno = 0; + EXPECT_EQ(sys_pidfd_getfd(self->pidfd, self->remote_fd, 0), -1); + EXPECT_EQ(errno, ESRCH); +} + #if __NR_pidfd_getfd == -1 int main(void) {