| 1 | #include <unistd.h> |
| 2 | #include <signal.h> |
| 3 | #include "syscall.h" |
| 4 | #include "libc.h" |
| 5 | |
| 6 | struct ctx { |
| 7 | 	int id, eid, sid; |
| 8 | 	int nr, ret; |
| 9 | }; |
| 10 | |
| 11 | static void do_setxid(void *p) |
| 12 | { |
| 13 | 	struct ctx *c = p; |
| 14 | 	if (c->ret<0) return; |
| 15 | 	int ret = __syscall(c->nr, c->id, c->eid, c->sid); |
| 16 | 	if (ret && !c->ret) { |
| 17 | 		/* If one thread fails to set ids after another has already |
| 18 | 		 * succeeded, forcibly killing the process is the only safe |
| 19 | 		 * thing to do. State is inconsistent and dangerous. Use |
| 20 | 		 * SIGKILL because it is uncatchable. */ |
| 21 | 		__block_all_sigs(0); |
| 22 | 		__syscall(SYS_kill, __syscall(SYS_getpid), SIGKILL); |
| 23 | 	} |
| 24 | 	c->ret = ret; |
| 25 | } |
| 26 | |
| 27 | int __setxid(int nr, int id, int eid, int sid) |
| 28 | { |
| 29 | 	/* ret is initially nonzero so that failure of the first thread does not |
| 30 | 	 * trigger the safety kill above. */ |
| 31 | 	struct ctx c = { .nr = nr, .id = id, .eid = eid, .sid = sid, .ret = 1 }; |
| 32 | 	__synccall(do_setxid, &c); |
| 33 | 	return __syscall_ret(c.ret > 0 ? -EAGAIN : c.ret); |
| 34 | } |