1 /*	$OpenBSD: setuid_regress.h,v 1.1 2014/08/27 07:36:14 blambert Exp $	*/
2 /*
3  *	Written by Bret Stephen Lambert <blambert@openbsd.org> 2014
4  *	Public Domain.
5  */
6 
7 #ifndef	_SETUID_REGRESS_H_
8 #define	_SETUID_REGRESS_H_
9 
10 #define	_SETUID_REGRESS_USER	"nobody"
11 
12 static inline int
read_kproc_pid(struct kinfo_proc * kproc,pid_t pid)13 read_kproc_pid(struct kinfo_proc *kproc, pid_t pid)
14 {
15 	int			 args[6];
16 	size_t			 size;
17 
18 	args[0] = CTL_KERN;
19 	args[1] = KERN_PROC;
20 	args[2] = KERN_PROC_PID;
21 	args[3] = pid;
22 	args[4] = sizeof(*kproc);
23 	args[5] = 1;
24 
25 	size = sizeof(*kproc);
26 	return (sysctl(args, 6, kproc, &size, NULL, 0));
27 }
28 
29 static inline void
checkuids(uid_t truid,uid_t teuid,uid_t tsuid,const char * str)30 checkuids(uid_t truid, uid_t teuid, uid_t tsuid, const char *str)
31 {
32 	uid_t			 ruid, euid, suid;
33 
34 	if (getresuid(&ruid, &euid, &suid) == -1)
35 		err(1, "getresuid %s", str);
36 
37 	if (ruid != truid)
38 		errx(1, "real uid incorrectly set %s: is %u should be %u",
39 		    str, ruid, truid);
40 	if (euid != teuid)
41 		errx(1, "effective uid incorrectly set %s: is %u should be %u",
42 		    str, euid, teuid);
43 	if (suid != tsuid)
44 		errx(1, "saved uid incorrectly set %s: is %u should be %u",
45 		    str, suid, tsuid);
46 }
47 
48 void
checkgids(gid_t trgid,gid_t tegid,gid_t tsgid,const char * str)49 checkgids(gid_t trgid, gid_t tegid, gid_t tsgid, const char *str)
50 {
51 	gid_t			rgid, egid, sgid;
52 
53 	if (getresgid(&rgid, &egid, &sgid) == -1)
54 		err(1, "getresgid %s", str);
55 
56 	if (rgid != trgid)
57 		errx(1, "real gid incorrectly set %s: is %u should be %u",
58 		    str, rgid, trgid);
59 	if (egid != tegid)
60 		errx(1, "effective gid incorrectly set %s: is %u should be %u",
61 		    str, egid, tegid);
62 	if (sgid != tsgid)
63 		errx(1, "saved gid incorrectly set %s: is %u should be %u",
64 		    str, sgid, tsgid);
65 }
66 #endif
67