From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.221.51 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795987661; b=2O/HV8sUrZJMasNSoaLb6GRo6iUfOjAXyltmbwJjO8boK16jCpuWKpmmcDGlVcWlzptf GYHbawjsg++2g7gH4Ioc0nYcbc2V4R6fx7th7PJDtjxopz9pm8VrHDcgcldSx0fQijSpV c2iL7OxObX2qwfQzuhDw25N1sLcQClSTDomkflKZ6aD/0peQ1iuPgnkgT2gs7kjtXInSZ 2eIysuu7RifPuDc4UDXgwIFFeoE5AqLiiPt/x0DQIhHE7zx/6vw/OteePbDuIuJVqHuoI zyYwosFXxPpH0Ozaaep6XLChjuKnOWzVex9kHwKd6bp4Q3MasHCCqQbpY85MTqTgSSg== 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=Bncx1er11AzKe9vUGFpCJ2j1k4DTLSHU+5i/aourEKzDls9nwdnAUrfmJ9WT/lbKZbvN 9jtvxi9qIqSTUkj7dEfNqZMIptLeRfNk+QWIBeFA8gSQko16v69n72jKbxeARBYjC4B39 WqC4aaPeN+E+Aw+6GJ3RMed4IUE/qehm9J6Mnfh/vrh17pvEFJ8QpDiHnmzbHB3UYHopp WF+K62LimldY7HWhlYhw//RhKcLl7H8HQRFKF76TmvbqMIruYYegC6JD6npp6tU73P3ea NkOhTfogfZA+cKEGqcoZFroIvp9bSgiOC6/bW+SNsI9d8ZY3Dtx8UQxqHW1dmtfQODQ== 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.51 Received: from mail-wr1-f51.google.com (mail-wr1-f51.google.com [209.85.221.51]) (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 E927E606A325 for ; Sun, 29 Nov 2026 21:27:40 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org E927E606A325 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-f51.google.com with SMTP id ffacd0b85a97d-4a9be758734so6186463f8f.1 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=TsanufOU2nl0R+Dq+Ny7lqGMd37CijTOQuWKbGbxL/fpRn+qpkqlb1OFp/OpxKZoIZ E72ICDxQm+N8RFK4teaZyBAW8DgnSFGTGhyLmSVkQ2lKmUhcCd4W05N6fLDDVpP9PJ8v Jg4rJt1RLNOqMZ00nHESOKXpIPLR3H9ZcvKn3crnYYzADajtc5M+peCzKE3stlMPiBjw 0YSqTNPTKIL/D6uQGk83aI5IJRa1IbtptVeACR/3FDm/pphgVuBF9fBhrph+OAy/GHD3 opoHoKoFcvx0qipmRP1vKmJCOCIkrT1sSZ15VVpvpIn2OxSPNep0uw+0PC6JYoMn98Oe YVnw== X-Forwarded-Encrypted: i=1; AL+hErEzxiku322ssHyJBLxI0qt2H7K2Nv7zW5zBUikXlpxoMutiYkun26H+lXlqfsO38ZOSDrOZ+LHxp7E1Bd6L4kw=@vger.kernel.org X-Gm-Message-State: APuU4Z9LpzuXRVFZmAckAXMjsCGcSyber6pTjA0eUrG81EIZvCx1EnOL Oma9aoEpubnnmmRs34aDyD24kHtwadSpw1PgLxD2YLecch4PaO2U0/kW X-Gm-Gg: AZFdTZfFTb5trwu3H6EqTgBchCbjZYLZtC1RXmaSNVljMAZ58vCCU8PiQ5WozKCi3or sUemNOpUrs1iFW5ArnredvuytqxVxSaRFAhDM5RdPFq7/vXTuvdwcO2YjSVUtveX0aPzhHdH787 LbUVKFiBiStYuQYsuDDE8kJfkk9StobpF39RKSgXCDVTYDzc+jev1URvDxBc67x0STrfHuLdQD1 1Tt0wzjmQQz6Ssb/sd+z716Ew1uf1FYowN0J8+AW0LfyJnozLaAXIvPKfhcOtKQQ4mjzXrQvBWG ZZ6wWQXSTgvAygTGEHmmEj5kcNH9mQaeR7JtSoxRJL5TtNP1XwxOjIZhJ0mKME9VP/xYKaIMMYX io8dm9iufigMokEy41m89LnWwyV7Der7zqh9uuZ5r2STSyh6tmCJ10xW8QMLhx1mC0FLM9Ymm6g cVbr2L0OSJzum95i5KlCV25WWl7X2tlCXraShryxYu4HQ52nq8uksvMegwvGiItRL3/x0o43fsm 10HMzNJvYwruA4oPceXkCrvJUKdHPKAdEg4y1z9s1gbDMSz3FRpuDp71zCK7/QTOIYIxrHNlZdR oZaEWjw2nAL+Ork1b8xHGlb5JibiolpyptdY7VQmX4DS7IXK8cq2YBXKFPfeVNL9mXCBZbySTKN IhdxoaIQtd+A1YA== 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-kselftest@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