faccessat.c 1.3 KB
Newer Older
R
Rich Felker 已提交
1
#include <unistd.h>
2
#include <fcntl.h>
3
#include <sys/wait.h>
R
Rich Felker 已提交
4
#include "syscall.h"
5 6 7 8 9 10 11 12 13 14 15 16 17
#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;
18 19
	if (__syscall(SYS_setregid, __syscall(SYS_getegid), -1)
	    || __syscall(SYS_setreuid, __syscall(SYS_geteuid), -1))
20 21 22
		__syscall(SYS_exit, 1);
	ret = __syscall(SYS_faccessat, c->fd, c->filename, c->amode, 0);
	__syscall(SYS_write, c->p, &ret, sizeof ret);
23
	return 0;
24
}
R
Rich Felker 已提交
25 26 27

int faccessat(int fd, const char *filename, int amode, int flag)
{
28 29 30 31 32 33 34 35
	if (!flag || (flag==AT_EACCESS && getuid()==geteuid() && getgid()==getegid()))
		return syscall(SYS_faccessat, fd, filename, amode, flag);

	if (flag != AT_EACCESS)
		return __syscall_ret(-EINVAL);

	char stack[1024];
	sigset_t set;
36 37
	pid_t pid;
	int status;
38 39
	int ret, p[2];

40
	if (pipe2(p, O_CLOEXEC)) return __syscall_ret(-EBUSY);
41 42
	struct ctx c = { .fd = fd, .filename = filename, .amode = amode, .p = p[1] };

43
	__block_all_sigs(&set);
44
	
45
	pid = __clone(checker, stack+sizeof stack, 0, &c);
46 47
	__syscall(SYS_close, p[1]);

48
	if (pid<0 || __syscall(SYS_read, p[0], &ret, sizeof ret) != sizeof(ret))
49 50
		ret = -EBUSY;
	__syscall(SYS_close, p[0]);
51
	__syscall(SYS_wait4, pid, &status, __WCLONE, 0);
52 53 54 55

	__restore_sigs(&set);

	return __syscall_ret(ret);
R
Rich Felker 已提交
56
}