X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=src%2Funistd%2Fsetxid.c;h=0239f8afa95019ae9aa6dad5d79fcaf4eade139b;hb=e95538fa07d2b460b25ee6c2fef05f820888776d;hp=d3bfaf6269bb6e65c8f1f2333a0ee47d0b4b31f0;hpb=afade2356ea148e715307be8f7334b790282341e;p=musl diff --git a/src/unistd/setxid.c b/src/unistd/setxid.c index d3bfaf62..0239f8af 100644 --- a/src/unistd/setxid.c +++ b/src/unistd/setxid.c @@ -1,48 +1,38 @@ #include #include -#include #include "syscall.h" #include "libc.h" +#include "pthread_impl.h" struct ctx { int id, eid, sid; - int nr, rlim, err; + int nr, err; }; -/* We jump through hoops to eliminate the possibility of partial failures. */ - static void do_setxid(void *p) { struct ctx *c = p; - if (c->err) return; - if (c->rlim && c->id >= 0 && c->id != getuid()) { - struct rlimit inf = { RLIM_INFINITY, RLIM_INFINITY }, old; - getrlimit(RLIMIT_NPROC, &old); - if (setrlimit(RLIMIT_NPROC, &inf) && libc.threads_minus_1) { - c->err = errno; - return; - } - if (__syscall(c->nr, c->id, c->eid, c->sid)) - c->err = errno; - setrlimit(RLIMIT_NPROC, &old); - return; + if (c->err>0) return; + int ret = -__syscall(c->nr, c->id, c->eid, c->sid); + if (ret && !c->err) { + /* If one thread fails to set ids after another has already + * succeeded, forcibly killing the process is the only safe + * thing to do. State is inconsistent and dangerous. Use + * SIGKILL because it is uncatchable. */ + __block_all_sigs(0); + __syscall(SYS_kill, __syscall(SYS_getpid), SIGKILL); } - if (__syscall(c->nr, c->id, c->eid, c->sid)) - c->err = errno; + c->err = ret; } int __setxid(int nr, int id, int eid, int sid) { - struct ctx c = { .nr = nr, .id = id, .eid = eid, .sid = sid }; - switch (nr) { - case SYS_setuid: - case SYS_setreuid: - case SYS_setresuid: - c.rlim = 1; - } + /* err is initially nonzero so that failure of the first thread does not + * trigger the safety kill above. */ + struct ctx c = { .nr = nr, .id = id, .eid = eid, .sid = sid, .err = -1 }; __synccall(do_setxid, &c); if (c.err) { - errno = c.err; + if (c.err>0) errno = c.err; return -1; } return 0;