Coverage Report

Created: 2025-11-09 06:43

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/selinux/checkpolicy/module_compiler.c
Line
Count
Source
1
/* Author : Joshua Brindle <jbrindle@tresys.com>
2
 *      Karl MacMillan <kmacmillan@tresys.com>
3
 *          Jason Tang     <jtang@tresys.com>
4
 *  Added support for binary policy modules
5
 *
6
 * Copyright (C) 2004 - 2005 Tresys Technology, LLC
7
 *  This program is free software; you can redistribute it and/or modify
8
 *    it under the terms of the GNU General Public License as published by
9
 *  the Free Software Foundation, version 2.
10
 */
11
12
#include <assert.h>
13
#include <stdarg.h>
14
#include <stdlib.h>
15
#include <string.h>
16
17
#include <sepol/policydb/policydb.h>
18
#include <sepol/policydb/avrule_block.h>
19
#include <sepol/policydb/conditional.h>
20
21
#include "queue.h"
22
#include "module_compiler.h"
23
24
typedef struct scope_stack {
25
  int type;   /* 1 = avrule block, 2 = conditional */
26
  avrule_decl_t *decl;  /* if in an avrule block, which
27
         * declaration is current */
28
  avrule_t *last_avrule;
29
  int in_else;    /* if in an avrule block, within ELSE branch */
30
  int require_given;  /* 1 if this block had at least one require */
31
  struct scope_stack *parent;
32
} scope_stack_t;
33
34
extern policydb_t *policydbp;
35
extern queue_t id_queue;
36
extern int yyerror(const char *msg);
37
__attribute__ ((format(printf, 1, 2)))
38
extern void yyerror2(const char *fmt, ...);
39
40
static int push_stack(int stack_type, ...);
41
static void pop_stack(void);
42
43
/* keep track of the last item added to the stack */
44
static scope_stack_t *stack_top = NULL;
45
static avrule_block_t *last_block;
46
static uint32_t next_decl_id = 1;
47
48
static const char * const flavor_str[SYM_NUM] = {
49
  [SYM_COMMONS] = "common",
50
  [SYM_CLASSES] = "class",
51
  [SYM_ROLES] = "role",
52
  [SYM_TYPES] = "type",
53
  [SYM_USERS] = "user",
54
  [SYM_BOOLS] = "bool",
55
  [SYM_LEVELS] = "level",
56
  [SYM_CATS] = "cat"
57
};
58
59
static void print_error_msg(int ret, uint32_t symbol_type)
60
17
{
61
17
  switch (ret) {
62
0
  case -3:
63
0
    yyerror("Out of memory!");
64
0
    break;
65
7
  case -2:
66
7
    yyerror2("Duplicate declaration of %s", flavor_str[symbol_type]);
67
7
    break;
68
10
  case -1:
69
10
    yyerror2("Could not declare %s here", flavor_str[symbol_type]);
70
10
    break;
71
0
  default:
72
0
    yyerror2("Unknown error %d", ret);
73
17
  }
74
17
}
75
76
int define_policy(int pass, int module_header_given)
77
13.2k
{
78
13.2k
  char *id;
79
80
13.2k
  if (module_header_given) {
81
1
    if (policydbp->policy_type != POLICY_MOD) {
82
1
      yyerror
83
1
          ("Module specification found while not building a policy module.");
84
1
      return -1;
85
1
    }
86
87
0
    if (pass == 2) {
88
0
      while ((id = queue_remove(id_queue)) != NULL)
89
0
        free(id);
90
0
    } else {
91
0
      id = (char *)queue_remove(id_queue);
92
0
      if (!id) {
93
0
        yyerror("no module name");
94
0
        return -1;
95
0
      }
96
0
      free(policydbp->name);
97
0
      policydbp->name = id;
98
0
      if ((policydbp->version =
99
0
           queue_remove(id_queue)) == NULL) {
100
0
        yyerror
101
0
            ("Expected a module version but none was found.");
102
0
        return -1;
103
0
      }
104
0
    }
105
13.2k
  } else {
106
13.2k
    if (policydbp->policy_type == POLICY_MOD) {
107
0
      yyerror
108
0
          ("Building a policy module, but no module specification found.");
109
0
      return -1;
110
0
    }
111
13.2k
  }
112
  /* the first declaration within the global avrule
113
     block will always have an id of 1 */
114
13.2k
  next_decl_id = 2;
115
116
  /* reset the scoping stack */
117
18.0k
  while (stack_top != NULL) {
118
4.82k
    pop_stack();
119
4.82k
  }
120
13.2k
  if (push_stack(1, policydbp->global, policydbp->global->branch_list) ==
121
13.2k
      -1) {
122
0
    return -1;
123
0
  }
124
13.2k
  last_block = policydbp->global;
125
13.2k
  return 0;
126
13.2k
}
127
128
/* Given the current parse stack, returns 1 if a declaration or require would
129
 * be allowed here or 0 if not.  For example, declarations and requirements are
130
 * not allowed in conditionals, so if there are any conditionals in the
131
 * current scope stack then this would return a 0.
132
 */
133
static int is_creation_allowed(void)
134
439k
{
135
439k
  if (stack_top->type != 1 || stack_top->in_else) {
136
12
    return 0;
137
12
  }
138
439k
  return 1;
139
439k
}
140
141
/* Attempt to declare or require a symbol within the current scope.
142
 * Returns:
143
 *  0: Success - Symbol had not been previously created.
144
 *  1: Success - Symbol had already been created and caller must free datum.
145
 * -1: Failure - Symbol cannot be created here
146
 * -2: Failure - Duplicate declaration or type/attribute mismatch
147
 * -3: Failure - Out of memory or some other error
148
 */
149
static int create_symbol(uint32_t symbol_type, hashtab_key_t key, hashtab_datum_t datum,
150
       uint32_t * dest_value, uint32_t scope)
151
439k
{
152
439k
  avrule_decl_t *decl = stack_top->decl;
153
439k
  int ret;
154
155
439k
  if (!is_creation_allowed()) {
156
12
    return -1;
157
12
  }
158
159
439k
  ret = symtab_insert(policydbp, symbol_type, key, datum, scope,
160
439k
          decl->decl_id, dest_value);
161
162
439k
  if (ret == 1 && dest_value) {
163
336k
    hashtab_datum_t s =
164
336k
      hashtab_search(policydbp->symtab[symbol_type].table,
165
336k
               key);
166
336k
    assert(s != NULL);
167
168
336k
    if (symbol_type == SYM_LEVELS) {
169
30.5k
      *dest_value = ((level_datum_t *)s)->level->sens;
170
305k
    } else {
171
305k
      *dest_value = ((symtab_datum_t *)s)->value;
172
305k
    }
173
336k
  } else if (ret == -2) {
174
22
    return -2;
175
103k
  } else if (ret < 0) {
176
0
    return -3;
177
0
  }
178
179
439k
  return ret;
180
439k
}
181
182
/* Attempt to declare a symbol within the current declaration.  If
183
 * currently within a non-conditional and in a non-else branch then
184
 * insert the symbol, return 0 on success if symbol was undeclared.
185
 * For roles and users, it is legal to have multiple declarations; as
186
 * such return 1 to indicate that caller must free() the datum because
187
 * it was not added.  If symbols may not be declared here return -1.
188
 * For duplicate declarations return -2.  For all else, including out
189
 * of memory, return -3.  Note that dest_value and datum_value might
190
 * not be restricted pointers. */
191
int declare_symbol(uint32_t symbol_type,
192
       hashtab_key_t key, hashtab_datum_t datum,
193
       uint32_t * dest_value, const uint32_t * datum_value)
