blob: 0239f8afa95019ae9aa6dad5d79fcaf4eade139b [file] [log] [blame]
Rich Felkerafade232011-07-29 23:10:07 -04001#include <unistd.h>
2#include <errno.h>
Rich Felkerafade232011-07-29 23:10:07 -04003#include "syscall.h"
4#include "libc.h"
Rich Felker84b5c542015-01-12 18:16:32 -05005#include "pthread_impl.h"
Rich Felkerafade232011-07-29 23:10:07 -04006
7struct ctx {
8 int id, eid, sid;
Rich Felker84b5c542015-01-12 18:16:32 -05009 int nr, err;
Rich Felkerafade232011-07-29 23:10:07 -040010};
11
Rich Felkerafade232011-07-29 23:10:07 -040012static void do_setxid(void *p)
13{
14 struct ctx *c = p;
Rich Felker84b5c542015-01-12 18:16:32 -050015 if (c->err>0) return;
16 int ret = -__syscall(c->nr, c->id, c->eid, c->sid);
17 if (ret && !c->err) {
18 /* If one thread fails to set ids after another has already
19 * succeeded, forcibly killing the process is the only safe
20 * thing to do. State is inconsistent and dangerous. Use
21 * SIGKILL because it is uncatchable. */
22 __block_all_sigs(0);
23 __syscall(SYS_kill, __syscall(SYS_getpid), SIGKILL);
Rich Felkerafade232011-07-29 23:10:07 -040024 }
Rich Felker84b5c542015-01-12 18:16:32 -050025 c->err = ret;
Rich Felkerafade232011-07-29 23:10:07 -040026}
27
28int __setxid(int nr, int id, int eid, int sid)
29{
Rich Felker84b5c542015-01-12 18:16:32 -050030 /* err is initially nonzero so that failure of the first thread does not
31 * trigger the safety kill above. */
32 struct ctx c = { .nr = nr, .id = id, .eid = eid, .sid = sid, .err = -1 };
Rich Felkerafade232011-07-29 23:10:07 -040033 __synccall(do_setxid, &c);
34 if (c.err) {
Rich Felker472e8b72015-01-15 07:09:14 -050035 if (c.err>0) errno = c.err;
Rich Felkerafade232011-07-29 23:10:07 -040036 return -1;
37 }
38 return 0;
39}