mergerfs/tools/preload.c
Antonio SJ Musumeci 11e759101d fix: make the preload helper correct on FreeBSD and safe on reopen
- The helper called Linux fgetxattr on FreeBSD. Read mergerfs.fullpath
  there through extattr_get_fd in EXTATTR_NAMESPACE_USER; Linux keeps
  user.mergerfs.fullpath.
- FreeBSD mode_t is promoted when passed through an ellipsis, so
  reading it back as mode_t was undefined. Read the promoted type.
- O_TMPFILE includes O_DIRECTORY on Linux, so a plain directory open
  consumed a mode argument that wasn't passed. Require O_CREAT or the
  whole nonzero O_TMPFILE mask.
- The backing path is returned without a terminating NUL. Reject empty
  or oversized values and terminate it before reopening; keep the
  already-open descriptor when no usable path is available.
- The reopen of the backing file repeated O_TRUNC, and creat() ran
  creat() again. The open through mergerfs had already truncated that
  file, so the second truncation could only destroy data another
  process wrote in between. Drop O_TRUNC from the reopen and reopen
  creat()ed files O_WRONLY.
- fopen and fopen64 reopened the branch path with the caller's mode,
  so w truncated a second time and a could recreate a name removed in
  between. Redirect only modes starting with r, including r+, as
  given, and keep the original stream otherwise; tooling.md says so.
  TEST_preload_fopen_modes checks the bytes and that a missing branch
  name is not created.
- fs_fstatat.hpp used AT_SYMLINK_NOFOLLOW without including fcntl.h,
  which failed against FreeBSD headers.
2026-11-29 23:11:08 -05:00

440 lines
9.4 KiB
C

/*
ISC License
Copyright (c) 2025, Antonio SJ Musumeci <trapexit@spawn.link>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#ifdef __FreeBSD__
#define O_TMPFILE 0
#else
#define _GNU_SOURCE
//#define _POSIX_C_SOURCE 200809L
#endif
#include <assert.h>
#include <dlfcn.h>
#include <fcntl.h>
#include <limits.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#ifdef __FreeBSD__
#include <sys/extattr.h>
#else
#include <sys/xattr.h>
#endif
#include <unistd.h>
// Unary + applies the same integer promotion as a variadic argument.
typedef __typeof__(+(mode_t)0) OpenModeArg;
static
int
_open_needs_mode(const int flags_)
{
return ((flags_ & O_CREAT) || (O_TMPFILE && ((flags_ & O_TMPFILE) == O_TMPFILE)));
}
typedef char IOCTL_BUF[4096];
#define IOCTL_APP_TYPE 0xDF
#define IOCTL_FILE_INFO _IOWR(IOCTL_APP_TYPE,0,IOCTL_BUF)
#define LOAD_FUNC(func) \
do \
{ \
if(!_libc_##func) \
_libc_##func = (func##_func_t)dlsym(RTLD_NEXT,#func); \
assert(_libc_##func != NULL); \
} \
while(0)
typedef int (*open_func_t)(const char*, int, ...);
typedef int (*open64_func_t)(const char*, int, ...);
typedef int (*openat_func_t)(int, const char*, int, ...);
typedef int (*openat64_func_t)(int, const char*, int, ...);
typedef int (*creat_func_t)(const char*, mode_t);
typedef int (*creat64_func_t)(const char*, mode_t);
typedef FILE* (*fopen_func_t)(const char*, const char*);
typedef FILE* (*fopen64_func_t)(const char*, const char*);
static open_func_t _libc_open = NULL;
static open64_func_t _libc_open64 = NULL;
static openat_func_t _libc_openat = NULL;
static openat64_func_t _libc_openat64 = NULL;
static fopen_func_t _libc_fopen = NULL;
static fopen64_func_t _libc_fopen64 = NULL;
static creat_func_t _libc_creat = NULL;
static creat64_func_t _libc_creat64 = NULL;
static
int
get_underlying_filepath(int fd_,
char *filepath_,
size_t filepath_size_)
{
ssize_t rv;
#ifdef __FreeBSD__
rv = extattr_get_fd(fd_,EXTATTR_NAMESPACE_USER,"mergerfs.fullpath",filepath_,filepath_size_);
#else
rv = fgetxattr(fd_,"user.mergerfs.fullpath",filepath_,filepath_size_);
#endif
if((rv <= 0) || ((size_t)rv >= filepath_size_))
return -1;
// The virtual xattr contains pathname bytes, not a trailing NUL.
filepath_[rv] = '\0';
return rv;
}
#ifndef _FILE_OFFSET_BITS
int
open(const char *pathname_,
int flags_,
...)
{
int rv;
int fd;
mode_t mode;
struct stat st;
LOAD_FUNC(open);
mode = 0;
if(_open_needs_mode(flags_))
{
va_list args;
va_start(args,flags_);
mode = va_arg(args,OpenModeArg);
va_end(args);
}
fd = _libc_open(pathname_,flags_,mode);
if(fd == -1)
return -1;
if(flags_ & (O_DIRECTORY|O_PATH|O_TMPFILE))
return fd;
rv = fstat(fd,&st);
if(rv == -1)
return fd;
if((st.st_mode & S_IFMT) != S_IFREG)
return fd;
char real_pathname[PATH_MAX];
rv = get_underlying_filepath(fd,real_pathname,sizeof(real_pathname));
if(rv == -1)
return fd;
// Truncation already happened on the mergerfs open; do not repeat it.
flags_ &= ~(O_EXCL|O_CREAT|O_TRUNC);
rv = _libc_open(real_pathname,flags_,mode);
if(rv == -1)
return fd;
close(fd);
return rv;
}
#endif
int
open64(const char *pathname_,
int flags_,
...)
{
int rv;
int fd;
mode_t mode;
struct stat st;
LOAD_FUNC(open64);
mode = 0;
if(_open_needs_mode(flags_))
{
va_list args;
va_start(args,flags_);
mode = va_arg(args,OpenModeArg);
va_end(args);
}
fd = _libc_open64(pathname_,flags_,mode);
if(fd == -1)
return -1;
if(flags_ & (O_DIRECTORY|O_PATH|O_TMPFILE))
return fd;
rv = fstat(fd,&st);
if(rv == -1)
return fd;
if((st.st_mode & S_IFMT) != S_IFREG)
return fd;
char real_pathname[PATH_MAX];
rv = get_underlying_filepath(fd,real_pathname,sizeof(real_pathname));
if(rv == -1)
return fd;
// Truncation already happened on the mergerfs open; do not repeat it.
flags_ &= ~(O_EXCL|O_CREAT|O_TRUNC);
rv = _libc_open64(real_pathname,flags_,mode);
if(rv == -1)
return fd;
close(fd);
return rv;
}
#ifndef _FILE_OFFSET_BITS
int
openat(int dirfd_,
const char *pathname_,
int flags_,
...)
{
int rv;
int fd;
mode_t mode;
struct stat st;
LOAD_FUNC(openat);
mode = 0;
if(_open_needs_mode(flags_))
{
va_list args;
va_start(args,flags_);
mode = va_arg(args,OpenModeArg);
va_end(args);
}
fd = _libc_openat(dirfd_,pathname_,flags_,mode);
if(fd == -1)
return -1;
if(flags_ & (O_DIRECTORY|O_PATH|O_TMPFILE))
return fd;
rv = fstat(fd,&st);
if(rv == -1)
return fd;
if((st.st_mode & S_IFMT) != S_IFREG)
return fd;
char real_pathname[PATH_MAX];
rv = get_underlying_filepath(fd,real_pathname,sizeof(real_pathname));
if(rv == -1)
return fd;
// Truncation already happened on the mergerfs open; do not repeat it.
flags_ &= ~(O_EXCL|O_CREAT|O_TRUNC);
rv = _libc_openat(dirfd_,real_pathname,flags_,mode);
if(rv == -1)
return fd;
close(fd);
return rv;
}
#endif
int
openat64(int dirfd_,
const char *pathname_,
int flags_,
...)
{
int rv;
int fd;
mode_t mode;
struct stat st;
LOAD_FUNC(openat64);
mode = 0;
if(_open_needs_mode(flags_))
{
va_list args;
va_start(args,flags_);
mode = va_arg(args,OpenModeArg);
va_end(args);
}
fd = _libc_openat64(dirfd_,pathname_,flags_,mode);
if(fd == -1)
return -1;
if(flags_ & (O_DIRECTORY|O_PATH|O_TMPFILE))
return fd;
rv = fstat(fd,&st);
if(rv == -1)
return fd;
if((st.st_mode & S_IFMT) != S_IFREG)
return fd;
char real_pathname[PATH_MAX];
rv = get_underlying_filepath(fd,real_pathname,sizeof(real_pathname));
if(rv == -1)
return fd;
// Truncation already happened on the mergerfs open; do not repeat it.
flags_ &= ~(O_EXCL|O_CREAT|O_TRUNC);
rv = _libc_openat64(dirfd_,real_pathname,flags_,mode);
if(rv == -1)
return fd;
close(fd);
return rv;
}
// Replaying a creating or truncating mode could recreate a vanished
// name or truncate an existing file twice. Only redirect read modes.
static
FILE*
_fopen_try_backing_stream(FILE *f_,
const char *mode_,
fopen_func_t fopen_func_)
{
int fd;
int rv;
FILE *f2;
char real_pathname[PATH_MAX];
struct stat st;
if((mode_[0] != 'r') || (strchr(mode_,'x') != NULL))
return f_;
fd = fileno(f_);
if(fd == -1)
return f_;
rv = fstat(fd,&st);
if(rv == -1)
return f_;
if((st.st_mode & S_IFMT) != S_IFREG)
return f_;
rv = get_underlying_filepath(fd,real_pathname,sizeof(real_pathname));
if(rv == -1)
return f_;
f2 = fopen_func_(real_pathname,mode_);
if(f2 == NULL)
return f_;
fclose(f_);
return f2;
}
#ifndef _FILE_OFFSET_BITS
FILE*
fopen(const char *pathname_,
const char *mode_)
{
FILE *f;
LOAD_FUNC(fopen);
f = _libc_fopen(pathname_,mode_);
if(f == NULL)
return NULL;
return _fopen_try_backing_stream(f,mode_,_libc_fopen);
}
#endif
FILE*
fopen64(const char *pathname_,
const char *mode_)
{
FILE *f;
LOAD_FUNC(fopen64);
f = _libc_fopen64(pathname_,mode_);
if(f == NULL)
return NULL;
return _fopen_try_backing_stream(f,mode_,_libc_fopen64);
}
#ifndef _FILE_OFFSET_BITS
int
creat(const char *pathname_,
mode_t mode_)
{
int fd;
int rv;
LOAD_FUNC(creat);
LOAD_FUNC(open);
fd = _libc_creat(pathname_,mode_);
if(fd == -1)
return -1;
char real_pathname[PATH_MAX];
rv = get_underlying_filepath(fd,real_pathname,sizeof(real_pathname));
if(rv == -1)
return fd;
// The mergerfs creat already created and truncated the backing file.
rv = _libc_open(real_pathname,O_WRONLY);
if(rv == -1)
return fd;
close(fd);
return rv;
}
#endif
int
creat64(const char *pathname_,
mode_t mode_)
{
int fd;
int rv;
LOAD_FUNC(creat64);
LOAD_FUNC(open64);
fd = _libc_creat64(pathname_,mode_);
if(fd == -1)
return -1;
char real_pathname[PATH_MAX];
rv = get_underlying_filepath(fd,real_pathname,sizeof(real_pathname));
if(rv == -1)
return fd;
// The mergerfs creat already created and truncated the backing file.
rv = _libc_open64(real_pathname,O_WRONLY);
if(rv == -1)
return fd;
close(fd);
return rv;
}