-
Notifications
You must be signed in to change notification settings - Fork 370
/
Copy pathdirect_api.c
2995 lines (2576 loc) · 73.7 KB
/
direct_api.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
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Author: Jason Tang <jtang@tresys.com>
* Christopher Ashworth <cashworth@tresys.com>
*
* Copyright (C) 2004-2006 Tresys Technology, LLC
* Copyright (C) 2005 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
*/
#include <sepol/module.h>
#include <sepol/handle.h>
#include <sepol/cil/cil.h>
#include <selinux/selinux.h>
#include <assert.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdio_ext.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/wait.h>
#include <limits.h>
#include <errno.h>
#include <dirent.h>
#include "user_internal.h"
#include "seuser_internal.h"
#include "port_internal.h"
#include "ibpkey_internal.h"
#include "ibendport_internal.h"
#include "iface_internal.h"
#include "boolean_internal.h"
#include "fcontext_internal.h"
#include "node_internal.h"
#include "genhomedircon.h"
#include "debug.h"
#include "handle.h"
#include "compressed_file.h"
#include "modules.h"
#include "direct_api.h"
#include "semanage_store.h"
#include "database_policydb.h"
#include "policy.h"
#include "sha256.h"
#define PIPE_READ 0
#define PIPE_WRITE 1
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
static void semanage_direct_destroy(semanage_handle_t * sh);
static int semanage_direct_disconnect(semanage_handle_t * sh);
static int semanage_direct_begintrans(semanage_handle_t * sh);
static int semanage_direct_commit(semanage_handle_t * sh);
static int semanage_direct_install(semanage_handle_t * sh, char *data,
size_t data_len, const char *module_name, const char *lang_ext);
static int semanage_direct_install_file(semanage_handle_t * sh, const char *module_name);
static int semanage_direct_extract(semanage_handle_t * sh,
semanage_module_key_t *modkey,
int extract_cil,
void **mapped_data,
size_t *data_len,
semanage_module_info_t **modinfo);
static int semanage_direct_remove(semanage_handle_t * sh, char *module_name);
static int semanage_direct_list(semanage_handle_t * sh,
semanage_module_info_t ** modinfo,
int *num_modules);
static int semanage_direct_get_enabled(semanage_handle_t *sh,
const semanage_module_key_t *modkey,
int *enabled);
static int semanage_direct_set_enabled(semanage_handle_t *sh,
const semanage_module_key_t *modkey,
int enabled);
static int semanage_direct_get_module_info(semanage_handle_t *sh,
const semanage_module_key_t *modkey,
semanage_module_info_t **modinfo);
static int semanage_direct_list_all(semanage_handle_t *sh,
semanage_module_info_t **modinfo,
int *num_modules);
static int semanage_direct_install_info(semanage_handle_t *sh,
const semanage_module_info_t *modinfo,
char *data,
size_t data_len);
static int semanage_direct_remove_key(semanage_handle_t *sh,
const semanage_module_key_t *modkey);
static struct semanage_policy_table direct_funcs = {
.get_serial = semanage_direct_get_serial,
.destroy = semanage_direct_destroy,
.disconnect = semanage_direct_disconnect,
.begin_trans = semanage_direct_begintrans,
.commit = semanage_direct_commit,
.install = semanage_direct_install,
.extract = semanage_direct_extract,
.install_file = semanage_direct_install_file,
.remove = semanage_direct_remove,
.list = semanage_direct_list,
.get_enabled = semanage_direct_get_enabled,
.set_enabled = semanage_direct_set_enabled,
.get_module_info = semanage_direct_get_module_info,
.list_all = semanage_direct_list_all,
.install_info = semanage_direct_install_info,
.remove_key = semanage_direct_remove_key,
};
int semanage_direct_is_managed(semanage_handle_t * sh)
{
if (semanage_check_init(sh, sh->conf->store_root_path))
goto err;
if (semanage_access_check(sh) < 0)
return 0;
return 1;
err:
ERR(sh, "could not check whether policy is managed");
return STATUS_ERR;
}
/* Check that the module store exists, creating it if necessary.
*/
int semanage_direct_connect(semanage_handle_t * sh)
{
const char *path;
struct stat sb;
if (semanage_check_init(sh, sh->conf->store_root_path))
goto err;
if (sh->create_store)
if (semanage_create_store(sh, 1))
goto err;
sh->u.direct.translock_file_fd = -1;
sh->u.direct.activelock_file_fd = -1;
/* set up function pointers */
sh->funcs = &direct_funcs;
/* Object databases: local modifications */
if (user_base_file_dbase_init(sh,
semanage_path(SEMANAGE_ACTIVE,
SEMANAGE_USERS_BASE_LOCAL),
semanage_path(SEMANAGE_TMP,
SEMANAGE_USERS_BASE_LOCAL),
semanage_user_base_dbase_local(sh)) < 0)
goto err;
if (user_extra_file_dbase_init(sh,
semanage_path(SEMANAGE_ACTIVE,
SEMANAGE_USERS_EXTRA_LOCAL),
semanage_path(SEMANAGE_TMP,
SEMANAGE_USERS_EXTRA_LOCAL),
semanage_user_extra_dbase_local(sh)) < 0)
goto err;
if (user_join_dbase_init(sh,
semanage_user_base_dbase_local(sh),
semanage_user_extra_dbase_local(sh),
semanage_user_dbase_local(sh)) < 0)
goto err;
if (port_file_dbase_init(sh,
semanage_path(SEMANAGE_ACTIVE,
SEMANAGE_PORTS_LOCAL),
semanage_path(SEMANAGE_TMP,
SEMANAGE_PORTS_LOCAL),
semanage_port_dbase_local(sh)) < 0)
goto err;
if (iface_file_dbase_init(sh,
semanage_path(SEMANAGE_ACTIVE,
SEMANAGE_INTERFACES_LOCAL),
semanage_path(SEMANAGE_TMP,
SEMANAGE_INTERFACES_LOCAL),
semanage_iface_dbase_local(sh)) < 0)
goto err;
if (bool_file_dbase_init(sh,
semanage_path(SEMANAGE_ACTIVE,
SEMANAGE_BOOLEANS_LOCAL),
semanage_path(SEMANAGE_TMP,
SEMANAGE_BOOLEANS_LOCAL),
semanage_bool_dbase_local(sh)) < 0)
goto err;
if (fcontext_file_dbase_init(sh,
semanage_path(SEMANAGE_ACTIVE, SEMANAGE_STORE_FC_LOCAL),
semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_FC_LOCAL),
semanage_fcontext_dbase_local(sh)) < 0)
goto err;
if (fcontext_file_dbase_init(sh,
semanage_path(SEMANAGE_ACTIVE, SEMANAGE_STORE_FC_HOMEDIRS),
semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_FC_HOMEDIRS),
semanage_fcontext_dbase_homedirs(sh)) < 0)
goto err;
if (seuser_file_dbase_init(sh,
semanage_path(SEMANAGE_ACTIVE,
SEMANAGE_SEUSERS_LOCAL),
semanage_path(SEMANAGE_TMP,
SEMANAGE_SEUSERS_LOCAL),
semanage_seuser_dbase_local(sh)) < 0)
goto err;
if (node_file_dbase_init(sh,
semanage_path(SEMANAGE_ACTIVE,
SEMANAGE_NODES_LOCAL),
semanage_path(SEMANAGE_TMP,
SEMANAGE_NODES_LOCAL),
semanage_node_dbase_local(sh)) < 0)
goto err;
if (ibpkey_file_dbase_init(sh,
semanage_path(SEMANAGE_ACTIVE,
SEMANAGE_IBPKEYS_LOCAL),
semanage_path(SEMANAGE_TMP,
SEMANAGE_IBPKEYS_LOCAL),
semanage_ibpkey_dbase_local(sh)) < 0)
goto err;
if (ibendport_file_dbase_init(sh,
semanage_path(SEMANAGE_ACTIVE,
SEMANAGE_IBENDPORTS_LOCAL),
semanage_path(SEMANAGE_TMP,
SEMANAGE_IBENDPORTS_LOCAL),
semanage_ibendport_dbase_local(sh)) < 0)
goto err;
/* Object databases: local modifications + policy */
if (user_base_policydb_dbase_init(sh,
semanage_user_base_dbase_policy(sh)) <
0)
goto err;
if (user_extra_file_dbase_init(sh,
semanage_path(SEMANAGE_ACTIVE,
SEMANAGE_USERS_EXTRA),
semanage_path(SEMANAGE_TMP,
SEMANAGE_USERS_EXTRA),
semanage_user_extra_dbase_policy(sh)) <
0)
goto err;
if (user_join_dbase_init(sh,
semanage_user_base_dbase_policy(sh),
semanage_user_extra_dbase_policy(sh),
semanage_user_dbase_policy(sh)) < 0)
goto err;
if (port_policydb_dbase_init(sh, semanage_port_dbase_policy(sh)) < 0)
goto err;
if (ibpkey_policydb_dbase_init(sh, semanage_ibpkey_dbase_policy(sh)) < 0)
goto err;
if (ibendport_policydb_dbase_init(sh, semanage_ibendport_dbase_policy(sh)) < 0)
goto err;
if (iface_policydb_dbase_init(sh, semanage_iface_dbase_policy(sh)) < 0)
goto err;
if (bool_policydb_dbase_init(sh, semanage_bool_dbase_policy(sh)) < 0)
goto err;
if (fcontext_file_dbase_init(sh,
semanage_path(SEMANAGE_ACTIVE, SEMANAGE_STORE_FC),
semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_FC),
semanage_fcontext_dbase_policy(sh)) < 0)
goto err;
if (seuser_file_dbase_init(sh,
semanage_path(SEMANAGE_ACTIVE, SEMANAGE_STORE_SEUSERS),
semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_SEUSERS),
semanage_seuser_dbase_policy(sh)) < 0)
goto err;
if (node_policydb_dbase_init(sh, semanage_node_dbase_policy(sh)) < 0)
goto err;
/* Active kernel policy */
if (bool_activedb_dbase_init(sh, semanage_bool_dbase_active(sh)) < 0)
goto err;
/* set the disable dontaudit value */
path = semanage_path(SEMANAGE_ACTIVE, SEMANAGE_DISABLE_DONTAUDIT);
if (stat(path, &sb) == 0)
sepol_set_disable_dontaudit(sh->sepolh, 1);
else if (errno == ENOENT) {
/* The file does not exist */
sepol_set_disable_dontaudit(sh->sepolh, 0);
} else {
ERR(sh, "Unable to access %s: %s\n", path, strerror(errno));
goto err;
}
return STATUS_SUCCESS;
err:
ERR(sh, "could not establish direct connection");
return STATUS_ERR;
}
static void semanage_direct_destroy(semanage_handle_t * sh
__attribute__ ((unused)))
{
/* do nothing */
}
static int semanage_remove_tmps(semanage_handle_t *sh)
{
if (sh->commit_err)
return 0;
/* destroy sandbox if it exists */
if (semanage_remove_directory
(semanage_path(SEMANAGE_TMP, SEMANAGE_TOPLEVEL)) < 0) {
if (errno != ENOENT) {
ERR(sh, "Could not cleanly remove sandbox %s.",
semanage_path(SEMANAGE_TMP, SEMANAGE_TOPLEVEL));
return -1;
}
}
/* destroy tmp policy if it exists */
if (semanage_remove_directory
(semanage_final_path(SEMANAGE_FINAL_TMP,
SEMANAGE_FINAL_TOPLEVEL)) < 0) {
if (errno != ENOENT) {
ERR(sh, "Could not cleanly remove tmp %s.",
semanage_final_path(SEMANAGE_FINAL_TMP,
SEMANAGE_FINAL_TOPLEVEL));
return -1;
}
}
return 0;
}
static int semanage_direct_disconnect(semanage_handle_t *sh)
{
int retval = 0;
/* destroy transaction and remove tmp files if no commit error */
if (sh->is_in_transaction) {
retval = semanage_remove_tmps(sh);
semanage_release_trans_lock(sh);
}
/* Release object databases: local modifications */
user_base_file_dbase_release(semanage_user_base_dbase_local(sh));
user_extra_file_dbase_release(semanage_user_extra_dbase_local(sh));
user_join_dbase_release(semanage_user_dbase_local(sh));
port_file_dbase_release(semanage_port_dbase_local(sh));
ibpkey_file_dbase_release(semanage_ibpkey_dbase_local(sh));
ibendport_file_dbase_release(semanage_ibendport_dbase_local(sh));
iface_file_dbase_release(semanage_iface_dbase_local(sh));
bool_file_dbase_release(semanage_bool_dbase_local(sh));
fcontext_file_dbase_release(semanage_fcontext_dbase_local(sh));
fcontext_file_dbase_release(semanage_fcontext_dbase_homedirs(sh));
seuser_file_dbase_release(semanage_seuser_dbase_local(sh));
node_file_dbase_release(semanage_node_dbase_local(sh));
/* Release object databases: local modifications + policy */
user_base_policydb_dbase_release(semanage_user_base_dbase_policy(sh));
user_extra_file_dbase_release(semanage_user_extra_dbase_policy(sh));
user_join_dbase_release(semanage_user_dbase_policy(sh));
port_policydb_dbase_release(semanage_port_dbase_policy(sh));
ibpkey_policydb_dbase_release(semanage_ibpkey_dbase_policy(sh));
ibendport_policydb_dbase_release(semanage_ibendport_dbase_policy(sh));
iface_policydb_dbase_release(semanage_iface_dbase_policy(sh));
bool_policydb_dbase_release(semanage_bool_dbase_policy(sh));
fcontext_file_dbase_release(semanage_fcontext_dbase_policy(sh));
seuser_file_dbase_release(semanage_seuser_dbase_policy(sh));
node_policydb_dbase_release(semanage_node_dbase_policy(sh));
/* Release object databases: active kernel policy */
bool_activedb_dbase_release(semanage_bool_dbase_active(sh));
return retval;
}
static int semanage_direct_begintrans(semanage_handle_t * sh)
{
if (semanage_get_trans_lock(sh) < 0) {
return -1;
}
if ((semanage_make_sandbox(sh)) < 0) {
return -1;
}
if ((semanage_make_final(sh)) < 0) {
return -1;
}
return 0;
}
/********************* utility functions *********************/
/* Takes a module stored in 'module_data' and parses its headers.
* Sets reference variables 'module_name' to module's name, and
* 'version' to module's version. The caller is responsible for
* free()ing 'module_name', and 'version'; they will be
* set to NULL upon entering this function. Returns 0 on success, -1
* if out of memory.
*/
static int parse_module_headers(semanage_handle_t * sh, char *module_data,
size_t data_len, char **module_name,
char **version)
{
struct sepol_policy_file *pf;
int file_type;
*module_name = *version = NULL;
if (sepol_policy_file_create(&pf)) {
ERR(sh, "Out of memory!");
return -1;
}
sepol_policy_file_set_mem(pf, module_data, data_len);
sepol_policy_file_set_handle(pf, sh->sepolh);
if (module_data != NULL && data_len > 0)
sepol_module_package_info(pf, &file_type, module_name,
version);
sepol_policy_file_free(pf);
return 0;
}
/* Writes a block of data to a file. Returns 0 on success, -1 on
* error. */
static int write_file(semanage_handle_t * sh,
const char *filename, const char *data, size_t num_bytes)
{
int out;
if ((out =
open(filename, O_WRONLY | O_CREAT | O_TRUNC,
S_IRUSR | S_IWUSR)) == -1) {
ERR(sh, "Could not open %s for writing.", filename);
return -1;
}
if (write(out, data, num_bytes) == -1) {
ERR(sh, "Error while writing to %s.", filename);
close(out);
return -1;
}
close(out);
return 0;
}
static int semanage_direct_update_user_extra(semanage_handle_t * sh, cil_db_t *cildb)
{
const char *ofilename = NULL;
int retval = -1;
char *data = NULL;
size_t size = 0;
dbase_config_t *pusers_extra = semanage_user_extra_dbase_policy(sh);
retval = cil_userprefixes_to_string(cildb, &data, &size);
if (retval != SEPOL_OK) {
goto cleanup;
}
if (size > 0) {
/*
* Write the users_extra entries from CIL modules.
* This file is used as our baseline when we do not require
* re-linking.
*/
ofilename = semanage_path(SEMANAGE_TMP,
SEMANAGE_USERS_EXTRA_LINKED);
if (ofilename == NULL) {
retval = -1;
goto cleanup;
}
retval = write_file(sh, ofilename, data, size);
if (retval < 0)
goto cleanup;
/*
* Write the users_extra file; users_extra.local
* will be merged into this file.
*/
ofilename = semanage_path(SEMANAGE_TMP, SEMANAGE_USERS_EXTRA);
if (ofilename == NULL) {
retval = -1;
goto cleanup;
}
retval = write_file(sh, ofilename, data, size);
if (retval < 0)
goto cleanup;
pusers_extra->dtable->drop_cache(pusers_extra->dbase);
} else {
retval = pusers_extra->dtable->clear(sh, pusers_extra->dbase);
}
cleanup:
free(data);
return retval;
}
static int semanage_direct_update_seuser(semanage_handle_t * sh, cil_db_t *cildb)
{
const char *ofilename = NULL;
int retval = -1;
char *data = NULL;
size_t size = 0;
dbase_config_t *pseusers = semanage_seuser_dbase_policy(sh);
retval = cil_selinuxusers_to_string(cildb, &data, &size);
if (retval != SEPOL_OK) {
goto cleanup;
}
if (size > 0) {
/*
* Write the seusers entries from CIL modules.
* This file is used as our baseline when we do not require
* re-linking.
*/
ofilename = semanage_path(SEMANAGE_TMP,
SEMANAGE_SEUSERS_LINKED);
if (ofilename == NULL) {
retval = -1;
goto cleanup;
}
retval = write_file(sh, ofilename, data, size);
if (retval < 0)
goto cleanup;
/*
* Write the seusers file; seusers.local will be merged into
* this file.
*/
ofilename = semanage_path(SEMANAGE_TMP, SEMANAGE_STORE_SEUSERS);
if (ofilename == NULL) {
retval = -1;
goto cleanup;
}
retval = write_file(sh, ofilename, data, size);
if (retval < 0)
goto cleanup;
pseusers->dtable->drop_cache(pseusers->dbase);
} else {
retval = pseusers->dtable->clear(sh, pseusers->dbase);
}
cleanup:
free(data);
return retval;
}
static int read_from_pipe_to_data(semanage_handle_t *sh, size_t initial_len, int fd, char **out_data_read, size_t *out_read_len)
{
size_t max_len = initial_len;
size_t read_len = 0;
size_t data_read_len = 0;
char *data_read = NULL;
if (max_len <= 0) {
max_len = 1;
}
data_read = malloc(max_len * sizeof(*data_read));
if (data_read == NULL) {
ERR(sh, "Failed to malloc, out of memory.\n");
return -1;
}
while ((read_len = read(fd, data_read + data_read_len, max_len - data_read_len)) > 0) {
data_read_len += read_len;
if (data_read_len == max_len) {
max_len *= 2;
data_read = realloc(data_read, max_len);
if (data_read == NULL) {
ERR(sh, "Failed to realloc, out of memory.\n");
return -1;
}
}
}
*out_read_len = data_read_len;
*out_data_read = data_read;
return 0;
}
static int semanage_pipe_data(semanage_handle_t *sh, char *path, char *in_data, size_t in_data_len, char **out_data, size_t *out_data_len, char **err_data, size_t *err_data_len)
{
int input_fd[2] = {-1, -1};
int output_fd[2] = {-1, -1};
int err_fd[2] = {-1, -1};
pid_t pid;
char *data_read = NULL;
char *err_data_read = NULL;
int retval;
int status = 0;
size_t initial_len;
size_t data_read_len = 0;
size_t err_data_read_len = 0;
struct sigaction old_signal;
struct sigaction new_signal;
new_signal.sa_handler = SIG_IGN;
sigemptyset(&new_signal.sa_mask);
new_signal.sa_flags = 0;
/* This is needed in case the read end of input_fd is closed causing a SIGPIPE signal to be sent.
* If SIGPIPE is not caught, the signal will cause semanage to terminate immediately. The sigaction below
* creates a new_signal that ignores SIGPIPE allowing the write to exit cleanly.
*
* Another sigaction is called in cleanup to restore the original behavior when a SIGPIPE is received.
*/
sigaction(SIGPIPE, &new_signal, &old_signal);
retval = pipe(input_fd);
if (retval == -1) {
ERR(sh, "Unable to create pipe for input pipe: %s\n", strerror(errno));
goto cleanup;
}
retval = pipe(output_fd);
if (retval == -1) {
ERR(sh, "Unable to create pipe for output pipe: %s\n", strerror(errno));
goto cleanup;
}
retval = pipe(err_fd);
if (retval == -1) {
ERR(sh, "Unable to create pipe for error pipe: %s\n", strerror(errno));
goto cleanup;
}
pid = fork();
if (pid == -1) {
ERR(sh, "Unable to fork from parent: %s.", strerror(errno));
retval = -1;
goto cleanup;
} else if (pid == 0) {
retval = dup2(input_fd[PIPE_READ], STDIN_FILENO);
if (retval == -1) {
ERR(sh, "Unable to dup2 input pipe: %s\n", strerror(errno));
goto cleanup;
}
retval = dup2(output_fd[PIPE_WRITE], STDOUT_FILENO);
if (retval == -1) {
ERR(sh, "Unable to dup2 output pipe: %s\n", strerror(errno));
goto cleanup;
}
retval = dup2(err_fd[PIPE_WRITE], STDERR_FILENO);
if (retval == -1) {
ERR(sh, "Unable to dup2 error pipe: %s\n", strerror(errno));
goto cleanup;
}
retval = close(input_fd[PIPE_WRITE]);
if (retval == -1) {
ERR(sh, "Unable to close input pipe: %s\n", strerror(errno));
goto cleanup;
}
retval = close(output_fd[PIPE_READ]);
if (retval == -1) {
ERR(sh, "Unable to close output pipe: %s\n", strerror(errno));
goto cleanup;
}
retval = close(err_fd[PIPE_READ]);
if (retval == -1) {
ERR(sh, "Unable to close error pipe: %s\n", strerror(errno));
goto cleanup;
}
retval = execl(path, path, NULL);
if (retval == -1) {
ERR(sh, "Unable to execute %s : %s\n", path, strerror(errno));
_exit(EXIT_FAILURE);
}
} else {
retval = close(input_fd[PIPE_READ]);
input_fd[PIPE_READ] = -1;
if (retval == -1) {
ERR(sh, "Unable to close read end of input pipe: %s\n", strerror(errno));
goto cleanup;
}
retval = close(output_fd[PIPE_WRITE]);
output_fd[PIPE_WRITE] = -1;
if (retval == -1) {
ERR(sh, "Unable to close write end of output pipe: %s\n", strerror(errno));
goto cleanup;
}
retval = close(err_fd[PIPE_WRITE]);
err_fd[PIPE_WRITE] = -1;
if (retval == -1) {
ERR(sh, "Unable to close write end of error pipe: %s\n", strerror(errno));
goto cleanup;
}
retval = write(input_fd[PIPE_WRITE], in_data, in_data_len);
if (retval == -1) {
ERR(sh, "Failed to write data to input pipe: %s\n", strerror(errno));
goto cleanup;
}
retval = close(input_fd[PIPE_WRITE]);
input_fd[PIPE_WRITE] = -1;
if (retval == -1) {
ERR(sh, "Unable to close write end of input pipe: %s\n", strerror(errno));
goto cleanup;
}
initial_len = 1 << 17;
retval = read_from_pipe_to_data(sh, initial_len, output_fd[PIPE_READ], &data_read, &data_read_len);
if (retval != 0) {
goto cleanup;
}
retval = close(output_fd[PIPE_READ]);
output_fd[PIPE_READ] = -1;
if (retval == -1) {
ERR(sh, "Unable to close read end of output pipe: %s\n", strerror(errno));
goto cleanup;
}
initial_len = 1 << 9;
retval = read_from_pipe_to_data(sh, initial_len, err_fd[PIPE_READ], &err_data_read, &err_data_read_len);
if (retval != 0) {
goto cleanup;
}
retval = close(err_fd[PIPE_READ]);
err_fd[PIPE_READ] = -1;
if (retval == -1) {
ERR(sh, "Unable to close read end of error pipe: %s\n", strerror(errno));
goto cleanup;
}
if (waitpid(pid, &status, 0) == -1 || !WIFEXITED(status)) {
ERR(sh, "Child process %s did not exit cleanly.", path);
retval = -1;
goto cleanup;
}
if (WEXITSTATUS(status) != 0) {
ERR(sh, "Child process %s failed with code: %d.", path, WEXITSTATUS(status));
retval = -1;
goto cleanup;
}
}
retval = 0;
cleanup:
sigaction(SIGPIPE, &old_signal, NULL);
if (data_read != NULL) {
*out_data = data_read;
*out_data_len = data_read_len;
}
if (err_data_read != NULL) {
*err_data = err_data_read;
*err_data_len = err_data_read_len;
}
if (output_fd[PIPE_READ] != -1) {
close(output_fd[PIPE_READ]);
}
if (output_fd[PIPE_WRITE] != -1) {
close(output_fd[PIPE_WRITE]);
}
if (err_fd[PIPE_READ] != -1) {
close(err_fd[PIPE_READ]);
}
if (err_fd[PIPE_WRITE] != -1) {
close(err_fd[PIPE_WRITE]);
}
if (input_fd[PIPE_READ] != -1) {
close(input_fd[PIPE_READ]);
}
if (input_fd[PIPE_WRITE] != -1) {
close(input_fd[PIPE_WRITE]);
}
return retval;
}
static int semanage_direct_write_langext(semanage_handle_t *sh,
const char *lang_ext,
const semanage_module_info_t *modinfo)
{
int ret = -1;
char fn[PATH_MAX];
FILE *fp = NULL;
ret = semanage_module_get_path(sh,
modinfo,
SEMANAGE_MODULE_PATH_LANG_EXT,
fn,
sizeof(fn));
if (ret != 0) {
goto cleanup;
}
fp = fopen(fn, "w");
if (fp == NULL) {
ERR(sh, "Unable to open %s module ext file.", modinfo->name);
ret = -1;
goto cleanup;
}
if (fputs(lang_ext, fp) < 0) {
ERR(sh, "Unable to write %s module ext file.", modinfo->name);
ret = -1;
goto cleanup;
}
if (fclose(fp) != 0) {
ERR(sh, "Unable to close %s module ext file.", modinfo->name);
fp = NULL;
ret = -1;
goto cleanup;
}
fp = NULL;
ret = 0;
cleanup:
if (fp != NULL) fclose(fp);
return ret;
}
static void update_checksum_with_len(Sha256Context *context, size_t s)
{
int i;
uint8_t buffer[8];
for (i = 0; i < 8; i++) {
buffer[i] = s & 0xff;
s >>= 8;
}
Sha256Update(context, buffer, 8);
}
static void update_checksum_with_bool(Sha256Context *context, bool b)
{
uint8_t byte;
byte = b ? UINT8_C(1) : UINT8_C(0);
Sha256Update(context, &byte, 1);
}
static int semanage_compile_module(semanage_handle_t *sh,
semanage_module_info_t *modinfo,
Sha256Context *context)
{
char cil_path[PATH_MAX];
char hll_path[PATH_MAX];
char *compiler_path = NULL;
char *cil_data = NULL;
char *err_data = NULL;
char *start = NULL;
char *end = NULL;
int status = 0;
size_t cil_data_len = 0;
size_t err_data_len = 0;
struct file_contents hll_contents = {};
if (!strcasecmp(modinfo->lang_ext, "cil")) {
goto cleanup;
}
status = semanage_get_hll_compiler_path(sh, modinfo->lang_ext, &compiler_path);
if (status != 0) {
goto cleanup;
}
status = semanage_module_get_path(
sh,
modinfo,
SEMANAGE_MODULE_PATH_CIL,
cil_path,
sizeof(cil_path));
if (status != 0) {
goto cleanup;
}
status = semanage_module_get_path(
sh,
modinfo,
SEMANAGE_MODULE_PATH_HLL,
hll_path,
sizeof(hll_path));
if (status != 0) {
goto cleanup;
}
status = map_compressed_file(sh, hll_path, &hll_contents);
if (status < 0) {
ERR(sh, "Unable to read file %s\n", hll_path);
goto cleanup;
}
status = semanage_pipe_data(sh, compiler_path, hll_contents.data,
hll_contents.len, &cil_data, &cil_data_len,
&err_data, &err_data_len);
if (err_data_len > 0) {
for (start = end = err_data; end < err_data + err_data_len; end++) {
if (*end == '\n') {
fprintf(stderr, "%s: ", modinfo->name);
fwrite(start, 1, end - start + 1, stderr);
start = end + 1;
}
}
if (end != start) {
fprintf(stderr, "%s: ", modinfo->name);
fwrite(start, 1, end - start, stderr);
fprintf(stderr, "\n");
}
}
if (status != 0) {
goto cleanup;
}
if (context) {
update_checksum_with_len(context, cil_data_len);
Sha256Update(context, cil_data, cil_data_len);
}
status = write_compressed_file(sh, cil_path, cil_data, cil_data_len);
if (status == -1) {
ERR(sh, "Failed to write %s\n", cil_path);
goto cleanup;
}
if (sh->conf->remove_hll == 1) {
status = unlink(hll_path);
if (status != 0) {
ERR(sh, "Error while removing HLL file %s: %s", hll_path, strerror(errno));
goto cleanup;
}
status = semanage_direct_write_langext(sh, "cil", modinfo);
if (status != 0) {
goto cleanup;
}
}
cleanup:
unmap_compressed_file(&hll_contents);
free(cil_data);
free(err_data);
free(compiler_path);
return status;
}
static int modinfo_cmp(const void *a, const void *b)
{
const semanage_module_info_t *ma = a;
const semanage_module_info_t *mb = b;
return strcmp(ma->name, mb->name);
}
struct extra_checksum_params {
int disable_dontaudit;
int preserve_tunables;
int target_platform;
int policyvers;
};
static int semanage_compile_hll_modules(semanage_handle_t *sh,
semanage_module_info_t *modinfos,
int num_modinfos,
const struct extra_checksum_params *extra,
char *cil_checksum)
{