aa-unconfined: Fix race when reading proc/*/attr/current

aa-unconfined can fault if it looses the race between checkking if
proc/*/attr/{apparmor/,}current exists, and actually opening the file.
Catch open/file errors and ignore them like the file doesn't exist.

Fixes: https://gitlab.com/apparmor/apparmor/-/issues/355
Signed-off-by: John Johansen <john.johansen@canonical.com>
This commit is contained in:
John Johansen 2024-02-24 20:33:35 -08:00
parent 95d9ba8d8b
commit c4f649da92

View file

@ -101,13 +101,18 @@ def get_pids_netstat(netstat='netstat'):
def read_proc_current(filename):
attr = None
if os.path.exists(filename):
try:
# don't bother with if os.path.exists(filename): there is always a race
with open_file_read(filename) as current:
for line in current:
line = line.strip()
if line.endswith(' (complain)', 1) or line.endswith(' (enforce)', 1) or line.endswith(' (kill)', 1): # enforce at least one char as profile name
# intentionally not checking for '(unconfined)', because $binary confined by $profile (unconfined) would look very confusing
attr = line
except OSError:
# just ignore errors atm
# print("Error trying to open {filename}")
return None
return attr