~apparmor-dev/apparmor/master

« back to all changes in this revision

Viewing changes to parser/parser.h

  • Committer: Steve Beattie
  • Date: 2019-02-19 09:38:13 UTC
  • Revision ID: sbeattie@ubuntu.com-20190219093813-ud526ee6hwn8nljz
The AppArmor project has been converted to git and is now hosted on
gitlab.

To get the converted repository, please do
  git clone https://gitlab.com/apparmor/apparmor

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 *   Copyright (c) 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2007
3
 
 *   NOVELL (All rights reserved)
4
 
 *
5
 
 *   Copyright (c) 2010 - 2012
6
 
 *   Canonical Ltd. (All rights reserved)
7
 
 *
8
 
 *   This program is free software; you can redistribute it and/or
9
 
 *   modify it under the terms of version 2 of the GNU General Public
10
 
 *   License published by the Free Software Foundation.
11
 
 *
12
 
 *   This program is distributed in the hope that it will be useful,
13
 
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 
 *   GNU General Public License for more details.
16
 
 *
17
 
 *   You should have received a copy of the GNU General Public License
18
 
 *   along with this program; if not, contact Novell, Inc. or Canonical
19
 
 *   Ltd.
20
 
 */
21
 
 
22
 
#ifndef __AA_PARSER_H
23
 
#define __AA_PARSER_H
24
 
 
25
 
 
26
 
#include <endian.h>
27
 
#include <string.h>
28
 
#include <sys/resource.h>
29
 
 
30
 
#include <libintl.h>
31
 
#define _(s) gettext(s)
32
 
 
33
 
#include <sys/apparmor.h>
34
 
 
35
 
#include "immunix.h"
36
 
#include "libapparmor_re/apparmor_re.h"
37
 
#include "libapparmor_re/aare_rules.h"
38
 
 
39
 
#include <string>
40
 
 
41
 
using namespace std;
42
 
 
43
 
#include <set>
44
 
class Profile;
45
 
class rule_t;
46
 
 
47
 
#define MODULE_NAME "apparmor"
48
 
 
49
 
/* Global variable to pass token to lexer.  Will be replaced by parameter
50
 
 * when lexer and parser are made reentrant
51
 
 */
52
 
extern int parser_token;
53
 
 
54
 
 
55
 
#define WARN_RULE_NOT_ENFORCED  1
56
 
#define WARN_RULE_DOWNGRADED    2
57
 
 
58
 
extern dfaflags_t warnflags;
59
 
 
60
 
 
61
 
typedef enum pattern_t pattern_t;
62
 
 
63
 
struct prefixes {
64
 
        int audit;
65
 
        int deny;
66
 
        int owner;
67
 
};
68
 
 
69
 
struct cod_pattern {
70
 
        char *regex;            // posix regex
71
 
};
72
 
 
73
 
struct value_list {
74
 
        char *value;
75
 
 
76
 
        struct value_list *next;
77
 
};
78
 
 
79
 
struct cond_entry {
80
 
        char *name;
81
 
        int eq;                 /* where equals was used in specifying list */
82
 
        struct value_list *vals;
83
 
 
84
 
        struct cond_entry *next;
85
 
};
86
 
 
87
 
struct cond_entry_list {
88
 
        char *name;
89
 
 
90
 
        struct cond_entry *list;
91
 
};
92
 
 
93
 
struct cod_entry {
94
 
        char *name;
95
 
        union {
96
 
                char *link_name;
97
 
                char *onexec;
98
 
        };
99
 
        char *nt_name;
100
 
        Profile *prof;                  /* Special profile defined
101
 
                                         * just for this executable */
102
 
        int mode;                       /* mode is 'or' of AA_* bits */
103
 
        int audit;                      /* audit flags for mode */
104
 
        int deny;                       /* TRUE or FALSE */
105
 
 
106
 
        int alias_ignore;               /* ignore for alias processing */
107
 
 
108
 
        int subset;
109
 
 
110
 
        pattern_t pattern_type;
111
 
        struct cod_pattern pat;
112
 
 
113
 
