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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
|
/* Workaround for http://bugs.python.org/issue4835 */
#ifndef SIZEOF_SOCKET_T
#define SIZEOF_SOCKET_T SIZEOF_INT
#endif
#include <Python.h>
#include <unistd.h>
#include <stdlib.h>
#include <ctype.h>
#include <errno.h>
#include <getopt.h>
#include <limits.h>
#include <sepol/sepol.h>
#include <sepol/policydb.h>
#include <sepol/policydb/services.h>
#include <selinux/selinux.h>
#define UNKNOWN -1
#define BADSCON -2
#define BADTCON -3
#define BADTCLASS -4
#define BADPERM -5
#define BADCOMPUTE -6
#define NOPOLICY -7
#define ALLOW 0
#define DONTAUDIT 1
#define TERULE 2
#define BOOLEAN 3
#define CONSTRAINT 4
#define RBAC 5
struct boolean_t {
char *name;
int active;
};
static struct boolean_t **boollist = NULL;
static int boolcnt = 0;
struct avc_t {
sepol_handle_t *handle;
sepol_policydb_t *policydb;
sepol_security_id_t ssid;
sepol_security_id_t tsid;
sepol_security_class_t tclass;
sepol_access_vector_t av;
};
static struct avc_t *avc = NULL;
static sidtab_t sidtab;
static int load_booleans(const sepol_bool_t * boolean,
void *arg __attribute__ ((__unused__)))
{
boollist[boolcnt] = malloc(sizeof(struct boolean_t));
boollist[boolcnt]->name = strdup(sepol_bool_get_name(boolean));
boollist[boolcnt]->active = sepol_bool_get_value(boolean);
boolcnt++;
return 0;
}
static int check_booleans(struct boolean_t **bools)
{
char errormsg[PATH_MAX];
struct sepol_av_decision avd;
unsigned int reason;
int rc;
int i;
sepol_bool_key_t *key = NULL;
sepol_bool_t *boolean = NULL;
int fcnt = 0;
int *foundlist = calloc(boolcnt, sizeof(int));
if (!foundlist) {
PyErr_SetString( PyExc_MemoryError, "Out of memory\n");
return fcnt;
}
for (i = 0; i < boolcnt; i++) {
char *name = boollist[i]->name;
int active = boollist[i]->active;
rc = sepol_bool_key_create(avc->handle, name, &key);
if (rc < 0) {
PyErr_SetString( PyExc_RuntimeError,
"Could not create boolean key.\n");
break;
}
rc = sepol_bool_query(avc->handle,
avc->policydb,
key, &boolean);
if (rc < 0) {
snprintf(errormsg, sizeof(errormsg),
"Could not find boolean %s.\n", name);
PyErr_SetString( PyExc_RuntimeError, errormsg);
break;
}
sepol_bool_set_value(boolean, !active);
rc = sepol_bool_set(avc->handle,
avc->policydb,
key, boolean);
if (rc < 0) {
snprintf(errormsg, sizeof(errormsg),
"Could not set boolean data %s.\n", name);
PyErr_SetString( PyExc_RuntimeError, errormsg);
break;
}
/* Reproduce the computation. */
rc = sepol_compute_av_reason(avc->ssid, avc->tsid, avc->tclass,
avc->av, &avd, &reason);
if (rc < 0) {
snprintf(errormsg, sizeof(errormsg),
"Error during access vector computation, skipping...");
PyErr_SetString( PyExc_RuntimeError, errormsg);
sepol_bool_free(boolean);
break;
} else {
if (!reason) {
foundlist[fcnt] = i;
fcnt++;
}
sepol_bool_set_value(boolean, active);
rc = sepol_bool_set(avc->handle,
avc->policydb, key,
boolean);
if (rc < 0) {
snprintf(errormsg, sizeof(errormsg),
"Could not set boolean data %s.\n",
name);
PyErr_SetString( PyExc_RuntimeError, errormsg);
break;
}
}
sepol_bool_free(boolean);
sepol_bool_key_free(key);
key = NULL;
boolean = NULL;
}
if (key)
sepol_bool_key_free(key);
if (boolean)
sepol_bool_free(boolean);
if (fcnt > 0) {
*bools = calloc(sizeof(struct boolean_t), fcnt + 1);
struct boolean_t *b = *bools;
for (i = 0; i < fcnt; i++) {
int ctr = foundlist[i];
b[i].name = strdup(boollist[ctr]->name);
b[i].active = !boollist[ctr]->active;
}
}
free(foundlist);
return fcnt;
}
static PyObject *finish(PyObject *self __attribute__((unused)), PyObject *args) {
PyObject *result = 0;
if (PyArg_ParseTuple(args,(char *)":finish")) {
int i = 0;
if (! avc)
Py_RETURN_NONE;
for (i = 0; i < boolcnt; i++) {
free(boollist[i]->name);
free(boollist[i]);
}
free(boollist);
sepol_sidtab_shutdown(&sidtab);
sepol_sidtab_destroy(&sidtab);
sepol_policydb_free(avc->policydb);
sepol_handle_destroy(avc->handle);
free(avc);
avc = NULL;
boollist = NULL;
boolcnt = 0;
/* Boilerplate to return "None" */
Py_RETURN_NONE;
}
return result;
}
static int __policy_init(const char *init_path)
{
FILE *fp;
char path[PATH_MAX];
char errormsg[PATH_MAX];
struct sepol_policy_file *pf = NULL;
int rc;
unsigned int cnt;
path[PATH_MAX-1] = '\0';
if (init_path) {
strncpy(path, init_path, PATH_MAX-1);
fp = fopen(path, "r");
if (!fp) {
snprintf(errormsg, sizeof(errormsg),
"unable to open %s: %s\n",
path, strerror(errno));
PyErr_SetString( PyExc_ValueError, errormsg);
return 1;
}
} else {
fp = fopen(selinux_current_policy_path(), "r");
if (!fp) {
snprintf(errormsg, sizeof(errormsg),
"unable to open %s: %s\n",
selinux_current_policy_path(),
strerror(errno));
PyErr_SetString( PyExc_ValueError, errormsg);
return 1;
}
}
avc = calloc(sizeof(struct avc_t), 1);
if (!avc) {
PyErr_SetString( PyExc_MemoryError, "Out of memory\n");
fclose(fp);
return 1;
}
/* Set up a policydb directly so that we can mutate it later
for testing what booleans might have allowed the access.
Otherwise, we'd just use sepol_set_policydb_from_file() here. */
if (sepol_policy_file_create(&pf) ||
sepol_policydb_create(&avc->policydb)) {
snprintf(errormsg, sizeof(errormsg),
"policydb_init failed: %s\n", strerror(errno));
PyErr_SetString( PyExc_RuntimeError, errormsg);
fclose(fp);
return 1;
}
sepol_policy_file_set_fp(pf, fp);
if (sepol_policydb_read(avc->policydb, pf)) {
snprintf(errormsg, sizeof(errormsg),
"invalid binary policy %s\n", path);
PyErr_SetString( PyExc_ValueError, errormsg);
fclose(fp);
return 1;
}
fclose(fp);
sepol_set_policydb(&avc->policydb->p);
avc->handle = sepol_handle_create();
/* Turn off messages */
sepol_msg_set_callback(avc->handle, NULL, NULL);
rc = sepol_bool_count(avc->handle,
avc->policydb, &cnt);
if (rc < 0) {
PyErr_SetString( PyExc_RuntimeError, "unable to get bool count\n");
return 1;
}
boollist = calloc(cnt, sizeof(*boollist));
if (!boollist) {
PyErr_SetString( PyExc_MemoryError, "Out of memory\n");
return 1;
}
sepol_bool_iterate(avc->handle, avc->policydb,
load_booleans, (void *)NULL);
/* Initialize the sidtab for subsequent use by sepol_context_to_sid
and sepol_compute_av_reason. */
rc = sepol_sidtab_init(&sidtab);
if (rc < 0) {
PyErr_SetString( PyExc_RuntimeError, "unable to init sidtab\n");
free(boollist);
return 1;
}
sepol_set_sidtab(&sidtab);
return 0;
}
static PyObject *init(PyObject *self __attribute__((unused)), PyObject *args) {
int result;
char *init_path=NULL;
if (avc) {
PyErr_SetString( PyExc_RuntimeError, "init called multiple times");
return NULL;
}
if (!PyArg_ParseTuple(args,(char *)"|s:policy_init",&init_path))
return NULL;
result = __policy_init(init_path);
return Py_BuildValue("i", result);
}
#define RETURN(X) \
{ \
return Py_BuildValue("iO", (X), Py_None); \
}
static PyObject *analyze(PyObject *self __attribute__((unused)) , PyObject *args) {
char *reason_buf = NULL;
security_context_t scon;
security_context_t tcon;
char *tclassstr;
PyObject *listObj;
PyObject *strObj;
int numlines;
struct boolean_t *bools;
unsigned int reason;
sepol_security_id_t ssid, tsid;
sepol_security_class_t tclass;
sepol_access_vector_t perm, av;
struct sepol_av_decision avd;
int rc;
int i=0;
if (!PyArg_ParseTuple(args,(char *)"sssO!:audit2why",&scon,&tcon,&tclassstr,&PyList_Type, &listObj))
return NULL;
/* get the number of lines passed to us */
numlines = PyList_Size(listObj);
/* should raise an error here. */
if (numlines < 0) return NULL; /* Not a list */
if (!avc)
RETURN(NOPOLICY)
rc = sepol_context_to_sid(scon, strlen(scon) + 1, &ssid);
if (rc < 0)
RETURN(BADSCON)
rc = sepol_context_to_sid(tcon, strlen(tcon) + 1, &tsid);
if (rc < 0)
RETURN(BADTCON)
tclass = string_to_security_class(tclassstr);
if (!tclass)
RETURN(BADTCLASS)
/* Convert the permission list to an AV. */
av = 0;
/* iterate over items of the list, grabbing strings, and parsing
for numbers */
for (i=0; i<numlines; i++){
char *permstr;
/* grab the string object from the next element of the list */
strObj = PyList_GetItem(listObj, i); /* Can't fail */
/* make it a string */
#if PY_MAJOR_VERSION >= 3
permstr = _PyUnicode_AsString( strObj );
#else
permstr = PyString_AsString( strObj );
#endif
perm = string_to_av_perm(tclass, permstr);
if (!perm)
RETURN(BADPERM)
av |= perm;
}
/* Reproduce the computation. */
rc = sepol_compute_av_reason_buffer(ssid, tsid, tclass, av, &avd, &reason, &reason_buf, 0);
if (rc < 0)
RETURN(BADCOMPUTE)
if (!reason)
RETURN(ALLOW)
if (reason & SEPOL_COMPUTEAV_TE) {
avc->ssid = ssid;
avc->tsid = tsid;
avc->tclass = tclass;
avc->av = av;
if (check_booleans(&bools) == 0) {
if (av & ~avd.auditdeny) {
RETURN(DONTAUDIT)
} else {
RETURN(TERULE)
}
} else {
PyObject *outboollist;
struct boolean_t *b = bools;
int len=0;
while (b->name) {
len++; b++;
}
b = bools;
outboollist = PyList_New(len);
len=0;
while(b->name) {
PyObject *bool_ = Py_BuildValue("(si)", b->name, b->active);
PyList_SetItem(outboollist, len++, bool_);
b++;
}
free(bools);
/* 'N' steals the reference to outboollist */
return Py_BuildValue("iN", BOOLEAN, outboollist);
}
}
if (reason & SEPOL_COMPUTEAV_CONS) {
if (reason_buf) {
PyObject *result = NULL;
result = Py_BuildValue("is", CONSTRAINT, reason_buf);
free(reason_buf);
return result;
}
RETURN(CONSTRAINT)
}
if (reason & SEPOL_COMPUTEAV_RBAC)
RETURN(RBAC)
RETURN(BADCOMPUTE)
}
static PyMethodDef audit2whyMethods[] = {
{"init", init, METH_VARARGS,
"Initialize policy database."},
{"analyze", analyze, METH_VARARGS,
"Analyze AVC."},
{"finish", finish, METH_VARARGS,
"Finish using policy, free memory."},
{NULL, NULL, 0, NULL} /* Sentinel */
};
#if PY_MAJOR_VERSION >= 3
/* Module-initialization logic specific to Python 3 */
struct module_state {
/* empty for now */
};
static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT,
"audit2why",
NULL,
sizeof(struct module_state),
audit2whyMethods,
NULL,
NULL,
NULL,
NULL
};
PyMODINIT_FUNC PyInit_audit2why(void); /* silence -Wmissing-prototypes */
PyMODINIT_FUNC PyInit_audit2why(void)
#else
PyMODINIT_FUNC initaudit2why(void); /* silence -Wmissing-prototypes */
PyMODINIT_FUNC initaudit2why(void)
#endif
{
PyObject *m;
#if PY_MAJOR_VERSION >= 3
m = PyModule_Create(&moduledef);
if (m == NULL) {
return NULL;
}
#else
m = Py_InitModule("audit2why", audit2whyMethods);
#endif
PyModule_AddIntConstant(m,"UNKNOWN", UNKNOWN);
PyModule_AddIntConstant(m,"BADSCON", BADSCON);
PyModule_AddIntConstant(m,"BADTCON", BADTCON);
PyModule_AddIntConstant(m,"BADTCLASS", BADTCLASS);
PyModule_AddIntConstant(m,"BADPERM", BADPERM);
PyModule_AddIntConstant(m,"BADCOMPUTE", BADCOMPUTE);
PyModule_AddIntConstant(m,"NOPOLICY", NOPOLICY);
PyModule_AddIntConstant(m,"ALLOW", ALLOW);
PyModule_AddIntConstant(m,"DONTAUDIT", DONTAUDIT);
PyModule_AddIntConstant(m,"TERULE", TERULE);
PyModule_AddIntConstant(m,"BOOLEAN", BOOLEAN);
PyModule_AddIntConstant(m,"CONSTRAINT", CONSTRAINT);
PyModule_AddIntConstant(m,"RBAC", RBAC);
#if PY_MAJOR_VERSION >= 3
return m;
#endif
}
|