Skip to content

Commit

Permalink
linux-user: add openat2 support in linux-user
Browse files Browse the repository at this point in the history
This commit adds support for the `openat2()` syscall in the
`linux-user` userspace emulator.

It is implemented by extracting a new helper `maybe_do_fake_open()`
out of the exiting `do_guest_openat()` and share that with the
new `do_guest_openat2()`. Unfortunatly we cannot just make
do_guest_openat2() a superset of do_guest_openat() because the
openat2() syscall is stricter with the argument checking and
will return an error for invalid flags or mode combinations (which
open()/openat() will ignore).

Note that in this commit using openat2() for a "faked" file in
/proc will ignore the "resolve" flags. This is not great but it
seems similar to the exiting behavior when openat() is called
with a dirfd to "/proc". Here too the fake file lookup will
not catch the special file. Alternatively we could simply
fail with `-TARGET_ENOSYS` (or similar) if `resolve` flags
are passed and we found something that looks like a file that
needs faking.

Signed-off-by: Michael Vogt <[email protected]>

Buglink: osbuild/bootc-image-builder#619
  • Loading branch information
mvo5 committed Aug 28, 2024
1 parent cec9917 commit 0da8162
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 4 deletions.
2 changes: 2 additions & 0 deletions linux-user/qemu.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ struct TaskState {
abi_long do_brk(abi_ulong new_brk);
int do_guest_openat(CPUArchState *cpu_env, int dirfd, const char *pathname,
int flags, mode_t mode, bool safe);
int do_guest_openat2(CPUArchState *cpu_env, int dirfd, const char *pathname,
struct target_open_how *how, bool safe);
ssize_t do_guest_readlink(const char *pathname, char *buf, size_t bufsiz);

/* user access */
Expand Down
71 changes: 67 additions & 4 deletions linux-user/syscall.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@
#include <sys/kcov.h>
#endif

#ifdef HAVE_OPENAT2_H
#include <linux/openat2.h>
/* glibc has no header for SYS_openat2 so we need to get it via syscall.h */
#include <sys/syscall.h>
#endif

#define termios host_termios
#define winsize host_winsize
#define termio host_termio
Expand Down Expand Up @@ -653,6 +659,10 @@ safe_syscall3(ssize_t, read, int, fd, void *, buff, size_t, count)
safe_syscall3(ssize_t, write, int, fd, const void *, buff, size_t, count)
safe_syscall4(int, openat, int, dirfd, const char *, pathname, \
int, flags, mode_t, mode)
#ifdef HAVE_OPENAT2_H
safe_syscall4(int, openat2, int, dirfd, const char *, pathname, \
const struct open_how *, how, size_t, size);
#endif
#if defined(TARGET_NR_wait4) || defined(TARGET_NR_waitpid)
safe_syscall4(pid_t, wait4, pid_t, pid, int *, status, int, options, \
struct rusage *, rusage)
Expand Down Expand Up @@ -8334,8 +8344,9 @@ static int open_net_route(CPUArchState *cpu_env, int fd)
}
#endif

int do_guest_openat(CPUArchState *cpu_env, int dirfd, const char *fname,
int flags, mode_t mode, bool safe)
static int maybe_do_fake_open(CPUArchState *cpu_env, int dirfd,
const char *fname, int flags, mode_t mode,
bool safe)
{
g_autofree char *proc_name = NULL;
const char *pathname;
Expand Down Expand Up @@ -8418,10 +8429,43 @@ int do_guest_openat(CPUArchState *cpu_env, int dirfd, const char *fname,
return fd;
}

return -1;
}

int do_guest_openat(CPUArchState *cpu_env, int dirfd, const char *fname,
int flags, mode_t mode, bool safe)
{
int fd = maybe_do_fake_open(cpu_env, dirfd, fname, flags, mode, safe);
if (fd >= 0)
return fd;

if (safe) {
return safe_openat(dirfd, path(fname), flags, mode);
} else {
return openat(dirfd, path(fname), flags, mode);
}
}

int do_guest_openat2(CPUArchState *cpu_env, int dirfd, const char *fname,
struct target_open_how *how, bool safe)
{
/*
* Ideally we would pass "how->resolve" flags into this helper too but
* the lookup for file that need faking is based on "realpath()" so
* neither a dirfd for "proc" nor restrictions via "resolve" flags can
* be honored.
*/
int fd = maybe_do_fake_open(cpu_env, dirfd, fname, how->flags, how->mode,
safe);
if (fd >= 0)
return fd;

if (safe) {
return safe_openat(dirfd, path(pathname), flags, mode);
return safe_openat2(dirfd, fname, (struct open_how *)how,
sizeof(struct target_open_how));
} else {
return openat(dirfd, path(pathname), flags, mode);
return syscall(SYS_openat2, dirfd, fname, (struct open_hosw *)how,
sizeof(struct target_open_how));
}
}

Expand Down Expand Up @@ -9197,6 +9241,25 @@ static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1,
fd_trans_unregister(ret);
unlock_user(p, arg2, 0);
return ret;
#if defined(TARGET_NR_openat2) && defined(HAVE_OPENAT2_H)
case TARGET_NR_openat2:
{
struct target_open_how how, *target_how;
if (!(p = lock_user_string(arg2)))
return -TARGET_EFAULT;
if (!(lock_user_struct(VERIFY_READ, target_how, arg3, 1)))
return -TARGET_EFAULT;
how.flags = target_to_host_bitmask(target_how->flags,
fcntl_flags_tbl);
how.mode = tswap64(target_how->mode);
how.resolve = tswap64(target_how->resolve);
ret = get_errno(do_guest_openat2(cpu_env, arg1, p, &how, true));
fd_trans_unregister(ret);
unlock_user_struct(target_how, arg3, 0);
unlock_user(p, arg2, 0);
return ret;
}
#endif
#if defined(TARGET_NR_name_to_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
case TARGET_NR_name_to_handle_at:
ret = do_name_to_handle_at(arg1, arg2, arg3, arg4, arg5);
Expand Down
7 changes: 7 additions & 0 deletions linux-user/syscall_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -2754,4 +2754,11 @@ struct target_sched_param {
abi_int sched_priority;
};

/* from kernel's include/uapi/linux/openat2.h */
struct target_open_how {
__u64 flags;
__u64 mode;
__u64 resolve;
};

#endif
1 change: 1 addition & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -2465,6 +2465,7 @@ config_host_data.set('CONFIG_LINUX_MAGIC_H', cc.has_header('linux/magic.h'))
config_host_data.set('CONFIG_VALGRIND_H', cc.has_header('valgrind/valgrind.h'))
config_host_data.set('HAVE_BTRFS_H', cc.has_header('linux/btrfs.h'))
config_host_data.set('HAVE_DRM_H', cc.has_header('libdrm/drm.h'))
config_host_data.set('HAVE_OPENAT2_H', cc.has_header('linux/openat2.h'))
config_host_data.set('HAVE_PTY_H', cc.has_header('pty.h'))
config_host_data.set('HAVE_SYS_DISK_H', cc.has_header('sys/disk.h'))
config_host_data.set('HAVE_SYS_IOCCOM_H', cc.has_header('sys/ioccom.h'))
Expand Down

0 comments on commit 0da8162

Please sign in to comment.