Merge patch series "pidfs: preserve thread pidfds reopened by file handle"

Li Chen <me@linux.beauty> says:

Reopening a thread pidfd via open_by_handle_at() silently dropped
PIDFD_THREAD because do_dentry_open() strips O_EXCL. Restore the flag
and add a selftest check.

* patches from https://patch.msgid.link/20260716052726.1032092-1-me@linux.beauty:
  selftests/pidfd: check PIDFD_THREAD survives open_by_handle_at()
  pidfs: preserve thread pidfds reopened by file handle

Link: https://patch.msgid.link/20260716052726.1032092-1-me@linux.beauty
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
This commit is contained in:
Christian Brauner 2026-07-22 13:55:36 +02:00
commit 172ed6c2b6
2 changed files with 8 additions and 1 deletions

View File

@ -939,12 +939,18 @@ static int pidfs_export_permission(struct handle_to_path_ctx *ctx,
static struct file *pidfs_export_open(const struct path *path, unsigned int oflags)
{
struct file *file;
/*
* Clear O_LARGEFILE as open_by_handle_at() forces it and raise
* O_RDWR as pidfds always are.
*/
oflags &= ~O_LARGEFILE;
return dentry_open(path, oflags | O_RDWR, current_cred());
file = dentry_open(path, oflags | O_RDWR, current_cred());
/* do_dentry_open() strips O_EXCL, which encodes PIDFD_THREAD. */
if (!IS_ERR(file))
file->f_flags |= oflags & PIDFD_THREAD;
return file;
}
static const struct export_operations pidfs_export_operations = {

View File

@ -373,6 +373,7 @@ TEST_F(file_handle, open_by_handle_at_valid_flags)
O_CLOEXEC |
O_EXCL);
ASSERT_GE(pidfd, 0);
ASSERT_NE(fcntl(pidfd, F_GETFL) & PIDFD_THREAD, 0);
ASSERT_EQ(fstat(pidfd, &st2), 0);
ASSERT_TRUE(st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino);