From nobody Sat Feb 7 09:37:01 2026 Received: from mail-pg1-f180.google.com (mail-pg1-f180.google.com [209.85.215.180]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C31AC2F5491 for ; Tue, 27 Jan 2026 18:01:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769536898; cv=none; b=XBN3ZJLNLhL8TEGnrMIrjYIflpBlnoj5Hmd9ezvywXOQiZgAhQVWIWUw6OLiW8aEJMP/cIR6/OXU9mXd3oqXrKtI6DpCc1hjT2Ph7fksbfxAmR66GPlIdQ0k3t7fpT4w8capEHC0KmSX0OHxYSrSpj62x14EM+QdFqFLU+zceEM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769536898; c=relaxed/simple; bh=s1VRtrN1rCxIrmB1bp08y4a8rJA3GT+7VXS+NY5X8Qo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Z3g1lB/+e/kLvuxJ04DTm2Vgc8bzVObRCPQQyZMg+m6mVSHDSy+ArqVnIgzvLVTWtIspVN2gaGRKOJ3/CUxeJdwKmcbWzJM4nUOivfGrZm+puR+TxNuLcva/edlWhPOy9U5GT0i1FRz+5v75Qht/D9heQHX23rb4xAQaea39ygo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=EKxy/k6a; arc=none smtp.client-ip=209.85.215.180 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.b="EKxy/k6a" Received: by mail-pg1-f180.google.com with SMTP id 41be03b00d2f7-c62239decbeso2138314a12.2 for ; Tue, 27 Jan 2026 10:01:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1769536896; x=1770141696; 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; bh=npSZ8A74m3ybU6LKADRoribfE0dXr+fNAZsDs5EV4p4=; b=EKxy/k6a5Ili/VXblawiyA1fGANXCEk1p7FC5fIzeUqRoY8mIRyTokKTLIfR2j3fd1 i6WFTpORdDPryFL4FQTzuONfJDR8YX/5vOs0aHahczh7sveQD+F7/Cqk1p3yRcmJqvKK kPH+p53y4ZQJNLXDfKTs5gb4m8dZm2Wc5gvX+VvljhN3E0cwDilYnGHMg72maVlYu+U3 c/DQBliEppKweYvUu/5F87VbyCnCFws2qYVagfYb7+KCV8u4F3qw5LsJsr2Us2z+eG9a I0bUDYv7Mu5TWjU8uznF7ke6c+yCV5jfiqiMz5/ay3Xm3zjb7FDDoMMrFnBNRAT7RDwN NR3g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769536896; x=1770141696; 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; bh=npSZ8A74m3ybU6LKADRoribfE0dXr+fNAZsDs5EV4p4=; b=YSNNDY0tiFtqL8b3RZPq7P2CHMcAcy367zeUkBgWtFvJ1TKackLhrZoenaZoml9gOZ fFJmAPQKxM6lRmEXDWv5kgCgj/RPcZUnwreBvFOInO6byHwvYKODP/therYJsPkvsCXe 9u2fLOG/b/PI/v+3uuY6juueQ1LKyGL2M4oLg3VEGhSpqL6EH9kZu/RgDlgHxFKT4nu/ BwarHEVC1jJ0tebIQ19eM/tbrnDwJLOK8OJVpiupcC50EGngqFTAU0jmo2Gs0FVEQOXT R6+6vFzQioLr0cWhZOLtYTFDK9/a95bU5RZl2Qu7kOyRpekULpZ++sxqK4FMUOhgZLI9 CBcg== X-Gm-Message-State: AOJu0YzeVoa10WLZnvigO1C3t4M3byc87AsbCkYWlnUXSF4B4AIQUNP9 EbzkMPQJrjWOFDxGRi95Q6Xe9op576mTj3V+8BqbEczzx9G0+fw/0PRV X-Gm-Gg: AZuq6aI2uCxNggXQJcATOR8l9WWxDd/UA1vcna+FAlSBQy3Cw9FJGnEMJenXbhCPmZz XSrJA3vONRonJ6MrY7IEZTqtD8lvYs698kdHofZHur76kDoYAeulh8AfcfFLbqJJZI8Vpf1/ifj Z9ZkV77eoqCr2GVlcBjVJTeEk3XRBsy5HBG2XjnYuVMryKDBpBrht1+0F0KhC8LA7YSP0mL84nK BuZqZOTDfp0ZkcTqd2XjdA+JM+qvE7v4eutLqSOc9uSep68SGQ36WtnIzkpIRB4xKZyRPtMGPgj XMUfaKfLst2/+bg6TCjYppKMxXEU2/LcpD/74M7x5tNBxgiuvbiMsGQgxOUQzL/70hlAe6z1Dru s6pULFY/i8NgIccdyqsFPnNfd1UcK50Z4LUA1wdQx8zaU7AZb0bgdk58SZ89HaqUv9uKESjct3Q hWF18jnzcsaK1rGCC+kSjGwC7kZkwEyMiZJqP578/4wzjNifx8PkZvNg== X-Received: by 2002:a17:90b:270e:b0:34a:b459:bd10 with SMTP id 98e67ed59e1d1-353fed7104bmr2394940a91.24.1769536894069; Tue, 27 Jan 2026 10:01:34 -0800 (PST) Received: from toolbx ([103.230.182.3]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-82379b1bc68sm216891b3a.2.2026.01.27.10.01.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Jan 2026 10:01:33 -0800 (PST) From: Dorjoy Chowdhury To: linux-fsdevel@vger.kernel.org Cc: linux-kernel@vger.kernel.org, viro@zeniv.linux.org.uk, brauner@kernel.org, jack@suse.cz, jlayton@kernel.org, chuck.lever@oracle.com, alex.aring@gmail.com, arnd@arndb.de, adilger@dilger.ca Subject: [PATCH v3 1/4] open: new O_REGULAR flag support Date: Tue, 27 Jan 2026 23:58:17 +0600 Message-ID: <20260127180109.66691-2-dorjoychy111@gmail.com> X-Mailer: git-send-email 2.52.0 In-Reply-To: <20260127180109.66691-1-dorjoychy111@gmail.com> References: <20260127180109.66691-1-dorjoychy111@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This flag indicates the path should be opened if it's a regular file. This is useful to write secure programs that want to avoid being tricked into opening device nodes with special semantics while thinking they operate on regular files. A corresponding error code ENOTREG has been introduced. For example, if open is called on path /dev/null with O_REGULAR in the flag param, it will return -ENOTREG. When used in combination with O_CREAT, either the regular file is created, or if the path already exists, it is opened if it's a regular file. Otherwise, -ENOTREG is returned. -EINVAL is returned when O_REGULAR is combined with O_DIRECTORY (not part of O_TMPFILE) because it doesn't make sense to open a path that is both a directory and a regular file. Signed-off-by: Dorjoy Chowdhury --- arch/alpha/include/uapi/asm/errno.h | 2 ++ arch/alpha/include/uapi/asm/fcntl.h | 1 + arch/mips/include/uapi/asm/errno.h | 2 ++ arch/parisc/include/uapi/asm/errno.h | 2 ++ arch/parisc/include/uapi/asm/fcntl.h | 1 + arch/sparc/include/uapi/asm/errno.h | 2 ++ arch/sparc/include/uapi/asm/fcntl.h | 1 + fs/fcntl.c | 2 +- fs/namei.c | 6 ++++++ fs/open.c | 4 +++- include/linux/fcntl.h | 2 +- include/uapi/asm-generic/errno.h | 2 ++ include/uapi/asm-generic/fcntl.h | 4 ++++ tools/arch/alpha/include/uapi/asm/errno.h | 2 ++ tools/arch/mips/include/uapi/asm/errno.h | 2 ++ tools/arch/parisc/include/uapi/asm/errno.h | 2 ++ tools/arch/sparc/include/uapi/asm/errno.h | 2 ++ tools/include/uapi/asm-generic/errno.h | 2 ++ 18 files changed, 38 insertions(+), 3 deletions(-) diff --git a/arch/alpha/include/uapi/asm/errno.h b/arch/alpha/include/uapi/= asm/errno.h index 6791f6508632..8bbcaa9024f9 100644 --- a/arch/alpha/include/uapi/asm/errno.h +++ b/arch/alpha/include/uapi/asm/errno.h @@ -127,4 +127,6 @@ =20 #define EHWPOISON 139 /* Memory page has hardware error */ =20 +#define ENOTREG 140 /* Not a regular file */ + #endif diff --git a/arch/alpha/include/uapi/asm/fcntl.h b/arch/alpha/include/uapi/= asm/fcntl.h index 50bdc8e8a271..4da5a64c23bd 100644 --- a/arch/alpha/include/uapi/asm/fcntl.h +++ b/arch/alpha/include/uapi/asm/fcntl.h @@ -34,6 +34,7 @@ =20 #define O_PATH 040000000 #define __O_TMPFILE 0100000000 +#define O_REGULAR 0200000000 =20 #define F_GETLK 7 #define F_SETLK 8 diff --git a/arch/mips/include/uapi/asm/errno.h b/arch/mips/include/uapi/as= m/errno.h index c01ed91b1ef4..293c78777254 100644 --- a/arch/mips/include/uapi/asm/errno.h +++ b/arch/mips/include/uapi/asm/errno.h @@ -126,6 +126,8 @@ =20 #define EHWPOISON 168 /* Memory page has hardware error */ =20 +#define ENOTREG 169 /* Not a regular file */ + #define EDQUOT 1133 /* Quota exceeded */ =20 =20 diff --git a/arch/parisc/include/uapi/asm/errno.h b/arch/parisc/include/uap= i/asm/errno.h index 8cbc07c1903e..442917484f99 100644 --- a/arch/parisc/include/uapi/asm/errno.h +++ b/arch/parisc/include/uapi/asm/errno.h @@ -124,4 +124,6 @@ =20 #define EHWPOISON 257 /* Memory page has hardware error */ =20 +#define ENOTREG 258 /* Not a regular file */ + #endif diff --git a/arch/parisc/include/uapi/asm/fcntl.h b/arch/parisc/include/uap= i/asm/fcntl.h index 03dee816cb13..0cc3320fe326 100644 --- a/arch/parisc/include/uapi/asm/fcntl.h +++ b/arch/parisc/include/uapi/asm/fcntl.h @@ -19,6 +19,7 @@ =20 #define O_PATH 020000000 #define __O_TMPFILE 040000000 +#define O_REGULAR 0100000000 =20 #define F_GETLK64 8 #define F_SETLK64 9 diff --git a/arch/sparc/include/uapi/asm/errno.h b/arch/sparc/include/uapi/= asm/errno.h index 4a41e7835fd5..8dce0bfeab74 100644 --- a/arch/sparc/include/uapi/asm/errno.h +++ b/arch/sparc/include/uapi/asm/errno.h @@ -117,4 +117,6 @@ =20 #define EHWPOISON 135 /* Memory page has hardware error */ =20 +#define ENOTREG 136 /* Not a regular file */ + #endif diff --git a/arch/sparc/include/uapi/asm/fcntl.h b/arch/sparc/include/uapi/= asm/fcntl.h index 67dae75e5274..a93d18d2c23e 100644 --- a/arch/sparc/include/uapi/asm/fcntl.h +++ b/arch/sparc/include/uapi/asm/fcntl.h @@ -37,6 +37,7 @@ =20 #define O_PATH 0x1000000 #define __O_TMPFILE 0x2000000 +#define O_REGULAR 0x4000000 =20 #define F_GETOWN 5 /* for sockets. */ #define F_SETOWN 6 /* for sockets. */ diff --git a/fs/fcntl.c b/fs/fcntl.c index f93dbca08435..62ab4ad2b6f5 100644 --- a/fs/fcntl.c +++ b/fs/fcntl.c @@ -1169,7 +1169,7 @@ static int __init fcntl_init(void) * Exceptions: O_NONBLOCK is a two bit define on parisc; O_NDELAY * is defined as O_NONBLOCK on some platforms and not on others. */ - BUILD_BUG_ON(20 - 1 /* for O_RDONLY being 0 */ !=3D + BUILD_BUG_ON(21 - 1 /* for O_RDONLY being 0 */ !=3D HWEIGHT32( (VALID_OPEN_FLAGS & ~(O_NONBLOCK | O_NDELAY)) | __FMODE_EXEC)); diff --git a/fs/namei.c b/fs/namei.c index b28ecb699f32..f5504ae4b03c 100644 --- a/fs/namei.c +++ b/fs/namei.c @@ -4616,6 +4616,10 @@ static int do_open(struct nameidata *nd, if (unlikely(error)) return error; } + + if ((open_flag & O_REGULAR) && !d_is_reg(nd->path.dentry)) + return -ENOTREG; + if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry)) return -ENOTDIR; =20 @@ -4765,6 +4769,8 @@ static int do_o_path(struct nameidata *nd, unsigned f= lags, struct file *file) struct path path; int error =3D path_lookupat(nd, flags, &path); if (!error) { + if ((file->f_flags & O_REGULAR) && !d_is_reg(path.dentry)) + return -ENOTREG; audit_inode(nd->name, path.dentry, 0); error =3D vfs_open(&path, file); path_put(&path); diff --git a/fs/open.c b/fs/open.c index 74c4c1462b3e..82153e21907e 100644 --- a/fs/open.c +++ b/fs/open.c @@ -1173,7 +1173,7 @@ struct file *kernel_file_open(const struct path *path= , int flags, EXPORT_SYMBOL_GPL(kernel_file_open); =20 #define WILL_CREATE(flags) (flags & (O_CREAT | __O_TMPFILE)) -#define O_PATH_FLAGS (O_DIRECTORY | O_NOFOLLOW | O_PATH | O_CLOEXEC) +#define O_PATH_FLAGS (O_DIRECTORY | O_NOFOLLOW | O_PATH | O_CLOEXEC | O_R= EGULAR) =20 inline struct open_how build_open_how(int flags, umode_t mode) { @@ -1250,6 +1250,8 @@ inline int build_open_flags(const struct open_how *ho= w, struct open_flags *op) return -EINVAL; if (!(acc_mode & MAY_WRITE)) return -EINVAL; + } else if ((flags & O_DIRECTORY) && (flags & O_REGULAR)) { + return -EINVAL; } if (flags & O_PATH) { /* O_PATH only permits certain other flags to be set. */ diff --git a/include/linux/fcntl.h b/include/linux/fcntl.h index a332e79b3207..4fd07b0e0a17 100644 --- a/include/linux/fcntl.h +++ b/include/linux/fcntl.h @@ -10,7 +10,7 @@ (O_RDONLY | O_WRONLY | O_RDWR | O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC | \ O_APPEND | O_NDELAY | O_NONBLOCK | __O_SYNC | O_DSYNC | \ FASYNC | O_DIRECT | O_LARGEFILE | O_DIRECTORY | O_NOFOLLOW | \ - O_NOATIME | O_CLOEXEC | O_PATH | __O_TMPFILE) + O_NOATIME | O_CLOEXEC | O_PATH | __O_TMPFILE | O_REGULAR) =20 /* List of all valid flags for the how->resolve argument: */ #define VALID_RESOLVE_FLAGS \ diff --git a/include/uapi/asm-generic/errno.h b/include/uapi/asm-generic/er= rno.h index 92e7ae493ee3..2216ab9aa32e 100644 --- a/include/uapi/asm-generic/errno.h +++ b/include/uapi/asm-generic/errno.h @@ -122,4 +122,6 @@ =20 #define EHWPOISON 133 /* Memory page has hardware error */ =20 +#define ENOTREG 134 /* Not a regular file */ + #endif diff --git a/include/uapi/asm-generic/fcntl.h b/include/uapi/asm-generic/fc= ntl.h index 613475285643..3468b352a575 100644 --- a/include/uapi/asm-generic/fcntl.h +++ b/include/uapi/asm-generic/fcntl.h @@ -88,6 +88,10 @@ #define __O_TMPFILE 020000000 #endif =20 +#ifndef O_REGULAR +#define O_REGULAR 040000000 +#endif + /* a horrid kludge trying to make sure that this will fail on old kernels = */ #define O_TMPFILE (__O_TMPFILE | O_DIRECTORY) =20 diff --git a/tools/arch/alpha/include/uapi/asm/errno.h b/tools/arch/alpha/i= nclude/uapi/asm/errno.h index 6791f6508632..8bbcaa9024f9 100644 --- a/tools/arch/alpha/include/uapi/asm/errno.h +++ b/tools/arch/alpha/include/uapi/asm/errno.h @@ -127,4 +127,6 @@ =20 #define EHWPOISON 139 /* Memory page has hardware error */ =20 +#define ENOTREG 140 /* Not a regular file */ + #endif diff --git a/tools/arch/mips/include/uapi/asm/errno.h b/tools/arch/mips/inc= lude/uapi/asm/errno.h index c01ed91b1ef4..293c78777254 100644 --- a/tools/arch/mips/include/uapi/asm/errno.h +++ b/tools/arch/mips/include/uapi/asm/errno.h @@ -126,6 +126,8 @@ =20 #define EHWPOISON 168 /* Memory page has hardware error */ =20 +#define ENOTREG 169 /* Not a regular file */ + #define EDQUOT 1133 /* Quota exceeded */ =20 =20 diff --git a/tools/arch/parisc/include/uapi/asm/errno.h b/tools/arch/parisc= /include/uapi/asm/errno.h index 8cbc07c1903e..442917484f99 100644 --- a/tools/arch/parisc/include/uapi/asm/errno.h +++ b/tools/arch/parisc/include/uapi/asm/errno.h @@ -124,4 +124,6 @@ =20 #define EHWPOISON 257 /* Memory page has hardware error */ =20 +#define ENOTREG 258 /* Not a regular file */ + #endif diff --git a/tools/arch/sparc/include/uapi/asm/errno.h b/tools/arch/sparc/i= nclude/uapi/asm/errno.h index 4a41e7835fd5..8dce0bfeab74 100644 --- a/tools/arch/sparc/include/uapi/asm/errno.h +++ b/tools/arch/sparc/include/uapi/asm/errno.h @@ -117,4 +117,6 @@ =20 #define EHWPOISON 135 /* Memory page has hardware error */ =20 +#define ENOTREG 136 /* Not a regular file */ + #endif diff --git a/tools/include/uapi/asm-generic/errno.h b/tools/include/uapi/as= m-generic/errno.h index 92e7ae493ee3..2216ab9aa32e 100644 --- a/tools/include/uapi/asm-generic/errno.h +++ b/tools/include/uapi/asm-generic/errno.h @@ -122,4 +122,6 @@ =20 #define EHWPOISON 133 /* Memory page has hardware error */ =20 +#define ENOTREG 134 /* Not a regular file */ + #endif --=20 2.52.0