Coverage Report

Created: 2026-12-01 06:41

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/selinux/checkpolicy/policy_define.c
Line
Count
Source
1
/*
2
 * Author : Stephen Smalley, <stephen.smalley.work@gmail.com>
3
 */
4
5
/*
6
 * Updated: Trusted Computer Solutions, Inc. <dgoeddel@trustedcs.com>
7
 *
8
 *  Support for enhanced MLS infrastructure.
9
 *
10
 * Updated: David Caplan, <dac@tresys.com>
11
 *
12
 *  Added conditional policy language extensions
13
 *
14
 * Updated: Joshua Brindle <jbrindle@tresys.com>
15
 *      Karl MacMillan <kmacmillan@mentalrootkit.com>
16
 *          Jason Tang     <jtang@tresys.com>
17
 *
18
 *  Added support for binary policy modules
19
 *
20
 * Copyright (C) 2004-2005 Trusted Computer Solutions, Inc.
21
 * Copyright (C) 2003 - 2008 Tresys Technology, LLC
22
 * Copyright (C) 2007 Red Hat Inc.
23
 * Copyright (C) 2017 Mellanox Techonologies Inc.
24
 *  This program is free software; you can redistribute it and/or modify
25
 *    it under the terms of the GNU General Public License as published by
26
 *  the Free Software Foundation, version 2.
27
 */
28
29
/* FLASK */
30
31
#include <sys/types.h>
32
#include <assert.h>
33
#include <stdarg.h>
34
#include <stdint.h>
35
#include <stdio.h>
36
#include <stdlib.h>
37
#include <string.h>
38
#include <sys/socket.h>
39
#include <netinet/in.h>
40
#ifndef IPPROTO_DCCP
41
#define IPPROTO_DCCP 33
42
#endif
43
#ifndef IPPROTO_SCTP
44
#define IPPROTO_SCTP 132
45
#endif
46
#include <arpa/inet.h>
47
#include <limits.h>
48
#include <inttypes.h>
49
#include <ctype.h>
50
51
#include <sepol/policydb/expand.h>
52
#include <sepol/policydb/policydb.h>
53
#include <sepol/policydb/services.h>
54
#include <sepol/policydb/conditional.h>
55
#include <sepol/policydb/hierarchy.h>
56
#include <sepol/policydb/polcaps.h>
57
#include <sepol/policydb/dtpath.h>
58
#include "queue.h"
59
#include "module_compiler.h"
60
#include "policy_define.h"
61
62
extern void init_parser(int pass_number, const char *input_name);
63
__attribute__((format(printf, 1, 2))) extern void yyerror2(const char *fmt,
64
                 ...);
65
66
policydb_t *policydbp;
67
queue_t id_queue = NULL;
68
unsigned int pass;
69
int mlspol = 0;
70
71
extern unsigned long policydb_lineno;
72
extern unsigned long source_lineno;
73
extern unsigned int policydb_errors;
74
extern char source_file[PATH_MAX];
75
extern void set_source_file(const char *name);
76
77
extern int yywarn(const char *msg);
78
extern int yyerror(const char *msg);
79
80
/* initialize all of the state variables for the scanner/parser */
81
void init_parser(int pass_number, const char *input_name)
82
17.6k
{
83
17.6k
  policydb_lineno = 1;
84
17.6k
  source_lineno = 1;
85
17.6k
  policydb_errors = 0;
86
17.6k
  pass = pass_number;
87
17.6k
  set_source_file(input_name);
88
17.6k
  queue_clear(id_queue);
89
17.6k
}
90
91
void yyerror2(const char *fmt, ...)
92
21.9k
{
93
21.9k
  char errormsg[256];
94
21.9k
  va_list ap;
95
21.9k
  va_start(ap, fmt);
96
21.9k
  vsnprintf(errormsg, sizeof(errormsg), fmt, ap);
97
21.9k
  yyerror(errormsg);
98
21.9k
  va_end(ap);
99
21.9k
}
100
101
__attribute__((format(printf, 1, 2))) static void yywarn2(const char *fmt, ...)
102
1
{
103
1
  char warnmsg[256];
104
1
  va_list ap;
105
1
  va_start(ap, fmt);
106
1
  vsnprintf(warnmsg, sizeof(warnmsg), fmt, ap);
107
1
  yywarn(warnmsg);
108
1
  va_end(ap);
109
1
}
110
111
int insert_separator(int push)
112
765k
{
113
765k
  int error;
114
115
765k
  if (push)
116
109k
    error = queue_push(id_queue, NULL);
117
655k
  else
118
655k
    error = queue_insert(id_queue, NULL);
119
120
765k
  if (error) {
121
0
    yyerror("queue overflow");
122
0
    return -1;
123
0
  }
124
765k
  return 0;
125
765k
}
126
127
int insert_id(const char *id, int push)
128
2.32M
{
129
2.32M
  char *newid = NULL;
130
2.32M
  int error;
131
132
2.32M
  newid = strdup(id);
133
2.32M
  if (!newid) {
134
0
    yyerror("out of memory");
135
0
    return -1;
136
0
  }
137
2.32M
  if (push)
138
111k
    error = queue_push(id_queue, (queue_element_t)newid);
139
2.21M
  else
140
2.21M
    error = queue_insert(id_queue, (queue_element_t)newid);
141
142
2.32M
  if (error) {
143
0
    yyerror("queue overflow");
144
0
    free(newid);
145
0
    return -1;
146
0
  }
147
2.32M
  return 0;
148
2.32M
}
149
150
/* If the identifier has a dot within it and that its first character
151
   is not a dot then return 1, else return 0. */
152
static int id_has_dot(const char *id)
153
23.7k
{
154
23.7k
  if (strchr(id, '.') >= id + 1) {
155
539
    return 1;
156
539
  }
157
23.2k
  return 0;
158
23.7k
}
159
160
int define_class(void)
161
30.7k
{
162
30.7k
  char *id = NULL;
163
30.7k
  class_datum_t *datum = NULL;
164
30.7k
  int ret = -1;
165
30.7k
  uint32_t value;
166
167
30.7k
  if (pass == 2) {
168
13.1k
    id = queue_remove(id_queue);
169
13.1k
    free(id);
170
13.1k
    return 0;
171
13.1k
  }
172
173
17.6k
  id = (char *)queue_remove(id_queue);
174
17.6k
  if (!id) {
175
0
    yyerror("no class name for class definition?");
176
0
    return -1;
177
0
  }
178
17.6k
  datum = calloc(1, sizeof(class_datum_t));
179
17.6k
  if (!datum) {
180
0
    yyerror("out of memory");
181
0
    goto cleanup;
182
0
  }
183
17.6k
  ret = declare_symbol(SYM_CLASSES, id, datum, &value, &value);
184
17.6k
  switch (ret) {
185
0
  case -3: {
186
0
    yyerror("Out of memory!");
187
0
    goto cleanup;
188
0
  }
189
4
  case -2: {
190
4
    yyerror2("duplicate declaration of class %s", id);
191
4
    goto cleanup;
192
0
  }
193
0
  case -1: {
194
0
    yyerror("could not declare class here");
195
0
    goto cleanup;
196
0
  }
197
17.6k
  case 0: {
198
17.6k
    break;
199
0
  }
200
0
  case 1: {
201
0
    goto cleanup;
202
0
  }
203
0
  default: {
204
0
    assert(0); /* should never get here */
205
0
  }
206
17.6k
  }
207
17.6k
  datum->s.value = value;
208
17.6k
  return 0;
209
210
4
cleanup:
211
4
  free(id);
212
4
  free(datum);
213
4
  return ret == 1 ? 0 : -1;
214
17.6k
}
215
216
int define_permissive(void)
217
611
{
218
611
  char *type = NULL;
219
611
  struct type_datum *t;
220
611
  int rc = 0;
221
222
611
  type = queue_remove(id_queue);
223
224
611
  if (!type) {
225
1
    yyerror2("forgot to include type in permissive definition?");
226
1
    rc = -1;
227
1
    goto out;
228
1
  }
229
230
610
  if (pass == 1)
231
403
    goto out;
232
233
207
  if (!is_id_in_scope(SYM_TYPES, type)) {
234
1
    yyerror2("type %s is not within scope", type);
235
1
    rc = -1;
236
1
    goto out;
237
1
  }
238
239
206
  t = hashtab_search(policydbp->p_types.table, type);
240
206
  if (!t) {
241
1
    yyerror2("type is not defined: %s", type);
242
1
    rc = -1;
243
1
    goto out;
244
1
  }
245
246
205
  if (t->flavor == TYPE_ATTRIB) {
247
1
    yyerror2("attributes may not be permissive: %s", type);
248
1
    rc = -1;
249
1
    goto out;
250
1
  }
251
252
204
  t->flags |= TYPE_FLAGS_PERMISSIVE;
253
254
611
out:
255
611
  free(type);
256
611
  return rc;
257
204
}
258
259
int define_neveraudit(void)
260
309
{
261
309
  char *type = NULL;
262
309
  struct type_datum *t;
263
309
  int rc = 0;
264
265
309
  type = queue_remove(id_queue);
266
267
309
  if (!type) {
268
1
    yyerror2("forgot to include type in neveraudit definition?");
269
1
    rc = -1;
270
1
    goto out;
271
1
  }
272
273
308
  if (pass == 1)
274
252
    goto out;
275
276
56
  if (!is_id_in_scope(SYM_TYPES, type)) {
277
1
    yyerror2("type %s is not within scope", type);
278
1
    rc = -1;
279
1
    goto out;
280
1
  }
281
282
55
  t = hashtab_search(policydbp->p_types.table, type);
283
55
  if (!t) {
284
3
    yyerror2("type is not defined: %s", type);
285
3
    rc = -1;
286
3
    goto out;
287
3
  }
288
289
52
  if (t->flavor == TYPE_ATTRIB) {
290
1
    yyerror2("attributes may not be neveraudit: %s", type);
291
1
    rc = -1;
292
1
    goto out;
293
1
  }
294
295
51
  t->flags |= TYPE_FLAGS_NEVERAUDIT;
296
297
309
out:
298
309
  free(type);
299
309
  return rc;
300
51
}
301
302
int define_polcap(void)
303
613
{
304
613
  char *id = NULL;
305
613
  int capnum;
306
307
613
  if (pass == 2) {
308
68
    id = queue_remove(id_queue);
309
68
    free(id);
310
68
    return 0;
311
68
  }
312
313
545
  id = (char *)queue_remove(id_queue);
314
545
  if (!id) {
315
1
    yyerror("no capability name for policycap definition?");
316
1
    goto bad;
317
1
  }
318
319
  /* Check for valid cap name -> number mapping */
320
544
  capnum = sepol_polcap_getnum(id);
321
544
  if (capnum < 0) {
322
118
    yyerror2("invalid policy capability name %s", id);
323
118
    goto bad;
324
118
  }
325
326
  /* Store it */
327
426
  if (ebitmap_set_bit(&policydbp->policycaps, capnum, TRUE)) {
328
0
    yyerror("out of memory");
329
0
    goto bad;
330
0
  }
331
332
426
  free(id);
333
426
  return 0;
334
335
119
bad:
336
119
  free(id);
337
119
  return -1;
338
426
}
339
340
int define_initial_sid(void)
341
24.1k
{
342
24.1k
  char *id = NULL;
343
24.1k
  ocontext_t *newc = NULL, *c, *head;
344
345
24.1k
  if (pass == 2) {
346
10.2k
    id = queue_remove(id_queue);
347
10.2k
    free(id);
348
10.2k
    return 0;
349
10.2k
  }
350
351
13.8k
  id = (char *)queue_remove(id_queue);
352
13.8k
  if (!id) {
353
0
    yyerror("no sid name for SID definition?");
354
0
    return -1;
355
0
  }
356
13.8k
  newc = calloc(1, sizeof(ocontext_t));
357
13.8k
  if (!newc) {
358
0
    yyerror("out of memory");
359
0
    goto bad;
360
0
  }
361
13.8k
  newc->u.name = id;
362
13.8k
  context_init(&newc->context[0]);
363
13.8k
  head = policydbp->ocontexts[OCON_ISID];
364
365
22.7k
  for (c = head; c; c = c->next) {
366
8.87k
    if (!strcmp(newc->u.name, c->u.name)) {
367
3
      yyerror2("duplicate initial SID %s", id);
368
3
      goto bad;
369
3
    }
370
8.87k
  }
371
372
13.8k
  if (head) {
373
3.82k
    newc->sid[0] = head->sid[0] + 1;
374
10.0k
  } else {
375
10.0k
    newc->sid[0] = 1;
376
10.0k
  }
377
13.8k
  newc->next = head;
378
13.8k
  policydbp->ocontexts[OCON_ISID] = newc;
379
380
13.8k
  return 0;
381
382
3
bad:
383
3
  free(id);
384
3
  free(newc);
385
3
  return -1;
386
13.8k
}
387
388
static int read_classes(ebitmap_t *e_classes)
389
36.4k
{
390
36.4k
  char *id;
391
36.4k
  class_datum_t *cladatum;
392
393
72.8k
  while ((id = queue_remove(id_queue))) {
394
36.4k
    if (!is_id_in_scope(SYM_CLASSES, id)) {
395
0
      yyerror2("class %s is not within scope", id);
396
0
      free(id);
397
0
      return -1;
398
0
    }
399
36.4k
    cladatum = hashtab_search(policydbp->p_classes.table, id);
400
36.4k
    if (!cladatum) {
401
47
      yyerror2("unknown class %s", id);
402
47
      free(id);
403
47
      return -1;
404
47
    }
405
36.4k
    free(id);
406
36.4k
    if (ebitmap_set_bit(e_classes, cladatum->s.value - 1, TRUE)) {
407
0
      yyerror("Out of memory");
408
0
      return -1;
409
0
    }
410
36.4k
  }
411
36.4k
  return 0;
412
36.4k
}
413
414
int define_default_user(int which)
415
352
{
416
352
  char *id;
417
352
  class_datum_t *cladatum;
418
419
352
  if (pass == 1) {
420
1.42k
    while ((id = queue_remove(id_queue)))
421
1.11k
      free(id);
422
305
    return 0;
423
305
  }
424
425
318
  while ((id = queue_remove(id_queue))) {
426
284
    if (!is_id_in_scope(SYM_CLASSES, id)) {
427
0
      yyerror2("class %s is not within scope", id);
428
0
      free(id);
429
0
      return -1;
430
0
    }
431
284
    cladatum = hashtab_search(policydbp->p_classes.table, id);
432
284
    if (!cladatum) {
433
12
      yyerror2("unknown class %s", id);
434
12
      free(id);
435
12
      return -1;
436
12
    }
437
272
    if (cladatum->default_user && cladatum->default_user != which) {
438
1
      yyerror2(
439
1
        "conflicting default user information for class %s",
440
1
        id);
441
1
      free(id);
442
1
      return -1;
443
1
    }
444
271
    cladatum->default_user = which;
445
271
    free(id);
446
271
  }
447
448
34
  return 0;
449
47
}
450
451
int define_default_role(int which)
452
4.19k
{
453
4.19k
  char *id;
454
4.19k
  class_datum_t *cladatum;
455
456
4.19k
  if (pass == 1) {
457
5.29k
    while ((id = queue_remove(id_queue)))
458
3.05k
      free(id);
459
2.24k
    return 0;
460
2.24k
  }
461
462
4.45k
  while ((id = queue_remove(id_queue))) {
463
2.52k
    if (!is_id_in_scope(SYM_CLASSES, id)) {
464
0
      yyerror2("class %s is not within scope", id);
465
0
      free(id);
466
0
      return -1;
467
0
    }
468
2.52k
    cladatum = hashtab_search(policydbp->p_classes.table, id);
469
2.52k
    if (!cladatum) {
470
14
      yyerror2("unknown class %s", id);
471
14
      free(id);
472
14
      return -1;
473
14
    }
474
2.51k
    if (cladatum->default_role && cladatum->default_role != which) {
475
1
      yyerror2(
476
1
        "conflicting default role information for class %s",
477
1
        id);
478
1
      free(id);
479
1
      return -1;
480
1
    }
481
2.51k
    cladatum->default_role = which;
482
2.51k
    free(id);
483
2.51k
  }
484
485
1.93k
  return 0;
486
1.94k
}
487
488
int define_default_type(int which)
489
2.61k
{
490
2.61k
  char *id;
491
2.61k
  class_datum_t *cladatum;
492
493
2.61k
  if (pass == 1) {
494
3.13k
    while ((id = queue_remove(id_queue)))
495
1.69k
      free(id);
496
1.43k
    return 0;
497
1.43k
  }
498
499
2.39k
  while ((id = queue_remove(id_queue))) {
500
1.23k
    if (!is_id_in_scope(SYM_CLASSES, id)) {
501
0
      yyerror2("class %s is not within scope", id);
502
0
      free(id);
503
0
      return -1;
504
0
    }
505
1.23k
    cladatum = hashtab_search(policydbp->p_classes.table, id);
506
1.23k
    if (!cladatum) {
507
23
      yyerror2("unknown class %s", id);
508
23
      free(id);
509
23
      return -1;
510
23
    }
511
1.21k
    if (cladatum->default_type && cladatum->default_type != which) {
512
1
      yyerror2(
513
1
        "conflicting default type information for class %s",
514
1
        id);
515
1
      free(id);
516
1
      return -1;
517
1
    }
518
1.21k
    cladatum->default_type = which;
519
1.21k
    free(id);
520
1.21k
  }
521
522
1.15k
  return 0;
523
1.18k
}
524
525
int define_default_range(int which)
526
123
{
527
123
  char *id;
528
123
  class_datum_t *cladatum;
529
530
123
  if (pass == 1) {
531
600
    while ((id = queue_remove(id_queue)))
532
496
      free(id);
533
104
    return 0;
534
104
  }
535
536
162
  while ((id = queue_remove(id_queue))) {
537
155
    if (!is_id_in_scope(SYM_CLASSES, id)) {
538
0
      yyerror2("class %s is not within scope", id);
539
0
      free(id);
540
0
      return -1;
541
0
    }
542
155
    cladatum = hashtab_search(policydbp->p_classes.table, id);
543
155
    if (!cladatum) {
544
12
      yyerror2("unknown class %s", id);
545
12
      free(id);
546
12
      return -1;
547
12
    }
548
143
    if (cladatum->default_range &&
549
117
        cladatum->default_range != which) {
550
0
      yyerror2(
551
0
        "conflicting default range information for class %s",
552
0
        id);
553
0
      free(id);
554
0
      return -1;
555
0
    }
556
143
    cladatum->default_range = which;
557
143
    free(id);
558
143
  }
559
560
7
  return 0;
561
19
}
562
563
int define_common_perms(void)
564
530
{
565
530
  char *id = NULL, *perm = NULL;
566
530
  common_datum_t *comdatum = NULL;
567
530
  perm_datum_t *perdatum = NULL;
568
530
  int ret;
569
570
530
  if (pass == 2) {
571
91
    while ((id = queue_remove(id_queue)))
572
70
      free(id);
573
21
    return 0;
574
21
  }
575
576
509
  id = (char *)queue_remove(id_queue);
577
509
  if (!id) {
578
0
    yyerror("no common name for common perm definition?");
579
0
    return -1;
580
0
  }
581
509
  comdatum = hashtab_search(policydbp->p_commons.table, id);
582
509
  if (comdatum) {
583
1
    yyerror2("duplicate declaration for common %s", id);
584
1
    free(id);
585
1
    return -1;
586
1
  }
587
508
  comdatum = calloc(1, sizeof(common_datum_t));
588
508
  if (!comdatum) {
589
0
    yyerror("out of memory");
590
0
    goto bad;
591
0
  }
592
508
  ret = hashtab_insert(policydbp->p_commons.table, (hashtab_key_t)id,
593
508
           (hashtab_datum_t)comdatum);
594
595
508
  if (ret == SEPOL_EEXIST) {
596
0
    yyerror("duplicate common definition");
597
0
    goto bad;
598
0
  }
599
508
  if (ret == SEPOL_ENOMEM) {
600
0
    yyerror("hash table overflow");
601
0
    goto bad;
602
0
  }
603
508
  comdatum->s.value = policydbp->p_commons.nprim + 1;
604
508
  if (symtab_init(&comdatum->permissions, PERM_SYMTAB_SIZE)) {
605
0
    yyerror("out of memory");
606
0
    goto bad;
607
0
  }
608
508
  policydbp->p_commons.nprim++;
609
1.27k
  while ((perm = queue_remove(id_queue))) {
610
777
    perdatum = calloc(1, sizeof(perm_datum_t));
611
777
    if (!perdatum) {
612
0
      yyerror("out of memory");
613
0
      goto bad_perm;
614
0
    }
615
777
    perdatum->s.value = comdatum->permissions.nprim + 1;
616
617
777
    if (perdatum->s.value > (sizeof(sepol_access_vector_t) * 8)) {
618
1
      yyerror2(
619
1
        "too many permissions (%d) to fit in an access vector",
620
1
        perdatum->s.value);
621
1
      goto bad_perm;
622
1
    }
623
776
    ret = hashtab_insert(comdatum->permissions.table,
624
776
             (hashtab_key_t)perm,
625
776
             (hashtab_datum_t)perdatum);
626
627
776
    if (ret == SEPOL_EEXIST) {
628
7
      yyerror2("duplicate permission %s in common %s", perm,
629
7
         id);
630
7
      goto bad_perm;
631
7
    }
632
769
    if (ret == SEPOL_ENOMEM) {
633
0
      yyerror("hash table overflow");
634
0
      goto bad_perm;
635
0
    }
636
769
    comdatum->permissions.nprim++;
637
769
  }
638
639
500
  return 0;
640
641
0
bad:
642
0
  free(id);
643
0
  free(comdatum);
644
0
  return -1;
645
646
8
bad_perm:
647
8
  free(perm);
648
8
  free(perdatum);
649
8
  return -1;
650
508
}
651
652
int define_av_perms(int inherits)
653
16.6k
{
654
16.6k
  char *id;
655
16.6k
  class_datum_t *cladatum;
656
16.6k
  common_datum_t *comdatum;
657
16.6k
  perm_datum_t *perdatum = NULL, *perdatum2 = NULL;
658
16.6k
  int ret;
659
660
16.6k
  if (pass == 2) {
661
30.0k
    while ((id = queue_remove(id_queue)))
662
23.1k
      free(id);
663
6.89k
    return 0;
664
6.89k
  }
665
666
9.74k
  id = (char *)queue_remove(id_queue);
667
9.74k
  if (!id) {
668
0
    yyerror("no tclass name for av perm definition?");
669
0
    return -1;
670
0
  }
671
9.74k
  cladatum = (class_datum_t *)hashtab_search(policydbp->p_classes.table,
672
9.74k
               (hashtab_key_t)id);
673
9.74k
  if (!cladatum) {
674
2
    yyerror2("class %s is not defined", id);
675
2
    goto bad;
676
2
  }
677
678
9.74k
  if (cladatum->comdatum || cladatum->permissions.nprim) {
679
5
    yyerror2("duplicate access vector definition for class %s", id);
680
5
    goto bad;
681
5
  }
682
683
9.73k
  free(id);
684
9.73k
  id = NULL;
685
686
9.73k
  if (symtab_init(&cladatum->permissions, PERM_SYMTAB_SIZE)) {
687
0
    yyerror("out of memory");
688
0
    return -1;
689
0
  }
690
9.73k
  if (inherits) {
691
2
    id = (char *)queue_remove(id_queue);
692
2
    if (!id) {
693
0
      yyerror("no inherits name for access vector definition?");
694
0
      return -1;
695
0
    }
696
2
    comdatum = (common_datum_t *)hashtab_search(
697
2
      policydbp->p_commons.table, (hashtab_key_t)id);
698
699
2
    if (!comdatum) {
700
2
      yyerror2("common %s is not defined", id);
701
2
      goto bad;
702
2
    }
703
0
    cladatum->comkey = id;
704
0
    cladatum->comdatum = comdatum;
705
706
    /*
707
     * Class-specific permissions start with values 
708
     * after the last common permission.
709
     */
710
0
    cladatum->permissions.nprim += comdatum->permissions.nprim;
711
0
  }
712
29.1k
  while ((id = queue_remove(id_queue))) {
713
19.3k
    perdatum = calloc(1, sizeof(perm_datum_t));
714
19.3k
    if (!perdatum) {
715
0
      yyerror("out of memory");
716
0
      goto bad;
717
0
    }
718
19.3k
    perdatum->s.value = ++cladatum->permissions.nprim;
719
720
19.3k
    if (perdatum->s.value > (sizeof(sepol_access_vector_t) * 8)) {
721
1
      yyerror2(
722
1
        "too many permissions (%d) to fit in an access vector",
723
1
        perdatum->s.value);
724
1
      goto bad;
725
1
    }
726
19.3k
    if (inherits) {
727
      /*
728
       * Class-specific permissions and 
729
       * common permissions exist in the same
730
       * name space.
731
       */
732
0
      perdatum2 = (perm_datum_t *)hashtab_search(
733
0
        cladatum->comdatum->permissions.table,
734
0
        (hashtab_key_t)id);
735
0
      if (perdatum2) {
736
0
        yyerror2("permission %s conflicts with an "
737
0
           "inherited permission",
738
0
           id);
739
0
        goto bad;
740
0
      }
741
0
    }
742
19.3k
    ret = hashtab_insert(cladatum->permissions.table,
743
19.3k
             (hashtab_key_t)id,
744
19.3k
             (hashtab_datum_t)perdatum);
745
746
19.3k
    if (ret == SEPOL_EEXIST) {
747
3
      yyerror2("duplicate permission %s", id);
748
3
      goto bad;
749
3
    }
750
19.3k
    if (ret == SEPOL_ENOMEM) {
751
0
      yyerror("hash table overflow");
752
0
      goto bad;
753
0
    }
754
19.3k
    if (add_perm_to_class(perdatum->s.value, cladatum->s.value)) {
755
0
      yyerror("out of memory");
756
0
      goto bad;
757
0
    }
758
19.3k
  }
759
760
9.73k
  return 0;
761
762
13
bad:
763
13
  free(id);
764
13
  free(perdatum);
765
13
  return -1;
766
9.73k
}
767
768
int define_sens(void)
769
8.43k
{
770
8.43k
  char *id;
771
8.43k
  mls_level_t *level = NULL;
772
8.43k
  level_datum_t *datum = NULL, *aliasdatum = NULL;
773
8.43k
  int ret;
774
8.43k
  uint32_t value; /* dummy variable -- its value is never used */
775
776
8.43k
  if (!mlspol) {
777
2
    yyerror("sensitivity definition in non-MLS configuration");
778
2
    return -1;
779
2
  }
780
781
8.43k
  if (pass == 2) {
782
8.22k
    while ((id = queue_remove(id_queue)))
783
4.13k
      free(id);
784
4.08k
    return 0;
785
4.08k
  }
786
787
4.34k
  id = (char *)queue_remove(id_queue);
788
4.34k
  if (!id) {
789
0
    yyerror("no sensitivity name for sensitivity definition?");
790
0
    return -1;
791
0
  }
792
4.34k
  if (id_has_dot(id)) {
793
1
    yyerror2("sensitivity identifier %s may not contain periods",
794
1
       id);
795
1
    goto bad;
796
1
  }
797
4.34k
  level = malloc(sizeof(mls_level_t));
798
4.34k
  if (!level) {
799
0
    yyerror("out of memory");
800
0
    goto bad;
801
0
  }
802
4.34k
  mls_level_init(level);
803
4.34k
  level->sens = 0; /* actual value set in define_dominance */
804
4.34k
  ebitmap_init(&level->cat); /* actual value set in define_level */
805
806
4.34k
  datum = malloc(sizeof(level_datum_t));
807
4.34k
  if (!datum) {
808
0
    yyerror("out of memory");
809
0
    goto bad;
810
0
  }
811
4.34k
  level_datum_init(datum);
812
4.34k
  datum->isalias = FALSE;
813
4.34k
  datum->level = level;
814
4.34k
  datum->notdefined = TRUE;
815
816
4.34k
  ret = declare_symbol(SYM_LEVELS, id, datum, &value, &value);
817
4.34k
  switch (ret) {
818
0
  case -3: {
819
0
    yyerror("Out of memory!");
820
0
    goto bad;
821
0
  }
822
1
  case -2: {
823
1
    yyerror2("duplicate declaration of sensitivity level %s", id);
824
1
    goto bad;
825
0
  }
826
0
  case -1: {
827
0
    yyerror2("could not declare sensitivity level %s here", id);
828
0
    goto bad;
829
0
  }
830
4.34k
  case 0: {
831
4.34k
    break;
832
0
  }
833
0
  case 1: {
834
0
    level_datum_destroy(datum);
835
0
    free(datum);
836
0
    free(id);
837
0
    break;
838
0
  }
839
0
  default: {
840
0
    assert(0); /* should never get here */
841
0
  }
842
4.34k
  }
843
844
4.54k
  while ((id = queue_remove(id_queue))) {
845
203
    if (id_has_dot(id)) {
846
1
      yyerror2("sensitivity alias %s may not contain periods",
847
1
         id);
848
1
      free(id);
849
1
      return -1;
850
1
    }
851
202
    aliasdatum = malloc(sizeof(level_datum_t));
852
202
    if (!aliasdatum) {
853
0
      yyerror("out of memory");
854
0
      free(id);
855
0
      return -1;
856
0
    }
857
202
    level_datum_init(aliasdatum);
858
202
    aliasdatum->isalias = TRUE;
859
202
    aliasdatum->level = level;
860
202
    aliasdatum->notdefined = TRUE;
861
862
202
    ret = declare_symbol(SYM_LEVELS, id, aliasdatum, NULL, &value);
863
202
    switch (ret) {
864
0
    case -3: {
865
0
      yyerror("Out of memory!");
866
0
      goto bad_alias;
867
0
    }
868
4
    case -2: {
869
4
      yyerror2(
870
4
        "duplicate declaration of sensitivity alias %s",
871
4
        id);
872
4
      goto bad_alias;
873
0
    }
874
0
    case -1: {
875
0
      yyerror2("could not declare sensitivity alias %s here",
876
0
         id);
877
0
      goto bad_alias;
878
0
    }
879
198
    case 0: {
880
198
      break;
881
0
    }
882
0
    case 1: {
883
0
      level_datum_destroy(aliasdatum);
884
0
      free(aliasdatum);
885
0
      free(id);
886
0
      break;
887
0
    }
888
0
    default: {
889
0
      assert(0); /* should never get here */
890
0
    }
891
202
    }
892
202
  }
893
894
4.33k
  return 0;
895
896
2
bad:
897
2
  free(id);
898
2
  free(level);
899
2
  if (datum) {
900
1
    level_datum_destroy(datum);
901
1
    free(datum);
902
1
  }
903
2
  return -1;
904
905
4
bad_alias:
906
4
  free(id);
907
4
  if (aliasdatum) {
908
4
    level_datum_destroy(aliasdatum);
909
4
    free(aliasdatum);
910
4
  }
911
4
  return -1;
912
4.34k
}
913
914
int define_dominance(void)
915
8.37k
{
916
8.37k
  level_datum_t *datum;
917
8.37k
  uint32_t order;
918
8.37k
  char *id;
919
920
8.37k
  if (!mlspol) {
921
0
    yyerror("dominance definition in non-MLS configuration");
922
0
    return -1;
923
0
  }
924
925
8.37k
  if (pass == 2) {
926
8.17k
    while ((id = queue_remove(id_queue)))
927
4.08k
      free(id);
928
4.08k
    return 0;
929
4.08k
  }
930
931
4.29k
  order = 0;
932
8.60k
  while ((id = (char *)queue_remove(id_queue))) {
933
4.31k
    datum = (level_datum_t *)hashtab_search(
934
4.31k
      policydbp->p_levels.table, (hashtab_key_t)id);
935
4.31k
    if (!datum) {
936
3
      yyerror2("unknown sensitivity %s used in dominance "
937
3
         "definition",
938
3
         id);
939
3
      free(id);
940
3
      return -1;
941
3
    }
942
4.30k
    if (datum->level->sens != 0) {
943
1
      yyerror2("sensitivity %s occurs multiply in dominance "
944
1
         "definition",
945
1
         id);
946
1
      free(id);
947
1
      return -1;
948
1
    }
949
4.30k
    datum->level->sens = ++order;
950
951
    /* no need to keep sensitivity name */
952
4.30k
    free(id);
953
4.30k
  }
954
955
4.28k
  if (order != policydbp->p_levels.nprim) {
956
2
    yyerror("all sensitivities must be specified in dominance definition");
957
2
    return -1;
958
2
  }
959
4.28k
  return 0;
960
4.28k
}
961
962
int define_category(void)
963
8.29k
{
964
8.29k
  char *id;
965
8.29k
  cat_datum_t *datum = NULL, *aliasdatum = NULL;
966
8.29k
  int ret;
967
8.29k
  uint32_t value;
968
969
8.29k
  if (!mlspol) {
970
0
    yyerror("category definition in non-MLS configuration");
971
0
    return -1;
972
0
  }
973
974
8.29k
  if (pass == 2) {
975
8.22k
    while ((id = queue_remove(id_queue)))
976
4.16k
      free(id);
977
4.05k
    return 0;
978
4.05k
  }
979
980
4.23k
  id = (char *)queue_remove(id_queue);
981
4.23k
  if (!id) {
982
0
    yyerror("no category name for category definition?");
983
0
    return -1;
984
0
  }
985
4.23k
  if (id_has_dot(id)) {
986
1
    yyerror2("category identifier %s may not contain periods", id);
987
1
    goto bad;
988
1
  }
989
4.23k
  datum = malloc(sizeof(cat_datum_t));
990
4.23k
  if (!datum) {
991
0
    yyerror("out of memory");
992
0
    goto bad;
993
0
  }
994
4.23k
  cat_datum_init(datum);
995
4.23k
  datum->isalias = FALSE;
996
997
4.23k
  ret = declare_symbol(SYM_CATS, id, datum, &value, &value);
998
4.23k
  switch (ret) {
999
0
  case -3: {
1000
0
    yyerror("Out of memory!");
1001
0
    goto bad;
1002
0
  }
1003
1
  case -2: {
1004
1
    yyerror2("duplicate declaration of category %s", id);
1005
1
    goto bad;
1006
0
  }
1007
0
  case -1: {
1008
0
    yyerror2("could not declare category %s here", id);
1009
0
    goto bad;
1010
0
  }
1011
4.23k
  case 0: {
1012
4.23k
    datum->s.value = value;
1013
4.23k
    break;
1014
0
  }
1015
0
  case 1: {
1016
0
    cat_datum_destroy(datum);
1017
0
    free(datum);
1018
0
    free(id);
1019
0
    break;
1020
0
  }
1021
0
  default: {
1022
0
    assert(0); /* should never get here */
1023
0
  }
1024
4.23k
  }
1025
1026
4.43k
  while ((id = queue_remove(id_queue))) {
1027
211
    if (id_has_dot(id)) {
1028
1
      yyerror2("category alias %s may not contain periods",
1029
1
         id);
1030
1
      free(id);
1031
1
      return -1;
1032
1
    }
1033
210
    aliasdatum = malloc(sizeof(cat_datum_t));
1034
210
    if (!aliasdatum) {
1035
0
      yyerror("out of memory");
1036
0
      free(id);
1037
0
      return -1;
1038
0
    }
1039
210
    cat_datum_init(aliasdatum);
1040
210
    aliasdatum->isalias = TRUE;
1041
210
    aliasdatum->s.value = value;
1042
1043
210
    ret = declare_symbol(SYM_CATS, id, aliasdatum, NULL, &value);
1044
210
    switch (ret) {
1045
0
    case -3: {
1046
0
      yyerror("Out of memory!");
1047
0
      goto bad_alias;
1048
0
    }
1049
4
    case -2: {
1050
4
      yyerror2("duplicate declaration of category alias %s",
1051
4
         id);
1052
4
      goto bad_alias;
1053
0
    }
1054
0
    case -1: {
1055
0
      yyerror2("could not declare category alias %s here",
1056
0
         id);
1057
0
      goto bad_alias;
1058
0
    }
1059
206
    case 0: {
1060
206
      break;
1061
0
    }
1062
0
    case 1: {
1063
0
      cat_datum_destroy(aliasdatum);
1064
0
      free(aliasdatum);
1065
0
      free(id);
1066
0
      break;
1067
0
    }
1068
0
    default: {
1069
0
      assert(0); /* should never get here */
1070
0
    }
1071
210
    }
1072
210
  }
1073
1074
4.22k
  return 0;
1075
1076
2
bad:
1077
2
  free(id);
1078
2
  if (datum) {
1079
1
    cat_datum_destroy(datum);
1080
1
    free(datum);
1081
1
  }
1082
2
  return -1;
1083
1084
4
bad_alias:
1085
4
  free(id);
1086
4
  if (aliasdatum) {
1087
4
    cat_datum_destroy(aliasdatum);
1088
4
    free(aliasdatum);
1089
4
  }
1090
4
  return -1;
1091
4.23k
}
1092
1093
static int clone_level(hashtab_key_t key __attribute__((unused)),
1094
           hashtab_datum_t datum, void *arg)
