From 8c1ab65c0f548d20b7f177bdb736daaf603340e1 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 17 Apr 2024 15:54:38 +0200 Subject: Adding upstream version 0.0~git20221206.8b7148f. Signed-off-by: Daniel Baumann --- libc-top-half/musl/src/unistd/faccessat.c | 61 +++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 libc-top-half/musl/src/unistd/faccessat.c (limited to 'libc-top-half/musl/src/unistd/faccessat.c') diff --git a/libc-top-half/musl/src/unistd/faccessat.c b/libc-top-half/musl/src/unistd/faccessat.c new file mode 100644 index 0000000..557503e --- /dev/null +++ b/libc-top-half/musl/src/unistd/faccessat.c @@ -0,0 +1,61 @@ +#include +#include +#include +#include "syscall.h" +#include "pthread_impl.h" + +struct ctx { + int fd; + const char *filename; + int amode; + int p; +}; + +static int checker(void *p) +{ + struct ctx *c = p; + int ret; + if (__syscall(SYS_setregid, __syscall(SYS_getegid), -1) + || __syscall(SYS_setreuid, __syscall(SYS_geteuid), -1)) + __syscall(SYS_exit, 1); + ret = __syscall(SYS_faccessat, c->fd, c->filename, c->amode, 0); + __syscall(SYS_write, c->p, &ret, sizeof ret); + return 0; +} + +int faccessat(int fd, const char *filename, int amode, int flag) +{ + if (flag) { + int ret = __syscall(SYS_faccessat2, fd, filename, amode, flag); + if (ret != -ENOSYS) return __syscall_ret(ret); + } + + if (flag & ~AT_EACCESS) + return __syscall_ret(-EINVAL); + + if (!flag || (getuid()==geteuid() && getgid()==getegid())) + return syscall(SYS_faccessat, fd, filename, amode); + + char stack[1024]; + sigset_t set; + pid_t pid; + int status; + int ret, p[2]; + + if (pipe2(p, O_CLOEXEC)) return __syscall_ret(-EBUSY); + struct ctx c = { .fd = fd, .filename = filename, .amode = amode, .p = p[1] }; + + __block_all_sigs(&set); + + pid = __clone(checker, stack+sizeof stack, 0, &c); + __syscall(SYS_close, p[1]); + + if (pid<0 || __syscall(SYS_read, p[0], &ret, sizeof ret) != sizeof(ret)) + ret = -EBUSY; + __syscall(SYS_close, p[0]); + __syscall(SYS_wait4, pid, &status, __WCLONE, 0); + + __restore_sigs(&set); + + return __syscall_ret(ret); +} -- cgit v1.2.3