Coverage Report

Created: 2026-02-26 06:44

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