File: test_syscall_gid.c

package info (click to toggle)
uid-wrapper 1.2.4%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 476 kB
  • sloc: ansic: 5,244; makefile: 48; sh: 11
file content (145 lines) | stat: -rw-r--r-- 2,823 bytes parent folder | download | duplicates (4)
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include "config.h"

#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>

#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>

#include <grp.h>

#ifdef HAVE_SYS_SYSCALL_H
#include <sys/syscall.h>
#endif
#ifdef HAVE_SYSCALL_H
#include <syscall.h>
#endif

#define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))

static void test_uwrap_syscall_setgid(void **state)
{
	long int rc;
	gid_t g;

	(void) state; /* unused */

	rc = syscall(SYS_setgid, 1);
	assert_int_equal(rc, 0);

	g = getgid();
	assert_int_equal(g, 1);
#ifdef __alpha__
	assert_int_equal(g, syscall(SYS_getxgid));
#else
	assert_int_equal(g, syscall(SYS_getgid));
#endif
}

static void test_uwrap_syscall_setregid(void **state)
{
	long int rc;
	gid_t g;

	(void) state; /* unused */

	rc = syscall(SYS_setregid, 2, 0);
	assert_int_equal(rc, 0);

	g = getegid();
	assert_int_equal(g, 0);
#ifdef SYS_getegid /* not available on Solaris */
	assert_int_equal(g, syscall(SYS_getegid));
#endif

	rc = syscall(SYS_setregid, -1, 42);
	assert_int_equal(rc, 0);

	g = getgid();
	assert_int_equal(g, 2);
	g = getegid();
	assert_int_equal(g, 42);
}

#if defined(SYS_setresgid)
static void test_uwrap_syscall_setresgid(void **state)
{
	long int rc;
	gid_t g;
	gid_t g_r, g_e, g_s;

	(void) state; /* unused */

	rc = syscall(SYS_setresgid, 42, 0, -1);
	assert_int_equal(rc, 0);

	g = getegid();
	assert_int_equal(g, 0);

#ifdef SYS_getresgid /* not available on Solaris */
	rc = syscall(SYS_getresgid, &g_r, &g_e, &g_s);
	assert_return_code(rc, errno);

	assert_int_equal(g_r, 42);
	assert_int_equal(g, g_e);
#endif

	rc = syscall(SYS_setregid, -1, 42);
	assert_return_code(rc, errno);

	g = getegid();
	assert_int_equal(g, 42);
}
#endif

#if defined(SYS_setgroups)
static void test_uwrap_syscall_setgroups(void **state)
{
	gid_t glist[] = { 100, 200, 300, 400, 500 };
	gid_t rlist[16];
	int rc = -1;

	(void) state; /* unused */

	rc = syscall(SYS_setgroups, ARRAY_SIZE(glist), glist);
	assert_int_equal(rc, 0);

	rc = getgroups(ARRAY_SIZE(rlist), rlist);
	assert_int_equal(rc, 5);

	assert_memory_equal(glist, rlist, sizeof(glist));

	/* Drop all supplementary groups. This is often done by daemons */
	memset(rlist, 0, sizeof(rlist));
	rc = syscall(SYS_setgroups, 0, NULL);
	assert_int_equal(rc, 0);

	rc = getgroups(ARRAY_SIZE(rlist), rlist);
	assert_int_equal(rc, 0);

	assert_int_equal(rlist[0], 0);
}
#endif

int main(void) {
	int rc;

	const struct CMUnitTest uwrap_tests[] = {
		cmocka_unit_test(test_uwrap_syscall_setgid),
		cmocka_unit_test(test_uwrap_syscall_setregid),
#if defined(SYS_setresgid)
		cmocka_unit_test(test_uwrap_syscall_setresgid),
#endif
#if defined(SYS_setgroups)
		cmocka_unit_test(test_uwrap_syscall_setgroups),
#endif
	};

	rc = cmocka_run_group_tests(uwrap_tests, NULL, NULL);

	return rc;
}