Skip to content

Commit 031dcc8

Browse files
committed
apparmor: dfa add support for state differential encoding
State differential encoding can provide better compression for apparmor policy, without having significant impact on match time. Signed-off-by: John Johansen <john.johansen@canonical.com>
1 parent 074c1cd commit 031dcc8

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

‎security/apparmor/include/match.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
*/
4141

4242
#define YYTH_MAGIC 0x1B5E783D
43+
#define YYTH_FLAG_DIFF_ENCODE 1
4344

4445
struct table_set_header {
4546
u32 th_magic; /* YYTH_MAGIC */
@@ -164,4 +165,7 @@ static inline void aa_put_dfa(struct aa_dfa *dfa)
164165
kref_put(&dfa->count, aa_dfa_free_kref);
165166
}
166167

168+
#define MATCH_FLAG_DIFF_ENCODE 0x80000000
169+
#define MARK_DIFF_ENCODE 0x40000000
170+
167171
#endif /* __AA_MATCH_H */

‎security/apparmor/match.c‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,8 @@ static int verify_dfa(struct aa_dfa *dfa, int flags)
185185

186186
if (flags & DFA_FLAG_VERIFY_STATES) {
187187
for (i = 0; i < state_count; i++) {
188-
if (DEFAULT_TABLE(dfa)[i] >= state_count)
188+
if (!(BASE_TABLE(dfa)[i] & MATCH_FLAG_DIFF_ENCODE) &&
189+
(DEFAULT_TABLE(dfa)[i] >= state_count))
189190
goto out;
190191
if (base_idx(BASE_TABLE(dfa)[i]) + 255 >= trans_count) {
191192
printk(KERN_ERR "AppArmor DFA next/check upper "
@@ -202,6 +203,24 @@ static int verify_dfa(struct aa_dfa *dfa, int flags)
202203
}
203204
}
204205

206+
/* Now that all the other tables are verified, verify diffencoding */
207+
if (flags & DFA_FLAG_VERIFY_STATES) {
208+
size_t j, k;
209+
210+
for (i = 0; i < state_count; i++) {
211+
for (j = i;
212+
(BASE_TABLE(dfa)[j] & MATCH_FLAG_DIFF_ENCODE) &&
213+
!(BASE_TABLE(dfa)[j] & MARK_DIFF_ENCODE);
214+
j = k) {
215+
k = DEFAULT_TABLE(dfa)[j];
216+
if (j == k)
217+
goto out;
218+
if (k < j)
219+
break; /* already verified */
220+
BASE_TABLE(dfa)[j] |= MARK_DIFF_ENCODE;
221+
}
222+
}
223+
}
205224
error = 0;
206225
out:
207226
return error;
@@ -274,6 +293,9 @@ struct aa_dfa *aa_dfa_unpack(void *blob, size_t size, int flags)
274293
goto fail;
275294

276295
dfa->flags = ntohs(*(__be16 *) (data + 12));
296+
if (dfa->flags != 0 && dfa->flags != YYTH_FLAG_DIFF_ENCODE)
297+
goto fail;
298+
277299
data += hsize;
278300
size -= hsize;
279301

@@ -335,6 +357,8 @@ do { \
335357
unsigned int pos = base_idx(b) + (C); \
336358
if ((check)[pos] != (state)) { \
337359
(state) = (def)[(state)]; \
360+
if (b & MATCH_FLAG_DIFF_ENCODE) \
361+
continue; \
338362
break; \
339363
} \
340364
(state) = (next)[pos]; \

0 commit comments

Comments
 (0)