-
Notifications
You must be signed in to change notification settings - Fork 345
/
utilities.c
281 lines (210 loc) · 5.38 KB
/
utilities.c
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/* Authors: Christopher Ashworth <[email protected]>
*
* Copyright (C) 2006 Tresys Technology, LLC
* Copyright (C) 2019 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/* The purpose of this file is to provide some functions commonly needed
* by our unit tests.
*/
#include "utilities.h"
int test_store_enabled = 0;
semanage_handle_t *sh = NULL;
/* Silence any error output caused by our tests
* by using this dummy function to catch messages.
*/
void test_msg_handler(void *varg, semanage_handle_t *handle, const char *fmt,
...)
{
}
int create_test_store(void) {
FILE *fptr;
if (mkdir("test-policy", 0700) < 0)
return -1;
if (mkdir("test-policy/store", 0700) < 0)
return -1;
if (mkdir("test-policy/store/active", 0700) < 0)
return -1;
if (mkdir("test-policy/store/active/modules", 0700) < 0)
return -1;
if (mkdir("test-policy/etc", 0700) < 0)
return -1;
if (mkdir("test-policy/etc/selinux", 0700) < 0)
return -1;
fptr = fopen("test-policy/etc/selinux/semanage.conf", "w+");
if (!fptr)
return -1;
fclose(fptr);
enable_test_store();
return 0;
}
void disable_test_store(void) {
test_store_enabled = 0;
}
void enable_test_store(void) {
test_store_enabled = 1;
}
static int write_test_policy(char *data, size_t data_len) {
FILE *fptr = fopen("test-policy/store/active/policy.kern", "wb+");
if (!fptr) {
perror("fopen");
return -1;
}
if (fwrite(data, data_len, 1, fptr) != 1) {
perror("fwrite");
fclose(fptr);
return -1;
}
fclose(fptr);
return 0;
}
int write_test_policy_from_file(const char *filename) {
char *buf = NULL;
size_t len = 0;
FILE *fptr = fopen(filename, "rb");
int rc;
if (!fptr) {
perror("fopen");
return -1;
}
fseek(fptr, 0, SEEK_END);
len = ftell(fptr);
fseek(fptr, 0, SEEK_SET);
buf = (char *) malloc(len);
if (!buf) {
perror("malloc");
fclose(fptr);
return -1;
}
fread(buf, len, 1, fptr);
fclose(fptr);
rc = write_test_policy(buf, len);
free(buf);
return rc;
}
int write_test_policy_src(unsigned char *data, unsigned int data_len) {
if (mkdir("test-policy/store/active/modules/100", 0700) < 0)
return -1;
if (mkdir("test-policy/store/active/modules/100/base", 0700) < 0)
return -1;
FILE *fptr = fopen("test-policy/store/active/modules/100/base/cil",
"w+");
if (!fptr) {
perror("fopen");
return -1;
}
if (fwrite(data, data_len, 1, fptr) != 1) {
perror("fwrite");
fclose(fptr);
return -1;
}
fclose(fptr);
fptr = fopen("test-policy/store/active/modules/100/base/lang_ext",
"w+");
if (!fptr) {
perror("fopen");
return -1;
}
if (fwrite("cil", sizeof("cil"), 1, fptr) != 1) {
perror("fwrite");
fclose(fptr);
return -1;
}
fclose(fptr);
return 0;
}
int destroy_test_store(void) {
FTS *ftsp = NULL;
FTSENT *curr = NULL;
int ret = 0;
disable_test_store();
char *files[] = { (char *) "test-policy", NULL };
ftsp = fts_open(files, FTS_NOCHDIR | FTS_PHYSICAL | FTS_XDEV, NULL);
if (!ftsp)
return -1;
while ((curr = fts_read(ftsp)))
switch (curr->fts_info) {
case FTS_DP:
case FTS_F:
case FTS_SL:
case FTS_SLNONE:
case FTS_DEFAULT:
if (remove(curr->fts_accpath) < 0)
ret = -1;
default:
break;
}
fts_close(ftsp);
return ret;
}
void helper_handle_create(void) {
if (test_store_enabled)
semanage_set_root("test-policy");
sh = semanage_handle_create();
CU_ASSERT_PTR_NOT_NULL(sh);
semanage_msg_set_callback(sh, test_msg_handler, NULL);
if (test_store_enabled) {
semanage_set_create_store(sh, 1);
semanage_set_reload(sh, 0);
semanage_set_store_root(sh, "");
semanage_select_store(sh, (char *) "store",
SEMANAGE_CON_DIRECT);
}
}
void helper_handle_destroy(void) {
semanage_handle_destroy(sh);
}
void helper_connect(void) {
CU_ASSERT(semanage_connect(sh) >= 0);
}
void helper_disconnect(void) {
CU_ASSERT(semanage_disconnect(sh) >= 0);
}
void helper_begin_transaction(void) {
CU_ASSERT(semanage_begin_transaction(sh) >= 0);
}
void helper_commit(void) {
CU_ASSERT(semanage_commit(sh) >= 0);
}
void setup_handle(level_t level) {
if (level >= SH_NULL)
sh = NULL;
if (level >= SH_HANDLE)
helper_handle_create();
if (level >= SH_CONNECT)
helper_connect();
if (level >= SH_TRANS)
helper_begin_transaction();
}
void cleanup_handle(level_t level) {
if (level >= SH_TRANS)
helper_commit();
if (level >= SH_CONNECT)
helper_disconnect();
if (level >= SH_HANDLE)
helper_handle_destroy();
if (level >= SH_NULL)
sh = NULL;
}
void setup_handle_invalid_store(level_t level) {
CU_ASSERT(level >= SH_HANDLE);
helper_handle_create();
semanage_select_store(sh, (char *) "", SEMANAGE_CON_INVALID);
if (level >= SH_CONNECT)
helper_connect();
if (level >= SH_TRANS)
helper_begin_transaction();
}