1095
5.03k
{
1096
5.03k
  level_datum_t *levdatum = (level_datum_t *)datum;
1097
5.03k
  mls_level_t *level = (mls_level_t *)arg, *newlevel;
1098
1099
5.03k
  if (levdatum->notdefined && levdatum->level == level) {
1100
4.37k
    if (!levdatum->isalias) {
1101
4.26k
      levdatum->notdefined = FALSE;
1102
4.26k
      return 0;
1103
4.26k
    }
1104
110
    newlevel = malloc(sizeof(mls_level_t));
1105
110
    if (!newlevel)
1106
0
      return -1;
1107
110
    if (mls_level_cpy(newlevel, level)) {
1108
0
      free(newlevel);
1109
0
      return -1;
1110
0
    }
1111
110
    levdatum->level = newlevel;
1112
110
    levdatum->notdefined = FALSE;
1113
110
  }
1114
776
  return 0;
1115
5.03k
}
1116
1117
int define_level(void)
1118
8.72k
{
1119
8.72k
  char *id;
1120
8.72k
  level_datum_t *levdatum;
1121
1122
8.72k
  if (!mlspol) {
1123
0
    yyerror("level definition in non-MLS configuration");
1124
0
    return -1;
1125
0
  }
1126
1127
8.72k
  if (pass == 2) {
1128
12.3k
    while ((id = queue_remove(id_queue)))
1129
8.22k
      free(id);
1130
4.11k
    return 0;
1131
4.11k
  }
1132
1133
4.61k
  id = (char *)queue_remove(id_queue);
1134
4.61k
  if (!id) {
1135
0
    yyerror("no level name for level definition?");
1136
0
    return -1;
1137
0
  }
1138
4.61k
  levdatum = (level_datum_t *)hashtab_search(policydbp->p_levels.table,
1139
4.61k
               (hashtab_key_t)id);
1140
4.61k
  if (!levdatum) {
1141
1
    yyerror2("unknown sensitivity %s used in level definition", id);
1142
1
    free(id);
1143
1
    return -1;
1144
1
  }
1145
4.61k
  if (ebitmap_length(&levdatum->level->cat)) {
1146
1
    yyerror2("sensitivity %s used in multiple level definitions",
1147
1
       id);
1148
1
    free(id);
1149
1
    return -1;
1150
1
  }
1151
4.61k
  free(id);
1152
1153
9.10k
  while ((id = queue_remove(id_queue))) {
1154
4.50k
    cat_datum_t *cdatum;
1155
4.50k
    uint32_t range_start, range_end, i;
1156
1157
4.50k
    if (id_has_dot(id)) {
1158
200
      char *id_start = id;
1159
200
      char *id_end = strchr(id, '.');
1160
1161
200
      *(id_end++) = '\0';
1162
1163
200
      cdatum = (cat_datum_t *)hashtab_search(
1164
200
        policydbp->p_cats.table,
1165
200
        (hashtab_key_t)id_start);
1166
200
      if (!cdatum) {
1167
2
        yyerror2("unknown category %s", id_start);
1168
2
        free(id);
1169
2
        return -1;
1170
2
      }
1171
198
      range_start = cdatum->s.value - 1;
1172
198
      cdatum = (cat_datum_t *)hashtab_search(
1173
198
        policydbp->p_cats.table, (hashtab_key_t)id_end);
1174
198
      if (!cdatum) {
1175
3
        yyerror2("unknown category %s", id_end);
1176
3
        free(id);
1177
3
        return -1;
1178
3
      }
1179
195
      range_end = cdatum->s.value - 1;
1180
1181
195
      if (range_end < range_start) {
1182
0
        yyerror2("category range %d-%d is invalid",
1183
0
           range_start, range_end);
1184
0
        free(id);
1185
0
        return -1;
1186
0
      }
1187
4.30k
    } else {
1188
4.30k
      cdatum = (cat_datum_t *)hashtab_search(
1189
4.30k
        policydbp->p_cats.table, (hashtab_key_t)id);
1190
4.30k
      if (!cdatum) {
1191
3
        yyerror2("unknown category %s", id);
1192
3
        free(id);
1193
3
        return -1;
1194
3
      }
1195
4.29k
      range_start = range_end = cdatum->s.value - 1;
1196
4.29k
    }
1197
1198
8.98k
    for (i = range_start; i <= range_end; i++) {
1199
4.49k
      if (ebitmap_set_bit(&levdatum->level->cat, i, TRUE)) {
1200
0
        yyerror("out of memory");
1201
0
        free(id);
1202
0
        return -1;
1203
0
      }
1204
4.49k
    }
1205
1206
4.49k
    free(id);
1207
4.49k
  }
1208
1209
4.60k
  if (hashtab_map(policydbp->p_levels.table, clone_level,
1210
4.60k
      levdatum->level)) {
1211
0
    yyerror("out of memory");
1212
0
    return -1;
1213
0
  }
1214
1215
4.60k
  return 0;
1216
4.60k
}
1217
1218
int define_attrib(void)
1219
1.90k
{
1220
1.90k
  if (pass == 2) {
1221
908
    free(queue_remove(id_queue));
1222
908
    return 0;
1223
908
  }
1224
1225
996
  if (declare_type(TRUE, TRUE) == NULL) {
1226
1
    return -1;
1227
1
  }
1228
995
  return 0;
1229
996
}
1230
1231
int expand_attrib(void)
1232
2.44k
{
1233
2.44k
  char *id;
1234
2.44k
  ebitmap_t attrs;
1235
2.44k
  type_datum_t *attr;
1236
2.44k
  ebitmap_node_t *node;
1237
2.44k
  const char *name;
1238
2.44k
  uint32_t i;
1239
2.44k
  int rc = -1;
1240
2.44k
  int flags = 0;
1241
1242
2.44k
  if (pass == 1) {
1243
7.27k
    for (i = 0; i < 2; i++) {
1244
10.7k
      while ((id = queue_remove(id_queue))) {
1245
5.92k
        free(id);
1246
5.92k
      }
1247
4.84k
    }
1248
2.42k
    return 0;
1249
2.42k
  }
1250
1251
24
  ebitmap_init(&attrs);
1252
44
  while ((id = queue_remove(id_queue))) {
1253
25
    if (!is_id_in_scope(SYM_TYPES, id)) {
1254
0
      yyerror2("attribute %s is not within scope", id);
1255
0
      goto exit;
1256
0
    }
1257
1258
25
    attr = hashtab_search(policydbp->p_types.table, id);
1259
25
    if (!attr) {
1260
4
      yyerror2("attribute %s is not declared", id);
1261
4
      goto exit;
1262
4
    }
1263
1264
21
    if (attr->flavor != TYPE_ATTRIB) {
1265
1
      yyerror2("%s is a type, not an attribute", id);
1266
1
      goto exit;
1267
1
    }
1268
1269
20
    if (ebitmap_set_bit(&attrs, attr->s.value - 1, TRUE)) {
1270
0
      yyerror("Out of memory!");
1271
0
      goto exit;
1272
0
    }
1273
1274
20
    free(id);
1275
20
  }
1276
1277
19
  id = (char *)queue_remove(id_queue);
1278
19
  if (!id) {
1279
0
    yyerror("No option specified for attribute expansion.");
1280
0
    goto exit;
1281
0
  }
1282
1283
19
  if (!strcmp(id, "T")) {
1284
7
    flags = TYPE_FLAGS_EXPAND_ATTR_TRUE;
1285
12
  } else {
1286
12
    flags = TYPE_FLAGS_EXPAND_ATTR_FALSE;
1287
12
  }
1288
1289
1.21k
  ebitmap_for_each_positive_bit(&attrs, node, i) {
1290
19
    name = policydbp->sym_val_to_name[SYM_TYPES][i];
1291
19
    attr = hashtab_search(policydbp->p_types.table, name);
1292
19
    attr->flags |= flags;
1293
19
    if ((attr->flags & TYPE_FLAGS_EXPAND_ATTR_TRUE) &&
1294
8
        (attr->flags & TYPE_FLAGS_EXPAND_ATTR_FALSE)) {
1295
1
      yywarn2("Expandattribute option of attribute %s was set to both true and false; "
1296
1
        "Resolving to false.",
1297
1
        name);
1298
1
      attr->flags &= ~TYPE_FLAGS_EXPAND_ATTR_TRUE;
1299
1
    }
1300
19
  }
1301
1302
19
  rc = 0;
1303
24
exit:
1304
24
  ebitmap_destroy(&attrs);
1305
24
  free(id);
1306
24
  return rc;
1307
19
}
1308
1309
static int add_aliases_to_type(type_datum_t *type)
1310
257
{
1311
257
  char *id;
1312
257
  type_datum_t *aliasdatum = NULL;
1313
257
  int ret;
1314
2.20k
  while ((id = queue_remove(id_queue))) {
1315
1.95k
    if (id_has_dot(id)) {
1316
1
      yyerror2(
1317
1
        "type alias identifier %s may not contain periods",
1318
1
        id);
1319
1
      free(id);
1320
1
      return -1;
1321
1
    }
1322
1.95k
    if (strcmp(id, "self") == 0) {
1323
1
      yyerror("\"self\" is a reserved type name.");
1324
1
      free(id);
1325
1
      return -1;
1326
1
    }
1327
1.95k
    aliasdatum = calloc(1, sizeof(type_datum_t));
1328
1.95k
    if (!aliasdatum) {
1329
0
      free(id);
1330
0
      yyerror("Out of memory!");
1331
0
      return -1;
1332
0
    }
1333
1.95k
    aliasdatum->s.value = type->s.value;
1334
1335
1.95k
    ret = declare_symbol(SYM_TYPES, id, aliasdatum, NULL,
1336
1.95k
             &aliasdatum->s.value);
1337
1.95k
    switch (ret) {
1338
0
    case -3: {
1339
0
      yyerror("Out of memory!");
1340
0
      goto cleanup;
1341
0
    }
1342
1
    case -2: {
1343
1
      yyerror2("duplicate declaration of alias %s", id);
1344
1
      goto cleanup;
1345
0
    }
1346
1
    case -1: {
1347
1
      yyerror2("could not declare alias %s here", id);
1348
1
      goto cleanup;
1349
0
    }
1350
1.92k
    case 0:
1351
1.92k
      break;
1352
19
    case 1: {
1353
      /* ret == 1 means the alias was required and therefore already
1354
         * has a value. Set it up as an alias with a different primary. */
1355
19
      type_datum_destroy(aliasdatum);
1356
19
      free(aliasdatum);
1357
1358
19
      aliasdatum = hashtab_search(
1359
19
        policydbp->symtab[SYM_TYPES].table, id);
1360
19
      assert(aliasdatum);
1361
1362
19
      aliasdatum->primary = type->s.value;
1363
19
      aliasdatum->flavor = TYPE_ALIAS;
1364
1365
19
      free(id);
1366
19
      break;
1367
19
    }
1368
0
    default: {
1369
0
      assert(0); /* should never get here */
1370
0
    }
1371
1.95k
    }
1372
1.95k
  }
1373
253
  return 0;
1374
2
cleanup:
1375
2
  free(id);
1376
2
  type_datum_destroy(aliasdatum);
1377
2
  free(aliasdatum);
1378
2
  return -1;
1379
257
}
1380
1381
int define_typealias(void)
1382
103
{
1383
103
  char *id;
1384
103
  type_datum_t *t;
1385
1386
103
  if (pass == 2) {
1387
184
    while ((id = queue_remove(id_queue)))
1388
143
      free(id);
1389
41
    return 0;
1390
41
  }
1391
1392
62
  id = (char *)queue_remove(id_queue);
1393
62
  if (!id) {
1394
1
    yyerror("no type name for typealias definition?");
1395
1
    return -1;
1396
1
  }
1397
1398
61
  if (!is_id_in_scope(SYM_TYPES, id)) {
1399
1
    yyerror2("type %s is not within scope", id);
1400
1
    free(id);
1401
1
    return -1;
1402
1
  }
1403
60
  t = hashtab_search(policydbp->p_types.table, id);
1404
60
  if (!t || t->flavor == TYPE_ATTRIB) {
1405
2
    yyerror2("unknown type %s, or it was already declared as an "
1406
2
       "attribute",
1407
2
       id);
1408
2
    free(id);
1409
2
    return -1;
1410
2
  }
1411
58
  free(id);
1412
58
  return add_aliases_to_type(t);
1413
60
}
1414
1415
int define_typeattribute(void)
1416
919
{
1417
919
  char *id;
1418
919
  type_datum_t *t, *attr;
1419
1420
919
  if (pass == 2) {
1421
977
    while ((id = queue_remove(id_queue)))
1422
717
      free(id);
1423
260
    return 0;
1424
260
  }
1425
1426
659
  id = (char *)queue_remove(id_queue);
1427
659
  if (!id) {
1428
1
    yyerror("no type name for typeattribute definition?");
1429
1
    return -1;
1430
1
  }
1431
1432
658
  if (!is_id_in_scope(SYM_TYPES, id)) {
1433
1
    yyerror2("type %s is not within scope", id);
1434
1
    free(id);
1435
1
    return -1;
1436
1
  }
1437
657
  t = hashtab_search(policydbp->p_types.table, id);
1438
657
  if (!t) {
1439
1
    yyerror2("unknown type %s", id);
1440
1
    free(id);
1441
1
    return -1;
1442
656
  } else if (t->flavor == TYPE_ATTRIB) {
1443
461
    if (policydbp->policy_type != POLICY_KERN &&
1444
461
        policydbp->policyvers <
1445
461
          MOD_POLICYDB_VERSION_TYPE_ATTR_ATTRS) {
1446
0
      yyerror2(
1447
0
        "Type attributes cannot be used in a typeattribute definition in policy version %d",
1448
0
        policydbp->policyvers);
1449
0
      free(id);
1450
0
      return -1;
1451
0
    }
1452
461
  }
1453
1454
656
  free(id);
1455
1456
1.68k
  while ((id = queue_remove(id_queue))) {
1457
1.03k
    if (!is_id_in_scope(SYM_TYPES, id)) {
1458
1
      yyerror2("attribute %s is not within scope", id);
1459
1
      free(id);
1460
1
      return -1;
1461
1
    }
1462
1.03k
    attr = hashtab_search(policydbp->p_types.table, id);
1463
1.03k
    if (!attr) {
1464
      /* treat it as a fatal error */
1465
3
      yyerror2("attribute %s is not declared", id);
1466
3
      free(id);
1467
3
      return -1;
1468
3
    }
1469
1470
1.03k
    if (attr->flavor != TYPE_ATTRIB) {
1471
1
      yyerror2("%s is a type, not an attribute", id);
1472
1
      free(id);
1473
1
      return -1;
1474
1
    }
1475
1476
1.03k
    if ((attr = get_local_type(id, attr->s.value, 1)) == NULL) {
1477
0
      yyerror("Out of memory!");
1478
0
      return -1;
1479
0
    }
1480
1481
1.03k
    if (ebitmap_set_bit(&attr->types, (t->s.value - 1), TRUE)) {
1482
0
      yyerror("out of memory");
1483
0
      return -1;
1484
0
    }
1485
1.03k
  }
1486
1487
651
  return 0;
1488
656
}
1489
1490
static int define_typebounds_helper(const char *bounds_id, const char *type_id)
1491
4.14k
{
1492
4.14k
  type_datum_t *bounds, *type;
1493
4.14k
  char *id;
1494
4.14k
  uint32_t value;
1495
1496
4.14k
  if (!is_id_in_scope(SYM_TYPES, bounds_id)) {
1497
2
    yyerror2("type %s is not within scope", bounds_id);
1498
2
    return -1;
1499
2
  }
1500
1501
4.14k
  bounds = hashtab_search(policydbp->p_types.table, bounds_id);
1502
4.14k
  if (!bounds || bounds->flavor == TYPE_ATTRIB) {
1503
4
    yyerror2("type %s is not declared", bounds_id);
1504
4
    return -1;
1505
4
  }
1506
1507
4.14k
  if (!is_id_in_scope(SYM_TYPES, type_id)) {
1508
1
    yyerror2("type %s is not within scope", type_id);
1509
1
    return -1;
1510
1
  }
1511
1512
4.14k
  type = hashtab_search(policydbp->p_types.table, type_id);
1513
4.14k
  if (!type || type->flavor == TYPE_ATTRIB) {
1514
14
    yyerror2("type %s is not declared", type_id);
1515
14
    return -1;
1516
14
  }
1517
1518
4.12k
  id = strdup(type_id);
1519
4.12k
  if (!id) {
1520
0
    yyerror("Out of memory!");
1521
0
    return -1;
1522
0
  }
1523
4.12k
  value = (type->flavor != TYPE_ALIAS) ? type->s.value : type->primary;
1524
4.12k
  type = get_local_type(id, value, 0);
1525
4.12k
  if (!type) {
1526
0
    yyerror("Out of memory!");
1527
0
    return -1;
1528
0
  }
1529
1530
4.12k
  if (!type->bounds)
1531
3.63k
    type->bounds = bounds->s.value;
1532
491
  else if (type->bounds != bounds->s.value) {
1533
0
    yyerror2("type %s has inconsistent bounds %s/%s", type_id,
1534
0
       policydbp->p_type_val_to_name[type->bounds - 1],
1535
0
       policydbp->p_type_val_to_name[bounds->s.value - 1]);
1536
0
    return -1;
1537
0
  }
1538
1539
4.12k
  return 0;
1540
4.12k
}
1541
1542
int define_typebounds(void)
1543
1.28k
{
1544
1.28k
  char *bounds, *id;
1545
1546
1.28k
  if (pass == 1) {
1547
6.60k
    while ((id = queue_remove(id_queue)))
1548
5.70k
      free(id);
1549
897
    return 0;
1550
897
  }
1551
1552
389
  bounds = (char *)queue_remove(id_queue);
1553
389
  if (!bounds) {
1554
0
    yyerror("no type name for typebounds definition?");
1555
0
    return -1;
1556
0
  }
1557
1558
4.32k
  while ((id = queue_remove(id_queue))) {
1559
3.95k
    if (define_typebounds_helper(bounds, id)) {
1560
19
      free(bounds);
1561
19
      free(id);
1562
19
      return -1;
1563
19
    }
1564
1565
3.93k
    free(id);
1566
3.93k
  }
1567
370
  free(bounds);
1568
1569
370
  return 0;
1570
389
}
1571
1572
int define_type(int alias)
1573
12.1k
{
1574
12.1k
  char *id;
1575
12.1k
  type_datum_t *datum, *attr;
1576
1577
12.1k
  if (pass == 2) {
1578
    /*
1579
     * If type name contains ".", we have to define boundary
1580
     * relationship implicitly to keep compatibility with
1581
     * old name based hierarchy.
1582
     */
1583
5.86k
    if ((id = queue_remove(id_queue))) {
1584
5.86k
      const char *delim;
1585
1586
5.86k
      if ((delim = strrchr(id, '.'))) {
1587
194
        int ret;
1588
194
        char *bounds = strdup(id);
1589
194
        if (!bounds) {
1590
0
          yyerror("out of memory");
1591
0
          free(id);
1592
0
          return -1;
1593
0
        }
1594
1595
194
        bounds[(size_t)(delim - id)] = '\0';
1596
1597
194
        ret = define_typebounds_helper(bounds, id);
1598
194
        free(bounds);
1599
194
        if (ret) {
1600
2
          free(id);
1601
2
          return -1;
1602
2
        }
1603
194
      }
1604
5.86k
      free(id);
1605
5.86k
    }
1606
1607
5.86k
    if (alias) {
1608
1.69k
      while ((id = queue_remove(id_queue)))
1609
1.55k
        free(id);
1610
137
    }
1611
1612
6.31k
    while ((id = queue_remove(id_queue)))
1613
451
      free(id);
1614
5.86k
    return 0;
1615
5.86k
  }
1616
1617
6.28k
  if ((datum = declare_type(TRUE, FALSE)) == NULL) {
1618
5
    return -1;
1619
5
  }
1620
1621
6.27k
  if (alias) {
1622
199
    if (add_aliases_to_type(datum) == -1) {
1623
3
      return -1;
1624
3
    }
1625
199
  }
1626
1627
7.23k
  while ((id = queue_remove(id_queue))) {
1628
972
    if (!is_id_in_scope(SYM_TYPES, id)) {
1629
1
      yyerror2("attribute %s is not within scope", id);
1630
1
      free(id);
1631
1
      return -1;
1632
1
    }
1633
971
    attr = hashtab_search(policydbp->p_types.table, id);
1634
971
    if (!attr) {
1635
      /* treat it as a fatal error */
1636
11
      yyerror2("attribute %s is not declared", id);
1637
11
      free(id);
1638
11
      return -1;
1639
11
    }
1640
1641
960
    if (attr->flavor != TYPE_ATTRIB) {
1642
3
      yyerror2("%s is a type, not an attribute", id);
1643
3
      free(id);
1644
3
      return -1;
1645
3
    }
1646
1647
957
    if ((attr = get_local_type(id, attr->s.value, 1)) == NULL) {
1648
0
      yyerror("Out of memory!");
1649
0
      return -1;
1650
0
    }
1651
1652
957
    if (ebitmap_set_bit(&attr->types, datum->s.value - 1, TRUE)) {
1653
0
      yyerror("Out of memory");
1654
0
      return -1;
1655
0
    }
1656
957
  }
1657
1658
6.25k
  return 0;
1659
6.27k
}
1660
1661
struct val_to_name {
1662
  unsigned int val;
1663
  char *name;
1664
};
1665
1666
/* Adds a type, given by its textual name, to a typeset.  If *add is
1667
   0, then add the type to the negative set; otherwise if *add is 1
1668
   then add it to the positive side.
1669
   The identifier `id` is always consumed. */
