linux-user: add openat2 support in linux-user
PATCH 1/1 linux-user: add openat2 support in linux-user
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 may not catch the special file because "realpath()" is used to determine if the path is in /proc. Alternatively to ignoring we could simply fail with -TARGET_ENOSYS (or similar) if resolve flags are passed and we found something that looks like a file in /proc that needs faking.
--- linux-user/qemu.h | 4 +++ linux-user/syscall.c | 73 ++++++++++++++++++++++++++++++++++++--- linux-user/syscall_defs.h | 7 ++++ meson.build | 1 + 4 files changed, 81 insertions(+), 4 deletions(-)
linux-user/qemu.h +4 -0
@@ -164,6 +164,10 @@ 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); +#ifdef HAVE_OPENAT2_H +int do_guest_openat2 (CPUArchState *cpu_env,int dirfd,const char *pathname, +struct target_open_how *how,bool safe); +#endif ssize_t do_guest_readlink (const char *pathname,char *buf,size_t bufsiz);/* user access */
linux-user/syscall.c +69 -4
108 lines not on this page · raw message
linux-user/syscall_defs.h +7 -0
@@ -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
meson.build +1 -0
@@ -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'))
Signature · 1 line
2.45.2
Quoted 36 lines · Michael Vogt
This commit adds support for the
openat2()syscall in thelinux-useruserspace emulator.It is implemented by extracting a new helper
maybe_do_fake_open()out of the exitingdo_guest_openat()and share that with the newdo_guest_openat2(). Unfortunatly we cannot just makedo_guest_openat2()a superset ofdo_guest_openat()because theopenat2()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 whenopenat()is called with a dirfd to "/proc". Here too the fake file lookup may not catch the special file because "realpath()" is used to determine if the path is in /proc. Alternatively to ignoring we could simply fail with-TARGET_ENOSYS(or similar) ifresolveflags are passed and we found something that looks like a file in /proc that needs faking.Signed-off-by Michael Vogt <[email protected]>--- linux-user/qemu.h | 4 +++ linux-user/syscall.c | 73 ++++++++++++++++++++++++++++++++++++--- linux-user/syscall_defs.h | 7 ++++ meson.build | 1 + 4 files changed, 81 insertions(+), 4 deletions(-)linux-user/qemu.h +0
-0
linux-user/qemu.h struct TaskState { · line 170
Quoted 7 lines
linux-user/qemu.h +4
-0@@ -164,6 +164,10 @@ 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); +#ifdef HAVE_OPENAT2_H +int do_guest_openat2 (CPUArchState *cpu_env,int dirfd,const char *pathname, +struct target_open_how *how,bool safe); +#endif
Not needed. The only reason do_guest_openat is declared here is for gdbstub/.
linux-user/qemu.h struct TaskState {
Quoted 5 lines
linux-user/qemu.h +5
-0+#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
No need for sys/syscall.h, handled by safe_syscall4.
linux-user/qemu.h
Quoted 3 lines
linux-user/qemu.h +3
-0+#ifdef HAVE_OPENAT2_H +int do_guest_openat2 (CPUArchState *cpu_env,int dirfd,const char *fname, +struct target_open_how *how,bool safe)
No need for safe argument, that is only used by gdbstub/.
Quoted 4 lines
linux-user/qemu.h +4
-0+{ +/* +* Ideally we would pass "how->resolve" flags into this helper too but +* the lookup for files that need faking is based on "realpath()" so
linux-user/qemu.h
Quoted 10 lines
linux-user/qemu.h +10
-0+* neither a dirfd for "proc" nor restrictions via "resolve" flags can +* be honored right now. +*/ +int fd =maybe_do_fake_open (cpu_env, dirfd, fname, how->flags, how->mode, + safe); +if (fd >=0 ) +return fd; + +if (safe) { + return safe_openat2(dirfd, fname, (struct open_how *)how,
This cast is an indication that you're doing something wrong.
linux-user/qemu.h
Quoted 6 lines
linux-user/qemu.h +6
-0+#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 ;
No assignment within if.
linux-user/qemu.h
Quoted 13 lines
linux-user/qemu.h +13
-0+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
Move all of this code into the helper function for clarity.
Missing validation of small arg4 (EINVAL). Missing validation of zeros for large arg4 (E2BIG).
The 'how' variable should be the host open_how structure.
Then you don't need the incorrect cast above.
Missing a zero of the structure, which in the future might contain extra fields.
Given linux/openat2.h appeared in linux 5.6, we might be coming to the point at which all supported host os must have it. Otherwise, are you using anything besides struct open_how itself? Perhaps we're better off always defining the structure locally, so that way it cannot expand on us with a mere upgrade of the system linux headers package?
linux-user/qemu.h
Quoted 6 lines
linux-user/qemu.h +6
-0+/* from kernel's include/uapi/linux/openat2.h */ +struct target_open_how { +__u64 flags; +__u64 mode; +__u64 resolve; +};
Using host __u64 is always wrong for target structures. Use "abi_*" types for target alignment requirements.
r~