linux-user: local results for relative proc opens (#4458)
Hi,
Issue #4458 occurs because maybe_do_fake_open() resolves relative paths against the working directory without accounting for dirfd. Opening "<pid>/cmdline" relative to /proc can therefore return QEMU's command line instead of the guest's.
I am handling the original openat/openat2 request before substituting proc contents. It reuses the existing content handlers and preserves the tested resolution restrictions, descriptor flags, and errors,
including O_PATH read rejection.
Validation on commit 2a0ac9c8ac2d:
- 61/61 diagnostic checks passed natively and under qemu-x86_64.
- Both TCG regression tests passed.
readlinkat changes are kept separate.
https://gitlab.com/qemu-project/qemu/-/issues/4458
Regards,
Artemii Mashanov
PATCH 1/1 linux-user: honor dirfd for synthetic proc opens
Opening "<pid>/cmdline" relative to a directory descriptor for /proc can return QEMU's command line instead of the guest's.
Honor dirfd when selecting synthetic proc contents and add regression coverage for openat and openat2.
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/4458
--- linux-user/syscall.c | 321 +++++++++++++++++------ tests/tcg/multiarch/linux/proc-openat.c | 94 +++++++ tests/tcg/multiarch/linux/proc-openat2.c | 83 ++++++ tests/tcg/multiarch/meson.build | 2 + 4 files changed, 418 insertions(+), 82 deletions(-) create mode 100644 tests/tcg/multiarch/linux/proc-openat.c create mode 100644 tests/tcg/multiarch/linux/proc-openat2.c
linux-user/syscall.c +239 -82
@@ -8839,12 +8839,132 @@ static int open_net_route(CPUArchState *cpu_env, int fd) }#endif -static int maybe_do_fake_open (CPUArchState *cpu_env,int dirfd, -const char *fname,int flags,mode_t mode, -int openat2_resolve,bool safe) +static char *proc_entry_path (int dirfd,const char *name) +{ + g_autofree char *base = NULL; + g_autofree char *absolute = NULL; + g_autofree char *parent = NULL; + g_autofree char *resolved = NULL; + g_autofree char *entry = NULL; +char fd_path[64 ]; + +if (!name[0 ]) { +snprintf (fd_path,sizeof (fd_path),"/proc/self/fd/%d" , dirfd); +return g_file_read_link (fd_path,NULL ); + } +if (name[0 ] =='/' ) { + absolute =g_strdup (name); + }else { +if (dirfd ==AT_FDCWD ) { + base =getcwd (NULL ,0 ); + }else { +snprintf (fd_path,sizeof (fd_path),"/proc/self/fd/%d" , dirfd); + base =g_file_read_link (fd_path,NULL ); + } +if (!base) { +return NULL ; + } + absolute =g_build_filename (base, name,NULL ); + } + parent =g_path_get_dirname (absolute); + resolved =realpath (parent,NULL ); +if (!resolved) { +return NULL ; + } + entry =g_path_get_basename (absolute); +return g_build_filename (resolved, entry,NULL ); +} + +static int fake_open_contents (CPUArchState *cpu_env, +int (*fill)(CPUArchState *,int ), +int status_flags,bool safe) +{ + g_autofree char *filename = NULL; +const char *tmpdir =g_get_tmp_dir (); +const char *reader_path; +char fd_path[64 ]; +bool named = status_flags &O_NOFOLLOW ; +int fd, reader =-1 , result =-1 , saved_errno; + +retry: +if (named) { +g_free (filename); + filename =g_build_filename (tmpdir,"qemu-open.XXXXXX" ,NULL ); + fd =g_mkstemp_full (filename,O_RDWR |O_CLOEXEC ,0600 ); +if (fd <0 &&strcmp (tmpdir, P_tmpdir) && + errno !=EMFILE && errno !=ENFILE && + errno !=ENOMEM && errno !=EINTR ) { + tmpdir = P_tmpdir; + goto retry; + } + }else { + fd =memfd_create ("qemu-open" ,MFD_CLOEXEC ); + } +if (fd <0 ) { +if (!named && (errno ==ENOSYS || errno ==EPERM )) { + named = true; + goto retry; + } +return -1 ; + } +if (fill (cpu_env, fd)) { + goto out; + } +if (named) { +int closed =close (fd); + + fd =-1 ; +if (closed <0 ) { + goto out; + } + reader_path = filename; + }else { +snprintf (fd_path,sizeof (fd_path),"/proc/self/fd/%d" , fd); + reader_path = fd_path; + } + reader = safe ?safe_openat (AT_FDCWD , reader_path, status_flags,0 ) : +openat (AT_FDCWD , reader_path, status_flags,0 ); +if (reader <0 ) { +if (!named && errno ==EMFILE ) { +close (fd); + named = true; + goto retry; + } + goto out; + } + result = named ? reader :dup3 (reader, fd, status_flags &O_CLOEXEC ); +if (result >=0 ) { +if (named) { + reader =-1 ; + }else { + fd =-1 ; + } + } +out: + saved_errno = errno; +if (reader >=0 ) { +close (reader); + } +if (fd >=0 ) { +close (fd); + } +if (named) { +unlink (filename); + } + errno = saved_errno; +return result; +} + +static int maybe_do_fake_open (CPUArchState *cpu_env,int opened_fd,int dirfd, +const char *fname, +const struct open_how_ver0 *how, +bool safe) { g_autofree char *proc_name = NULL; +char fd_path[64 ];const char *pathname; +int flags = how->flags; +int original_errno = errno;struct fake_open {const char *filename;int (*fill)(CPUArchState *cpu_env,int fd);
@@ -8869,26 +8989,70 @@ static int maybe_do_fake_open(CPUArchState *cpu_env, int dirfd, { NULL, NULL, NULL } }; -/* if this is a file from /proc/ filesystem, expand full name */ - proc_name =realpath (fname,NULL ); -if (proc_name &&strncmp (proc_name,"/proc/" ,6 ) ==0 ) { - pathname = proc_name; - }else { - pathname = fname; +if (opened_fd >=0 && + ((flags & (O_PATH |O_NOFOLLOW )) == (O_PATH |O_NOFOLLOW ))) { +return opened_fd; + } +if (opened_fd <0 && original_errno !=ENOENT ) { +return opened_fd; } -if (is_proc_myself (pathname,"exe" )) { -/* Honor openat2 resolve flags */ -if ((openat2_resolve &RESOLVE_NO_MAGICLINKS ) || - (openat2_resolve &RESOLVE_NO_SYMLINKS )) { - errno =ELOOP ; -return -1 ; +if (opened_fd >=0 && !(flags &O_NOFOLLOW ) && + !(how->resolve & (RESOLVE_IN_ROOT |RESOLVE_BENEATH | +RESOLVE_NO_SYMLINKS |RESOLVE_NO_MAGICLINKS ))) { +struct stat opened, executable; + +if (fstat (opened_fd, &opened) ==0 && +stat ("/proc/self/exe" , &executable) ==0 && + opened.st_dev == executable.st_dev && + opened.st_ino == executable.st_ino) { + g_autofree char *entry = proc_entry_path(dirfd, fname); + +if (entry &&is_proc_myself (entry,"exe" )) { +close (opened_fd); +if (how->resolve) { +return safe_openat2 (AT_FDCWD , real_exec_path, + how,sizeof (*how)); + } +return safe ?safe_openat (AT_FDCWD , real_exec_path, + flags, how->mode) : +openat (AT_FDCWD , real_exec_path, flags, how->mode); + } } -if (safe) { -return safe_openat (dirfd, real_exec_path, flags, mode); - }else { -return openat (dirfd, real_exec_path, flags, mode); + } + +if (opened_fd <0 ) { + g_autofree char *parent = g_path_get_dirname(fname); + g_autofree char *entry = g_path_get_basename(fname); + g_autofree char *directory = NULL; +struct open_how_ver0 parent_how = { + .flags =O_PATH |O_DIRECTORY |O_CLOEXEC , + .resolve = how->resolve, + }; +int fd; + + fd = how->resolve ? +safe_openat2 (dirfd, parent, &parent_how,sizeof (parent_how)) : + safe ?safe_openat (dirfd, parent, parent_how.flags,0 ) : +openat (dirfd, parent, parent_how.flags,0 ); +if (fd >=0 ) { +snprintf (fd_path,sizeof (fd_path),"/proc/self/fd/%d" , fd); + directory =g_file_read_link (fd_path,NULL ); +close (fd); +if (directory) { + proc_name =g_build_filename (directory, entry,NULL ); + } } + }else { +snprintf (fd_path,sizeof (fd_path),"/proc/self/fd/%d" , opened_fd); + proc_name =g_file_read_link (fd_path,NULL ); + } + pathname = proc_name; + +if ((opened_fd >=0 && (flags &O_PATH )) || + !pathname ||strncmp (pathname,"/proc/" ,6 )) { + errno = original_errno; +return opened_fd; }for (fake_open = fakes; fake_open->filename; fake_open++) {
@@ -8898,60 +9062,52 @@ static int maybe_do_fake_open(CPUArchState *cpu_env, int dirfd, }if (fake_open->filename) { -const char *tmpdir; -char filename[PATH_MAX ]; -int fd, r; - - fd =memfd_create ("qemu-open" ,0 ); -if (fd <0 ) { -if (errno !=ENOSYS ) { -return fd; - } -/* create temporary file to map stat to */ - tmpdir =getenv ("TMPDIR" ); -if (!tmpdir) - tmpdir ="/tmp" ; -snprintf (filename,sizeof (filename),"%s/qemu-open.XXXXXX" , tmpdir); - fd =mkstemp (filename); -if (fd <0 ) { -return fd; - } -unlink (filename); - } - -if ((r = fake_open->fill (cpu_env, fd))) { -int e = errno; -close (fd); - errno = e; -return r; +int saved_errno, status_flags; + + status_flags = flags & ~(O_CREAT |O_EXCL |O_TRUNC ); +if (opened_fd >=0 ) { + status_flags =fcntl (opened_fd,F_GETFL ); + saved_errno = errno; +close (opened_fd); +if (status_flags <0 ) { + errno = saved_errno; +return -1 ; + } + }else if (flags &O_DIRECTORY ) { + errno =ENOTDIR ; +return -1 ; + }else if ((flags & (O_CREAT |O_EXCL )) == (O_CREAT |O_EXCL )) { + errno =EEXIST ; +return -1 ; } -lseek (fd,0 ,SEEK_SET ); -return fd; + status_flags |= flags &O_CLOEXEC ; +return fake_open_contents (cpu_env, fake_open->fill, status_flags, safe); } -return -2 ; + errno = original_errno; +return opened_fd; }int do_guest_openat (CPUArchState *cpu_env,int dirfd,const char *pathname,int flags,mode_t mode,bool safe) { -int fd =maybe_do_fake_open (cpu_env, dirfd, pathname, flags, mode,0 , safe); -if (fd >-2 ) { -return fd; - } +struct open_how_ver0 how = { .flags = flags, .mode = mode }; +const char *host_name =path (pathname); +int fd;if (safe) { -return safe_openat (dirfd,path (pathname), flags, mode); + fd =safe_openat (dirfd, host_name, flags, mode); }else { -return openat (dirfd,path (pathname), flags, mode); + fd =openat (dirfd, host_name, flags, mode); } +return maybe_do_fake_open (cpu_env, fd, dirfd, host_name, &how, + safe); } -static int do_openat2 (CPUArchState *cpu_env,abi_long dirfd, -abi_ptr guest_pathname,abi_ptr guest_open_how, -abi_ulong guest_size) +abi_ptr guest_pathname,abi_ptr guest_open_how, +abi_ulong guest_size) {struct open_how_ver0 how = {0 };char *pathname;
@@ -8981,23 +9137,19 @@ static int do_openat2(CPUArchState *cpu_env, abi_long dirfd, how.flags =target_to_host_bitmask (tswap64 (how.flags), fcntl_flags_tbl); how.mode =tswap64 (how.mode); how.resolve =tswap64 (how.resolve); -int fd =maybe_do_fake_open (cpu_env, dirfd, pathname, how.flags, how.mode, - how.resolve, true); -if (fd >-2 ) { - ret =get_errno (fd); - }else { -const char *host_pathname = pathname; -if (pathname[0 ] =='/' && - !(how.resolve & (RESOLVE_IN_ROOT |RESOLVE_BENEATH ))) { -/* -* RESOLVE_BENEATH rejects absolute paths; RESOLVE_IN_ROOT -* resolves them relative to dirfd. -*/ - host_pathname =path (pathname); - } - ret =get_errno (safe_openat2 (dirfd, host_pathname, &how, -sizeof (struct open_how_ver0 ))); +const char *host_pathname = pathname; +if (pathname[0 ] =='/' && + !(how.resolve & (RESOLVE_IN_ROOT |RESOLVE_BENEATH ))) { +/* +* RESOLVE_BENEATH rejects absolute paths; RESOLVE_IN_ROOT +* resolves them relative to dirfd. +*/ + host_pathname =path (pathname); } +int fd =safe_openat2 (dirfd, host_pathname, &how, +sizeof (struct open_how_ver0 )); + ret =get_errno (maybe_do_fake_open (cpu_env, fd, dirfd, host_pathname, &how, + true));fd_trans_unregister (ret);unlock_user (pathname, guest_pathname,0 );
@@ -9034,6 +9186,19 @@ ssize_t do_guest_readlink(const char *pathname, char *buf, size_t bufsiz)return ret; } +static ssize_t do_guest_readlinkat (int dirfd,const char *pathname, +char *buf,size_t bufsiz) +{ +const char *host_name =path (pathname); + g_autofree char *entry_path = proc_entry_path(dirfd, host_name); +ssize_t ret =readlinkat (dirfd, host_name, buf, bufsiz); + +if (ret >=0 && entry_path &&is_proc_myself (entry_path,"exe" )) { +return do_guest_readlink (entry_path, buf, bufsiz); + } +return ret; +} +static int do_execv (CPUArchState *cpu_env,int dirfd,abi_long pathname,abi_long guest_argp,abi_long guest_envp,int flags,bool is_execveat)
@@ -11143,16 +11308,8 @@ static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1, }else if (!arg4) {/* Short circuit this for the magic exe check. */ ret = -TARGET_EINVAL ; - }else if (is_proc_myself ((const char *)p,"exe" )) { -/* -* Don't worry about sign mismatch as earlier mapping -* logic would have thrown a bad address error. -*/ - ret =MIN (strlen (real_exec_path), arg4); -/* We cannot NUL terminate the string. */ -memcpy (p2, real_exec_path, ret); }else { - ret =get_errno (readlinkat (arg1,path (p), p2, arg4)); + ret =get_errno (do_guest_readlinkat (arg1, p, p2, arg4)); }unlock_user (p2, arg3, ret);unlock_user (p, arg2,0 );
tests/tcg/multiarch/linux/proc-openat.c new file +94 -0
94 lines not on this page · raw message
tests/tcg/multiarch/linux/proc-openat2.c new file +83 -0
83 lines not on this page · raw message
tests/tcg/multiarch/meson.build +2 -0
@@ -61,6 +61,8 @@ tests += { multiarch/'linux/linux-shmat-null.c': {}, multiarch/'linux/linux-sigrtminmax.c': {}, multiarch/'linux/linux-test.c': {}, + multiarch/'linux/proc-openat.c': {}, + multiarch/'linux/proc-openat2.c': {}, multiarch/'linux/test-vma.c': {}, }
Signature · 1 line
2.55.0
Re: [PATCH] linux-user: honor dirfd for synthetic proc opens
Hi,
Ping on this patch. Could someone please take a look when time permits?
Please let me know if any changes or additional testing are needed.
Thanks,
Artemii
вс, 25 окт. 2026 г. в 18:23, Artemii Mashanov <[email protected]>:
Quoted 615 lines
Opening "<pid>/cmdline" relative to a directory descriptor for /proc can return QEMU's command line instead of the guest's.
Honor dirfd when selecting synthetic proc contents and add regression coverage for openat and openat2.
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/4458
Signed-off-by Artemii Mashanov <[email protected]>--- linux-user/syscall.c | 321 +++++++++++++++++------ tests/tcg/multiarch/linux/proc-openat.c | 94 +++++++ tests/tcg/multiarch/linux/proc-openat2.c | 83 ++++++ tests/tcg/multiarch/meson.build | 2 + 4 files changed, 418 insertions(+), 82 deletions(-) create mode 100644 tests/tcg/multiarch/linux/proc-openat.c create mode 100644 tests/tcg/multiarch/linux/proc-openat2.clinux-user/syscall.c +239
-82@@ -8839,12 +8839,132 @@ static int open_net_route(CPUArchState *cpu_env, int fd) }#endif -static int maybe_do_fake_open (CPUArchState *cpu_env,int dirfd, -const char *fname,int flags,mode_t mode, -int openat2_resolve,bool safe) +static char *proc_entry_path (int dirfd,const char *name) +{ + g_autofree char *base = NULL; + g_autofree char *absolute = NULL; + g_autofree char *parent = NULL; + g_autofree char *resolved = NULL; + g_autofree char *entry = NULL; +char fd_path[64 ]; + +if (!name[0 ]) { +snprintf (fd_path,sizeof (fd_path),"/proc/self/fd/%d" , dirfd); +return g_file_read_link (fd_path,NULL ); + } +if (name[0 ] =='/' ) { + absolute =g_strdup (name); + }else { +if (dirfd ==AT_FDCWD ) { + base =getcwd (NULL ,0 ); + }else { +snprintf (fd_path,sizeof (fd_path),"/proc/self/fd/%d" , dirfd); + base =g_file_read_link (fd_path,NULL ); + } +if (!base) { +return NULL ; + } + absolute =g_build_filename (base, name,NULL ); + } + parent =g_path_get_dirname (absolute); + resolved =realpath (parent,NULL ); +if (!resolved) { +return NULL ; + } + entry =g_path_get_basename (absolute); +return g_build_filename (resolved, entry,NULL ); +} + +static int fake_open_contents (CPUArchState *cpu_env, +int (*fill)(CPUArchState *,int ), +int status_flags,bool safe) +{ + g_autofree char *filename = NULL; +const char *tmpdir =g_get_tmp_dir (); +const char *reader_path; +char fd_path[64 ]; +bool named = status_flags &O_NOFOLLOW ; +int fd, reader =-1 , result =-1 , saved_errno; + +retry: +if (named) { +g_free (filename); + filename =g_build_filename (tmpdir,"qemu-open.XXXXXX" ,NULL ); + fd =g_mkstemp_full (filename,O_RDWR |O_CLOEXEC ,0600 ); +if (fd <0 &&strcmp (tmpdir, P_tmpdir) && + errno !=EMFILE && errno !=ENFILE && + errno !=ENOMEM && errno !=EINTR ) { + tmpdir = P_tmpdir; + goto retry; + } + }else { + fd =memfd_create ("qemu-open" ,MFD_CLOEXEC ); + } +if (fd <0 ) { +if (!named && (errno ==ENOSYS || errno ==EPERM )) { + named = true; + goto retry; + } +return -1 ; + } +if (fill (cpu_env, fd)) { + goto out; + } +if (named) { +int closed =close (fd); + + fd =-1 ; +if (closed <0 ) { + goto out; + } + reader_path = filename; + }else { +snprintf (fd_path,sizeof (fd_path),"/proc/self/fd/%d" , fd); + reader_path = fd_path; + } + reader = safe ?safe_openat (AT_FDCWD , reader_path, status_flags,0 ) : +openat (AT_FDCWD , reader_path, status_flags,0 ); +if (reader <0 ) { +if (!named && errno ==EMFILE ) { +close (fd); + named = true; + goto retry; + } + goto out; + } + result = named ? reader :dup3 (reader, fd, status_flags &O_CLOEXEC ); +if (result >=0 ) { +if (named) { + reader =-1 ; + }else { + fd =-1 ; + } + } +out: + saved_errno = errno; +if (reader >=0 ) { +close (reader); + } +if (fd >=0 ) { +close (fd); + } +if (named) { +unlink (filename); + } + errno = saved_errno; +return result; +} + +static int maybe_do_fake_open (CPUArchState *cpu_env,int opened_fd,int dirfd, +const char *fname, +const struct open_how_ver0 *how, +bool safe) { g_autofree char *proc_name = NULL; +char fd_path[64 ];const char *pathname; +int flags = how->flags; +int original_errno = errno;struct fake_open {const char *filename;int (*fill)(CPUArchState *cpu_env,int fd);@@ -8869,26 +8989,70 @@ static int maybe_do_fake_open(CPUArchState *cpu_env, int dirfd, { NULL, NULL, NULL } }; -/* if this is a file from /proc/ filesystem, expand full name */ - proc_name =realpath (fname,NULL ); -if (proc_name &&strncmp (proc_name,"/proc/" ,6 ) ==0 ) { - pathname = proc_name; - }else { - pathname = fname; +if (opened_fd >=0 && + ((flags & (O_PATH |O_NOFOLLOW )) == (O_PATH |O_NOFOLLOW ))) { +return opened_fd; + } +if (opened_fd <0 && original_errno !=ENOENT ) { +return opened_fd; } -if (is_proc_myself (pathname,"exe" )) { -/* Honor openat2 resolve flags */ -if ((openat2_resolve &RESOLVE_NO_MAGICLINKS ) || - (openat2_resolve &RESOLVE_NO_SYMLINKS )) { - errno =ELOOP ; -return -1 ; +if (opened_fd >=0 && !(flags &O_NOFOLLOW ) && + !(how->resolve & (RESOLVE_IN_ROOT |RESOLVE_BENEATH | +RESOLVE_NO_SYMLINKS |RESOLVE_NO_MAGICLINKS ))) { +struct stat opened, executable; + +if (fstat (opened_fd, &opened) ==0 && +stat ("/proc/self/exe" , &executable) ==0 && + opened.st_dev == executable.st_dev && + opened.st_ino == executable.st_ino) { + g_autofree char *entry = proc_entry_path(dirfd, fname); + +if (entry &&is_proc_myself (entry,"exe" )) { +close (opened_fd); +if (how->resolve) { +return safe_openat2 (AT_FDCWD , real_exec_path, + how,sizeof (*how)); + } +return safe ?safe_openat (AT_FDCWD , real_exec_path, + flags, how->mode) : +openat (AT_FDCWD , real_exec_path, flags, how->mode); + } } -if (safe) { -return safe_openat (dirfd, real_exec_path, flags, mode); - }else { -return openat (dirfd, real_exec_path, flags, mode); + } + +if (opened_fd <0 ) { + g_autofree char *parent = g_path_get_dirname(fname); + g_autofree char *entry = g_path_get_basename(fname); + g_autofree char *directory = NULL; +struct open_how_ver0 parent_how = { + .flags =O_PATH |O_DIRECTORY |O_CLOEXEC , + .resolve = how->resolve, + }; +int fd; + + fd = how->resolve ? +safe_openat2 (dirfd, parent, &parent_how,sizeof (parent_how)) : + safe ?safe_openat (dirfd, parent, parent_how.flags,0 ) : +openat (dirfd, parent, parent_how.flags,0 ); +if (fd >=0 ) { +snprintf (fd_path,sizeof (fd_path),"/proc/self/fd/%d" , fd); + directory =g_file_read_link (fd_path,NULL ); +close (fd); +if (directory) { + proc_name =g_build_filename (directory, entry,NULL ); + } } + }else { +snprintf (fd_path,sizeof (fd_path),"/proc/self/fd/%d" , opened_fd); + proc_name =g_file_read_link (fd_path,NULL ); + } + pathname = proc_name; + +if ((opened_fd >=0 && (flags &O_PATH )) || + !pathname ||strncmp (pathname,"/proc/" ,6 )) { + errno = original_errno; +return opened_fd; }for (fake_open = fakes; fake_open->filename; fake_open++) {@@ -8898,60 +9062,52 @@ static int maybe_do_fake_open(CPUArchState *cpu_env, int dirfd, }if (fake_open->filename) { -const char *tmpdir; -char filename[PATH_MAX ]; -int fd, r; - - fd =memfd_create ("qemu-open" ,0 ); -if (fd <0 ) { -if (errno !=ENOSYS ) { -return fd; - } -/* create temporary file to map stat to */ - tmpdir =getenv ("TMPDIR" ); -if (!tmpdir) - tmpdir ="/tmp" ; -snprintf (filename,sizeof (filename),"%s/qemu-open.XXXXXX" , tmpdir); - fd =mkstemp (filename); -if (fd <0 ) { -return fd; - } -unlink (filename); - } - -if ((r = fake_open->fill (cpu_env, fd))) { -int e = errno; -close (fd); - errno = e; -return r; +int saved_errno, status_flags; + + status_flags = flags & ~(O_CREAT |O_EXCL |O_TRUNC ); +if (opened_fd >=0 ) { + status_flags =fcntl (opened_fd,F_GETFL ); + saved_errno = errno; +close (opened_fd); +if (status_flags <0 ) { + errno = saved_errno; +return -1 ; + } + }else if (flags &O_DIRECTORY ) { + errno =ENOTDIR ; +return -1 ; + }else if ((flags & (O_CREAT |O_EXCL )) == (O_CREAT |O_EXCL )) { + errno =EEXIST ; +return -1 ; } -lseek (fd,0 ,SEEK_SET ); -return fd; + status_flags |= flags &O_CLOEXEC ; +return fake_open_contents (cpu_env, fake_open->fill, status_flags, safe); } -return -2 ; + errno = original_errno; +return opened_fd; }int do_guest_openat (CPUArchState *cpu_env,int dirfd,const char *pathname,int flags,mode_t mode,bool safe) { -int fd =maybe_do_fake_open (cpu_env, dirfd, pathname, flags, mode,0 , safe); -if (fd >-2 ) { -return fd; - } +struct open_how_ver0 how = { .flags = flags, .mode = mode }; +const char *host_name =path (pathname); +int fd;if (safe) { -return safe_openat (dirfd,path (pathname), flags, mode); + fd =safe_openat (dirfd, host_name, flags, mode); }else { -return openat (dirfd,path (pathname), flags, mode); + fd =openat (dirfd, host_name, flags, mode); } +return maybe_do_fake_open (cpu_env, fd, dirfd, host_name, &how, + safe); } -static int do_openat2 (CPUArchState *cpu_env,abi_long dirfd, -abi_ptr guest_pathname,abi_ptr guest_open_how, -abi_ulong guest_size) +abi_ptr guest_pathname,abi_ptr guest_open_how, +abi_ulong guest_size) {struct open_how_ver0 how = {0 };char *pathname;@@ -8981,23 +9137,19 @@ static int do_openat2(CPUArchState *cpu_env, abi_long dirfd, how.flags =target_to_host_bitmask (tswap64 (how.flags), fcntl_flags_tbl); how.mode =tswap64 (how.mode); how.resolve =tswap64 (how.resolve); -int fd =maybe_do_fake_open (cpu_env, dirfd, pathname, how.flags, how.mode, - how.resolve, true); -if (fd >-2 ) { - ret =get_errno (fd); - }else { -const char *host_pathname = pathname; -if (pathname[0 ] =='/' && - !(how.resolve & (RESOLVE_IN_ROOT |RESOLVE_BENEATH ))) { -/* -* RESOLVE_BENEATH rejects absolute paths; RESOLVE_IN_ROOT -* resolves them relative to dirfd. -*/ - host_pathname =path (pathname); - } - ret =get_errno (safe_openat2 (dirfd, host_pathname, &how, -sizeof (struct open_how_ver0 ))); +const char *host_pathname = pathname; +if (pathname[0 ] =='/' && + !(how.resolve & (RESOLVE_IN_ROOT |RESOLVE_BENEATH ))) { +/* +* RESOLVE_BENEATH rejects absolute paths; RESOLVE_IN_ROOT +* resolves them relative to dirfd. +*/ + host_pathname =path (pathname); } +int fd =safe_openat2 (dirfd, host_pathname, &how, +sizeof (struct open_how_ver0 )); + ret =get_errno (maybe_do_fake_open (cpu_env, fd, dirfd, host_pathname, &how, + true));fd_trans_unregister (ret);unlock_user (pathname, guest_pathname,0 );@@ -9034,6 +9186,19 @@ ssize_t do_guest_readlink(const char *pathname, char *buf, size_t bufsiz)return ret; } +static ssize_t do_guest_readlinkat (int dirfd,const char *pathname, +char *buf,size_t bufsiz) +{ +const char *host_name =path (pathname); + g_autofree char *entry_path = proc_entry_path(dirfd, host_name); +ssize_t ret =readlinkat (dirfd, host_name, buf, bufsiz); + +if (ret >=0 && entry_path &&is_proc_myself (entry_path,"exe" )) { +return do_guest_readlink (entry_path, buf, bufsiz); + } +return ret; +} +static int do_execv (CPUArchState *cpu_env,int dirfd,abi_long pathname,abi_long guest_argp,abi_long guest_envp,int flags,bool is_execveat)@@ -11143,16 +11308,8 @@ static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1, }else if (!arg4) {/* Short circuit this for the magic exe check. */ ret = -TARGET_EINVAL ; - }else if (is_proc_myself ((const char *)p,"exe" )) { -/* -* Don't worry about sign mismatch as earlier mapping -* logic would have thrown a bad address error. -*/ - ret =MIN (strlen (real_exec_path), arg4); -/* We cannot NUL terminate the string. */ -memcpy (p2, real_exec_path, ret); }else { - ret =get_errno (readlinkat (arg1,path (p), p2, arg4)); + ret =get_errno (do_guest_readlinkat (arg1, p, p2, arg4)); }unlock_user (p2, arg3, ret);unlock_user (p, arg2,0 );tests/tcg/multiarch/linux/proc-openat.c new file +94
-0@@ -0,0 +1,94 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +#define _GNU_SOURCE +#include <assert.h> +#include <errno.h> +#include <fcntl.h> +#include <stdlib.h> +#include <string.h> +#include <sys/resource.h> +#include <unistd.h> + +static char expected[16384 ]; +static size_t expected_size; + +static void check (int dirfd,const char *name,int flags,int expected_errno) +{ +char actual[sizeof (expected)]; +size_t size =0 ; +int fd =openat (dirfd, name, flags); + +if (expected_errno) { +assert (fd ==-1 && errno == expected_errno); +return ; + } +assert (fd >=0 ); +assert ((fcntl (fd,F_GETFD ) &FD_CLOEXEC ) == + ((flags &O_CLOEXEC ) ?FD_CLOEXEC :0 )); +assert ((fcntl (fd,F_GETFL ) & (O_PATH |O_NOFOLLOW |O_ACCMODE )) == + (flags & (O_PATH |O_NOFOLLOW |O_ACCMODE ))); +if (flags &O_PATH ) { +assert (read (fd, actual,1 ) ==-1 && errno ==EBADF ); + }else { +for (;;) { +ssize_t n =read (fd, actual + size,sizeof (actual) - size); + +if (n <0 && errno ==EINTR ) { +continue ; + } +assert (n >=0 ); +if (!n) { +break ; + } + size += n; +assert (size <sizeof (actual)); + } +assert (size == expected_size && !memcmp (actual, expected, size)); +assert (write (fd,"x" ,1 ) ==-1 && errno ==EBADF ); + } +assert (close (fd) ==0 ); +} + +int main (int argc,char **argv) +{ +struct rlimit saved, limit; +int cwd =open ("." ,O_PATH |O_DIRECTORY |O_CLOEXEC ); +int proc =open ("/proc" ,O_PATH |O_DIRECTORY |O_CLOEXEC ); +int self =open ("/proc/self" ,O_PATH |O_DIRECTORY |O_CLOEXEC ); +int flags =O_RDONLY |O_CLOEXEC ; + +assert (cwd >=0 && proc >=0 && self >=0 ); +for (int i =0 ; i < argc; i++) { +size_t size =strlen (argv[i]) +1 ; + +assert (size <=sizeof (expected) - expected_size); +memcpy (expected + expected_size, argv[i], size); + expected_size += size; + } +check (AT_FDCWD ,"/proc/self/cmdline" , flags,0 ); +check (-1 ,"/proc/self/cmdline" , flags,0 ); +check (proc,"self/cmdline" , flags,0 ); +check (proc,"./self/cmdline" , flags,0 ); +check (self,"cmdline" , flags,0 ); +check (self,"cmdline" , flags |O_NOFOLLOW ,0 ); +check (self,"cmdline" ,O_PATH |O_CLOEXEC ,0 ); +check (self,"cmdline" , flags |O_DIRECTORY ,ENOTDIR ); +assert (chdir ("/proc" ) ==0 ); +check (AT_FDCWD ,"self/cmdline" , flags,0 ); +check (-1 ,"self/cmdline" , flags,EBADF ); +assert (fchdir (cwd) ==0 ); +assert (close (self) ==0 ); +assert (close (proc) ==0 ); +assert (close (cwd) ==0 ); +assert (getrlimit (RLIMIT_NOFILE , &saved) ==0 ); +int next =open ("/dev/null" ,O_RDONLY |O_CLOEXEC ); + +assert (next >=0 ); + limit = saved; + limit.rlim_cur = next +1 ; +assert (setrlimit (RLIMIT_NOFILE , &limit) ==0 ); +assert (close (next) ==0 ); +check (AT_FDCWD ,"/proc/self/cmdline" , flags,0 ); +check (AT_FDCWD ,"/proc/self/cmdline" , flags |O_NOFOLLOW ,0 ); +assert (setrlimit (RLIMIT_NOFILE , &saved) ==0 ); +return EXIT_SUCCESS ; +}tests/tcg/multiarch/linux/proc-openat2.c new file +83
-0@@ -0,0 +1,83 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +#define _GNU_SOURCE +#include <assert.h> +#include <errno.h> +#include <fcntl.h> +#include <stdint.h> +#include <stdlib.h> +#include <sys/syscall.h> +#include <unistd.h> + +#ifdef SYS_openat2 +#include <linux/openat2.h> + +static int cmdline_argc; +static char **cmdline_argv; + +static void check (int dirfd,const char *name,uint64_t flags, +uint64_t resolve,int expected_errno) +{ +struct open_how how = { .flags = flags, .resolve = resolve }; +char byte; +int fd =syscall (SYS_openat2, dirfd, name, &how,sizeof (how)); + +if (expected_errno) { +assert (fd ==-1 && errno == expected_errno); +return ; + } +assert (fd >=0 ); +assert (fcntl (fd,F_GETFD ) ==FD_CLOEXEC ); +if (flags &O_PATH ) { +assert (fcntl (fd,F_GETFL ) &O_PATH ); +assert (read (fd, &byte,1 ) ==-1 && errno ==EBADF ); + }else { +for (int i =0 ; i < cmdline_argc; i++) { +const char *p = cmdline_argv[i]; + +do { +assert (read (fd, &byte,1 ) ==1 && byte == *p); + }while (*p++); + } +assert (read (fd, &byte,1 ) ==0 ); +assert (write (fd,"x" ,1 ) ==-1 && errno ==EBADF ); + } +assert (close (fd) ==0 ); +} +#endif + +int main (int argc,char **argv) +{ +#ifdef SYS_openat2 +struct open_how how = { .flags =O_RDONLY |O_CLOEXEC }; +int fd =syscall (SYS_openat2,AT_FDCWD ,"/proc/self/cmdline" , + &how,sizeof (how)); +int proc, self, root, flags =O_RDONLY |O_CLOEXEC ; + + cmdline_argc = argc; + cmdline_argv = argv; +if (fd <0 && errno ==ENOSYS ) { +return EXIT_SUCCESS ; + } +assert (fd >=0 &&close (fd) ==0 ); + proc =open ("/proc" ,O_PATH |O_DIRECTORY |O_CLOEXEC ); + self =open ("/proc/self" ,O_PATH |O_DIRECTORY |O_CLOEXEC ); + root =open ("/" ,O_PATH |O_DIRECTORY |O_CLOEXEC ); +assert (proc >=0 && self >=0 && root >=0 ); +check (-1 ,"/proc/self/cmdline" , flags,0 ,0 ); +check (AT_FDCWD ,"/proc/self/cmdline" , flags, +RESOLVE_NO_SYMLINKS ,ELOOP ); +check (proc,"self/cmdline" , flags,0 ,0 ); +check (proc,"self/cmdline" , flags,RESOLVE_NO_SYMLINKS ,ELOOP ); +check (proc,"self/root/proc/self/cmdline" , flags, +RESOLVE_NO_MAGICLINKS ,ELOOP ); +check (self,"../self/cmdline" , flags,RESOLVE_BENEATH ,EXDEV ); +check (self,"../self/cmdline" , flags,RESOLVE_IN_ROOT ,ENOENT ); +check (root,"proc/self/cmdline" , flags,RESOLVE_NO_XDEV ,EXDEV ); +check (proc,"self/cmdline" , flags,UINT64_C (1 ) <<63 ,EINVAL ); +check (self,"cmdline" ,O_PATH |O_CLOEXEC ,0 ,0 ); +assert (close (root) ==0 ); +assert (close (self) ==0 ); +assert (close (proc) ==0 ); +#endif +return EXIT_SUCCESS ; +}tests/tcg/multiarch/meson.build +2
-1@@ -61,6 +61,8 @@ tests += { multiarch/'linux/linux-shmat-null.c': {}, multiarch/'linux/linux-sigrtminmax.c': {}, multiarch/'linux/linux-test.c': {}, + multiarch/'linux/proc-openat.c': {}, + multiarch/'linux/proc-openat2.c': {}, multiarch/'linux/test-vma.c': {}, } --2.55.0
Re: [PATCH] linux-user: honor dirfd for synthetic proc opens
Quoted 5 lines · Artemii Mashanov
Hi,
Ping on this patch. Could someone please take a look when time permits?
Please let me know if any changes or additional testing are needed.
I've queued this patch up for qemu v12.0.
Overall I'm not happy how we handle the relative paths when running outside of a chroot. I think this something we should take a closer look during the next release cycle.
Thanks!
Helge
Quoted 619 lines
Thanks,
Artemiiвс, 25 окт. 2026 г. в 18:23, Artemii Mashanov <[email protected]>:
Quoted 615 lines
Opening "<pid>/cmdline" relative to a directory descriptor for /proc can return QEMU's command line instead of the guest's.
Honor dirfd when selecting synthetic proc contents and add regression coverage for openat and openat2.
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/4458
Signed-off-by Artemii Mashanov <[email protected]>--- linux-user/syscall.c | 321 +++++++++++++++++------ tests/tcg/multiarch/linux/proc-openat.c | 94 +++++++ tests/tcg/multiarch/linux/proc-openat2.c | 83 ++++++ tests/tcg/multiarch/meson.build | 2 + 4 files changed, 418 insertions(+), 82 deletions(-)create mode 100644
tests/tcg/multiarch/linux/proc-openat.ccreate mode 100644tests/tcg/multiarch/linux/proc-openat2.clinux-user/syscall.c +239
-82@@ -8839,12 +8839,132 @@ static int open_net_route(CPUArchState *cpu_env, int fd) }#endif -static int maybe_do_fake_open (CPUArchState *cpu_env,int dirfd, -const char *fname,int flags,mode_t mode, -int openat2_resolve,bool safe) +static char *proc_entry_path (int dirfd,const char *name) +{ + g_autofree char *base = NULL; + g_autofree char *absolute = NULL; + g_autofree char *parent = NULL; + g_autofree char *resolved = NULL; + g_autofree char *entry = NULL; +char fd_path[64 ]; + +if (!name[0 ]) { +snprintf (fd_path,sizeof (fd_path),"/proc/self/fd/%d" , dirfd); +return g_file_read_link (fd_path,NULL ); + } +if (name[0 ] =='/' ) { + absolute =g_strdup (name); + }else { +if (dirfd ==AT_FDCWD ) { + base =getcwd (NULL ,0 ); + }else { +snprintf (fd_path,sizeof (fd_path),"/proc/self/fd/%d" , dirfd); + base =g_file_read_link (fd_path,NULL ); + } +if (!base) { +return NULL ; + } + absolute =g_build_filename (base, name,NULL ); + } + parent =g_path_get_dirname (absolute); + resolved =realpath (parent,NULL ); +if (!resolved) { +return NULL ; + } + entry =g_path_get_basename (absolute); +return g_build_filename (resolved, entry,NULL ); +} + +static int fake_open_contents (CPUArchState *cpu_env, +int (*fill)(CPUArchState *,int ), +int status_flags,bool safe) +{ + g_autofree char *filename = NULL; +const char *tmpdir =g_get_tmp_dir (); +const char *reader_path; +char fd_path[64 ]; +bool named = status_flags &O_NOFOLLOW ; +int fd, reader =-1 , result =-1 , saved_errno; + +retry: +if (named) { +g_free (filename); + filename =g_build_filename (tmpdir,"qemu-open.XXXXXX" ,NULL ); + fd =g_mkstemp_full (filename,O_RDWR |O_CLOEXEC ,0600 ); +if (fd <0 &&strcmp (tmpdir, P_tmpdir) && + errno !=EMFILE && errno !=ENFILE && + errno !=ENOMEM && errno !=EINTR ) { + tmpdir = P_tmpdir; + goto retry; + } + }else { + fd =memfd_create ("qemu-open" ,MFD_CLOEXEC ); + } +if (fd <0 ) { +if (!named && (errno ==ENOSYS || errno ==EPERM )) { + named = true; + goto retry; + } +return -1 ; + } +if (fill (cpu_env, fd)) { + goto out; + } +if (named) { +int closed =close (fd); + + fd =-1 ; +if (closed <0 ) { + goto out; + } + reader_path = filename; + }else { +snprintf (fd_path,sizeof (fd_path),"/proc/self/fd/%d" , fd); + reader_path = fd_path; + } + reader = safe ?safe_openat (AT_FDCWD , reader_path, status_flags,0 ) : +openat (AT_FDCWD , reader_path, status_flags,0 ); +if (reader <0 ) { +if (!named && errno ==EMFILE ) { +close (fd); + named = true; + goto retry; + } + goto out; + } + result = named ? reader :dup3 (reader, fd, status_flags &O_CLOEXEC ); +if (result >=0 ) { +if (named) { + reader =-1 ; + }else { + fd =-1 ; + } + } +out: + saved_errno = errno; +if (reader >=0 ) { +close (reader); + } +if (fd >=0 ) { +close (fd); + } +if (named) { +unlink (filename); + } + errno = saved_errno; +return result; +} + +static int maybe_do_fake_open (CPUArchState *cpu_env,int opened_fd,int dirfd, +const char *fname, +const struct open_how_ver0 *how, +bool safe) { g_autofree char *proc_name = NULL; +char fd_path[64 ];const char *pathname; +int flags = how->flags; +int original_errno = errno;struct fake_open {const char *filename;int (*fill)(CPUArchState *cpu_env,int fd);@@ -8869,26 +8989,70 @@ static int maybe_do_fake_open(CPUArchState *cpu_env, int dirfd, { NULL, NULL, NULL } }; -/* if this is a file from /proc/ filesystem, expand full name */ - proc_name =realpath (fname,NULL ); -if (proc_name &&strncmp (proc_name,"/proc/" ,6 ) ==0 ) { - pathname = proc_name; - }else { - pathname = fname; +if (opened_fd >=0 && + ((flags & (O_PATH |O_NOFOLLOW )) == (O_PATH |O_NOFOLLOW ))) { +return opened_fd; + } +if (opened_fd <0 && original_errno !=ENOENT ) { +return opened_fd; } -if (is_proc_myself (pathname,"exe" )) { -/* Honor openat2 resolve flags */ -if ((openat2_resolve &RESOLVE_NO_MAGICLINKS ) || - (openat2_resolve &RESOLVE_NO_SYMLINKS )) { - errno =ELOOP ; -return -1 ; +if (opened_fd >=0 && !(flags &O_NOFOLLOW ) && + !(how->resolve & (RESOLVE_IN_ROOT |RESOLVE_BENEATH | +RESOLVE_NO_SYMLINKS |RESOLVE_NO_MAGICLINKS ))) { +struct stat opened, executable; + +if (fstat (opened_fd, &opened) ==0 && +stat ("/proc/self/exe" , &executable) ==0 && + opened.st_dev == executable.st_dev && + opened.st_ino == executable.st_ino) { + g_autofree char *entry = proc_entry_path(dirfd, fname); + +if (entry &&is_proc_myself (entry,"exe" )) { +close (opened_fd); +if (how->resolve) { +return safe_openat2 (AT_FDCWD , real_exec_path, + how,sizeof (*how)); + } +return safe ?safe_openat (AT_FDCWD , real_exec_path, + flags, how->mode) : +openat (AT_FDCWD , real_exec_path, flags, how->mode); + } } -if (safe) { -return safe_openat (dirfd, real_exec_path, flags, mode); - }else { -return openat (dirfd, real_exec_path, flags, mode); + } + +if (opened_fd <0 ) { + g_autofree char *parent = g_path_get_dirname(fname); + g_autofree char *entry = g_path_get_basename(fname); + g_autofree char *directory = NULL; +struct open_how_ver0 parent_how = { + .flags =O_PATH |O_DIRECTORY |O_CLOEXEC , + .resolve = how->resolve, + }; +int fd; + + fd = how->resolve ? +safe_openat2 (dirfd, parent, &parent_how,sizeof (parent_how)) : + safe ?safe_openat (dirfd, parent, parent_how.flags,0 ) : +openat (dirfd, parent, parent_how.flags,0 ); +if (fd >=0 ) { +snprintf (fd_path,sizeof (fd_path),"/proc/self/fd/%d" , fd); + directory =g_file_read_link (fd_path,NULL ); +close (fd); +if (directory) { + proc_name =g_build_filename (directory, entry,NULL ); + } } + }else { +snprintf (fd_path,sizeof (fd_path),"/proc/self/fd/%d" , opened_fd); + proc_name =g_file_read_link (fd_path,NULL ); + } + pathname = proc_name; + +if ((opened_fd >=0 && (flags &O_PATH )) || + !pathname ||strncmp (pathname,"/proc/" ,6 )) { + errno = original_errno; +return opened_fd; }for (fake_open = fakes; fake_open->filename; fake_open++) {@@ -8898,60 +9062,52 @@ static int maybe_do_fake_open(CPUArchState *cpu_env, int dirfd, }if (fake_open->filename) { -const char *tmpdir; -char filename[PATH_MAX ]; -int fd, r; - - fd =memfd_create ("qemu-open" ,0 ); -if (fd <0 ) { -if (errno !=ENOSYS ) { -return fd; - } -/* create temporary file to map stat to */ - tmpdir =getenv ("TMPDIR" ); -if (!tmpdir) - tmpdir ="/tmp" ; -snprintf (filename,sizeof (filename),"%s/qemu-open.XXXXXX" , tmpdir); - fd =mkstemp (filename); -if (fd <0 ) { -return fd; - } -unlink (filename); - } - -if ((r = fake_open->fill (cpu_env, fd))) { -int e = errno; -close (fd); - errno = e; -return r; +int saved_errno, status_flags; + + status_flags = flags & ~(O_CREAT |O_EXCL |O_TRUNC ); +if (opened_fd >=0 ) { + status_flags =fcntl (opened_fd,F_GETFL ); + saved_errno = errno; +close (opened_fd); +if (status_flags <0 ) { + errno = saved_errno; +return -1 ; + } + }else if (flags &O_DIRECTORY ) { + errno =ENOTDIR ; +return -1 ; + }else if ((flags & (O_CREAT |O_EXCL )) == (O_CREAT |O_EXCL )) { + errno =EEXIST ; +return -1 ; } -lseek (fd,0 ,SEEK_SET ); -return fd; + status_flags |= flags &O_CLOEXEC ; +return fake_open_contents (cpu_env, fake_open->fill, status_flags, safe); } -return -2 ; + errno = original_errno; +return opened_fd; }int do_guest_openat (CPUArchState *cpu_env,int dirfd,const char *pathname,int flags,mode_t mode,bool safe) { -int fd =maybe_do_fake_open (cpu_env, dirfd, pathname, flags, mode,0 , safe); -if (fd >-2 ) { -return fd; - } +struct open_how_ver0 how = { .flags = flags, .mode = mode }; +const char *host_name =path (pathname); +int fd;if (safe) { -return safe_openat (dirfd,path (pathname), flags, mode); + fd =safe_openat (dirfd, host_name, flags, mode); }else { -return openat (dirfd,path (pathname), flags, mode); + fd =openat (dirfd, host_name, flags, mode); } +return maybe_do_fake_open (cpu_env, fd, dirfd, host_name, &how, + safe); } -static int do_openat2 (CPUArchState *cpu_env,abi_long dirfd, -abi_ptr guest_pathname,abi_ptr guest_open_how, -abi_ulong guest_size) +abi_ptr guest_pathname,abi_ptr guest_open_how, +abi_ulong guest_size) {struct open_how_ver0 how = {0 };char *pathname;@@ -8981,23 +9137,19 @@ static int do_openat2(CPUArchState *cpu_env, abi_long dirfd, how.flags =target_to_host_bitmask (tswap64 (how.flags), fcntl_flags_tbl); how.mode =tswap64 (how.mode); how.resolve =tswap64 (how.resolve); -int fd =maybe_do_fake_open (cpu_env, dirfd, pathname, how.flags, how.mode, - how.resolve, true); -if (fd >-2 ) { - ret =get_errno (fd); - }else { -const char *host_pathname = pathname; -if (pathname[0 ] =='/' && - !(how.resolve & (RESOLVE_IN_ROOT |RESOLVE_BENEATH ))) { -/* -* RESOLVE_BENEATH rejects absolute paths; RESOLVE_IN_ROOT -* resolves them relative to dirfd. -*/ - host_pathname =path (pathname); - } - ret =get_errno (safe_openat2 (dirfd, host_pathname, &how, -sizeof (struct open_how_ver0 ))); +const char *host_pathname = pathname; +if (pathname[0 ] =='/' && + !(how.resolve & (RESOLVE_IN_ROOT |RESOLVE_BENEATH ))) { +/* +* RESOLVE_BENEATH rejects absolute paths; RESOLVE_IN_ROOT +* resolves them relative to dirfd. +*/ + host_pathname =path (pathname); } +int fd =safe_openat2 (dirfd, host_pathname, &how, +sizeof (struct open_how_ver0 )); + ret =get_errno (maybe_do_fake_open (cpu_env, fd, dirfd, host_pathname, &how, + true));fd_trans_unregister (ret);unlock_user (pathname, guest_pathname,0 );@@ -9034,6 +9186,19 @@ ssize_t do_guest_readlink(const char *pathname, char *buf, size_t bufsiz)return ret; } +static ssize_t do_guest_readlinkat (int dirfd,const char *pathname, +char *buf,size_t bufsiz) +{ +const char *host_name =path (pathname); + g_autofree char *entry_path = proc_entry_path(dirfd, host_name); +ssize_t ret =readlinkat (dirfd, host_name, buf, bufsiz); + +if (ret >=0 && entry_path &&is_proc_myself (entry_path,"exe" )) { +return do_guest_readlink (entry_path, buf, bufsiz); + } +return ret; +} +static int do_execv (CPUArchState *cpu_env,int dirfd,abi_long pathname,abi_long guest_argp,abi_long guest_envp,int flags,bool is_execveat)@@ -11143,16 +11308,8 @@ static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1, }else if (!arg4) {/* Short circuit this for the magic exe check. */ ret = -TARGET_EINVAL ; - }else if (is_proc_myself ((const char *)p,"exe" )) { -/* -* Don't worry about sign mismatch as earlier mapping -* logic would have thrown a bad address error. -*/ - ret =MIN (strlen (real_exec_path), arg4); -/* We cannot NUL terminate the string. */ -memcpy (p2, real_exec_path, ret); }else { - ret =get_errno (readlinkat (arg1,path (p), p2, arg4)); + ret =get_errno (do_guest_readlinkat (arg1, p, p2, arg4)); }unlock_user (p2, arg3, ret);unlock_user (p, arg2,0 );tests/tcg/multiarch/linux/proc-openat.c new file +94
-0@@ -0,0 +1,94 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +#define _GNU_SOURCE +#include <assert.h> +#include <errno.h> +#include <fcntl.h> +#include <stdlib.h> +#include <string.h> +#include <sys/resource.h> +#include <unistd.h> + +static char expected[16384 ]; +static size_t expected_size; + +static void check (int dirfd,const char *name,int flags,int expected_errno) +{ +char actual[sizeof (expected)]; +size_t size =0 ; +int fd =openat (dirfd, name, flags); + +if (expected_errno) { +assert (fd ==-1 && errno == expected_errno); +return ; + } +assert (fd >=0 ); +assert ((fcntl (fd,F_GETFD ) &FD_CLOEXEC ) == + ((flags &O_CLOEXEC ) ?FD_CLOEXEC :0 )); +assert ((fcntl (fd,F_GETFL ) & (O_PATH |O_NOFOLLOW |O_ACCMODE )) == + (flags & (O_PATH |O_NOFOLLOW |O_ACCMODE ))); +if (flags &O_PATH ) { +assert (read (fd, actual,1 ) ==-1 && errno ==EBADF ); + }else { +for (;;) { +ssize_t n =read (fd, actual + size,sizeof (actual) - size); + +if (n <0 && errno ==EINTR ) { +continue ; + } +assert (n >=0 ); +if (!n) { +break ; + } + size += n; +assert (size <sizeof (actual)); + } +assert (size == expected_size && !memcmp (actual, expected, size)); +assert (write (fd,"x" ,1 ) ==-1 && errno ==EBADF ); + } +assert (close (fd) ==0 ); +} + +int main (int argc,char **argv) +{ +struct rlimit saved, limit; +int cwd =open ("." ,O_PATH |O_DIRECTORY |O_CLOEXEC ); +int proc =open ("/proc" ,O_PATH |O_DIRECTORY |O_CLOEXEC ); +int self =open ("/proc/self" ,O_PATH |O_DIRECTORY |O_CLOEXEC ); +int flags =O_RDONLY |O_CLOEXEC ; + +assert (cwd >=0 && proc >=0 && self >=0 ); +for (int i =0 ; i < argc; i++) { +size_t size =strlen (argv[i]) +1 ; + +assert (size <=sizeof (expected) - expected_size); +memcpy (expected + expected_size, argv[i], size); + expected_size += size; + } +check (AT_FDCWD ,"/proc/self/cmdline" , flags,0 ); +check (-1 ,"/proc/self/cmdline" , flags,0 ); +check (proc,"self/cmdline" , flags,0 ); +check (proc,"./self/cmdline" , flags,0 ); +check (self,"cmdline" , flags,0 ); +check (self,"cmdline" , flags |O_NOFOLLOW ,0 ); +check (self,"cmdline" ,O_PATH |O_CLOEXEC ,0 ); +check (self,"cmdline" , flags |O_DIRECTORY ,ENOTDIR ); +assert (chdir ("/proc" ) ==0 ); +check (AT_FDCWD ,"self/cmdline" , flags,0 ); +check (-1 ,"self/cmdline" , flags,EBADF ); +assert (fchdir (cwd) ==0 ); +assert (close (self) ==0 ); +assert (close (proc) ==0 ); +assert (close (cwd) ==0 ); +assert (getrlimit (RLIMIT_NOFILE , &saved) ==0 ); +int next =open ("/dev/null" ,O_RDONLY |O_CLOEXEC ); + +assert (next >=0 ); + limit = saved; + limit.rlim_cur = next +1 ; +assert (setrlimit (RLIMIT_NOFILE , &limit) ==0 ); +assert (close (next) ==0 ); +check (AT_FDCWD ,"/proc/self/cmdline" , flags,0 ); +check (AT_FDCWD ,"/proc/self/cmdline" , flags |O_NOFOLLOW ,0 ); +assert (setrlimit (RLIMIT_NOFILE , &saved) ==0 ); +return EXIT_SUCCESS ; +}tests/tcg/multiarch/linux/proc-openat2.c new file +83
-0@@ -0,0 +1,83 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +#define _GNU_SOURCE +#include <assert.h> +#include <errno.h> +#include <fcntl.h> +#include <stdint.h> +#include <stdlib.h> +#include <sys/syscall.h> +#include <unistd.h> + +#ifdef SYS_openat2 +#include <linux/openat2.h> + +static int cmdline_argc; +static char **cmdline_argv; + +static void check (int dirfd,const char *name,uint64_t flags, +uint64_t resolve,int expected_errno) +{ +struct open_how how = { .flags = flags, .resolve = resolve }; +char byte; +int fd =syscall (SYS_openat2, dirfd, name, &how,sizeof (how)); + +if (expected_errno) { +assert (fd ==-1 && errno == expected_errno); +return ; + } +assert (fd >=0 ); +assert (fcntl (fd,F_GETFD ) ==FD_CLOEXEC ); +if (flags &O_PATH ) { +assert (fcntl (fd,F_GETFL ) &O_PATH ); +assert (read (fd, &byte,1 ) ==-1 && errno ==EBADF ); + }else { +for (int i =0 ; i < cmdline_argc; i++) { +const char *p = cmdline_argv[i]; + +do { +assert (read (fd, &byte,1 ) ==1 && byte == *p); + }while (*p++); + } +assert (read (fd, &byte,1 ) ==0 ); +assert (write (fd,"x" ,1 ) ==-1 && errno ==EBADF ); + } +assert (close (fd) ==0 ); +} +#endif + +int main (int argc,char **argv) +{ +#ifdef SYS_openat2 +struct open_how how = { .flags =O_RDONLY |O_CLOEXEC }; +int fd =syscall (SYS_openat2,AT_FDCWD ,"/proc/self/cmdline" , + &how,sizeof (how)); +int proc, self, root, flags =O_RDONLY |O_CLOEXEC ; + + cmdline_argc = argc; + cmdline_argv = argv; +if (fd <0 && errno ==ENOSYS ) { +return EXIT_SUCCESS ; + } +assert (fd >=0 &&close (fd) ==0 ); + proc =open ("/proc" ,O_PATH |O_DIRECTORY |O_CLOEXEC ); + self =open ("/proc/self" ,O_PATH |O_DIRECTORY |O_CLOEXEC ); + root =open ("/" ,O_PATH |O_DIRECTORY |O_CLOEXEC ); +assert (proc >=0 && self >=0 && root >=0 ); +check (-1 ,"/proc/self/cmdline" , flags,0 ,0 ); +check (AT_FDCWD ,"/proc/self/cmdline" , flags, +RESOLVE_NO_SYMLINKS ,ELOOP ); +check (proc,"self/cmdline" , flags,0 ,0 ); +check (proc,"self/cmdline" , flags,RESOLVE_NO_SYMLINKS ,ELOOP ); +check (proc,"self/root/proc/self/cmdline" , flags, +RESOLVE_NO_MAGICLINKS ,ELOOP ); +check (self,"../self/cmdline" , flags,RESOLVE_BENEATH ,EXDEV ); +check (self,"../self/cmdline" , flags,RESOLVE_IN_ROOT ,ENOENT ); +check (root,"proc/self/cmdline" , flags,RESOLVE_NO_XDEV ,EXDEV ); +check (proc,"self/cmdline" , flags,UINT64_C (1 ) <<63 ,EINVAL ); +check (self,"cmdline" ,O_PATH |O_CLOEXEC ,0 ,0 ); +assert (close (root) ==0 ); +assert (close (self) ==0 ); +assert (close (proc) ==0 ); +#endif +return EXIT_SUCCESS ; +}tests/tcg/multiarch/meson.build +2
-1@@ -61,6 +61,8 @@ tests += { multiarch/'linux/linux-shmat-null.c': {}, multiarch/'linux/linux-sigrtminmax.c': {}, multiarch/'linux/linux-test.c': {}, + multiarch/'linux/proc-openat.c': {}, + multiarch/'linux/proc-openat2.c': {}, multiarch/'linux/test-vma.c': {}, } --2.55.0
Re: [PATCH] linux-user: honor dirfd for synthetic proc opens
Hi,
Thanks for queuing the patch. It also fixes the HPPA case reported in QEMU issue #2004.
On the parent commit (8f66cb79), the test from the issue returned the x86 host's cpu info when opening cpuinfo relative to a directory fd for /proc. Opening /proc/cpuinfo by its absolute path returned the emulated PA-RISC cpu info.
With ea62db5e, both calls reported PA7300LC (PCX-L2). Their outputs matched byte for byte in all five runs (1,332 bytes each).
https://gitlab.com/qemu-project/qemu/-/issues/2004
https://github.com/hdeller/qemu-hppa/commit/ea62db5e88bbfe2ea3de81d41aa29b029962425f
Best,
Artemii
вс, 22 нояб. 2026 г. в 19:07, Helge Deller <[email protected]>:
Quoted 636 lines · Artemii Mashanov
Quoted 5 lines
Hi,
Ping on this patch. Could someone please take a look when time permits?
Please let me know if any changes or additional testing are needed.
I've queued this patch up for qemu v12.0.
Overall I'm not happy how we handle the relative paths when running outside of a chroot. I think this something we should take a closer look during the next release cycle.
Thanks!
HelgeQuoted 619 lines
Thanks,
Artemiiвс, 25 окт. 2026 г. в 18:23, Artemii Mashanov <[email protected]>:
Quoted 615 lines
Opening "<pid>/cmdline" relative to a directory descriptor for /proc can return QEMU's command line instead of the guest's.
Honor dirfd when selecting synthetic proc contents and add regression coverage for openat and openat2.
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/4458
Signed-off-by Artemii Mashanov <[email protected]>--- linux-user/syscall.c | 321 +++++++++++++++++------ tests/tcg/multiarch/linux/proc-openat.c | 94 +++++++ tests/tcg/multiarch/linux/proc-openat2.c | 83 ++++++ tests/tcg/multiarch/meson.build | 2 + 4 files changed, 418 insertions(+), 82 deletions(-)create mode 100644
tests/tcg/multiarch/linux/proc-openat.ccreate mode 100644tests/tcg/multiarch/linux/proc-openat2.clinux-user/syscall.c +239
-82@@ -8839,12 +8839,132 @@ static int open_net_route(CPUArchState *cpu_env, int fd) }#endif -static int maybe_do_fake_open (CPUArchState *cpu_env,int dirfd, -const char *fname,int flags,mode_t mode, -int openat2_resolve,bool safe) +static char *proc_entry_path (int dirfd,const char *name) +{ + g_autofree char *base = NULL; + g_autofree char *absolute = NULL; + g_autofree char *parent = NULL; + g_autofree char *resolved = NULL; + g_autofree char *entry = NULL; +char fd_path[64 ]; + +if (!name[0 ]) { +snprintf (fd_path,sizeof (fd_path),"/proc/self/fd/%d" , dirfd); +return g_file_read_link (fd_path,NULL ); + } +if (name[0 ] =='/' ) { + absolute =g_strdup (name); + }else { +if (dirfd ==AT_FDCWD ) { + base =getcwd (NULL ,0 ); + }else { +snprintf (fd_path,sizeof (fd_path),"/proc/self/fd/%d" , dirfd); + base =g_file_read_link (fd_path,NULL ); + } +if (!base) { +return NULL ; + } + absolute =g_build_filename (base, name,NULL ); + } + parent =g_path_get_dirname (absolute); + resolved =realpath (parent,NULL ); +if (!resolved) { +return NULL ; + } + entry =g_path_get_basename (absolute); +return g_build_filename (resolved, entry,NULL ); +} + +static int fake_open_contents (CPUArchState *cpu_env, +int (*fill)(CPUArchState *,int ), +int status_flags,bool safe) +{ + g_autofree char *filename = NULL; +const char *tmpdir =g_get_tmp_dir (); +const char *reader_path; +char fd_path[64 ]; +bool named = status_flags &O_NOFOLLOW ; +int fd, reader =-1 , result =-1 , saved_errno; + +retry: +if (named) { +g_free (filename); + filename =g_build_filename (tmpdir,"qemu-open.XXXXXX" ,NULL ); + fd =g_mkstemp_full (filename,O_RDWR |O_CLOEXEC ,0600 ); +if (fd <0 &&strcmp (tmpdir, P_tmpdir) && + errno !=EMFILE && errno !=ENFILE && + errno !=ENOMEM && errno !=EINTR ) { + tmpdir = P_tmpdir; + goto retry; + } + }else { + fd =memfd_create ("qemu-open" ,MFD_CLOEXEC ); + } +if (fd <0 ) { +if (!named && (errno ==ENOSYS || errno ==EPERM )) { + named = true; + goto retry; + } +return -1 ; + } +if (fill (cpu_env, fd)) { + goto out; + } +if (named) { +int closed =close (fd); + + fd =-1 ; +if (closed <0 ) { + goto out; + } + reader_path = filename; + }else { +snprintf (fd_path,sizeof (fd_path),"/proc/self/fd/%d" , fd); + reader_path = fd_path; + } + reader = safe ?safe_openat (AT_FDCWD , reader_path, status_flags,0 ) : +openat (AT_FDCWD , reader_path, status_flags,0 ); +if (reader <0 ) { +if (!named && errno ==EMFILE ) { +close (fd); + named = true; + goto retry; + } + goto out; + } + result = named ? reader :dup3 (reader, fd, status_flags &O_CLOEXEC ); +if (result >=0 ) { +if (named) { + reader =-1 ; + }else { + fd =-1 ; + } + } +out: + saved_errno = errno; +if (reader >=0 ) { +close (reader); + } +if (fd >=0 ) { +close (fd); + } +if (named) { +unlink (filename); + } + errno = saved_errno; +return result; +} + +static int maybe_do_fake_open (CPUArchState *cpu_env,int opened_fd,int dirfd, +const char *fname, +const struct open_how_ver0 *how, +bool safe) { g_autofree char *proc_name = NULL; +char fd_path[64 ];const char *pathname; +int flags = how->flags; +int original_errno = errno;struct fake_open {const char *filename;int (*fill)(CPUArchState *cpu_env,int fd);@@ -8869,26 +8989,70 @@ static int maybe_do_fake_open(CPUArchState *cpu_env, int dirfd, { NULL, NULL, NULL } }; -/* if this is a file from /proc/ filesystem, expand full name */ - proc_name =realpath (fname,NULL ); -if (proc_name &&strncmp (proc_name,"/proc/" ,6 ) ==0 ) { - pathname = proc_name; - }else { - pathname = fname; +if (opened_fd >=0 && + ((flags & (O_PATH |O_NOFOLLOW )) == (O_PATH |O_NOFOLLOW ))) { +return opened_fd; + } +if (opened_fd <0 && original_errno !=ENOENT ) { +return opened_fd; } -if (is_proc_myself (pathname,"exe" )) { -/* Honor openat2 resolve flags */ -if ((openat2_resolve &RESOLVE_NO_MAGICLINKS ) || - (openat2_resolve &RESOLVE_NO_SYMLINKS )) { - errno =ELOOP ; -return -1 ; +if (opened_fd >=0 && !(flags &O_NOFOLLOW ) && + !(how->resolve & (RESOLVE_IN_ROOT |RESOLVE_BENEATH | +RESOLVE_NO_SYMLINKS |RESOLVE_NO_MAGICLINKS ))) { +struct stat opened, executable; + +if (fstat (opened_fd, &opened) ==0 && +stat ("/proc/self/exe" , &executable) ==0 && + opened.st_dev == executable.st_dev && + opened.st_ino == executable.st_ino) { + g_autofree char *entry = proc_entry_path(dirfd, fname); + +if (entry &&is_proc_myself (entry,"exe" )) { +close (opened_fd); +if (how->resolve) { +return safe_openat2 (AT_FDCWD , real_exec_path, + how,sizeof (*how)); + } +return safe ?safe_openat (AT_FDCWD , real_exec_path, + flags, how->mode) : +openat (AT_FDCWD , real_exec_path, flags, how->mode); + } } -if (safe) { -return safe_openat (dirfd, real_exec_path, flags, mode); - }else { -return openat (dirfd, real_exec_path, flags, mode); + } + +if (opened_fd <0 ) { + g_autofree char *parent = g_path_get_dirname(fname); + g_autofree char *entry = g_path_get_basename(fname); + g_autofree char *directory = NULL; +struct open_how_ver0 parent_how = { + .flags =O_PATH |O_DIRECTORY |O_CLOEXEC , + .resolve = how->resolve, + }; +int fd; + + fd = how->resolve ? +safe_openat2 (dirfd, parent, &parent_how,sizeof (parent_how)) : + safe ?safe_openat (dirfd, parent, parent_how.flags,0 ) : +openat (dirfd, parent, parent_how.flags,0 ); +if (fd >=0 ) { +snprintf (fd_path,sizeof (fd_path),"/proc/self/fd/%d" , fd); + directory =g_file_read_link (fd_path,NULL ); +close (fd); +if (directory) { + proc_name =g_build_filename (directory, entry,NULL ); + } } + }else { +snprintf (fd_path,sizeof (fd_path),"/proc/self/fd/%d" , opened_fd); + proc_name =g_file_read_link (fd_path,NULL ); + } + pathname = proc_name; + +if ((opened_fd >=0 && (flags &O_PATH )) || + !pathname ||strncmp (pathname,"/proc/" ,6 )) { + errno = original_errno; +return opened_fd; }for (fake_open = fakes; fake_open->filename; fake_open++) {@@ -8898,60 +9062,52 @@ static int maybe_do_fake_open(CPUArchState *cpu_env, int dirfd, }if (fake_open->filename) { -const char *tmpdir; -char filename[PATH_MAX ]; -int fd, r; - - fd =memfd_create ("qemu-open" ,0 ); -if (fd <0 ) { -if (errno !=ENOSYS ) { -return fd; - } -/* create temporary file to map stat to */ - tmpdir =getenv ("TMPDIR" ); -if (!tmpdir) - tmpdir ="/tmp" ; -snprintf (filename,sizeof (filename),"%s/qemu-open.XXXXXX" , tmpdir); - fd =mkstemp (filename); -if (fd <0 ) { -return fd; - } -unlink (filename); - } - -if ((r = fake_open->fill (cpu_env, fd))) { -int e = errno; -close (fd); - errno = e; -return r; +int saved_errno, status_flags; + + status_flags = flags & ~(O_CREAT |O_EXCL |O_TRUNC ); +if (opened_fd >=0 ) { + status_flags =fcntl (opened_fd,F_GETFL ); + saved_errno = errno; +close (opened_fd); +if (status_flags <0 ) { + errno = saved_errno; +return -1 ; + } + }else if (flags &O_DIRECTORY ) { + errno =ENOTDIR ; +return -1 ; + }else if ((flags & (O_CREAT |O_EXCL )) == (O_CREAT |O_EXCL )) { + errno =EEXIST ; +return -1 ; } -lseek (fd,0 ,SEEK_SET ); -return fd; + status_flags |= flags &O_CLOEXEC ; +return fake_open_contents (cpu_env, fake_open->fill, status_flags, safe); } -return -2 ; + errno = original_errno; +return opened_fd; }int do_guest_openat (CPUArchState *cpu_env,int dirfd,const char *pathname,int flags,mode_t mode,bool safe) { -int fd =maybe_do_fake_open (cpu_env, dirfd, pathname, flags, mode,0 , safe); -if (fd >-2 ) { -return fd; - } +struct open_how_ver0 how = { .flags = flags, .mode = mode }; +const char *host_name =path (pathname); +int fd;if (safe) { -return safe_openat (dirfd,path (pathname), flags, mode); + fd =safe_openat (dirfd, host_name, flags, mode); }else { -return openat (dirfd,path (pathname), flags, mode); + fd =openat (dirfd, host_name, flags, mode); } +return maybe_do_fake_open (cpu_env, fd, dirfd, host_name, &how, + safe); } -static int do_openat2 (CPUArchState *cpu_env,abi_long dirfd, -abi_ptr guest_pathname,abi_ptr guest_open_how, -abi_ulong guest_size) +abi_ptr guest_pathname,abi_ptr guest_open_how, +abi_ulong guest_size) {struct open_how_ver0 how = {0 };char *pathname;@@ -8981,23 +9137,19 @@ static int do_openat2(CPUArchState *cpu_env, abi_long dirfd, how.flags =target_to_host_bitmask (tswap64 (how.flags), fcntl_flags_tbl); how.mode =tswap64 (how.mode); how.resolve =tswap64 (how.resolve); -int fd =maybe_do_fake_open (cpu_env, dirfd, pathname, how.flags, how.mode, - how.resolve, true); -if (fd >-2 ) { - ret =get_errno (fd); - }else { -const char *host_pathname = pathname; -if (pathname[0 ] =='/' && - !(how.resolve & (RESOLVE_IN_ROOT |RESOLVE_BENEATH ))) { -/* -* RESOLVE_BENEATH rejects absolute paths; RESOLVE_IN_ROOT -* resolves them relative to dirfd. -*/ - host_pathname =path (pathname); - } - ret =get_errno (safe_openat2 (dirfd, host_pathname, &how, -sizeof (struct open_how_ver0 ))); +const char *host_pathname = pathname; +if (pathname[0 ] =='/' && + !(how.resolve & (RESOLVE_IN_ROOT |RESOLVE_BENEATH ))) { +/* +* RESOLVE_BENEATH rejects absolute paths; RESOLVE_IN_ROOT +* resolves them relative to dirfd. +*/ + host_pathname =path (pathname); } +int fd =safe_openat2 (dirfd, host_pathname, &how, +sizeof (struct open_how_ver0 )); + ret =get_errno (maybe_do_fake_open (cpu_env, fd, dirfd, host_pathname, &how, + true));fd_trans_unregister (ret);unlock_user (pathname, guest_pathname,0 );@@ -9034,6 +9186,19 @@ ssize_t do_guest_readlink(const char *pathname, char *buf, size_t bufsiz)return ret; } +static ssize_t do_guest_readlinkat (int dirfd,const char *pathname, +char *buf,size_t bufsiz) +{ +const char *host_name =path (pathname); + g_autofree char *entry_path = proc_entry_path(dirfd, host_name); +ssize_t ret =readlinkat (dirfd, host_name, buf, bufsiz); + +if (ret >=0 && entry_path &&is_proc_myself (entry_path,"exe" )) { +return do_guest_readlink (entry_path, buf, bufsiz); + } +return ret; +} +static int do_execv (CPUArchState *cpu_env,int dirfd,abi_long pathname,abi_long guest_argp,abi_long guest_envp,int flags,bool is_execveat)@@ -11143,16 +11308,8 @@ static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1, }else if (!arg4) {/* Short circuit this for the magic exe check. */ ret = -TARGET_EINVAL ; - }else if (is_proc_myself ((const char *)p,"exe" )) { -/* -* Don't worry about sign mismatch as earlier mapping -* logic would have thrown a bad address error. -*/ - ret =MIN (strlen (real_exec_path), arg4); -/* We cannot NUL terminate the string. */ -memcpy (p2, real_exec_path, ret); }else { - ret =get_errno (readlinkat (arg1,path (p), p2, arg4)); + ret =get_errno (do_guest_readlinkat (arg1, p, p2, arg4)); }unlock_user (p2, arg3, ret);unlock_user (p, arg2,0 );tests/tcg/multiarch/linux/proc-openat.c new file +94
-0@@ -0,0 +1,94 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +#define _GNU_SOURCE +#include <assert.h> +#include <errno.h> +#include <fcntl.h> +#include <stdlib.h> +#include <string.h> +#include <sys/resource.h> +#include <unistd.h> + +static char expected[16384 ]; +static size_t expected_size; + +static void check (int dirfd,const char *name,int flags,int expected_errno) +{ +char actual[sizeof (expected)]; +size_t size =0 ; +int fd =openat (dirfd, name, flags); + +if (expected_errno) { +assert (fd ==-1 && errno == expected_errno); +return ; + } +assert (fd >=0 ); +assert ((fcntl (fd,F_GETFD ) &FD_CLOEXEC ) == + ((flags &O_CLOEXEC ) ?FD_CLOEXEC :0 )); +assert ((fcntl (fd,F_GETFL ) & (O_PATH |O_NOFOLLOW |O_ACCMODE )) == + (flags & (O_PATH |O_NOFOLLOW |O_ACCMODE ))); +if (flags &O_PATH ) { +assert (read (fd, actual,1 ) ==-1 && errno ==EBADF ); + }else { +for (;;) { +ssize_t n =read (fd, actual + size,sizeof (actual) - size); + +if (n <0 && errno ==EINTR ) { +continue ; + } +assert (n >=0 ); +if (!n) { +break ; + } + size += n; +assert (size <sizeof (actual)); + } +assert (size == expected_size && !memcmp (actual, expected, size)); +assert (write (fd,"x" ,1 ) ==-1 && errno ==EBADF ); + } +assert (close (fd) ==0 ); +} + +int main (int argc,char **argv) +{ +struct rlimit saved, limit; +int cwd =open ("." ,O_PATH |O_DIRECTORY |O_CLOEXEC ); +int proc =open ("/proc" ,O_PATH |O_DIRECTORY |O_CLOEXEC ); +int self =open ("/proc/self" ,O_PATH |O_DIRECTORY |O_CLOEXEC ); +int flags =O_RDONLY |O_CLOEXEC ; + +assert (cwd >=0 && proc >=0 && self >=0 ); +for (int i =0 ; i < argc; i++) { +size_t size =strlen (argv[i]) +1 ; + +assert (size <=sizeof (expected) - expected_size); +memcpy (expected + expected_size, argv[i], size); + expected_size += size; + } +check (AT_FDCWD ,"/proc/self/cmdline" , flags,0 ); +check (-1 ,"/proc/self/cmdline" , flags,0 ); +check (proc,"self/cmdline" , flags,0 ); +check (proc,"./self/cmdline" , flags,0 ); +check (self,"cmdline" , flags,0 ); +check (self,"cmdline" , flags |O_NOFOLLOW ,0 ); +check (self,"cmdline" ,O_PATH |O_CLOEXEC ,0 ); +check (self,"cmdline" , flags |O_DIRECTORY ,ENOTDIR ); +assert (chdir ("/proc" ) ==0 ); +check (AT_FDCWD ,"self/cmdline" , flags,0 ); +check (-1 ,"self/cmdline" , flags,EBADF ); +assert (fchdir (cwd) ==0 ); +assert (close (self) ==0 ); +assert (close (proc) ==0 ); +assert (close (cwd) ==0 ); +assert (getrlimit (RLIMIT_NOFILE , &saved) ==0 ); +int next =open ("/dev/null" ,O_RDONLY |O_CLOEXEC ); + +assert (next >=0 ); + limit = saved; + limit.rlim_cur = next +1 ; +assert (setrlimit (RLIMIT_NOFILE , &limit) ==0 ); +assert (close (next) ==0 ); +check (AT_FDCWD ,"/proc/self/cmdline" , flags,0 ); +check (AT_FDCWD ,"/proc/self/cmdline" , flags |O_NOFOLLOW ,0 ); +assert (setrlimit (RLIMIT_NOFILE , &saved) ==0 ); +return EXIT_SUCCESS ; +}tests/tcg/multiarch/linux/proc-openat2.c new file +83
-0@@ -0,0 +1,83 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +#define _GNU_SOURCE +#include <assert.h> +#include <errno.h> +#include <fcntl.h> +#include <stdint.h> +#include <stdlib.h> +#include <sys/syscall.h> +#include <unistd.h> + +#ifdef SYS_openat2 +#include <linux/openat2.h> + +static int cmdline_argc; +static char **cmdline_argv; + +static void check (int dirfd,const char *name,uint64_t flags, +uint64_t resolve,int expected_errno) +{ +struct open_how how = { .flags = flags, .resolve = resolve }; +char byte; +int fd =syscall (SYS_openat2, dirfd, name, &how,sizeof (how)); + +if (expected_errno) { +assert (fd ==-1 && errno == expected_errno); +return ; + } +assert (fd >=0 ); +assert (fcntl (fd,F_GETFD ) ==FD_CLOEXEC ); +if (flags &O_PATH ) { +assert (fcntl (fd,F_GETFL ) &O_PATH ); +assert (read (fd, &byte,1 ) ==-1 && errno ==EBADF ); + }else { +for (int i =0 ; i < cmdline_argc; i++) { +const char *p = cmdline_argv[i]; + +do { +assert (read (fd, &byte,1 ) ==1 && byte == *p); + }while (*p++); + } +assert (read (fd, &byte,1 ) ==0 ); +assert (write (fd,"x" ,1 ) ==-1 && errno ==EBADF ); + } +assert (close (fd) ==0 ); +} +#endif + +int main (int argc,char **argv) +{ +#ifdef SYS_openat2 +struct open_how how = { .flags =O_RDONLY |O_CLOEXEC }; +int fd =syscall (SYS_openat2,AT_FDCWD ,"/proc/self/cmdline" , + &how,sizeof (how)); +int proc, self, root, flags =O_RDONLY |O_CLOEXEC ; + + cmdline_argc = argc; + cmdline_argv = argv; +if (fd <0 && errno ==ENOSYS ) { +return EXIT_SUCCESS ; + } +assert (fd >=0 &&close (fd) ==0 ); + proc =open ("/proc" ,O_PATH |O_DIRECTORY |O_CLOEXEC ); + self =open ("/proc/self" ,O_PATH |O_DIRECTORY |O_CLOEXEC ); + root =open ("/" ,O_PATH |O_DIRECTORY |O_CLOEXEC ); +assert (proc >=0 && self >=0 && root >=0 ); +check (-1 ,"/proc/self/cmdline" , flags,0 ,0 ); +check (AT_FDCWD ,"/proc/self/cmdline" , flags, +RESOLVE_NO_SYMLINKS ,ELOOP ); +check (proc,"self/cmdline" , flags,0 ,0 ); +check (proc,"self/cmdline" , flags,RESOLVE_NO_SYMLINKS ,ELOOP ); +check (proc,"self/root/proc/self/cmdline" , flags, +RESOLVE_NO_MAGICLINKS ,ELOOP ); +check (self,"../self/cmdline" , flags,RESOLVE_BENEATH ,EXDEV ); +check (self,"../self/cmdline" , flags,RESOLVE_IN_ROOT ,ENOENT ); +check (root,"proc/self/cmdline" , flags,RESOLVE_NO_XDEV ,EXDEV ); +check (proc,"self/cmdline" , flags,UINT64_C (1 ) <<63 ,EINVAL ); +check (self,"cmdline" ,O_PATH |O_CLOEXEC ,0 ,0 ); +assert (close (root) ==0 ); +assert (close (self) ==0 ); +assert (close (proc) ==0 ); +#endif +return EXIT_SUCCESS ; +}tests/tcg/multiarch/meson.build +2
-1@@ -61,6 +61,8 @@ tests += { multiarch/'linux/linux-shmat-null.c': {}, multiarch/'linux/linux-sigrtminmax.c': {}, multiarch/'linux/linux-test.c': {}, + multiarch/'linux/proc-openat.c': {}, + multiarch/'linux/proc-openat2.c': {}, multiarch/'linux/test-vma.c': {}, } --2.55.0
Re: [PATCH] linux-user: honor dirfd for synthetic proc opens
Quoted 4 lines · Artemii Mashanov
Hi,
Thanks for queuing the patch. It also fixes the HPPA case reported in QEMU issue #2004.
Thanks!
I added a new resolves tag for thus issue to the patch.
Helge
Quoted 651 lines
On the parent commit (
8f66cb79), the test from the issue returned the x86 host's cpu info when opening cpuinfo relative to a directory fd for /proc. Opening/proc/cpuinfoby its absolute path returned the emulated PA-RISC cpu info.With
ea62db5e, both calls reported PA7300LC (PCX-L2). Their outputs matched byte for byte in all five runs (1,332 bytes each).https://gitlab.com/qemu-project/qemu/-/issues/2004
https://github.com/hdeller/qemu-hppa/commit/ea62db5e88bbfe2ea3de81d41aa29b029962425fBest,
Artemiiвс, 22 нояб. 2026 г. в 19:07, Helge Deller <[email protected]>:
Quoted 636 lines · Artemii Mashanov
Quoted 5 lines
Hi,
Ping on this patch. Could someone please take a look when time permits?
Please let me know if any changes or additional testing are needed.
I've queued this patch up for qemu v12.0.
Overall I'm not happy how we handle the relative paths when running outside of a chroot. I think this something we should take a closer look during the next release cycle.
Thanks!
HelgeQuoted 619 lines
Thanks,
Artemiiвс, 25 окт. 2026 г. в 18:23, Artemii Mashanov <[email protected]>:
Quoted 615 lines
Opening "<pid>/cmdline" relative to a directory descriptor for /proc can return QEMU's command line instead of the guest's.
Honor dirfd when selecting synthetic proc contents and add regression coverage for openat and openat2.
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/4458
Signed-off-by Artemii Mashanov <[email protected]>--- linux-user/syscall.c | 321 +++++++++++++++++------ tests/tcg/multiarch/linux/proc-openat.c | 94 +++++++ tests/tcg/multiarch/linux/proc-openat2.c | 83 ++++++ tests/tcg/multiarch/meson.build | 2 + 4 files changed, 418 insertions(+), 82 deletions(-)create mode 100644
tests/tcg/multiarch/linux/proc-openat.ccreate mode 100644tests/tcg/multiarch/linux/proc-openat2.clinux-user/syscall.c +239
-82@@ -8839,12 +8839,132 @@ static int open_net_route(CPUArchState *cpu_env, int fd) }#endif -static int maybe_do_fake_open (CPUArchState *cpu_env,int dirfd, -const char *fname,int flags,mode_t mode, -int openat2_resolve,bool safe) +static char *proc_entry_path (int dirfd,const char *name) +{ + g_autofree char *base = NULL; + g_autofree char *absolute = NULL; + g_autofree char *parent = NULL; + g_autofree char *resolved = NULL; + g_autofree char *entry = NULL; +char fd_path[64 ]; + +if (!name[0 ]) { +snprintf (fd_path,sizeof (fd_path),"/proc/self/fd/%d" , dirfd); +return g_file_read_link (fd_path,NULL ); + } +if (name[0 ] =='/' ) { + absolute =g_strdup (name); + }else { +if (dirfd ==AT_FDCWD ) { + base =getcwd (NULL ,0 ); + }else { +snprintf (fd_path,sizeof (fd_path),"/proc/self/fd/%d" , dirfd); + base =g_file_read_link (fd_path,NULL ); + } +if (!base) { +return NULL ; + } + absolute =g_build_filename (base, name,NULL ); + } + parent =g_path_get_dirname (absolute); + resolved =realpath (parent,NULL ); +if (!resolved) { +return NULL ; + } + entry =g_path_get_basename (absolute); +return g_build_filename (resolved, entry,NULL ); +} + +static int fake_open_contents (CPUArchState *cpu_env, +int (*fill)(CPUArchState *,int ), +int status_flags,bool safe) +{ + g_autofree char *filename = NULL; +const char *tmpdir =g_get_tmp_dir (); +const char *reader_path; +char fd_path[64 ]; +bool named = status_flags &O_NOFOLLOW ; +int fd, reader =-1 , result =-1 , saved_errno; + +retry: +if (named) { +g_free (filename); + filename =g_build_filename (tmpdir,"qemu-open.XXXXXX" ,NULL ); + fd =g_mkstemp_full (filename,O_RDWR |O_CLOEXEC ,0600 ); +if (fd <0 &&strcmp (tmpdir, P_tmpdir) && + errno !=EMFILE && errno !=ENFILE && + errno !=ENOMEM && errno !=EINTR ) { + tmpdir = P_tmpdir; + goto retry; + } + }else { + fd =memfd_create ("qemu-open" ,MFD_CLOEXEC ); + } +if (fd <0 ) { +if (!named && (errno ==ENOSYS || errno ==EPERM )) { + named = true; + goto retry; + } +return -1 ; + } +if (fill (cpu_env, fd)) { + goto out; + } +if (named) { +int closed =close (fd); + + fd =-1 ; +if (closed <0 ) { + goto out; + } + reader_path = filename; + }else { +snprintf (fd_path,sizeof (fd_path),"/proc/self/fd/%d" , fd); + reader_path = fd_path; + } + reader = safe ?safe_openat (AT_FDCWD , reader_path, status_flags,0 ) : +openat (AT_FDCWD , reader_path, status_flags,0 ); +if (reader <0 ) { +if (!named && errno ==EMFILE ) { +close (fd); + named = true; + goto retry; + } + goto out; + } + result = named ? reader :dup3 (reader, fd, status_flags &O_CLOEXEC ); +if (result >=0 ) { +if (named) { + reader =-1 ; + }else { + fd =-1 ; + } + } +out: + saved_errno = errno; +if (reader >=0 ) { +close (reader); + } +if (fd >=0 ) { +close (fd); + } +if (named) { +unlink (filename); + } + errno = saved_errno; +return result; +} + +static int maybe_do_fake_open (CPUArchState *cpu_env,int opened_fd,int dirfd, +const char *fname, +const struct open_how_ver0 *how, +bool safe) { g_autofree char *proc_name = NULL; +char fd_path[64 ];const char *pathname; +int flags = how->flags; +int original_errno = errno;struct fake_open {const char *filename;int (*fill)(CPUArchState *cpu_env,int fd);@@ -8869,26 +8989,70 @@ static int maybe_do_fake_open(CPUArchState *cpu_env, int dirfd, { NULL, NULL, NULL } }; -/* if this is a file from /proc/ filesystem, expand full name */ - proc_name =realpath (fname,NULL ); -if (proc_name &&strncmp (proc_name,"/proc/" ,6 ) ==0 ) { - pathname = proc_name; - }else { - pathname = fname; +if (opened_fd >=0 && + ((flags & (O_PATH |O_NOFOLLOW )) == (O_PATH |O_NOFOLLOW ))) { +return opened_fd; + } +if (opened_fd <0 && original_errno !=ENOENT ) { +return opened_fd; } -if (is_proc_myself (pathname,"exe" )) { -/* Honor openat2 resolve flags */ -if ((openat2_resolve &RESOLVE_NO_MAGICLINKS ) || - (openat2_resolve &RESOLVE_NO_SYMLINKS )) { - errno =ELOOP ; -return -1 ; +if (opened_fd >=0 && !(flags &O_NOFOLLOW ) && + !(how->resolve & (RESOLVE_IN_ROOT |RESOLVE_BENEATH | +RESOLVE_NO_SYMLINKS |RESOLVE_NO_MAGICLINKS ))) { +struct stat opened, executable; + +if (fstat (opened_fd, &opened) ==0 && +stat ("/proc/self/exe" , &executable) ==0 && + opened.st_dev == executable.st_dev && + opened.st_ino == executable.st_ino) { + g_autofree char *entry = proc_entry_path(dirfd, fname); + +if (entry &&is_proc_myself (entry,"exe" )) { +close (opened_fd); +if (how->resolve) { +return safe_openat2 (AT_FDCWD , real_exec_path, + how,sizeof (*how)); + } +return safe ?safe_openat (AT_FDCWD , real_exec_path, + flags, how->mode) : +openat (AT_FDCWD , real_exec_path, flags, how->mode); + } } -if (safe) { -return safe_openat (dirfd, real_exec_path, flags, mode); - }else { -return openat (dirfd, real_exec_path, flags, mode); + } + +if (opened_fd <0 ) { + g_autofree char *parent = g_path_get_dirname(fname); + g_autofree char *entry = g_path_get_basename(fname); + g_autofree char *directory = NULL; +struct open_how_ver0 parent_how = { + .flags =O_PATH |O_DIRECTORY |O_CLOEXEC , + .resolve = how->resolve, + }; +int fd; + + fd = how->resolve ? +safe_openat2 (dirfd, parent, &parent_how,sizeof (parent_how)) : + safe ?safe_openat (dirfd, parent, parent_how.flags,0 ) : +openat (dirfd, parent, parent_how.flags,0 ); +if (fd >=0 ) { +snprintf (fd_path,sizeof (fd_path),"/proc/self/fd/%d" , fd); + directory =g_file_read_link (fd_path,NULL ); +close (fd); +if (directory) { + proc_name =g_build_filename (directory, entry,NULL ); + } } + }else { +snprintf (fd_path,sizeof (fd_path),"/proc/self/fd/%d" , opened_fd); + proc_name =g_file_read_link (fd_path,NULL ); + } + pathname = proc_name; + +if ((opened_fd >=0 && (flags &O_PATH )) || + !pathname ||strncmp (pathname,"/proc/" ,6 )) { + errno = original_errno; +return opened_fd; }for (fake_open = fakes; fake_open->filename; fake_open++) {@@ -8898,60 +9062,52 @@ static int maybe_do_fake_open(CPUArchState *cpu_env, int dirfd, }if (fake_open->filename) { -const char *tmpdir; -char filename[PATH_MAX ]; -int fd, r; - - fd =memfd_create ("qemu-open" ,0 ); -if (fd <0 ) { -if (errno !=ENOSYS ) { -return fd; - } -/* create temporary file to map stat to */ - tmpdir =getenv ("TMPDIR" ); -if (!tmpdir) - tmpdir ="/tmp" ; -snprintf (filename,sizeof (filename),"%s/qemu-open.XXXXXX" , tmpdir); - fd =mkstemp (filename); -if (fd <0 ) { -return fd; - } -unlink (filename); - } - -if ((r = fake_open->fill (cpu_env, fd))) { -int e = errno; -close (fd); - errno = e; -return r; +int saved_errno, status_flags; + + status_flags = flags & ~(O_CREAT |O_EXCL |O_TRUNC ); +if (opened_fd >=0 ) { + status_flags =fcntl (opened_fd,F_GETFL ); + saved_errno = errno; +close (opened_fd); +if (status_flags <0 ) { + errno = saved_errno; +return -1 ; + } + }else if (flags &O_DIRECTORY ) { + errno =ENOTDIR ; +return -1 ; + }else if ((flags & (O_CREAT |O_EXCL )) == (O_CREAT |O_EXCL )) { + errno =EEXIST ; +return -1 ; } -lseek (fd,0 ,SEEK_SET ); -return fd; + status_flags |= flags &O_CLOEXEC ; +return fake_open_contents (cpu_env, fake_open->fill, status_flags, safe); } -return -2 ; + errno = original_errno; +return opened_fd; }int do_guest_openat (CPUArchState *cpu_env,int dirfd,const char *pathname,int flags,mode_t mode,bool safe) { -int fd =maybe_do_fake_open (cpu_env, dirfd, pathname, flags, mode,0 , safe); -if (fd >-2 ) { -return fd; - } +struct open_how_ver0 how = { .flags = flags, .mode = mode }; +const char *host_name =path (pathname); +int fd;if (safe) { -return safe_openat (dirfd,path (pathname), flags, mode); + fd =safe_openat (dirfd, host_name, flags, mode); }else { -return openat (dirfd,path (pathname), flags, mode); + fd =openat (dirfd, host_name, flags, mode); } +return maybe_do_fake_open (cpu_env, fd, dirfd, host_name, &how, + safe); } -static int do_openat2 (CPUArchState *cpu_env,abi_long dirfd, -abi_ptr guest_pathname,abi_ptr guest_open_how, -abi_ulong guest_size) +abi_ptr guest_pathname,abi_ptr guest_open_how, +abi_ulong guest_size) {struct open_how_ver0 how = {0 };char *pathname;@@ -8981,23 +9137,19 @@ static int do_openat2(CPUArchState *cpu_env, abi_long dirfd, how.flags =target_to_host_bitmask (tswap64 (how.flags), fcntl_flags_tbl); how.mode =tswap64 (how.mode); how.resolve =tswap64 (how.resolve); -int fd =maybe_do_fake_open (cpu_env, dirfd, pathname, how.flags, how.mode, - how.resolve, true); -if (fd >-2 ) { - ret =get_errno (fd); - }else { -const char *host_pathname = pathname; -if (pathname[0 ] =='/' && - !(how.resolve & (RESOLVE_IN_ROOT |RESOLVE_BENEATH ))) { -/* -* RESOLVE_BENEATH rejects absolute paths; RESOLVE_IN_ROOT -* resolves them relative to dirfd. -*/ - host_pathname =path (pathname); - } - ret =get_errno (safe_openat2 (dirfd, host_pathname, &how, -sizeof (struct open_how_ver0 ))); +const char *host_pathname = pathname; +if (pathname[0 ] =='/' && + !(how.resolve & (RESOLVE_IN_ROOT |RESOLVE_BENEATH ))) { +/* +* RESOLVE_BENEATH rejects absolute paths; RESOLVE_IN_ROOT +* resolves them relative to dirfd. +*/ + host_pathname =path (pathname); } +int fd =safe_openat2 (dirfd, host_pathname, &how, +sizeof (struct open_how_ver0 )); + ret =get_errno (maybe_do_fake_open (cpu_env, fd, dirfd, host_pathname, &how, + true));fd_trans_unregister (ret);unlock_user (pathname, guest_pathname,0 );@@ -9034,6 +9186,19 @@ ssize_t do_guest_readlink(const char *pathname, char *buf, size_t bufsiz)return ret; } +static ssize_t do_guest_readlinkat (int dirfd,const char *pathname, +char *buf,size_t bufsiz) +{ +const char *host_name =path (pathname); + g_autofree char *entry_path = proc_entry_path(dirfd, host_name); +ssize_t ret =readlinkat (dirfd, host_name, buf, bufsiz); + +if (ret >=0 && entry_path &&is_proc_myself (entry_path,"exe" )) { +return do_guest_readlink (entry_path, buf, bufsiz); + } +return ret; +} +static int do_execv (CPUArchState *cpu_env,int dirfd,abi_long pathname,abi_long guest_argp,abi_long guest_envp,int flags,bool is_execveat)@@ -11143,16 +11308,8 @@ static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1, }else if (!arg4) {/* Short circuit this for the magic exe check. */ ret = -TARGET_EINVAL ; - }else if (is_proc_myself ((const char *)p,"exe" )) { -/* -* Don't worry about sign mismatch as earlier mapping -* logic would have thrown a bad address error. -*/ - ret =MIN (strlen (real_exec_path), arg4); -/* We cannot NUL terminate the string. */ -memcpy (p2, real_exec_path, ret); }else { - ret =get_errno (readlinkat (arg1,path (p), p2, arg4)); + ret =get_errno (do_guest_readlinkat (arg1, p, p2, arg4)); }unlock_user (p2, arg3, ret);unlock_user (p, arg2,0 );tests/tcg/multiarch/linux/proc-openat.c new file +94
-0@@ -0,0 +1,94 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +#define _GNU_SOURCE +#include <assert.h> +#include <errno.h> +#include <fcntl.h> +#include <stdlib.h> +#include <string.h> +#include <sys/resource.h> +#include <unistd.h> + +static char expected[16384 ]; +static size_t expected_size; + +static void check (int dirfd,const char *name,int flags,int expected_errno) +{ +char actual[sizeof (expected)]; +size_t size =0 ; +int fd =openat (dirfd, name, flags); + +if (expected_errno) { +assert (fd ==-1 && errno == expected_errno); +return ; + } +assert (fd >=0 ); +assert ((fcntl (fd,F_GETFD ) &FD_CLOEXEC ) == + ((flags &O_CLOEXEC ) ?FD_CLOEXEC :0 )); +assert ((fcntl (fd,F_GETFL ) & (O_PATH |O_NOFOLLOW |O_ACCMODE )) == + (flags & (O_PATH |O_NOFOLLOW |O_ACCMODE ))); +if (flags &O_PATH ) { +assert (read (fd, actual,1 ) ==-1 && errno ==EBADF ); + }else { +for (;;) { +ssize_t n =read (fd, actual + size,sizeof (actual) - size); + +if (n <0 && errno ==EINTR ) { +continue ; + } +assert (n >=0 ); +if (!n) { +break ; + } + size += n; +assert (size <sizeof (actual)); + } +assert (size == expected_size && !memcmp (actual, expected, size)); +assert (write (fd,"x" ,1 ) ==-1 && errno ==EBADF ); + } +assert (close (fd) ==0 ); +} + +int main (int argc,char **argv) +{ +struct rlimit saved, limit; +int cwd =open ("." ,O_PATH |O_DIRECTORY |O_CLOEXEC ); +int proc =open ("/proc" ,O_PATH |O_DIRECTORY |O_CLOEXEC ); +int self =open ("/proc/self" ,O_PATH |O_DIRECTORY |O_CLOEXEC ); +int flags =O_RDONLY |O_CLOEXEC ; + +assert (cwd >=0 && proc >=0 && self >=0 ); +for (int i =0 ; i < argc; i++) { +size_t size =strlen (argv[i]) +1 ; + +assert (size <=sizeof (expected) - expected_size); +memcpy (expected + expected_size, argv[i], size); + expected_size += size; + } +check (AT_FDCWD ,"/proc/self/cmdline" , flags,0 ); +check (-1 ,"/proc/self/cmdline" , flags,0 ); +check (proc,"self/cmdline" , flags,0 ); +check (proc,"./self/cmdline" , flags,0 ); +check (self,"cmdline" , flags,0 ); +check (self,"cmdline" , flags |O_NOFOLLOW ,0 ); +check (self,"cmdline" ,O_PATH |O_CLOEXEC ,0 ); +check (self,"cmdline" , flags |O_DIRECTORY ,ENOTDIR ); +assert (chdir ("/proc" ) ==0 ); +check (AT_FDCWD ,"self/cmdline" , flags,0 ); +check (-1 ,"self/cmdline" , flags,EBADF ); +assert (fchdir (cwd) ==0 ); +assert (close (self) ==0 ); +assert (close (proc) ==0 ); +assert (close (cwd) ==0 ); +assert (getrlimit (RLIMIT_NOFILE , &saved) ==0 ); +int next =open ("/dev/null" ,O_RDONLY |O_CLOEXEC ); + +assert (next >=0 ); + limit = saved; + limit.rlim_cur = next +1 ; +assert (setrlimit (RLIMIT_NOFILE , &limit) ==0 ); +assert (close (next) ==0 ); +check (AT_FDCWD ,"/proc/self/cmdline" , flags,0 ); +check (AT_FDCWD ,"/proc/self/cmdline" , flags |O_NOFOLLOW ,0 ); +assert (setrlimit (RLIMIT_NOFILE , &saved) ==0 ); +return EXIT_SUCCESS ; +}tests/tcg/multiarch/linux/proc-openat2.c new file +83
-0@@ -0,0 +1,83 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +#define _GNU_SOURCE +#include <assert.h> +#include <errno.h> +#include <fcntl.h> +#include <stdint.h> +#include <stdlib.h> +#include <sys/syscall.h> +#include <unistd.h> + +#ifdef SYS_openat2 +#include <linux/openat2.h> + +static int cmdline_argc; +static char **cmdline_argv; + +static void check (int dirfd,const char *name,uint64_t flags, +uint64_t resolve,int expected_errno) +{ +struct open_how how = { .flags = flags, .resolve = resolve }; +char byte; +int fd =syscall (SYS_openat2, dirfd, name, &how,sizeof (how)); + +if (expected_errno) { +assert (fd ==-1 && errno == expected_errno); +return ; + } +assert (fd >=0 ); +assert (fcntl (fd,F_GETFD ) ==FD_CLOEXEC ); +if (flags &O_PATH ) { +assert (fcntl (fd,F_GETFL ) &O_PATH ); +assert (read (fd, &byte,1 ) ==-1 && errno ==EBADF ); + }else { +for (int i =0 ; i < cmdline_argc; i++) { +const char *p = cmdline_argv[i]; + +do { +assert (read (fd, &byte,1 ) ==1 && byte == *p); + }while (*p++); + } +assert (read (fd, &byte,1 ) ==0 ); +assert (write (fd,"x" ,1 ) ==-1 && errno ==EBADF ); + } +assert (close (fd) ==0 ); +} +#endif + +int main (int argc,char **argv) +{ +#ifdef SYS_openat2 +struct open_how how = { .flags =O_RDONLY |O_CLOEXEC }; +int fd =syscall (SYS_openat2,AT_FDCWD ,"/proc/self/cmdline" , + &how,sizeof (how)); +int proc, self, root, flags =O_RDONLY |O_CLOEXEC ; + + cmdline_argc = argc; + cmdline_argv = argv; +if (fd <0 && errno ==ENOSYS ) { +return EXIT_SUCCESS ; + } +assert (fd >=0 &&close (fd) ==0 ); + proc =open ("/proc" ,O_PATH |O_DIRECTORY |O_CLOEXEC ); + self =open ("/proc/self" ,O_PATH |O_DIRECTORY |O_CLOEXEC ); + root =open ("/" ,O_PATH |O_DIRECTORY |O_CLOEXEC ); +assert (proc >=0 && self >=0 && root >=0 ); +check (-1 ,"/proc/self/cmdline" , flags,0 ,0 ); +check (AT_FDCWD ,"/proc/self/cmdline" , flags, +RESOLVE_NO_SYMLINKS ,ELOOP ); +check (proc,"self/cmdline" , flags,0 ,0 ); +check (proc,"self/cmdline" , flags,RESOLVE_NO_SYMLINKS ,ELOOP ); +check (proc,"self/root/proc/self/cmdline" , flags, +RESOLVE_NO_MAGICLINKS ,ELOOP ); +check (self,"../self/cmdline" , flags,RESOLVE_BENEATH ,EXDEV ); +check (self,"../self/cmdline" , flags,RESOLVE_IN_ROOT ,ENOENT ); +check (root,"proc/self/cmdline" , flags,RESOLVE_NO_XDEV ,EXDEV ); +check (proc,"self/cmdline" , flags,UINT64_C (1 ) <<63 ,EINVAL ); +check (self,"cmdline" ,O_PATH |O_CLOEXEC ,0 ,0 ); +assert (close (root) ==0 ); +assert (close (self) ==0 ); +assert (close (proc) ==0 ); +#endif +return EXIT_SUCCESS ; +}tests/tcg/multiarch/meson.build +2
-1@@ -61,6 +61,8 @@ tests += { multiarch/'linux/linux-shmat-null.c': {}, multiarch/'linux/linux-sigrtminmax.c': {}, multiarch/'linux/linux-test.c': {}, + multiarch/'linux/proc-openat.c': {}, + multiarch/'linux/proc-openat2.c': {}, multiarch/'linux/test-vma.c': {}, } --2.55.0