1e06f75a6SJohn Johansen /*
2e06f75a6SJohn Johansen  * AppArmor security module
3e06f75a6SJohn Johansen  *
4e06f75a6SJohn Johansen  * This file contains AppArmor policy dfa matching engine definitions.
5e06f75a6SJohn Johansen  *
6e06f75a6SJohn Johansen  * Copyright (C) 1998-2008 Novell/SUSE
78e4ff109SJohn Johansen  * Copyright 2009-2012 Canonical Ltd.
8e06f75a6SJohn Johansen  *
9e06f75a6SJohn Johansen  * This program is free software; you can redistribute it and/or
10e06f75a6SJohn Johansen  * modify it under the terms of the GNU General Public License as
11e06f75a6SJohn Johansen  * published by the Free Software Foundation, version 2 of the
12e06f75a6SJohn Johansen  * License.
13e06f75a6SJohn Johansen  */
14e06f75a6SJohn Johansen 
15e06f75a6SJohn Johansen #ifndef __AA_MATCH_H
16e06f75a6SJohn Johansen #define __AA_MATCH_H
17e06f75a6SJohn Johansen 
1857cc7215SAlexey Dobriyan #include <linux/kref.h>
19e06f75a6SJohn Johansen 
20e06f75a6SJohn Johansen #define DFA_NOMATCH			0
21e06f75a6SJohn Johansen #define DFA_START			1
22e06f75a6SJohn Johansen 
23e06f75a6SJohn Johansen 
24e06f75a6SJohn Johansen /**
25e06f75a6SJohn Johansen  * The format used for transition tables is based on the GNU flex table
26e06f75a6SJohn Johansen  * file format (--tables-file option; see Table File Format in the flex
27e06f75a6SJohn Johansen  * info pages and the flex sources for documentation). The magic number
28b595076aSUwe Kleine-König  * used in the header is 0x1B5E783D instead of 0xF13C57B1 though, because
298e4ff109SJohn Johansen  * new tables have been defined and others YY_ID_CHK (check) and YY_ID_DEF
308e4ff109SJohn Johansen  * (default) tables are used slightly differently (see the apparmor-parser
318e4ff109SJohn Johansen  * package).
328e4ff109SJohn Johansen  *
338e4ff109SJohn Johansen  *
348e4ff109SJohn Johansen  * The data in the packed dfa is stored in network byte order, and the tables
358e4ff109SJohn Johansen  * are arranged for flexibility.  We convert the table data to host native
368e4ff109SJohn Johansen  * byte order.
378e4ff109SJohn Johansen  *
388e4ff109SJohn Johansen  * The dfa begins with a table set header, and is followed by the actual
398e4ff109SJohn Johansen  * tables.
40e06f75a6SJohn Johansen  */
41e06f75a6SJohn Johansen 
42e06f75a6SJohn Johansen #define YYTH_MAGIC	0x1B5E783D
43e06f75a6SJohn Johansen 
44e06f75a6SJohn Johansen struct table_set_header {
45e06f75a6SJohn Johansen 	u32 th_magic;		/* YYTH_MAGIC */
46e06f75a6SJohn Johansen 	u32 th_hsize;
47e06f75a6SJohn Johansen 	u32 th_ssize;
48e06f75a6SJohn Johansen 	u16 th_flags;
49e06f75a6SJohn Johansen 	char th_version[];
50e06f75a6SJohn Johansen };
51e06f75a6SJohn Johansen 
52e06f75a6SJohn Johansen /* The YYTD_ID are one less than flex table mappings.  The flex id
53e06f75a6SJohn Johansen  * has 1 subtracted at table load time, this allows us to directly use the
54e06f75a6SJohn Johansen  * ID's as indexes.
55e06f75a6SJohn Johansen  */
56e06f75a6SJohn Johansen #define	YYTD_ID_ACCEPT	0
57e06f75a6SJohn Johansen #define YYTD_ID_BASE	1
58e06f75a6SJohn Johansen #define YYTD_ID_CHK	2
59e06f75a6SJohn Johansen #define YYTD_ID_DEF	3
60e06f75a6SJohn Johansen #define YYTD_ID_EC	4
61e06f75a6SJohn Johansen #define YYTD_ID_META	5
62e06f75a6SJohn Johansen #define YYTD_ID_ACCEPT2 6
63e06f75a6SJohn Johansen #define YYTD_ID_NXT	7
64e06f75a6SJohn Johansen #define YYTD_ID_TSIZE	8
6515756178SJohn Johansen #define YYTD_ID_MAX	8
66e06f75a6SJohn Johansen 
67e06f75a6SJohn Johansen #define YYTD_DATA8	1
68e06f75a6SJohn Johansen #define YYTD_DATA16	2
69e06f75a6SJohn Johansen #define YYTD_DATA32	4
70e06f75a6SJohn Johansen #define YYTD_DATA64	8
71e06f75a6SJohn Johansen 
728e4ff109SJohn Johansen /* ACCEPT & ACCEPT2 tables gets 6 dedicated flags, YYTD_DATAX define the
73e06f75a6SJohn Johansen  * first flags
74e06f75a6SJohn Johansen  */
75e06f75a6SJohn Johansen #define ACCEPT1_FLAGS(X) ((X) & 0x3f)
76e06f75a6SJohn Johansen #define ACCEPT2_FLAGS(X) ACCEPT1_FLAGS((X) >> YYTD_ID_ACCEPT2)
77e06f75a6SJohn Johansen #define TO_ACCEPT1_FLAG(X) ACCEPT1_FLAGS(X)
78e06f75a6SJohn Johansen #define TO_ACCEPT2_FLAG(X) (ACCEPT1_FLAGS(X) << YYTD_ID_ACCEPT2)
79e06f75a6SJohn Johansen #define DFA_FLAG_VERIFY_STATES 0x1000
80e06f75a6SJohn Johansen 
81e06f75a6SJohn Johansen struct table_header {
82e06f75a6SJohn Johansen 	u16 td_id;
83e06f75a6SJohn Johansen 	u16 td_flags;
84e06f75a6SJohn Johansen 	u32 td_hilen;
85e06f75a6SJohn Johansen 	u32 td_lolen;
86e06f75a6SJohn Johansen 	char td_data[];
87e06f75a6SJohn Johansen };
88e06f75a6SJohn Johansen 
89e06f75a6SJohn Johansen #define DEFAULT_TABLE(DFA) ((u16 *)((DFA)->tables[YYTD_ID_DEF]->td_data))
90e06f75a6SJohn Johansen #define BASE_TABLE(DFA) ((u32 *)((DFA)->tables[YYTD_ID_BASE]->td_data))
91e06f75a6SJohn Johansen #define NEXT_TABLE(DFA) ((u16 *)((DFA)->tables[YYTD_ID_NXT]->td_data))
92e06f75a6SJohn Johansen #define CHECK_TABLE(DFA) ((u16 *)((DFA)->tables[YYTD_ID_CHK]->td_data))
93e06f75a6SJohn Johansen #define EQUIV_TABLE(DFA) ((u8 *)((DFA)->tables[YYTD_ID_EC]->td_data))
94e06f75a6SJohn Johansen #define ACCEPT_TABLE(DFA) ((u32 *)((DFA)->tables[YYTD_ID_ACCEPT]->td_data))
95e06f75a6SJohn Johansen #define ACCEPT_TABLE2(DFA) ((u32 *)((DFA)->tables[YYTD_ID_ACCEPT2]->td_data))
96e06f75a6SJohn Johansen 
97e06f75a6SJohn Johansen struct aa_dfa {
98e06f75a6SJohn Johansen 	struct kref count;
99e06f75a6SJohn Johansen 	u16 flags;
100e06f75a6SJohn Johansen 	struct table_header *tables[YYTD_ID_TSIZE];
101e06f75a6SJohn Johansen };
102e06f75a6SJohn Johansen 
10311c236b8SJohn Johansen extern struct aa_dfa *nulldfa;
10411c236b8SJohn Johansen 
105e06f75a6SJohn Johansen #define byte_to_byte(X) (X)
106e06f75a6SJohn Johansen 
107e06f75a6SJohn Johansen #define UNPACK_ARRAY(TABLE, BLOB, LEN, TYPE, NTOHX) \
108e06f75a6SJohn Johansen 	do { \
109e06f75a6SJohn Johansen 		typeof(LEN) __i; \
110e06f75a6SJohn Johansen 		TYPE *__t = (TYPE *) TABLE; \
111e06f75a6SJohn Johansen 		TYPE *__b = (TYPE *) BLOB; \
112e06f75a6SJohn Johansen 		for (__i = 0; __i < LEN; __i++) { \
113e06f75a6SJohn Johansen 			__t[__i] = NTOHX(__b[__i]); \
114e06f75a6SJohn Johansen 		} \
115e06f75a6SJohn Johansen 	} while (0)
116e06f75a6SJohn Johansen 
117e06f75a6SJohn Johansen static inline size_t table_size(size_t len, size_t el_size)
118e06f75a6SJohn Johansen {
119e06f75a6SJohn Johansen 	return ALIGN(sizeof(struct table_header) + len * el_size, 8);
120e06f75a6SJohn Johansen }
121e06f75a6SJohn Johansen 
12211c236b8SJohn Johansen int aa_setup_dfa_engine(void);
12311c236b8SJohn Johansen void aa_teardown_dfa_engine(void);
12411c236b8SJohn Johansen 
125e06f75a6SJohn Johansen struct aa_dfa *aa_dfa_unpack(void *blob, size_t size, int flags);
126e06f75a6SJohn Johansen unsigned int aa_dfa_match_len(struct aa_dfa *dfa, unsigned int start,
127e06f75a6SJohn Johansen 			      const char *str, int len);
128e06f75a6SJohn Johansen unsigned int aa_dfa_match(struct aa_dfa *dfa, unsigned int start,
129e06f75a6SJohn Johansen 			  const char *str);
1300fe1212dSJohn Johansen unsigned int aa_dfa_next(struct aa_dfa *dfa, unsigned int state,
1310fe1212dSJohn Johansen 			 const char c);
1320fe1212dSJohn Johansen 
133e06f75a6SJohn Johansen void aa_dfa_free_kref(struct kref *kref);
134e06f75a6SJohn Johansen 
135e06f75a6SJohn Johansen /**
136293a4886SJohn Johansen  * aa_get_dfa - increment refcount on dfa @p
137293a4886SJohn Johansen  * @dfa: dfa  (MAYBE NULL)
138293a4886SJohn Johansen  *
139293a4886SJohn Johansen  * Returns: pointer to @dfa if @dfa is NULL will return NULL
140293a4886SJohn Johansen  * Requires: @dfa must be held with valid refcount when called
141293a4886SJohn Johansen  */
142293a4886SJohn Johansen static inline struct aa_dfa *aa_get_dfa(struct aa_dfa *dfa)
143293a4886SJohn Johansen {
144293a4886SJohn Johansen 	if (dfa)
145293a4886SJohn Johansen 		kref_get(&(dfa->count));
146293a4886SJohn Johansen 
147293a4886SJohn Johansen 	return dfa;
148293a4886SJohn Johansen }
149293a4886SJohn Johansen 
150293a4886SJohn Johansen /**
151e06f75a6SJohn Johansen  * aa_put_dfa - put a dfa refcount
152e06f75a6SJohn Johansen  * @dfa: dfa to put refcount   (MAYBE NULL)
153e06f75a6SJohn Johansen  *
154e06f75a6SJohn Johansen  * Requires: if @dfa != NULL that a valid refcount be held
155e06f75a6SJohn Johansen  */
156e06f75a6SJohn Johansen static inline void aa_put_dfa(struct aa_dfa *dfa)
157e06f75a6SJohn Johansen {
158e06f75a6SJohn Johansen 	if (dfa)
159e06f75a6SJohn Johansen 		kref_put(&dfa->count, aa_dfa_free_kref);
160e06f75a6SJohn Johansen }
161e06f75a6SJohn Johansen 
162e06f75a6SJohn Johansen #endif /* __AA_MATCH_H */
163