forked from multirom-dev/multirom
-
Notifications
You must be signed in to change notification settings - Fork 12
/
rq_inject_file_contexts.c
635 lines (519 loc) · 21.7 KB
/
rq_inject_file_contexts.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
/*
* This file is part of MultiROM.
*
* MultiROM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* MultiROM 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MultiROM. If not, see <http://www.gnu.org/licenses/>.
*/
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "rq_inject_file_contexts.h"
#ifndef TARGET_RECOVERY_IS_MULTIROM
#include "lib/log.h"
#else
#include "twcommon.h"
#define INFO(...) LOGINFO("MultiROM " __VA_ARGS__)
#define ERROR(...) LOGINFO("MultiROM " __VA_ARGS__)
#endif
#ifndef UNUSED
#define UNUSED __attribute__((unused))
#endif
#ifndef ARRAY_SIZE
#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
#endif
// NOTE: stems are still hardcoded to 3 possibilities:
// "/data", "/realdata" and "/mnt"
const char *multirom_exclusion_path[] = {
"/data/media/multirom",
"/data/media/0/multirom",
"/realdata/media/multirom",
"/realdata/media/0/multirom",
"/data/media/MultiROM",
"/data/media/0/MultiROM",
"/realdata/media/MultiROM",
"/realdata/media/0/MultiROM",
"/mnt/mrom",
};
#define REGEX_PATTERN "(/.*)?"
#define CONTEXT_STRING "<<none>>"
/* Inject text format file_contexts */
static int inject_file_contexts_text(const char *path)
{
FILE *f;
char line[512];
unsigned int i;
f = fopen(path, "re");
if (!f) {
ERROR("Failed to open /file_contexts!\n");
return -1;
}
while (fgets(line, sizeof(line), f)) {
if (strncmp(line, multirom_exclusion_path[0], strlen(multirom_exclusion_path[0])) == 0) {
INFO("/file_contexts has been already injected.\n");
fclose(f);
return 0;
}
}
fclose(f);
INFO("Injecting /file_contexts\n");
f = fopen(path, "ae");
if (!f) {
ERROR("Failed to open /file_contexts for appending!\n");
return -1;
}
fputs("\n"
"# MultiROM folders\n", f);
for (i = 0; i < ARRAY_SIZE(multirom_exclusion_path); ++i) {
fprintf(f, "%s%s %s\n", multirom_exclusion_path[i], REGEX_PATTERN, CONTEXT_STRING);
}
fclose(f);
return 0;
}
/* ************************************************************************************************************************************************ */
/*
* Functions to deal with bin formatted file_contexts (ie compiled regex)
*
*/
// The structure and content of the compiled regex_info and study_data
// was extrapolated from '/data/media(/.*)?' compiled regex, and then
// hardcoded here, which seems to work fine for now.
//
// Alternatively, we can decompile the existing file_contexts.bin and
// find the '/data/media(/.*)?' compiled regex, and use it to build
// the multirom exclusion regexs; this may actually be a better (if possible)
// solution than hardcoding, but for the time being all the file_contexts.bin
// I've seen are all version 4, and have the same structure:
//
// Offset(h) 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
//
// 00000000 21 00 00 00 75 3A 6F 62 6A 65 63 74 5F 72 3A 6D !...u:object_r:m
// 00000010 65 64 69 61 5F 72 77 5F 64 61 74 61 5F 66 69 6C edia_rw_data_fil
// 00000020 65 3A 73 30 00 12 00 00 00 2F 64 61 74 61 2F 6D e:s0...../data/m
// 00000030 65 64 69 61 28 2F 2E 2A 29 3F 00 00 00 00 00 03 edia(/.*)?......
// 00000040 00 00 00 01 00 00 00 0B 00 00 00 62 00 00 00 45 ...........b...E
// 00000050 52 43 50 62 00 00 00 14 00 00 00 01 00 00 00 FF RCPb...........ÿ
// 00000060 FF FF FF FF FF FF FF 00 00 00 00 00 00 01 00 00 ÿÿÿÿÿÿÿ.........
// 00000070 00 40 00 00 00 00 00 00 00 00 00 00 00 00 00 00 .@..............
// 00000080 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 83 ...............ƒ
// 00000090 00 1E 1B 1D 2F 1D 6D 1D 65 1D 64 1D 69 1D 61 92 ..../.m.e.d.i.a’
// 000000A0 85 00 09 00 01 1D 2F 55 0D 78 00 09 19 78 00 1E …...../U.x...x..
// 000000B0 00 2C 00 00 00 2C 00 00 00 02 00 00 00 00 00 00 .,...,..........
// 000000C0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
// 000000D0 00 00 00 00 00 00 00 00 00 00 00 00 00 06 00 00 ................
// 000000E0 00 .
#define COMPILED_ERCP_P1 (char[]) { \
0x14, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, \
0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, \
0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x83, 0x00 \
}
#define COMPILED_ERCP_P2 (char[]) { 0x92, 0x85, 0x00, 0x09, 0x00, 0x01, 0x1D, 0x2F, 0x55, 0x0D, 0x78, 0x00, 0x09, 0x19, 0x78, 0x00 }
#define SELINUX_MAGIC_COMPILED_FCONTEXT 0xf97cff8a
/* Version specific changes */
#define SELINUX_COMPILED_FCONTEXT_NOPCRE_VERS 1
#define SELINUX_COMPILED_FCONTEXT_PCRE_VERS 2
#define SELINUX_COMPILED_FCONTEXT_MODE 3
#define SELINUX_COMPILED_FCONTEXT_PREFIX_LEN 4
#define SELINUX_COMPILED_FCONTEXT_REGEX_ARCH 5
#define SELINUX_COMPILED_FCONTEXT_MAX_VERS \
SELINUX_COMPILED_FCONTEXT_REGEX_ARCH
/* A regular expression stem */
typedef struct {
uint32_t stem_len;
char *stem_string;
} stem_s;
typedef struct {
uint32_t len_context_string;
char * str_context_string;
uint32_t len_regex_string;
char * str_regex_string;
uint32_t mode_bits;
int32_t stem_id;
uint32_t spec_has_meta_chars;
uint32_t prefix_len;
uint32_t len_raw_pcre_regex_info;
char * buf_raw_pcre_regex_info;
uint32_t len_raw_pcre_regex_study_data;
char * buf_raw_pcre_regex_study_data;
} regex_s;
static uint8_t calc_len_raw_pcre_regex_subpart(const char *exclusion_path)
{
char *path_less_stem = strchr(exclusion_path + 1, '/');
return (sizeof(uint8_t) + (strlen(path_less_stem) * 2) + sizeof(COMPILED_ERCP_P2) + sizeof(uint8_t));
}
static uint32_t calc_len_raw_pcre_regex_info(const char *exclusion_path)
{
uint32_t r = 0;
char *path_less_stem = strchr(exclusion_path + 1, '/');
r += sizeof(uint32_t); // MAGIC
r += sizeof(uint32_t); // len_raw_pcre_regex_info
r += sizeof(COMPILED_ERCP_P1); //
r += sizeof(uint8_t); // len_raw_pcre_regex_subpart
r += sizeof(char); //
r += strlen(path_less_stem) * 2; // length of the unicode string
r += sizeof(COMPILED_ERCP_P2); //
r += sizeof(uint8_t); // len_raw_pcre_regex_subpart
r += sizeof(uint8_t); // null
return r;
}
static char *construct_raw_pcre_regex_info(char *dest, const char *exclusion_path)
{
char *buf = dest;
size_t off = 0;
char *path_less_stem = strchr(exclusion_path + 1, '/');
uint32_t len_raw_pcre_regex_info = calc_len_raw_pcre_regex_info(exclusion_path);
uint8_t len_raw_pcre_regex_subpart = calc_len_raw_pcre_regex_subpart(exclusion_path);
memcpy(buf + off, "ERCP", 4);
off += 4;
memcpy(buf + off, &len_raw_pcre_regex_info, sizeof(uint32_t));
off += sizeof(uint32_t);
memcpy(buf + off, COMPILED_ERCP_P1, sizeof(COMPILED_ERCP_P1));
off += sizeof(COMPILED_ERCP_P1);
memcpy(buf + off, &len_raw_pcre_regex_subpart, sizeof(uint8_t));
off += sizeof(uint8_t);
memcpy(buf + off, (char[]){ 0x1B }, 1);
off += 1;
// copy as unicode string, without trailing null
while (path_less_stem[0])
{
buf[off++] = 0x1D;
buf[off++] = path_less_stem[0];
path_less_stem++;
}
memcpy(buf + off, COMPILED_ERCP_P2, sizeof(COMPILED_ERCP_P2));
off += sizeof(COMPILED_ERCP_P2);
memcpy(buf + off, &len_raw_pcre_regex_subpart, sizeof(uint8_t));
off += sizeof(uint8_t);
memcpy(buf + off, (char[]){ 0x00 }, 1);
off += 1;
return dest;
}
typedef struct {
uint32_t len_raw_pcre_regex_study_data;
uint32_t unknown02;
uint32_t null_1;
uint32_t null_2;
uint32_t null_3;
uint32_t null_4;
uint32_t null_5;
uint32_t null_6;
uint32_t null_7;
uint32_t null_9;
uint32_t len_path_less_stem;
} raw_pcre_regex_study_data_s;
static uint32_t calc_len_raw_pcre_regex_study_data(UNUSED const char *exclusion_path)
{
return sizeof(raw_pcre_regex_study_data_s);
}
static uint32_t calc_len_path_less_stem(const char *exclusion_path)
{
return strlen(strchr(exclusion_path + 1, '/'));
}
static char *construct_raw_pcre_regex_study_data(char *dest, const char *exclusion_path)
{
uint32_t len_raw_pcre_regex_study_data = calc_len_raw_pcre_regex_study_data(exclusion_path);
uint32_t len_path_less_stem = calc_len_path_less_stem(exclusion_path);
memset(dest, 0, sizeof(raw_pcre_regex_study_data_s));
((raw_pcre_regex_study_data_s *)dest)->len_raw_pcre_regex_study_data = len_raw_pcre_regex_study_data;
((raw_pcre_regex_study_data_s *)dest)->unknown02 = 2;
((raw_pcre_regex_study_data_s *)dest)->len_path_less_stem = len_path_less_stem;
return dest;
}
/* Inject binary format file_contexts */
// TODO: add error handling
static int inject_file_contexts_bin(const char *path)
{
/*
* File Format
*
* u32 - magic number
* u32 - version
* u32 - length of pcre version EXCLUDING nul
* char - pcre version string EXCLUDING nul
* u32 - number of stems
* ** Stems
* u32 - length of stem EXCLUDING nul
* char - stem char array INCLUDING nul
* u32 - number of regexs
* ** Regexes
* u32 - length of upcoming context INCLUDING nul
* char - char array of the raw context
* u32 - length of the upcoming regex_str
* char - char array of the original regex string including the stem.
* u32 - mode bits for >= SELINUX_COMPILED_FCONTEXT_MODE
* mode_t for <= SELINUX_COMPILED_FCONTEXT_PCRE_VERS
* s32 - stemid associated with the regex
* u32 - spec has meta characters
* u32 - The specs prefix_len if >= SELINUX_COMPILED_FCONTEXT_PREFIX_LEN
* u32 - data length of the pcre regex
* char - a bufer holding the raw pcre regex info
* u32 - data length of the pcre regex study daya
* char - a buffer holding the raw pcre regex study data
*/
FILE *bin_file_in;
FILE *bin_file_out;
size_t len;
// header
uint32_t magic;
uint32_t version;
uint32_t reg_version_len;
char *reg_version_string;
uint32_t regex_arch_len;
char *regex_arch_string;
bin_file_in = fopen(path, "rb");
if (!bin_file_in) {
ERROR("Failed to open '%s' for reading!\n", path);
return -1;
}
/* check if this looks like an fcontext file */
len = fread(&magic, sizeof(uint32_t), 1, bin_file_in);
if (len != 1 || magic != SELINUX_MAGIC_COMPILED_FCONTEXT)
{
fclose(bin_file_in);
return -1;
}
/* check if this version is higher than we understand */
len = fread(&version, sizeof(uint32_t), 1, bin_file_in);
if (len != 1 || version != 4) // if (len != 1 || version > SELINUX_COMPILED_FCONTEXT_MAX_VERS)
{
// we currently only support version 4, all the ones i have seen are version 4 at the moment
ERROR("Unsupported /file_contexts.bin version %d\n", version);
fclose(bin_file_in);
return -1;
}
// we can process this, let's open the output file and start
char *tmp_name = NULL;
const int size = strlen(path) + 5;
tmp_name = malloc(size);
snprintf(tmp_name, size, "%s-new", path);
bin_file_out = fopen(tmp_name, "w");
if (!bin_file_out) {
ERROR("Failed to open '%s' for writing!\n", tmp_name);
fclose(bin_file_in);
free(tmp_name);
return -1;
}
/* write some magic number */
len = fwrite(&magic, sizeof(uint32_t), 1, bin_file_out);
/* write the version */
len = fwrite(&version, sizeof(uint32_t), 1, bin_file_out);
if (version >= SELINUX_COMPILED_FCONTEXT_PCRE_VERS) {
/* read version of the regex back-end */
len = fread(®_version_len, sizeof(uint32_t), 1, bin_file_in);
reg_version_string = malloc(reg_version_len + 1);
len = fread(reg_version_string, sizeof(char), reg_version_len, bin_file_in);
reg_version_string[reg_version_len] = '\0';
/* write version of the regex back-end */
len = fwrite(®_version_len, sizeof(uint32_t), 1, bin_file_out);
len = fwrite(reg_version_string, sizeof(char), reg_version_len, bin_file_out);
free(reg_version_string);
if (version >= SELINUX_COMPILED_FCONTEXT_REGEX_ARCH) {
/* read regex arch string */
len = fread(®ex_arch_len, sizeof(uint32_t), 1, bin_file_in);
regex_arch_string = malloc(regex_arch_len + 1);
len = fread(regex_arch_string, sizeof(char), regex_arch_len, bin_file_in);
regex_arch_string[regex_arch_len] = '\0';
/* write regex arch string */
len = fwrite(®ex_arch_len, sizeof(uint32_t), 1, bin_file_out);
len = fwrite(regex_arch_string, sizeof(char), regex_arch_len, bin_file_out);
free(regex_arch_string);
}
}
// read in the stems, check for missing and add
uint32_t i;
uint32_t number_of_stems;
int32_t data_stem_id = -1;
int32_t realdata_stem_id = -1;
int32_t mnt_stem_id = -1;
/* read the number of stems coming */
len = fread(&number_of_stems, sizeof(uint32_t), 1, bin_file_in);
stem_s *stem_array;
stem_array = malloc((number_of_stems + 3) * sizeof(stem_s)); // add 3 since mrom additions could use them
for (i = 0; i < number_of_stems; i++)
{
/* read the strlen (aka no nul) */
len = fread(&stem_array[i].stem_len, sizeof(uint32_t), 1, bin_file_in);
/* include the nul in the file */
stem_array[i].stem_string = malloc(stem_array[i].stem_len + 1);
len = fread(stem_array[i].stem_string, sizeof(char), stem_array[i].stem_len + 1, bin_file_in);
if (strcmp(stem_array[i].stem_string, "/data") == 0)
data_stem_id = i;
else if (strcmp(stem_array[i].stem_string, "/realdata") == 0)
realdata_stem_id = i;
else if (strcmp(stem_array[i].stem_string, "/mnt") == 0)
mnt_stem_id = i;
}
// if 'realdata' already exists assume multirom exclusions are present
// this should be faster than finding 'multirom' in the regexs
if (realdata_stem_id != -1)
{
INFO("/file_contexts.bin has been already injected.\n");
for (i = 0; i < number_of_stems; i++)
free(stem_array[i].stem_string);
free(stem_array);
goto noerr;
}
INFO("Injecting /file_contexts.bin\n");
// add new stems here
if (data_stem_id == -1)
{
stem_array[i].stem_string = strdup("/data");
stem_array[number_of_stems].stem_len = strlen(stem_array[i].stem_string);
data_stem_id = number_of_stems;
number_of_stems += 1;
}
if (realdata_stem_id == -1)
{
stem_array[i].stem_string = strdup("/realdata");
stem_array[number_of_stems].stem_len = strlen(stem_array[i].stem_string);
realdata_stem_id = number_of_stems;
number_of_stems += 1;
}
if (mnt_stem_id == -1)
{
stem_array[i].stem_string = strdup("/mnt");
stem_array[number_of_stems].stem_len = strlen(stem_array[i].stem_string);
mnt_stem_id = number_of_stems;
number_of_stems += 1;
}
/* write the number of stems coming */
len = fwrite(&number_of_stems, sizeof(uint32_t), 1, bin_file_out);
for (i = 0; i < number_of_stems; i++)
{
/* write the strlen (aka no nul) */
len = fwrite(&stem_array[i].stem_len, sizeof(uint32_t), 1, bin_file_out);
/* include the nul in the file */
len = fwrite(stem_array[i].stem_string, sizeof(char), stem_array[i].stem_len + 1, bin_file_out);
free(stem_array[i].stem_string);
}
free(stem_array);
uint32_t number_of_regexs;
/* read the number of regexes coming */
len = fread(&number_of_regexs, sizeof(uint32_t), 1, bin_file_in);
/* write the number of regexes coming */
number_of_regexs += ARRAY_SIZE(multirom_exclusion_path); // add mrom exclusions count
len = fwrite(&number_of_regexs, sizeof(uint32_t), 1, bin_file_out);
//// now write (actually copy/paste) the normal regexs back since we don't want to
//// read and parse them in, do nothing and write them back
#define BUFFER_SIZE 1024*1024
char *buf = malloc(BUFFER_SIZE);
while ((len = fread(buf, 1, BUFFER_SIZE, bin_file_in)) > 0)
{
len = fwrite(buf, 1, len, bin_file_out);
}
free(buf);
// now adjust the stem_id and write the multirom exclusions
for (i = 0; i < ARRAY_SIZE(multirom_exclusion_path); ++i)
{
regex_s regex;
// build the compiled regex
regex.str_context_string = CONTEXT_STRING,
regex.len_context_string = strlen(regex.str_context_string) + 1;
regex.str_regex_string = malloc(strlen(multirom_exclusion_path[i]) + strlen(REGEX_PATTERN) + 1);
strcpy(regex.str_regex_string, multirom_exclusion_path[i]);
strcat(regex.str_regex_string, REGEX_PATTERN);
regex.len_regex_string = strlen(regex.str_regex_string) + 1;
regex.prefix_len = strlen(multirom_exclusion_path[i]);
regex.mode_bits = 0x00000000;
regex.spec_has_meta_chars = 0x00000001;
if (strncmp(regex.str_regex_string, "/data", sizeof("/data") - 1) == 0)
regex.stem_id = data_stem_id;
else if (strncmp(regex.str_regex_string, "/realdata", sizeof("/realdata") - 1) == 0)
regex.stem_id = realdata_stem_id;
else if (strncmp(regex.str_regex_string, "/mnt", sizeof("/mnt") - 1) == 0)
regex.stem_id = mnt_stem_id;
// else error
// now write the compiled regex
len = fwrite(®ex.len_context_string, sizeof(uint32_t), 1, bin_file_out);
len = fwrite(regex.str_context_string, sizeof(char), regex.len_context_string, bin_file_out);
len = fwrite(®ex.len_regex_string, sizeof(uint32_t), 1, bin_file_out);
len = fwrite(regex.str_regex_string, sizeof(char), regex.len_regex_string, bin_file_out);
len = fwrite(®ex.mode_bits, sizeof(uint32_t), 1, bin_file_out);
len = fwrite(®ex.stem_id, sizeof(int32_t), 1, bin_file_out);
len = fwrite(®ex.spec_has_meta_chars, sizeof(uint32_t), 1, bin_file_out);
len = fwrite(®ex.prefix_len, sizeof(uint32_t), 1, bin_file_out);
free(regex.str_regex_string);
// construct here (ie we're constructing it from extrapolating from '/data/media(/.*)?', not generating a compiled version)
regex.len_raw_pcre_regex_info = (uint32_t)calc_len_raw_pcre_regex_info(multirom_exclusion_path[i]);
regex.buf_raw_pcre_regex_info = malloc(regex.len_raw_pcre_regex_info);
if(regex.buf_raw_pcre_regex_info)
{
construct_raw_pcre_regex_info(regex.buf_raw_pcre_regex_info, multirom_exclusion_path[i]);
len = fwrite(®ex.len_raw_pcre_regex_info, sizeof(uint32_t), 1, bin_file_out);
len = fwrite(regex.buf_raw_pcre_regex_info, sizeof(char), regex.len_raw_pcre_regex_info, bin_file_out);
free(regex.buf_raw_pcre_regex_info);
}
//else error
regex.len_raw_pcre_regex_study_data = (uint32_t)calc_len_raw_pcre_regex_study_data(multirom_exclusion_path[i]);
regex.buf_raw_pcre_regex_study_data = malloc(regex.len_raw_pcre_regex_study_data);
if(regex.buf_raw_pcre_regex_study_data)
{
construct_raw_pcre_regex_study_data(regex.buf_raw_pcre_regex_study_data, multirom_exclusion_path[i]);
len = fwrite(®ex.len_raw_pcre_regex_study_data, sizeof(uint32_t), 1, bin_file_out);
len = fwrite(regex.buf_raw_pcre_regex_study_data, sizeof(char), regex.len_raw_pcre_regex_study_data, bin_file_out);
free(regex.buf_raw_pcre_regex_study_data);
}
//else error
}
out:
fclose(bin_file_out);
fclose(bin_file_in);
rename(tmp_name, path);
chmod(path, 0644);
#if 0
// in case we need to debug
copy_file(path, "/cache/file_contexts.bin-new");
#endif
free(tmp_name);
return 0;
noerr:
fclose(bin_file_out);
fclose(bin_file_in);
remove(tmp_name);
free(tmp_name);
return 0;
err:
fclose(bin_file_out);
fclose(bin_file_in);
remove(tmp_name);
free(tmp_name);
return -2;
}
/* ************************************************************************************************************************************************ */
/*
* Main inject_file_contexts() function will determine
* if the file is a compiled binary format or text format
* and act accordingly.
*/
int inject_file_contexts(const char *path)
{
FILE *f;
uint32_t magic = 0;
int res = 1;
f = fopen(path, "rb");
if (f) {
if (fread(&magic, sizeof magic, 1, f) != 1)
ERROR("Could not read magic in '%s'\n", path);
fclose(f);
if (magic == SELINUX_MAGIC_COMPILED_FCONTEXT)
res = inject_file_contexts_bin(path);
else
res = inject_file_contexts_text(path);
}
return res;
}