Thomas Gleixner | b886d83c | 2019-06-01 10:08:55 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
John Johansen | e06f75a | 2010-07-29 14:48:01 -0700 | [diff] [blame] | 2 | /* |
| 3 | * AppArmor security module |
| 4 | * |
| 5 | * This file contains AppArmor policy dfa matching engine definitions. |
| 6 | * |
| 7 | * Copyright (C) 1998-2008 Novell/SUSE |
John Johansen | 8e4ff10 | 2013-02-18 16:08:34 -0800 | [diff] [blame] | 8 | * Copyright 2009-2012 Canonical Ltd. |
John Johansen | e06f75a | 2010-07-29 14:48:01 -0700 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #ifndef __AA_MATCH_H |
| 12 | #define __AA_MATCH_H |
| 13 | |
Alexey Dobriyan | 57cc721 | 2011-01-10 08:18:25 +0200 | [diff] [blame] | 14 | #include <linux/kref.h> |
John Johansen | e06f75a | 2010-07-29 14:48:01 -0700 | [diff] [blame] | 15 | |
| 16 | #define DFA_NOMATCH 0 |
| 17 | #define DFA_START 1 |
| 18 | |
John Johansen | e06f75a | 2010-07-29 14:48:01 -0700 | [diff] [blame] | 19 | |
| 20 | /** |
| 21 | * The format used for transition tables is based on the GNU flex table |
| 22 | * file format (--tables-file option; see Table File Format in the flex |
| 23 | * info pages and the flex sources for documentation). The magic number |
Uwe Kleine-König | b595076 | 2010-11-01 15:38:34 -0400 | [diff] [blame] | 24 | * used in the header is 0x1B5E783D instead of 0xF13C57B1 though, because |
John Johansen | 8e4ff10 | 2013-02-18 16:08:34 -0800 | [diff] [blame] | 25 | * new tables have been defined and others YY_ID_CHK (check) and YY_ID_DEF |
| 26 | * (default) tables are used slightly differently (see the apparmor-parser |
| 27 | * package). |
| 28 | * |
| 29 | * |
| 30 | * The data in the packed dfa is stored in network byte order, and the tables |
| 31 | * are arranged for flexibility. We convert the table data to host native |
| 32 | * byte order. |
| 33 | * |
| 34 | * The dfa begins with a table set header, and is followed by the actual |
| 35 | * tables. |
John Johansen | e06f75a | 2010-07-29 14:48:01 -0700 | [diff] [blame] | 36 | */ |
| 37 | |
| 38 | #define YYTH_MAGIC 0x1B5E783D |
John Johansen | 031dcc8 | 2017-08-08 12:10:50 -0700 | [diff] [blame] | 39 | #define YYTH_FLAG_DIFF_ENCODE 1 |
John Johansen | e06f75a | 2010-07-29 14:48:01 -0700 | [diff] [blame] | 40 | |
| 41 | struct table_set_header { |
| 42 | u32 th_magic; /* YYTH_MAGIC */ |
| 43 | u32 th_hsize; |
| 44 | u32 th_ssize; |
| 45 | u16 th_flags; |
| 46 | char th_version[]; |
| 47 | }; |
| 48 | |
| 49 | /* The YYTD_ID are one less than flex table mappings. The flex id |
| 50 | * has 1 subtracted at table load time, this allows us to directly use the |
| 51 | * ID's as indexes. |
| 52 | */ |
| 53 | #define YYTD_ID_ACCEPT 0 |
| 54 | #define YYTD_ID_BASE 1 |
| 55 | #define YYTD_ID_CHK 2 |
| 56 | #define YYTD_ID_DEF 3 |
| 57 | #define YYTD_ID_EC 4 |
| 58 | #define YYTD_ID_META 5 |
| 59 | #define YYTD_ID_ACCEPT2 6 |
| 60 | #define YYTD_ID_NXT 7 |
| 61 | #define YYTD_ID_TSIZE 8 |
John Johansen | 1575617 | 2016-06-02 02:37:02 -0700 | [diff] [blame] | 62 | #define YYTD_ID_MAX 8 |
John Johansen | e06f75a | 2010-07-29 14:48:01 -0700 | [diff] [blame] | 63 | |
| 64 | #define YYTD_DATA8 1 |
| 65 | #define YYTD_DATA16 2 |
| 66 | #define YYTD_DATA32 4 |
| 67 | #define YYTD_DATA64 8 |
| 68 | |
John Johansen | 8e4ff10 | 2013-02-18 16:08:34 -0800 | [diff] [blame] | 69 | /* ACCEPT & ACCEPT2 tables gets 6 dedicated flags, YYTD_DATAX define the |
John Johansen | e06f75a | 2010-07-29 14:48:01 -0700 | [diff] [blame] | 70 | * first flags |
| 71 | */ |
| 72 | #define ACCEPT1_FLAGS(X) ((X) & 0x3f) |
| 73 | #define ACCEPT2_FLAGS(X) ACCEPT1_FLAGS((X) >> YYTD_ID_ACCEPT2) |
| 74 | #define TO_ACCEPT1_FLAG(X) ACCEPT1_FLAGS(X) |
| 75 | #define TO_ACCEPT2_FLAG(X) (ACCEPT1_FLAGS(X) << YYTD_ID_ACCEPT2) |
| 76 | #define DFA_FLAG_VERIFY_STATES 0x1000 |
| 77 | |
| 78 | struct table_header { |
| 79 | u16 td_id; |
| 80 | u16 td_flags; |
| 81 | u32 td_hilen; |
| 82 | u32 td_lolen; |
| 83 | char td_data[]; |
| 84 | }; |
| 85 | |
| 86 | #define DEFAULT_TABLE(DFA) ((u16 *)((DFA)->tables[YYTD_ID_DEF]->td_data)) |
| 87 | #define BASE_TABLE(DFA) ((u32 *)((DFA)->tables[YYTD_ID_BASE]->td_data)) |
| 88 | #define NEXT_TABLE(DFA) ((u16 *)((DFA)->tables[YYTD_ID_NXT]->td_data)) |
| 89 | #define CHECK_TABLE(DFA) ((u16 *)((DFA)->tables[YYTD_ID_CHK]->td_data)) |
| 90 | #define EQUIV_TABLE(DFA) ((u8 *)((DFA)->tables[YYTD_ID_EC]->td_data)) |
| 91 | #define ACCEPT_TABLE(DFA) ((u32 *)((DFA)->tables[YYTD_ID_ACCEPT]->td_data)) |
| 92 | #define ACCEPT_TABLE2(DFA) ((u32 *)((DFA)->tables[YYTD_ID_ACCEPT2]->td_data)) |
| 93 | |
| 94 | struct aa_dfa { |
| 95 | struct kref count; |
| 96 | u16 flags; |
| 97 | struct table_header *tables[YYTD_ID_TSIZE]; |
| 98 | }; |
| 99 | |
John Johansen | 11c236b | 2017-01-16 00:42:42 -0800 | [diff] [blame] | 100 | extern struct aa_dfa *nulldfa; |
John Johansen | 6e0654d | 2017-09-06 14:57:59 -0700 | [diff] [blame] | 101 | extern struct aa_dfa *stacksplitdfa; |
John Johansen | 11c236b | 2017-01-16 00:42:42 -0800 | [diff] [blame] | 102 | |
John Johansen | e06f75a | 2010-07-29 14:48:01 -0700 | [diff] [blame] | 103 | #define byte_to_byte(X) (X) |
| 104 | |
John Johansen | e6e8bf4 | 2017-01-16 00:43:13 -0800 | [diff] [blame] | 105 | #define UNPACK_ARRAY(TABLE, BLOB, LEN, TTYPE, BTYPE, NTOHX) \ |
John Johansen | e06f75a | 2010-07-29 14:48:01 -0700 | [diff] [blame] | 106 | do { \ |
| 107 | typeof(LEN) __i; \ |
John Johansen | e6e8bf4 | 2017-01-16 00:43:13 -0800 | [diff] [blame] | 108 | TTYPE *__t = (TTYPE *) TABLE; \ |
| 109 | BTYPE *__b = (BTYPE *) BLOB; \ |
John Johansen | e06f75a | 2010-07-29 14:48:01 -0700 | [diff] [blame] | 110 | for (__i = 0; __i < LEN; __i++) { \ |
| 111 | __t[__i] = NTOHX(__b[__i]); \ |
| 112 | } \ |
| 113 | } while (0) |
| 114 | |
| 115 | static inline size_t table_size(size_t len, size_t el_size) |
| 116 | { |
| 117 | return ALIGN(sizeof(struct table_header) + len * el_size, 8); |
| 118 | } |
| 119 | |
John Johansen | 11c236b | 2017-01-16 00:42:42 -0800 | [diff] [blame] | 120 | int aa_setup_dfa_engine(void); |
| 121 | void aa_teardown_dfa_engine(void); |
| 122 | |
John Johansen | e06f75a | 2010-07-29 14:48:01 -0700 | [diff] [blame] | 123 | struct aa_dfa *aa_dfa_unpack(void *blob, size_t size, int flags); |
| 124 | unsigned int aa_dfa_match_len(struct aa_dfa *dfa, unsigned int start, |
| 125 | const char *str, int len); |
| 126 | unsigned int aa_dfa_match(struct aa_dfa *dfa, unsigned int start, |
| 127 | const char *str); |
John Johansen | 0fe1212 | 2012-02-16 06:20:26 -0800 | [diff] [blame] | 128 | unsigned int aa_dfa_next(struct aa_dfa *dfa, unsigned int state, |
| 129 | const char c); |
John Johansen | cf65fab | 2017-09-06 02:53:15 -0700 | [diff] [blame] | 130 | unsigned int aa_dfa_match_until(struct aa_dfa *dfa, unsigned int start, |
| 131 | const char *str, const char **retpos); |
| 132 | unsigned int aa_dfa_matchn_until(struct aa_dfa *dfa, unsigned int start, |
| 133 | const char *str, int n, const char **retpos); |
John Johansen | 0fe1212 | 2012-02-16 06:20:26 -0800 | [diff] [blame] | 134 | |
John Johansen | e06f75a | 2010-07-29 14:48:01 -0700 | [diff] [blame] | 135 | void aa_dfa_free_kref(struct kref *kref); |
| 136 | |
John Johansen | 21f6066 | 2017-11-18 19:43:13 -0800 | [diff] [blame] | 137 | #define WB_HISTORY_SIZE 8 |
| 138 | struct match_workbuf { |
| 139 | unsigned int count; |
| 140 | unsigned int pos; |
| 141 | unsigned int len; |
| 142 | unsigned int size; /* power of 2, same as history size */ |
| 143 | unsigned int history[WB_HISTORY_SIZE]; |
| 144 | }; |
| 145 | #define DEFINE_MATCH_WB(N) \ |
| 146 | struct match_workbuf N = { \ |
| 147 | .count = 0, \ |
| 148 | .pos = 0, \ |
| 149 | .len = 0, \ |
| 150 | .size = WB_HISTORY_SIZE, \ |
| 151 | } |
| 152 | |
| 153 | unsigned int aa_dfa_leftmatch(struct aa_dfa *dfa, unsigned int start, |
| 154 | const char *str, unsigned int *count); |
| 155 | |
John Johansen | e06f75a | 2010-07-29 14:48:01 -0700 | [diff] [blame] | 156 | /** |
John Johansen | 293a488 | 2017-01-16 00:42:40 -0800 | [diff] [blame] | 157 | * aa_get_dfa - increment refcount on dfa @p |
| 158 | * @dfa: dfa (MAYBE NULL) |
| 159 | * |
| 160 | * Returns: pointer to @dfa if @dfa is NULL will return NULL |
| 161 | * Requires: @dfa must be held with valid refcount when called |
| 162 | */ |
| 163 | static inline struct aa_dfa *aa_get_dfa(struct aa_dfa *dfa) |
| 164 | { |
| 165 | if (dfa) |
| 166 | kref_get(&(dfa->count)); |
| 167 | |
| 168 | return dfa; |
| 169 | } |
| 170 | |
| 171 | /** |
John Johansen | e06f75a | 2010-07-29 14:48:01 -0700 | [diff] [blame] | 172 | * aa_put_dfa - put a dfa refcount |
| 173 | * @dfa: dfa to put refcount (MAYBE NULL) |
| 174 | * |
| 175 | * Requires: if @dfa != NULL that a valid refcount be held |
| 176 | */ |
| 177 | static inline void aa_put_dfa(struct aa_dfa *dfa) |
| 178 | { |
| 179 | if (dfa) |
| 180 | kref_put(&dfa->count, aa_dfa_free_kref); |
| 181 | } |
| 182 | |
John Johansen | 031dcc8 | 2017-08-08 12:10:50 -0700 | [diff] [blame] | 183 | #define MATCH_FLAG_DIFF_ENCODE 0x80000000 |
| 184 | #define MARK_DIFF_ENCODE 0x40000000 |
| 185 | |
John Johansen | e06f75a | 2010-07-29 14:48:01 -0700 | [diff] [blame] | 186 | #endif /* __AA_MATCH_H */ |