194
189k
{
195
189k
  avrule_decl_t *decl = stack_top->decl;
196
189k
  int ret = create_symbol(symbol_type, key, datum, dest_value, SCOPE_DECL);
197
198
189k
  if (ret < 0) {
199
27
    return ret;
200
27
  }
201
202
189k
  if (ebitmap_set_bit(decl->declared.scope + symbol_type,
203
189k
          *datum_value - 1, 1)) {
204
0
    return -3;
205
0
  }
206
207
189k
  return ret;
208
189k
}
209
210
static int role_implicit_bounds(hashtab_t roles_tab,
211
        char *role_id, role_datum_t *role)
212
122k
{
213
122k
  role_datum_t *bounds;
214
122k
  char *bounds_id, *delim;
215
216
122k
  delim = strrchr(role_id, '.');
217
122k
  if (!delim)
218
121k
    return 0;  /* no implicit boundary */
219
220
894
  bounds_id = strdup(role_id);
221
894
  if (!bounds_id) {
222
0
    yyerror("out of memory");
223
0
    return -1;
224
0
  }
225
894
  bounds_id[(size_t)(delim - role_id)] = '\0';
226
227
894
  bounds = hashtab_search(roles_tab, bounds_id);
228
894
  if (!bounds) {
229
1
    yyerror2("role %s doesn't exist, is implicit bounds of %s",
230
1
       bounds_id, role_id);
231
1
    free(bounds_id);
232
1
    return -1;
233
1
  }
234
235
893
  if (!role->bounds)
236
893
    role->bounds = bounds->s.value;
237
0
  else if (role->bounds != bounds->s.value) {
238
0
    yyerror2("role %s has inconsistent bounds %s/%s",
239
0
       role_id, bounds_id,
240
0
       policydbp->p_role_val_to_name[role->bounds - 1]);
241
0
    free(bounds_id);
242
0
    return -1;
243
0
  }
244
893
  free(bounds_id);
245
246
893
  return 0;
247
893
}
248
249
static int create_role(uint32_t scope, unsigned char isattr, role_datum_t **role, char **key)
250
258k
{
251
258k
  char *id = queue_remove(id_queue);
252
258k
  role_datum_t *datum = NULL;
253
258k
  int ret;
254
258k
  uint32_t value;
255
256
258k
  *role = NULL;
257
258k
  *key = NULL;
258
258k
  isattr = isattr ? ROLE_ATTRIB : ROLE_ROLE;
259
260
258k
  if (id == NULL) {
261
1
    yyerror("no role name");
262
1
    return -1;
263
1
  }
264
265
258k
  datum = malloc(sizeof(*datum));
266
258k
  if (datum == NULL) {
267
0
    yyerror("Out of memory!");
268
0
    free(id);
269
0
    return -1;
270
0
  }
271
272
258k
  role_datum_init(datum);
273
258k
  datum->flavor = isattr;
274
275
258k
  if (scope == SCOPE_DECL) {
276
160k
    ret = declare_symbol(SYM_ROLES, id, datum, &value, &value);
277
160k
  } else {
278
98.1k
    ret = require_symbol(SYM_ROLES, id, datum, &value, &value);
279
98.1k
  }
280
281
258k
  if (ret == 0) {
282
38.9k
    datum->s.value = value;
283
38.9k
    *role = datum;
284
38.9k
    *key = strdup(id);
285
38.9k
    if (*key == NULL) {
286
0
      yyerror("Out of memory!");
287
0
      return -1;
288
0
    }
289
219k
  } else if (ret == 1) {
290
219k
    *role = hashtab_search(policydbp->symtab[SYM_ROLES].table, id);
291
219k
    if (*role && (isattr != (*role)->flavor)) {
292
5
      yyerror2("Identifier %s used as both an attribute and a role",
293
5
         id);
294
5
      *role = NULL;
295
5
      free(id);
296
5
      role_datum_destroy(datum);
297
5
      free(datum);
298
5
      return -1;
299
5
    }
300
219k
    datum->s.value = value;
301
219k
    *role = datum;
302
219k
    *key = id;
303
219k
  } else {
304
4
    print_error_msg(ret, SYM_ROLES);
305
4
    free(id);
306
4
    role_datum_destroy(datum);
307
4
    free(datum);
308
4
  }
309
310
258k
  return ret;
311
258k
}
312
313
role_datum_t *declare_role(unsigned char isattr)
314
160k
{
315
160k
  char *key = NULL;
316
160k
  role_datum_t *role = NULL;
317
160k
  role_datum_t *dest_role = NULL;
318
160k
  hashtab_t roles_tab;
319
160k
  int ret, ret2;
320
321
160k
  ret = create_role(SCOPE_DECL, isattr, &role, &key);
322
160k
  if (ret < 0) {
323
5
    return NULL;
324
5
  }
325
326
  /* create a new role_datum_t for this decl, if necessary */
327
160k
  assert(stack_top->type == 1);
328
329
160k
  if (stack_top->parent == NULL) {
330
    /* in parent, so use global symbol table */
331
31.8k
    roles_tab = policydbp->p_roles.table;
332
128k
  } else {
333
128k
    roles_tab = stack_top->decl->p_roles.table;
334
128k
  }
335
336
160k
  dest_role = hashtab_search(roles_tab, key);
337
160k
  if (dest_role == NULL) {
338
122k
    if (ret == 0) {
339
6.57k
      dest_role = malloc(sizeof(*dest_role));
340
6.57k
      if (dest_role == NULL) {
341
0
        yyerror("Out of memory!");
342
0
        free(key);
343
0
        return NULL;
344
0
      }
345
6.57k
      role_datum_init(dest_role);
346
6.57k
      dest_role->s.value = role->s.value;
347
6.57k
      dest_role->flavor = role->flavor;
348
115k
    } else {
349
115k
      dest_role = role;
350
115k
    }
351
122k
    ret2 = role_implicit_bounds(roles_tab, key, dest_role);
352
122k
    if (ret2 != 0) {
353
1
      free(key);
354
1
      role_datum_destroy(dest_role);
355
1
      free(dest_role);
356
1
      return NULL;
357
1
    }
358
122k
    ret2 = hashtab_insert(roles_tab, key, dest_role);
359
122k
    if (ret2 != 0) {
360
0
      yyerror("Out of memory!");
361
0
      free(key);
362
0
      role_datum_destroy(dest_role);
363
0
      free(dest_role);
364
0
      return NULL;
365
0
    }
366
122k
  } else {
367
38.1k
    free(key);
368
38.1k
    if (ret == 1) {
369
15.3k
      role_datum_destroy(role);
370
15.3k
      free(role);
371
15.3k
    }
372
38.1k
  }
373
374
160k
  if (ret == 0) {
375
29.3k
    ret2 = ebitmap_set_bit(&dest_role->dominates, dest_role->s.value - 1, 1);
376
29.3k
    if (ret2 != 0) {
377
0
      yyerror("out of memory");
378
0
      return NULL;
379
0
    }
380
29.3k
  }
381
382
160k
  return dest_role;
383
160k
}
384
385
static int create_type(uint32_t scope, unsigned char isattr, type_datum_t **type)
386
61.7k
{
387
61.7k
  char *id;
388
61.7k
  type_datum_t *datum;
389
61.7k
  int ret;
390
61.7k
  uint32_t value = 0;
391
392
61.7k
  *type = NULL;
393
61.7k
  isattr = isattr ? TYPE_ATTRIB : TYPE_TYPE;
394
395
61.7k
  id = (char *)queue_remove(id_queue);
396
61.7k
  if (!id) {
397
1
    yyerror("no type/attribute name?");
398
1
    return -1;
399
1
  }
400
61.7k
  if (strcmp(id, "self") == 0) {
401
4
    yyerror("\"self\" is a reserved type name.");
402
4
    free(id);
403
4
    return -1;
404
4
  }
405
406
61.7k
  datum = malloc(sizeof(*datum));
407
61.7k
  if (!datum) {
408
0
    yyerror("Out of memory!");
409
0
    free(id);
410
0
    return -1;
411
0
  }
412
61.7k
  type_datum_init(datum);
413
61.7k
  datum->primary = 1;
414
61.7k
  datum->flavor = isattr;
415
416
61.7k
  if (scope == SCOPE_DECL) {
417
2.56k
    ret = declare_symbol(SYM_TYPES, id, datum, &value, &value);
418
59.1k
  } else {
419
59.1k
    ret = require_symbol(SYM_TYPES, id, datum, &value, &value);
420
59.1k
  }
421
422
61.7k
  if (ret == 0) {
423
24.1k
    datum->s.value = value;
424
24.1k
    *type = datum;
425
37.6k
  } else if (ret == 1) {
426
37.6k
    type_datum_destroy(datum);
427
37.6k
    free(datum);
428
37.6k
    *type = hashtab_search(policydbp->symtab[SYM_TYPES].table, id);
429
37.6k
    if (*type && (isattr != (*type)->flavor)) {
430
4
      yyerror2("Identifier %s used as both an attribute and a type",
431
4
         id);
432
4
      *type = NULL;
433
4
      free(id);
434
4
      return -1;
435
4
    }
436
37.6k
    free(id);
437
37.6k
  } else {
438
7
    print_error_msg(ret, SYM_TYPES);
439
7
    free(id);
440
7
    type_datum_destroy(datum);
441
7
    free(datum);
442
7
  }
443
444
61.7k
  return ret;
445
61.7k
}
446
447
type_datum_t *declare_type(unsigned char primary, unsigned char isattr)
448
2.56k
{
449
2.56k
  type_datum_t *type = NULL;
450
2.56k
  int ret = create_type(SCOPE_DECL, isattr, &type);
451
452
2.56k
  if (ret == 0) {
453
2.53k
    type->primary = primary;
454
2.53k
  }
455
456
2.56k
  return type;
457
2.56k
}
458
459
static int user_implicit_bounds(hashtab_t users_tab,
460
        char *user_id, user_datum_t *user)
