Debian Codegraph @

cil/src/cil_copy_ast.c

raw
1 /*
2 * Copyright 2011 Tresys Technology, LLC. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY TRESYS TECHNOLOGY, LLC ``AS IS'' AND ANY EXPRESS
15 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
17 * EVENT SHALL TRESYS TECHNOLOGY, LLC OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
18 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
21 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
22 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
23 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *
25 * The views and conclusions contained in the software and documentation are those
26 * of the authors and should not be interpreted as representing official policies,
27 * either expressed or implied, of Tresys Technology, LLC.
28 */
29
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <string.h>
33
34 #include "cil_internal.h"
35 #include "cil_log.h"
36 #include "cil_mem.h"
37 #include "cil_tree.h"
38 #include "cil_list.h"
39 #include "cil_symtab.h"
40 #include "cil_copy_ast.h"
41 #include "cil_build_ast.h"
42 #include "cil_strpool.h"
43 #include "cil_verify.h"
44
45 struct cil_args_copy {
46 struct cil_tree_node *orig_dest;
47 struct cil_tree_node *dest;
48 struct cil_db *db;
49 };
50
51 void cil_copy_list(struct cil_list *data, struct cil_list **copy)
52 {
53 struct cil_list *new;
54 struct cil_list_item *orig_item;
55
56 cil_list_init(&new, data->flavor);
57
58 cil_list_for_each(orig_item, data) {
59 switch (orig_item->flavor) {
60 case CIL_STRING:
61 cil_list_append(new, CIL_STRING, orig_item->data);
62 break;
63 case CIL_LIST: {
64 struct cil_list *new_sub = NULL;
65 cil_copy_list((struct cil_list*)orig_item->data, &new_sub);
66 cil_list_append(new, CIL_LIST, new_sub);
67 break;
68 }
69 case CIL_PARAM: {
70 struct cil_param *po = orig_item->data;
71 struct cil_param *pn;
72 cil_param_init(&pn);
73 pn->str = po->str;
74 pn->flavor = po->flavor;
75 cil_list_append(new, CIL_PARAM, pn);
76 }
77 break;
78
79 default:
80 cil_list_append(new, orig_item->flavor, orig_item->data);
81 break;
82 }
83 }
84
85 *copy = new;
86 }
87
88 static int cil_copy_node(__attribute__((unused)) struct cil_db *db, void *data, void **copy, __attribute__((unused)) symtab_t *symtab)
89 {
90 char *new = NULL;
91
92 if (data != NULL) {
93 new = data;
94 }
95 *copy = new;
96
97 return SEPOL_OK;
98 }
99
100 int cil_copy_ordered(__attribute__((unused)) struct cil_db *db, void *data, void **copy, __attribute__((unused)) symtab_t *symtab)
101 {
102 struct cil_ordered *orig = data;
103 struct cil_ordered *new = NULL;
104
105 cil_ordered_init(&new);
106 if (orig->strs != NULL) {
107 cil_copy_list(orig->strs, &new->strs);
108 }
109 if (orig->datums != NULL) {
110 cil_copy_list(orig->datums, &new->datums);
111 }
112
113 *copy = new;
114
115 return SEPOL_OK;
116 }
117
118 int cil_copy_block(__attribute__((unused)) struct cil_db *db, void *data, void **copy, symtab_t *symtab)
119 {
120 struct cil_block *orig = data;
121 char *key = orig->datum.name;
122 struct cil_symtab_datum *datum = NULL;
123
124 cil_symtab_get_datum(symtab, key, &datum);
125 if (datum != NULL) {
126 if (FLAVOR(datum) != CIL_BLOCK) {
127 cil_tree_log(NODE(orig), CIL_ERR, "Block %s being copied", key);
128 cil_tree_log(NODE(datum), CIL_ERR, " Conflicts with %s already declared", cil_node_to_string(NODE(datum)));
129 return SEPOL_ERR;
130 }
131 cil_tree_log(NODE(orig), CIL_WARN, "Block %s being copied", key);
132 cil_tree_log(NODE(datum), CIL_WARN, " Previously declared");
133 *copy = datum;
134 } else {
135 struct cil_block *new;
136 cil_block_init(&new);
137 *copy = new;
138 }
139
140 return SEPOL_OK;
141 }
142
143 int cil_copy_blockabstract(__attribute__((unused)) struct cil_db *db, void *data, void **copy, __attribute__((unused)) symtab_t *symtab)
144 {
145 struct cil_blockabstract *orig = data;
146 struct cil_blockabstract *new = NULL;
147
148 cil_blockabstract_init(&new);
149
150 new->block_str = orig->block_str;
151 new->block = orig->block;
152
153 *copy = new;
154
155 return SEPOL_OK;
156 }
157
158 int cil_copy_blockinherit(__attribute__((unused)) struct cil_db *db, void *data, void **copy, __attribute__((unused)) symtab_t *symtab)
159 {
160 struct cil_blockinherit *orig = data;
161 struct cil_blockinherit *new = NULL;
162
163 cil_blockinherit_init(&new);
164
165 new->block_str = orig->block_str;
166 new->block = orig->block;
167
168 *copy = new;
169
170 return SEPOL_OK;
171 }
172
173 static int cil_copy_policycap(__attribute__((unused)) struct cil_db *db, __attribute__((unused)) void *data, void **copy, __attribute__((unused)) symtab_t *symtab)
174 {
175 struct cil_policycap *new;
176
177 cil_policycap_init(&new);
178 *copy = new;
179
180 return SEPOL_OK;
181 }
182
183 int cil_copy_perm(__attribute__((unused)) struct cil_db *db, __attribute__((unused)) void *data, void **copy, __attribute__((unused)) symtab_t *symtab)
184 {
185 struct cil_perm *new;
186
187 cil_perm_init(&new);
188 *copy = new;
189
190 return SEPOL_OK;
191 }
192
193 void cil_copy_classperms(struct cil_classperms *orig, struct cil_classperms **new)
194 {
195 cil_classperms_init(new);
196 (*new)->class_str = orig->class_str;
197 cil_copy_list(orig->perm_strs, &((*new)->perm_strs));
198 }
199
200 void cil_copy_classperms_set(struct cil_classperms_set *orig, struct cil_classperms_set **new)
201 {
202 cil_classperms_set_init(new);
203 (*new)->set_str = orig->set_str;
204 }
205
206 void cil_copy_classperms_list(struct cil_list *orig, struct cil_list **new)
207 {
208 struct cil_list_item *orig_item;
209
210 if (orig == NULL) {
211 return;
212 }
213
214 cil_list_init(new, CIL_LIST_ITEM);
215 cil_list_for_each(orig_item, orig) {
216 if (orig_item->flavor == CIL_CLASSPERMS) {
217 struct cil_classperms *cp;
218 cil_copy_classperms(orig_item->data, &cp);
219 cil_list_append(*new, CIL_CLASSPERMS, cp);
220 } else {
221 struct cil_classperms_set *cp_set;
222 cil_copy_classperms_set(orig_item->data, &cp_set);
223 cil_list_append(*new, CIL_CLASSPERMS_SET, cp_set);
224 }
225 }
226 }
227
228 int cil_copy_classmapping(__attribute__((unused)) struct cil_db *db, void *data, void **copy, __attribute__((unused)) symtab_t *symtab)
229 {
230 struct cil_classmapping *orig = data;
231 struct cil_classmapping *new = NULL;
232
233 cil_classmapping_init(&new);
234
235 new->map_class_str = orig->map_class_str;
236 new->map_class = orig->map_class;
237 new->map_perm_str = orig->map_perm_str;
238 new->map_perm = orig->map_perm;
239
240 cil_copy_classperms_list(orig->classperms, &new->classperms);
241
242 *copy = new;
243
244 return SEPOL_OK;
245 }
246
247 int cil_copy_class(__attribute__((unused)) struct cil_db *db, __attribute__((unused)) void *data, void **copy, __attribute__((unused)) symtab_t *symtab)
248 {
249 struct cil_class *new;
250
251 cil_class_init(&new);
252 *copy = new;
253
254 return SEPOL_OK;
255 }
256
257 int cil_copy_classpermission(__attribute__((unused)) struct cil_db *db, void *data, void **copy, symtab_t *symtab)
258 {
259 struct cil_classpermission *orig = data;
260 struct cil_classpermission *new;
261 char *key = orig->datum.name;
262 struct cil_symtab_datum *datum;
263
264 if (key != NULL) {
265 cil_symtab_get_datum(symtab, key, &datum);
266 if (datum != NULL) {
267 cil_log(CIL_ERR, "Named classpermission cannot be redefined\n");
268 return SEPOL_ERR;
269 }
270 }
271
272 cil_classpermission_init(&new);
273 cil_copy_classperms_list(orig->classperms, &new->classperms);
274 *copy = new;
275
276 return SEPOL_OK;
277 }
278
279 int cil_copy_classpermissionset(__attribute__((unused)) struct cil_db *db, void *data, void **copy, __attribute__((unused)) symtab_t *symtab)
280 {
281 struct cil_classpermissionset *orig = data;
282 struct cil_classpermissionset *new = NULL;
283
284 cil_classpermissionset_init(&new);
285
286 new->set_str = orig->set_str;
287 new->set = orig->set;
288
289 cil_copy_classperms_list(orig->classperms, &new->classperms);
290
291 *copy = new;
292
293 return SEPOL_OK;
294 }
295
296 int cil_copy_classcommon(__attribute__((unused)) struct cil_db *db, void *data, void **copy, __attribute__((unused)) symtab_t *symtab)
297 {
298 struct cil_classcommon *orig = data;
299 struct cil_classcommon *new = NULL;
300
301 cil_classcommon_init(&new);
302
303 new->class_str = orig->class_str;
304 new->class = orig->class;
305 new->common_str = orig->common_str;
306 new->common = orig->common;
307
308 *copy = new;
309
310 return SEPOL_OK;
311 }
312
313 int cil_copy_sid(__attribute__((unused)) struct cil_db *db, __attribute__((unused)) void *data, void **copy, __attribute__((unused)) symtab_t *symtab)
314 {
315 struct cil_sid *new;
316
317 cil_sid_init(&new);
318 *copy = new;
319
320 return SEPOL_OK;
321 }
322
323 int cil_copy_sidcontext(struct cil_db *db, void *data, void **copy, __attribute__((unused)) symtab_t *symtab)
324 {
325 struct cil_sidcontext *orig = data;
326 struct cil_sidcontext *new = NULL;
327
328 cil_sidcontext_init(&new);
329
330 if (orig->context_str != NULL) {
331 new->context_str = orig->context_str;
332 } else {
333 cil_context_init(&new->context);
334 cil_copy_fill_context(db, orig->context, new->context);
335 }
336
337 *copy = new;
338
339 return SEPOL_OK;
340 }
341
342 int cil_copy_user(__attribute__((unused)) struct cil_db *db, __attribute__((unused)) void *data, void **copy, __attribute__((unused)) symtab_t *symtab)
343 {
344 struct cil_user *new;
345
346 cil_user_init(&new);
347 *copy = new;
348
349 return SEPOL_OK;
350 }
351
352 int cil_copy_userattribute(__attribute__((unused)) struct cil_db *db, __attribute__((unused)) void *data, void **copy, __attribute__((unused)) symtab_t *symtab)
353 {
354 struct cil_userattribute *new;
355
356 cil_userattribute_init(&new);
357 *copy = new;
358
359 return SEPOL_OK;
360 }
361
362 int cil_copy_userattributeset(struct cil_db *db, void *data, void **copy, __attribute__((unused)) symtab_t *symtab)
363 {
364 struct cil_userattributeset *orig = data;
365 struct cil_userattributeset *new = NULL;
366
367 cil_userattributeset_init(&new);
368
369 new->attr_str = orig->attr_str;
370
371 cil_copy_expr(db, orig->str_expr, &new->str_expr);
372 cil_copy_expr(db, orig->datum_expr, &new->datum_expr);
373
374 *copy = new;
375
376 return SEPOL_OK;
377 }
378
379 int cil_copy_userrole(__attribute__((unused)) struct cil_db *db, void *data, void **copy, __attribute__((unused)) symtab_t *symtab)
380 {
381 struct cil_userrole *orig = data;
382 struct cil_userrole *new = NULL;
383
384 cil_userrole_init(&new);
385
386 new->user_str = orig->user_str;
387 new->role_str = orig->role_str;
388
389 *copy = new;
390
391 return SEPOL_OK;
392 }
393
394 int cil_copy_userlevel(struct cil_db *db, void *data, void **copy, __attribute__((unused)) symtab_t *symtab)
395 {
396 struct cil_userlevel *orig = data;
397 struct cil_userlevel *new = NULL;
398
399 cil_userlevel_init(&new);
400
401 new->user_str = orig->user_str;
402
403 if (orig->level_str != NULL) {
404 new->level_str = orig->level_str;
405 } else {
406 cil_copy_fill_level(db, orig->level, &new->level);
407 }
408
409 *copy = new;
410
411 return SEPOL_OK;
412 }
413
414 int cil_copy_userrange(struct cil_db *db, void *data, void **copy, __attribute__((unused)) symtab_t *symtab)
415 {
416 struct cil_userrange *orig = data;
417 struct cil_userrange *new = NULL;
418
419 cil_userrange_init(&new);
420
421 new->user_str = orig->user_str;
422
423 if (orig->range_str != NULL) {
424 new->range_str = orig->range_str;
425 } else {
426 cil_levelrange_init(&new->range);
427 cil_copy_fill_levelrange(db, orig->range, new->range);
428 }
429
430 *copy = new;
431
432 return SEPOL_OK;
433 }
434
435 int cil_copy_userprefix(__attribute__((unused)) struct cil_db *db, void *data, void **copy, __attribute__((unused)) symtab_t *symtab)
436 {
437 struct cil_userprefix *orig = data;
438 struct cil_userprefix *new = NULL;
439
440 cil_userprefix_init(&new);
441
442 new->user_str = orig->user_str;
443 new->prefix_str = orig->prefix_str;
444
445 *copy = new;
446
447 return SEPOL_OK;
448 }
449
450 int cil_copy_role(__attribute__((unused)) struct cil_db *db, __attribute__((unused)) void *data, void **copy, __attribute__((unused)) symtab_t *symtab)
451 {
452 struct cil_role *new;
453
454 cil_role_init(&new);
455 *copy = new;
456
457 return SEPOL_OK;
458 }
459
460 int cil_copy_roletype(__attribute__((unused)) struct cil_db *db, void *data, void **copy, __attribute__((unused)) symtab_t *symtab)
461 {
462 struct cil_roletype *orig = data;
463 struct cil_roletype *new = NULL;
464
465 cil_roletype_init(&new);
466
467 new->role_str = orig->role_str;
468 new->type_str = orig->type_str;
469
470 *copy = new;
471
472 return SEPOL_OK;
473 }
474
475 int cil_copy_roleattribute(__attribute__((unused)) struct cil_db *db, __attribute__((unused)) void *data, void **copy, __attribute__((unused)) symtab_t *symtab)
476 {
477 struct cil_roleattribute *new;
478
479 cil_roleattribute_init(&new);
480 *copy = new;
481
482 return SEPOL_OK;
483 }
484
485 int cil_copy_roleattributeset(struct cil_db *db, void *data, void **copy, __attribute__((unused)) symtab_t *symtab)
486 {
487 struct cil_roleattributeset *orig = data;
488 struct cil_roleattributeset *new = NULL;
489
490 cil_roleattributeset_init(&new);
491
492 new->attr_str = orig->attr_str;
493
494 cil_copy_expr(db, orig->str_expr, &new->str_expr);
495 cil_copy_expr(db, orig->datum_expr, &new->datum_expr);
496
497 *copy = new;
498
499 return SEPOL_OK;
500 }
501
502 int cil_copy_roleallow(__attribute__((unused)) struct cil_db *db, void *data, void **copy, __attribute__((unused)) symtab_t *symtab)
503 {
504 struct cil_roleallow *orig = data;
505 struct cil_roleallow *new = NULL;
506
507 cil_roleallow_init(&new);
508
509 new->src_str = orig->src_str;
510 new->tgt_str = orig->tgt_str;
511
512 *copy = new;
513
514 return SEPOL_OK;
515 }
516
517 int cil_copy_type(__attribute__((unused)) struct cil_db *db, __attribute__((unused)) void *data, void **copy, __attribute__((unused)) symtab_t *symtab)
518 {
519 struct cil_type *new;
520
521 cil_type_init(&new);
522 *copy = new;
523
524 return SEPOL_OK;
525 }
526
527 int cil_copy_typepermissive(__attribute__((unused)) struct cil_db *db, void *data, void **copy, __attribute__((unused)) symtab_t *symtab)
528 {
529 struct cil_typepermissive *orig = data;
530 struct cil_typepermissive *new = NULL;
531
532 cil_typepermissive_init(&new);
533
534 new->type_str = orig->type_str;
535
536 *copy = new;
537
538 return SEPOL_OK;
539 }
540
541 int cil_copy_typeneveraudit(__attribute__((unused)) struct cil_db *db, void *data, void **copy, __attribute__((unused)) symtab_t *symtab)
542 {
543 struct cil_typeneveraudit *orig = data;
544 struct cil_typeneveraudit *new = NULL;
545
546 cil_typeneveraudit_init(&new);
547
548 new->type_str = orig->type_str;
549
550 *copy = new;
551
552 return SEPOL_OK;
553 }
554
555 int cil_copy_typeattribute(__attribute__((unused)) struct cil_db *db, __attribute__((unused)) void *data, void **copy, __attribute__((unused)) symtab_t *symtab)
556 {
557 struct cil_typeattribute *new;
558
559 cil_typeattribute_init(&new);
560 *copy = new;
561
562 return SEPOL_OK;
563 }
564
565 int cil_copy_typeattributeset(struct cil_db *db, void *data, void **copy, __attribute__((unused)) symtab_t *symtab)
566 {
567 struct cil_typeattributeset *orig = data;
568 struct cil_typeattributeset *new = NULL;
569
570 cil_typeattributeset_init(&new);
571
572 new->attr_str = orig->attr_str;
573
574 cil_copy_expr(db, orig->str_expr, &new->str_expr);
575 cil_copy_expr(db, orig->datum_expr, &new->datum_expr);
576
577 *copy = new;
578
579 return SEPOL_OK;
580 }
581
582 static int cil_copy_expandtypeattribute(__attribute__((unused)) struct cil_db *db, void *data, void **copy, __attribute__((unused)) symtab_t *symtab)
583 {
584 struct cil_expandtypeattribute *orig = data;
585 struct cil_expandtypeattribute *new = NULL;
586
587 cil_expandtypeattribute_init(&new);
588
589 if (orig->attr_strs != NULL) {
590 cil_copy_list(orig->attr_strs, &new->attr_strs);
591 }
592
593 if (orig->attr_datums != NULL) {
594 cil_copy_list(orig->attr_datums, &new->attr_datums);
595 }
596
597 new->expand = orig->expand;
598
599 *copy = new;
600
601 return SEPOL_OK;
602 }
603
604 static int cil_copy_alias(__attribute__((unused)) struct cil_db *db, __attribute__((unused)) void *data, void **copy, __attribute__((unused)) symtab_t *symtab)
605 {
606 struct cil_alias *new;
607
608 cil_alias_init(&new);
609 *copy = new;
610
611 return SEPOL_OK;
612 }
613
614 static int cil_copy_aliasactual(__attribute__((unused)) struct cil_db *db, void *data, void **copy, __attribute__((unused))symtab_t *symtab)
615 {
616 struct cil_aliasactual *orig = data;
617 struct cil_aliasactual *new = NULL;
618
619 cil_aliasactual_init(&new);
620
621 new->alias_str = orig->alias_str;
622 new->alias = orig->alias;
623 new->actual_str = orig->actual_str;
624 new->actual = orig->actual;
625
626 *copy = new;
627
628 return SEPOL_OK;
629 }
630
631 static int cil_copy_roletransition(__attribute__((unused)) struct cil_db *db, void *data, void **copy, __attribute__((unused)) symtab_t *symtab)
632 {
633 struct cil_roletransition *orig = data;
634 struct cil_roletransition *new = NULL;
635
636 cil_roletransition_init(&new);
637
638 new->src_str = orig->src_str;
639 new->tgt_str = orig->tgt_str;
640 new->obj_str = orig->obj_str;
641 new->result_str = orig->result_str;
642
643 *copy = new;
644
645 return SEPOL_OK;
646 }
647
648 int cil_copy_nametypetransition(__attribute__((unused)) struct cil_db *db, void *data, void **copy, __attribute__((unused)) symtab_t *symtab)
649 {
650 struct cil_nametypetransition *orig = data;
651 struct cil_nametypetransition *new = NULL;
652
653 cil_nametypetransition_init(&new);
654
655 new->src_str = orig->src_str;
656 new->tgt_str = orig->tgt_str;
657 new->obj_str = orig->obj_str;
658 new->name_str = orig->name_str;
659 new->name = orig->name;
660 new->result_str = orig->result_str;
661
662
663 *copy = new;
664
665 return SEPOL_OK;
666 }
667
668 int cil_copy_rangetransition(struct cil_db *db, void *data, void **copy, __attribute__((unused)) symtab_t *symtab)
669 {
670 struct cil_rangetransition *orig = data;
671 struct cil_rangetransition *new = NULL;
672
673 cil_rangetransition_init(&new);
674
675 new->src_str = orig->src_str;
676 new->exec_str = orig->exec_str;
677 new->obj_str = orig->obj_str;
678
679 if (orig->range_str != NULL) {
680 new->range_str = orig->range_str;
681 } else {
682 cil_levelrange_init(&new->range);
683 cil_copy_fill_levelrange(db, orig->range, new->range);
684 }
685
686 *copy = new;
687
688 return SEPOL_OK;
689 }
690
691 int cil_copy_bool(__attribute__((unused)) struct cil_db *db, void *data, void **copy, __attribute__((unused)) symtab_t *symtab)
692 {
693 struct cil_bool *orig = data;
694 struct cil_bool *new;
695
696 cil_bool_init(&new);
697 new->value = orig->value;
698 *copy = new;
699
700 return SEPOL_OK;
701 }
702
703 static int cil_copy_tunable(__attribute__((unused)) struct cil_db *db, void *data, void **copy, __attribute__((unused)) symtab_t *symtab)
704 {
705 struct cil_tunable *orig = data;
706 struct cil_tunable *new;
707
708 cil_tunable_init(&new);
709 new->value = orig->value;
710 *copy = new;
711
712 return SEPOL_OK;
713 }
714
715 static void cil_copy_fill_permissionx(struct cil_db *db, struct cil_permissionx *orig, struct cil_permissionx *new)
716 {
717 new->kind = orig->kind;
718 new->obj_str = orig->obj_str;
719 cil_copy_expr(db, orig->expr_str, &new->expr_str);
720 }
721
722 int cil_copy_avrule(struct cil_db *db, void *data, void **copy, __attribute__((unused)) symtab_t *symtab)
723 {
724 struct cil_avrule *orig = data;
725 struct cil_avrule *new = NULL;
726
727 cil_avrule_init(&new);
728
729 new->is_extended = orig->is_extended;
730 new->rule_kind = orig->rule_kind;
731 new->src_str = orig->src_str;
732 new->tgt_str = orig->tgt_str;
733
734 if (!new->is_extended) {
735 cil_copy_classperms_list(orig->perms.classperms, &new->perms.classperms);
736 } else {
737 if (orig->perms.x.permx_str != NULL) {
738 new->perms.x.permx_str = orig->perms.x.permx_str;
739 } else {
740 cil_permissionx_init(&new->perms.x.permx);
741 cil_copy_fill_permissionx(db, orig->perms.x.permx, new->perms.x.permx);
742 }
743 }
744
745 *copy = new;
746
747 return SEPOL_OK;
748 }
749
750 static int cil_copy_permissionx(struct cil_db *db, void *data, void **copy, symtab_t *symtab)
751 {
752 struct cil_permissionx *orig = data;
753 struct cil_permissionx *new;
754 char *key = orig->datum.name;
755 struct cil_symtab_datum *datum;
756
757 cil_symtab_get_datum(symtab, key, &datum);
758 if (datum != NULL) {
759 cil_log(CIL_ERR, "Permissionx cannot be redefined\n");
760 return SEPOL_ERR;
761 }
762
763 cil_permissionx_init(&new);
764 cil_copy_fill_permissionx(db, orig, new);
765 *copy = new;
766
767 return SEPOL_OK;
768 }
769
770 int cil_copy_deny_rule(__attribute__((unused)) struct cil_db *db, void *data, void **copy, __attribute__((unused)) symtab_t *symtab)
771 {
772 struct cil_deny_rule *orig = data;
773 struct cil_deny_rule *new = NULL;
774
775 cil_deny_rule_init(&new);
776
777 new->src_str = orig->src_str;
778 new->tgt_str = orig->tgt_str;
779 cil_copy_classperms_list(orig->classperms, &new->classperms);
780
781 *copy = new;
782
783 return SEPOL_OK;
784 }
785
786 int cil_copy_type_rule(__attribute__((unused)) struct cil_db *db, void *data, void **copy, __attribute__((unused)) symtab_t *symtab)
787 {
788 struct cil_type_rule *orig = data;
789 struct cil_type_rule *new = NULL;
790
791 cil_type_rule_init(&new);
792
793 new->rule_kind = orig->rule_kind;
794 new->src_str = orig->src_str;
795 new->tgt_str = orig->tgt_str;
796 new->obj_str = orig->obj_str;
797 new->result_str = orig->result_str;
798
799 *copy = new;
800
801 return SEPOL_OK;
802 }
803
804 int cil_copy_sens(__attribute__((unused)) struct cil_db *db, __attribute__((unused)) void *data, void **copy, __attribute__((unused)) symtab_t *symtab)
805 {
806 struct cil_sens *new;
807
808 cil_sens_init(&new);
809 *copy = new;
810
811 return SEPOL_OK;
812 }
813
814 int cil_copy_cat(__attribute__((unused)) struct cil_db *db, __attribute__((unused)) void *data, void **copy, __attribute__((unused)) symtab_t *symtab)
815 {
816 struct cil_cat *new;
817
818 cil_cat_init(&new);
819 *copy = new;
820
821 return SEPOL_OK;
822 }
823
824 static void cil_copy_cats(struct cil_db *db, struct cil_cats *orig, struct cil_cats **new)
825 {
826 cil_cats_init(new);
827 cil_copy_expr(db, orig->str_expr, &(*new)->str_expr);
828 cil_copy_expr(db, orig->datum_expr, &(*new)->datum_expr);
829 }
830
831 int cil_copy_catset(struct cil_db *db, void *data, void **copy, symtab_t *symtab)
832 {
833 struct cil_catset *orig = data;
834 struct cil_catset *new;
835 char *key = orig->datum.name;
836 struct cil_symtab_datum *datum;
837
838 cil_symtab_get_datum(symtab, key, &datum);
839 if (datum != NULL) {
840 cil_log(CIL_ERR, "Category set cannot be redefined\n");
841 return SEPOL_ERR;
842 }
843
844 cil_catset_init(&new);
845 cil_copy_cats(db, orig->cats, &new->cats);
846 *copy = new;
847
848 return SEPOL_OK;
849 }
850
851 int cil_copy_senscat(struct cil_db *db, void *data, void **copy, __attribute__((unused)) symtab_t *symtab)
852 {
853 struct cil_senscat *orig = data;
854 struct cil_senscat *new = NULL;
855
856 cil_senscat_init(&new);
857
858 new->sens_str = orig->sens_str;
859
860 cil_copy_cats(db, orig->cats, &new->cats);
861
862 *copy = new;
863
864 return SEPOL_OK;
865 }
866
867 void cil_copy_fill_level(struct cil_db *db, struct cil_level *orig, struct cil_level **new)
868 {
869 cil_level_init(new);
870
871 (*new)->sens_str = orig->sens_str;
872
873 if (orig->cats != NULL) {
874 cil_copy_cats(db, orig->cats, &(*new)->cats);
875 }
876 }
877
878 int cil_copy_level(struct cil_db *db, void *data, void **copy, symtab_t *symtab)
879 {
880 struct cil_level *orig = data;
881 struct cil_level *new;
882 char *key = orig->datum.name;
883 struct cil_symtab_datum *datum;
884
885 if (key != NULL) {
886 cil_symtab_get_datum(symtab, key, &datum);
887 if (datum != NULL) {
888 cil_log(CIL_ERR, "Named level cannot be redefined\n");
889 return SEPOL_ERR;
890 }
891 }
892
893 cil_copy_fill_level(db, orig, &new);
894 *copy = new;
895
896 return SEPOL_OK;
897 }
898
899 void cil_copy_fill_levelrange(struct cil_db *db, struct cil_levelrange *data, struct cil_levelrange *new)
900 {
901 if (data->low_str != NULL) {
902 new->low_str = data->low_str;
903 } else {
904 cil_copy_fill_level(db, data->low, &new->low);
905 }
906
907 if (data->high_str != NULL) {
908 new->high_str = data->high_str;
909 } else {
910 cil_copy_fill_level(db, data->high, &new->high);
911 }
912 }
913
914 int cil_copy_levelrange(struct cil_db *db, void *data, void **copy, symtab_t *symtab)
915 {
916 struct cil_levelrange *orig = data;
917 struct cil_levelrange *new;
918 char *key = orig->datum.name;
919 struct cil_symtab_datum *datum;
920
921 if (key != NULL) {
922 cil_symtab_get_datum(symtab, key, &datum);
923 if (datum != NULL) {
924 cil_log(CIL_ERR, "Named levelrange cannot be redefined\n");
925 return SEPOL_ERR;
926 }
927 }
928
929 cil_levelrange_init(&new);
930 cil_copy_fill_levelrange(db, orig, new);
931 *copy = new;
932
933 return SEPOL_OK;
934 }
935
936 void cil_copy_fill_context(struct cil_db *db, struct cil_context *data, struct cil_context *new)
937 {
938 new->user_str = data->user_str;
939 new->role_str = data->role_str;
940 new->type_str = data->type_str;
941
942 if (data->range_str != NULL) {
943 new->range_str = data->range_str;
944 } else {
945 cil_levelrange_init(&new->range);
946 cil_copy_fill_levelrange(db, data->range, new->range);
947 }
948 }
949
950 int cil_copy_context(struct cil_db *db, void *data, void **copy, symtab_t *symtab)
951 {
952 struct cil_context *orig = data;
953 struct cil_context *new;
954 char *key = orig->datum.name;
955 struct cil_symtab_datum *datum;
956
957 if (key != NULL) {
958 cil_symtab_get_datum(symtab, key, &datum);
959 if (datum != NULL) {
960 cil_log(CIL_ERR, "Named context cannot be redefined\n");
961 return SEPOL_ERR;
962 }
963 }
964
965 cil_context_init(&new);
966 cil_copy_fill_context(db, orig, new);
967 *copy = new;
968
969 return SEPOL_OK;
970 }
971
972 int cil_copy_netifcon(struct cil_db *db, void *data, void **copy, __attribute__((unused)) symtab_t *symtab)
973 {
974 struct cil_netifcon *orig = data;
975 struct cil_netifcon *new = NULL;
976
977 cil_netifcon_init(&new);
978
979 new->interface_str = orig->interface_str;
980
981 if (orig->if_context_str != NULL) {
982 new->if_context_str = orig->if_context_str;
983 } else {
984 cil_context_init(&new->if_context);
985 cil_copy_fill_context(db, orig->if_context, new->if_context);
986 }
987
988 if (orig->packet_context_str != NULL) {
989 new->packet_context_str = orig->packet_context_str;
990 } else {
991 cil_context_init(&new->packet_context);
992 cil_copy_fill_context(db, orig->packet_context, new->packet_context);
993 }
994
995 *copy = new;
996
997 return SEPOL_OK;
998 }
999
1000 int cil_copy_genfscon(struct cil_db *db, void *data, void **copy, __attribute__((unused)) symtab_t *symtab)
1001 {
1002 struct cil_genfscon *orig = data;
1003 struct cil_genfscon *new = NULL;
1004
1005 cil_genfscon_init(&new);
1006
1007 new->fs_str = orig->fs_str;
1008 new->path_str = orig->path_str;
1009
1010 if (orig->context_str != NULL) {
1011 new->context_str = orig->context_str;
1012 } else {
1013 cil_context_init(&new->context);
1014 cil_copy_fill_context(db, orig->context, new->context);
1015 }
1016
1017 *copy = new;
1018
1019 return SEPOL_OK;
1020 }
1021
1022 int cil_copy_filecon(struct cil_db *db, void *data, void **copy, __attribute__((unused)) symtab_t *symtab)
1023 {
1024 struct cil_filecon *orig = data;
1025 struct cil_filecon *new = NULL;
1026
1027 cil_filecon_init(&new);
1028
1029 new->path_str = orig->path_str;
1030 new->path = orig->path;
1031 new->type = orig->type;
1032
1033 if (orig->context_str != NULL) {
1034 new->context_str = orig->context_str;
1035 } else if (orig->context != NULL) {
1036 cil_context_init(&new->context);
1037 cil_copy_fill_context(db, orig->context, new->context);
1038 }
1039
1040 *copy = new;
1041
1042 return SEPOL_OK;
1043 }
1044
1045 int cil_copy_nodecon(struct cil_db *db, void *data, void **copy, __attribute__((unused)) symtab_t *symtab)
1046 {
1047 struct cil_nodecon *orig = data;
1048 struct cil_nodecon *new = NULL;
1049
1050 cil_nodecon_init(&new);
1051
1052 if (orig->addr_str != NULL) {
1053 new->addr_str = orig->addr_str;
1054 } else {
1055 cil_ipaddr_init(&new->addr);
1056 cil_copy_fill_ipaddr(orig->addr, new->addr);
1057 }
1058
1059 if (orig->mask_str != NULL) {
1060 new->mask_str = orig->mask_str;
1061 } else {
1062 cil_ipaddr_init(&new->mask);
1063 cil_copy_fill_ipaddr(orig->mask, new->mask);
1064 }
1065
1066 if (orig->context_str != NULL) {
1067 new->context_str = orig->context_str;
1068 } else {
1069 cil_context_init(&new->context);
1070 cil_copy_fill_context(db, orig->context, new->context);
1071 }
1072
1073 *copy = new;
1074
1075 return SEPOL_OK;
1076 }
1077
1078 int cil_copy_ibpkeycon(struct cil_db *db, void *data, void **copy, __attribute__((unused)) symtab_t *symtab)
1079 {
1080 struct cil_ibpkeycon *orig = data;
1081 struct cil_ibpkeycon *new = NULL;
1082
1083 cil_ibpkeycon_init(&new);
1084
1085 new->subnet_prefix_str = orig->subnet_prefix_str;
1086 new->pkey_low = orig->pkey_low;
1087 new->pkey_high = orig->pkey_high;
1088
1089 if (orig->context_str) {
1090 new->context_str = orig->context_str;
1091 } else {
1092 cil_context_init(&new->context);
1093 cil_copy_fill_context(db, orig->context, new->context);
1094 }
1095
1096 *copy = new;
1097
1098 return SEPOL_OK;
1099 }
1100
1101 static int cil_copy_ibendportcon(struct cil_db *db, void *data, void **copy, __attribute__((unused)) symtab_t *symtab)
1102 {
1103 struct cil_ibendportcon *orig = data;
1104 struct cil_ibendportcon *new = NULL;
1105
1106 cil_ibendportcon_init(&new);
1107
1108 new->dev_name_str = orig->dev_name_str;
1109 new->port = orig->port;
1110
1111 if (orig->context_str) {
1112 new->context_str = orig->context_str;
1113 } else {
1114 cil_context_init(&new->context);
1115 cil_copy_fill_context(db, orig->context, new->context);
1116 }
1117
1118 *copy = new;
1119
1120 return SEPOL_OK;
1121 }
1122
1123 int cil_copy_portcon(struct cil_db *db, void *data, void **copy, __attribute__((unused)) symtab_t *symtab)
1124 {
1125 struct cil_portcon *orig = data;
1126 struct cil_portcon *new = NULL;
1127
1128 cil_portcon_init(&new);
1129
1130 new->proto = orig->proto;
1131 new->port_low = orig->port_low;
1132 new->port_high = orig->port_high;
1133
1134 if (orig->context_str != NULL) {
1135 new->context_str = orig->context_str;
1136 } else {
1137 cil_context_init(&new->context);
1138 cil_copy_fill_context(db, orig->context, new->context);
1139 }
1140
1141 *copy = new;
1142
1143 return SEPOL_OK;
1144 }
1145
1146 int cil_copy_pirqcon(struct cil_db *db, void *data, void **copy, __attribute__((unused)) symtab_t *symtab)
1147 {
1148 struct cil_pirqcon *orig = data;
1149 struct cil_pirqcon *new = NULL;
1150
1151 cil_pirqcon_init(&new);
1152
1153 new->pirq = orig->pirq;
1154
1155 if (orig->context_str != NULL) {
1156 new->context_str = orig->context_str;
1157 } else {
1158 cil_context_init(&new->context);
1159 cil_copy_fill_context(db, orig->context, new->context);
1160 }
1161
1162 *copy = new;
1163
1164 return SEPOL_OK;
1165 }
1166
1167 int cil_copy_iomemcon(struct cil_db *db, void *data, void **copy, __attribute__((unused)) symtab_t *symtab)
1168 {
1169 struct cil_iomemcon *orig = data;
1170 struct cil_iomemcon *new = NULL;
1171
1172 cil_iomemcon_init(&new);
1173
1174 new->iomem_low = orig->iomem_low;
1175 new->iomem_high = orig->iomem_high;
1176
1177 if (orig->context_str != NULL) {
1178 new->context_str = orig->context_str;
1179 } else {
1180 cil_context_init(&new->context);
1181 cil_copy_fill_context(db, orig->context, new->context);
1182 }
1183
1184 *copy = new;
1185
1186 return SEPOL_OK;
1187 }
1188
1189 int cil_copy_ioportcon(struct cil_db *db, void *data, void **copy, __attribute__((unused)) symtab_t *symtab)
1190 {
1191 struct cil_ioportcon *orig = data;
1192 struct cil_ioportcon *new = NULL;
1193
1194 cil_ioportcon_init(&new);
1195
1196 new->ioport_low = orig->ioport_low;
1197 new->ioport_high = orig->ioport_high;
1198
1199 if (orig->context_str != NULL) {
1200 new->context_str = orig->context_str;
1201 } else {
1202 cil_context_init(&new->context);
1203 cil_copy_fill_context(db, orig->context, new->context);
1204 }
1205
1206 *copy = new;
1207
1208 return SEPOL_OK;
1209 }
1210
1211 int cil_copy_pcidevicecon(struct cil_db *db, void *data, void **copy, __attribute__((unused)) symtab_t *symtab)
1212 {
1213 struct cil_pcidevicecon *orig = data;
1214 struct cil_pcidevicecon *new = NULL;
1215
1216 cil_pcidevicecon_init(&new);
1217
1218 new->dev = orig->dev;
1219
1220 if (orig->context_str != NULL) {
1221 new->context_str = orig->context_str;
1222 } else {
1223 cil_context_init(&new->context);
1224 cil_copy_fill_context(db, orig->context, new->context);
1225 }
1226
1227 *copy = new;
1228
1229 return SEPOL_OK;
1230 }
1231
1232 static int cil_copy_devicetreecon(struct cil_db *db, void *data, void **copy, __attribute__((unused)) symtab_t *symtab)
1233 {
1234 struct cil_devicetreecon *orig = data;
1235 struct cil_devicetreecon *new = NULL;
1236
1237 cil_devicetreecon_init(&new);
1238
1239 new->path = orig->path;
1240
1241 if (orig->context_str != NULL) {
1242 new->context_str = orig->context_str;
1243 } else {
1244 cil_context_init(&new->context);
1245 cil_copy_fill_context(db, orig->context, new->context);
1246 }
1247
1248 *copy = new;
1249
1250 return SEPOL_OK;
1251 }
1252
1253 int cil_copy_fsuse(struct cil_db *db, void *data, void **copy, __attribute__((unused)) symtab_t *symtab)
1254 {
1255 struct cil_fsuse *orig = data;
1256 struct cil_fsuse *new = NULL;
1257
1258 cil_fsuse_init(&new);
1259
1260 new->type = orig->type;
1261 new->fs_str = orig->fs_str;
1262
1263 if (orig->context_str != NULL) {
1264 new->context_str = orig->context_str;
1265 } else {
1266 cil_context_init(&new->context);
1267 cil_copy_fill_context(db, orig->context, new->context);
1268 }
1269
1270 *copy = new;
1271
1272 return SEPOL_OK;
1273 }
1274
1275 int cil_copy_expr(struct cil_db *db, struct cil_list *orig, struct cil_list **new)
1276 {
1277 struct cil_list_item *curr;
1278
1279 if (orig == NULL) {
1280 *new = NULL;
1281 return SEPOL_OK;
1282 }
1283
1284 cil_list_init(new, orig->flavor);
1285
1286 cil_list_for_each(curr, orig) {
1287 switch (curr->flavor) {
1288 case CIL_LIST: {
1289 struct cil_list *sub_list;
1290 cil_copy_expr(db, curr->data, &sub_list);
1291 cil_list_append(*new, CIL_LIST, sub_list);
1292 break;
1293 }
1294 case CIL_STRING:
1295 cil_list_append(*new, CIL_STRING, curr->data);
1296 break;
1297 case CIL_DATUM:
1298 cil_list_append(*new, curr->flavor, curr->data);
1299 break;
1300 case CIL_OP:
1301 cil_list_append(*new, curr->flavor, curr->data);
1302 break;
1303 case CIL_CONS_OPERAND:
1304 cil_list_append(*new, curr->flavor, curr->data);
1305 break;
1306 default:
1307 cil_log(CIL_INFO, "Unknown flavor %d in expression being copied\n",curr->flavor);
1308 cil_list_append(*new, curr->flavor, curr->data);
1309 break;
1310 }
1311 }
1312
1313 return SEPOL_OK;
1314 }
1315
1316 int cil_copy_constrain(struct cil_db *db, void *data, void **copy, __attribute__((unused)) symtab_t *symtab)
1317 {
1318 struct cil_constrain *orig = data;
1319 struct cil_constrain *new = NULL;
1320
1321 cil_constrain_init(&new);
1322 cil_copy_classperms_list(orig->classperms, &new->classperms);
1323
1324 cil_copy_expr(db, orig->str_expr, &new->str_expr);
1325 cil_copy_expr(db, orig->datum_expr, &new->datum_expr);
1326
1327 *copy = new;
1328
1329 return SEPOL_OK;
1330 }
1331
1332 int cil_copy_validatetrans(struct cil_db *db, void *data, void **copy, __attribute__((unused)) symtab_t *symtab)
1333 {
1334 struct cil_validatetrans *orig = data;
1335 struct cil_validatetrans *new = NULL;
1336
1337 cil_validatetrans_init(&new);
1338
1339 new->class_str = orig->class_str;
1340
1341 cil_copy_expr(db, orig->str_expr, &new->str_expr);
1342 cil_copy_expr(db, orig->datum_expr, &new->datum_expr);
1343
1344 *copy = new;
1345
1346 return SEPOL_OK;
1347 }
1348
1349 int cil_copy_call(struct cil_db *db, void *data, void **copy, __attribute__((unused)) symtab_t *symtab)
1350 {
1351 struct cil_call *orig = data;
1352 struct cil_call *new = NULL;
1353 int rc = SEPOL_ERR;
1354
1355 cil_call_init(&new);
1356
1357 new->macro_str = orig->macro_str;
1358 new->macro = orig->macro;
1359
1360 if (orig->args_tree != NULL) {
1361 cil_tree_init(&new->args_tree);
1362 rc = cil_copy_ast(db, orig->args_tree->root, new->args_tree->root);
1363 if (rc != SEPOL_OK) {
1364 goto exit;
1365 }
1366 }
1367
1368 new->copied = orig->copied;
1369
1370 *copy = new;
1371
1372 return SEPOL_OK;
1373
1374 exit:
1375 cil_destroy_call(new);
1376 return rc;
1377 }
1378
1379 static int cil_copy_macro(__attribute__((unused)) struct cil_db *db, void *data, void **copy, symtab_t *symtab)
1380 {
1381 struct cil_macro *orig = data;
1382 char *key = orig->datum.name;
1383 struct cil_symtab_datum *datum = NULL;
1384
1385 cil_symtab_get_datum(symtab, key, &datum);
1386 if (datum != NULL) {
1387 if (FLAVOR(datum) != CIL_MACRO) {
1388 cil_tree_log(NODE(orig), CIL_ERR, "Macro %s being copied", key);
1389 cil_tree_log(NODE(datum), CIL_ERR, " Conflicts with %s already declared", cil_node_to_string(NODE(datum)));
1390 return SEPOL_ERR;
1391 }
1392 cil_tree_log(NODE(orig), CIL_WARN, "Skipping macro %s", key);
1393 cil_tree_log(NODE(datum), CIL_WARN, " Previously declared");
1394 *copy = NULL;
1395 } else {
1396 struct cil_macro *new;
1397 cil_macro_init(&new);
1398 if (orig->params != NULL) {
1399 cil_copy_list(orig->params, &new->params);
1400 }
1401 *copy = new;
1402 }
1403
1404 return SEPOL_OK;
1405 }
1406
1407 int cil_copy_optional(__attribute__((unused)) struct cil_db *db, __attribute__((unused)) void *data, void **copy, __attribute__((unused)) symtab_t *symtab)
1408 {
1409 struct cil_optional *new;
1410
1411 cil_optional_init(&new);
1412 *copy = new;
1413
1414 return SEPOL_OK;
1415 }
1416
1417 void cil_copy_fill_ipaddr(struct cil_ipaddr *data, struct cil_ipaddr *new)
1418 {
1419 new->family = data->family;
1420 memcpy(&new->ip, &data->ip, sizeof(data->ip));
1421 }
1422
1423 int cil_copy_ipaddr(__attribute__((unused)) struct cil_db *db, void *data, void **copy, symtab_t *symtab)
1424 {
1425 struct cil_ipaddr *orig = data;
1426 struct cil_ipaddr *new;
1427 char * key = orig->datum.name;
1428 struct cil_symtab_datum *datum;
1429
1430 cil_symtab_get_datum(symtab, key, &datum);
1431 if (datum != NULL) {
1432 cil_log(CIL_ERR, "Named IP address cannot be redefined\n");
1433 return SEPOL_ERR;
1434 }
1435
1436 cil_ipaddr_init(&new);
1437 cil_copy_fill_ipaddr(orig, new);
1438 *copy = new;
1439
1440 return SEPOL_OK;
1441 }
1442
1443 static int cil_copy_condblock(__attribute__((unused)) struct cil_db *db, void *data, void **copy, __attribute__((unused)) symtab_t *symtab)
1444 {
1445 struct cil_condblock *orig = data;
1446 struct cil_condblock *new = *copy;
1447 cil_condblock_init(&new);
1448 new->flavor = orig->flavor;
1449 *copy = new;
1450
1451 return SEPOL_OK;
1452 }
1453
1454 int cil_copy_boolif(struct cil_db *db, void *data, void **copy, __attribute__((unused)) symtab_t *symtab)
1455 {
1456 struct cil_booleanif *orig = data;
1457 struct cil_booleanif *new = NULL;
1458
1459 cil_boolif_init(&new);
1460
1461 cil_copy_expr(db, orig->str_expr, &new->str_expr);
1462 cil_copy_expr(db, orig->datum_expr, &new->datum_expr);
1463 new->preserved_tunable = orig->preserved_tunable;
1464
1465 *copy = new;
1466
1467 return SEPOL_OK;
1468 }
1469
1470 static int cil_copy_tunif(struct cil_db *db, void *data, void **copy, __attribute__((unused)) symtab_t *symtab)
1471 {
1472 struct cil_tunableif *orig = data;
1473 struct cil_tunableif *new = NULL;
1474
1475 cil_tunif_init(&new);
1476
1477 cil_copy_expr(db, orig->str_expr, &new->str_expr);
1478 cil_copy_expr(db, orig->datum_expr, &new->datum_expr);
1479
1480 *copy = new;
1481
1482 return SEPOL_OK;
1483 }
1484
1485 static int cil_copy_default(__attribute__((unused)) struct cil_db *db, void *data, void **copy, __attribute__((unused)) symtab_t *symtab)
1486 {
1487 struct cil_default *orig = data;
1488 struct cil_default *new = NULL;
1489
1490 cil_default_init(&new);
1491
1492 new->flavor = orig->flavor;
1493
1494 if (orig->class_strs != NULL) {
1495 cil_copy_list(orig->class_strs, &new->class_strs);
1496 }
1497
1498 new->object = orig->object;
1499
1500 *copy = new;
1501
1502 return SEPOL_OK;
1503 }
1504
1505 static int cil_copy_defaultrange(__attribute__((unused)) struct cil_db *db, void *data, void **copy, __attribute__((unused)) symtab_t *symtab)
1506 {
1507 struct cil_defaultrange *orig = data;
1508 struct cil_defaultrange *new = NULL;
1509
1510 cil_defaultrange_init(&new);
1511
1512 if (orig->class_strs != NULL) {
1513 cil_copy_list(orig->class_strs, &new->class_strs);
1514 }
1515
1516 new->object_range = orig->object_range;
1517
1518 *copy = new;
1519
1520 return SEPOL_OK;
1521 }
1522
1523 static int cil_copy_handleunknown(__attribute__((unused)) struct cil_db *db, void *data, void **copy, __attribute__((unused)) symtab_t *symtab)
1524 {
1525 struct cil_handleunknown *orig = data;
1526 struct cil_handleunknown *new = NULL;
1527
1528 cil_handleunknown_init(&new);
1529 new->handle_unknown = orig->handle_unknown;
1530 *copy = new;
1531
1532 return SEPOL_OK;
1533 }
1534
1535 static int cil_copy_mls(__attribute__((unused)) struct cil_db *db, void *data, void **copy, __attribute__((unused)) symtab_t *symtab)
1536 {
1537 struct cil_mls *orig = data;
1538 struct cil_mls *new = NULL;
1539
1540 cil_mls_init(&new);
1541 new->value = orig->value;
1542 *copy = new;
1543
1544 return SEPOL_OK;
1545 }
1546
1547 static int cil_copy_bounds(__attribute__((unused)) struct cil_db *db, void *data, void **copy, __attribute__((unused)) symtab_t *symtab)
1548 {
1549 struct cil_bounds *orig = data;
1550 struct cil_bounds *new = NULL;
1551
1552 cil_bounds_init(&new);
1553
1554 new->parent_str = orig->parent_str;
1555 new->child_str = orig->child_str;
1556
1557 *copy = new;
1558
1559 return SEPOL_OK;
1560 }
1561
1562 static int cil_copy_src_info(__attribute__((unused)) struct cil_db *db, void *data, void **copy, __attribute__((unused)) symtab_t *symtab)
1563 {
1564 struct cil_src_info *orig = data;
1565 struct cil_src_info *new = NULL;
1566
1567 cil_src_info_init(&new);
1568
1569 new->kind = orig->kind;
1570 new->hll_line = orig->hll_line;
1571 new->path = orig->path;
1572
1573 *copy = new;
1574
1575 return SEPOL_OK;
1576 }
1577
1578 static int __cil_copy_node_helper(struct cil_tree_node *orig, uint32_t *finished, void *extra_args)
1579 {
1580 int rc = SEPOL_ERR;
1581 struct cil_tree_node *parent = NULL;
1582 struct cil_tree_node *new = NULL;
1583 struct cil_db *db = NULL;
1584 struct cil_args_copy *args = NULL;
1585 struct cil_tree_node *namespace = NULL;
1586 enum cil_sym_index sym_index = CIL_SYM_UNKNOWN;
1587 symtab_t *symtab = NULL;
1588 void *data = NULL;
1589 int (*copy_func)(struct cil_db *db, void *data, void **copy, symtab_t *symtab) = NULL;
1590 struct cil_blockinherit *blockinherit = NULL;
1591
1592 if (orig == NULL || extra_args == NULL) {
1593 goto exit;
1594 }
1595
1596 args = extra_args;
1597 parent = args->dest;
1598 db = args->db;
1599
1600
1601 switch (orig->flavor) {
1602 case CIL_BLOCK:
1603 copy_func = &cil_copy_block;
1604 break;
1605 case CIL_BLOCKABSTRACT:
1606 if (args->orig_dest->flavor == CIL_BLOCKINHERIT) {
1607 /* When inheriting a block, don't copy any blockabstract
1608 * statements. Inheriting a block from a block that was
1609 * just inherited never worked. */
1610 return SEPOL_OK;
1611 }
1612 copy_func = &cil_copy_blockabstract;
1613 break;
1614 case CIL_BLOCKINHERIT:
1615 copy_func = &cil_copy_blockinherit;
1616 break;
1617 case CIL_POLICYCAP:
1618 copy_func = &cil_copy_policycap;
1619 break;
1620 case CIL_PERM:
1621 case CIL_MAP_PERM:
1622 copy_func = &cil_copy_perm;
1623 break;
1624 case CIL_CLASSMAPPING:
1625 copy_func = &cil_copy_classmapping;
1626 break;
1627 case CIL_CLASS:
1628 case CIL_COMMON:
1629 case CIL_MAP_CLASS:
1630 copy_func = &cil_copy_class;
1631 break;
1632 case CIL_CLASSORDER:
1633 copy_func = &cil_copy_ordered;
1634 break;
1635 case CIL_CLASSPERMISSION:
1636 copy_func = &cil_copy_classpermission;
1637 break;
1638 case CIL_CLASSPERMISSIONSET:
1639 copy_func = &cil_copy_classpermissionset;
1640 break;
1641 case CIL_CLASSCOMMON:
1642 copy_func = &cil_copy_classcommon;
1643 break;
1644 case CIL_SID:
1645 copy_func = &cil_copy_sid;
1646 break;
1647 case CIL_SIDCONTEXT:
1648 copy_func = &cil_copy_sidcontext;
1649 break;
1650 case CIL_SIDORDER:
1651 copy_func = &cil_copy_ordered;
1652 break;
1653 case CIL_USER:
1654 copy_func = &cil_copy_user;
1655 break;
1656 case CIL_USERATTRIBUTE:
1657 copy_func = &cil_copy_userattribute;
1658 break;
1659 case CIL_USERATTRIBUTESET:
1660 copy_func = &cil_copy_userattributeset;
1661 break;
1662 case CIL_USERROLE:
1663 copy_func = &cil_copy_userrole;
1664 break;
1665 case CIL_USERLEVEL:
1666 copy_func = &cil_copy_userlevel;
1667 break;
1668 case CIL_USERRANGE:
1669 copy_func = &cil_copy_userrange;
1670 break;
1671 case CIL_USERBOUNDS:
1672 copy_func = &cil_copy_bounds;
1673 break;
1674 case CIL_USERPREFIX:
1675 copy_func = &cil_copy_userprefix;
1676 break;
1677 case CIL_ROLE:
1678 copy_func = &cil_copy_role;
1679 break;
1680 case CIL_ROLETYPE:
1681 copy_func = &cil_copy_roletype;
1682 break;
1683 case CIL_ROLEBOUNDS:
1684 copy_func = &cil_copy_bounds;
1685 break;
1686 case CIL_ROLEATTRIBUTE:
1687 copy_func = &cil_copy_roleattribute;
1688 break;
1689 case CIL_ROLEATTRIBUTESET:
1690 copy_func = &cil_copy_roleattributeset;
1691 break;
1692 case CIL_ROLEALLOW:
1693 copy_func = &cil_copy_roleallow;
1694 break;
1695 case CIL_TYPE:
1696 copy_func = &cil_copy_type;
1697 break;
1698 case CIL_TYPEBOUNDS:
1699 copy_func = &cil_copy_bounds;
1700 break;
1701 case CIL_TYPEPERMISSIVE:
1702 copy_func = cil_copy_typepermissive;
1703 break;
1704 case CIL_TYPENEVERAUDIT:
1705 copy_func = cil_copy_typeneveraudit;
1706 break;
1707 case CIL_TYPEATTRIBUTE:
1708 copy_func = &cil_copy_typeattribute;
1709 break;
1710 case CIL_TYPEATTRIBUTESET:
1711 copy_func = &cil_copy_typeattributeset;
1712 break;
1713 case CIL_EXPANDTYPEATTRIBUTE:
1714 copy_func = &cil_copy_expandtypeattribute;
1715 break;
1716 case CIL_TYPEALIAS:
1717 copy_func = &cil_copy_alias;
1718 break;
1719 case CIL_TYPEALIASACTUAL:
1720 copy_func = &cil_copy_aliasactual;
1721 break;
1722 case CIL_ROLETRANSITION:
1723 copy_func = &cil_copy_roletransition;
1724 break;
1725 case CIL_NAMETYPETRANSITION:
1726 copy_func = &cil_copy_nametypetransition;
1727 break;
1728 case CIL_RANGETRANSITION:
1729 copy_func = &cil_copy_rangetransition;
1730 break;
1731 case CIL_TUNABLE:
1732 copy_func = &cil_copy_tunable;
1733 break;
1734 case CIL_BOOL:
1735 copy_func = &cil_copy_bool;
1736 break;
1737 case CIL_AVRULE:
1738 case CIL_AVRULEX:
1739 copy_func = &cil_copy_avrule;
1740 break;
1741 case CIL_PERMISSIONX:
1742 copy_func = &cil_copy_permissionx;
1743 break;
1744 case CIL_DENY_RULE:
1745 copy_func = &cil_copy_deny_rule;
1746 break;
1747 case CIL_TYPE_RULE:
1748 copy_func = &cil_copy_type_rule;
1749 break;
1750 case CIL_SENS:
1751 copy_func = &cil_copy_sens;
1752 break;
1753 case CIL_SENSALIAS:
1754 copy_func = &cil_copy_alias;
1755 break;
1756 case CIL_SENSALIASACTUAL:
1757 copy_func = &cil_copy_aliasactual;
1758 break;
1759 case CIL_CAT:
1760 copy_func = &cil_copy_cat;
1761 break;
1762 case CIL_CATALIAS:
1763 copy_func = &cil_copy_alias;
1764 break;
1765 case CIL_CATALIASACTUAL:
1766 copy_func = &cil_copy_aliasactual;
1767 break;
1768 case CIL_CATSET:
1769 copy_func = &cil_copy_catset;
1770 break;
1771 case CIL_SENSCAT:
1772 copy_func = &cil_copy_senscat;
1773 break;
1774 case CIL_CATORDER:
1775 copy_func = &cil_copy_ordered;
1776 break;
1777 case CIL_SENSITIVITYORDER:
1778 copy_func = &cil_copy_ordered;
1779 break;
1780 case CIL_LEVEL:
1781 copy_func = &cil_copy_level;
1782 break;
1783 case CIL_LEVELRANGE:
1784 copy_func = &cil_copy_levelrange;
1785 break;
1786 case CIL_CONTEXT:
1787 copy_func = &cil_copy_context;
1788 break;
1789 case CIL_NETIFCON:
1790 copy_func = &cil_copy_netifcon;
1791 break;
1792 case CIL_GENFSCON:
1793 copy_func = &cil_copy_genfscon;
1794 break;
1795 case CIL_FILECON:
1796 copy_func = &cil_copy_filecon;
1797 break;
1798 case CIL_NODECON:
1799 copy_func = &cil_copy_nodecon;
1800 break;
1801 case CIL_IBPKEYCON:
1802 copy_func = &cil_copy_ibpkeycon;
1803 break;
1804 case CIL_IBENDPORTCON:
1805 copy_func = &cil_copy_ibendportcon;
1806 break;
1807 case CIL_PORTCON:
1808 copy_func = &cil_copy_portcon;
1809 break;
1810 case CIL_PIRQCON:
1811 copy_func = &cil_copy_pirqcon;
1812 break;
1813 case CIL_IOMEMCON:
1814 copy_func = &cil_copy_iomemcon;
1815 break;
1816 case CIL_IOPORTCON:
1817 copy_func = &cil_copy_ioportcon;
1818 break;
1819 case CIL_PCIDEVICECON:
1820 copy_func = &cil_copy_pcidevicecon;
1821 break;
1822 case CIL_DEVICETREECON:
1823 copy_func = &cil_copy_devicetreecon;
1824 break;
1825 case CIL_FSUSE:
1826 copy_func = &cil_copy_fsuse;
1827 break;
1828 case CIL_CONSTRAIN:
1829 case CIL_MLSCONSTRAIN:
1830 copy_func = &cil_copy_constrain;
1831 break;
1832 case CIL_VALIDATETRANS:
1833 case CIL_MLSVALIDATETRANS:
1834 copy_func = &cil_copy_validatetrans;
1835 break;
1836 case CIL_CALL:
1837 copy_func = &cil_copy_call;
1838 break;
1839 case CIL_MACRO:
1840 copy_func = &cil_copy_macro;
1841 break;
1842 case CIL_NODE:
1843 copy_func = &cil_copy_node;
1844 break;
1845 case CIL_OPTIONAL:
1846 copy_func = &cil_copy_optional;
1847 break;
1848 case CIL_IPADDR:
1849 copy_func = &cil_copy_ipaddr;
1850 break;
1851 case CIL_CONDBLOCK:
1852 copy_func = &cil_copy_condblock;
1853 break;
1854 case CIL_BOOLEANIF:
1855 copy_func = &cil_copy_boolif;
1856 break;
1857 case CIL_TUNABLEIF:
1858 copy_func = &cil_copy_tunif;
1859 break;
1860 case CIL_DEFAULTUSER:
1861 case CIL_DEFAULTROLE:
1862 case CIL_DEFAULTTYPE:
1863 copy_func = &cil_copy_default;
1864 break;
1865 case CIL_DEFAULTRANGE:
1866 copy_func = &cil_copy_defaultrange;
1867 break;
1868 case CIL_HANDLEUNKNOWN:
1869 copy_func = &cil_copy_handleunknown;
1870 break;
1871 case CIL_MLS:
1872 copy_func = &cil_copy_mls;
1873 break;
1874 case CIL_SRC_INFO:
1875 copy_func = &cil_copy_src_info;
1876 break;
1877 default:
1878 goto exit;
1879 }
1880
1881 if (orig->flavor >= CIL_MIN_DECLARATIVE) {
1882 rc = cil_flavor_to_symtab_index(orig->flavor, &sym_index);
1883 if (rc != SEPOL_OK) {
1884 goto exit;
1885 }
1886
1887 rc = cil_get_symtab(parent, &symtab, sym_index);
1888 if (rc != SEPOL_OK) {
1889 goto exit;
1890 }
1891 }
1892
1893 rc = (*copy_func)(db, orig->data, &data, symtab);
1894 if (rc == SEPOL_OK) {
1895 if (orig->flavor == CIL_MACRO && data == NULL) {
1896 /* Skipping macro re-declaration */
1897 if (args->orig_dest->flavor != CIL_BLOCKINHERIT) {
1898 cil_log(CIL_ERR, " Re-declaration of macro is only allowed when inheriting a block\n");
1899 return SEPOL_ERR;
1900 }
1901 *finished = CIL_TREE_SKIP_HEAD;
1902 return SEPOL_OK;
1903 }
1904
1905 cil_tree_node_init(&new);
1906
1907 new->parent = parent;
1908 new->line = orig->line;
1909 new->hll_offset = orig->hll_offset;
1910 new->flavor = orig->flavor;
1911 new->data = data;
1912
1913 if (orig->flavor == CIL_BLOCK && DATUM(data)->nodes->head != NULL) {
1914 /* Duplicate block */
1915 if (args->orig_dest->flavor != CIL_BLOCKINHERIT) {
1916 cil_log(CIL_ERR, " Re-declaration of block is only allowed when inheriting a block\n");
1917 rc = SEPOL_ERR;
1918 goto exit;
1919 }
1920 cil_list_append(DATUM(new->data)->nodes, CIL_NODE, new);
1921 } else if (orig->flavor >= CIL_MIN_DECLARATIVE) {
1922 /* Check the flavor of data if was found in the destination symtab */
1923 if (DATUM(data)->nodes->head && FLAVOR(data) != orig->flavor) {
1924 cil_tree_log(orig, CIL_ERR, "Incompatible flavor when trying to copy %s", DATUM(data)->name);
1925 cil_tree_log(NODE(data), CIL_ERR, "Note: conflicting declaration");
1926 new->flavor = FLAVOR(data);
1927 rc = SEPOL_ERR;
1928 goto exit;
1929 }
1930
1931 rc = cil_add_decl_to_symtab(db, symtab, DATUM(orig->data)->name, DATUM(data), new);
1932 if (rc != SEPOL_OK) {
1933 if (rc == SEPOL_EEXIST) {
1934 cil_symtab_datum_destroy(data);
1935 free(data);
1936 data = NULL;
1937 rc = SEPOL_OK;
1938 } else {
1939 goto exit;
1940 }
1941 }
1942
1943 namespace = new;
1944 while (namespace->flavor != CIL_MACRO && namespace->flavor != CIL_BLOCK && namespace->flavor != CIL_ROOT) {
1945 namespace = namespace->parent;
1946 }
1947
1948 if (namespace->flavor == CIL_MACRO) {
1949 rc = cil_verify_decl_does_not_shadow_macro_parameter(namespace->data, orig, DATUM(orig->data)->name);
1950 if (rc != SEPOL_OK) {
1951 goto exit;
1952 }
1953 }
1954 }
1955
1956 if (new->flavor == CIL_BLOCKINHERIT) {
1957 blockinherit = new->data;
1958 // if a blockinherit statement is copied before blockinherit are
1959 // resolved (like in an in-statement), the block will not have been
1960 // resolved yet, so there's nothing to append yet. This is fine,
1961 // the copied blockinherit statement will be handled later, as if
1962 // it wasn't in an in-statement
1963 if (blockinherit->block != NULL) {
1964 cil_list_append(blockinherit->block->bi_nodes, CIL_NODE, new);
1965 }
1966 }
1967
1968 if (parent->cl_head == NULL) {
1969 parent->cl_head = new;
1970 parent->cl_tail = new;
1971 } else {
1972 parent->cl_tail->next = new;
1973 parent->cl_tail = new;
1974 }
1975
1976 if (orig->cl_head != NULL) {
1977 args->dest = new;
1978 }
1979 } else {
1980 cil_tree_log(orig, CIL_ERR, "Problem copying %s node", cil_node_to_string(orig));
1981 goto exit;
1982 }
1983
1984 return SEPOL_OK;
1985
1986 exit:
1987 cil_tree_node_destroy(&new);
1988 return rc;
1989 }
1990
1991 static int __cil_copy_last_child_helper(__attribute__((unused)) struct cil_tree_node *orig, void *extra_args)
1992 {
1993 struct cil_tree_node *node = NULL;
1994 struct cil_args_copy *args = NULL;
1995
1996 args = extra_args;
1997 node = args->dest;
1998
1999 if (node->flavor != CIL_ROOT) {
2000 args->dest = node->parent;
2001 }
2002
2003 return SEPOL_OK;
2004 }
2005
2006 // dest is the parent node to copy into
2007 // if the copy is for a call to a macro, dest should be a pointer to the call
2008 int cil_copy_ast(struct cil_db *db, struct cil_tree_node *orig, struct cil_tree_node *dest)
2009 {
2010 int rc = SEPOL_ERR;
2011 struct cil_args_copy extra_args;
2012
2013 extra_args.orig_dest = dest;
2014 extra_args.dest = dest;
2015 extra_args.db = db;
2016
2017 rc = cil_tree_walk(orig, __cil_copy_node_helper, NULL, __cil_copy_last_child_helper, &extra_args);
2018 if (rc != SEPOL_OK) {
2019 cil_tree_log(dest, CIL_ERR, "Failed to copy %s to %s", cil_node_to_string(orig), cil_node_to_string(dest));
2020 goto exit;
2021 }
2022
2023 return SEPOL_OK;
2024
2025 exit:
2026 return rc;
2027 }
2028