uapi/samples: guard renameat2 flag macros
Subsystems (2)
v2 cover uapi/samples: guard renameat2 flag macros
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
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
Signature · 1 line
2.51.1
PATCH v2 1/2 uapi: fcntl: guard AT_RENAME_* aliases
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.
--- include/uapi/linux/fcntl.h | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-)
include/uapi/linux/fcntl.h +14 -1
@@ -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
Signature · 1 line
2.51.1
Re: [PATCH v2 1/2] uapi: fcntl: guard AT_RENAME_* aliases
Quoted 20 lines · On Mon, 10 Nov 2025 23:42:31 +0900 Masaharu Noguchi <[email protected]> wrote:
Including <
linux/fcntl.h> after libc headers such asstdio.hmay leave therenameat2()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 <[email protected]>--- include/uapi/linux/fcntl.h | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-)include/uapi/linux/fcntl.h +0
-0
include/uapi/linux/fcntl.h line 163
Quoted 9 lines
include/uapi/linux/fcntl.h +5
-1@@ -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
Quoted 15 lines
include/uapi/linux/fcntl.h +9
-0#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
PATCH v2 2/2 samples: vfs: avoid libc AT_RENAME_* redefinitions
In reply to Masaharu NoguchiUsers 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.
--- samples/vfs/test-statx.c | 9 +++++++++ 1 file changed, 9 insertions(+)
samples/vfs/test-statx.c +9 -0
@@ -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_timestampstruct statx ;
Signature · 1 line
2.51.1