]> git.ipfire.org Git - thirdparty/linux.git/commit
apparmor: fix loop detection used in conflicting attachment resolution
authorRyan Lee <ryan.lee@canonical.com>
Thu, 1 May 2025 19:54:39 +0000 (12:54 -0700)
committerJohn Johansen <john.johansen@canonical.com>
Mon, 26 May 2025 03:14:53 +0000 (20:14 -0700)
commita88db916b8c77552f49f7d9f8744095ea01a268f
treeb286b8231653ef23436e1c832710a3fac7abbbb3
parent6c055e62560b958354625604293652753d82bcae
apparmor: fix loop detection used in conflicting attachment resolution

Conflicting attachment resolution is based on the number of states
traversed to reach an accepting state in the attachment DFA, accounting
for DFA loops traversed during the matching process. However, the loop
counting logic had multiple bugs:

 - The inc_wb_pos macro increments both position and length, but length
   is supposed to saturate upon hitting buffer capacity, instead of
   wrapping around.
 - If no revisited state is found when traversing the history, is_loop
   would still return true, as if there was a loop found the length of
   the history buffer, instead of returning false and signalling that
   no loop was found. As a result, the adjustment step of
   aa_dfa_leftmatch would sometimes produce negative counts with loop-
   free DFAs that traversed enough states.
 - The iteration in the is_loop for loop is supposed to stop before
   i = wb->len, so the conditional should be < instead of <=.

This patch fixes the above bugs as well as the following nits:
 - The count and size fields in struct match_workbuf were not used,
   so they can be removed.
 - The history buffer in match_workbuf semantically stores aa_state_t
   and not unsigned ints, even if aa_state_t is currently unsigned int.
 - The local variables in is_loop are counters, and thus should be
   unsigned ints instead of aa_state_t's.

Fixes: 21f606610502 ("apparmor: improve overlapping domain attachment resolution")
Signed-off-by: Ryan Lee <ryan.lee@canonical.com>
Co-developed-by: John Johansen <john.johansen@canonical.com>
Signed-off-by: John Johansen <john.johansen@canonical.com>
security/apparmor/include/match.h
security/apparmor/match.c