diff --git a/utils/apparmor/aa.py b/utils/apparmor/aa.py index 145e1b934..03aaaa5b4 100644 --- a/utils/apparmor/aa.py +++ b/utils/apparmor/aa.py @@ -2277,14 +2277,18 @@ def write_profile(profile, is_attachment=False): serialize_options = {'METADATA': True, 'is_attachment': is_attachment} profile_string = serialize_profile(split_to_merged(aa), profile, serialize_options) - with NamedTemporaryFile('w', suffix='~', delete=False, dir=profile_dir) as newprof: - if os.path.exists(prof_filename): - shutil.copymode(prof_filename, newprof.name) - else: - # permission_600 = stat.S_IRUSR | stat.S_IWUSR # Owner read and write - # os.chmod(newprof.name, permission_600) - pass - newprof.write(profile_string) + try: + with NamedTemporaryFile('w', suffix='~', delete=False, dir=profile_dir) as newprof: + if os.path.exists(prof_filename): + shutil.copymode(prof_filename, newprof.name) + else: + # permission_600 = stat.S_IRUSR | stat.S_IWUSR # Owner read and write + # os.chmod(newprof.name, permission_600) + pass + newprof.write(profile_string) + except PermissionError as e: + raise AppArmorException(e) + os.rename(newprof.name, prof_filename) if profile in changed: