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
7e06f75a6SJohn Johansen  * Copyright 2009-2010 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 #include <linux/workqueue.h>
20e06f75a6SJohn Johansen 
21e06f75a6SJohn Johansen #define DFA_NOMATCH			0
22e06f75a6SJohn Johansen #define DFA_START			1
23e06f75a6SJohn Johansen 
24e06f75a6SJohn Johansen #define DFA_VALID_PERM_MASK		0xffffffff
25e06f75a6SJohn Johansen #define DFA_VALID_PERM2_MASK		0xffffffff
26e06f75a6SJohn Johansen 
27e06f75a6SJohn Johansen /**
28e06f75a6SJohn Johansen  * The format used for transition tables is based on the GNU flex table
29e06f75a6SJohn Johansen  * file format (--tables-file option; see Table File Format in the flex
30e06f75a6SJohn Johansen  * info pages and the flex sources for documentation). The magic number
31e06f75a6SJohn Johansen  * used in the header is 0x1B5E783D insted of 0xF13C57B1 though, because
32e06f75a6SJohn Johansen  * the YY_ID_CHK (check) and YY_ID_DEF (default) tables are used
33e06f75a6SJohn Johansen  * slightly differently (see the apparmor-parser package).
34e06f75a6SJohn Johansen  */
35e06f75a6SJohn Johansen 
36e06f75a6SJohn Johansen #define YYTH_MAGIC	0x1B5E783D
37e06f75a6SJohn Johansen #define YYTH_DEF_RECURSE 0x1			/* DEF Table is recursive */
38e06f75a6SJohn Johansen 
39e06f75a6SJohn Johansen struct table_set_header {
40e06f75a6SJohn Johansen 	u32 th_magic;		/* YYTH_MAGIC */
41e06f75a6SJohn Johansen 	u32 th_hsize;
42e06f75a6SJohn Johansen 	u32 th_ssize;
43e06f75a6SJohn Johansen 	u16 th_flags;
44e06f75a6SJohn Johansen 	char th_version[];
45e06f75a6SJohn Johansen };
46e06f75a6SJohn Johansen 
47e06f75a6SJohn Johansen /* The YYTD_ID are one less than flex table mappings.  The flex id
48e06f75a6SJohn Johansen  * has 1 subtracted at table load time, this allows us to directly use the
49e06f75a6SJohn Johansen  * ID's as indexes.
50e06f75a6SJohn Johansen  */
51e06f75a6SJohn Johansen #define	YYTD_ID_ACCEPT	0
52e06f75a6SJohn Johansen #define YYTD_ID_BASE	1
53e06f75a6SJohn Johansen #define YYTD_ID_CHK	2
54e06f75a6SJohn Johansen #define YYTD_ID_DEF	3
55e06f75a6SJohn Johansen #define YYTD_ID_EC	4
56e06f75a6SJohn Johansen #define YYTD_ID_META	5
57e06f75a6SJohn Johansen #define YYTD_ID_ACCEPT2 6
58e06f75a6SJohn Johansen #define YYTD_ID_NXT	7
59e06f75a6SJohn Johansen #define YYTD_ID_TSIZE	8
60e06f75a6SJohn Johansen 
61e06f75a6SJohn Johansen #define YYTD_DATA8	1
62e06f75a6SJohn Johansen #define YYTD_DATA16	2
63e06f75a6SJohn Johansen #define YYTD_DATA32	4
64e06f75a6SJohn Johansen #define YYTD_DATA64	8
65e06f75a6SJohn Johansen 
66e06f75a6SJohn Johansen /* Each ACCEPT2 table gets 6 dedicated flags, YYTD_DATAX define the
67e06f75a6SJohn Johansen  * first flags
68e06f75a6SJohn Johansen  */
69e06f75a6SJohn Johansen #define ACCEPT1_FLAGS(X) ((X) & 0x3f)
70e06f75a6SJohn Johansen #define ACCEPT2_FLAGS(X) ACCEPT1_FLAGS((X) >> YYTD_ID_ACCEPT2)
71e06f75a6SJohn Johansen #define TO_ACCEPT1_FLAG(X) ACCEPT1_FLAGS(X)
72e06f75a6SJohn Johansen #define TO_ACCEPT2_FLAG(X) (ACCEPT1_FLAGS(X) << YYTD_ID_ACCEPT2)
73e06f75a6SJohn Johansen #define DFA_FLAG_VERIFY_STATES 0x1000
74e06f75a6SJohn Johansen 
75e06f75a6SJohn Johansen struct table_header {
76e06f75a6SJohn Johansen 	u16 td_id;
77e06f75a6SJohn Johansen 	u16 td_flags;
78e06f75a6SJohn Johansen 	u32 td_hilen;
79e06f75a6SJohn Johansen 	u32 td_lolen;
80e06f75a6SJohn Johansen 	char td_data[];
81e06f75a6SJohn Johansen };
82e06f75a6SJohn Johansen 
83e06f75a6SJohn Johansen #define DEFAULT_TABLE(DFA) ((u16 *)((DFA)->tables[YYTD_ID_DEF]->td_data))
84e06f75a6SJohn Johansen #define BASE_TABLE(DFA) ((u32 *)((DFA)->tables[YYTD_ID_BASE]->td_data))
85e06f75a6SJohn Johansen #define NEXT_TABLE(DFA) ((u16 *)((DFA)->tables[YYTD_ID_NXT]->td_data))
86e06f75a6SJohn Johansen #define CHECK_TABLE(DFA) ((u16 *)((DFA)->tables[YYTD_ID_CHK]->td_data))
87e06f75a6SJohn Johansen #define EQUIV_TABLE(DFA) ((u8 *)((DFA)->tables[YYTD_ID_EC]->td_data))
88e06f75a6SJohn Johansen #define ACCEPT_TABLE(DFA) ((u32 *)((DFA)->tables[YYTD_ID_ACCEPT]->td_data))
89e06f75a6SJohn Johansen #define ACCEPT_TABLE2(DFA) ((u32 *)((DFA)->tables[YYTD_ID_ACCEPT2]->td_data))
90e06f75a6SJohn Johansen 
91e06f75a6SJohn Johansen struct aa_dfa {
92e06f75a6SJohn Johansen 	struct kref count;
93e06f75a6SJohn Johansen 	u16 flags;
94e06f75a6SJohn Johansen 	struct table_header *tables[YYTD_ID_TSIZE];
95e06f75a6SJohn Johansen };
96e06f75a6SJohn Johansen 
97e06f75a6SJohn Johansen #define byte_to_byte(X) (X)
98e06f75a6SJohn Johansen 
99e06f75a6SJohn Johansen #define UNPACK_ARRAY(TABLE, BLOB, LEN, TYPE, NTOHX) \
100e06f75a6SJohn Johansen 	do { \
101e06f75a6SJohn Johansen 		typeof(LEN) __i; \
102e06f75a6SJohn Johansen 		TYPE *__t = (TYPE *) TABLE; \
103e06f75a6SJohn Johansen 		TYPE *__b = (TYPE *) BLOB; \
104e06f75a6SJohn Johansen 		for (__i = 0; __i < LEN; __i++) { \
105e06f75a6SJohn Johansen 			__t[__i] = NTOHX(__b[__i]); \
106e06f75a6SJohn Johansen 		} \
107e06f75a6SJohn Johansen 	} while (0)
108e06f75a6SJohn Johansen 
109e06f75a6SJohn Johansen static inline size_t table_size(size_t len, size_t el_size)
110e06f75a6SJohn Johansen {
111e06f75a6SJohn Johansen 	return ALIGN(sizeof(struct table_header) + len * el_size, 8);
112e06f75a6SJohn Johansen }
113e06f75a6SJohn Johansen 
114e06f75a6SJohn Johansen struct aa_dfa *aa_dfa_unpack(void *blob, size_t size, int flags);
115e06f75a6SJohn Johansen unsigned int aa_dfa_match_len(struct aa_dfa *dfa, unsigned int start,
116e06f75a6SJohn Johansen 			      const char *str, int len);
117e06f75a6SJohn Johansen unsigned int aa_dfa_match(struct aa_dfa *dfa, unsigned int start,
118e06f75a6SJohn Johansen 			  const char *str);
119e06f75a6SJohn Johansen void aa_dfa_free_kref(struct kref *kref);
120e06f75a6SJohn Johansen 
121e06f75a6SJohn Johansen /**
122e06f75a6SJohn Johansen  * aa_put_dfa - put a dfa refcount
123e06f75a6SJohn Johansen  * @dfa: dfa to put refcount   (MAYBE NULL)
124e06f75a6SJohn Johansen  *
125e06f75a6SJohn Johansen  * Requires: if @dfa != NULL that a valid refcount be held
126e06f75a6SJohn Johansen  */
127e06f75a6SJohn Johansen static inline void aa_put_dfa(struct aa_dfa *dfa)
128e06f75a6SJohn Johansen {
129e06f75a6SJohn Johansen 	if (dfa)
130e06f75a6SJohn Johansen 		kref_put(&dfa->count, aa_dfa_free_kref);
131e06f75a6SJohn Johansen }
132e06f75a6SJohn Johansen 
133e06f75a6SJohn Johansen #endif /* __AA_MATCH_H */
134