Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date: Mon, 6 May 2024 10:50:57 -0400
From: Rich Felker <dalias@...c.org>
To: Tony Ambardar <tony.ambardar@...il.com>
Cc: musl@...ts.openwall.com
Subject: Re: [PATCH v2] add renameat2 linux syscall wrapper

On Tue, Apr 23, 2024 at 04:43:55PM -0700, Tony Ambardar wrote:
> This syscall is available since Linux 3.15 and also implemented in glibc
> from version 2.28. It is commonly used in filesystem or security contexts.
> 
> Constants RENAME_NOREPLACE, RENAME_EXCHANGE, RENAME_WHITEOUT are guarded by
> _GNU_SOURCE as with glibc.
> 
> Signed-off-by: Tony Ambardar <Tony.Ambardar@...il.com>
> ---
> v1 -> v2:
>  * align related constants
>  * drop 'int' from 'unsigned int'
>  * add fallback to SYS_renameat where applicable
> ---
>  include/stdio.h       |  7 +++++++
>  src/linux/renameat2.c | 12 ++++++++++++
>  2 files changed, 19 insertions(+)
>  create mode 100644 src/linux/renameat2.c
> 
> diff --git a/include/stdio.h b/include/stdio.h
> index cb858618..4ea4c170 100644
> --- a/include/stdio.h
> +++ b/include/stdio.h
> @@ -158,6 +158,13 @@ char *ctermid(char *);
>  #define L_ctermid 20
>  #endif
>  
> +#if defined(_GNU_SOURCE)
> +#define RENAME_NOREPLACE (1 << 0)
> +#define RENAME_EXCHANGE  (1 << 1)
> +#define RENAME_WHITEOUT  (1 << 2)
> +
> +int renameat2(int, const char *, int, const char *, unsigned);
> +#endif
>  
>  #if defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \
>   || defined(_BSD_SOURCE)
> diff --git a/src/linux/renameat2.c b/src/linux/renameat2.c
> new file mode 100644
> index 00000000..48ee8d53
> --- /dev/null
> +++ b/src/linux/renameat2.c
> @@ -0,0 +1,12 @@
> +#define _GNU_SOURCE
> +#include <stdio.h>
> +#include "syscall.h"
> +
> +int renameat2(int oldfd, const char *old, int newfd, const char *new, unsigned flags)
> +{
> +	int r = __syscall(SYS_renameat2, oldfd, old, newfd, new, flags);
> +#ifdef SYS_renameat
> +	if (r==-ENOSYS && !flags) r = __syscall(SYS_renameat, oldfd, old, newfd, new);
> +#endif
> +	return __syscall_ret(r);
> +}
> -- 
> 2.34.1

If flags is 0, the SYS_renameat syscall is semantically equivalent to
the SYS_renameat2 one, so it would be better to just unconditionally
do that first rather than failing and falling back.

Rich

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.