1 | #include <unistd.h>
|
---|
2 | #include <fcntl.h>
|
---|
3 | #include <errno.h>
|
---|
4 | #include <sys/wait.h>
|
---|
5 | #include "syscall.h"
|
---|
6 | #include "pthread_impl.h"
|
---|
7 |
|
---|
8 | struct ctx {
|
---|
9 | int fd;
|
---|
10 | const char *filename;
|
---|
11 | int amode;
|
---|
12 | };
|
---|
13 |
|
---|
14 | static const int errors[] = {
|
---|
15 | 0, -EACCES, -ELOOP, -ENAMETOOLONG, -ENOENT, -ENOTDIR,
|
---|
16 | -EROFS, -EBADF, -EINVAL, -ETXTBSY,
|
---|
17 | -EFAULT, -EIO, -ENOMEM,
|
---|
18 | -EBUSY
|
---|
19 | };
|
---|
20 |
|
---|
21 | static int checker(void *p)
|
---|
22 | {
|
---|
23 | struct ctx *c = p;
|
---|
24 | int ret;
|
---|
25 | int i;
|
---|
26 | if (__syscall(SYS_setregid, __syscall(SYS_getegid), -1)
|
---|
27 | || __syscall(SYS_setreuid, __syscall(SYS_geteuid), -1))
|
---|
28 | __syscall(SYS_exit, 1);
|
---|
29 | ret = __syscall(SYS_faccessat, c->fd, c->filename, c->amode, 0);
|
---|
30 | for (i=0; i < sizeof errors/sizeof *errors - 1 && ret!=errors[i]; i++);
|
---|
31 | return i;
|
---|
32 | }
|
---|
33 |
|
---|
34 | int faccessat(int fd, const char *filename, int amode, int flag)
|
---|
35 | {
|
---|
36 | if (!flag || (flag==AT_EACCESS && getuid()==geteuid() && getgid()==getegid()))
|
---|
37 | return syscall(SYS_faccessat, fd, filename, amode, flag);
|
---|
38 |
|
---|
39 | if (flag != AT_EACCESS)
|
---|
40 | return __syscall_ret(-EINVAL);
|
---|
41 |
|
---|
42 | char stack[1024];
|
---|
43 | sigset_t set;
|
---|
44 | pid_t pid;
|
---|
45 | int ret = -EBUSY;
|
---|
46 | struct ctx c = { .fd = fd, .filename = filename, .amode = amode };
|
---|
47 |
|
---|
48 | __block_all_sigs(&set);
|
---|
49 |
|
---|
50 | pid = __clone(checker, stack+sizeof stack, 0, &c);
|
---|
51 | if (pid > 0) {
|
---|
52 | int status;
|
---|
53 | do {
|
---|
54 | __syscall(SYS_wait4, pid, &status, __WCLONE, 0);
|
---|
55 | } while (!WIFEXITED(status) && !WIFSIGNALED(status));
|
---|
56 | if (WIFEXITED(status))
|
---|
57 | ret = errors[WEXITSTATUS(status)];
|
---|
58 | }
|
---|
59 |
|
---|
60 | __restore_sigs(&set);
|
---|
61 |
|
---|
62 | return __syscall_ret(ret);
|
---|
63 | }
|
---|