        struct cod_entry *next;
114
 
};
115
 
 
116
 
struct aa_rlimits {
117
 
        unsigned int specified;                 /* limits that are set */
118
 
        rlim_t limits[RLIMIT_NLIMITS];
119
 
};
120
 
 
121
 
struct alt_name {
122
 
        char *name;
123
 
        struct alt_name *next;
124
 
};
125
 
 
126
 
struct sd_hat {
127
 
        char *hat_name;
128
 
        unsigned int hat_magic;
129
 
};
130
 
 
131
 
struct var_string {
132
 
        char *prefix;
133
 
        char *var;
134
 
        char *suffix;
135
 
};
136
 
 
137
 
#define COD_READ_CHAR           'r'
138
 
#define COD_WRITE_CHAR          'w'
139
 
#define COD_APPEND_CHAR         'a'
140
 
#define COD_EXEC_CHAR           'x'
141
 
#define COD_LINK_CHAR           'l'
142
 
#define COD_LOCK_CHAR           'k'
143
 
#define COD_MMAP_CHAR           'm'
144
 
#define COD_INHERIT_CHAR        'i'
145
 
#define COD_UNCONFINED_CHAR     'U'
146
 
#define COD_UNSAFE_UNCONFINED_CHAR      'u'
147
 
#define COD_PROFILE_CHAR        'P'
148
 
#define COD_UNSAFE_PROFILE_CHAR 'p'
149
 
#define COD_LOCAL_CHAR          'C'
150
 
#define COD_UNSAFE_LOCAL_CHAR   'c'
151
 
 
152
 
#define OPTION_ADD      1
153
 
#define OPTION_REMOVE   2
154
 
#define OPTION_REPLACE  3
155
 
#define OPTION_STDOUT   4
156
 
#define OPTION_OFILE    5
157
 
 
158
 
#define BOOL int
159
 
 
160
 
extern int preprocess_only;
161
 
 
162
 
#define PATH_CHROOT_REL 0x1
163
 
#define PATH_NS_REL 0x2
164
 
#define PATH_CHROOT_NSATTACH 0x4
165
 
#define PATH_CHROOT_NO_ATTACH 0x8
166
 
#define PATH_MEDIATE_DELETED 0x10
167
 
#define PATH_DELEGATE_DELETED 0x20
168
 
#define PATH_ATTACH 0x40
169
 
#define PATH_NO_ATTACH 0x80
170
 
 
171
 
 
172
 
 
173
 
#ifdef DEBUG
174
 
#define PDEBUG(fmt, args...) fprintf(stderr, "parser: " fmt, ## args)
175
 
#else
176
 
#define PDEBUG(fmt, args...)    /* Do nothing */
177
 
#endif
178
 
#define NPDEBUG(fmt, args...)   /* Do nothing */
179
 
 
180
 
#define PERROR(fmt, args...) fprintf(stderr, fmt, ## args)
181
 
 
182
 
#ifndef TRUE
183
 
#define TRUE    (1)
184
 
#endif
185
 
#ifndef FALSE
186
 
#define FALSE   (0)
187
 
#endif
188
 
 
189
 
#define MIN_PORT 0
190
 
#define MAX_PORT 65535
191
 
 
192
 
#ifndef unused
193
 
#define unused __attribute__ ((unused))
194
 
#endif
195
 
 
196
 
 
197
 
#define list_for_each(LIST, ENTRY) \
198
 
        for ((ENTRY) = (LIST); (ENTRY); (ENTRY) = (ENTRY)->next)
199
 
#define list_for_each_safe(LIST, ENTRY, TMP) \
200
 
        for ((ENTRY) = (LIST), (TMP) = (LIST) ? (LIST)->next : NULL; (ENTRY); (ENTRY) = (TMP), (TMP) = (TMP) ? (TMP)->next : NULL)
201
 
#define list_last_entry(LIST, ENTRY) \
202
 
        for ((ENTRY) = (LIST); (ENTRY) && (ENTRY)->next; (ENTRY) = (ENTRY)->next)
203
 
