tzh
2024-08-22 c7d0944258c7d0943aa7b2211498fd612971ce27
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
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
 
#include "debug.h"
#include "context.h"
#include "handle.h"
 
#include <sepol/policydb/policydb.h>
#include "node_internal.h"
 
/* Create a low level node structure from
 * a high level representation */
static int node_from_record(sepol_handle_t * handle,
               const policydb_t * policydb,
               ocontext_t ** node, const sepol_node_t * data)
{
 
   ocontext_t *tmp_node = NULL;
   context_struct_t *tmp_con = NULL;
   char *addr_buf = NULL, *mask_buf = NULL;
 
   tmp_node = (ocontext_t *) calloc(1, sizeof(ocontext_t));
   if (!tmp_node)
       goto omem;
 
   size_t addr_bsize, mask_bsize;
 
   /* Address and netmask */
   if (sepol_node_get_addr_bytes(handle, data, &addr_buf, &addr_bsize) < 0)
       goto err;
   if (sepol_node_get_mask_bytes(handle, data, &mask_buf, &mask_bsize) < 0)
       goto err;
 
   int proto = sepol_node_get_proto(data);
 
   switch (proto) {
   case SEPOL_PROTO_IP4:
       memcpy(&tmp_node->u.node.addr, addr_buf, addr_bsize);
       memcpy(&tmp_node->u.node.mask, mask_buf, mask_bsize);
       break;
   case SEPOL_PROTO_IP6:
       memcpy(tmp_node->u.node6.addr, addr_buf, addr_bsize);
       memcpy(tmp_node->u.node6.mask, mask_buf, mask_bsize);
       break;
   default:
       ERR(handle, "unsupported protocol %u", proto);
       goto err;
   }
   free(addr_buf);
   free(mask_buf);
   addr_buf = NULL;
   mask_buf = NULL;
 
   /* Context */
   if (context_from_record(handle, policydb, &tmp_con,
               sepol_node_get_con(data)) < 0)
       goto err;
   context_cpy(&tmp_node->context[0], tmp_con);
   context_destroy(tmp_con);
   free(tmp_con);
   tmp_con = NULL;
 
   *node = tmp_node;
   return STATUS_SUCCESS;
 
      omem:
   ERR(handle, "out of memory");
 
      err:
   if (tmp_node != NULL) {
       context_destroy(&tmp_node->context[0]);
       free(tmp_node);
   }
   context_destroy(tmp_con);
   free(tmp_con);
   free(addr_buf);
   free(mask_buf);
   ERR(handle, "could not create node structure");
   return STATUS_ERR;
}
 
static int node_to_record(sepol_handle_t * handle,
             const policydb_t * policydb,
             ocontext_t * node, int proto, sepol_node_t ** record)
{
 
   context_struct_t *con = &node->context[0];
 
   sepol_context_t *tmp_con = NULL;
   sepol_node_t *tmp_record = NULL;
 
   if (sepol_node_create(handle, &tmp_record) < 0)
       goto err;
 
   sepol_node_set_proto(tmp_record, proto);
 
   switch (proto) {
 
   case SEPOL_PROTO_IP4:
       if (sepol_node_set_addr_bytes(handle, tmp_record,
                         (const char *)&node->u.node.addr,
                         4) < 0)
           goto err;
 
       if (sepol_node_set_mask_bytes(handle, tmp_record,
                         (const char *)&node->u.node.mask,
                         4) < 0)
           goto err;
       break;
 
   case SEPOL_PROTO_IP6:
       if (sepol_node_set_addr_bytes(handle, tmp_record,
                         (const char *)&node->u.node6.addr,
                         16) < 0)
           goto err;
 
       if (sepol_node_set_mask_bytes(handle, tmp_record,
                         (const char *)&node->u.node6.mask,
                         16) < 0)
           goto err;
       break;
 
   default:
       ERR(handle, "unsupported protocol %u", proto);
       goto err;
   }
 
   if (context_to_record(handle, policydb, con, &tmp_con) < 0)
       goto err;
 
   if (sepol_node_set_con(handle, tmp_record, tmp_con) < 0)
       goto err;
 
   sepol_context_free(tmp_con);
   *record = tmp_record;
   return STATUS_SUCCESS;
 
      err:
   ERR(handle, "could not convert node to record");
   sepol_context_free(tmp_con);
   sepol_node_free(tmp_record);
   return STATUS_ERR;
}
 
/* Return the number of nodes */
extern int sepol_node_count(sepol_handle_t * handle __attribute__ ((unused)),
               const sepol_policydb_t * p, unsigned int *response)
{
 
   unsigned int count = 0;
   ocontext_t *c, *head;
   const policydb_t *policydb = &p->p;
 
   head = policydb->ocontexts[OCON_NODE];
   for (c = head; c != NULL; c = c->next)
       count++;
 
   head = policydb->ocontexts[OCON_NODE6];
   for (c = head; c != NULL; c = c->next)
       count++;
 
   *response = count;
 
   return STATUS_SUCCESS;
}
 