1670
static int set_types(type_set_t *set, char *id, int *add, char starallowed)
1671
119k
{
1672
119k
  type_datum_t *t;
1673
1674
119k
  if (strcmp(id, "*") == 0) {
1675
4.45k
    free(id);
1676
4.45k
    if (!starallowed) {
1677
4
      yyerror("* not allowed in this type of rule");
1678
4
      return -1;
1679
4
    }
1680
    /* set TYPE_STAR flag */
1681
4.45k
    set->flags = TYPE_STAR;
1682
4.45k
    *add = 1;
1683
4.45k
    return 0;
1684
4.45k
  }
1685
1686
115k
  if (strcmp(id, "~") == 0) {
1687
4.76k
    free(id);
1688
4.76k
    if (!starallowed) {
1689
6
      yyerror("~ not allowed in this type of rule");
1690
6
      return -1;
1691
6
    }
1692
    /* complement the set */
1693
4.75k
    set->flags = TYPE_COMP;
1694
4.75k
    *add = 1;
1695
4.75k
    return 0;
1696
4.76k
  }
1697
1698
110k
  if (strcmp(id, "-") == 0) {
1699
17.3k
    *add = 0;
1700
17.3k
    free(id);
1701
17.3k
    return 0;
1702
17.3k
  }
1703
1704
93.3k
  if (!is_id_in_scope(SYM_TYPES, id)) {
1705
10
    yyerror2("type %s is not within scope", id);
1706
10
    free(id);
1707
10
    return -1;
1708
10
  }
1709
93.3k
  t = hashtab_search(policydbp->p_types.table, id);
1710
93.3k
  if (!t) {
1711
347
    yyerror2("unknown type %s", id);
1712
347
    free(id);
1713
347
    return -1;
1714
347
  }
1715
1716
93.0k
  if (*add == 0) {
1717
17.2k
    if (ebitmap_set_bit(&set->negset, t->s.value - 1, TRUE))
1718
0
      goto oom;
1719
75.7k
  } else {
1720
75.7k
    if (ebitmap_set_bit(&set->types, t->s.value - 1, TRUE))
1721
0
      goto oom;
1722
75.7k
  }
1723
93.0k
  free(id);
1724
93.0k
  *add = 1;
1725
93.0k
  return 0;
1726
0
oom:
1727
0
  yyerror("Out of memory");
1728
0
  free(id);
1729
0
  return -1;
1730
93.0k
}
1731
1732
static int define_compute_type_helper(int which, avrule_t **rule)
1733
1.51k
{
1734
1.51k
  char *id;
1735
1.51k
  type_datum_t *datum;
1736
1.51k
  ebitmap_t tclasses;
1737
1.51k
  ebitmap_node_t *node;
1738
1.51k
  avrule_t *avrule;
1739
1.51k
  class_perm_node_t *perm;
1740
1.51k
  uint32_t i;
1741
1.51k
  int add = 1;
1742
1743
1.51k
  avrule = malloc(sizeof(avrule_t));
1744
1.51k
  if (!avrule) {
1745
0
    yyerror("out of memory");
1746
0
    return -1;
1747
0
  }
1748
1.51k
  avrule_init(avrule);
1749
1.51k
  avrule->specified = which;
1750
1.51k
  avrule->line = policydb_lineno;
1751
1.51k
  avrule->source_line = source_lineno;
1752
1.51k
  avrule->source_filename = strdup(source_file);
1753
1.51k
  if (!avrule->source_filename) {
1754
0
    yyerror("out of memory");
1755
0
    return -1;
1756
0
  }
1757
1758
1.51k
  ebitmap_init(&tclasses);
1759
1760
3.02k
  while ((id = queue_remove(id_queue))) {
1761
1.52k
    if (set_types(&avrule->stypes, id, &add, 0))
1762
9
      goto bad;
1763
1.52k
  }
1764
1.50k
  add = 1;
1765
4.83k
  while ((id = queue_remove(id_queue))) {
1766
3.37k
    if (strcmp(id, "self") == 0) {
1767
323
      free(id);
1768
323
      if (add == 0) {
1769
1
        yyerror("-self is not supported");
1770
1
        goto bad;
1771
1
      }
1772
322
      avrule->flags |= RULE_SELF;
1773
322
      continue;
1774
323
    }
1775
3.04k
    if (set_types(&avrule->ttypes, id, &add, 0))
1776
38
      goto bad;
1777
3.04k
  }
1778
1779
1.46k
  if (read_classes(&tclasses))
1780
10
    goto bad;
1781
1782
1.45k
  id = (char *)queue_remove(id_queue);
1783
1.45k
  if (!id) {
1784
0
    yyerror("no newtype?");
1785
0
    goto bad;
1786
0
  }
1787
1.45k
  if (!is_id_in_scope(SYM_TYPES, id)) {
1788
2
    yyerror2("type %s is not within scope", id);
1789
2
    free(id);
1790
2
    goto bad;
1791
2
  }
1792
1.45k
  datum = (type_datum_t *)hashtab_search(policydbp->p_types.table,
1793
1.45k
                 (hashtab_key_t)id);
1794
1.45k
  if (!datum || datum->flavor == TYPE_ATTRIB) {
1795
16
    yyerror2("unknown type %s", id);
1796
16
    free(id);
1797
16
    goto bad;
1798
16
  }
1799
1.43k
  free(id);
1800
1801
91.9k
  ebitmap_for_each_positive_bit(&tclasses, node, i) {
1802
1.43k
    perm = malloc(sizeof(class_perm_node_t));
1803
1.43k
    if (!perm) {
1804
0
      yyerror("out of memory");
1805
0
      goto bad;
1806
0
    }
1807
1.43k
    class_perm_node_init(perm);
1808
1.43k
    perm->tclass = i + 1;
1809
1.43k
    perm->data = datum->s.value;
1810
1.43k
    perm->next = avrule->perms;
1811
1.43k
    avrule->perms = perm;
1812
1.43k
  }
1813
1.43k
  ebitmap_destroy(&tclasses);
1814
1815
1.43k
  *rule = avrule;
1816
1.43k
  return 0;
1817
1818
76
bad:
1819
76
  ebitmap_destroy(&tclasses);
1820
76
  avrule_destroy(avrule);
1821
76
  free(avrule);
1822
76
  return -1;
1823
1.43k
}
1824
1825
int define_compute_type(int which)
1826
5.89k
{
1827
5.89k
  char *id;
1828
5.89k
  avrule_t *avrule;
1829
1830
5.89k
  if (pass == 1) {
1831
8.83k
    while ((id = queue_remove(id_queue)))
1832
4.43k
      free(id);
1833
11.8k
    while ((id = queue_remove(id_queue)))
1834
7.41k
      free(id);
1835
10.2k
    while ((id = queue_remove(id_queue)))
1836
5.82k
      free(id);
1837
4.39k
    id = queue_remove(id_queue);
1838
4.39k
    free(id);
1839
4.39k
    return 0;
1840
4.39k
  }
1841
1842
1.49k
  if (define_compute_type_helper(which, &avrule))
1843
69
    return -1;
1844
1845
1.42k
  append_avrule(avrule);
1846
1.42k
  return 0;
1847
1.49k
}
1848
1849
avrule_t *define_cond_compute_type(int which)
1850
2.23k
{
1851
2.23k
  char *id;
1852
2.23k
  avrule_t *avrule;
1853
1854
2.23k
  if (pass == 1) {
1855
4.27k
    while ((id = queue_remove(id_queue)))
1856
2.05k
      free(id);
1857
5.15k
    while ((id = queue_remove(id_queue)))
1858
2.93k
      free(id);
1859
5.47k
    while ((id = queue_remove(id_queue)))
1860
3.25k
      free(id);
1861
2.21k
    id = queue_remove(id_queue);
1862
2.21k
    free(id);
1863
2.21k
    return (avrule_t *)1;
1864
2.21k
  }
1865
1866
17
  if (define_compute_type_helper(which, &avrule))
1867
7
    return COND_ERR;
1868
1869
10
  return avrule;
1870
17
}
1871
1872
int define_bool_tunable(int is_tunable)
1873
136
{
1874
136
  char *id, *bool_value;
1875
136
  cond_bool_datum_t *datum;
1876
136
  int ret;
1877
136
  uint32_t value;
1878
1879
136
  if (pass == 2) {
1880
138
    while ((id = queue_remove(id_queue)))
1881
92
      free(id);
1882
46
    return 0;
1883
46
  }
1884
1885
90
  id = (char *)queue_remove(id_queue);
1886
90
  if (!id) {
1887
1
    yyerror("no identifier for bool definition?");
1888
1
    return -1;
1889
1
  }
1890
89
  if (id_has_dot(id)) {
1891
1
    yyerror2("boolean identifier %s may not contain periods", id);
1892
1
    free(id);
1893
1
    return -1;
1894
1
  }
1895
88
  datum = calloc(1, sizeof(cond_bool_datum_t));
1896
88
  if (!datum) {
1897
0
    yyerror("out of memory");
1898
0
    free(id);
1899
0
    return -1;
1900
0
  }
1901
88
  if (is_tunable)
1902
34
    datum->flags |= COND_BOOL_FLAGS_TUNABLE;
1903
88
  ret = declare_symbol(SYM_BOOLS, id, datum, &value, &value);
1904
88
  switch (ret) {
1905
0
  case -3: {
1906
0
    yyerror("Out of memory!");
1907
0
    goto cleanup;
1908
0
  }
1909
2
  case -2: {
1910
2
    yyerror2("duplicate declaration of boolean %s", id);
1911
2
    goto cleanup;
1912
0
  }
1913
1
  case -1: {
1914
1
    yyerror2("could not declare boolean %s here", id);
1915
1
    goto cleanup;
1916
0
  }
1917
82
  case 0: {
1918
82
    break;
1919
0
  }
1920
3
  case 1: {
1921
3
    goto cleanup;
1922
0
  }
1923
0
  default: {
1924
0
    assert(0); /* should never get here */
1925
0
  }
1926
88
  }
1927
82
  datum->s.value = value;
1928
1929
82
  bool_value = (char *)queue_remove(id_queue);
1930
82
  if (!bool_value) {
1931
1
    yyerror("no default value for bool definition?");
1932
1
    return -1;
1933
1
  }
1934
1935
81
  datum->state = (bool_value[0] == 'T') ? 1 : 0;
1936
81
  free(bool_value);
1937
81
  return 0;
1938
6
cleanup:
1939
6
  cond_destroy_bool(id, datum, NULL);
1940
6
  return ret == 1 ? 0 : -1;
1941
82
}
1942
1943
avrule_t *define_cond_pol_list(avrule_t *avlist, avrule_t *sl)
1944
32.5k
{
1945
32.5k
  avrule_t *last;
1946
1947
32.5k
  if (pass == 1) {
1948
    /* return something so we get through pass 1 */
1949
20.4k
    return (avrule_t *)1;
1950
20.4k
  }
1951
1952
12.1k
  if (sl == NULL) {
1953
    /* This is a require block, return previous list */
1954
706
    return avlist;
1955
706
  }
1956
1957
  /* Prepend the new avlist to the pre-existing one.
1958
   * An extended permission statement might consist of multiple av
1959
   * rules. */
1960
16.3k
  for (last = sl; last->next; last = last->next)
1961
4.90k
    ;
1962
11.4k
  last->next = avlist;
1963
11.4k
  return sl;
1964
12.1k
}
1965
1966
typedef struct av_xperm_range {
1967
  uint16_t low;
1968
  uint16_t high;
1969
} av_xperm_range_t;
1970
1971
struct av_xperm_range_list {
1972
  uint8_t omit;
1973
  av_xperm_range_t range;
1974
  struct av_xperm_range_list *next;
1975
};
1976
1977
static int avrule_sort_xperms(struct av_xperm_range_list **rangehead)
1978
9.16k
{
1979
9.16k
  struct av_xperm_range_list *r, *r2, *sorted, *sortedhead = NULL;
1980
1981
  /* order list by range.low */
1982
18.3k
  for (r = *rangehead; r != NULL; r = r->next) {
1983
9.16k
    sorted = malloc(sizeof(struct av_xperm_range_list));
1984
9.16k
    if (sorted == NULL)
1985
0
      goto error;
1986
9.16k
    memcpy(sorted, r, sizeof(struct av_xperm_range_list));
1987
9.16k
    sorted->next = NULL;
1988
9.16k
    if (sortedhead == NULL) {
1989
9.16k
      sortedhead = sorted;
1990
9.16k
      continue;
1991
9.16k
    }
1992
0
    for (r2 = sortedhead; r2 != NULL; r2 = r2->next) {
1993
0
      if (sorted->range.low < r2->range.low) {
1994
        /* range is the new head */
1995
0
        sorted->next = r2;
1996
0
        sortedhead = sorted;
1997
0
        break;
1998
0
      } else if ((r2->next != NULL) &&
1999
0
           (r->range.low < r2->next->range.low)) {
2000
        /* insert range between elements */
2001
0
        sorted->next = r2->next;
2002
0
        r2->next = sorted;
2003
0
        break;
2004
0
      } else if (r2->next == NULL) {
2005
        /* range is the new tail*/
2006
0
        r2->next = sorted;
2007
0
        break;
2008
0
      }
2009
0
    }
2010
0
  }
2011
2012
9.16k
  r = *rangehead;
2013
18.3k
  while (r != NULL) {
2014
9.16k
    r2 = r;
2015
9.16k
    r = r->next;
2016
9.16k
    free(r2);
2017
9.16k
  }
2018
9.16k
  *rangehead = sortedhead;
2019
9.16k
  return 0;
2020
0
error:
2021
0
  yyerror("out of memory");
2022
0
  return -1;
2023
9.16k
}
2024
2025
static void avrule_merge_xperms(struct av_xperm_range_list **rangehead)
2026
9.16k
{
2027
9.16k
  struct av_xperm_range_list *r, *tmp;
2028
9.16k
  r = *rangehead;
2029
9.16k
  while (r != NULL && r->next != NULL) {
2030
    /* merge */
2031
0
    if ((r->range.high + 1) >= r->next->range.low) {
2032
      /* keep the higher of the two */
2033
0
      if (r->range.high < r->next->range.high)
2034
0
        r->range.high = r->next->range.high;
2035
0
      tmp = r->next;
2036
0
      r->next = r->next->next;
2037
0
      free(tmp);
2038
0
      continue;
2039
0
    }
2040
0
    r = r->next;
2041
0
  }
2042
9.16k
}
2043
2044
static int avrule_read_xperm_value(const char *id, uint16_t *value)
2045
9.18k
{
2046
9.18k
  unsigned long val = strtoul(id, NULL, 0);
2047
2048
9.18k
  if (val > UINT16_MAX) {
2049
27
    yyerror2(
2050
27
      "extended permission value 0x%lx exceeds the maximum of 0x%x",
2051
27
      val, UINT16_MAX);
2052
27
    return -1;
2053
27
  }
2054
9.16k
  *value = (uint16_t)val;
2055
9.16k
  return 0;
2056
9.18k
}
2057
2058
static int avrule_read_xperm_ranges(struct av_xperm_range_list **rangehead)
2059
9.18k
{
2060
9.18k
  char *id;
2061
9.18k
  struct av_xperm_range_list *rnew, *r = NULL;
2062
9.18k
  uint8_t omit = 0;
2063
2064
9.18k
  *rangehead = NULL;
2065
2066
  /* read in all the ioctl/netlink commands */
2067
25.6k
  while ((id = queue_remove(id_queue))) {
2068
16.4k
    if (strcmp(id, "~") == 0) {
2069
      /* these are values to be omitted */
2070
7.26k
      free(id);
2071
7.26k
      omit = 1;
2072
9.18k
    } else if (strcmp(id, "-") == 0) {
2073
      /* high value of range */
2074
0
      free(id);
2075
0
      id = queue_remove(id_queue);
2076
0
      if (avrule_read_xperm_value(id, &r->range.high)) {
2077
0
        free(id);
2078
0
        return -1;
2079
0
      }
2080
0
      if (r->range.high < r->range.low) {
2081
0
        yyerror2(
2082
0
          "extended permission range %#x-%#x must be in ascending order.",
2083
0
          r->range.low, r->range.high);
2084
0
        return -1;
2085
0
      }
2086
0
      free(id);
2087
9.18k
    } else {
2088
      /* read in new low value */
2089
9.18k
      rnew = malloc(sizeof(struct av_xperm_range_list));
2090
9.18k
      if (rnew == NULL)
2091
0
        goto error;
2092
9.18k
      rnew->next = NULL;
2093
9.18k
      if (*rangehead == NULL) {
2094
9.18k
        *rangehead = rnew;
2095
9.18k
        r = *rangehead;
2096
9.18k
      } else {
2097
0
        r->next = rnew;
2098
0
        r = r->next;
2099
0
      }
2100
9.18k
      if (avrule_read_xperm_value(id, &rnew->range.low)) {
2101
27
        free(id);
2102
27
        return -1;
2103
27
      }
2104
9.16k
      rnew->range.high = rnew->range.low;
2105
9.16k
      free(id);
2106
9.16k
    }
2107
16.4k
  }
2108
9.16k
  r = *rangehead;
2109
9.16k
  if (r) {
2110
9.16k
    r->omit = omit;
2111
9.16k
  }
2112
9.16k
  return 0;
2113
0
error:
2114
0
  yyerror("out of memory");
2115
0
  return -1;
2116
9.18k
}
2117
2118
/* flip to included ranges */
2119
static int avrule_omit_xperms(struct av_xperm_range_list **rangehead)
2120
7.26k
{
2121
7.26k
  struct av_xperm_range_list *rnew, *r, *newhead = NULL, *tail = NULL;
2122
7.26k
  uint32_t next_low = 0;
2123
2124
  /*
2125
   * The input ranges are sorted and merged. Emit the gaps between them
2126
   * as the complement. next_low is the first value not yet covered by an
2127
   * emitted (allowed) range; promote it to uint32_t so that high + 1 does
2128
   * not wrap when a range ends at 0xffff.
2129
   */
2130
14.5k
  for (r = *rangehead; r != NULL; r = r->next) {
2131
7.26k
    if (r->range.low > next_low) {
2132
6.69k
      rnew = calloc(1, sizeof(struct av_xperm_range_list));
2133
6.69k
      if (!rnew)
2134
0
        goto error;
2135
6.69k
      rnew->range.low = next_low;
2136
6.69k
      rnew->range.high = r->range.low - 1;
2137
6.69k
      if (tail)
2138
0
        tail->next = rnew;
2139
6.69k
      else
2140
6.69k
        newhead = rnew;
2141
6.69k
      tail = rnew;
2142
6.69k
    }
2143
7.26k
    next_low = (uint32_t)r->range.high + 1;
2144
7.26k
  }
2145
2146
7.26k
  if (next_low <= UINT16_MAX) {
2147
7.04k
    rnew = calloc(1, sizeof(struct av_xperm_range_list));
2148
7.04k
    if (!rnew)
2149
0
      goto error;
2150
7.04k
    rnew->range.low = next_low;
2151
7.04k
    rnew->range.high = UINT16_MAX;
2152
7.04k
    if (tail)
2153
6.48k
      tail->next = rnew;
2154
564
    else
2155
564
      newhead = rnew;
2156
7.04k
    tail = rnew;
2157
7.04k
  }
2158
2159
7.26k
  r = *rangehead;
2160
14.5k
  while (r != NULL) {
2161
7.26k
    rnew = r;
2162
7.26k
    r = r->next;
2163
7.26k
    free(rnew);
2164
7.26k
  }
2165
7.26k
  *rangehead = newhead;
2166
7.26k
  return 0;
2167
2168
0
error:
2169
0
  while (newhead != NULL) {
2170
0
    rnew = newhead;
2171
0
    newhead = newhead->next;
2172
0
    free(rnew);
2173
0
  }
2174
0
  yyerror("out of memory");
2175
0
  return -1;
2176
7.26k
}
2177
2178
static int avrule_xperm_ranges(struct av_xperm_range_list **rangelist)
2179
9.18k
{
2180
9.18k
  struct av_xperm_range_list *rangehead;
2181
9.18k
  uint8_t omit;
2182
2183
  /* read in ranges to include and omit */
2184
9.18k
  if (avrule_read_xperm_ranges(&rangehead))
2185
27
    return -1;
2186
9.16k
  if (rangehead == NULL) {
2187
0
    yyerror("error processing ioctl/netlink commands");
2188
0
    return -1;
2189
0
  }
2190
9.16k
  omit = rangehead->omit;
2191
  /* sort and merge the input ranges */
2192
9.16k
  if (avrule_sort_xperms(&rangehead))
2193
0
    return -1;
2194
9.16k
  avrule_merge_xperms(&rangehead);
2195
  /* flip ranges if these are omitted */
2196
9.16k
  if (omit) {
2197
7.26k
    if (avrule_omit_xperms(&rangehead))
2198
0
      return -1;
2199
7.26k
  }
2200
2201
9.16k
  *rangelist = rangehead;
2202
9.16k
  return 0;
2203
9.16k
}
2204
2205
static int define_te_avtab_xperms_helper(int which, avrule_t **rule)
2206
9.48k
{
2207
9.48k
  char *id;
2208
9.48k
  class_perm_node_t *perms, *tail = NULL, *cur_perms = NULL;
2209
9.48k
  const class_datum_t *cladatum;
2210
9.48k
  const perm_datum_t *perdatum;
2211
9.48k
  ebitmap_t tclasses;
2212
9.48k
  ebitmap_node_t *node;
2213
9.48k
  avrule_t *avrule;
2214
9.48k
  unsigned int i;
2215
9.48k
  int add = 1, ret;
2216
2217
9.48k
  avrule = malloc(sizeof(avrule_t));
2218
9.48k
  if (!avrule) {
2219
0
    yyerror("out of memory");
2220
0
    goto out;
2221
0
  }
2222
9.48k
  avrule_init(avrule);
2223
9.48k
  avrule->specified = which;
2224
9.48k
  avrule->line = policydb_lineno;
2225
9.48k
  avrule->source_line = source_lineno;
2226
9.48k
  avrule->source_filename = strdup(source_file);
2227
9.48k
  avrule->xperms = NULL;
2228
9.48k
  if (!avrule->source_filename) {
2229
0
    yyerror("out of memory");
2230
0
    goto out;
2231
0
  }
2232
2233
19.0k
  while ((id = queue_remove(id_queue))) {
2234
9.58k
    if (set_types(&avrule->stypes, id, &add,
2235
9.58k
            which == AVRULE_XPERMS_NEVERALLOW ? 1 : 0)) {
2236
16
      goto out;
2237
16
    }
2238
9.58k
  }
2239
9.46k
  add = 1;
2240
19.1k
  while ((id = queue_remove(id_queue))) {
2241
9.75k
    if (strcmp(id, "self") == 0) {
2242
740
      free(id);
2243
740
      if (add == 0 && which != AVRULE_XPERMS_NEVERALLOW) {
2244
0
        yyerror("-self is only supported in neverallow and neverallowxperm rules");
2245
0
        goto out;
2246
0
      }
2247
740
      avrule->flags |= (add ? RULE_SELF : RULE_NOTSELF);
2248
740
      if ((avrule->flags & RULE_SELF) &&
2249
740
          (avrule->flags & RULE_NOTSELF)) {
2250
0
        yyerror("self and -self are mutual exclusive");
2251
0
        goto out;
2252
0
      }
2253
740
      continue;
2254
740
    }
2255
9.01k
    if (set_types(&avrule->ttypes, id, &add,
2256
9.01k
            which == AVRULE_XPERMS_NEVERALLOW ? 1 : 0)) {
2257
38
      goto out;
2258
38
    }
2259
9.01k
  }
2260
2261
9.42k
  if ((avrule->ttypes.flags & TYPE_COMP)) {
2262
279
    if (avrule->flags & RULE_NOTSELF) {
2263
0
      yyerror("-self is not supported in complements");
2264
0
      goto out;
2265
0
    }
2266
279
    if (avrule->flags & RULE_SELF) {
2267
0
      avrule->flags &= ~RULE_SELF;
2268
0
      avrule->flags |= RULE_NOTSELF;
2269
0
    }
2270
279
  }
2271
2272
9.42k
  ebitmap_init(&tclasses);
2273
9.42k
  ret = read_classes(&tclasses);
2274
9.42k
  if (ret)
2275
8
    goto out2;
2276
2277
9.42k
  perms = NULL;
2278
9.42k
  id = queue_head(id_queue);
2279
602k
  ebitmap_for_each_positive_bit(&tclasses, node, i) {
2280
9.42k
    cur_perms = malloc(sizeof(class_perm_node_t));
2281
9.42k
    if (!cur_perms) {
2282
0
      yyerror("out of memory");
2283
0
      goto out2;
2284
0
    }
2285
9.42k
    class_perm_node_init(cur_perms);
2286
9.42k
    cur_perms->tclass = i + 1;
2287
9.42k
    if (!perms)
2288
9.42k
      perms = cur_perms;
2289
9.42k
    if (tail)
2290
0
      tail->next = cur_perms;
2291
9.42k
    tail = cur_perms;
2292
2293
9.42k
    cladatum = policydbp->class_val_to_struct[i];
2294
9.42k
    perdatum = hashtab_search(cladatum->permissions.table, id);
2295
9.42k
    if (!perdatum) {
2296
9.19k
      if (cladatum->comdatum) {
2297
0
        perdatum = hashtab_search(
2298
0
          cladatum->comdatum->permissions.table,
2299
0
          id);
2300
0
      }
2301
9.19k
    }
2302
9.42k
    if (!perdatum) {
2303
9.19k
      yyerror2("permission %s is not defined"
2304
9.19k
         " for class %s",
2305
9.19k
         id, policydbp->p_class_val_to_name[i]);
2306
9.19k
      continue;
2307
9.19k
    } else if (!is_perm_in_scope(
2308
231
           id, policydbp->p_class_val_to_name[i])) {
2309
0
      yyerror2("permission %s of class %s is"
2310
0
         " not within scope",
2311
0
         id, policydbp->p_class_val_to_name[i]);
2312
0
      continue;
2313
231
    } else {
2314
231
      cur_perms->data |= UINT32_C(1)
2315
231
             << (perdatum->s.value - 1);
2316
231
    }
2317
9.42k
  }
2318
2319
9.42k
  ebitmap_destroy(&tclasses);
2320
2321
9.42k
  avrule->perms = perms;
2322
9.42k
  *rule = avrule;
2323
9.42k
  return 0;
2324
2325
8
out2:
2326
8
  ebitmap_destroy(&tclasses);
2327
62
out:
2328
62
  avrule_destroy(avrule);
2329
62
  free(avrule);
2330
62
  return -1;
2331
8
}
2332
2333
/* index of the u32 containing the permission */
2334
205k
#define XPERM_IDX(x) ((x) >> 5)
2335
/* set bits 0 through x-1 within the u32 */
2336
19.7k
#define XPERM_SETBITS(x) ((UINT32_C(1) << ((x) & 0x1f)) - 1)
2337
/* low value for this u32 */
2338
184k
#define XPERM_LOW(x) ((x) << 5)
2339
/* high value for this u32 */
2340
161k
#define XPERM_HIGH(x) ((((x) + 1) << 5) - 1)
2341
static void avrule_xperm_setrangebits(uint16_t low, uint16_t high,
2342
              av_extended_perms_t *xperms)
2343
26.1k
{
2344
26.1k
  unsigned int i;
2345
26.1k
  uint16_t h = high + 1;
2346
  /* for each u32 that this low-high range touches, set driver permissions */
2347
178k
  for (i = XPERM_IDX(low); i <= XPERM_IDX(high); i++) {
2348
    /* set all bits in u32 */
2349
152k
    if ((low <= XPERM_LOW(i)) && (high >= XPERM_HIGH(i)))
2350
134k
      xperms->perms[i] |= ~0U;
2351
    /* set low bits */
2352
18.7k
    else if ((low <= XPERM_LOW(i)) && (high < XPERM_HIGH(i)))
2353
7.23k
      xperms->perms[i] |= XPERM_SETBITS(h);
2354
    /* set high bits */
2355
11.4k
    else if ((low > XPERM_LOW(i)) && (high >= XPERM_HIGH(i)))
2356
10.4k
      xperms->perms[i] |= ~0U - XPERM_SETBITS(low);
2357
    /* set middle bits */
2358
1.07k
    else if ((low > XPERM_LOW(i)) && (high <= XPERM_HIGH(i)))
2359
1.07k
      xperms->perms[i] |=
2360
1.07k
        XPERM_SETBITS(h) - XPERM_SETBITS(low);
2361
152k
  }
2362
26.1k
}
2363
2364
static int avrule_xperms_used(const av_extended_perms_t *xperms)
2365
39.6k
{
2366
39.6k
  unsigned int i;
2367
2368
76.0k
  for (i = 0; i < sizeof(xperms->perms) / sizeof(xperms->perms[0]); i++) {
2369
74.8k
    if (xperms->perms[i])
2370
38.4k
      return 1;
2371
74.8k
  }
2372
1.20k
  return 0;
2373
39.6k
}
2374
2375
/*
2376
 * using definitions found in kernel document ioctl-number.txt
2377
 * The kernel components of an ioctl command are:
2378
 * dir, size, driver, and function. Only the driver and function fields
2379
 * are considered here
2380
 */
2381
114k
#define IOC_DRIV(x) ((x) >> 8)
2382
49.1k
#define IOC_FUNC(x) ((x) & 0xff)
2383
34.6k
#define IOC_CMD(driver, func) (((driver) << 8) + (func))
2384
static int avrule_xperm_partialdriver(struct av_xperm_range_list *rangelist,
2385
              av_extended_perms_t *complete_driver,
2386
              av_extended_perms_t **extended_perms)
2387
9.16k
{
2388
9.16k
  struct av_xperm_range_list *r;
2389
9.16k
  av_extended_perms_t *xperms;
2390
9.16k
  uint8_t low, high;
2391
2392
9.16k
  xperms = calloc(1, sizeof(av_extended_perms_t));
2393
9.16k
  if (!xperms) {
2394
0
    yyerror("out of memory");
2395
0
    return -1;
2396
0
  }
2397
2398
9.16k
  r = rangelist;
2399
24.8k
  while (r) {
2400
15.6k
    low = IOC_DRIV(r->range.low);
2401
15.6k
    high = IOC_DRIV(r->range.high);
2402
15.6k
    if (complete_driver) {
2403
8.71k
      if (!xperm_test(low, complete_driver->perms))
2404
5.87k
        xperm_set(low, xperms->perms);
2405
8.71k
      if (!xperm_test(high, complete_driver->perms))
2406
4.41k
        xperm_set(high, xperms->perms);
2407
8.71k
    } else {
2408
6.93k
      xperm_set(low, xperms->perms);
2409
6.93k
      xperm_set(high, xperms->perms);
2410
6.93k
    }
2411
15.6k
    r = r->next;
2412
15.6k
  }
2413
9.16k
  if (avrule_xperms_used(xperms)) {
2414
9.16k
    *extended_perms = xperms;
2415
9.16k
  } else {
2416
0
    free(xperms);
2417
0
    *extended_perms = NULL;
2418
0
  }
2419
9.16k
  return 0;
2420
9.16k
}
2421
2422
static int avrule_ioctl_completedriver(struct av_xperm_range_list *rangelist,
2423
               av_extended_perms_t **extended_perms)
2424
5.83k
{
2425
5.83k
  struct av_xperm_range_list *r;
2426
5.83k
  av_extended_perms_t *xperms;
2427
5.83k
  uint16_t low, high;
2428
5.83k
  xperms = calloc(1, sizeof(av_extended_perms_t));
2429
5.83k
  if (!xperms) {
2430
0
    yyerror("out of memory");
2431
0
    return -1;
2432
0
  }
2433
2434
5.83k
  r = rangelist;
2435
15.7k
  while (r) {
2436
    /*
2437
     * Any driver code that has sequence 0x00 - 0xff is a complete code,
2438
     *
2439
     * if command number = 0xff, then round high up to next code,
2440
     * else 0x00 - 0xfe keep current code
2441
     * of this range. temporarily u32 for the + 1
2442
     * to account for possible rollover before right shift
2443
     */
2444
9.91k
    high = IOC_DRIV((uint32_t)(r->range.high + 1));
2445
    /* if 0x00 keep current driver code else 0x01 - 0xff round up to next code*/
2446
9.91k
    low = IOC_DRIV(r->range.low);
2447
9.91k
    if (IOC_FUNC(r->range.low))
2448
5.05k
      low++;
2449
9.91k
    if (high > low)
2450
6.55k
      avrule_xperm_setrangebits(low, high - 1, xperms);
2451
9.91k
    r = r->next;
2452
9.91k
  }
2453
5.83k
  if (avrule_xperms_used(xperms)) {
2454
4.63k
    xperms->driver = 0x00;
2455
4.63k
    xperms->specified = AVRULE_XPERMS_IOCTLDRIVER;
2456
4.63k
    *extended_perms = xperms;
2457
4.63k
  } else {
2458
1.20k
    free(xperms);
2459
1.20k
    *extended_perms = NULL;
2460
1.20k
  }
2461
5.83k
  return 0;
2462
5.83k
}
2463
2464
static int avrule_xperm_func(struct av_xperm_range_list *rangelist,
2465
           av_extended_perms_t **extended_perms,
2466
           unsigned int driver, uint8_t specified)
2467
15.5k
{
2468
15.5k
  struct av_xperm_range_list *r;
2469
15.5k
  av_extended_perms_t *xperms;
2470
15.5k
  uint16_t low, high;
2471
2472
15.5k
  *extended_perms = NULL;
2473
15.5k
  xperms = calloc(1, sizeof(av_extended_perms_t));
2474
15.5k
  if (!xperms) {
2475
0
    yyerror("out of memory");
2476
0
    return -1;
2477
0
  }
2478
2479
15.5k
  r = rangelist;
2480
  /* for the passed in driver code, find the ranges that apply */
2481
43.6k
  while (r) {
2482
28.1k
    low = r->range.low;
2483
28.1k
    high = r->range.high;
2484
28.1k
    if ((driver != IOC_DRIV(low)) && (driver != IOC_DRIV(high))) {
2485
8.49k
      r = r->next;
2486
8.49k
      continue;
2487
8.49k
    }
2488
2489
19.6k
    if (driver == IOC_DRIV(low)) {
2490
12.8k
      if (high > IOC_CMD(driver, 0xff))
2491
8.26k
        high = IOC_CMD(driver, 0xff);
2492
2493
12.8k
    } else {
2494
6.80k
      if (low < IOC_CMD(driver, 0))
2495
6.80k
        low = IOC_CMD(driver, 0);
2496
6.80k
    }
2497
2498
19.6k
    low = IOC_FUNC(low);
2499
19.6k
    high = IOC_FUNC(high);
2500
19.6k
    avrule_xperm_setrangebits(low, high, xperms);
2501
19.6k
    xperms->driver = driver;
2502
19.6k
    xperms->specified = specified;
2503
19.6k
    r = r->next;
2504
19.6k
  }
2505
2506
15.5k
  if (avrule_xperms_used(xperms)) {
2507
15.5k
    *extended_perms = xperms;
2508
15.5k
  } else {
2509
0
    free(xperms);
2510
0
    *extended_perms = NULL;
2511
0
  }
2512
15.5k
  return 0;
2513
15.5k
}
2514
2515
static unsigned int xperms_for_each_bit(unsigned int *bit,
2516
          av_extended_perms_t *xperms)
2517
24.6k
{
2518
24.6k
  unsigned int i;
2519
2.36M
  for (i = *bit; i < sizeof(xperms->perms) * 8; i++) {
2520
2.36M
    if (xperm_test(i, xperms->perms)) {
2521
15.5k
      xperm_clear(i, xperms->perms);
2522
15.5k
      *bit = i;
2523
15.5k
      return 1;
2524
15.5k
    }
2525
2.36M
  }
2526
9.16k
  return 0;
2527
24.6k
}
2528
2529
static int avrule_cpy(avrule_t *dest, const avrule_t *src)
2530
20.1k
{
2531
20.1k
  class_perm_node_t *src_perms;
2532
20.1k
  class_perm_node_t *dest_perms, *dest_tail;
2533
20.1k
  dest_tail = NULL;
2534
2535
20.1k
  avrule_init(dest);
2536
20.1k
  dest->specified = src->specified;
2537
20.1k
  dest->flags = src->flags;
2538
20.1k
  if (type_set_cpy(&dest->stypes, &src->stypes)) {
2539
0
    yyerror("out of memory");
2540
0
    return -1;
2541
0
  }
2542
20.1k
  if (type_set_cpy(&dest->ttypes, &src->ttypes)) {
2543
0
    yyerror("out of memory");
2544
0
    return -1;
2545
0
  }
2546
20.1k
  dest->line = src->line;
2547
20.1k
  dest->source_filename = strdup(source_file);
2548
20.1k
  if (!dest->source_filename) {
2549
0
    yyerror("out of memory");
2550
0
    return -1;
2551
0
  }
2552
20.1k
  dest->source_line = src->source_line;
2553
2554
  /* increment through the class perms and copy over */
2555
20.1k
  src_perms = src->perms;
2556
40.2k
  while (src_perms) {
2557
20.1k
    dest_perms = calloc(1, sizeof(class_perm_node_t));
2558
20.1k
    if (!dest_perms) {
2559
0
      yyerror("out of memory");
2560
0
      return -1;
2561
0
    }
2562
20.1k
    class_perm_node_init(dest_perms);
2563
2564
20.1k
    if (!dest->perms)
2565
20.1k
      dest->perms = dest_perms;
2566
0
    else
2567
0
      dest_tail->next = dest_perms;
2568
2569
20.1k
    dest_perms->tclass = src_perms->tclass;
2570
20.1k
    dest_perms->data = src_perms->data;
2571
20.1k
    dest_perms->next = NULL;
2572
20.1k
    dest_tail = dest_perms;
2573
20.1k
    src_perms = src_perms->next;
2574
20.1k
  }
2575
20.1k
  return 0;
2576
20.1k
}
2577
2578
static int define_te_avtab_ioctl(const avrule_t *avrule_template,
2579
         avrule_t **ret_avrules)
2580
5.85k
{
2581
5.85k
  avrule_t *avrule, *ret = NULL, **last = &ret;
2582
5.85k
  struct av_xperm_range_list *rangelist, *r;
2583
5.85k
  av_extended_perms_t *complete_driver, *partial_driver, *xperms;
2584
5.85k
  unsigned int i;
2585
2586
  /* organize ioctl ranges */
2587
5.85k
  if (avrule_xperm_ranges(&rangelist))
2588
18
    return -1;
2589
2590
  /* create rule for ioctl driver types that are entirely enabled */
2591
5.83k
  if (avrule_ioctl_completedriver(rangelist, &complete_driver))
2592
0
    return -1;
2593
5.83k
  if (complete_driver) {
2594
4.63k
    avrule = calloc(1, sizeof(avrule_t));
2595
4.63k
    if (!avrule) {
2596
0
      yyerror("out of memory");
2597
0
      return -1;
2598
0
    }
2599
4.63k
    if (avrule_cpy(avrule, avrule_template))
2600
0
      return -1;
2601
4.63k
    avrule->xperms = complete_driver;
2602
2603
4.63k
    if (ret_avrules) {
2604
4.43k
      *last = avrule;
2605
4.43k
      last = &(avrule->next);
2606
4.43k
    } else {
2607
197
      append_avrule(avrule);
2608
197
    }
2609
4.63k
  }
2610
2611
  /* flag ioctl driver codes that are partially enabled */
2612
5.83k
  if (avrule_xperm_partialdriver(rangelist, complete_driver,
2613
5.83k
               &partial_driver))
2614
0
    return -1;
2615
2616
5.83k
  if (!partial_driver || !avrule_xperms_used(partial_driver))
2617
0
    goto done;
2618
2619
  /*
2620
   * create rule for each partially used driver codes
2621
   * "partially used" meaning that the code number e.g. socket 0x89
2622
   * has some permission bits set and others not set.
2623
   */
2624
5.83k
  i = 0;
2625
11.6k
  while (xperms_for_each_bit(&i, partial_driver)) {
2626
5.83k
    if (avrule_xperm_func(rangelist, &xperms, i,
2627
5.83k
              AVRULE_XPERMS_IOCTLFUNCTION))
2628
0
      return -1;
2629
2630
5.83k
    if (xperms) {
2631
5.83k
      avrule = calloc(1, sizeof(avrule_t));
2632
5.83k
      if (!avrule) {
2633
0
        yyerror("out of memory");
2634
0
        return -1;
2635
0
      }
2636
5.83k
      if (avrule_cpy(avrule, avrule_template))
2637
0
        return -1;
2638
5.83k
      avrule->xperms = xperms;
2639
2640
5.83k
      if (ret_avrules) {
2641
4.66k
        *last = avrule;
2642
4.66k
        last = &(avrule->next);
2643
4.66k
      } else {
2644
1.17k
        append_avrule(avrule);
2645
1.17k
      }
2646
5.83k
    }
2647
5.83k
  }
2648
2649
5.83k
done:
2650
5.83k
  if (partial_driver)
2651
5.83k
    free(partial_driver);
2652
2653
15.7k
  while (rangelist != NULL) {
2654
9.91k
    r = rangelist;
2655
9.91k
    rangelist = rangelist->next;
2656
9.91k
    free(r);
2657
9.91k
  }
2658
2659
5.83k
  if (ret_avrules)
2660
4.66k
    *ret_avrules = ret;
2661
2662
5.83k
  return 0;
2663
5.83k
}
2664
2665
static int define_te_avtab_netlink(const avrule_t *avrule_template,
2666
           avrule_t **ret_avrules)
2667
3.33k
{
2668
3.33k
  avrule_t *avrule, *ret = NULL, **last = &ret;
2669
3.33k
  struct av_xperm_range_list *rangelist, *r;
2670
3.33k
  av_extended_perms_t *partial_driver, *xperms;
2671
3.33k
  unsigned int i;
2672
2673
  /* organize ranges */
2674
3.33k
  if (avrule_xperm_ranges(&rangelist))
2675
9
    return -1;
2676
2677
  /* flag driver codes that are partially enabled */
2678
3.32k
  if (avrule_xperm_partialdriver(rangelist, NULL, &partial_driver))
2679
0
    return -1;
2680
2681
3.32k
  if (!partial_driver || !avrule_xperms_used(partial_driver))
2682
0
    goto done;
2683
2684
  /*
2685
   * create rule for each partially used driver codes
2686
   * "partially used" meaning that the code number e.g. socket 0x89
2687
   * has some permission bits set and others not set.
2688
   */
2689
3.32k
  i = 0;
2690
12.9k
  while (xperms_for_each_bit(&i, partial_driver)) {
2691
9.66k
    if (avrule_xperm_func(rangelist, &xperms, i,
2692
9.66k
              AVRULE_XPERMS_NLMSG))
2693
0
      return -1;
2694
2695
9.66k
    if (xperms) {
2696
9.66k
      avrule = calloc(1, sizeof(avrule_t));
2697
9.66k
      if (!avrule) {
2698
0
        yyerror("out of memory");
2699
0
        return -1;
2700
0
      }
2701
9.66k
      if (avrule_cpy(avrule, avrule_template))
2702
0
        return -1;
2703
9.66k
      avrule->xperms = xperms;
2704
2705
9.66k
      if (ret_avrules) {
2706
968
        *last = avrule;
2707
968
        last = &(avrule->next);
2708
8.69k
      } else {
2709
8.69k
        append_avrule(avrule);
2710
8.69k
      }
2711
9.66k
    }
2712
9.66k
  }
2713
2714
3.32k
done:
2715
3.32k
  if (partial_driver)
2716
3.32k
    free(partial_driver);
2717
2718
9.05k
  while (rangelist != NULL) {
2719
5.72k
    r = rangelist;
2720
5.72k
    rangelist = rangelist->next;
2721
5.72k
    free(r);
2722
5.72k
  }
2723
2724
3.32k
  if (ret_avrules)
2725
505
    *ret_avrules = ret;
2726
2727
3.32k
  return 0;
2728
3.32k
}
2729
2730
avrule_t *define_cond_te_avtab_extended_perms(int which)
2731
12.8k
{
2732
12.8k
  char *id;
2733
12.8k
  unsigned int i;
2734
12.8k
  avrule_t *avrule_template, *rules = NULL;
2735
12.8k
  int rc = 0;
2736
2737
12.8k
  if (policydbp->policy_type == POLICY_KERN &&
2738
0
      policydbp->policyvers < POLICYDB_VERSION_COND_XPERMS) {
2739
0
    yyerror2(
2740
0
      "extended permissions in conditional policies are only supported since policy version %d, found policy version %d",
2741
0
      POLICYDB_VERSION_COND_XPERMS, policydbp->policyvers);
2742
0
    return COND_ERR;
2743
0
  }
2744
12.8k
  if (policydbp->policy_type != POLICY_KERN &&
2745
12.8k
      policydbp->policyvers < MOD_POLICYDB_VERSION_COND_XPERMS) {
2746
0
    yyerror2(
2747
0
      "extended permissions in conditional policies are only supported since module policy version %d, found module policy version %d",
2748
0
      MOD_POLICYDB_VERSION_COND_XPERMS,
2749
0
      policydbp->policyvers);
2750
0
    return COND_ERR;
2751
0
  }
2752
2753
12.8k
  if (pass == 1) {
2754
37.2k
    for (i = 0; i < 4; i++) {
2755
73.6k
      while ((id = queue_remove(id_queue)))
2756
43.8k
        free(id);
2757
29.7k
    }
2758
7.44k
    return (avrule_t *)1; /* any non-NULL value */
2759
7.44k
  }
2760
2761
  /* populate avrule template with source/target/tclass */
2762
5.39k
  if (define_te_avtab_xperms_helper(which, &avrule_template))
2763
50
    return COND_ERR;
2764
2765
5.34k
  id = queue_remove(id_queue);
2766
5.34k
  if (strcmp(id, "ioctl") == 0) {
2767
4.67k
    rc = define_te_avtab_ioctl(avrule_template, &rules);
2768
4.67k
  } else if (strcmp(id, "nlmsg") == 0) {
2769
514
    rc = define_te_avtab_netlink(avrule_template, &rules);
2770
514
  } else {
2771
149
    yyerror2(
2772
149
      "only ioctl and nlmsg extended permissions are supported, found %s",
2773
149
      id);
2774
149
    rc = -1;
2775
149
  }
2776
2777
5.34k
  free(id);
2778
5.34k
  avrule_destroy(avrule_template);
2779
5.34k
  free(avrule_template);
2780
2781
5.34k
  if (rc) {
2782
176
    avrule_destroy(rules);
2783
176
    return NULL;
2784
176
  }
2785
2786
5.16k
  return rules;
2787
5.34k
}
2788
2789
int define_te_avtab_extended_perms(int which)
2790
9.74k
{
2791
9.74k
  char *id;
2792
9.74k
  unsigned int i;
2793
9.74k
  avrule_t *avrule_template;
2794
9.74k
  int rc = 0;
2795
2796
9.74k
  if (pass == 1) {
2797
28.2k
    for (i = 0; i < 4; i++) {
2798
54.7k
      while ((id = queue_remove(id_queue)))
2799
32.1k
        free(id);
2800
22.6k
    }
2801
5.65k
    return 0;
2802
5.65k
  }
2803
2804
  /* populate avrule template with source/target/tclass */
2805
4.09k
  if (define_te_avtab_xperms_helper(which, &avrule_template))
2806
12
    return -1;
2807
2808
4.07k
  id = queue_remove(id_queue);
2809
4.07k
  if (strcmp(id, "ioctl") == 0) {
2810
1.17k
    rc = define_te_avtab_ioctl(avrule_template, NULL);
2811
2.90k
  } else if (strcmp(id, "nlmsg") == 0) {
2812
2.81k
    rc = define_te_avtab_netlink(avrule_template, NULL);
2813
2.81k
  } else {
2814
84
    yyerror2(
2815
84
      "only ioctl and nlmsg extended permissions are supported, found %s",
2816
84
      id);
2817
84
    rc = -1;
2818
84
  }
2819
2820
4.07k
  free(id);
2821
4.07k
  avrule_destroy(avrule_template);
2822
4.07k
  free(avrule_template);
2823
2824
4.07k
  return rc;
2825
4.09k
}
2826
2827
#define PERMISSION_MASK(nprim)                          \
2828
17.6k
  ((nprim) == PERM_SYMTAB_SIZE ? (~UINT32_C(0)) : \
2829
17.6k
               ((UINT32_C(1) << (nprim)) - 1))
2830
2831
static int define_te_avtab_helper(int which, avrule_t **rule)
2832
19.6k
{
2833
19.6k
  char *id;
2834
19.6k
  class_datum_t *cladatum;
2835
19.6k
  perm_datum_t *perdatum = NULL;
2836
19.6k
  class_perm_node_t *perms, *tail = NULL, *cur_perms = NULL;
2837
19.6k
  ebitmap_t tclasses;
2838
19.6k
  ebitmap_node_t *node;
2839
19.6k
  avrule_t *avrule;
2840
19.6k
  unsigned int i;
2841
19.6k
  int add = 1, ret = 0;
2842
19.6k
  int suppress = 0;
2843
2844
19.6k
  ebitmap_init(&tclasses);
2845
2846
19.6k
  avrule = malloc(sizeof(avrule_t));
2847
19.6k
  if (!avrule) {
2848
0
    yyerror("memory error");
2849
0
    ret = -1;
2850
0
    goto out;
2851
0
  }
2852
19.6k
  avrule_init(avrule);
2853
19.6k
  avrule->specified = which;
2854
19.6k
  avrule->line = policydb_lineno;
2855
19.6k
  avrule->source_line = source_lineno;
2856
19.6k
  avrule->source_filename = strdup(source_file);
2857
19.6k
  avrule->xperms = NULL;
2858
19.6k
  if (!avrule->source_filename) {
2859
0
    yyerror("out of memory");
2860
0
    return -1;
2861
0
  }
2862
2863
39.4k
  while ((id = queue_remove(id_queue))) {
2864
19.7k
    if (set_types(&avrule->stypes, id, &add,
2865
19.7k
            which == AVRULE_NEVERALLOW ? 1 : 0)) {
2866
35
      ret = -1;
2867
35
      goto out;
2868
35
    }
2869
19.7k
  }
2870
19.6k
  add = 1;
2871
44.2k
  while ((id = queue_remove(id_queue))) {
2872
24.5k
    if (strcmp(id, "self") == 0) {
2873
8.81k
      free(id);
2874
8.81k
      if (add == 0 && which != AVRULE_NEVERALLOW) {
2875
1
        yyerror("-self is only supported in neverallow and neverallowxperm rules");
2876
1
        ret = -1;
2877
1
        goto out;
2878
1
      }
2879
8.81k
      avrule->flags |= (add ? RULE_SELF : RULE_NOTSELF);
2880
8.81k
      if ((avrule->flags & RULE_SELF) &&
2881
8.76k
          (avrule->flags & RULE_NOTSELF)) {
2882
1
        yyerror("self and -self are mutual exclusive");
2883
1
        ret = -1;
2884
1
        goto out;
2885
1
      }
2886
8.81k
      continue;
2887
8.81k
    }
2888
15.7k
    if (set_types(&avrule->ttypes, id, &add,
2889
15.7k
            which == AVRULE_NEVERALLOW ? 1 : 0)) {
2890
37
      ret = -1;
2891
37
      goto out;
2892
37
    }
2893
15.7k
  }
2894
2895
19.6k
  if ((avrule->ttypes.flags & TYPE_COMP)) {
2896
4.43k
    if (avrule->flags & RULE_NOTSELF) {
2897
1
      yyerror("-self is not supported in complements");
2898
1
      ret = -1;
2899
1
      goto out;
2900
1
    }
2901
4.43k
    if (avrule->flags & RULE_SELF) {
2902
3.15k
      avrule->flags &= ~RULE_SELF;
2903
3.15k
      avrule->flags |= RULE_NOTSELF;
2904
3.15k
    }
2905
4.43k
  }
2906
2907
19.6k
  ret = read_classes(&tclasses);
2908
19.6k
  if (ret)
2909
15
    goto out;
2910
2911
19.5k
  perms = NULL;
2912
1.25M
  ebitmap_for_each_positive_bit(&tclasses, node, i) {
2913
19.5k
    cur_perms = malloc(sizeof(class_perm_node_t));
2914
19.5k
    if (!cur_perms) {
2915
0
      yyerror("out of memory");
2916
0
      ret = -1;
2917
0
      goto out;
2918
0
    }
2919
19.5k
    class_perm_node_init(cur_perms);
2920
19.5k
    cur_perms->tclass = i + 1;
2921
19.5k
    if (!perms)
2922
19.5k
      perms = cur_perms;
2923
19.5k
    if (tail)
2924
0
      tail->next = cur_perms;
2925
19.5k
    tail = cur_perms;
2926
19.5k
  }
2927
2928
56.9k
  while ((id = queue_remove(id_queue))) {
2929
37.3k
    cur_perms = perms;
2930
2.39M
    ebitmap_for_each_positive_bit(&tclasses, node, i) {
2931
37.3k
      cladatum = policydbp->class_val_to_struct[i];
2932
2933
37.3k
      if (strcmp(id, "*") == 0) {
2934
        /* set all declared permissions in the class */
2935
2
        cur_perms->data = PERMISSION_MASK(
2936
2
          cladatum->permissions.nprim);
2937
2
        goto next;
2938
2
      }
2939
2940
37.3k
      if (strcmp(id, "~") == 0) {
2941
        /* complement the set */
2942
15.8k
        if (which == AVRULE_DONTAUDIT)
2943
862
          yywarn("dontaudit rule with a ~?");
2944
15.8k
        cur_perms->data =
2945
15.8k
          ~cur_perms->data &
2946
15.8k
          PERMISSION_MASK(
2947
15.8k
            cladatum->permissions.nprim);
2948
15.8k
        if (cur_perms->data == 0) {
2949
1.00k
          class_perm_node_t *tmp = cur_perms;
2950
1.00k
          yywarn("omitting avrule with no permission set");
2951
1.00k
          if (perms == cur_perms)
2952
1.00k
            perms = cur_perms->next;
2953
1.00k
          cur_perms = cur_perms->next;
2954
1.00k
          free(tmp);
2955
1.00k
          continue;
2956
1.00k
        }
2957
14.8k
        goto next;
2958
15.8k
      }
2959
2960
21.4k
      perdatum =
2961
21.4k
        hashtab_search(cladatum->permissions.table, id);
2962
21.4k
      if (!perdatum) {
2963
7.90k
        if (cladatum->comdatum) {
2964
0
          perdatum = hashtab_search(
2965
0
            cladatum->comdatum->permissions
2966
0
              .table,
2967
0
            id);
2968
0
        }
2969
7.90k
      }
2970
21.4k
      if (!perdatum) {
2971
7.90k
        if (!suppress)
2972
7.90k
          yyerror2("permission %s is not defined"
2973
7.90k
             " for class %s",
2974
7.90k
             id,
2975
7.90k
             policydbp->p_class_val_to_name
2976
7.90k
               [i]);
2977
7.90k
        continue;
2978
13.5k
      } else if (!is_perm_in_scope(
2979
13.5k
             id,
2980
13.5k
             policydbp->p_class_val_to_name[i])) {
2981
0
        if (!suppress) {
2982
0
          yyerror2("permission %s of class %s is"
2983
0
             " not within scope",
2984
0
             id,
2985
0
             policydbp->p_class_val_to_name
2986
0
               [i]);
2987
0
        }
2988
0
        continue;
2989
13.5k
      } else {
2990
13.5k
        cur_perms->data |= UINT32_C(1)
2991
13.5k
               << (perdatum->s.value - 1);
2992
13.5k
      }
2993
28.4k
next:
2994
28.4k
      cur_perms = cur_perms->next;
2995
28.4k
    }
2996
2997
37.3k
    free(id);
2998
37.3k
  }
2999
3000
19.5k
  avrule->perms = perms;
3001
19.5k
  *rule = avrule;
3002
3003
19.6k
out:
3004
19.6k
  if (ret) {
3005
90
    avrule_destroy(avrule);
3006
90
    free(avrule);
3007
90
  }
3008
3009
19.6k
  ebitmap_destroy(&tclasses);
3010
3011
19.6k
  return ret;
3012
19.5k
}
3013
3014
avrule_t *define_cond_te_avtab(int which)
3015
16.4k
{
3016
16.4k
  char *id;
3017
16.4k
  avrule_t *avrule;
3018
16.4k
  int i;
3019
3020
16.4k
  if (pass == 1) {
3021
50.7k
    for (i = 0; i < 4; i++) {
3022
91.8k
      while ((id = queue_remove(id_queue)))
3023
51.2k
        free(id);
3024
40.6k
    }
3025
10.1k
    return (avrule_t *)1; /* any non-NULL value */
3026
10.1k
  }
3027
3028
6.25k
  if (define_te_avtab_helper(which, &avrule))
3029
25
    return COND_ERR;
3030
3031
6.23k
  return avrule;
3032
6.25k
}
3033
3034
int define_te_avtab(int which)
3035
29.7k
{
3036
29.7k
  char *id;
3037
29.7k
  avrule_t *avrule;
3038
29.7k
  int i;
3039
3040
29.7k
  if (pass == 1) {
3041
81.5k
    for (i = 0; i < 4; i++) {
3042
150k
      while ((id = queue_remove(id_queue)))
3043
84.7k
        free(id);
3044
65.2k
    }
3045
16.3k
    return 0;
3046
16.3k
  }
3047
3048
13.4k
  if (define_te_avtab_helper(which, &avrule))
3049
65
    return -1;
3050
3051
  /* append this avrule to the end of the current rules list */
3052
13.3k
  append_avrule(avrule);
3053
13.3k
  return 0;
3054
13.4k
}
3055
3056
/* The role-types rule is no longer used to declare regular role or
3057
 * role attribute, but solely aimed for declaring role-types associations.
3058
 */
3059
int define_role_types(void)
3060
10.8k
{
3061
10.8k
  role_datum_t *role;
3062
10.8k
  char *id;
3063
10.8k
  int add = 1;
3064
3065
10.8k
  if (pass == 1) {
3066
18.7k
    while ((id = queue_remove(id_queue)))
3067
12.8k
      free(id);
3068
5.89k
    return 0;
3069
5.89k
  }
3070
3071
4.92k
  id = (char *)queue_remove(id_queue);
3072
4.92k
  if (!id) {
3073
0
    yyerror("no role name for role-types rule?");
3074
0
    return -1;
3075
0
  }
3076
3077
4.92k
  if (!is_id_in_scope(SYM_ROLES, id)) {
3078
4
    yyerror2("role %s is not within scope", id);
3079
4
    free(id);
3080
4
    return -1;
3081
4
  }
3082
3083
4.91k
  role = hashtab_search(policydbp->p_roles.table, id);
3084
4.91k
  if (!role) {
3085
34
    yyerror2("unknown role %s", id);
3086
34
    free(id);
3087
34
    return -1;
3088
34
  }
3089
4.88k
  role = get_local_role(id, role->s.value, (role->flavor == ROLE_ATTRIB));
3090
3091
11.0k
  while ((id = queue_remove(id_queue))) {
3092
6.14k
    if (set_types(&role->types, id, &add, 0))
3093
5
      return -1;
3094
6.14k
  }
3095
3096
4.87k
  return 0;
3097
4.88k
}
3098
3099
int define_attrib_role(void)
3100
2.24k
{
3101
2.24k
  if (pass == 2) {
3102
1.08k
    free(queue_remove(id_queue));
3103
1.08k
    return 0;
3104
1.08k
  }
3105
3106
  /* Declare a role attribute */
3107
1.16k
  if (declare_role(TRUE) == NULL)
3108
1
    return -1;
3109
3110
1.15k
  return 0;
3111
1.16k
}
3112
3113
int define_role_attr(void)
3114
304k
{
3115
304k
  char *id;
3116
304k
  role_datum_t *r, *attr;
3117
3118
304k
  if (pass == 2) {
3119
193k
    while ((id = queue_remove(id_queue)))
3120
104k
      free(id);
3121
88.4k
    return 0;
3122
88.4k
  }
3123
3124
  /* Declare a regular role */
3125
215k
  if ((r = declare_role(FALSE)) == NULL)
3126
4
    return -1;
3127
3128
250k
  while ((id = queue_remove(id_queue))) {
3129
34.7k
    if (!is_id_in_scope(SYM_ROLES, id)) {
3130
7
      yyerror2("attribute %s is not within scope", id);
3131
7
      free(id);
3132
7
      return -1;
3133
7
    }
3134
34.7k
    attr = hashtab_search(policydbp->p_roles.table, id);
3135
34.7k
    if (!attr) {
3136
      /* treat it as a fatal error */
3137
18
      yyerror2("role attribute %s is not declared", id);
3138
18
      free(id);
3139
18
      return -1;
3140
18
    }
3141
3142
34.6k
    if (attr->flavor != ROLE_ATTRIB) {
3143
10
      yyerror2("%s is a regular role, not an attribute", id);
3144
10
      free(id);
3145
10
      return -1;
3146
10
    }
3147
3148
34.6k
    if ((attr = get_local_role(id, attr->s.value, 1)) == NULL) {
3149
0
      yyerror("Out of memory!");
3150
0
      return -1;
3151
0
    }
3152
3153
34.6k
    if (ebitmap_set_bit(&attr->roles, (r->s.value - 1), TRUE)) {
3154
0
      yyerror("out of memory");
3155
0
      return -1;
3156
0
    }
3157
34.6k
  }
3158
3159
215k
  return 0;
3160
215k
}
3161
3162
int define_roleattribute(void)
3163
825
{
3164
825
  char *id;
3165
825
  role_datum_t *r, *attr;
3166
3167
825
  if (pass == 2) {
3168
1.06k
    while ((id = queue_remove(id_queue)))
3169
712
      free(id);
3170
355
    return 0;
3171
355
  }
3172
3173
470
  id = (char *)queue_remove(id_queue);
3174
470
  if (!id) {
3175
1
    yyerror("no role name for roleattribute definition?");
3176
1
    return -1;
3177
1
  }
3178
3179
469
  if (!is_id_in_scope(SYM_ROLES, id)) {
3180
1
    yyerror2("role %s is not within scope", id);
3181
1
    free(id);
3182
1
    return -1;
3183
1
  }
3184
468
  r = hashtab_search(policydbp->p_roles.table, id);
3185
  /* We support adding one role attribute into another */
3186
468
  if (!r) {
3187
1
    yyerror2("unknown role %s", id);
3188
1
    free(id);
3189
1
    return -1;
3190
1
  }
3191
467
  free(id);
3192
3193
968
  while ((id = queue_remove(id_queue))) {
3194
504
    if (!is_id_in_scope(SYM_ROLES, id)) {
3195
1
      yyerror2("attribute %s is not within scope", id);
3196
1
      free(id);
3197
1
      return -1;
3198
1
    }
3199
503
    attr = hashtab_search(policydbp->p_roles.table, id);
3200
503
    if (!attr) {
3201
      /* treat it as a fatal error */
3202
1
      yyerror2("role attribute %s is not declared", id);
3203
1
      free(id);
3204
1
      return -1;
3205
1
    }
3206
3207
502
    if (attr->flavor != ROLE_ATTRIB) {
3208
1
      yyerror2("%s is a regular role, not an attribute", id);
3209
1
      free(id);
3210
1
      return -1;
3211
1
    }
3212
3213
501
    if ((attr = get_local_role(id, attr->s.value, 1)) == NULL) {
3214
0
      yyerror("Out of memory!");
3215
0
      return -1;
3216
0
    }
3217
3218
501
    if (ebitmap_set_bit(&attr->roles, (r->s.value - 1), TRUE)) {
3219
0
      yyerror("out of memory");
3220
0
      return -1;
3221
0
    }
3222
501
  }
3223
3224
464
  return 0;
3225
467
}
3226
3227
static int role_val_to_name_helper(hashtab_key_t key, hashtab_datum_t datum,
3228
           void *p)
