2
* Copyright (c) 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2007
3
* NOVELL (All rights reserved)
5
* Copyright (c) 2010 - 2012
6
* Canonical Ltd. (All rights reserved)
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.
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.
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
28
#include <sys/resource.h>
31
#define _(s) gettext(s)
33
#include <sys/apparmor.h>
36
#include "libapparmor_re/apparmor_re.h"
37
#include "libapparmor_re/aare_rules.h"
47
#define MODULE_NAME "apparmor"
49
/* Global variable to pass token to lexer. Will be replaced by parameter
50
* when lexer and parser are made reentrant
52
extern int parser_token;
55
#define WARN_RULE_NOT_ENFORCED 1
56
#define WARN_RULE_DOWNGRADED 2
58
extern dfaflags_t warnflags;
61
typedef enum pattern_t pattern_t;
70
char *regex; // posix regex
76
struct value_list *next;
81
int eq; /* where equals was used in specifying list */
82
struct value_list *vals;
84
struct cond_entry *next;
87
struct cond_entry_list {
90
struct cond_entry *list;
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 */
106
int alias_ignore; /* ignore for alias processing */
110
pattern_t pattern_type;
111
struct cod_pattern pat;
113
struct cod_entry *next;
117
unsigned int specified; /* limits that are set */
118
rlim_t limits[RLIMIT_NLIMITS];
123
struct alt_name *next;
128
unsigned int hat_magic;
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'
153
#define OPTION_REMOVE 2
154
#define OPTION_REPLACE 3
155
#define OPTION_STDOUT 4
156
#define OPTION_OFILE 5
160
extern int preprocess_only;
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
174
#define PDEBUG(fmt, args...) fprintf(stderr, "parser: " fmt, ## args)
176
#define PDEBUG(fmt, args...) /* Do nothing */
178
#define NPDEBUG(fmt, args...) /* Do nothing */
180
#define PERROR(fmt, args...) fprintf(stderr, fmt, ## args)
190
#define MAX_PORT 65535
193
#define unused __attribute__ ((unused))
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) \
205
typeof(LISTA) ___tmp; \
206
list_last_entry((LISTA), ___tmp);\
207
___tmp->next = (LISTB); \
210
#define list_len(LIST) \
214
list_for_each((LIST), tmp) \
219
#define list_find_prev(LIST, ENTRY) \
221
typeof(ENTRY) tmp, prev = NULL; \
222
list_for_each((LIST), tmp) { \
223
if (tmp == (ENTRY)) \
230
#define list_remove_at(LIST, PREV, ENTRY) \
232
(PREV)->next = (ENTRY)->next; \
233
if ((ENTRY) == (LIST)) \
234
(LIST) = (ENTRY)->next; \
235
(ENTRY)->next = NULL; \
237
#define list_remove(LIST, ENTRY) \
239
typeof(ENTRY) prev = list_find_prev((LIST), (ENTRY)); \
240
list_remove_at((LIST), prev, (ENTRY)); \
244
#define DUP_STRING(orig, new, field, fail_target) \
246
(new)->field = ((orig)->field) ? strdup((orig)->field) : NULL; \
247
if (((orig)->field) && !((new)->field)) \
252
#define u8 unsigned char
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)))
261
/* The encoding for kernal abi > 5 is
263
* 20-27: policy version
264
* 12-19: policy abi version
265
* 11: force complain flag
267
* 0-9: kernel abi version
269
#define ENCODE_VERSION(C, P, PABI, KABI) \
271
u32 version = (KABI) & 0x3ff; \
273
version |= (C) ? 1 << 11 : 0; \
274
version |= ((PABI) & 0xff) << 12; \
275
version |= ((P) & 0xff) << 20; \
280
/* The parser fills this variable in automatically */
281
#define PROFILE_NAME_VARIABLE "profile_name"
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;
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;
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;
313
extern int read_implies_exec;
314
extern void pwarn(const char *fmt, ...) __attribute__((__format__(__printf__, 1, 2)));
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;
333
/* provided by parser_lex.l (cannot be used in tst builds) */
335
extern void yyrestart(FILE *fp);
336
extern int yyparse(void);
337
extern void yyerror(const char *msg, ...);
338
extern int yylex(void);
340
/* parser_include.c */
341
extern const char *basedir;
344
#define default_match_pattern "[^\\000]*"
345
#define anyone_match_pattern "[^\\000]+"
347
#define glob_default 0
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);
357
extern int process_policydb(Profile *prof);
359
extern int process_policy_ents(Profile *prof);
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);
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);
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);
401
#define SECONDS_P_MS (1000LL * 1000LL)
402
long long convert_time_units(long long value, long long base, const char *units);
405
/* parser_symtab.c */
408
struct set_value *next;
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);
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);
427
extern int profile_merge_rules(Profile *prof);
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,
434
extern int sd_load_buffer(int option, char *buffer, int size);
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,
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,
458
extern void dump_policy_hats(Profile *prof);
459
extern void dump_policy_names(void);
460
void dump_policy(void);
462
void free_policies(void);
464
#endif /** __AA_PARSER_H */