461
2.86k
{
462
2.86k
  user_datum_t *bounds;
463
2.86k
  char *bounds_id, *delim;
464
465
2.86k
  delim = strrchr(user_id, '.');
466
2.86k
  if (!delim)
467
2.61k
    return 0;  /* no implicit boundary */
468
469
256
  bounds_id = strdup(user_id);
470
256
  if (!bounds_id) {
471
0
    yyerror("out of memory");
472
0
    return -1;
473
0
  }
474
256
  bounds_id[(size_t)(delim - user_id)] = '\0';
475
476
256
  bounds = hashtab_search(users_tab, bounds_id);
477
256
  if (!bounds) {
478
2
    yyerror2("user %s doesn't exist, is implicit bounds of %s",
479
2
       bounds_id, user_id);
480
2
    free(bounds_id);
481
2
    return -1;
482
2
  }
483
484
254
  if (!user->bounds)
485
254
    user->bounds = bounds->s.value;
486
0
  else if (user->bounds != bounds->s.value) {
487
0
    yyerror2("user %s has inconsistent bounds %s/%s",
488
0
       user_id, bounds_id,
489
0
       policydbp->p_role_val_to_name[user->bounds - 1]);
490
0
    free(bounds_id);
491
0
    return -1;
492
0
  }
493
254
  free(bounds_id);
494
495
254
  return 0;
496
254
}
497
498
static int create_user(uint32_t scope, user_datum_t **user, char **key)
499
14.8k
{
500
14.8k
  char *id = queue_remove(id_queue);
501
14.8k
  user_datum_t *datum = NULL;
502
14.8k
  int ret;
503
14.8k
  uint32_t value;
504
505
14.8k
  *user = NULL;
506
14.8k
  *key = NULL;
507
508
14.8k
  if (id == NULL) {
509
1
    yyerror("no user name");
510
1
    return -1;
511
1
  }
512
513
14.8k
  datum = malloc(sizeof(*datum));
514
14.8k
  if (datum == NULL) {
515
0
    yyerror("Out of memory!");
516
0
    free(id);
517
0
    return -1;
518
0
  }
519
520
14.8k
  user_datum_init(datum);
521
522
14.8k
  if (scope == SCOPE_DECL) {
523
9.14k
    ret = declare_symbol(SYM_USERS, id, datum, &value, &value);
524
9.14k
  } else {
525
5.68k
    ret = require_symbol(SYM_USERS, id, datum, &value, &value);
526
5.68k
  }
527
528
14.8k
  if (ret == 0) {
529
8.89k
    datum->s.value = value;
530
8.89k
    *user = datum;
531
8.89k
    *key = strdup(id);
532
8.89k
    if (*key == NULL) {
533
0
      yyerror("Out of memory!");
534
0
      return -1;
535
0
    }
536
8.89k
  } else if (ret == 1) {
537
5.94k
    datum->s.value = value;
538
5.94k
    *user = datum;
539
5.94k
    *key = id;
540
5.94k
  } else {
541
2
    print_error_msg(ret, SYM_USERS);
542
2
    free(id);
543
2
    user_datum_destroy(datum);
544
2
    free(datum);
545
2
  }
546
547
14.8k
  return ret;
548
14.8k
}
549
550
user_datum_t *declare_user(void)
551
9.14k
{
552
9.14k
  char *key = NULL;
553
9.14k
  user_datum_t *user = NULL;
554
9.14k
  user_datum_t *dest_user = NULL;
555
9.14k
  hashtab_t users_tab;
556
9.14k
  int ret, ret2;
557
558
9.14k
  ret = create_user(SCOPE_DECL, &user, &key);
559
9.14k
  if (ret < 0) {
560
1
    return NULL;
561
1
  }
562
563
  /* create a new user_datum_t for this decl, if necessary */
564
9.14k
  assert(stack_top->type == 1);
565
566
9.14k
  if (stack_top->parent == NULL) {
567
    /* in parent, so use global symbol table */
568
5.01k
    users_tab = policydbp->p_users.table;
569
5.01k
  } else {
570
4.13k
    users_tab = stack_top->decl->p_users.table;
571
4.13k
  }
572
573
9.14k
  dest_user = hashtab_search(users_tab, key);
574
9.14k
  if (dest_user == NULL) {
575
2.86k
    if (ret == 0) {
576
987
      dest_user = malloc(sizeof(*dest_user));
577
987
      if (dest_user == NULL) {
578
0
        yyerror("Out of memory!");
579
0
        free(key);
580
0
        return NULL;
581
0
      }
582
987
      user_datum_init(dest_user);
583
987
      dest_user->s.value = user->s.value;
584
1.88k
    } else {
585
1.88k
      dest_user = user;
586
1.88k
    }
587
2.86k
    ret2 = user_implicit_bounds(users_tab, key, dest_user);
588
2.86k
    if (ret2 != 0) {
589
2
      free(key);
590
2
      user_datum_destroy(dest_user);
591
2
      free(dest_user);
592
2
      return NULL;
593
2
    }
594
2.86k
    ret2 = hashtab_insert(users_tab, key, dest_user);
595
2.86k
    if (ret2 != 0) {
596
0
      yyerror("Out of memory!");
597
0
      free(key);
598
0
      user_datum_destroy(dest_user);
599
0
      free(dest_user);
600
0
      return NULL;
601
0
    }
602
6.27k
  } else {
603
6.27k
    free(key);
604
6.27k
    if (ret == 1) {
605
2.25k
      user_datum_destroy(user);
606
2.25k
      free(user);
607
2.25k
    }
608
6.27k
  }
609
610
9.14k
  return dest_user;
611
9.14k
}
612
613
/* Return a type_datum_t for the local avrule_decl with the given ID.
614
 * If it does not exist, create one with the same value as 'value'.
615
 * This function assumes that the ID is within scope.  c.f.,
616
 * is_id_in_scope().
617
 *
618
 * NOTE: this function usurps ownership of id afterwards.  The caller
619
 * shall not reference it nor free() it afterwards.
620
 */