3229
485
{
3230
485
  struct val_to_name *v = p;
3231
485
  role_datum_t *roldatum;
3232
3233
485
  roldatum = (role_datum_t *)datum;
3234
3235
485
  if (v->val == roldatum->s.value) {
3236
31
    v->name = key;
3237
31
    return 1;
3238
31
  }
3239
3240
454
  return 0;
3241
485
}
3242
3243
static char *role_val_to_name(unsigned int val)
3244
31
{
3245
31
  struct val_to_name v;
3246
31
  int rc;
3247
3248
31
  v.val = val;
3249
31
  rc = hashtab_map(policydbp->p_roles.table, role_val_to_name_helper, &v);
3250
31
  if (rc)
3251
31
    return v.name;
3252
0
  return NULL;
3253
31
}
3254
3255
static int set_roles(role_set_t *set, char *id)
3256
16.4k
{
3257
16.4k
  role_datum_t *r;
3258
3259
16.4k
  if (strcmp(id, "*") == 0) {
3260
1
    free(id);
3261
1
    yyerror("* is not allowed for role sets");
3262
1
    return -1;
3263
1
  }
3264
3265
16.3k
  if (strcmp(id, "~") == 0) {
3266
1
    free(id);
3267
1
    yyerror("~ is not allowed for role sets");
3268
1
    return -1;
3269
1
  }
3270
16.3k
  if (!is_id_in_scope(SYM_ROLES, id)) {
3271
3
    yyerror2("role %s is not within scope", id);
3272
3
    free(id);
3273
3
    return -1;
3274
3
  }
3275
16.3k
  r = hashtab_search(policydbp->p_roles.table, id);
3276
16.3k
  if (!r) {
3277
24
    yyerror2("unknown role %s", id);
3278
24
    free(id);
3279
24
    return -1;
3280
24
  }
3281
3282
16.3k
  if (ebitmap_set_bit(&set->roles, r->s.value - 1, TRUE)) {
3283
0
    yyerror("out of memory");
3284
0
    free(id);
3285
0
    return -1;
3286
0
  }
3287
16.3k
  free(id);
3288
16.3k
  return 0;
3289
16.3k
}
3290
3291
int define_role_trans(int class_specified)
3292
10.0k
{
3293
10.0k
  char *id;
3294
10.0k
  const role_datum_t *role;
3295
10.0k
  role_set_t roles;
3296
10.0k
  type_set_t types;
3297
10.0k
  const class_datum_t *cladatum;
3298
10.0k
  ebitmap_t e_types, e_roles, e_classes;
3299
10.0k
  ebitmap_node_t *tnode, *rnode, *cnode;
3300
10.0k
  struct role_trans *tr = NULL;
3301
10.0k
  struct role_trans_rule *rule = NULL;
3302
10.0k
  unsigned int i, j, k;
3303
10.0k
  int add = 1;
3304
3305
10.0k
  if (pass == 1) {
3306
20.3k
    while ((id = queue_remove(id_queue)))
3307
12.3k
      free(id);
3308
38.5k
    while ((id = queue_remove(id_queue)))
3309
30.5k
      free(id);
3310
8.04k
    if (class_specified)
3311
20.0k
      while ((id = queue_remove(id_queue)))
3312
12.3k
        free(id);
3313
8.04k
    id = queue_remove(id_queue);
3314
8.04k
    free(id);
3315
8.04k
    return 0;
3316
8.04k
  }
3317
3318
2.01k
  role_set_init(&roles);
3319
2.01k
  ebitmap_init(&e_roles);
3320
2.01k
  type_set_init(&types);
3321
2.01k
  ebitmap_init(&e_types);
3322
2.01k
  ebitmap_init(&e_classes);
3323
3324
4.01k
  while ((id = queue_remove(id_queue))) {
3325
2.01k
    if (set_roles(&roles, id))
3326
9
      goto bad;
3327
2.01k
  }
3328
2.00k
  add = 1;
3329
21.5k
  while ((id = queue_remove(id_queue))) {
3330
19.5k
    if (set_types(&types, id, &add, 0))
3331
3
      goto bad;
3332
19.5k
  }
3333
3334
1.99k
  if (class_specified) {
3335
1.80k
    if (read_classes(&e_classes))
3336
2
      goto bad;
3337
1.80k
  } else {
3338
193
    cladatum =
3339
193
      hashtab_search(policydbp->p_classes.table, "process");
3340
193
    if (!cladatum) {
3341
3
      yyerror2("could not find process class for "
3342
3
         "legacy role_transition statement");
3343
3
      goto bad;
3344
3
    }
3345
3346
190
    if (ebitmap_set_bit(&e_classes, cladatum->s.value - 1, TRUE)) {
3347
0
      yyerror("out of memory");
3348
0
      goto bad;
3349
0
    }
3350
190
  }
3351
3352
1.99k
  id = (char *)queue_remove(id_queue);
3353
1.99k
  if (!id) {
3354
0
    yyerror("no new role in transition definition?");
3355
0
    goto bad;
3356
0
  }
3357
1.99k
  if (!is_id_in_scope(SYM_ROLES, id)) {
3358
1
    yyerror2("role %s is not within scope", id);
3359
1
    free(id);
3360
1
    goto bad;
3361
1
  }
3362
1.99k
  role = hashtab_search(policydbp->p_roles.table, id);
3363
1.99k
  if (!role) {
3364
18
    yyerror2("unknown role %s used in transition definition", id);
3365
18
    free(id);
3366
18
    goto bad;
3367
18
  }
3368
3369
1.97k
  if (role->flavor != ROLE_ROLE) {
3370
1
    yyerror2("the new role %s must be a regular role", id);
3371
1
    free(id);
3372
1
    goto bad;
3373
1
  }
3374
1.97k
  free(id);
3375
3376
  /* This ebitmap business is just to ensure that there are not conflicting role_trans rules */
3377
1.97k
  if (role_set_expand(&roles, &e_roles, policydbp, NULL, NULL))
3378
0
    goto bad;
3379
3380
1.97k
  if (type_set_expand(&types, &e_types, policydbp, 1))
3381
0
    goto bad;
3382
3383
124k
  ebitmap_for_each_positive_bit(&e_roles, rnode, i) {
3384
10.3k
    ebitmap_for_each_positive_bit(&e_types, tnode, j) {
3385
10.0k
      ebitmap_for_each_positive_bit(&e_classes, cnode, k) {
3386
406
        for (tr = policydbp->role_tr; tr;
3387
250
             tr = tr->next) {
3388
250
          if (tr->role == (i + 1) &&
3389
69
              tr->type == (j + 1) &&
3390
31
              tr->tclass == (k + 1)) {
3391
31
            yyerror2(
3392
31
              "duplicate role "
3393
31
              "transition for "
3394
31
              "(%s,%s,%s)",
3395
31
              role_val_to_name(i + 1),
3396
31
              policydbp->p_type_val_to_name
3397
31
                [j],
3398
31
              policydbp->p_class_val_to_name
3399
31
                [k]);
3400
31
            goto bad;
3401
31
          }
3402
250
        }
3403
3404
156
        tr = calloc(1, sizeof(struct role_trans));
3405
156
        if (!tr) {
3406
0
          yyerror("out of memory");
3407
0
          goto bad;
3408
0
        }
3409
156
        tr->role = i + 1;
3410
156
        tr->type = j + 1;
3411
156
        tr->tclass = k + 1;
3412
156
        tr->new_role = role->s.value;
3413
156
        tr->next = policydbp->role_tr;
3414
156
        policydbp->role_tr = tr;
3415
156
      }
3416
187
    }
3417
1.97k
  }
3418
  /* Now add the real rule */
3419
1.94k
  rule = calloc(1, sizeof(struct role_trans_rule));
3420
1.94k
  if (!rule) {
3421
0
    yyerror("out of memory");
3422
0
    goto bad;
3423
0
  }
3424
1.94k
  rule->roles = roles;
3425
1.94k
  rule->types = types;
3426
1.94k
  rule->classes = e_classes;
3427
1.94k
  rule->new_role = role->s.value;
3428
3429
1.94k
  append_role_trans(rule);
3430
3431
1.94k
  ebitmap_destroy(&e_roles);
3432
1.94k
  ebitmap_destroy(&e_types);
3433
3434
1.94k
  return 0;
3435
3436
68
bad:
3437
68
  role_set_destroy(&roles);
3438
68
  type_set_destroy(&types);
3439
68
  ebitmap_destroy(&e_roles);
3440
68
  ebitmap_destroy(&e_types);
3441
68
  ebitmap_destroy(&e_classes);
3442
68
  return -1;
3443
1.94k
}
3444
3445
int define_role_allow(void)
3446
27.7k
{
3447
27.7k
  char *id;
3448
27.7k
  struct role_allow_rule *ra = NULL;
3449
3450
27.7k
  if (pass == 1) {
3451
42.5k
    while ((id = queue_remove(id_queue)))
3452
22.0k
      free(id);
3453
43.1k
    while ((id = queue_remove(id_queue)))
3454
22.5k
      free(id);
3455
20.5k
    return 0;
3456
20.5k
  }
3457
3458
7.19k
  ra = malloc(sizeof(role_allow_rule_t));
3459
7.19k
  if (!ra) {
3460
0
    yyerror("out of memory");
3461
0
    return -1;
3462
0
  }
3463
7.19k
  role_allow_rule_init(ra);
3464
3465
14.3k
  while ((id = queue_remove(id_queue))) {
3466
7.19k
    if (set_roles(&ra->roles, id)) {
3467
6
      role_allow_rule_destroy(ra);
3468
6
      free(ra);
3469
6
      return -1;
3470
6
    }
3471
7.19k
  }
3472
3473
14.3k
  while ((id = queue_remove(id_queue))) {
3474
7.19k
    if (set_roles(&ra->new_roles, id)) {
3475
14
      role_allow_rule_destroy(ra);
3476
14
      free(ra);
3477
14
      return -1;
3478
14
    }
3479
7.19k
  }
3480
3481
7.17k
  append_role_allow(ra);
3482
7.17k
  return 0;
3483
7.19k
}
3484
3485
avrule_t *define_cond_filename_trans(void)
3486
1
{
3487
1
  yyerror("type transitions with a filename not allowed inside "
3488
1
    "conditionals");
3489
1
  return COND_ERR;
3490
1
}
3491
3492
int define_filename_trans(void)
3493
8.69k
{
3494
8.69k
  char *id, *name = NULL;
3495
8.69k
  type_set_t stypes, ttypes;
3496
8.69k
  ebitmap_t e_stypes, e_ttypes;
3497
8.69k
  ebitmap_t e_tclasses;
3498
8.69k
  ebitmap_node_t *snode, *tnode, *cnode;
3499
8.69k
  filename_trans_rule_t *ftr;
3500
8.69k
  type_datum_t *typdatum;
3501
8.69k
  uint32_t otype;
3502
8.69k
  unsigned int c, s, t;
3503
8.69k
  int add, self, rc;
3504
3505
8.69k
  if (pass == 1) {
3506
    /* stype */
3507
10.8k
    while ((id = queue_remove(id_queue)))
3508
5.76k
      free(id);
3509
    /* ttype */
3510
27.0k
    while ((id = queue_remove(id_queue)))
3511
21.9k
      free(id);
3512
    /* tclass */
3513
11.7k
    while ((id = queue_remove(id_queue)))
3514
6.69k
      free(id);
3515
    /* otype */
3516
5.05k
    id = queue_remove(id_queue);
3517
5.05k
    free(id);
3518
    /* name */
3519
5.05k
    id = queue_remove(id_queue);
3520
5.05k
    free(id);
3521
5.05k
    return 0;
3522
5.05k
  }
3523
3524
3.64k
  type_set_init(&stypes);
3525
3.64k
  type_set_init(&ttypes);
3526
3.64k
  ebitmap_init(&e_stypes);
3527
3.64k
  ebitmap_init(&e_ttypes);
3528
3.64k
  ebitmap_init(&e_tclasses);
3529
3530
3.64k
  add = 1;
3531
7.55k
  while ((id = queue_remove(id_queue))) {
3532
3.95k
    if (set_types(&stypes, id, &add, 0))
3533
39
      goto bad;
3534
3.95k
  }
3535
3536
3.60k
  self = 0;
3537
3.60k
  add = 1;
3538
23.1k
  while ((id = queue_remove(id_queue))) {
3539
19.6k
    if (strcmp(id, "self") == 0) {
3540
242
      free(id);
3541
242
      if (add == 0) {
3542
1
        yyerror("-self is not supported");
3543
1
        goto bad;
3544
1
      }
3545
241
      self = 1;
3546
241
      continue;
3547
242
    }
3548
19.3k
    if (set_types(&ttypes, id, &add, 0))
3549
111
      goto bad;
3550
19.3k
  }
3551
3552
3.49k
  if (read_classes(&e_tclasses))
3553
8
    goto bad;
3554
3555
3.48k
  id = (char *)queue_remove(id_queue);
3556
3.48k
  if (!id) {
3557
0
    yyerror("no otype in transition definition?");
3558
0
    goto bad;
3559
0
  }
3560
3.48k
  if (!is_id_in_scope(SYM_TYPES, id)) {
3561
1
    yyerror2("type %s is not within scope", id);
3562
1
    free(id);
3563
1
    goto bad;
3564
1
  }
3565
3.48k
  typdatum = hashtab_search(policydbp->p_types.table, id);
3566
3.48k
  if (!typdatum) {
3567
27
    yyerror2("unknown type %s used in transition definition", id);
3568
27
    free(id);
3569
27
    goto bad;
3570
27
  }
3571
3.45k
  free(id);
3572
3.45k
  otype = typdatum->s.value;
3573
3574
3.45k
  name = queue_remove(id_queue);
3575
3.45k
  if (!name) {
3576
0
    yyerror("no pathname specified in filename_trans definition?");
3577
0
    goto bad;
3578
0
  }
3579
3580
  /* We expand the class set into separate rules.  We expand the types
3581
   * just to make sure there are not duplicates.  They will get turned
3582
   * into separate rules later */
3583
3.45k
  if (type_set_expand(&stypes, &e_stypes, policydbp, 1))
3584
0
    goto bad;
3585
3586
3.45k
  if (type_set_expand(&ttypes, &e_ttypes, policydbp, 1))
3587
0
    goto bad;
3588
3589
218k
  ebitmap_for_each_positive_bit(&e_tclasses, cnode, c) {
3590
114k
    ebitmap_for_each_positive_bit(&e_stypes, snode, s) {
3591
43.8k
      ebitmap_for_each_positive_bit(&e_ttypes, tnode, t) {
3592
903
        rc = policydb_filetrans_insert(policydbp, s + 1,
3593
903
                     t + 1, c + 1,
3594
903
                     name, NULL,
3595
903
                     otype, NULL);
3596
903
        if (rc != SEPOL_OK) {
3597
23
          if (rc == SEPOL_EEXIST) {
3598
23
            yyerror2(
3599
23
              "duplicate filename transition for: filename_trans %s %s %s:%s",
3600
23
              name,
3601
23
              policydbp->p_type_val_to_name
3602
23
                [s],
3603
23
              policydbp->p_type_val_to_name
3604
23
                [t],
3605
23
              policydbp->p_class_val_to_name
3606
23
                [c]);
3607
23
            goto bad;
3608
23
          }
3609
0
          yyerror("out of memory");
3610
0
          goto bad;
3611
23
        }
3612
903
      }
3613
1.86k
      if (self) {
3614
59
        rc = policydb_filetrans_insert(policydbp, s + 1,
3615
59
                     s + 1, c + 1,
3616
59
                     name, NULL,
3617
59
                     otype, NULL);
3618
59
        if (rc != SEPOL_OK) {
3619
12
          if (rc == SEPOL_EEXIST) {
3620
12
            yyerror2(
3621
12
              "duplicate filename transition for: filename_trans %s %s %s:%s",
3622
12
              name,
3623
12
              policydbp->p_type_val_to_name
3624
12
                [s],
3625
12
              policydbp->p_type_val_to_name
3626
12
                [s],
3627
12
              policydbp->p_class_val_to_name
3628
12
                [c]);
3629
12
            goto bad;
3630
12
          }
3631
0
          yyerror("out of memory");
3632
0
          goto bad;
3633
12
        }
3634
59
      }
3635
1.86k
    }
3636
3637
    /* Now add the real rule since we didn't find any duplicates */
3638
3.42k
    ftr = malloc(sizeof(*ftr));
3639
3.42k
    if (!ftr) {
3640
0
      yyerror("out of memory");
3641
0
      goto bad;
3642
0
    }
3643
3.42k
    filename_trans_rule_init(ftr);
3644
3.42k
    append_filename_trans(ftr);
3645
3646
3.42k
    ftr->name = strdup(name);
3647
3.42k
    if (!ftr->name) {
3648
0
      yyerror("out of memory");
3649
0
      goto bad;
3650
0
    }
3651
3.42k
    if (type_set_cpy(&ftr->stypes, &stypes)) {
3652
0
      yyerror("out of memory");
3653
0
      goto bad;
3654
0
    }
3655
3.42k
    if (type_set_cpy(&ftr->ttypes, &ttypes)) {
3656
0
      yyerror("out of memory");
3657
0
      goto bad;
3658
0
    }
3659
3.42k
    ftr->tclass = c + 1;
3660
3.42k
    ftr->otype = otype;
3661
3.42k
    ftr->flags = self ? RULE_SELF : 0;
3662
3.42k
  }
3663
3664
3.42k
  free(name);
3665
3.42k
  ebitmap_destroy(&e_stypes);
3666
3.42k
  ebitmap_destroy(&e_ttypes);
3667
3.42k
  ebitmap_destroy(&e_tclasses);
3668
3.42k
  type_set_destroy(&stypes);
3669
3.42k
  type_set_destroy(&ttypes);
3670
3671
3.42k
  return 0;
3672
3673
222
bad:
3674
222
  free(name);
3675
222
  ebitmap_destroy(&e_stypes);
3676
222
  ebitmap_destroy(&e_ttypes);
3677
222
  ebitmap_destroy(&e_tclasses);
3678
222
  type_set_destroy(&stypes);
3679
222
  type_set_destroy(&ttypes);
3680
222
  return -1;
3681
3.45k
}
3682
3683
static constraint_expr_t *constraint_expr_clone(const constraint_expr_t *expr)
3684
0
{
3685
0
  constraint_expr_t *h = NULL, *l = NULL, *newe;
3686
0
  const constraint_expr_t *e;
3687
0
  for (e = expr; e; e = e->next) {
3688
0
    newe = malloc(sizeof(*newe));
3689
0
    if (!newe)
3690
0
      goto oom;
3691
0
    if (constraint_expr_init(newe) == -1) {
3692
0
      free(newe);
3693
0
      goto oom;
3694
0
    }
3695
0
    if (l)
3696
0
      l->next = newe;
3697
0
    else
3698
0
      h = newe;
3699
0
    l = newe;
3700
0
    newe->expr_type = e->expr_type;
3701
0
    newe->attr = e->attr;
3702
0
    newe->op = e->op;
3703
0
    if (newe->expr_type == CEXPR_NAMES) {
3704
0
      if (newe->attr & CEXPR_TYPE) {
3705
0
        if (type_set_cpy(newe->type_names,
3706
0
             e->type_names))
3707
0
          goto oom;
3708
0
      } else {
3709
0
        if (ebitmap_cpy(&newe->names, &e->names))
3710
0
          goto oom;
3711
0
      }
3712
0
    }
3713
0
  }
3714
3715
0
  return h;
3716
0
oom:
3717
0
  constraint_expr_destroy(h);
3718
0
  return NULL;
3719
0
}
3720
3721
int define_constraint(constraint_expr_t *expr)
3722
25.5k
{
3723
25.5k
  struct constraint_node *node;
3724
25.5k
  char *id;
3725
25.5k
  class_datum_t *cladatum;
3726
25.5k
  perm_datum_t *perdatum;
3727
25.5k
  ebitmap_t classmap;
3728
25.5k
  ebitmap_node_t *enode;
3729
25.5k
  constraint_expr_t *e;
3730
25.5k
  unsigned int i;
3731
25.5k
  int depth;
3732
25.5k
  unsigned char useexpr = 1;
3733
3734
25.5k
  if (pass == 1) {
3735
27.4k
    while ((id = queue_remove(id_queue)))
3736
13.8k
      free(id);
3737
29.3k
    while ((id = queue_remove(id_queue)))
3738
15.8k
      free(id);
3739
13.5k
    return 0;
3740
13.5k
  }
3741
3742
12.0k
  ebitmap_init(&classmap);
3743
3744
12.0k
  depth = -1;
3745
1.85M
  for (e = expr; e; e = e->next) {
3746
1.84M
    switch (e->expr_type) {
3747
1.50M
    case CEXPR_NOT:
3748
1.50M
      if (depth < 0) {
3749
0
        yyerror("illegal constraint expression");
3750
0
        goto bad;
3751
0
      }
3752
1.50M
      break;
3753
1.50M
    case CEXPR_AND:
3754
161k
    case CEXPR_OR:
3755
161k
      if (depth < 1) {
3756
0
        yyerror("illegal constraint expression");
3757
0
        goto bad;
3758
0
      }
3759
161k
      depth--;
3760
161k
      break;
3761
146k
    case CEXPR_ATTR:
3762
173k
    case CEXPR_NAMES:
3763
173k
      if (e->attr & CEXPR_XTARGET) {
3764
65
        yyerror("illegal constraint expression");
3765
65
        goto bad; /* only for validatetrans rules */
3766
65
      }
3767
173k
      if (depth == (CEXPR_MAXDEPTH - 1)) {
3768
1
        yyerror("constraint expression is too deep");
3769
1
        goto bad;
3770
1
      }
3771
173k
      depth++;
3772
173k
      break;
3773
0
    default:
3774
0
      yyerror("illegal constraint expression");
3775
0
      goto bad;
3776
1.84M
    }
3777
1.84M
  }
3778
11.9k
  if (depth != 0) {
3779
0
    yyerror("illegal constraint expression");
3780
0
    goto bad;
3781
0
  }
3782
3783
23.7k
  while ((id = queue_remove(id_queue))) {
3784
11.9k
    if (!is_id_in_scope(SYM_CLASSES, id)) {
3785
0
      yyerror2("class %s is not within scope", id);
3786
0
      free(id);
3787
0
      goto bad;
3788
0
    }
3789
11.9k
    cladatum = (class_datum_t *)hashtab_search(
3790
11.9k
      policydbp->p_classes.table, (hashtab_key_t)id);
3791
11.9k
    if (!cladatum) {
3792
121
      yyerror2("class %s is not defined", id);
3793
121
      free(id);
3794
121
      goto bad;
3795
121
    }
3796
    /* a class listed twice gets a single constraint */
3797
11.8k
    if (ebitmap_get_bit(&classmap, cladatum->s.value - 1)) {
3798
0
      free(id);
3799
0
      continue;
3800
0
    }
3801
11.8k
    if (ebitmap_set_bit(&classmap, cladatum->s.value - 1, TRUE)) {
3802
0
      yyerror("out of memory");
3803
0
      free(id);
3804
0
      goto bad;
3805
0
    }
3806
11.8k
    node = calloc(1, sizeof(struct constraint_node));
3807
11.8k
    if (!node) {
3808
0
      yyerror("out of memory");
3809
0
      free(node);
3810
0
      goto bad;
3811
0
    }
3812
11.8k
    if (useexpr) {
3813
11.8k
      node->expr = expr;
3814
11.8k
      useexpr = 0;
3815
11.8k
    } else {
3816
0
      node->expr = constraint_expr_clone(expr);
3817
0
    }
3818
11.8k
    if (!node->expr) {
3819
0
      yyerror("out of memory");
3820
0
      free(node);
3821
0
      goto bad;
3822
0
    }
3823
11.8k
    node->permissions = 0;
3824
3825
11.8k
    node->next = cladatum->constraints;
3826
11.8k
    cladatum->constraints = node;
3827
3828
11.8k
    free(id);
3829
11.8k
  }
3830
3831
25.1k
  while ((id = queue_remove(id_queue))) {
3832
855k
    ebitmap_for_each_positive_bit(&classmap, enode, i) {
3833
13.4k
      cladatum = policydbp->class_val_to_struct[i];
3834
13.4k
      node = cladatum->constraints;
3835
3836
13.4k
      if (strcmp(id, "*") == 0) {
3837
100
        node->permissions = PERMISSION_MASK(
3838
100
          cladatum->permissions.nprim);
3839
100
        continue;
3840
100
      }
3841
3842
13.3k
      if (strcmp(id, "~") == 0) {
3843
1.64k
        node->permissions =
3844
1.64k
          ~node->permissions &
3845
1.64k
          PERMISSION_MASK(
3846
1.64k
            cladatum->permissions.nprim);
3847
1.64k
        if (node->permissions == 0) {
3848
267
          yywarn("omitting constraint with no permission set");
3849
267
          cladatum->constraints = node->next;
3850
267
          constraint_expr_destroy(node->expr);
3851
267
          free(node);
3852
267
        }
3853
1.64k
        continue;
3854
1.64k
      }
3855
3856
11.7k
      perdatum = (perm_datum_t *)hashtab_search(
3857
11.7k
        cladatum->permissions.table, (hashtab_key_t)id);
3858
11.7k
      if (!perdatum) {
3859
97
        if (cladatum->comdatum) {
3860
0
          perdatum =
3861
0
            (perm_datum_t *)hashtab_search(
3862
0
              cladatum->comdatum
3863
0
                ->permissions
3864
0
                .table,
3865
0
              (hashtab_key_t)id);
3866
0
        }
3867
97
        if (!perdatum) {
3868
97
          yyerror2("permission %s is not"
3869
97
             " defined for class %s",
3870
97
             id,
3871
97
             policydbp->p_class_val_to_name
3872
97
               [i]);
3873
97
          free(id);
3874
97
          goto bad;
3875
97
        }
3876
97
      }
3877
11.6k
      node->permissions |=
3878
11.6k
        (UINT32_C(1) << (perdatum->s.value - 1));
3879
11.6k
    }
3880
13.3k
    free(id);
3881
13.3k
  }
3882
3883
11.7k
  ebitmap_destroy(&classmap);
3884
3885
11.7k
  return 0;
3886
3887
284
bad:
3888
284
  ebitmap_destroy(&classmap);
3889
284
  if (useexpr)
3890
179
    constraint_expr_destroy(expr);
3891
3892
284
  return -1;
3893
11.8k
}
3894
3895
int define_validatetrans(constraint_expr_t *expr)
3896
1.36k
{
3897
1.36k
  struct constraint_node *node;
3898
1.36k
  char *id;
3899
1.36k
  class_datum_t *cladatum;
3900
1.36k
  ebitmap_t classmap;
3901
1.36k
  constraint_expr_t *e;
3902
1.36k
  int depth;
3903
1.36k
  unsigned char useexpr = 1;
3904
3905
1.36k
  if (pass == 1) {
3906
2.71k
    while ((id = queue_remove(id_queue)))
3907
1.53k
      free(id);
3908
1.17k
    return 0;
3909
1.17k
  }
3910
3911
192
  ebitmap_init(&classmap);
3912
3913
192
  depth = -1;
3914
42.3k
  for (e = expr; e; e = e->next) {
3915
42.2k
    switch (e->expr_type) {
3916
21.9k
    case CEXPR_NOT:
3917
21.9k
      if (depth < 0) {
3918
0
        yyerror("illegal validatetrans expression");
3919
0
        goto bad;
3920
0
      }
3921
21.9k
      break;
3922
21.9k
    case CEXPR_AND:
3923
10.0k
    case CEXPR_OR:
3924
10.0k
      if (depth < 1) {
3925
0
        yyerror("illegal validatetrans expression");
3926
0
        goto bad;
3927
0
      }
3928
10.0k
      depth--;
3929
10.0k
      break;
3930
1.11k
    case CEXPR_ATTR:
3931
10.2k
    case CEXPR_NAMES:
3932
10.2k
      if (depth == (CEXPR_MAXDEPTH - 1)) {
3933
1
        yyerror("validatetrans expression is too deep");
3934
1
        goto bad;
3935
1
      }
3936
10.2k
      depth++;
3937
10.2k
      break;
3938
0
    default:
3939
0
      yyerror("illegal validatetrans expression");
3940
0
      goto bad;
3941
42.2k
    }
3942
42.2k
  }
3943
191
  if (depth != 0) {
3944
0
    yyerror("illegal validatetrans expression");
3945
0
    goto bad;
3946
0
  }
3947
3948
295
  while ((id = queue_remove(id_queue))) {
3949
195
    if (!is_id_in_scope(SYM_CLASSES, id)) {
3950
0
      yyerror2("class %s is not within scope", id);
3951
0
      free(id);
3952
0
      goto bad;
3953
0
    }
3954
195
    cladatum = (class_datum_t *)hashtab_search(
3955
195
      policydbp->p_classes.table, (hashtab_key_t)id);
3956
195
    if (!cladatum) {
3957
91
      yyerror2("class %s is not defined", id);
3958
91
      free(id);
3959
91
      goto bad;
3960
91
    }
3961
    /* a class listed twice gets a single validatetrans rule */
3962
104
    if (ebitmap_get_bit(&classmap, cladatum->s.value - 1)) {
3963
0
      free(id);
3964
0
      continue;
3965
0
    }
3966
104
    if (ebitmap_set_bit(&classmap, (cladatum->s.value - 1), TRUE)) {
3967
0
      yyerror("out of memory");
3968
0
      free(id);
3969
0
      goto bad;
3970
0
    }
3971
3972
104
    node = calloc(1, sizeof(struct constraint_node));
3973
104
    if (!node) {
3974
0
      yyerror("out of memory");
3975
0
      free(id);
3976
0
      goto bad;
3977
0
    }
3978
104
    if (useexpr) {
3979
104
      node->expr = expr;
3980
104
      useexpr = 0;
3981
104
    } else {
3982
0
      node->expr = constraint_expr_clone(expr);
3983
0
    }
3984
104
    node->permissions = 0;
3985
3986
104
    node->next = cladatum->validatetrans;
3987
104
    cladatum->validatetrans = node;
3988
3989
104
    free(id);
3990
104
  }
3991
3992
100
  ebitmap_destroy(&classmap);
3993
3994
100
  return 0;
3995
3996
92
bad:
3997
92
  ebitmap_destroy(&classmap);
3998
92
  if (useexpr)
3999
88
    constraint_expr_destroy(expr);
4000
4001
92
  return -1;
4002
191
}
4003
4004
uintptr_t define_cexpr(uint32_t expr_type, uintptr_t arg1, uintptr_t arg2)
4005
3.94M
{
4006
3.94M
  struct constraint_expr *expr, *e1 = NULL, *e2;
4007
3.94M
  user_datum_t *user;
4008
3.94M
  role_datum_t *role;
4009
3.94M
  ebitmap_t negset;
4010
3.94M
  char *id;
4011
3.94M
  uint32_t val;
4012
3.94M
  int add = 1;
4013
4014
3.94M
  if (pass == 1) {
4015
1.98M
    if (expr_type == CEXPR_NAMES) {
4016
116k
      while ((id = queue_remove(id_queue)))
4017
58.8k
        free(id);
4018
57.4k
    }
4019
1.98M
    return 1; /* any non-NULL value */
4020
1.98M
  }
4021
4022
1.95M
  if ((expr = malloc(sizeof(*expr))) == NULL ||
4023
1.95M
      constraint_expr_init(expr) == -1) {
4024
0
    yyerror("out of memory");
4025
0
    free(expr);
4026
0
    return 0;
4027
0
  }
4028
1.95M
  expr->expr_type = expr_type;
4029
4030
1.95M
  switch (expr_type) {
4031
1.56M
  case CEXPR_NOT:
4032
1.56M
    e1 = NULL;
4033
1.56M
    e2 = (struct constraint_expr *)arg1;
4034
68.8M
    while (e2) {
4035
67.3M
      e1 = e2;
4036
67.3M
      e2 = e2->next;
4037
67.3M
    }
4038
1.56M
    if (!e1 || e1->next) {
4039
0
      yyerror("illegal constraint expression");
4040
0
      constraint_expr_destroy(expr);
4041
0
      return 0;
4042
0
    }
4043
1.56M
    e1->next = expr;
4044
1.56M
    return arg1;
4045
142k
  case CEXPR_AND:
4046
190k
  case CEXPR_OR:
4047
190k
    e1 = NULL;
4048
190k
    e2 = (struct constraint_expr *)arg1;
4049
506M
    while (e2) {
4050
506M
      e1 = e2;
4051
506M
      e2 = e2->next;
4052
506M
    }
4053
190k
    if (!e1 || e1->next) {
4054
0
      yyerror("illegal constraint expression");
4055
0
      constraint_expr_destroy(expr);
4056
0
      return 0;
4057
0
    }
4058
190k
    e1->next = (struct constraint_expr *)arg2;
4059
4060
190k
    e1 = NULL;
4061
190k
    e2 = (struct constraint_expr *)arg2;
4062
2.64M
    while (e2) {
4063
2.45M
      e1 = e2;
4064
2.45M
      e2 = e2->next;
4065
2.45M
    }
4066
190k
    if (!e1 || e1->next) {
4067
0
      yyerror("illegal constraint expression");
4068
0
      constraint_expr_destroy(expr);
4069
0
      return 0;
4070
0
    }
4071
190k
    e1->next = expr;
4072
190k
    return arg1;
4073
150k
  case CEXPR_ATTR:
4074
150k
    if (!mlspol && (arg1 >= CEXPR_L1L2)) {
4075
6
      yyerror("MLS constraint expression "
4076
6
        "in non-MLS configuration");
4077
6
      constraint_expr_destroy(expr);
4078
6
      return 0;
4079
6
    }
4080
150k
    expr->attr = arg1;
4081
150k
    expr->op = arg2;
4082
150k
    return (uintptr_t)expr;
4083
52.3k
  case CEXPR_NAMES:
4084
52.3k
    add = 1;
4085
52.3k
    expr->attr = arg1;
4086
52.3k
    expr->op = arg2;
4087
52.3k
    ebitmap_init(&negset);
4088
104k
    while ((id = (char *)queue_remove(id_queue))) {
4089
52.3k
      if (expr->attr & CEXPR_USER) {
4090
22.1k
        if (!is_id_in_scope(SYM_USERS, id)) {
4091
0
          yyerror2("user %s is not within scope",
4092
0
             id);
4093
0
          free(id);
4094
0
          constraint_expr_destroy(expr);
4095
0
          return 0;
4096
0
        }
4097
22.1k
        user = (user_datum_t *)hashtab_search(
4098
22.1k
          policydbp->p_users.table,
4099
22.1k
          (hashtab_key_t)id);
4100
22.1k
        if (!user) {
4101
12
          yyerror2("unknown user %s", id);
4102
12
          free(id);
4103
12
          constraint_expr_destroy(expr);
4104
12
          return 0;
4105
12
        }
4106
22.1k
        val = user->s.value;
4107
30.1k
      } else if (expr->attr & CEXPR_ROLE) {
4108
26.7k
        if (!is_id_in_scope(SYM_ROLES, id)) {
4109
1
          yyerror2("role %s is not within scope",
4110
1
             id);
4111
1
          constraint_expr_destroy(expr);
4112
1
          free(id);
4113
1
          return 0;
4114
1
        }
4115
26.7k
        role = (role_datum_t *)hashtab_search(
4116
26.7k
          policydbp->p_roles.table,
4117
26.7k
          (hashtab_key_t)id);
4118
26.7k
        if (!role) {
4119
17
          yyerror2("unknown role %s", id);
4120
17
          constraint_expr_destroy(expr);
4121
17
          free(id);
4122
17
          return 0;
4123
17
        }
4124
26.7k
        val = role->s.value;
4125
26.7k
      } else if (expr->attr & CEXPR_TYPE) {
4126
3.36k
        if (set_types(expr->type_names, id, &add, 0)) {
4127
33
          constraint_expr_destroy(expr);
4128
33
          return 0;
4129
33
        }
4130
3.33k
        continue;
4131
3.36k
      } else {
4132
0
        yyerror("invalid constraint expression");
4133
0
        constraint_expr_destroy(expr);
4134
0
        free(id);
4135
0
        return 0;
4136
0
      }
4137
48.9k
      if (ebitmap_set_bit(&expr->names, val - 1, TRUE)) {
4138
0
        yyerror("out of memory");
4139
0
        ebitmap_destroy(&expr->names);
4140
0
        free(id);
4141
0
        constraint_expr_destroy(expr);
4142
0
        return 0;
4143
0
      }
4144
48.9k
      free(id);
4145
48.9k
    }
4146
52.2k
    ebitmap_destroy(&negset);
4147
52.2k
    return (uintptr_t)expr;
4148
0
  default:
4149
0
    break;
4150
1.95M
  }
4151
4152
0
  yyerror("invalid constraint expression");
4153
0
  constraint_expr_destroy(expr);
4154
0
  return 0;
4155
1.95M
}
4156
4157
int define_conditional(cond_expr_t *expr, avrule_t *t_list, avrule_t *f_list)
4158
8.59k
{
4159
8.59k
  cond_expr_t *e;
4160
8.59k
  int depth, booleans, tunables;
4161
8.59k
  cond_node_t cn, *cn_old;
4162
8.59k
  const cond_bool_datum_t *bool_var;
4163
4164
  /* expression cannot be NULL */
4165
8.59k
  if (!expr) {
4166
374
    yyerror("illegal conditional expression");
4167
374
    return -1;
4168
374
  }
4169
8.21k
  if (!t_list) {
4170
2.64k
    if (!f_list) {
4171
      /* empty is fine, destroy expression and return */
4172
2.00k
      cond_expr_destroy(expr);
4173
2.00k
      return 0;
4174
2.00k
    }
4175
    /* Invert */
4176
642
    t_list = f_list;
4177
642
    f_list = NULL;
4178
642
    expr = define_cond_expr(COND_NOT, expr, NULL);
4179
642
    if (!expr) {
4180
0
      yyerror("unable to invert conditional expression");
4181
0
      return -1;
4182
0
    }
4183
642
  }
4184
4185
  /* verify expression */
4186
6.21k
  depth = -1;
4187
6.21k
  booleans = 0;
4188
6.21k
  tunables = 0;
4189
144k
  for (e = expr; e; e = e->next) {
4190
137k
    switch (e->expr_type) {
4191
68.9k
    case COND_NOT:
4192
68.9k
      if (depth < 0) {
4193
0
        yyerror("illegal conditional expression; Bad NOT");
4194
0
        return -1;
4195
0
      }
4196
68.9k
      break;
4197
68.9k
    case COND_AND:
4198
1.43k
    case COND_OR:
4199
4.08k
    case COND_XOR:
4200
28.2k
    case COND_EQ:
4201
31.2k
    case COND_NEQ:
4202
31.2k
      if (depth < 1) {
4203
0
        yyerror("illegal conditional expression; Bad binary op");
4204
0
        return -1;
4205
0
      }
4206
31.2k
      depth--;
4207
31.2k
      break;
4208
37.5k
    case COND_BOOL:
4209
37.5k
      if (depth == (COND_EXPR_MAXDEPTH - 1)) {
4210
2
        yyerror("conditional expression is like totally too deep");
4211
2
        return -1;
4212
2
      }
4213
37.5k
      depth++;
4214
4215
37.5k
      bool_var =
4216
37.5k
        policydbp->bool_val_to_struct[e->boolean - 1];
4217
37.5k
      if (bool_var->flags & COND_BOOL_FLAGS_TUNABLE) {
4218
309
        tunables = 1;
4219
37.2k
      } else {
4220
37.2k
        booleans = 1;
4221
37.2k
      }
4222
4223
37.5k
      break;
4224
0
    default:
4225
0
      yyerror("illegal conditional expression");
4226
0
      return -1;
4227
137k
    }
4228
137k
  }
4229
6.21k
  if (depth != 0) {
4230
0
    yyerror("illegal conditional expression");
4231
0
    return -1;
4232
0
  }
4233
6.21k
  if (booleans && tunables) {
4234
7
    yyerror("illegal conditional expression; Contains boolean and tunable");
4235
7
    return -1;
4236
7
  }
4237
4238
  /*  use tmp conditional node to partially build new node */
4239
6.20k
  memset(&cn, 0, sizeof(cn));
4240
6.20k
  cn.expr = expr;
4241
6.20k
  cn.avtrue_list = t_list;
4242
6.20k
  cn.avfalse_list = f_list;
4243
4244
  /* normalize/precompute expression */
4245
6.20k
  if (cond_normalize_expr(policydbp, &cn) < 0) {
4246
0
    yyerror("problem normalizing conditional expression");
4247
0
    return -1;
4248
0
  }
4249
4250
  /* get the existing conditional node, or create a new one */
4251
6.20k
  cn_old = get_current_cond_list(&cn);
4252
6.20k
  if (!cn_old) {
4253
0
    return -1;
4254
0
  }
4255
4256
6.20k
  append_cond_list(&cn);
4257
4258
  /* note that there is no check here for duplicate rules, nor
4259
   * check that rule already exists in base -- that will be
4260
   * handled during conditional expansion, in expand.c */
4261
4262
6.20k
  cn.avtrue_list = NULL;
4263
6.20k
  cn.avfalse_list = NULL;
4264
6.20k
  cond_node_destroy(&cn);
4265
4266
6.20k
  return 0;
4267
6.20k
}
4268
4269
cond_expr_t *define_cond_expr(uint32_t expr_type, void *arg1, void *arg2)
4270
300k
{
4271
300k
  struct cond_expr *expr, *e1 = NULL, *e2;
4272
300k
  cond_bool_datum_t *bool_var;
4273
300k
  char *id;
4274
4275
  /* expressions are handled in the second pass */
4276
300k
  if (pass == 1) {
4277
157k
    if (expr_type == COND_BOOL) {
4278
92.9k
      while ((id = queue_remove(id_queue))) {
4279
46.5k
        free(id);
4280
46.5k
      }
4281
46.4k
    }
4282
157k
    return (cond_expr_t *)1; /* any non-NULL value */
4283
157k
  }
4284
4285
  /* create a new expression struct */
4286
142k
  expr = calloc(1, sizeof(struct cond_expr));
4287
142k
  if (!expr) {
4288
0
    yyerror("out of memory");
4289
0
    return NULL;
4290
0
  }
4291
142k
  expr->expr_type = expr_type;
4292
4293
  /* create the type asked for */
4294
142k
  switch (expr_type) {
4295
69.6k
  case COND_NOT:
4296
69.6k
    e1 = NULL;
4297
69.6k
    e2 = (struct cond_expr *)arg1;
4298
2.14M
    while (e2) {
4299
2.07M
      e1 = e2;
4300
2.07M
      e2 = e2->next;
4301
2.07M
    }
4302
69.6k
    if (!e1 || e1->next) {
4303
27
      yyerror("illegal conditional NOT expression");
4304
27
      free(expr);
4305
27
      return NULL;
4306
27
    }
4307
69.6k
    e1->next = expr;
4308
69.6k
    return (struct cond_expr *)arg1;
4309
360
  case COND_AND:
4310
1.47k
  case COND_OR:
4311
4.16k
  case COND_XOR:
4312
28.7k
  case COND_EQ:
4313
32.0k
  case COND_NEQ:
4314
32.0k
    e1 = NULL;
4315
32.0k
    e2 = (struct cond_expr *)arg1;
4316
1.65M
    while (e2) {
4317
1.62M
      e1 = e2;
4318
1.62M
      e2 = e2->next;
4319
1.62M
    }
4320
32.0k
    if (!e1 || e1->next) {
4321
13
      yyerror("illegal left side of conditional binary op expression");
4322
13
      free(expr);
4323
13
      return NULL;
4324
13
    }
4325
31.9k
    e1->next = (struct cond_expr *)arg2;
4326
4327
31.9k
    e1 = NULL;
4328
31.9k
    e2 = (struct cond_expr *)arg2;
4329
313k
    while (e2) {
4330
281k
      e1 = e2;
4331
281k
      e2 = e2->next;
4332
281k
    }
4333
31.9k
    if (!e1 || e1->next) {
4334
31
      yyerror("illegal right side of conditional binary op expression");
4335
31
      cond_expr_destroy(arg1);
4336
31
      free(expr);
4337
31
      return NULL;
4338
31
    }
4339
31.9k
    e1->next = expr;
4340
31.9k
    return (struct cond_expr *)arg1;
4341
41.2k
  case COND_BOOL:
4342
41.2k
    id = (char *)queue_remove(id_queue);
4343
41.2k
    if (!id) {
4344
0
      yyerror("bad conditional; expected boolean id");
4345
0
      free(id);
4346
0
      free(expr);
4347
0
      return NULL;
4348
0
    }
4349
41.2k
    if (!is_id_in_scope(SYM_BOOLS, id)) {
4350
21
      yyerror2("boolean %s is not within scope", id);
4351
21
      free(id);
4352
21
      free(expr);
4353
21
      return NULL;
4354
21
    }
4355
41.1k
    bool_var = (cond_bool_datum_t *)hashtab_search(
4356
41.1k
      policydbp->p_bools.table, (hashtab_key_t)id);
4357
41.1k
    if (!bool_var) {
4358
917
      yyerror2("unknown boolean %s in conditional expression",
4359
917
         id);
4360
917
      free(expr);
4361
917
      free(id);
4362
917
      return NULL;
4363
917
    }
4364
40.2k
    expr->boolean = bool_var->s.value;
4365
40.2k
    free(id);
4366
40.2k
    return expr;
4367
0
  default:
4368
0
    yyerror("illegal conditional expression");
4369
0
    free(expr);
4370
0
    return NULL;
4371
142k
  }
4372
142k
}
4373
4374
static int set_user_roles(role_set_t *set, char *id)
4375
10.1k
{
4376
10.1k
  role_datum_t *r;
4377
4378
10.1k
  if (strcmp(id, "*") == 0) {
4379
1
    free(id);
4380
1
    yyerror("* is not allowed in user declarations");
4381
1
    return -1;
4382
1
  }
4383
4384
10.1k
  if (strcmp(id, "~") == 0) {
4385
1
    free(id);
4386
1
    yyerror("~ is not allowed in user declarations");
4387
1
    return -1;
4388
1
  }
4389
4390
10.1k
  if (!is_id_in_scope(SYM_ROLES, id)) {
4391
17
    yyerror2("role %s is not within scope", id);
4392
17
    free(id);
4393
17
    return -1;
4394
17
  }
4395
10.1k
  r = hashtab_search(policydbp->p_roles.table, id);
4396
10.1k
  if (!r) {
4397
1.14k
    yyerror2("unknown role %s", id);
4398
1.14k
    free(id);
4399
1.14k
    return -1;
4400
1.14k
  }
4401
4402
8.99k
  free(id);
4403
8.99k
  if (ebitmap_set_bit(&set->roles, r->s.value - 1, TRUE))
4404
0
    goto oom;
4405
8.99k
  return 0;
4406
0
oom:
4407
0
  yyerror("out of memory");
4408
0
  return -1;
4409
8.99k
}
4410
4411
static int parse_categories(char *id, level_datum_t *levdatum, ebitmap_t *cats)
4412
511
{
4413
511
  cat_datum_t *cdatum;
4414
511
  uint32_t range_start, range_end, i;
4415
4416
511
  if (id_has_dot(id)) {
4417
204
    char *id_start = id;
4418
204
    char *id_end = strchr(id, '.');
4419
4420
204
    *(id_end++) = '\0';
4421
4422
204
    cdatum = (cat_datum_t *)hashtab_search(policydbp->p_cats.table,
4423
204
                   (hashtab_key_t)id_start);
4424
204
    if (!cdatum) {
4425
2
      yyerror2("unknown category %s", id_start);
4426
2
      return -1;
4427
2
    }
4428
202
    range_start = cdatum->s.value - 1;
4429
202
    cdatum = (cat_datum_t *)hashtab_search(policydbp->p_cats.table,
4430
202
                   (hashtab_key_t)id_end);
4431
202
    if (!cdatum) {
4432
2
      yyerror2("unknown category %s", id_end);
4433
2
      return -1;
4434
2
    }
4435
200
    range_end = cdatum->s.value - 1;
4436
4437
200
    if (range_end < range_start) {
4438
0
      yyerror2("category range %d-%d is invalid", range_start,
4439
0
         range_end);
4440
0
      return -1;
4441
0
    }
4442
307
  } else {
4443
307
    cdatum = (cat_datum_t *)hashtab_search(policydbp->p_cats.table,
4444
307
                   (hashtab_key_t)id);
4445
307
    if (!cdatum) {
4446
10
      yyerror2("unknown category %s", id);
4447
10
      return -1;
4448
10
    }
4449
297
    range_start = range_end = cdatum->s.value - 1;
4450
297
  }
4451
4452
993
  for (i = range_start; i <= range_end; i++) {
4453
497
    if (!ebitmap_get_bit(&levdatum->level->cat, i)) {
4454
1
      uint32_t level_value = levdatum->level->sens - 1;
4455
1
      policydb_index_others(NULL, policydbp, 0);
4456
1
      yyerror2("category %s can not be associated "
4457
1
         "with level %s",
4458
1
         policydbp->p_cat_val_to_name[i],
4459
1
         policydbp->p_sens_val_to_name[level_value]);
4460
1
      return -1;
4461
1
    }
4462
496
    if (ebitmap_set_bit(cats, i, TRUE)) {
4463
0
      yyerror("out of memory");
4464
0
      return -1;
4465
0
    }
4466
496
  }
4467
4468
496
  return 0;
4469
497
}
4470
4471
static int mls_semantic_cats_merge(mls_semantic_cat_t **dst,
4472
           const mls_semantic_cat_t *src)
