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