621
type_datum_t *get_local_type(char *id, uint32_t value, unsigned char isattr)
622
1.14k
{
623
1.14k
  type_datum_t *dest_typdatum;
624
1.14k
  hashtab_t types_tab;
625
1.14k
  assert(stack_top->type == 1);
626
1.14k
  if (stack_top->parent == NULL) {
627
    /* in global, so use global symbol table */
628
729
    types_tab = policydbp->p_types.table;
629
729
  } else {
630
416
    types_tab = stack_top->decl->p_types.table;
631
416
  }
632
1.14k
  dest_typdatum = hashtab_search(types_tab, id);
633
1.14k
  if (!dest_typdatum) {
634
149
    dest_typdatum = (type_datum_t *) malloc(sizeof(type_datum_t));
635
149
    if (dest_typdatum == NULL) {
636
0
      free(id);
637
0
      return NULL;
638
0
    }
639
149
    type_datum_init(dest_typdatum);
640
149
    dest_typdatum->s.value = value;
641
149
    dest_typdatum->flavor = isattr ? TYPE_ATTRIB : TYPE_TYPE;
642
149
    dest_typdatum->primary = 1;
643
149
    if (hashtab_insert(types_tab, id, dest_typdatum)) {
644
0
      free(id);
645
0
      type_datum_destroy(dest_typdatum);
646
0
      free(dest_typdatum);
647
0
      return NULL;
648
0
    }
649
650
996
  } else {
651
996
    free(id);
652
996
    if (dest_typdatum->flavor != isattr ? TYPE_ATTRIB : TYPE_TYPE) {
653
0
      return NULL;
654
0
    }
655
996
  }
656
1.14k
  return dest_typdatum;
657
1.14k
}
658
659
/* Return a role_datum_t for the local avrule_decl with the given ID.
660
 * If it does not exist, create one with the same value as 'value'.
661
 * This function assumes that the ID is within scope.  c.f.,
662
 * is_id_in_scope().
663
 *
664
 * NOTE: this function usurps ownership of id afterwards.  The caller
665
 * shall not reference it nor free() it afterwards.
666
 */
667
role_datum_t *get_local_role(char *id, uint32_t value, unsigned char isattr)
668
13.2k
{
669
13.2k
  role_datum_t *dest_roledatum;
670
13.2k
  hashtab_t roles_tab;
671
672
13.2k
  assert(stack_top->type == 1);
673
674
13.2k
  if (stack_top->parent == NULL) {
675
    /* in global, so use global symbol table */
676
5.81k
    roles_tab = policydbp->p_roles.table;
677
7.42k
  } else {
678
7.42k
    roles_tab = stack_top->decl->p_roles.table;
679
7.42k
  }
680
681
13.2k
  dest_roledatum = hashtab_search(roles_tab, id);
682
13.2k
  if (!dest_roledatum) {
683
5.62k
    dest_roledatum = (role_datum_t *)malloc(sizeof(role_datum_t));
684
5.62k
    if (dest_roledatum == NULL) {
685
0
      free(id);
686
0
      return NULL;
687
0
    }
688
689
5.62k
    role_datum_init(dest_roledatum);
690
5.62k
    dest_roledatum->s.value = value;
691
5.62k
    dest_roledatum->flavor = isattr ? ROLE_ATTRIB : ROLE_ROLE;
692
693
5.62k
    if (hashtab_insert(roles_tab, id, dest_roledatum)) {
694
0
      free(id);
695
0
      role_datum_destroy(dest_roledatum);
696
0
      free(dest_roledatum);
697
0
      return NULL;
698
0
    }
699
7.61k
  } else {
700
7.61k
    free(id);
701
7.61k
    if (dest_roledatum->flavor != isattr ? ROLE_ATTRIB : ROLE_ROLE)
702
0
      return NULL;
703
7.61k
  }
704
  
705
13.2k
  return dest_roledatum;
706
13.2k
}
707
708
/* Attempt to require a symbol within the current scope.  If currently
709
 * within an optional (and not its else branch), add the symbol to the
710
 * required list.  Return 0 on success, 1 if caller needs to free()
711
 * datum.  If symbols may not be declared here return -1.  For duplicate
712
 * declarations return -2.  For all else, including out of memory,
713
 * return -3..  Note that dest_value and datum_value might not be
714
 * restricted pointers.
715
 */
716
int require_symbol(uint32_t symbol_type,
717
       hashtab_key_t key, hashtab_datum_t datum,
718
       uint32_t * dest_value, uint32_t * datum_value)
719
249k
{
720
249k
  avrule_decl_t *decl = stack_top->decl;
721
249k
  int ret = create_symbol(symbol_type, key, datum, dest_value, SCOPE_REQ);
722
723
249k
  if (ret < 0) {
724
7
    return ret;
725
7
  }
726
727
249k
  if (ebitmap_set_bit(decl->required.scope + symbol_type,
728
249k
          *datum_value - 1, 1)) {
729
0
    return -3;
730
0
  }
731
732
249k
  stack_top->require_given = 1;
733
249k
  return ret;
734
249k
}
735
736
int add_perm_to_class(uint32_t perm_value, uint32_t class_value)
737
17.6k
{
738
17.6k
  avrule_decl_t *decl = stack_top->decl;
739
17.6k
  scope_index_t *scope;
740
741
17.6k
  assert(perm_value >= 1);
742
17.6k
  assert(class_value >= 1);
743
17.6k
  scope = &decl->required;
744
17.6k
  if (class_value > scope->class_perms_len) {
745
10.0k
    uint32_t i;
746
10.0k
    ebitmap_t *new_map = realloc(scope->class_perms_map,
747
10.0k
               class_value * sizeof(*new_map));
748
10.0k
    if (new_map == NULL) {
749
0
      return -1;
750
0
    }
751
10.0k
    scope->class_perms_map = new_map;
752
20.9k
    for (i = scope->class_perms_len; i < class_value; i++) {
753
10.8k
      ebitmap_init(scope->class_perms_map + i);
754
10.8k
    }
755
10.0k
    scope->class_perms_len = class_value;
756
10.0k
  }
757
17.6k
  if (ebitmap_set_bit(scope->class_perms_map + class_value - 1,
758
17.6k
          perm_value - 1, 1)) {
759
0
    return -1;
760
0
  }
761
17.6k
  return 0;
762
17.6k
}
763
764
static int perm_destroy(hashtab_key_t key, hashtab_datum_t datum, void *p
765
      __attribute__ ((unused)))