#define list_append(LISTA, LISTB)               \
204
 
        do {                                    \
205
 
                typeof(LISTA) ___tmp;           \
206
 
                list_last_entry((LISTA), ___tmp);\
207
 
                ___tmp->next = (LISTB);         \
208
 
        } while (0)
209
 
 
210
 
#define list_len(LIST)          \
211
 
({                              \
212
 
        int len = 0;            \
213
 
        typeof(LIST) tmp;               \
214
 
        list_for_each((LIST), tmp)      \
215
 
                len++;          \
216
 
        len;                    \
217
 
})
218
 
 
219
 
#define list_find_prev(LIST, ENTRY)     \
220
 
({                                      \
221
 
        typeof(ENTRY) tmp, prev = NULL; \
222
 
        list_for_each((LIST), tmp) {    \
223
 
                if (tmp == (ENTRY))     \
224
 
                        break;          \
225
 
                prev = tmp;             \
226
 
        }                               \
227
 
        prev;                           \
228
 
})
229
 
 
230
 
#define list_remove_at(LIST, PREV, ENTRY)                       \
231
 
        if (PREV)                                               \
232
 
                (PREV)->next = (ENTRY)->next;                   \
233
 
        if ((ENTRY) == (LIST))                                  \
234
 
                (LIST) = (ENTRY)->next;                         \
235
 
        (ENTRY)->next = NULL;                                   \
236
 
 
237
 
#define list_remove(LIST, ENTRY)                                \
238
 
do {                                                            \
239
 
        typeof(ENTRY) prev = list_find_prev((LIST), (ENTRY));   \
240
 
        list_remove_at((LIST), prev, (ENTRY));                  \
241
 
} while (0)
242
 
 
243
 
 
244
 
#define DUP_STRING(orig, new, field, fail_target) \
245
 
        do {                                                                    \
246
 
                (new)->field = ((orig)->field) ? strdup((orig)->field) : NULL;  \
247
 
                if (((orig)->field) && !((new)->field))                         \
248
 
                                goto fail_target;                               \
249
 
        } while (0)
250
 
 
251
 
 
252
 
#define u8  unsigned char
253
 
#define u16 uint16_t
254
 
#define u32 uint32_t
255
 
#define u64 uint64_t
256
 
 
257
 
#define cpu_to_le16(x) ((u16)(htole16 ((u16) x)))
258
 
#define cpu_to_le32(x) ((u32)(htole32 ((u32) x)))
259
 
#define cpu_to_le64(x) ((u64)(htole64 ((u64) x)))
260
 
 
261
 
/* The encoding for kernal abi > 5 is
262
 
 * 28-31: reserved
263
 
 * 20-27: policy version
264
 
 * 12-19: policy abi version
265
 
 * 11:    force complain flag
266
 
 * 10:    reserved
267
 
 * 0-9:   kernel abi version
268
 
 */
269
 
#define ENCODE_VERSION(C, P, PABI, KABI)                \
270
 
({                                                      \
271
 
        u32 version = (KABI) & 0x3ff;                   \
272
 
        if ((KABI) > 5) {                               \
273
 
                version |= (C) ? 1 << 11 : 0;           \
274
 
                version |= ((PABI) & 0xff) << 12;       \
275
 
                version |= ((P) & 0xff) << 20;          \
276
 
        }                                               \
277
 
        version;                                        \
278
 
})
279
 
 
280
 
/* The parser fills this variable in automatically */
281
 
#define PROFILE_NAME_VARIABLE "profile_name"
282
 
 
283
 
/* from parser_common.c */
284
 
extern uint32_t policy_version;
285
 
extern uint32_t parser_abi_version;
286
 
extern uint32_t kernel_abi_version;
287
 
 
288
 
extern int force_complain;
289
 
extern int perms_create;
290
 
extern int net_af_max_override;
291
 
extern int kernel_load;
292
 
extern int kernel_supports_setload;
293
 
extern int kernel_supports_network;
294
 
extern int kernel_supports_policydb;
295
 
extern int kernel_supports_diff_encode;
296
 
extern int kernel_supports_mount;
297
 
extern int kernel_supports_dbus;
298
 
