Received: by 2002:a05:7412:bbc7:b0:fc:a2b0:25d7 with SMTP id kh7csp3158118rdb; Tue, 6 Feb 2024 08:43:37 -0800 (PST) X-Google-Smtp-Source: AGHT+IFmzLpsWPWTit/PnQvsFjVL2mEZK0sQtDojnVgQKvrHja2fnjOAdnc7RtlUgASvI/nYTNfB X-Received: by 2002:a05:6808:f01:b0:3be:b86e:35d with SMTP id m1-20020a0568080f0100b003beb86e035dmr3482617oiw.12.1707237816880; Tue, 06 Feb 2024 08:43:36 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707237816; cv=pass; d=google.com; s=arc-20160816; b=BFXbz6E/09G7cmGy35ehWrEhzoAeH2q68nt2/5VN+ob7+RPtWn1AeIRFT5AlbxYYhe dMpSEaV1YrY7q3It/62yoj6eIifJxd9+iRQbTwgtaREARHPa+kR3WsGP/4hGs2hukrW9 /5w9wP7Px6wzZBJl2R6jaovkHwZndgxFHZwWN0arNfLdgycjWC/dKjJlVpd5ApmhELXT tQvhVI/JNgIYWQub9QZjrXIpQdUJM4pHBkvgzVrFvSTl61hn+yNZr0kVo24Wo7mCTcR7 S8sFIdkchbnecXe42d5gGxbzMFnHQzaugD5nJTeCclZaQs/aloLmchukp/ww6i7QhWid XhKQ== 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=XrC3DbVOd2gdqgUAXS3SQtr37xjPszWrTzodGVT7bUo=; b=dq5EK/h2PLzcOHiO+gt0oTrQct+MLY5Ykcmfn3Rlj07LNDFK1CkuNQsIQY+E2bylTh YU2DVKAMgvP6sFdkpr6NK18tMffjnCIn7RdEGxapqIBP46Jhmo+i8PVN4Ei6L1HvPEFa 1KmVv+v1gNtrplVA6B/udc3ifNegNuQOUkHMsV4VFX998JUbiUwGeYaUtvhxtMFTNQYD HBw/nmjEOQKzC0eUVNzar392Gywwu5LstpAFiPA2mdwZFwgrPVQ02YBZoX3Eobv76pCM WP/DxafDBjY0GsjD7BHi+G4h+XGpcqF88hvD0YQurHCLyXC6Ae+XO3nMY895thrEWre2 oK5g==; 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-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-55331-linux.lists.archive=gmail.com@vger.kernel.org" X-Forwarded-Encrypted: i=1; AJvYcCXn0e9tgh0geGqtNTIrsN2us47btW/PqruB1e0bSmq0pw37wJH5zsX/JyQYQfSDQWpGeymKagLZ/Ot7IF/ksPq/Q7qcY3R8zX7h32bcYQ== Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id o1-20020a05622a008100b0042c3baf8390si334820qtw.793.2024.02.06.08.43.36 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Feb 2024 08:43:36 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-55331-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=@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-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-55331-linux.lists.archive=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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 823461C24C15 for ; Tue, 6 Feb 2024 16:43:36 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5E3D54A11; Tue, 6 Feb 2024 16:43:27 +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: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrtddtgdeivdcutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpefhvfevufffkffoggfgsedtkeertdertddtnecuhfhrohhmpefvhigthhhoucet nhguvghrshgvnhcuoehthigthhhosehthigthhhordhpihiiiigrqeenucggtffrrghtth gvrhhnpeehfeefheelfedtgfejgeehleeifedvgffhueduueehheeuhffhhfethfeivdeg geenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpehthi gthhhosehthigthhhordhpihiiiigr X-ME-Proxy: Feedback-ID: i21f147d5:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 6 Feb 2024 11:43:21 -0500 (EST) From: Tycho Andersen To: Christian Brauner Cc: Oleg Nesterov , "Eric W . Biederman" , linux-kernel@vger.kernel.org, linux-api@vger.kernel.org, Tycho Andersen , Tycho Andersen 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: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 --- kernel/pid.c | 2 +- .../selftests/pidfd/pidfd_getfd_test.c | 31 ++++++++++++++++++- 2 files changed, 31 insertions(+), 2 deletions(-) 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 #include #include +#include #include #include #include @@ -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) { base-commit: 082d11c164aef02e51bcd9c7cbf1554a8e42d9b5 -- 2.34.1