766
0
{
767
0
  if (key)
768
0
    free(key);
769
0
  free(datum);
770
0
  return 0;
771
0
}
772
773
static void class_datum_destroy(class_datum_t * cladatum)
774
3.15k
{
775
3.15k
  if (cladatum != NULL) {
776
3.15k
    hashtab_map(cladatum->permissions.table, perm_destroy, NULL);
777
3.15k
    hashtab_destroy(cladatum->permissions.table);
778
3.15k
    free(cladatum);
779
3.15k
  }
780
3.15k
}
781
782
int require_class(int pass)
783
3.80k
{
784
3.80k
  char *class_id = queue_remove(id_queue);
785
3.80k
  char *perm_id = NULL;
786
3.80k
  class_datum_t *datum = NULL;
787
3.80k
  perm_datum_t *perm = NULL;
788
3.80k
  int ret;
789
790
3.80k
  if (pass == 2) {
791
637
    free(class_id);
792
1.30k
    while ((perm_id = queue_remove(id_queue)) != NULL)
793
664
      free(perm_id);
794
637
    return 0;
795
637
  }
796
797
  /* first add the class if it is not already there */
798
3.16k
  if (class_id == NULL) {
799
1
    yyerror("no class name for class definition?");
800
1
    return -1;
801
1
  }
802
803
3.16k
  if ((datum = calloc(1, sizeof(*datum))) == NULL ||
804
3.16k
      symtab_init(&datum->permissions, PERM_SYMTAB_SIZE)) {
805
0
    yyerror("Out of memory!");
806
0
    class_datum_destroy(datum);
807
0
    return -1;
808
0
  }
809
3.16k
  ret =
810
3.16k
      require_symbol(SYM_CLASSES, class_id, datum, &datum->s.value,
811
3.16k
         &datum->s.value);
812
3.16k
  if (ret < 0) {
813
1
    print_error_msg(ret, SYM_CLASSES);
814
1
    free(class_id);
815
1
    class_datum_destroy(datum);
816
1
    return -1;
817
1
  }
818
819
3.16k
  if (ret == 0) {
820
    /* a new class was added; reindex everything */
821
15
    if (policydb_index_classes(policydbp)) {
822
0
      yyerror("Out of memory!");
823
0
      return -1;
824
0
    }
825
3.15k
  } else {
826
3.15k
    class_datum_destroy(datum);
827
3.15k
    datum = hashtab_search(policydbp->p_classes.table, class_id);
828
3.15k
    assert(datum);  /* the class datum should have existed */
829
3.15k
    free(class_id);
830
3.15k
  }
831
832
  /* now add each of the permissions to this class's requirements */
833
6.53k
  while ((perm_id = queue_remove(id_queue)) != NULL) {
834
3.37k
    int allocated = 0;
835
836
    /* Is the permission already in the table? */
837
3.37k
    perm = hashtab_search(datum->permissions.table, perm_id);
838
3.37k
    if (!perm && datum->comdatum)
839
0
      perm =
840
0
          hashtab_search(datum->comdatum->permissions.table,
841
0
             perm_id);
842
3.37k
    if (perm) {
843
      /* Yes, drop the name. */
844
3.36k
      free(perm_id);
845
3.36k
    } else {
846
      /* No - allocate and insert an entry for it. */
847
10
      if (policydbp->policy_type == POLICY_BASE) {
848
10
        yyerror2
849
10
            ("Base policy - require of permission %s without prior declaration.",
850
10
             perm_id);
851
10
        free(perm_id);
852
10
        return -1;
853
10
      }
854
0
      if (datum->permissions.nprim >= PERM_SYMTAB_SIZE) {
855
0
        yyerror2("Class %s would have too many permissions "
856
0
           "to fit in an access vector with permission %s",
857
0
           policydbp->p_class_val_to_name[datum->s.value - 1],
858
0
           perm_id);
859
0
        free(perm_id);
860
0
        return -1;
861
0
      }
862
0
      allocated = 1;
863
0
      if ((perm = malloc(sizeof(*perm))) == NULL) {
864
0
        yyerror("Out of memory!");
865
0
        free(perm_id);
866
0
        return -1;
867
0
      }
868
0
      memset(perm, 0, sizeof(*perm));
869
0
      ret =
870
0
          hashtab_insert(datum->permissions.table, perm_id,
871
0
             perm);
872
0
      if (ret) {
873
0
        yyerror("Out of memory!");
874
0
        free(perm_id);
875
0
        free(perm);
876
0
        return -1;
877
0
      }
878
0
      perm->s.value = datum->permissions.nprim + 1;
879
0
    }
880
881
3.36k
    if (add_perm_to_class(perm->s.value, datum->s.value) == -1) {
882
0
      yyerror("Out of memory!");
883
0
      return -1;
884
0
    }
885
886
    /* Update number of primitives if we allocated one. */
887
3.36k
    if (allocated)
888
0
      datum->permissions.nprim++;
889
3.36k
  }
890
3.15k
  return 0;
891
3.16k
}
892
893
static int require_role_or_attribute(int pass, unsigned char isattr)
894
190k
{
895
190k
  char *key = NULL;
896
190k
  role_datum_t *role = NULL;
897
190k
  int ret;
898
899
190k
  if (pass == 2) {
900
92.7k
    free(queue_remove(id_queue));
901
92.7k
    return 0;
902
92.7k
  }
903
904
98.1k
  ret = create_role(SCOPE_REQ, isattr, &role, &key);
905
98.1k
  if (ret < 0) {
906
5
    return -1;
907
5
  }
908
909
98.1k
  free(key);
910
911
98.1k
  if (ret == 0) {
912
9.54k
    ret = ebitmap_set_bit(&role->dominates, role->s.value - 1, 1);
913
9.54k
    if (ret != 0) {
914
0
      yyerror("Out of memory");
915
0
      return -1;
916
0
    }
917
88.6k
  } else {
918
88.6k
    role_datum_destroy(role);
919
88.6k
    free(role);
920
88.6k
  }
921
922
98.1k
  return 0;
923
98.1k
}
924
925
int require_role(int pass)
926
106k
{
927
106k
  return require_role_or_attribute(pass, 0);
928
106k
}
929
930
int require_attribute_role(int pass)
931
84.5k
{
932
84.5k
  return require_role_or_attribute(pass, 1);
933
84.5k
}
934
935
static int require_type_or_attribute(int pass, unsigned char isattr)
936
107k
{
937
107k
  type_datum_t *type = NULL;
938
107k
  int ret;
939
940
107k
  if (pass == 2) {
941
48.0k
    free(queue_remove(id_queue));
942
48.0k
    return 0;
943
48.0k
  }
944
945
59.1k
  ret = create_type(SCOPE_REQ, isattr, &type);
946
947
59.1k
  if (ret < 0) {
948
6
    return -1;
949
6
  }
950
951
59.1k
  return 0;
952
59.1k
}
953
954
int require_type(int pass)
955
62.4k
{
956
62.4k
  return require_type_or_attribute(pass, 0);
957
62.4k
}
958
959
int require_attribute(int pass)
960
44.7k
{
961
44.7k
  return require_type_or_attribute(pass, 1);
962
44.7k
}
963
964
int require_user(int pass)
965
11.8k
{
966
11.8k
  char *key = NULL;
967
11.8k
  user_datum_t *user = NULL;
968
11.8k
  int ret;
969
970
11.8k
  if (pass == 1) {
971
6.18k
    free(queue_remove(id_queue));
972
6.18k
    return 0;
973
6.18k
  }
974
975
5.68k
  ret = create_user(SCOPE_REQ, &user, &key);
976
5.68k
  if (ret < 0) {
977
2
    return -1;
978
2
  }
979
980
5.68k
  free(key);
981
982
5.68k
  if (ret == 1) {
983
1.80k
    user_datum_destroy(user);
984
1.80k
    free(user);
985
1.80k
  }
986
987
5.68k
  return 0;
988
5.68k
}
989
990
static int require_bool_tunable(int pass, int is_tunable)
991
91.5k
{
992
91.5k
  char *id = queue_remove(id_queue);
993
91.5k
  cond_bool_datum_t *booldatum = NULL;
994
91.5k
  int retval;
995
91.5k
  if (pass == 2) {
996
44.6k
    free(id);
997
44.6k
    return 0;
998
44.6k
  }
999
46.8k
  if (id == NULL) {
1000
2
    yyerror("no boolean name");
1001
2
    return -1;
1002
2
  }
1003
46.8k
  if ((booldatum = calloc(1, sizeof(*booldatum))) == NULL) {
1004
0
    cond_destroy_bool(id, booldatum, NULL);
1005
0
    yyerror("Out of memory!");
1006
0
    return -1;
1007
0
  }
1008
46.8k
  if (is_tunable)
1009
797
    booldatum->flags |= COND_BOOL_FLAGS_TUNABLE;
1010
46.8k
  retval =
1011
46.8k
      require_symbol(SYM_BOOLS, id, booldatum,
1012
46.8k
         &booldatum->s.value, &booldatum->s.value);
1013
46.8k
  if (retval != 0) {
1014
38.0k
    cond_destroy_bool(id, booldatum, NULL);
1015
38.0k
    if (retval < 0) {
1016
1
      print_error_msg(retval, SYM_BOOLS);
1017
1
      return -1;
1018
1
    }
1019
38.0k
  }
1020
1021
46.8k
  return 0;
1022
46.8k
}
1023
1024
int require_bool(int pass)
1025
90.3k
{
1026
90.3k
  return require_bool_tunable(pass, 0);
1027
90.3k
}
1028
1029
int require_tunable(int pass)
1030
1.14k
{
1031
1.14k
  return require_bool_tunable(pass, 1);
1032
1.14k
}
1033
1034
int require_sens(int pass)
1035
64.7k
{
1036
64.7k
  char *id = queue_remove(id_queue);
1037
64.7k
  level_datum_t *level = NULL;
1038
64.7k
  int retval;
1039
64.7k
  if (pass == 2) {
1040
31.3k
    free(id);
1041
31.3k
    return 0;
1042
31.3k
  }
1043
33.3k
  if (!id) {
1044
1
    yyerror("no sensitivity name");
1045
1
    return -1;
1046
1
  }
1047
33.3k
  level = malloc(sizeof(level_datum_t));
1048
33.3k
  if (!level) {
1049
0
    free(id);
1050
0
    yyerror("Out of memory!");
1051
0
    return -1;
1052
0
  }
1053
33.3k
  level_datum_init(level);
1054
33.3k
  level->level = malloc(sizeof(mls_level_t));
1055
33.3k
  if (!level->level) {
1056
0
    free(id);
1057
0
    level_datum_destroy(level);
1058
0
    free(level);
1059
0
    yyerror("Out of memory!");
1060
0
    return -1;
1061
0
  }
1062
33.3k
  mls_level_init(level->level);
1063
33.3k
  retval = require_symbol(SYM_LEVELS, id, level,
1064
33.3k
        &level->level->sens, &level->level->sens);
1065
33.3k
  if (retval != 0) {
1066
30.5k
    free(id);
1067
30.5k
    mls_level_destroy(level->level);
1068
30.5k
    free(level->level);
1069
30.5k
    level_datum_destroy(level);
1070
30.5k
    free(level);
1071
30.5k
    if (retval < 0) {
1072
1
      print_error_msg(retval, SYM_LEVELS);
1073
1
      return -1;
1074
1
    }
1075
30.5k
  }
1076
1077
33.3k
  return 0;
1078
33.3k
}
1079
1080
int require_cat(int pass)
1081
4.87k
{
1082
4.87k
  char *id = queue_remove(id_queue);
1083
4.87k
  cat_datum_t *cat = NULL;
1084
4.87k
  int retval;
1085
4.87k
  if (pass == 2) {
1086
1.48k
    free(id);
1087
1.48k
    return 0;
1088
1.48k
  }
1089
3.38k
  if (!id) {
1090
1
    yyerror("no category name");
1091
1
    return -1;
1092
1
  }
1093
3.38k
  cat = malloc(sizeof(cat_datum_t));
1094
3.38k
  if (!cat) {
1095
0
    free(id);
1096
0
    yyerror("Out of memory!");
1097
0
    return -1;
1098
0
  }
1099
3.38k
  cat_datum_init(cat);
1100
1101
3.38k
  retval = require_symbol(SYM_CATS, id, cat,
1102
3.38k
        &cat->s.value, &cat->s.value);
1103
3.38k
  if (retval != 0) {
1104
1.45k
    free(id);
1105
1.45k
    cat_datum_destroy(cat);
1106
1.45k
    free(cat);
1107
1.45k
    if (retval < 0) {
1108
1
      print_error_msg(retval, SYM_CATS);
1109
1
      return -1;
1110
1
    }
1111
1.45k
  }
1112
1113
3.38k
  return 0;
1114
3.38k
}
1115
1116
static int is_scope_in_stack(const scope_datum_t * scope, const scope_stack_t * stack)
1117
816k
{
1118
816k
  uint32_t i;
1119
816k
  if (stack == NULL) {
1120
378
    return 0; /* no matching scope found */
1121
378
  }
1122
816k
  if (stack->type == 1) {
1123
816k
    const avrule_decl_t *decl = stack->decl;
1124
4.72M
    for (i = 0; i < scope->decl_ids_len; i++) {
1125
4.28M
      if (scope->decl_ids[i] == decl->decl_id) {
1126
373k
        return 1;
1127
373k
      }
1128
4.28M
    }
1129
816k
  } else {
1130
    /* note that conditionals can't declare or require
1131
     * symbols, so skip this level */
1132
0
  }
1133
1134
  /* not within scope of this stack, so try its parent */
1135
442k
  return is_scope_in_stack(scope, stack->parent);
1136
816k
}
1137
1138
int is_id_in_scope(uint32_t symbol_type, const_hashtab_key_t id)
1139
377k
{
1140
377k
  const scope_datum_t *scope =
1141
377k
      (scope_datum_t *) hashtab_search(policydbp->scope[symbol_type].
1142
377k
               table, id);
1143
377k
  if (scope == NULL) {
1144
2.98k
    return 1; /* id is not known, so return success */
1145
2.98k
  }
1146
374k
  return is_scope_in_stack(scope, stack_top);
1147
377k
}
1148
1149
static int is_perm_in_scope_index(uint32_t perm_value, uint32_t class_value,
1150
          const scope_index_t * scope)