4473
7.14k
{
4474
7.14k
  mls_semantic_cat_t *new;
4475
4476
34.0k
  while (src) {
4477
26.8k
    new = malloc(sizeof(mls_semantic_cat_t));
4478
26.8k
    if (!new)
4479
0
      return -1;
4480
4481
26.8k
    mls_semantic_cat_init(new);
4482
26.8k
    new->low = src->low;
4483
26.8k
    new->high = src->high;
4484
26.8k
    new->next = *dst;
4485
26.8k
    *dst = new;
4486
4487
26.8k
    src = src->next;
4488
26.8k
  }
4489
4490
7.14k
  return 0;
4491
7.14k
}
4492
4493
static int mls_add_or_check_level(mls_semantic_level_t *dst,
4494
          const mls_semantic_level_t *src)
4495
11.6k
{
4496
11.6k
  if (!dst->sens) {
4497
4.49k
    if (mls_semantic_level_cpy(dst, src) < 0) {
4498
0
      yyerror("out of memory");
4499
0
      return -1;
4500
0
    }
4501
7.14k
  } else {
4502
7.14k
    if (dst->sens != src->sens) {
4503
3
      return -1;
4504
3
    }
4505
    /* Duplicate cats won't cause problems, but different cats will
4506
     * result in an error during expansion */
4507
7.14k
    if (mls_semantic_cats_merge(&dst->cat, src->cat) < 0) {
4508
0
      yyerror("out of memory");
4509
0
      return -1;
4510
0
    }
4511
7.14k
  }
4512
4513
11.6k
  return 0;
4514
11.6k
}
4515
4516
static int parse_semantic_categories(char *id,
4517
             level_datum_t *levdatum
4518
             __attribute__((unused)),
4519
             mls_semantic_cat_t **cats)