extern int kernel_supports_signal;
299
 
extern int kernel_supports_ptrace;
300
 
extern int kernel_supports_unix;
301
 
extern int kernel_supports_stacking;
302
 
extern int conf_verbose;
303
 
extern int conf_quiet;
304
 
extern int names_only;
305
 
extern int option;
306
 
extern int current_lineno;
307
 
extern dfaflags_t dfaflags;
308
 
extern const char *progname;
309
 
extern char *profilename;
310
 
extern char *profile_ns;
311
 
extern char *current_filename;
312
 
extern FILE *ofile;
313
 
extern int read_implies_exec;
314
 
extern void pwarn(const char *fmt, ...) __attribute__((__format__(__printf__, 1, 2)));
315
 
 
316
 
/* from parser_main (cannot be used in tst builds) */
317
 
extern int force_complain;
318
 
extern void display_version(void);
319
 
extern int show_cache;
320
 
extern int skip_cache;
321
 
extern int skip_read_cache;
322
 
extern int write_cache;
323
 
extern int cond_clear_cache;
324
 
extern int force_clear_cache;
325
 
extern int create_cache_dir;
326
 
extern int preprocess_only;
327
 
extern int skip_mode_force;
328
 
extern int abort_on_error;
329
 
extern int skip_bad_cache_rebuild;
330
 
extern int mru_skip_cache;
331
 
extern int debug_cache;
332
 
 
333
 
/* provided by parser_lex.l (cannot be used in tst builds) */
334
 
extern FILE *yyin;
335
 
extern void yyrestart(FILE *fp);
336
 
extern int yyparse(void);
337
 
extern void yyerror(const char *msg, ...);
338
 
extern int yylex(void);
339
 
 
340
 
/* parser_include.c */
341
 
extern const char *basedir;
342
 
 
343
 
/* parser_regex.c */
344
 
#define default_match_pattern "[^\\000]*"
345
 
#define anyone_match_pattern "[^\\000]+"
346
 
 
347
 
#define glob_default    0
348
 
#define glob_null       1
349
 
extern pattern_t convert_aaregex_to_pcre(const char *aare, int anchor, int glob,
350
 
                                         std::string& pcre, int *first_re_pos);
351
 
extern int build_list_val_expr(std::string& buffer, struct value_list *list);
352
 
extern int convert_entry(std::string& buffer, char *entry);
353
 
extern int clear_and_convert_entry(std::string& buffer, char *entry);
354
 
extern int process_regex(Profile *prof);
355
 
extern int post_process_entry(struct cod_entry *entry);
356
 
 
357
 
extern int process_policydb(Profile *prof);
358
 
 
359
 
extern int process_policy_ents(Profile *prof);
360
 
 
361
 
/* parser_variable.c */
362
 
int expand_entry_variables(char **name);
363
 
extern int process_variables(Profile *prof);
364
 
extern struct var_string *split_out_var(const char *string);
365
 
extern void free_var_string(struct var_string *var);
366
 
 
367
 
/* parser_misc.c */
368
 
extern void warn_uppercase(void);
369
 
extern int is_blacklisted(const char *name, const char *path);
370
 
extern struct value_list *new_value_list(char *value);
371
 
extern struct value_list *dup_value_list(struct value_list *list);
372
 
extern void free_value_list(struct value_list *list);
373
 
extern void print_value_list(struct value_list *list);
374
 
extern struct cond_entry *new_cond_entry(char *name, int eq, struct value_list *list);
375
 
extern void move_conditional_value(const char *rulename, char **dst_ptr,
376
 
                                   struct cond_entry *cond_ent);
377
 
extern void free_cond_entry(struct cond_entry *ent);
378
 
extern void free_cond_list(struct cond_entry *ents);
379
 
extern void print_cond_entry(struct cond_entry *ent);
380
 
extern char *processid(const char *string, int len);
381
 
extern char *processquoted(const char *string, int len);
382
 
extern char *processunquoted(const char *string, int len);
383
 
extern int get_keyword_token(const char *keyword);
384
 
extern int name_to_capability(const char *keyword);
385
 
