linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 0/2] uapi/samples: guard renameat2 flag macros
@ 2025-11-10 14:42 Masaharu Noguchi
  2025-11-10 14:42 ` [PATCH v2 1/2] uapi: fcntl: guard AT_RENAME_* aliases Masaharu Noguchi
  2025-11-10 14:42 ` [PATCH v2 2/2] samples: vfs: avoid libc AT_RENAME_* redefinitions Masaharu Noguchi
  0 siblings, 2 replies; 4+ messages in thread
From: Masaharu Noguchi @ 2025-11-10 14:42 UTC (permalink / raw)
  To: Jeff Layton, Chuck Lever
  Cc: Jesper Juhl, David Laight, Alexander Aring, linux-fsdevel,
	linux-kernel, Masaharu Noguchi

Including `<linux/fcntl.h>` after libc headers leaves the renameat2 flag
macros stuck with libc's values, and our sample code in turn redefines
them when it includes the uapi header.  This little series makes the
uapi header resilient to prior definitions and ensures the sample drops
any libc remnants before pulling in the kernel constants.

Changes since v1 (based on feedback from David Laight):
- uapi change now checks the macro values and undefines mismatches
- sample code always undefines the macros up front and documents why

Link: https://lore.kernel.org/all/20251109071304.2415982-1-nogunix@gmail.com/

Masaharu Noguchi (2):
  uapi: fcntl: guard AT_RENAME_* aliases
  samples: vfs: avoid libc AT_RENAME_* redefinitions

 include/uapi/linux/fcntl.h | 15 ++++++++++++++-
 samples/vfs/test-statx.c   |  9 +++++++++
 2 files changed, 23 insertions(+), 1 deletion(-)


base-commit: e9a6fb0bcdd7609be6969112f3fbfcce3b1d4a7c
-- 
2.51.1

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v2 1/2] uapi: fcntl: guard AT_RENAME_* aliases
  2025-11-10 14:42 [PATCH v2 0/2] uapi/samples: guard renameat2 flag macros Masaharu Noguchi
@ 2025-11-10 14:42 ` Masaharu Noguchi
  2025-11-10 23:11   ` David Laight
  2025-11-10 14:42 ` [PATCH v2 2/2] samples: vfs: avoid libc AT_RENAME_* redefinitions Masaharu Noguchi
  1 sibling, 1 reply; 4+ messages in thread
From: Masaharu Noguchi @ 2025-11-10 14:42 UTC (permalink / raw)
  To: Jeff Layton, Chuck Lever
  Cc: Jesper Juhl, David Laight, Alexander Aring, linux-fsdevel,
	linux-kernel, Masaharu Noguchi

Including <linux/fcntl.h> after libc headers such as stdio.h may leave
the renameat2() flag macros defined to libc's values.  That leaks the
wrong numbers into user space even though the kernel header tries to
provide its own aliases.

Check whether AT_RENAME_* is already defined and whether the value
matches what the uapi header expects.  If not, drop the old definition
and replace it with the kernel one so the exported flags stay stable
regardless of include order.

Signed-off-by: Masaharu Noguchi <nogunix@gmail.com>
---
 include/uapi/linux/fcntl.h | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/include/uapi/linux/fcntl.h b/include/uapi/linux/fcntl.h
index 3741ea1b73d8..8b667550e44a 100644
--- a/include/uapi/linux/fcntl.h
+++ b/include/uapi/linux/fcntl.h
@@ -156,10 +156,23 @@
  * as possible, so we can use them for generic bits in the future if necessary.
  */
 
-/* Flags for renameat2(2) (must match legacy RENAME_* flags). */
+/* Flags for renameat2(2) (must match legacy RENAME_* flags).
+ * stdio.h may define these differently, so check explicitly.
+ */
+#if !defined(AT_RENAME_NOREPLACE) || AT_RENAME_NOREPLACE != 0x0001
+#undef AT_RENAME_NOREPLACE
 #define AT_RENAME_NOREPLACE	0x0001
+#endif
+
+#if !defined(AT_RENAME_EXCHANGE) || AT_RENAME_EXCHANGE != 0x0002
+#undef AT_RENAME_EXCHANGE
 #define AT_RENAME_EXCHANGE	0x0002
+#endif
+
+#if !defined(AT_RENAME_WHITEOUT) || AT_RENAME_WHITEOUT != 0x0004
+#undef AT_RENAME_WHITEOUT
 #define AT_RENAME_WHITEOUT	0x0004
+#endif
 
 /* Flag for faccessat(2). */
 #define AT_EACCESS		0x200	/* Test access permitted for
-- 
2.51.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH v2 2/2] samples: vfs: avoid libc AT_RENAME_* redefinitions
  2025-11-10 14:42 [PATCH v2 0/2] uapi/samples: guard renameat2 flag macros Masaharu Noguchi
  2025-11-10 14:42 ` [PATCH v2 1/2] uapi: fcntl: guard AT_RENAME_* aliases Masaharu Noguchi