1151
1.91k
{
1152
1.91k
  if (class_value > scope->class_perms_len) {
1153
1.39k
    return 1;
1154
1.39k
  }
1155
519
  if (ebitmap_get_bit(scope->class_perms_map + class_value - 1,
1156
519
          perm_value - 1)) {
1157
452
    return 1;
1158
452
  }
1159
67
  return 0;
1160
519
}
1161
1162
static int is_perm_in_stack(uint32_t perm_value, uint32_t class_value,
1163
          const scope_stack_t * stack)
1164
1.85k
{
1165
1.85k
  if (stack == NULL) {
1166
0
    return 0; /* no matching scope found */
1167
0
  }
1168
1.85k
  if (stack->type == 1) {
1169
1.85k
    avrule_decl_t *decl = stack->decl;
1170
1.85k
    if (is_perm_in_scope_index
1171
1.85k
        (perm_value, class_value, &decl->required)
1172
67
        || is_perm_in_scope_index(perm_value, class_value,
1173
1.85k
                &decl->declared)) {
1174
1.85k
      return 1;
1175
1.85k
    }
1176
1.85k
  } else {
1177
    /* note that conditionals can't declare or require
1178
     * symbols, so skip this level */
1179
0
  }
1180
1181
  /* not within scope of this stack, so try its parent */
1182
0
  return is_perm_in_stack(perm_value, class_value, stack->parent);
1183
1.85k
}
1184
1185
int is_perm_in_scope(const_hashtab_key_t perm_id, const_hashtab_key_t class_id)
1186
1.85k
{
1187
1.85k
  const class_datum_t *cladatum =
1188
1.85k
      (class_datum_t *) hashtab_search(policydbp->p_classes.table,
1189
1.85k
               class_id);
1190
1.85k
  const perm_datum_t *perdatum;
1191
1.85k
  if (cladatum == NULL) {
1192
0
    return 1;
1193
0
  }
1194
1.85k
  perdatum = (perm_datum_t *) hashtab_search(cladatum->permissions.table,
1195
1.85k
               perm_id);
1196
1.85k
  if (perdatum == NULL) {
1197
0
    return 1;
1198
0
  }
1199
1.85k
  return is_perm_in_stack(perdatum->s.value, cladatum->s.value,
1200
1.85k
        stack_top);
1201
1.85k
}
1202
1203
cond_list_t *get_current_cond_list(cond_list_t * cond)
1204
10.0k
{
1205
  /* FIX ME: do something different here if in a nested
1206
   * conditional? */
1207
10.0k
  avrule_decl_t *decl = stack_top->decl;
1208
10.0k
  return get_decl_cond_list(policydbp, decl, cond);
1209
10.0k
}
1210
1211
/* Append the new conditional node to the existing ones.  During
1212
 * expansion the list will be reversed -- i.e., the last AV rule will
1213
 * be the first one listed in the policy.  This matches the behavior
1214
 * of the upstream compiler. */