extern int get_rlimit(const char *name);
386
 
extern char *process_var(const char *var);
387
 
extern int parse_mode(const char *mode);
388
 
extern int parse_X_mode(const char *X, int valid, const char *str_mode, int *mode, int fail);
389
 
bool label_contains_ns(const char *label);
390
 
bool parse_label(bool *_stack, char **_ns, char **_name,
391
 
                 const char *label, bool yyerr);
392
 
extern struct cod_entry *new_entry(char *id, int mode, char *link_id);
393
 
 
394
 
/* returns -1 if value != true or false, otherwise 0 == false, 1 == true */
395
 
extern int str_to_boolean(const char* str);
396
 
extern struct cod_entry *copy_cod_entry(struct cod_entry *cod);
397
 
extern void free_cod_entries(struct cod_entry *list);
398
 
extern void __debug_capabilities(uint64_t capset, const char *name);
399
 
void debug_cod_entries(struct cod_entry *list);
400
 
 
401
 
#define SECONDS_P_MS (1000LL * 1000LL)
402
 
long long convert_time_units(long long value, long long base, const char *units);
403
 
 
404
 
 
405
 
/* parser_symtab.c */
406
 
struct set_value {
407
 
        char *val;
408
 
        struct set_value *next;
409
 
};
410
 
extern int add_boolean_var(const char *var, int boolean);
411
 
extern int get_boolean_var(const char *var);
412
 
extern int new_set_var(const char *var, const char *value);
413
 
extern int add_set_value(const char *var, const char *value);
414
 
extern struct set_value *get_set_var(const char *var);
415
 
extern char *get_next_set_value(struct set_value **context);
416
 
extern int delete_set_var(const char *var_name);
417
 
extern void dump_symtab(void);
418
 
extern void dump_expanded_symtab(void);
419
 
void free_symtabs(void);
420
 
 
421
 
/* parser_alias.c */
422
 
extern int new_alias(const char *from, const char *to);
423
 
extern int replace_profile_aliases(Profile *prof);
424
 
extern void free_aliases(void);
425
 
 
426
 
/* parser_merge.c */
427
 
extern int profile_merge_rules(Profile *prof);
428
 
 
429
 
/* parser_interface.c */
430
 
extern int load_profile(int option, aa_kernel_interface *kernel_interface,
431
 
                        Profile *prof, int cache_fd);
432
 
extern void sd_serialize_profile(std::ostringstream &buf, Profile *prof,
433
 
                                int flatten);
434
 
extern int sd_load_buffer(int option, char *buffer, int size);
435
 
extern int cache_fd;
436
 
 
437
 
 
438
 
/* parser_policy.c */
439
 
extern void add_to_list(Profile *profile);
440
 
extern void add_hat_to_policy(Profile *policy, Profile *hat);
441
 
extern int add_entry_to_x_table(Profile *prof, char *name);
442
 
extern void add_entry_to_policy(Profile *policy, struct cod_entry *entry);
443
 
extern void post_process_file_entries(Profile *prof);
444
 
extern void post_process_rule_entries(Profile *prof);
445
 
extern int post_process_policy(int debug_only);
446
 
extern int process_profile_regex(Profile *prof);
447
 
extern int process_profile_variables(Profile *prof);
448
 
extern int process_profile_policydb(Profile *prof);
449
 
extern int post_merge_rules(void);
450
 
extern int merge_hat_rules(Profile *prof);
451
 
extern Profile *merge_policy(Profile *a, Profile *b);
452
 
extern int load_policy(int option, aa_kernel_interface *kernel_interface,
453
 
                       int cache_fd);
454
 
extern int load_hats(std::ostringstream &buf, Profile *prof);
455
 
extern int load_flattened_hats(Profile *prof, int option,
456
 
                               aa_kernel_interface *kernel_interface,
457
 
                               int cache_fd);
458
 
extern void dump_policy_hats(Profile *prof);
459
 
extern void dump_policy_names(void);
460
 
void dump_policy(void);
461
 
 
462
 
void free_policies(void);
463
 
 
464
 
#endif /** __AA_PARSER_H */