/* Check if a node exists */
int sepol_node_exists(sepol_handle_t * handle,
             const sepol_policydb_t * p,
             const sepol_node_key_t * key, int *response)
{
 
   const policydb_t *policydb = &p->p;
   ocontext_t *c, *head;
 
   int proto;
   const char *addr, *mask;
   sepol_node_key_unpack(key, &addr, &mask, &proto);
 
   switch (proto) {
 
   case SEPOL_PROTO_IP4:
       {
           head = policydb->ocontexts[OCON_NODE];
           for (c = head; c; c = c->next) {
               unsigned int *addr2 = &c->u.node.addr;
               unsigned int *mask2 = &c->u.node.mask;
 
               if (!memcmp(addr, addr2, 4) &&
                   !memcmp(mask, mask2, 4)) {
 
                   *response = 1;
                   return STATUS_SUCCESS;
               }
           }
           break;
       }
   case SEPOL_PROTO_IP6:
       {
           head = policydb->ocontexts[OCON_NODE6];
           for (c = head; c; c = c->next) {
               unsigned int *addr2 = c->u.node6.addr;
               unsigned int *mask2 = c->u.node6.mask;
 
               if (!memcmp(addr, addr2, 16) &&
                   !memcmp(mask, mask2, 16)) {
                   *response = 1;
                   return STATUS_SUCCESS;
               }
           }
           break;
       }
   default:
       ERR(handle, "unsupported protocol %u", proto);
       goto err;
   }
 
   *response = 0;
   return STATUS_SUCCESS;
 
      err:
   ERR(handle, "could not check if node %s/%s (%s) exists",
       addr, mask, sepol_node_get_proto_str(proto));
   return STATUS_ERR;
}
 
/* Query a node */
int sepol_node_query(sepol_handle_t * handle,
            const sepol_policydb_t * p,
            const sepol_node_key_t * key, sepol_node_t ** response)
{
 
   const policydb_t *policydb = &p->p;
   ocontext_t *c, *head;
 
   int proto;
   const char *addr, *mask;
   sepol_node_key_unpack(key, &addr, &mask, &proto);
 
   switch (proto) {
 
   case SEPOL_PROTO_IP4:
       {
           head = policydb->ocontexts[OCON_NODE];
           for (c = head; c; c = c->next) {
               unsigned int *addr2 = &c->u.node.addr;
               unsigned int *mask2 = &c->u.node.mask;
 
               if (!memcmp(addr, addr2, 4) &&
                   !memcmp(mask, mask2, 4)) {
 
                   if (node_to_record(handle, policydb,
                              c, SEPOL_PROTO_IP4,
                              response) < 0)
                       goto err;
                   return STATUS_SUCCESS;
               }
           }
           break;
       }
   case SEPOL_PROTO_IP6:
       {
           head = policydb->ocontexts[OCON_NODE6];
           for (c = head; c; c = c->next) {
               unsigned int *addr2 = c->u.node6.addr;
               unsigned int *mask2 = c->u.node6.mask;
 
               if (!memcmp(addr, addr2, 16) &&
                   !memcmp(mask, mask2, 16)) {
 
                   if (node_to_record(handle, policydb,
                              c, SEPOL_PROTO_IP6,
                              response) < 0)
                       goto err;
                   return STATUS_SUCCESS;
               }
           }
           break;
       }
   default:
       ERR(handle, "unsupported protocol %u", proto);
       goto err;
   }
   *response = NULL;
   return STATUS_SUCCESS;
 
      err:
   ERR(handle, "could not query node %s/%s (%s)",
       addr, mask, sepol_node_get_proto_str(proto));
   return STATUS_ERR;
 
}
 
/* Load a node into policy */
int sepol_node_modify(sepol_handle_t * handle,
             sepol_policydb_t * p,
             const sepol_node_key_t * key, const sepol_node_t * data)
{
 
   policydb_t *policydb = &p->p;
   ocontext_t *node = NULL;
 
   int proto;
   const char *addr, *mask;
 
   sepol_node_key_unpack(key, &addr, &mask, &proto);
 
   if (node_from_record(handle, policydb, &node, data) < 0)
       goto err;
 
   switch (proto) {
 
   case SEPOL_PROTO_IP4:
       {
           /* Attach to context list */
           node->next = policydb->ocontexts[OCON_NODE];
           policydb->ocontexts[OCON_NODE] = node;
           break;
       }
   case SEPOL_PROTO_IP6:
       {
           /* Attach to context list */
           node->next = policydb->ocontexts[OCON_NODE6];
           policydb->ocontexts[OCON_NODE6] = node;
           break;
       }
   default:
       ERR(handle, "unsupported protocol %u", proto);
       goto err;
   }
 
   return STATUS_SUCCESS;
 
      err:
   ERR(handle, "could not load node %s/%s (%s)",
       addr, mask, sepol_node_get_proto_str(proto));
   if (node != NULL) {
       context_destroy(&node->context[0]);
       free(node);
   }
   return STATUS_ERR;
}
 
int sepol_node_iterate(sepol_handle_t * handle,
              const sepol_policydb_t * p,
              int (*fn) (const sepol_node_t * node,
                 void *fn_arg), void *arg)
{
 
   const policydb_t *policydb = &p->p;
   ocontext_t *c, *head;
   sepol_node_t *node = NULL;
   int status;
 
   head = policydb->ocontexts[OCON_NODE];
   for (c = head; c; c = c->next) {
       if (node_to_record(handle, policydb, c, SEPOL_PROTO_IP4, &node)
           < 0)
           goto err;
 
       /* Invoke handler */
       status = fn(node, arg);
       if (status < 0)
           goto err;
 
       sepol_node_free(node);
       node = NULL;
 
       /* Handler requested exit */
       if (status > 0)
           break;
   }
 
   head = policydb->ocontexts[OCON_NODE6];
   for (c = head; c; c = c->next) {
       if (node_to_record(handle, policydb, c, SEPOL_PROTO_IP6, &node)
           < 0)
           goto err;
 
       /* Invoke handler */
       status = fn(node, arg);
       if (status < 0)
           goto err;
 
       sepol_node_free(node);
       node = NULL;
 
       /* Handler requested exit */
       if (status > 0)
           break;
   }
 
   return STATUS_SUCCESS;
 
      err:
   ERR(handle, "could not iterate over nodes");
   sepol_node_free(node);
   return STATUS_ERR;
}