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