4520
7.74k
{
4521
7.74k
  cat_datum_t *cdatum;
4522
7.74k
  mls_semantic_cat_t *newcat;
4523
7.74k
  unsigned int range_start, range_end;
4524
4525
7.74k
  if (id_has_dot(id)) {
4526
129
    char *id_start = id;
4527
129
    char *id_end = strchr(id, '.');
4528
4529
129
    *(id_end++) = '\0';
4530
4531
129
    cdatum = (cat_datum_t *)hashtab_search(policydbp->p_cats.table,
4532
129
                   (hashtab_key_t)id_start);
4533
129
    if (!cdatum) {
4534
3
      yyerror2("unknown category %s", id_start);
4535
3
      return -1;
4536
3
    }
4537
126
    range_start = cdatum->s.value;
4538
4539
126
    cdatum = (cat_datum_t *)hashtab_search(policydbp->p_cats.table,
4540
126
                   (hashtab_key_t)id_end);
4541
126
    if (!cdatum) {
4542
6
      yyerror2("unknown category %s", id_end);
4543
6
      return -1;
4544
6
    }
4545
120
    range_end = cdatum->s.value;
4546
7.62k
  } else {
4547
7.62k
    cdatum = (cat_datum_t *)hashtab_search(policydbp->p_cats.table,
4548
7.62k
                   (hashtab_key_t)id);
4549
7.62k
    if (!cdatum) {
4550
16
      yyerror2("unknown category %s", id);
4551
16
      return -1;
4552
16
    }
4553
7.60k
    range_start = range_end = cdatum->s.value;
4554
7.60k
  }
4555
4556
7.72k
  newcat = malloc(sizeof(mls_semantic_cat_t));
4557
7.72k
  if (!newcat) {
4558
0
    yyerror("out of memory");
4559
0
    return -1;
4560
0
  }
4561
4562
7.72k
  mls_semantic_cat_init(newcat);
4563
7.72k
  newcat->next = *cats;
4564
7.72k
  newcat->low = range_start;
4565
7.72k
  newcat->high = range_end;
4566
4567
7.72k
  *cats = newcat;
4568
4569
7.72k
  return 0;
4570
7.72k
}
4571
4572
int define_user(void)
4573
37.8k
{
4574
37.8k
  const char *username;
4575
37.8k
  char *id;
4576
37.8k
  user_datum_t *usrdatum, *usr_global;
4577
37.8k
  level_datum_t *levdatum;
4578
37.8k
  int l;
4579
4580
37.8k
  if (pass == 1) {
4581
75.4k
    while ((id = queue_remove(id_queue)))
4582
47.8k
      free(id);
4583
27.6k
    if (mlspol) {
4584
21.7k
      while ((id = queue_remove(id_queue)))
4585
10.9k
        free(id);
4586
10.7k
      id = queue_remove(id_queue);
4587
10.7k
      free(id);
4588
15.3k
      for (l = 0; l < 2; l++) {
4589
21.9k
        while ((id = queue_remove(id_queue))) {
4590
6.64k
          free(id);
4591
6.64k
        }
4592
15.3k
        id = queue_remove(id_queue);
4593
15.3k
        if (!id)
4594
10.7k
          break;
4595
4.54k
        free(id);
4596
4.54k
      }
4597
10.7k
    }
4598
27.6k
    return 0;
4599
27.6k
  }
4600
4601
10.1k
  username = queue_head(id_queue);
4602
10.1k
  if (!username) {
4603
2
    yyerror("no user name");
4604
2
    return -1;
4605
2
  }
4606
4607
10.1k
  id = strdup(username);
4608
10.1k
  if (!id) {
4609
0
    yyerror("out of memory");
4610
0
    return -1;
4611
0
  }
4612
4613
10.1k
  if ((usrdatum = declare_user()) == NULL) {
4614
2
    free(id);
4615
2
    return -1;
4616
2
  }
4617
4618
10.1k
  usr_global =
4619
10.1k
    hashtab_search(policydbp->p_users.table, (hashtab_key_t)id);
4620
10.1k
  free(id);
4621
4622
19.1k
  while ((id = queue_remove(id_queue))) {
4623
10.1k
    if (set_user_roles(&usrdatum->roles, id))
4624
1.16k
      return -1;
4625
10.1k
  }
4626
4627
9.00k
  if (mlspol) {
4628
8.32k
    id = queue_remove(id_queue);
4629
8.32k
    if (!id) {
4630
7
      yyerror("no default level specified for user");
4631
7
      return -1;
4632
7
    }
4633
4634
8.32k
    levdatum = (level_datum_t *)hashtab_search(
4635
8.32k
      policydbp->p_levels.table, (hashtab_key_t)id);
4636
8.32k
    if (!levdatum) {
4637
10
      yyerror2("unknown sensitivity %s used in user"
4638
10
         " level definition",
4639
10
         id);
4640
10
      free(id);
4641
10
      return -1;
4642
10
    }
4643
8.31k
    free(id);
4644
4645
8.31k
    usrdatum->dfltlevel.sens = levdatum->level->sens;
4646
4647
10.1k
    while ((id = queue_remove(id_queue))) {
4648
      /* This will add to any already existing categories */
4649
1.81k
      if (parse_semantic_categories(
4650
1.81k
            id, levdatum, &usrdatum->dfltlevel.cat)) {
4651
13
        free(id);
4652
13
        return -1;
4653
13
      }
4654
1.80k
      free(id);
4655
1.80k
    }
4656
4657
8.29k
    id = queue_remove(id_queue);
4658
4659
12.4k
    for (l = 0; l < 2; l++) {
4660
12.4k
      levdatum = (level_datum_t *)hashtab_search(
4661
12.4k
        policydbp->p_levels.table, (hashtab_key_t)id);
4662
12.4k
      if (!levdatum) {
4663
14
        yyerror2("unknown sensitivity %s used in user"
4664
14
           " range definition",
4665
14
           id);
4666
14
        free(id);
4667
14
        return -1;
4668
14
      }
4669
12.4k
      free(id);
4670
4671
12.4k
      usrdatum->range.level[l].sens = levdatum->level->sens;
4672
4673
18.3k
      while ((id = queue_remove(id_queue))) {
4674
        /* This will add to any already existing categories */
4675
5.93k
        if (parse_semantic_categories(
4676
5.93k
              id, levdatum,
4677
5.93k
              &usrdatum->range.level[l].cat)) {
4678
12
          free(id);
4679
12
          return -1;
4680
12
        }
4681
5.92k
        free(id);
4682
5.92k
      }
4683
4684
12.3k
      id = queue_remove(id_queue);
4685
12.3k
      if (!id)
4686
8.27k
        break;
4687
12.3k
    }
4688
4689
8.27k
    if (l == 0) {
4690
4.15k
      if (mls_semantic_level_cpy(&usrdatum->range.level[1],
4691
4.15k
               &usrdatum->range.level[0])) {
4692
0
        yyerror("out of memory");
4693
0
        return -1;
4694
0
      }
4695
4.15k
    }
4696
4697
8.27k
    if (usr_global && usr_global != usrdatum) {
4698
3.88k
      if (mls_add_or_check_level(&usr_global->dfltlevel,
4699
3.88k
               &usrdatum->dfltlevel)) {
4700
1
        yyerror("Problem with user default level");
4701
1
        return -1;
4702
1
      }
4703
3.88k
      if (mls_add_or_check_level(&usr_global->range.level[0],
4704
3.88k
               &usrdatum->range.level[0])) {
4705
1
        yyerror("Problem with user low level");
4706
1
        return -1;
4707
1
      }
4708
3.87k
      if (mls_add_or_check_level(&usr_global->range.level[1],
4709
3.87k
               &usrdatum->range.level[1])) {
4710
1
        yyerror("Problem with user high level");
4711
1
        return -1;
4712
1
      }
4713
3.87k
    }
4714
8.27k
  }
4715
8.94k
  return 0;
4716
9.00k
}
4717
4718
static int parse_security_context(context_struct_t *c)
4719
25.8k
{
4720
25.8k
  char *id;
4721
25.8k
  role_datum_t *role;
4722
25.8k
  type_datum_t *typdatum;
4723
25.8k
  user_datum_t *usrdatum;
4724
25.8k
  level_datum_t *levdatum;
4725
25.8k
  int l;
4726
4727
25.8k
  if (pass == 1) {
4728
18.6k
    id = queue_remove(id_queue);
4729
18.6k
    free(id); /* user  */
4730
18.6k
    id = queue_remove(id_queue);
4731
18.6k
    free(id); /* role  */
4732
18.6k
    id = queue_remove(id_queue);
4733
18.6k
    free(id); /* type  */
4734
18.6k
    if (mlspol) {
4735
14.1k
      id = queue_remove(id_queue);
4736
14.1k
      free(id);
4737
14.8k
      for (l = 0; l < 2; l++) {
4738
17.4k
        while ((id = queue_remove(id_queue))) {
4739
2.67k
          free(id);
4740
2.67k
        }
4741
14.8k
        id = queue_remove(id_queue);
4742
14.8k
        if (!id)
4743
14.1k
          break;
4744
626
        free(id);
4745
626
      }
4746
14.1k
    }
4747
18.6k
    return 0;
4748
18.6k
  }
4749
4750
  /* check context c to make sure ok to dereference c later */
4751
7.22k
  if (c == NULL) {
4752
0
    yyerror("null context pointer!");
4753
0
    return -1;
4754
0
  }
4755
4756
7.22k
  context_init(c);
4757
4758
  /* extract the user */
4759
7.22k
  id = queue_remove(id_queue);
4760
7.22k
  if (!id) {
4761
2
    yyerror("no effective user?");
4762
2
    goto bad;
4763
2
  }
4764
7.21k
  if (!is_id_in_scope(SYM_USERS, id)) {
4765
1
    yyerror2("user %s is not within scope", id);
4766
1
    free(id);
4767
1
    goto bad;
4768
1
  }
4769
7.21k
  usrdatum = (user_datum_t *)hashtab_search(policydbp->p_users.table,
4770
7.21k
              (hashtab_key_t)id);
4771
7.21k
  if (!usrdatum) {
4772
369
    yyerror2("user %s is not defined", id);
4773
369
    free(id);
4774
369
    goto bad;
4775
369
  }
4776
6.84k
  c->user = usrdatum->s.value;
4777
4778
  /* no need to keep the user name */
4779
6.84k
  free(id);
4780
4781
  /* extract the role */
4782
6.84k
  id = (char *)queue_remove(id_queue);
4783
6.84k
  if (!id) {
4784
0
    yyerror("no role name for sid context definition?");
4785
0
    return -1;
4786
0
  }
4787
6.84k
  if (!is_id_in_scope(SYM_ROLES, id)) {
4788
1
    yyerror2("role %s is not within scope", id);
4789
1
    free(id);
4790
1
    return -1;
4791
1
  }
4792
6.84k
  role = (role_datum_t *)hashtab_search(policydbp->p_roles.table,
4793
6.84k
                (hashtab_key_t)id);
4794
6.84k
  if (!role) {
4795
81
    yyerror2("role %s is not defined", id);
4796
81
    free(id);
4797
81
    return -1;
4798
81
  }
4799
6.76k
  c->role = role->s.value;
4800
4801
  /* no need to keep the role name */
4802
6.76k
  free(id);
4803
4804
  /* extract the type */
4805
6.76k
  id = (char *)queue_remove(id_queue);
4806
6.76k
  if (!id) {
4807
0
    yyerror("no type name for sid context definition?");
4808
0
    return -1;
4809
0
  }
4810
6.76k
  if (!is_id_in_scope(SYM_TYPES, id)) {
4811
1
    yyerror2("type %s is not within scope", id);
4812
1
    free(id);
4813
1
    return -1;
4814
1
  }
4815
6.76k
  typdatum = (type_datum_t *)hashtab_search(policydbp->p_types.table,
4816
6.76k
              (hashtab_key_t)id);
4817
6.76k
  if (!typdatum || typdatum->flavor == TYPE_ATTRIB) {
4818
52
    yyerror2("type %s is not defined or is an attribute", id);
4819
52
    free(id);
4820
52
    return -1;
4821
52
  }
4822
6.71k
  c->type = typdatum->s.value;
4823
4824
  /* no need to keep the type name */
4825
6.71k
  free(id);
4826
4827
6.71k
  if (mlspol) {
4828
    /* extract the low sensitivity */
4829
6.70k
    id = (char *)queue_remove(id_queue);
4830
6.70k
    if (!id) {
4831
2
      yyerror("no sensitivity name for sid context"
4832
2
        " definition?");
4833
2
      return -1;
4834
2
    }
4835
4836
6.73k
    for (l = 0; l < 2; l++) {
4837
6.73k
      levdatum = (level_datum_t *)hashtab_search(
4838
6.73k
        policydbp->p_levels.table, (hashtab_key_t)id);
4839
6.73k
      if (!levdatum) {
4840
16
        yyerror2("Sensitivity %s is not defined", id);
4841
16
        free(id);
4842
16
        return -1;
4843
16
      }
4844
6.72k
      free(id);
4845
6.72k
      c->range.level[l].sens = levdatum->level->sens;
4846
4847
      /* extract low category set */
4848
7.21k
      while ((id = queue_remove(id_queue))) {
4849
511
        if (parse_categories(id, levdatum,
4850
511
                 &c->range.level[l].cat)) {
4851
15
          free(id);
4852
15
          return -1;
4853
15
        }
4854
496
        free(id);
4855
496
      }
4856
4857
      /* extract high sensitivity */
4858
6.70k
      id = (char *)queue_remove(id_queue);
4859
6.70k
      if (!id)
4860
6.67k
        break;
4861
6.70k
    }
4862
4863
6.67k
    if (l == 0) {
4864
6.63k
      c->range.level[1].sens = c->range.level[0].sens;
4865
6.63k
      if (ebitmap_cpy(&c->range.level[1].cat,
4866
6.63k
          &c->range.level[0].cat)) {
4867
0
        yyerror("out of memory");
4868
0
        goto bad;
4869
0
      }
4870
6.63k
    }
4871
6.67k
  }
4872
4873
6.68k
  if (!policydb_context_isvalid(policydbp, c)) {
4874
30
    yyerror("invalid security context");
4875
30
    goto bad;
4876
30
  }
4877
6.65k
  return 0;
4878
4879
402
bad:
4880
402
  context_destroy(c);
4881
4882
402
  return -1;
4883
6.68k
}
4884
4885
int define_initial_sid_context(void)
4886
11.7k
{
4887
11.7k
  char *id;
4888
11.7k
  ocontext_t *c, *head;
4889
4890
11.7k
  if (pass == 1) {
4891
7.75k
    id = (char *)queue_remove(id_queue);
4892
7.75k
    free(id);
4893
7.75k
    parse_security_context(NULL);
4894
7.75k
    return 0;
4895
7.75k
  }
4896
4897
3.97k
  id = (char *)queue_remove(id_queue);
4898
3.97k
  if (!id) {
4899
1
    yyerror("no sid name for SID context definition?");
4900
1
    return -1;
4901
1
  }
4902
3.97k
  head = policydbp->ocontexts[OCON_ISID];
4903
6.85k
  for (c = head; c; c = c->next) {
4904
6.57k
    if (!strcmp(id, c->u.name))
4905
3.69k
      break;
4906
6.57k
  }
4907
4908
3.97k
  if (!c) {
4909
280
    yyerror2("SID %s is not defined", id);
4910
280
    free(id);
4911
280
    return -1;
4912
280
  }
4913
3.69k
  if (c->context[0].user) {
4914
7
    yyerror2("The context for SID %s is multiply defined", id);
4915
7
    free(id);
4916
7
    return -1;
4917
7
  }
4918
  /* no need to keep the sid name */
4919
3.68k
  free(id);
4920
4921
3.68k
  if (parse_security_context(&c->context[0]))
4922
70
    return -1;
4923
4924
3.61k
  return 0;
4925
3.68k
}
4926
4927
int define_pirq_context(unsigned int pirq)
4928
114
{
4929
114
  ocontext_t *newc, *c, *l, *head;
4930
4931
114
  if (policydbp->target_platform != SEPOL_TARGET_XEN) {
4932
1
    yyerror("pirqcon not supported for target");
4933
1
    return -1;
4934
1
  }
4935
4936
113
  if (pass == 1) {
4937
113
    parse_security_context(NULL);
4938
113
    return 0;
4939
113
  }
4940
4941
0
  if (pirq > 0xffff) {
4942
0
    yyerror2("pirq %u out of range", pirq);
4943
0
    return -1;
4944
0
  }
4945
4946
0
  newc = calloc(1, sizeof(ocontext_t));
4947
0
  if (!newc) {
4948
0
    yyerror("out of memory");
4949
0
    return -1;
4950
0
  }
4951
4952
0
  newc->u.pirq = pirq;
4953
4954
0
  if (parse_security_context(&newc->context[0])) {
4955
0
    free(newc);
4956
0
    return -1;
4957
0
  }
4958
4959
0
  head = policydbp->ocontexts[OCON_XEN_PIRQ];
4960
0
  for (l = NULL, c = head; c; l = c, c = c->next) {
4961
0
    unsigned int pirq2;
4962
4963
0
    pirq2 = c->u.pirq;
4964
0
    if (pirq == pirq2) {
4965
0
      yyerror2("duplicate pirqcon entry for %d ", pirq);
4966
0
      goto bad;
4967
0
    }
4968
0
  }
4969
4970
0
  if (l)
4971
0
    l->next = newc;
4972
0
  else
4973
0
    policydbp->ocontexts[OCON_XEN_PIRQ] = newc;
4974
4975
0
  return 0;
4976
4977
0
bad:
4978
0
  free(newc);
4979
0
  return -1;
4980
0
}
4981
4982
int define_iomem_context(uint64_t low, uint64_t high)
4983
816
{
4984
816
  ocontext_t *newc, *c, *l, *head;
4985
4986
816
  if (policydbp->target_platform != SEPOL_TARGET_XEN) {
4987
2
    yyerror("iomemcon not supported for target");
4988
2
    return -1;
4989
2
  }
4990
4991
814
  if (pass == 1) {
4992
814
    parse_security_context(NULL);
4993
814
    return 0;
4994
814
  }
4995
4996
0
  newc = calloc(1, sizeof(ocontext_t));
4997
0
  if (!newc) {
4998
0
    yyerror("out of memory");
4999
0
    return -1;
5000
0
  }
5001
5002
0
  newc->u.iomem.low_iomem = low;
5003
0
  newc->u.iomem.high_iomem = high;
5004
5005
0
  if (low > high) {
5006
0
    yyerror2("low memory 0x%" PRIx64
5007
0
       " exceeds high memory 0x%" PRIx64 "",
5008
0
       low, high);
5009
0
    free(newc);
5010
0
    return -1;
5011
0
  }
5012
5013
0
  if (parse_security_context(&newc->context[0])) {
5014
0
    free(newc);
5015
0
    return -1;
5016
0
  }
5017
5018
0
  head = policydbp->ocontexts[OCON_XEN_IOMEM];
5019
0
  for (l = NULL, c = head; c; l = c, c = c->next) {
5020
0
    uint64_t low2, high2;
5021
5022
0
    low2 = c->u.iomem.low_iomem;
5023
0
    high2 = c->u.iomem.high_iomem;
5024
0
    if (low <= high2 && low2 <= high) {
5025
0
      yyerror2("iomemcon entry for 0x%" PRIx64 "-0x%" PRIx64
5026
0
         " overlaps with "
5027
0
         "earlier entry 0x%" PRIx64 "-0x%" PRIx64 "",
5028
0
         low, high, low2, high2);
5029
0
      goto bad;
5030
0
    }
5031
0
  }
5032
5033
0
  if (l)
5034
0
    l->next = newc;
5035
0
  else
5036
0
    policydbp->ocontexts[OCON_XEN_IOMEM] = newc;
5037
5038
0
  return 0;
5039
5040
0
bad:
5041
0
  free(newc);
5042
0
  return -1;
5043
0
}
5044
5045
int define_ioport_context(unsigned long low, unsigned long high)
5046
420
{
5047
420
  ocontext_t *newc, *c, *l, *head;
5048
5049
420
  if (policydbp->target_platform != SEPOL_TARGET_XEN) {
5050
2
    yyerror("ioportcon not supported for target");
5051
2
    return -1;
5052
2
  }
5053
5054
418
  if (pass == 1) {
5055
418
    parse_security_context(NULL);
5056
418
    return 0;
5057
418
  }
5058
5059
0
  newc = calloc(1, sizeof(ocontext_t));
5060
0
  if (!newc) {
5061
0
    yyerror("out of memory");
5062
0
    return -1;
5063
0
  }
5064
5065
0
  newc->u.ioport.low_ioport = low;
5066
0
  newc->u.ioport.high_ioport = high;
5067
5068
0
  if (low > high) {
5069
0
    yyerror2("low ioport 0x%lx exceeds high ioport 0x%lx", low,
5070
0
       high);
5071
0
    free(newc);
5072
0
    return -1;
5073
0
  }
5074
5075
0
  if (parse_security_context(&newc->context[0])) {
5076
0
    free(newc);
5077
0
    return -1;
5078
0
  }
5079
5080
0
  head = policydbp->ocontexts[OCON_XEN_IOPORT];
5081
0
  for (l = NULL, c = head; c; l = c, c = c->next) {
5082
0
    uint32_t low2, high2;
5083
5084
0
    low2 = c->u.ioport.low_ioport;
5085
0
    high2 = c->u.ioport.high_ioport;
5086
0
    if (low <= high2 && low2 <= high) {
5087
0
      yyerror2("ioportcon entry for 0x%lx-0x%lx overlaps with"
5088
0
         "earlier entry 0x%x-0x%x",
5089
0
         low, high, low2, high2);
5090
0
      goto bad;
5091
0
    }
5092
0
  }
5093
5094
0
  if (l)
5095
0
    l->next = newc;
5096
0
  else
5097
0
    policydbp->ocontexts[OCON_XEN_IOPORT] = newc;
5098
5099
0
  return 0;
5100
5101
0
bad:
5102
0
  free(newc);
5103
0
  return -1;
5104
0
}
5105
5106
int define_pcidevice_context(unsigned long device)
5107
218
{
5108
218
  ocontext_t *newc, *c, *l, *head;
5109
5110
218
  if (policydbp->target_platform != SEPOL_TARGET_XEN) {
5111
1
    yyerror("pcidevicecon not supported for target");
5112
1
    return -1;
5113
1
  }
5114
5115
217
  if (pass == 1) {
5116
217
    parse_security_context(NULL);
5117
217
    return 0;
5118
217
  }
5119
5120
0
  newc = calloc(1, sizeof(ocontext_t));
5121
0
  if (!newc) {
5122
0
    yyerror("out of memory");
5123
0
    return -1;
5124
0
  }
5125
5126
0
  newc->u.device = device;
5127
5128
0
  if (parse_security_context(&newc->context[0])) {
5129
0
    free(newc);
5130
0
    return -1;
5131
0
  }
5132
5133
0
  head = policydbp->ocontexts[OCON_XEN_PCIDEVICE];
5134
0
  for (l = NULL, c = head; c; l = c, c = c->next) {
5135
0
    unsigned int device2;
5136
5137
0
    device2 = c->u.device;
5138
0
    if (device == device2) {
5139
0
      yyerror2("duplicate pcidevicecon entry for 0x%lx",
5140
0
         device);
5141
0
      goto bad;
5142
0
    }
5143
0
  }
5144
5145
0
  if (l)
5146
0
    l->next = newc;
5147
0
  else
5148
0
    policydbp->ocontexts[OCON_XEN_PCIDEVICE] = newc;
5149
5150
0
  return 0;
5151
5152
0
bad:
5153
0
  free(newc);
5154
0
  return -1;
5155
0
}
5156
5157
int define_devicetree_context(void)
5158
198
{
5159
198
  ocontext_t *newc, *c, *l, *head;
5160
198
  char *path;
5161
5162
198
  if (policydbp->target_platform != SEPOL_TARGET_XEN) {
5163
1
    yyerror("devicetreecon not supported for target");
5164
1
    return -1;
5165
1
  }
5166
5167
197
  if (pass == 1) {
5168
197
    free(queue_remove(id_queue));
5169
197
    parse_security_context(NULL);
5170
197
    return 0;
5171
197
  }
5172
5173
0
  path = (char *)queue_remove(id_queue);
5174
0
  if (!path) {
5175
0
    yyerror("Missing devicetreepath");
5176
0
    return -1;
5177
0
  }
5178
5179
0
  if (!is_valid_dt_path(path)) {
5180
0
    yyerror2("invalid devicetreecon path \"%s\"", path);
5181
0
    free(path);
5182
0
    return -1;
5183
0
  }
5184
5185
0
  newc = calloc(1, sizeof(ocontext_t));
5186
0
  if (!newc) {
5187
0
    yyerror("out of memory");
5188
0
    free(path);
5189
0
    return -1;
5190
0
  }
5191
5192
0
  newc->u.name = path;
5193
5194
0
  if (parse_security_context(&newc->context[0])) {
5195
0
    free(newc->u.name);
5196
0
    free(newc);
5197
0
    return -1;
5198
0
  }
5199
5200
0
  head = policydbp->ocontexts[OCON_XEN_DEVICETREE];
5201
0
  for (l = NULL, c = head; c; l = c, c = c->next) {
5202
0
    if (strcmp(newc->u.name, c->u.name) == 0) {
5203
0
      yyerror2("duplicate devicetree entry for '%s'",
5204
0
         newc->u.name);
5205
0
      goto bad;
5206
0
    }
5207
0
  }
5208
5209
0
  if (l)
5210
0
    l->next = newc;
5211
0
  else
5212
0
    policydbp->ocontexts[OCON_XEN_DEVICETREE] = newc;
5213
5214
0
  return 0;
5215
5216
0
bad:
5217
0
  free(newc->u.name);
5218
0
  free(newc);
5219
0
  return -1;
5220
0
}
5221
5222
int define_port_context(unsigned int low, unsigned int high)
5223
743
{
5224
743
  ocontext_t *newc, *c, *l, *head;
5225
743
  unsigned int protocol;
5226
743
  char *id;
5227
5228
743
  if (policydbp->target_platform != SEPOL_TARGET_SELINUX) {
5229
2
    yyerror("portcon not supported for target");
5230
2
    return -1;
5231
2
  }
5232
5233
741
  if (pass == 1) {
5234
526
    id = (char *)queue_remove(id_queue);
5235
526
    free(id);
5236
526
    parse_security_context(NULL);
5237
526
    return 0;
5238
526
  }
5239
5240
215
  newc = calloc(1, sizeof(ocontext_t));
5241
215
  if (!newc) {
5242
0
    yyerror("out of memory");
5243
0
    return -1;
5244
0
  }
5245
5246
215
  id = (char *)queue_remove(id_queue);
5247
215
  if (!id) {
5248
0
    free(newc);
5249
0
    return -1;
5250
0
  }
5251
215
  if ((strcmp(id, "tcp") == 0) || (strcmp(id, "TCP") == 0)) {
5252
97
    protocol = IPPROTO_TCP;
5253
118
  } else if ((strcmp(id, "udp") == 0) || (strcmp(id, "UDP") == 0)) {
5254
2
    protocol = IPPROTO_UDP;
5255
116
  } else if ((strcmp(id, "dccp") == 0) || (strcmp(id, "DCCP") == 0)) {
5256
7
    protocol = IPPROTO_DCCP;
5257
109
  } else if ((strcmp(id, "sctp") == 0) || (strcmp(id, "SCTP") == 0)) {
5258
2
    protocol = IPPROTO_SCTP;
5259
107
  } else {
5260
107
    yyerror2("unrecognized protocol %s", id);
5261
107
    goto bad;
5262
107
  }
5263
5264
108
  if (low == 0 || low > high || high > 0xffff) {
5265
95
    yyerror2("Invalid port range %u-%u", low, high);
5266
95
    goto bad;
5267
95
  }
5268
5269
13
  newc->u.port.protocol = protocol;
5270
13
  newc->u.port.low_port = low;
5271
13
  newc->u.port.high_port = high;
5272
5273
13
  if (parse_security_context(&newc->context[0])) {
5274
13
    goto bad;
5275
13
  }
5276
5277
  /* Preserve the matching order specified in the configuration. */
5278
0
  head = policydbp->ocontexts[OCON_PORT];
5279
0
  for (l = NULL, c = head; c; l = c, c = c->next) {
5280
0
    unsigned int prot2, low2, high2;
5281
5282
0
    prot2 = c->u.port.protocol;
5283
0
    low2 = c->u.port.low_port;
5284
0
    high2 = c->u.port.high_port;
5285
0
    if (protocol != prot2)
5286
0
      continue;
5287
0
    if (low == low2 && high == high2) {
5288
0
      yyerror2("duplicate portcon entry for %s %d-%d ", id,
5289
0
         low, high);
5290
0
      goto bad;
5291
0
    }
5292
0
    if (low2 <= low && high2 >= high) {
5293
0
      yyerror2("portcon entry for %s %d-%d hidden by earlier "
5294
0
         "entry for %d-%d",
5295
0
         id, low, high, low2, high2);
5296
0
      goto bad;
5297
0
    }
5298
0
  }
5299
5300
0
  if (l)
5301
0
    l->next = newc;
5302
0
  else
5303
0
    policydbp->ocontexts[OCON_PORT] = newc;
5304
5305
0
  free(id);
5306
0
  return 0;
5307
5308
215
bad:
5309
215
  free(id);
5310
215
  free(newc);
5311
215
  return -1;
5312
0
}
5313
5314
int define_ibpkey_context(unsigned int low, unsigned int high)
5315
1.82k
{
5316
1.82k
  ocontext_t *newc, *c, *l, *head;
5317
1.82k
  struct in6_addr subnet_prefix;
5318
1.82k
  char *id;
5319
1.82k
  int rc = 0;
5320
5321
1.82k
  if (policydbp->target_platform != SEPOL_TARGET_SELINUX) {
5322
2
    yyerror("ibpkeycon not supported for target");
5323
2
    return -1;
5324
2
  }
5325
5326
1.81k
  if (pass == 1) {
5327
1.31k
    id = (char *)queue_remove(id_queue);
5328
1.31k
    free(id);
5329
1.31k
    parse_security_context(NULL);
5330
1.31k
    return 0;
5331
1.31k
  }
5332
5333
500
  newc = calloc(1, sizeof(*newc));
5334
500
  if (!newc) {
5335
0
    yyerror("out of memory");
5336
0
    return -1;
5337
0
  }
5338
5339
500
  id = queue_remove(id_queue);
5340
500
  if (!id) {
5341
0
    yyerror("failed to read the subnet prefix");
5342
0
    rc = -1;
5343
0
    goto out;
5344
0
  }
5345
5346
500
  rc = inet_pton(AF_INET6, id, &subnet_prefix);
5347
500
  free(id);
5348
500
  if (rc < 1) {
5349
1
    yyerror("failed to parse the subnet prefix");
5350
1
    if (rc == 0)
5351
1
      rc = -1;
5352
1
    goto out;
5353
1
  }
5354
5355
499
  if (subnet_prefix.s6_addr32[2] || subnet_prefix.s6_addr32[3]) {
5356
35
    yyerror("subnet prefix should be 0's in the low order 64 bits.");
5357
35
    rc = -1;
5358
35
    goto out;
5359
35
  }
5360
5361
464
  if (low > 0xffff || high > 0xffff) {
5362
40
    yyerror("pkey value too large, pkeys are 16 bits.");
5363
40
    rc = -1;
5364
40
    goto out;
5365
40
  }
5366
5367
424
  memcpy(&newc->u.ibpkey.subnet_prefix, &subnet_prefix.s6_addr[0],
5368
424
         sizeof(newc->u.ibpkey.subnet_prefix));
5369
5370
424
  newc->u.ibpkey.low_pkey = low;
5371
424
  newc->u.ibpkey.high_pkey = high;
5372
5373
424
  if (low > high) {
5374
18
    yyerror2("low pkey %d exceeds high pkey %d", low, high);
5375
18
    rc = -1;
5376
18
    goto out;
5377
18
  }
5378
5379
406
  rc = parse_security_context(&newc->context[0]);
5380
406
  if (rc)
5381
40
    goto out;
5382
5383
  /* Preserve the matching order specified in the configuration. */
5384
366
  head = policydbp->ocontexts[OCON_IBPKEY];
5385
771
  for (l = NULL, c = head; c; l = c, c = c->next) {
5386
414
    unsigned int low2, high2;
5387
5388
414
    low2 = c->u.ibpkey.low_pkey;
5389
414
    high2 = c->u.ibpkey.high_pkey;
5390
5391
414
    if (low == low2 && high == high2 &&
5392
39
        c->u.ibpkey.subnet_prefix == newc->u.ibpkey.subnet_prefix) {
5393
7
      yyerror2("duplicate ibpkeycon entry for %d-%d ", low,
5394
7
         high);
5395
7
      rc = -1;
5396
7
      goto out;
5397
7
    }
5398
407
    if (low2 <= low && high2 >= high &&
5399
34
        c->u.ibpkey.subnet_prefix == newc->u.ibpkey.subnet_prefix) {
5400
2
      yyerror2(
5401
2
        "ibpkeycon entry for %d-%d hidden by earlier entry for %d-%d",
5402
2
        low, high, low2, high2);
5403
2
      rc = -1;
5404
2
      goto out;
5405
2
    }
5406
407
  }
5407
5408
357
  if (l)
5409
214
    l->next = newc;
5410
143
  else
5411
143
    policydbp->ocontexts[OCON_IBPKEY] = newc;
5412
5413
357
  return 0;
5414
5415
143
out:
5416
143
  free(newc);
5417
143
  return rc;
5418
366
}
5419
5420
int define_ibendport_context(unsigned int port)
5421
251
{
5422
251
  ocontext_t *newc, *c, *l, *head;
5423
251
  char *id;
5424
251
  int rc = 0;
5425
5426
251
  if (policydbp->target_platform != SEPOL_TARGET_SELINUX) {
5427
1
    yyerror("ibendportcon not supported for target");
5428
1
    return -1;
5429
1
  }
5430
5431
250
  if (pass == 1) {
5432
250
    id = (char *)queue_remove(id_queue);
5433
250
    free(id);
5434
250
    parse_security_context(NULL);
5435
250
    return 0;
5436
250
  }
5437
5438
0
  if (port > 0xff || port == 0) {
5439
0
    yyerror2(
5440
0
      "Invalid ibendport port number %d, should be 0 < port < 256",
5441
0
      port);
5442
0
    return -1;
5443
0
  }
5444
5445
0
  newc = calloc(1, sizeof(*newc));
5446
0
  if (!newc) {
5447
0
    yyerror("out of memory");
5448
0
    return -1;
5449
0
  }
5450
5451
0
  newc->u.ibendport.dev_name = queue_remove(id_queue);
5452
0
  if (!newc->u.ibendport.dev_name) {
5453
0
    yyerror("failed to read infiniband device name.");
5454
0
    rc = -1;
5455
0
    goto out;
5456
0
  }
5457
5458
0
  if (strlen(newc->u.ibendport.dev_name) > IB_DEVICE_NAME_MAX - 1) {
5459
0
    yyerror2("infiniband device name %s exceeds max length of 63.",
5460
0
       newc->u.ibendport.dev_name);
5461
0
    rc = -1;
5462
0
    goto out;
5463
0
  }
5464
5465
0
  newc->u.ibendport.port = port;
5466
5467
0
  if (parse_security_context(&newc->context[0])) {
5468
0
    free(newc);
5469
0
    return -1;
5470
0
  }
5471
5472
  /* Preserve the matching order specified in the configuration. */
5473
0
  head = policydbp->ocontexts[OCON_IBENDPORT];
5474
0
  for (l = NULL, c = head; c; l = c, c = c->next) {
5475
0
    unsigned int port2;
5476
5477
0
    port2 = c->u.ibendport.port;
5478
5479
0
    if (port == port2 && !strcmp(c->u.ibendport.dev_name,
5480
0
               newc->u.ibendport.dev_name)) {
5481
0
      yyerror2("duplicate ibendportcon entry for %s port %u",
5482
0
         newc->u.ibendport.dev_name, port);
5483
0
      rc = -1;
5484
0
      goto out;
5485
0
    }
5486
0
  }
5487
5488
0
  if (l)
5489
0
    l->next = newc;
5490
0
  else
5491
0
    policydbp->ocontexts[OCON_IBENDPORT] = newc;
5492
5493
0
  return 0;
5494
5495
0
out:
5496
0
  free(newc->u.ibendport.dev_name);
5497
0
  free(newc);
5498
0
  return rc;
5499
0
}
5500
5501
int define_netif_context(void)
5502
855
{
5503
855
  ocontext_t *newc, *c, *head;
5504
5505
855
  if (policydbp->target_platform != SEPOL_TARGET_SELINUX) {
5506
1
    yyerror("netifcon not supported for target");
5507
1
    return -1;
5508
1
  }
5509
5510
854
  if (pass == 1) {
5511
853
    free(queue_remove(id_queue));
5512
853
    parse_security_context(NULL);
5513
853
    parse_security_context(NULL);
5514
853
    return 0;
5515
853
  }
5516
5517
1
  newc = calloc(1, sizeof(ocontext_t));
5518
1
  if (!newc) {
5519
0
    yyerror("out of memory");
5520
0
    return -1;
5521
0
  }
5522
5523
1
  newc->u.name = (char *)queue_remove(id_queue);
5524
1
  if (!newc->u.name) {
5525
0
    free(newc);
5526
0
    return -1;
5527
0
  }
5528
1
  if (parse_security_context(&newc->context[0])) {
5529
1
    free(newc->u.name);
5530
1
    free(newc);
5531
1
    return -1;
5532
1
  }
5533
0
  if (parse_security_context(&newc->context[1])) {
5534
0
    context_destroy(&newc->context[0]);
5535
0
    free(newc->u.name);
5536
0
    free(newc);
5537
0
    return -1;
5538
0
  }
5539
0
  head = policydbp->ocontexts[OCON_NETIF];
5540
5541
0
  for (c = head; c; c = c->next) {
5542
0
    if (!strcmp(newc->u.name, c->u.name)) {
5543
0
      yyerror2("duplicate entry for network interface %s",
5544
0
         newc->u.name);
5545
0
      context_destroy(&newc->context[0]);
5546
0
      context_destroy(&newc->context[1]);
5547
0
      free(newc->u.name);
5548
0
      free(newc);
5549
0
      return -1;
5550
0
    }
5551
0
  }
5552
5553
0
  newc->next = head;
5554
0
  policydbp->ocontexts[OCON_NETIF] = newc;
5555
0
  return 0;
5556
0
}
5557
5558
static int insert_ipv4_node(ocontext_t *newc)
5559
520
{
5560
520
  ocontext_t *c, *l;
5561
520
  char addr[INET_ADDRSTRLEN];
5562
520
  char mask[INET_ADDRSTRLEN];
5563
5564
  /* Create order of most specific to least retaining
5565
     the order specified in the configuration. */
5566
1.04k
  for (l = NULL, c = policydbp->ocontexts[OCON_NODE]; c;
5567
704
       l = c, c = c->next) {
5568
704
    if (newc->u.node.mask == c->u.node.mask &&
5569
257
        newc->u.node.addr == c->u.node.addr) {
5570
7
      yyerror2(
5571
7
        "duplicate entry for network node %s %s",
5572
7
        inet_ntop(AF_INET, &newc->u.node.addr, addr, INET_ADDRSTRLEN) ?:
5573
7
          "<invalid>",
5574
7
        inet_ntop(AF_INET, &newc->u.node.mask, mask, INET_ADDRSTRLEN) ?:
5575
7
          "<invalid>");
5576
7
      context_destroy(&newc->context[0]);
5577
7
      free(newc);
5578
7
      return -1;
5579
7
    }
5580
5581
    /* The addresses and masks are stored in network byte order. */
5582
697
    if (be32toh(newc->u.node.mask) > be32toh(c->u.node.mask))
5583
170
      break;
5584
697
  }
5585
5586
513
  newc->next = c;
5587
5588
513
  if (l)
5589
245
    l->next = newc;
5590
268
  else
5591
268
    policydbp->ocontexts[OCON_NODE] = newc;
5592
5593
513
  return 0;
5594
520
}
5595
5596
int define_ipv4_node_context(void)
5597
1.11k
{
5598
1.11k
  char *id;
5599
1.11k
  int rc = 0;
5600
1.11k
  struct in_addr addr, mask;
5601
1.11k
  ocontext_t *newc;
5602
5603
1.11k
  if (policydbp->target_platform != SEPOL_TARGET_SELINUX) {
5604
1
    yyerror("nodecon not supported for target");
5605
1
    return -1;
5606
1
  }
5607
5608
1.11k
  if (pass == 1) {
5609
696
    free(queue_remove(id_queue));
5610
696
    free(queue_remove(id_queue));
5611
696
    parse_security_context(NULL);
5612
696
    return 0;
5613
696
  }
5614
5615
414
  id = queue_remove(id_queue);
5616
414
  if (!id) {
5617
0
    yyerror("failed to read ipv4 address");
5618
0
    return -1;
5619
0
  }
5620
5621
414
  rc = inet_pton(AF_INET, id, &addr);
5622
414
  if (rc < 1) {
5623
1
    yyerror2("failed to parse ipv4 address %s", id);
5624
1
    free(id);
5625
1
    return -1;
5626
1
  }
5627
413
  free(id);
5628
5629
413
  id = queue_remove(id_queue);
5630
413
  if (!id) {
5631
0
    yyerror("failed to read ipv4 address");
5632
0
    return -1;
5633
0
  }
5634
5635
413
  rc = inet_pton(AF_INET, id, &mask);
5636
413
  if (rc < 1) {
5637
1
    yyerror2("failed to parse ipv4 mask %s", id);
5638
1
    free(id);
5639
1
    return -1;
5640
1
  }
5641
5642
412
  free(id);
5643
5644
412
  if (mask.s_addr != 0 &&
5645
411
      ((~be32toh(mask.s_addr) + 1) & ~be32toh(mask.s_addr)) != 0) {
5646
411
    yywarn("ipv4 mask is not contiguous");
5647
411
  }
5648
5649
412
  if ((~mask.s_addr & addr.s_addr) != 0) {
5650
343
    yywarn("host bits in ipv4 address set");
5651
343
  }
5652
5653
412
  newc = calloc(1, sizeof(ocontext_t));
5654
412
  if (!newc) {
5655
0
    yyerror("out of memory");
5656
0
    return -1;
5657
0
  }
5658
5659
412
  newc->u.node.addr = addr.s_addr;
5660
412
  newc->u.node.mask = mask.s_addr;
5661
5662
412
  if (parse_security_context(&newc->context[0])) {
5663
60
    free(newc);
5664
60
    return -1;
5665
60
  }
5666
5667
352
  return insert_ipv4_node(newc);
5668
412
}
5669
5670
int define_ipv4_cidr_node_context(void)
5671
479
{
5672
479
  char *endptr, *id, *split;
5673
479
  unsigned long mask_bits;
5674
479
  uint32_t mask;
5675
479
  struct in_addr addr;
5676
479
  ocontext_t *newc;
5677
479
  int rc;
5678
5679
479
  if (policydbp->target_platform != SEPOL_TARGET_SELINUX) {
5680
1
    yyerror("nodecon not supported for target");
5681
1
    return -1;
5682
1
  }
5683
5684
478
  if (pass == 1) {
5685
276
    free(queue_remove(id_queue));
5686
276
    parse_security_context(NULL);
5687
276
    return 0;
5688
276
  }
5689
5690
202
  id = queue_remove(id_queue);
5691
202
  if (!id) {
5692
0
    yyerror("failed to read IPv4 address");
5693
0
    return -1;
5694
0
  }
5695
5696
202
  split = strchr(id, '/');
5697
202
  if (!split) {
5698
0
    yyerror2("invalid IPv4 CIDR notation: %s", id);
5699
0
    free(id);
5700
0
    return -1;
5701
0
  }
5702
202
  *split = '\0';
5703
5704
202
  rc = inet_pton(AF_INET, id, &addr);
5705
202
  if (rc < 1) {
5706
1
    yyerror2("failed to parse IPv4 address %s", id);
5707
1
    free(id);
5708
1
    return -1;
5709
1
  }
5710
5711
202
  errno = 0;
5712
201
  mask_bits = strtoul(split + 1, &endptr, 10);
5713
201
  if (errno || *endptr != '\0' || mask_bits > 32) {
5714
2
    yyerror2("invalid mask in IPv4 CIDR notation: %s", split + 1);
5715
2
    free(id);
5716
2
    return -1;
5717
2
  }
5718
5719
199
  free(id);
5720
5721
199
  if (mask_bits == 0) {
5722
23
    yywarn("IPv4 CIDR mask of 0, matching all IPs");
5723
23
    mask = 0;
5724
176
  } else {
5725
176
    mask = ~((UINT32_C(1) << (32 - mask_bits)) - 1);
5726
176
    mask = htobe32(mask);
5727
176
  }
5728
5729
199
  if ((~mask & addr.s_addr) != 0)
5730
104
    yywarn("host bits in IPv4 address set");
5731
5732
199
  newc = calloc(1, sizeof(ocontext_t));
5733
199
  if (!newc) {
5734
0
    yyerror("out of memory");
5735
0
    return -1;
5736
0
  }
5737
5738
199
  newc->u.node.addr = addr.s_addr & mask;
5739
199
  newc->u.node.mask = mask;
5740
5741
199
  if (parse_security_context(&newc->context[0])) {
5742
31
    free(newc);
5743
31
    return -1;
5744
31
  }
5745
5746
168
  return insert_ipv4_node(newc);
5747
199
}
5748
5749
static int ipv6_is_mask_contiguous(const struct in6_addr *mask)
5750
152
{
5751
152
  int filled = 1;
5752
152
  unsigned i;
5753
5754
2.17k
  for (i = 0; i < 16; i++) {
5755
2.05k
    if ((((~mask->s6_addr[i] & 0xFF) + 1) &
5756
2.05k
         (~mask->s6_addr[i] & 0xFF)) != 0) {
5757
30
      return 0;
5758
30
    }
5759
2.02k
    if (!filled && mask->s6_addr[i] != 0) {
5760
1
      return 0;
5761
1
    }
5762
5763
2.02k
    if (filled && mask->s6_addr[i] != 0xFF) {
5764
152
      filled = 0;
5765
152
    }
5766
2.02k
  }
5767
5768
121
  return 1;
5769
152
}
5770
5771
static int ipv6_has_host_bits_set(const struct in6_addr *addr,
5772
          const struct in6_addr *mask)
