From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.221.53 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795987661; b=1mw0QjZacJ094tb8a5LWFOk5A9D5zvkiC98fxxysiqahhtadD5e46KUX8igYOtgPilqL u3SU03xUSkxtRQ7nLbLlzh9liwNrnZ3RrDvAks1E9hCpx0Q4/UVm6tQHCiFzgzBksgmNi +bCRsqvvzfuuvI1qZS0oM1FM42wlKMh/qqhP85iOMmhXDHkJYTmV2UAIGPmcTwjm1vDzg 9U+iCN5t78wmhGCCPim+sS8/ylwqbzKhWHmIOGm0M2hwxreF8xFVge985sVTEZFeTh3yj FdwBoKuVrjtKWeDU+34EHvbmGT4Jsm4d6kvWS0yF+fvViOuLJkciZEIsUCIVQ0CLJgA== 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=J7Nrd0u7mzkAvJ5ABL3KvkrkURYq+WQi9l0kssxodHom8Zui6q9238tsU8lVOze4+hn6 WhxFAmouvm27pxumhnPhwgyJTUPToJmM/qAIR6Xljd44V/NU+NmuJNbp1fqeRdHCjEtj0 IzRet7h5uxkiKw1PIEjoo4/XOhxCNOixICuTKqcF/sC5MDgGSawOVpxCJAeBe/nwNc2NK WZTTbvKmkSH7rJVipYDnHpWuQOGDHuMv+oTtmwh8NYdggewdE9JuV2zfE3kEjnHsCE0/h 3qAxDdmJSsySFu1omMVdIg/MePv8wr14elUmnUEZ1dEhSO6jUus8ZsN/vAGcU368GDQ== 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.53 Received: from mail-wr1-f53.google.com (mail-wr1-f53.google.com [209.85.221.53]) (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 D0657606A32D for ; Sun, 29 Nov 2026 21:27:40 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org D0657606A32D 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-f53.google.com with SMTP id ffacd0b85a97d-491f34b4e0fso4325340f8f.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=ueisk0XB+TC3GGw3G9TVVWmiFuCr2pOwlkfjfnmKEdSqHuixnCQM64le691X3E+yV5 s2WDdjU+9xekFbiKQrNlAVwNQusLumvHHufqeZwJF1H65I/Ic038+VGYTK/aVBGseznD legWoPB/omGZy9TDZ7MIYnly99UlH3PTbinwCkO8NSucgudLtcfLIcWeOxqZOHGUzQg7 iQ/ALKwYJfl4Pkw9yaTWrFm1PqNz1MlFCKyYYAIBwjpGQTvnzKYMY2pdl0up7PCSYQXz KDBUuzxWTNQ4s6nouwI/FdCMGwa+HwADJk+msXd7zmKD6GopxPMCX/YzSZhc5g78S3wi n0Pw== X-Forwarded-Encrypted: i=1; AL+hErE/WtUvHgvA/G1TZepTz0KaFTVyo/DN+yyWnBqorOYvx4O3y4/v6eStB99fU2adACpYzEknwmppARXx5fwO@vger.kernel.org X-Gm-Message-State: APuU4Z/WIpP08w6uWdJxFgGeITKfw21Z2nC55tOJu38mNerzau/LpzcP P6MbLELUhHcxwZx4buwtkYpqrzDP86yrgMGDWdlYvhpzz5Rix6X/mIB8 X-Gm-Gg: AZFdTZeDxFpHW0q6uvCENwfRo7Y7unn+7Y09hI8Ufcvq3IiAXjSrCX0hvl4sWv4SlN6 MlllOSQA2G0cHy8MQiqHGSxKo88z2L/J3o+Ao88RmFMpxRnJ+cdw4cmJSRdHMA46yZt1pvjuK04 338mbQmwKgSAkL2T9SJ3WZ4Et0zBkx54IWqjHJPOZWKcKwctQiaB5LVNStJL7Yjd+T0/9Pwnb0N YWR9gqrK00KuMO4q0JoQn39WlfjX7iO7l8bymRymSqgIxoT9j6PeoU75TaZAzEXaKtgU2KsH2mb /cVM2nKmpYcX0MAfyUKcFqSbdEpT4lWL8A01yFbHsuF9cOQRSds3Akp47qmTQZMUoaptZM0TgwM wuH50ch7A+9lHWaUU03qte6u+e5DjIcEhlPM+fdVlY7sEC4dKL18belFfd9KDLTSHakukYT7siv ODVbMx8UkXk9xRVVzf2oyX+UvW5KW0dxI8h7KSwKC8tOxYlKQQ/9KCYO6fFFHR/IVUMXEn8Eht1 MezVHXrUaU0IbuhmRmpFc39n2D8y6oz/lvje8u5d8BjAufUe3RXPtmjXUG5eN+SwdrBjfQ7VwMy Bvhw5GA8cVrUiE1Joxno1eGFvm/mTXM8+1870nfljIApLUZiOItp2yrLmIP61u+GzvXMRYB1h8O 3mCwRc1FX0k0lNw== 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-fsdevel@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