1215
void append_cond_list(cond_list_t * cond)
1216
5.03k
{
1217
5.03k
  cond_list_t *old_cond = get_current_cond_list(cond);
1218
5.03k
  avrule_t *tmp;
1219
5.03k
  assert(old_cond != NULL); /* probably out of memory */
1220
5.03k
  if (old_cond->avtrue_list == NULL) {
1221
3.41k
    old_cond->avtrue_list = cond->avtrue_list;
1222
3.41k
  } else {
1223
11.9k
    for (tmp = old_cond->avtrue_list; tmp->next != NULL;
1224
10.3k
         tmp = tmp->next) ;
1225
1.61k
    tmp->next = cond->avtrue_list;
1226
1.61k
  }
1227
5.03k
  if (old_cond->avfalse_list == NULL) {
1228
3.53k
    old_cond->avfalse_list = cond->avfalse_list;
1229
3.53k
  } else {
1230
132k
    for (tmp = old_cond->avfalse_list; tmp->next != NULL;
1231
130k
         tmp = tmp->next) ;
1232
1.49k
    tmp->next = cond->avfalse_list;
1233
1.49k
  }
1234
1235
5.03k
  old_cond->flags |= cond->flags;
1236
5.03k
}
1237
1238
void append_avrule(avrule_t * avrule)
1239
13.4k
{
1240
13.4k
  avrule_decl_t *decl = stack_top->decl;
1241
1242
  /* currently avrules follow a completely different code path
1243
   * for handling avrules and compute types
1244
   * (define_cond_avrule_te_avtab, define_cond_compute_type);
1245
   * therefore there ought never be a conditional on top of the
1246
   * scope stack */
1247
13.4k
  assert(stack_top->type == 1);
1248
1249
13.4k
  if (stack_top->last_avrule == NULL) {
1250
872
    decl->avrules = avrule;
1251
12.5k
  } else {
1252
12.5k
    stack_top->last_avrule->next = avrule;
1253
12.5k
  }
1254
13.4k
  stack_top->last_avrule = avrule;
1255
13.4k
}
1256
1257
/* this doesn't actually append, but really prepends it */
1258
void append_role_trans(role_trans_rule_t * role_tr_rules)
1259
1.08k
{
1260
1.08k
  avrule_decl_t *decl = stack_top->decl;
1261
1262
  /* role transitions are not allowed within conditionals */
1263
1.08k
  assert(stack_top->type == 1);
1264
1265
1.08k
  role_tr_rules->next = decl->role_tr_rules;
1266
1.08k
  decl->role_tr_rules = role_tr_rules;
1267
1.08k
}
1268
1269
/* this doesn't actually append, but really prepends it */
1270
void append_role_allow(role_allow_rule_t * role_allow_rules)
1271
5.54k
{
1272
5.54k
  avrule_decl_t *decl = stack_top->decl;
1273
1274
  /* role allows are not allowed within conditionals */
1275
5.54k
  assert(stack_top->type == 1);
1276
1277
5.54k
  role_allow_rules->next = decl->role_allow_rules;
1278
5.54k
  decl->role_allow_rules = role_allow_rules;
1279
5.54k
}
1280
1281
/* this doesn't actually append, but really prepends it */
1282
void append_filename_trans(filename_trans_rule_t * filename_trans_rules)
1283
4.10k
{
1284
4.10k
  avrule_decl_t *decl = stack_top->decl;
1285
1286
  /* filename transitions are not allowed within conditionals */
1287
4.10k
  assert(stack_top->type == 1);
1288
1289
4.10k
  filename_trans_rules->next = decl->filename_trans_rules;
1290
4.10k
  decl->filename_trans_rules = filename_trans_rules;
1291
4.10k
}
1292
1293
/* this doesn't actually append, but really prepends it */
1294
void append_range_trans(range_trans_rule_t * range_tr_rules)
1295
0
{
1296
0
  avrule_decl_t *decl = stack_top->decl;
1297
1298
  /* range transitions are not allowed within conditionals */
1299
0
  assert(stack_top->type == 1);
1300
1301
0
  range_tr_rules->next = decl->range_tr_rules;
1302
0
  decl->range_tr_rules = range_tr_rules;
1303
0
}
1304
1305
int begin_optional(int pass)
1306
140k
{
1307
140k
  avrule_block_t *block = NULL;
1308
140k
  avrule_decl_t *decl;
1309
140k
  if (pass == 1) {
1310
    /* allocate a new avrule block for this optional block */
1311
126k
    if ((block = avrule_block_create()) == NULL ||
1312
126k
        (decl = avrule_decl_create(next_decl_id)) == NULL) {
1313
0
      goto cleanup;
1314
0
    }
1315
126k
    block->flags |= AVRULE_OPTIONAL;
1316
126k
    block->branch_list = decl;
1317
126k
    last_block->next = block;
1318
126k
  } else {
1319
    /* select the next block from the chain built during pass 1 */
1320
13.8k
    block = last_block->next;
1321
13.8k
    assert(block != NULL &&
1322
13.8k
           block->branch_list != NULL &&
1323
13.8k
           block->branch_list->decl_id == next_decl_id);
1324
13.8k
    decl = block->branch_list;
1325
13.8k
  }
1326
140k
  if (push_stack(1, block, decl) == -1) {
1327
0
    goto cleanup;
1328
0
  }
1329
140k
  stack_top->last_avrule = NULL;
1330
140k
  last_block = block;
1331
140k
  next_decl_id++;
1332
140k
  return 0;
1333
0
      cleanup:
1334
0
  yyerror("Out of memory!");
1335
0
  avrule_block_destroy(block);
1336
0
  return -1;
1337
140k
}
1338
1339
int end_optional(int pass __attribute__ ((unused)))
1340
104k
{
1341
  /* once nested conditionals are allowed, do the stack unfolding here */
1342
104k
  pop_stack();
1343
104k
  return 0;
1344
104k
}
1345
1346
int begin_optional_else(int pass)
1347
1.73k
{
1348
1.73k
  avrule_decl_t *decl;
1349
1.73k
  assert(stack_top->type == 1 && stack_top->in_else == 0);
1350
1.73k
  if (pass == 1) {
1351
    /* allocate a new declaration and add it to the
1352
     * current chain */
1353
1.40k
    if ((decl = avrule_decl_create(next_decl_id)) == NULL) {
1354
0
      yyerror("Out of memory!");
1355
0
      return -1;
1356
0
    }
1357
1.40k
    stack_top->decl->next = decl;
1358
1.40k
  } else {
1359
    /* pick the (hopefully last) declaration of this
1360
       avrule block, built from pass 1 */
1361
325
    decl = stack_top->decl->next;
1362
325
    assert(decl != NULL &&
1363
325
           decl->next == NULL && decl->decl_id == next_decl_id);
1364
325
  }
1365
1.73k
  stack_top->in_else = 1;
1366
1.73k
  stack_top->decl = decl;
1367
1.73k
  stack_top->last_avrule = NULL;
1368
1.73k
  stack_top->require_given = 0;
1369
1.73k
  next_decl_id++;
1370
1.73k
  return 0;
1371
1.73k
}
1372
1373
static int copy_requirements(avrule_decl_t * dest, const scope_stack_t * stack)
1374
352k
{
1375
352k
  uint32_t i;
1376
352k
  if (stack == NULL) {
1377
10.3k
    return 0;
1378
10.3k
  }
1379
341k
  if (stack->type == 1) {
1380
341k
    const scope_index_t *src_scope = &stack->decl->required;
1381
341k
    scope_index_t *dest_scope = &dest->required;
1382
3.07M
    for (i = 0; i < SYM_NUM; i++) {
1383
2.73M
      const ebitmap_t *src_bitmap = &src_scope->scope[i];
1384
2.73M
      ebitmap_t *dest_bitmap = &dest_scope->scope[i];
1385
2.73M
      if (ebitmap_union(dest_bitmap, src_bitmap)) {
1386
0
        yyerror("Out of memory!");
1387
0
        return -1;
1388
0
      }
1389
2.73M
    }
1390
    /* now copy class permissions */
1391
341k
    if (src_scope->class_perms_len > dest_scope->class_perms_len) {
1392
10.1k
      ebitmap_t *new_map =
1393
10.1k
          realloc(dest_scope->class_perms_map,
1394
10.1k
            src_scope->class_perms_len *
1395
10.1k
            sizeof(*new_map));
1396
10.1k
      if (new_map == NULL) {
1397
0
        yyerror("Out of memory!");
1398
0
        return -1;
1399
0
      }
1400
10.1k
      dest_scope->class_perms_map = new_map;
1401
10.1k
      for (i = dest_scope->class_perms_len;
1402
28.2k
           i < src_scope->class_perms_len; i++) {
1403
18.0k
        ebitmap_init(dest_scope->class_perms_map + i);
1404
18.0k
      }
1405
10.1k
      dest_scope->class_perms_len =
1406
10.1k
          src_scope->class_perms_len;
1407
10.1k
    }
1408
360k
    for (i = 0; i < src_scope->class_perms_len; i++) {
1409
18.2k
      const ebitmap_t *src_bitmap = &src_scope->class_perms_map[i];
1410
18.2k
      ebitmap_t *dest_bitmap =
1411
18.2k
          &dest_scope->class_perms_map[i];
1412
18.2k
      if (ebitmap_union(dest_bitmap, src_bitmap)) {
1413
0
        yyerror("Out of memory!");
1414
0
        return -1;
1415
0
      }
1416
18.2k
    }
1417
341k
  }
1418
341k
  return copy_requirements(dest, stack->parent);
1419
341k
}
1420
1421
/* During pass 1, check that at least one thing was required within
1422
 * this block, for those places where a REQUIRED is necessary.  During
1423
 * pass 2, have this block inherit its parents' requirements.  Return
1424
 * 0 on success, -1 on failure. */
