From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.221.52 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795987661; b=yR5kd0biLC4mDlU++nQ0nheM284iDAigdMz5tpJpDg5eo7C5LSUEeEDlHL+nq2z8791i 0rnIy435tPwg05gAhS4E8NxTxJWMvmRXXC8yf6nQwH5W2wICXoDImHBHxQYCpzQsO+gap QZaRuEMg927XgZe4Qq6yJ+QZmQoK4qH/hP0zmtQUz8CNPPUmRThek0sChwVsJhof7jgHX 1Gpgu8ieHBKfJWGlt2sEhYDC5cjJXG5XwlpTUnbH1tgqXaW/ZMVJIvUthDrXwDu+37kbc diN9FXK4ejmoJNButoVnvTwwNi7TWC54ey4cdV1IVhahhEigdP3vmXY2t9X5wRsnJPw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795987661; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding; bh=+lgy90frHUVIhanQ8+PjG5uNZFYhV+/9T7ZVgiG4pkA=; b=H600kkF3irvUtHBNSC+NPlDcuZg1+ThexTXs/0xhoWPabJTcngjUvbQ4lLP7GuslMiB0 csK8bimtqfUgC7Z6YJjTS8HYTeBLPL1Fc8+uOFzbo+ETSrzl68dLYKtWgvWKcQgeAnFkj 0E3+uhrIfCqCCVz2FTuIOi7/yd7Pf5Lt0euqRGjP4FDvTSANJ3ty0zXfZAFxgtYahAiYl nNLV2ISuSp/zsXHwiaJrXjFWxl0t0Yv6rfkRvEg4z93aUVF5WHbVfMR8deqzgBWSsvqnK FfWM153VbqT+ht6De19WxbOsy59KbUCf12LkV8OJEbENySolL6U/19WKHpu+vRoUNJw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=EcjNgV5M; arc=none smtp.remote-ip=209.85.221.52 Received: from mail-wr1-f52.google.com (mail-wr1-f52.google.com [209.85.221.52]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E224E606A32F for ; Sun, 29 Nov 2026 21:27:40 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org E224E606A32F Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=EcjNgV5M Received: by mail-wr1-f52.google.com with SMTP id ffacd0b85a97d-491f34b4e0fso4325339f8f.2 for ; Sun, 29 Nov 2026 13:27:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1795987659; x=1796592459; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=+lgy90frHUVIhanQ8+PjG5uNZFYhV+/9T7ZVgiG4pkA=; b=EcjNgV5MNbYaAPWyIHfQx0a4VNqnhoRMId105Kw4jLOKacfIpVKXnSwRc1xOqjgeSA FVGK8RgdLfDTl++ADj6mbahMoZ54KZRs5HctHjnUrHJgvxQohGd83DsedSYSRvfsvfw2 20poOGaK6GZ52Ohalu164g/suIW+imeDkfuff7R8oeesIsYkAGJMeE5K8VLhmwARMRyw 60xP7+UOExZCuDoB/NixQDom3XgbnkROySMVs6YjpM0/lLoFtRTWUlVAcdufXtm4JV3D 3/A9G6Hes+6+z90RyZITRtZBJ1ef9WEQV1fwGE4TNGcJNA+CeUqIUbEJgkBUKqJsoKxj uH7Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795987659; x=1796592459; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=+lgy90frHUVIhanQ8+PjG5uNZFYhV+/9T7ZVgiG4pkA=; b=bYlnqAK18BVNKZx/sJ0aRDBIMDzvyvc6uKOuJ5QDUxw9UFl2ClNe/J0oepff1D85pv 0OZpOaSD1v3fZ3KC//mqAtXN4Zh3pskuPUwIfBefEHY0cUQ9vSsng/3czZpCe43AoAOg i2WrnbKU7OW9ohkvMG+YdA/vS7G8VUUy2B9thz5GMm/F79RIbSCwcz4ydocDpfBSewmQ ePsTSETlE6+ka9o2GTZ+0Np7AbkmwhIBUJZDYQJnD83gr86ws9JgI7467mRM9vnst+NP YiQRaCg2qaUI5iNxtPAd6IV/qzmkdlB7ChCi41/aJfvWvsYEFQXLPKVjGZeMPBn6/SVn Uduw== X-Forwarded-Encrypted: i=1; AL+hErHBQEW1WPY27oQTvX7g/yK1R4tZcBF+132hbHIUow1M9IcUEfstYBi5ftAY+A5A1M7GDV9HsaLNlxeY4KU=@vger.kernel.org X-Gm-Message-State: APuU4Z8okrzsYK/K2Ty23BYfvu2AybJkHbw6NxDpXdPfCIFs764lsJjN Gj7EOkjLO9xvtr4hWnKYGKUwALPwsa68QRuG7ZlYiH23V+dtyit8G571 X-Gm-Gg: AZFdTZcEIwNbit0D3+z5wphtgAXQZQtdI5nMYCbKCRO74ztGd08F5iLFu9Fj5ngYDAT NNgj+YEbEmLQkrpda22cpRc8MimV7M86CLKPkQs15WqIqmSjnQo+ClDLRyr9u2Fvz+8C5O94nWX Uh6HKPtj5tFYUwC2dY+Y4ZEtMief1OLLIfxDiCvkNvY4VgmQZ9wYZZHn/5O/I3e4XF/01y+IllB wsJvWlr9AGY1tFEQ8NXJ36WVyw10kMrWIXpMjBsOySEhYpvzRZKVbvP3TkY+8j+QtSJpE8mfxNc ycXlvIR6EjLB4PJiXF9vbDLLhRwapyEDL5coTXKzXRXJEB81PaU6hdTIUoCKMqzc58uYxThJL3B QBD9+O2+ttpthrQPVbBBELe8ClCqRvq0mvztFBGTECTpSvswZnZxAx7iNNUYMUND1y2ffgUtIp6 sPQzo5qVUll5lJTkyIZgPmj40u+MXQLNnVYEGW0PHEFIWjhW7EH1ybzz4s/n4GgaZ02kAyKpW4X JbJHW6ZzwiNnq11XKCTAo0lx3KfL0zgZpuMoRfh+GWCNRadnWkgw1EZYnIWRy/3L0g7CjppRKF3 pwBbloooJsOCgI/PV61Qh0YUdLz7HG3bAxcNa6CA1XDRoo8qdyjHPfQFknfFBqC9PSNT1V/xgmj 6lN9jyI4fUsC0GA== X-Received: by 2002:a05:6000:26ca:b0:498:c3fa:5ca9 with SMTP id ffacd0b85a97d-4abaf7402a9mr55347128f8f.20.1795987659161; Sun, 29 Nov 2026 13:27:39 -0800 (PST) Received: from dohko.chello.ie (188-141-5-72.dynamic.upc.ie. [188.141.5.72]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4ab6baa397asm61090172f8f.48.2026.11.29.13.27.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 29 Nov 2026 13:27:38 -0800 (PST) From: David Carlier To: Christian Brauner , Shuah Khan Cc: Oleg Nesterov , Josh Triplett , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, David Carlier Subject: [RFC PATCH 1/2] pidfd: add PIDFD_GETFD_NO_SELF to pidfd_getfd() Date: Sun, 29 Nov 2026 21:27:31 +0000 Message-ID: <20261129212732.805592-2-devnexen@gmail.com> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261129212732.805592-1-devnexen@gmail.com> References: <20261129212732.805592-1-devnexen@gmail.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit pidfd_getfd() on a task that shares our fd table just duplicates one of our own descriptors, behind the back of whatever code owns it. That is the only thing stopping the Rust standard library from exposing the syscall as a safe function. Add PIDFD_GETFD_NO_SELF to refuse such a target with ELOOP. This covers the caller, its threads and any CLONE_FILES task that has not unshared. A task can stop sharing an fd table but never start sharing an existing one, so the check cannot be raced. Signed-off-by: David Carlier --- include/uapi/linux/pidfd.h | 3 +++ kernel/pid.c | 29 ++++++++++++++++++++++------- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/include/uapi/linux/pidfd.h b/include/uapi/linux/pidfd.h index 95ce1819f423..4c3357601cd5 100644 --- a/include/uapi/linux/pidfd.h +++ b/include/uapi/linux/pidfd.h @@ -21,6 +21,9 @@ #define PIDFD_SIGNAL_THREAD_GROUP (1UL << 1) #define PIDFD_SIGNAL_PROCESS_GROUP (1UL << 2) +/* Flags for pidfd_getfd(). */ +#define PIDFD_GETFD_NO_SELF (1UL << 0) /* Fail if target shares caller's fd table */ + /* Flags for pidfd_info. */ #define PIDFD_INFO_PID (1UL << 0) /* Always returned, even if not requested */ #define PIDFD_INFO_CREDS (1UL << 1) /* Always returned, even if not requested */ diff --git a/kernel/pid.c b/kernel/pid.c index 530aa93d6fdb..e312b4d1b108 100644 --- a/kernel/pid.c +++ b/kernel/pid.c @@ -874,7 +874,18 @@ static __init int pid_namespace_sysctl_init(void) } subsys_initcall(pid_namespace_sysctl_init); -static struct file *__pidfd_fget(struct task_struct *task, int fd) +static bool pidfd_shares_files(struct task_struct *task) +{ + bool same; + + task_lock(task); + same = task->files == current->files; + task_unlock(task); + return same; +} + +static struct file *__pidfd_fget(struct task_struct *task, int fd, + unsigned int flags) { struct file *file; int ret; @@ -887,6 +898,8 @@ static struct file *__pidfd_fget(struct task_struct *task, int fd) file = ERR_PTR(-EPERM); else if (task->flags & PF_EXITING) file = ERR_PTR(-ESRCH); + else if ((flags & PIDFD_GETFD_NO_SELF) && pidfd_shares_files(task)) + file = ERR_PTR(-ELOOP); else file = fget_task(task, fd); @@ -914,7 +927,7 @@ static struct file *__pidfd_fget(struct task_struct *task, int fd) return file; } -static int pidfd_getfd(struct pid *pid, int fd) +static int pidfd_getfd(struct pid *pid, int fd, unsigned int flags) { struct task_struct *task; struct file *file; @@ -931,7 +944,7 @@ static int pidfd_getfd(struct pid *pid, int fd) if (!task) return -ESRCH; - file = __pidfd_fget(task, fd); + file = __pidfd_fget(task, fd, flags); put_task_struct(task); if (IS_ERR(file)) return PTR_ERR(file); @@ -947,7 +960,7 @@ static int pidfd_getfd(struct pid *pid, int fd) * * @pidfd: the pidfd file descriptor of the process * @fd: the file descriptor number to get - * @flags: flags on how to get the fd (reserved) + * @flags: flags on how to get the fd (PIDFD_GETFD_NO_SELF) * * This syscall gets a copy of a file descriptor from another process * based on the pidfd, and file descriptor number. It requires that @@ -956,6 +969,9 @@ static int pidfd_getfd(struct pid *pid, int fd) * The process which is having its file descriptor copied is otherwise * unaffected. * + * If PIDFD_GETFD_NO_SELF is set, the call fails with -ELOOP when the + * process shares its file descriptor table with the caller. + * * Return: On success, a cloexec file descriptor is returned. * On error, a negative errno number will be returned. */ @@ -964,8 +980,7 @@ SYSCALL_DEFINE3(pidfd_getfd, int, pidfd, int, fd, { struct pid *pid; - /* flags is currently unused - make sure it's unset */ - if (flags) + if (flags & ~PIDFD_GETFD_NO_SELF) return -EINVAL; CLASS(fd, f)(pidfd); @@ -976,5 +991,5 @@ SYSCALL_DEFINE3(pidfd_getfd, int, pidfd, int, fd, if (IS_ERR(pid)) return PTR_ERR(pid); - return pidfd_getfd(pid, fd); + return pidfd_getfd(pid, fd, flags); } -- 2.55.0