summaryrefslogtreecommitdiff
path: root/src/unistd/setxid.c
blob: 2f651a110eb64f15f68c34a0be464466d2fa11c1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <unistd.h>
#include <errno.h>
#include <sys/resource.h>
#include "syscall.h"
#include "libc.h"

struct ctx {
	int id, eid, sid;
	int nr, rlim, err;
};

/* We jump through hoops to eliminate the possibility of partial failures. */

int __setrlimit(int, const struct rlimit *);

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 ((c->err = -__setrlimit(RLIMIT_NPROC, &inf)) && libc.threads_minus_1)
			return;
		c->err = -__syscall(c->nr, c->id, c->eid, c->sid);
		__setrlimit(RLIMIT_NPROC, &old);
		return;
	}
	c->err = -__syscall(c->nr, c->id, c->eid, c->sid);
}

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;
	}
	__synccall(do_setxid, &c);
	if (c.err) {
		errno = c.err;
		return -1;
	}
	return 0;
}