1425
int end_avrule_block(int pass)
1426
105k
{
1427
105k
  avrule_decl_t *decl = stack_top->decl;
1428
105k
  assert(stack_top->type == 1);
1429
105k
  if (pass == 2) {
1430
    /* this avrule_decl inherits all of its parents'
1431
     * requirements */
1432
10.3k
    if (copy_requirements(decl, stack_top->parent) == -1) {
1433
0
      return -1;
1434
0
    }
1435
10.3k
    return 0;
1436
10.3k
  }
1437
95.3k
  if (!stack_top->in_else && !stack_top->require_given) {
1438
88.0k
    if (policydbp->policy_type == POLICY_BASE
1439
88.0k
        && stack_top->parent != NULL) {
1440
      /* if this is base no require should be in the global block */
1441
88.0k
      return 0;
1442
88.0k
    } else {
1443
      /* non-ELSE branches must have at least one thing required */
1444
0
      yyerror("This block has no require section.");
1445
0
      return -1;
1446
0
    }
1447
88.0k
  }
1448
7.33k
  return 0;
1449
95.3k
}
1450
1451
/* Push a new scope on to the stack and update the 'last' pointer.
1452
 * Return 0 on success, -1 if out * of memory. */
1453
static int push_stack(int stack_type, ...)
1454
153k
{
1455
153k
  scope_stack_t *s = calloc(1, sizeof(*s));
1456
153k
  va_list ap;
1457
153k
  if (s == NULL) {
1458
0
    return -1;
1459
0
  }
1460
153k
  va_start(ap, stack_type);
1461
153k
  switch (s->type = stack_type) {
1462
153k
  case 1:{
1463
153k
      va_arg(ap, avrule_block_t *);
1464
153k
      s->decl = va_arg(ap, avrule_decl_t *);
1465
153k
      break;
1466
0
    }
1467
0
  case 2:{
1468
0
      va_arg(ap, cond_list_t *);
1469
0
      break;
1470
0
    }
1471
0
  default:
1472
    /* invalid stack type given */
1473
0
    assert(0);
1474
153k
  }
1475
153k
  va_end(ap);
1476
153k
  s->parent = stack_top;
1477
153k
  stack_top = s;
1478
153k
  return 0;
1479
153k
}
1480
1481
/* Pop off the most recently added from the stack.  Update the 'last'
1482
 * pointer. */
1483
static void pop_stack(void)
1484
153k
{
1485
153k
  scope_stack_t *parent;
1486
153k
  assert(stack_top != NULL);
1487
153k
  parent = stack_top->parent;
1488
153k
  free(stack_top);
1489
153k
  stack_top = parent;
1490
153k
}
1491
1492
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
1493
void module_compiler_reset(void)
1494
8.55k
{
1495
52.9k
  while (stack_top)
1496
44.4k
    pop_stack();
1497
1498
  last_block = NULL;
1499
8.55k
  next_decl_id = 1;
1500
8.55k
}
1501
#endif