5773
581
{
5774
581
  unsigned i;
5775
5776
9.60k
  for (i = 0; i < 16; i++) {
5777
9.15k
    if ((addr->s6_addr[i] & ~mask->s6_addr[i]) != 0) {
5778
134
      return 1;
5779
134
    }
5780
9.15k
  }
5781
5782
447
  return 0;
5783
581
}
5784
5785
static void ipv6_cidr_bits_to_mask(unsigned long cidr_bits,
5786
           struct in6_addr *mask)
5787
429
{
5788
429
  unsigned i;
5789
5790
2.14k
  for (i = 0; i < 4; i++) {
5791
1.71k
    if (cidr_bits == 0) {
5792
712
      mask->s6_addr32[i] = 0;
5793
1.00k
    } else if (cidr_bits >= 32) {
5794
606
      mask->s6_addr32[i] = ~UINT32_C(0);
5795
606
    } else {
5796
398
      mask->s6_addr32[i] = htobe32(
5797
398
        ~((UINT32_C(1) << (32 - cidr_bits)) - 1));
5798
398
    }
5799
5800
1.71k
    if (cidr_bits >= 32)
5801
606
      cidr_bits -= 32;
5802
1.11k
    else
5803
1.11k
      cidr_bits = 0;
5804
1.71k
  }
5805
429
}
5806
5807
static void ipv6_apply_mask(struct in6_addr *restrict addr,
5808
          const struct in6_addr *restrict mask)
5809
429
{
5810
429
  unsigned i;
5811
5812
2.14k
  for (i = 0; i < 4; i++)
5813
1.71k
    addr->s6_addr32[i] &= mask->s6_addr32[i];
5814
429
}
5815
5816
static int insert_ipv6_node(ocontext_t *newc)
5817
514
{
5818
514
  ocontext_t *c, *l;
5819
514
  char addr[INET6_ADDRSTRLEN];
5820
514
  char mask[INET6_ADDRSTRLEN];
5821
5822
  /* Create order of most specific to least retaining
5823
     the order specified in the configuration. */
5824
1.09k
  for (l = NULL, c = policydbp->ocontexts[OCON_NODE6]; c;
5825
728
       l = c, c = c->next) {
5826
728
    if (memcmp(&newc->u.node6.mask, &c->u.node6.mask, 16) == 0 &&
5827
214
        memcmp(&newc->u.node6.addr, &c->u.node6.addr, 16) == 0) {
5828
10
      yyerror2(
5829
10
        "duplicate entry for network node %s %s",
5830
10
        inet_ntop(AF_INET6, &newc->u.node6.addr, addr, INET6_ADDRSTRLEN) ?:
5831
10
          "<invalid>",
5832
10
        inet_ntop(AF_INET6, &newc->u.node6.mask, mask, INET6_ADDRSTRLEN) ?:
5833
10
          "<invalid>");
5834
10
      context_destroy(&newc->context[0]);
5835
10
      free(newc);
5836
10
      return -1;
5837
10
    }
5838
5839
718
    if (memcmp(&newc->u.node6.mask, &c->u.node6.mask, 16) > 0)
5840
134
      break;
5841
718
  }
5842
5843
504
  newc->next = c;
5844
5845
504
  if (l)
5846
239
    l->next = newc;
5847
265
  else
5848
265
    policydbp->ocontexts[OCON_NODE6] = newc;
5849
5850
504
  return 0;
5851
514
}
5852
5853
int define_ipv6_node_context(void)
5854
571
{
5855
571
  char *id;
5856
571
  int rc = 0;
5857
571
  struct in6_addr addr, mask;
5858
571
  ocontext_t *newc;
5859
5860
571
  if (policydbp->target_platform != SEPOL_TARGET_SELINUX) {
5861
1
    yyerror("nodecon not supported for target");
5862
1
    return -1;
5863
1
  }
5864
5865
570
  if (pass == 1) {
5866
416
    free(queue_remove(id_queue));
5867
416
    free(queue_remove(id_queue));
5868
416
    parse_security_context(NULL);
5869
416
    return 0;
5870
416
  }
5871
5872
154
  id = queue_remove(id_queue);
5873
154
  if (!id) {
5874
0
    yyerror("failed to read ipv6 address");
5875
0
    return -1;
5876
0
  }
5877
5878
154
  rc = inet_pton(AF_INET6, id, &addr);
5879
154
  if (rc < 1) {
5880
1
    yyerror2("failed to parse ipv6 address %s", id);
5881
1
    free(id);
5882
1
    return -1;
5883
1
  }
5884
5885
153
  free(id);
5886
5887
153
  id = queue_remove(id_queue);
5888
153
  if (!id) {
5889
0
    yyerror("failed to read ipv6 address");
5890
0
    return -1;
5891
0
  }
5892
5893
153
  rc = inet_pton(AF_INET6, id, &mask);
5894
153
  if (rc < 1) {
5895
1
    yyerror2("failed to parse ipv6 mask %s", id);
5896
1
    free(id);
5897
1
    return -1;
5898
1
  }
5899
5900
152
  free(id);
5901
5902
152
  if (!ipv6_is_mask_contiguous(&mask)) {
5903
31
    yywarn("ipv6 mask is not contiguous");
5904
31
  }
5905
5906
152
  if (ipv6_has_host_bits_set(&addr, &mask)) {
5907
120
    yywarn("host bits in ipv6 address set");
5908
120
  }
5909
5910
152
  newc = calloc(1, sizeof(ocontext_t));
5911
152
  if (!newc) {
5912
0
    yyerror("out of memory");
5913
0
    return -1;
5914
0
  }
5915
5916
152
  memcpy(&newc->u.node6.addr[0], &addr.s6_addr[0], 16);
5917
152
  memcpy(&newc->u.node6.mask[0], &mask.s6_addr[0], 16);
5918
5919
152
  if (parse_security_context(&newc->context[0])) {
5920
33
    free(newc);
5921
33
    return -1;
5922
33
  }
5923
5924
119
  return insert_ipv6_node(newc);
5925
152
}
5926
5927
int define_ipv6_cidr_node_context(void)
5928
1.08k
{
5929
1.08k
  char *endptr, *id, *split;
5930
1.08k
  unsigned long mask_bits;
5931
1.08k
  int rc;
5932
1.08k
  struct in6_addr addr, mask;
5933
1.08k
  ocontext_t *newc;
5934
5935
1.08k
  if (policydbp->target_platform != SEPOL_TARGET_SELINUX) {
5936
1
    yyerror("nodecon not supported for target");
5937
1
    return -1;
5938
1
  }
5939
5940
1.08k
  if (pass == 1) {
5941
649
    free(queue_remove(id_queue));
5942
649
    parse_security_context(NULL);
5943
649
    return 0;
5944
649
  }
5945
5946
433
  id = queue_remove(id_queue);
5947
433
  if (!id) {
5948
0
    yyerror("failed to read IPv6 address");
5949
0
    return -1;
5950
0
  }
5951
5952
433
  split = strchr(id, '/');
5953
433
  if (!split) {
5954
0
    yyerror2("invalid IPv6 CIDR notation: %s", id);
5955
0
    free(id);
5956
0
    return -1;
5957
0
  }
5958
433
  *split = '\0';
5959
5960
433
  rc = inet_pton(AF_INET6, id, &addr);
5961
433
  if (rc < 1) {
5962
2
    yyerror2("failed to parse IPv6 address %s", id);
5963
2
    free(id);
5964
2
    return -1;
5965
2
  }
5966
5967
433
  errno = 0;
5968
431
  mask_bits = strtoul(split + 1, &endptr, 10);
5969
431
  if (errno || *endptr != '\0' || mask_bits > 128) {
5970
2
    yyerror2("invalid mask in IPv6 CIDR notation: %s", split + 1);
5971
2
    free(id);
5972
2
    return -1;
5973
2
  }
5974
5975
429
  if (mask_bits == 0) {
5976
17
    yywarn("IPv6 CIDR mask of 0, matching all IPs");
5977
17
  }
5978
5979
429
  ipv6_cidr_bits_to_mask(mask_bits, &mask);
5980
5981
429
  if (ipv6_has_host_bits_set(&addr, &mask)) {
5982
14
    yywarn("host bits in ipv6 address set");
5983
14
  }
5984
5985
429
  free(id);
5986
5987
429
  newc = calloc(1, sizeof(ocontext_t));
5988
429
  if (!newc) {
5989
0
    yyerror("out of memory");
5990
0
    return -1;
5991
0
  }
5992
5993
429
  ipv6_apply_mask(&addr, &mask);
5994
429
  memcpy(&newc->u.node6.addr[0], &addr.s6_addr[0], 16);
5995
429
  memcpy(&newc->u.node6.mask[0], &mask.s6_addr[0], 16);
5996
5997
429
  if (parse_security_context(&newc->context[0])) {
5998
34
    free(newc);
5999
34
    return -1;
6000
34
  }
6001
6002
395
  return insert_ipv6_node(newc);
6003
429
}
6004
6005
int define_fs_use(int behavior)
6006
338
{
6007
338
  ocontext_t *newc, *c, *head;
6008
6009
338
  if (policydbp->target_platform != SEPOL_TARGET_SELINUX) {
6010
3
    yyerror("fsuse not supported for target");
6011
3
    return -1;
6012
3
  }
6013
6014
335
  if (pass == 1) {
6015
334
    free(queue_remove(id_queue));
6016
334
    parse_security_context(NULL);
6017
334
    return 0;
6018
334
  }
6019
6020
1
  newc = calloc(1, sizeof(ocontext_t));
6021
1
  if (!newc) {
6022
0
    yyerror("out of memory");
6023
0
    return -1;
6024
0
  }
6025
6026
1
  newc->u.name = (char *)queue_remove(id_queue);
6027
1
  if (!newc->u.name) {
6028
0
    free(newc);
6029
0
    return -1;
6030
0
  }
6031
1
  newc->v.behavior = behavior;
6032
1
  if (parse_security_context(&newc->context[0])) {
6033
1
    free(newc->u.name);
6034
1
    free(newc);
6035
1
    return -1;
6036
1
  }
6037
6038
0
  head = policydbp->ocontexts[OCON_FSUSE];
6039
6040
0
  for (c = head; c; c = c->next) {
6041
0
    if (!strcmp(newc->u.name, c->u.name)) {
6042
0
      yyerror2(
6043
0
        "duplicate fs_use entry for filesystem type %s",
6044
0
        newc->u.name);
6045
0
      context_destroy(&newc->context[0]);
6046
0
      free(newc->u.name);
6047
0
      free(newc);
6048
0
      return -1;
6049
0
    }
6050
0
  }
6051
6052
0
  newc->next = head;
6053
0
  policydbp->ocontexts[OCON_FSUSE] = newc;
6054
0
  return 0;
6055
0
}
6056
6057
static int define_genfs_context_helper(char *fstype, int has_type)
6058
4.91k
{
6059
4.91k
  struct genfs *genfs_p, *genfs, *newgenfs;
6060
4.91k
  ocontext_t *newc, *c, *head, *p;
6061
4.91k
  class_datum_t *cladatum;
6062
4.91k
  char *type = NULL;
6063
4.91k
  const char *sclass;
6064
4.91k
  size_t len, len2;
6065
4.91k
  int wildcard = ebitmap_get_bit(&policydbp->policycaps,
6066
4.91k
               POLICYDB_CAP_GENFS_SECLABEL_WILDCARD);
6067
6068
4.91k
  if (policydbp->target_platform != SEPOL_TARGET_SELINUX) {
6069
3
    yyerror("genfs not supported for target");
6070
3
    return -1;
6071
3
  }
6072
6073
4.90k
  if (pass == 1) {
6074
2.96k
    free(fstype);
6075
2.96k
    free(queue_remove(id_queue));
6076
2.96k
    if (has_type)
6077
748
      free(queue_remove(id_queue));
6078
2.96k
    parse_security_context(NULL);
6079
2.96k
    return 0;
6080
2.96k
  }
6081
6082
3.40k
  for (genfs_p = NULL, genfs = policydbp->genfs; genfs;
6083
2.64k
       genfs_p = genfs, genfs = genfs->next) {
6084
2.64k
    if (strcmp(fstype, genfs->fstype) <= 0)
6085
1.17k
      break;
6086
2.64k
  }
6087
6088
1.94k
  if (!genfs || strcmp(fstype, genfs->fstype)) {
6089
1.21k
    newgenfs = calloc(1, sizeof(struct genfs));
6090
1.21k
    if (!newgenfs) {
6091
0
      yyerror("out of memory");
6092
0
      return -1;
6093
0
    }
6094
1.21k
    newgenfs->fstype = fstype;
6095
    /* ownership transferred into the genfs list; do not free on error */
6096
1.21k
    fstype = NULL;
6097
1.21k
    newgenfs->next = genfs;
6098
1.21k
    if (genfs_p)
6099
408
      genfs_p->next = newgenfs;
6100
811
    else
6101
811
      policydbp->genfs = newgenfs;
6102
1.21k
    genfs = newgenfs;
6103
1.21k
  } else {
6104
724
    free(fstype);
6105
724
    fstype = NULL;
6106
724
  }
6107
6108
1.94k
  newc = calloc(1, sizeof(ocontext_t));
6109
1.94k
  if (!newc) {
6110
0
    yyerror("out of memory");
6111
0
    return -1;
6112
0
  }
6113
6114
1.94k
  newc->u.name = (char *)queue_remove(id_queue);
6115
1.94k
  if (!newc->u.name)
6116
1
    goto fail;
6117
6118
1.94k
  if (wildcard) {
6119
0
    size_t name_len = strlen(newc->u.name);
6120
0
    newc->u.name = realloc(newc->u.name, name_len + 2);
6121
0
    if (newc->u.name == NULL) {
6122
0
      yyerror("out of memory");
6123
0
      return -1;
6124
0
    }
6125
6126
0
    newc->u.name[name_len] = '*';
6127
0
    newc->u.name[name_len + 1] = '\0';
6128
0
  }
6129
6130
1.94k
  if (has_type) {
6131
322
    type = (char *)queue_remove(id_queue);
6132
322
    if (!type)
6133
0
      goto fail;
6134
322
    if (type[1] != 0) {
6135
9
      yyerror2("invalid type %s", type);
6136
9
      goto fail;
6137
9
    }
6138
313
    switch (type[0]) {
6139
304
    case 'b':
6140
304
      sclass = "blk_file";
6141
304
      break;
6142
1
    case 'c':
6143
1
      sclass = "chr_file";
6144
1
      break;
6145
1
    case 'd':
6146
1
      sclass = "dir";
6147
1
      break;
6148
1
    case 'p':
6149
1
      sclass = "fifo_file";
6150
1
      break;
6151
1
    case 'l':
6152
1
      sclass = "lnk_file";
6153
1
      break;
6154
1
    case 's':
6155
1
      sclass = "sock_file";
6156
1
      break;
6157
1
    case '-':
6158
1
      sclass = "file";
6159
1
      break;
6160
3
    default:
6161
3
      yyerror2("invalid type %s", type);
6162
3
      goto fail;
6163
313
    }
6164
6165
310
    cladatum = hashtab_search(policydbp->p_classes.table, sclass);
6166
310
    if (!cladatum) {
6167
6
      yyerror2("could not find class %s for "
6168
6
         "genfscon statement",
6169
6
         sclass);
6170
6
      goto fail;
6171
6
    }
6172
304
    newc->v.sclass = cladatum->s.value;
6173
304
  }
6174
1.92k
  if (parse_security_context(&newc->context[0]))
6175
287
    goto fail;
6176
6177
1.63k
  head = genfs->head;
6178
6179
3.29k
  for (p = NULL, c = head; c; p = c, c = c->next) {
6180
2.19k
    if (!strcmp(newc->u.name, c->u.name) &&
6181
7
        (!newc->v.sclass || !c->v.sclass ||
6182
7
         newc->v.sclass == c->v.sclass)) {
6183
7
      yyerror2("duplicate entry for genfs entry (%s, %s)",
6184
7
         genfs->fstype, newc->u.name);
6185
7
      goto fail;
6186
7
    }
6187
2.18k
    len = strlen(newc->u.name);
6188
2.18k
    len2 = strlen(c->u.name);
6189
2.18k
    if (len > len2)
6190
534
      break;
6191
2.18k
  }
6192
6193
1.63k
  newc->next = c;
6194
1.63k
  if (p)
6195
480
    p->next = newc;
6196
1.15k
  else
6197
1.15k
    genfs->head = newc;
6198
1.63k
  free(type);
6199
1.63k
  return 0;
6200
313
fail:
6201
313
  free(type);
6202
313
  context_destroy(&newc->context[0]);
6203
313
  free(fstype);
6204
313
  free(newc->u.name);
6205
313
  free(newc);
6206
313
  return -1;
6207
1.63k
}
6208
6209
int define_genfs_context(int has_type)
6210
4.91k
{
6211
4.91k
  return define_genfs_context_helper(queue_remove(id_queue), has_type);
6212
4.91k
}
6213
6214
int define_range_trans(int class_specified)
6215
2.95k
{
6216
2.95k
  char *id;
6217
2.95k
  level_datum_t *levdatum = NULL;
6218
2.95k
  class_datum_t *cladatum;
6219
2.95k
  range_trans_rule_t *rule;
6220
2.95k
  int l, add = 1;
6221
6222
2.95k
  if (!mlspol) {
6223
2
    yyerror("range_transition rule in non-MLS configuration");
6224
2
    return -1;
6225
2
  }
6226
6227
2.95k
  if (pass == 1) {
6228
6.12k
    while ((id = queue_remove(id_queue)))
6229
3.84k
      free(id);
6230
12.4k
    while ((id = queue_remove(id_queue)))
6231
10.1k
      free(id);
6232
2.28k
    if (class_specified)
6233
3.94k
      while ((id = queue_remove(id_queue)))
6234
2.56k
        free(id);
6235
2.28k
    id = queue_remove(id_queue);
6236
2.28k
    free(id);
6237
3.23k
    for (l = 0; l < 2; l++) {
6238
4.58k
      while ((id = queue_remove(id_queue))) {
6239
1.34k
        free(id);
6240
1.34k
      }
6241
3.23k
      id = queue_remove(id_queue);
6242
3.23k
      if (!id)
6243
2.28k
        break;
6244
956
      free(id);
6245
956
    }
6246
2.28k
    return 0;
6247
2.28k
  }
6248
6249
671
  rule = malloc(sizeof(struct range_trans_rule));
6250
671
  if (!rule) {
6251
0
    yyerror("out of memory");
6252
0
    return -1;
6253
0
  }
6254
671
  range_trans_rule_init(rule);
6255
6256
1.34k
  while ((id = queue_remove(id_queue))) {
6257
671
    if (set_types(&rule->stypes, id, &add, 0))
6258
1
      goto out;
6259
671
  }
6260
670
  add = 1;
6261
8.80k
  while ((id = queue_remove(id_queue))) {
6262
8.13k
    if (set_types(&rule->ttypes, id, &add, 0))
6263
2
      goto out;
6264
8.13k
  }
6265
6266
668
  if (class_specified) {
6267
664
    if (read_classes(&rule->tclasses))
6268
4
      goto out;
6269
664
  } else {
6270
4
    cladatum =
6271
4
      hashtab_search(policydbp->p_classes.table, "process");
6272
4
    if (!cladatum) {
6273
3
      yyerror2("could not find process class for "
6274
3
         "legacy range_transition statement");
6275
3
      goto out;
6276
3
    }
6277
6278
1
    if (ebitmap_set_bit(&rule->tclasses, cladatum->s.value - 1,
6279
1
            TRUE)) {
6280
0
      yyerror("out of memory");
6281
0
      goto out;
6282
0
    }
6283
1
  }
6284
6285
661
  id = (char *)queue_remove(id_queue);
6286
661
  if (!id) {
6287
0
    yyerror("no range in range_transition definition?");
6288
0
    goto out;
6289
0
  }
6290
661
  for (l = 0; l < 2; l++) {
6291
661
    levdatum = hashtab_search(policydbp->p_levels.table, id);
6292
661
    if (!levdatum) {
6293
2
      yyerror2("unknown level %s used in range_transition "
6294
2
         "definition",
6295
2
         id);
6296
2
      free(id);
6297
2
      goto out;
6298
2
    }
6299
659
    free(id);
6300
6301
659
    rule->trange.level[l].sens = levdatum->level->sens;
6302
6303
659
    while ((id = queue_remove(id_queue))) {
6304
0
      if (parse_semantic_categories(
6305
0
            id, levdatum, &rule->trange.level[l].cat)) {
6306
0
        free(id);
6307
0
        goto out;
6308
0
      }
6309
0
      free(id);
6310
0
    }
6311
6312
659
    id = (char *)queue_remove(id_queue);
6313
659
    if (!id)
6314
659
      break;
6315
659
  }
6316
659
  if (l == 0) {
6317
659
    if (mls_semantic_level_cpy(&rule->trange.level[1],
6318
659
             &rule->trange.level[0])) {
6319
0
      yyerror("out of memory");
6320
0
      goto out;
6321
0
    }
6322
659
  }
6323
6324
659
  append_range_trans(rule);
6325
659
  return 0;
6326
6327
12
out:
6328
12
  range_trans_rule_destroy(rule);
6329
12
  free(rule);
6330
12
  return -1;
6331
659
}
6332
6333
/* FLASK */