@ 2025-11-10 14:42 ` Masaharu Noguchi
  1 sibling, 0 replies; 4+ messages in thread
From: Masaharu Noguchi @ 2025-11-10 14:42 UTC (permalink / raw)
  To: Jeff Layton, Chuck Lever
  Cc: Jesper Juhl, David Laight, Alexander Aring, linux-fsdevel,
	linux-kernel, Masaharu Noguchi

Users building the sample after including libc headers such as stdio.h
may inherit libc's AT_RENAME_* macros before <linux/fcntl.h> is pulled
in.  When that happens, the sample ends up with conflicting definitions
or the libc values leak into the rest of the build.

Drop any existing AT_RENAME_* macros before including the uapi header so
that the sample always uses the kernel values and does not trip -Werror
redefinition checks.

Signed-off-by: Masaharu Noguchi <nogunix@gmail.com>
---
 samples/vfs/test-statx.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/samples/vfs/test-statx.c b/samples/vfs/test-statx.c
index 49c7a46cee07..0123ab4efe0a 100644
--- a/samples/vfs/test-statx.c
+++ b/samples/vfs/test-statx.c
@@ -20,7 +20,16 @@
 #include <sys/syscall.h>
 #include <sys/types.h>
 #include <linux/stat.h>
+
+/* Undefine AT_RENAME_* macros that may have been set by libc headers
+ * (e.g. stdio.h) to avoid redefinition conflicts with uapi fcntl.h.
+ */
+#undef AT_RENAME_NOREPLACE
+#undef AT_RENAME_EXCHANGE
+#undef AT_RENAME_WHITEOUT
+
 #include <linux/fcntl.h>
+
 #define statx foo
 #define statx_timestamp foo_timestamp
 struct statx;
-- 
2.51.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v2 1/2] uapi: fcntl: guard AT_RENAME_* aliases
  2025-11-10 14:42 ` [PATCH v2 1/2] uapi: fcntl: guard AT_RENAME_* aliases Masaharu Noguchi
@ 2025-11-10 23:11   ` David Laight
  0 siblings, 0 replies; 4+ messages in thread
From: David Laight @ 2025-11-10 23:11 UTC (permalink / raw)
  To: Masaharu Noguchi
  Cc: Jeff Layton, Chuck Lever, Jesper Juhl, Alexander Aring,
	linux-fsdevel, linux-kernel

On Mon, 10 Nov 2025 23:42:31 +0900
Masaharu Noguchi <nogunix@gmail.com> wrote:

> Including <linux/fcntl.h> after libc headers such as stdio.h may leave
> the renameat2() flag macros defined to libc's values.  That leaks the
> wrong numbers into user space even though the kernel header tries to
> provide its own aliases.
> 
> Check whether AT_RENAME_* is already defined and whether the value
> matches what the uapi header expects.  If not, drop the old definition
> and replace it with the kernel one so the exported flags stay stable
> regardless of include order.
> 
> Signed-off-by: Masaharu Noguchi <nogunix@gmail.com>
> ---
>  include/uapi/linux/fcntl.h | 15 ++++++++++++++-
>  1 file changed, 14 insertions(+), 1 deletion(-)
> 
> diff --git a/include/uapi/linux/fcntl.h b/include/uapi/linux/fcntl.h
> index 3741ea1b73d8..8b667550e44a 100644
> --- a/include/uapi/linux/fcntl.h
> +++ b/include/uapi/linux/fcntl.h
> @@ -156,10 +156,23 @@
>   * as possible, so we can use them for generic bits in the future if necessary.
>   */
>  
> -/* Flags for renameat2(2) (must match legacy RENAME_* flags). */
> +/* Flags for renameat2(2) (must match legacy RENAME_* flags).
> + * stdio.h may define these differently, so check explicitly.
> + */
> +#if !defined(AT_RENAME_NOREPLACE) || AT_RENAME_NOREPLACE != 0x0001
> +#undef AT_RENAME_NOREPLACE

I deliberately left out the #undef so you'd get a compile-time error
if the values of the definitions differ.

	David

>  #define AT_RENAME_NOREPLACE	0x0001
> +#endif
> +
> +#if !defined(AT_RENAME_EXCHANGE) || AT_RENAME_EXCHANGE != 0x0002
> +#undef AT_RENAME_EXCHANGE
>  #define AT_RENAME_EXCHANGE	0x0002
> +#endif
> +
> +#if !defined(AT_RENAME_WHITEOUT) || AT_RENAME_WHITEOUT != 0x0004
> +#undef AT_RENAME_WHITEOUT
>  #define AT_RENAME_WHITEOUT	0x0004
> +#endif
>  
>  /* Flag for faccessat(2). */
>  #define AT_EACCESS		0x200	/* Test access permitted for


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2025-11-10 23:12 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-11-10 14:42 [PATCH v2 0/2] uapi/samples: guard renameat2 flag macros Masaharu Noguchi
2025-11-10 14:42 ` [PATCH v2 1/2] uapi: fcntl: guard AT_RENAME_* aliases Masaharu Noguchi
2025-11-10 23:11   ` David Laight
2025-11-10 14:42 ` [PATCH v2 2/2] samples: vfs: avoid libc AT_RENAME_* redefinitions Masaharu Noguchi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).