mirror of
https://gitlab.com/apparmor/apparmor
synced 2025-09-02 15:25:27 +00:00
When changing flags, use correct amount of whitespace
.. instead of preserving the original leading whitespace. This change affects the behaviour of aa-complain, aa-enforce and aa-audit.
This commit is contained in:
@@ -624,6 +624,7 @@ def change_profile_flags(prof_filename, program, flag, set_flag):
|
|||||||
# TODO: change child profile flags even if program is specified
|
# TODO: change child profile flags even if program is specified
|
||||||
|
|
||||||
found = False
|
found = False
|
||||||
|
depth = -1
|
||||||
|
|
||||||
if not flag or (type_is_str(flag) and flag.strip() == ''):
|
if not flag or (type_is_str(flag) and flag.strip() == ''):
|
||||||
raise AppArmorBug('New flag for %s is empty' % prof_filename)
|
raise AppArmorBug('New flag for %s is empty' % prof_filename)
|
||||||
@@ -634,8 +635,8 @@ def change_profile_flags(prof_filename, program, flag, set_flag):
|
|||||||
with open_file_write(temp_file.name) as f_out:
|
with open_file_write(temp_file.name) as f_out:
|
||||||
for line in f_in:
|
for line in f_in:
|
||||||
if RE_PROFILE_START.search(line):
|
if RE_PROFILE_START.search(line):
|
||||||
|
depth += 1
|
||||||
matches = parse_profile_start_line(line, prof_filename)
|
matches = parse_profile_start_line(line, prof_filename)
|
||||||
space = matches['leadingspace'] or ''
|
|
||||||
profile = matches['profile']
|
profile = matches['profile']
|
||||||
old_flags = matches['flags']
|
old_flags = matches['flags']
|
||||||
newflags = ', '.join(add_or_remove_flag(old_flags, flag, set_flag))
|
newflags = ', '.join(add_or_remove_flag(old_flags, flag, set_flag))
|
||||||
@@ -657,11 +658,11 @@ def change_profile_flags(prof_filename, program, flag, set_flag):
|
|||||||
prof_storage['header_comment'] = matches['comment'] or ''
|
prof_storage['header_comment'] = matches['comment'] or ''
|
||||||
prof_storage['xattrs'] = matches['xattrs']
|
prof_storage['xattrs'] = matches['xattrs']
|
||||||
|
|
||||||
line = prof_storage.get_header(len(space)/2, profile, False, True)
|
line = prof_storage.get_header(depth, profile, False, True)
|
||||||
line = '%s\n' % line[0]
|
line = '%s\n' % line[0]
|
||||||
elif RE_PROFILE_HAT_DEF.search(line):
|
elif RE_PROFILE_HAT_DEF.search(line):
|
||||||
|
depth += 1
|
||||||
matches = RE_PROFILE_HAT_DEF.search(line)
|
matches = RE_PROFILE_HAT_DEF.search(line)
|
||||||
space = matches.group('leadingspace') or ''
|
|
||||||
hat_keyword = matches.group('hat_keyword')
|
hat_keyword = matches.group('hat_keyword')
|
||||||
hat = matches.group('hat')
|
hat = matches.group('hat')
|
||||||
old_flags = matches['flags']
|
old_flags = matches['flags']
|
||||||
@@ -670,10 +671,14 @@ def change_profile_flags(prof_filename, program, flag, set_flag):
|
|||||||
if comment:
|
if comment:
|
||||||
comment = ' %s' % comment
|
comment = ' %s' % comment
|
||||||
|
|
||||||
|
space = depth * 2 * ' '
|
||||||
if newflags:
|
if newflags:
|
||||||
line = '%s%s%s flags=(%s) {%s\n' % (space, hat_keyword, hat, newflags, comment)
|
line = '%s%s%s flags=(%s) {%s\n' % (space, hat_keyword, hat, newflags, comment)
|
||||||
else:
|
else:
|
||||||
line = '%s%s%s {%s\n' % (space, hat_keyword, hat, comment)
|
line = '%s%s%s {%s\n' % (space, hat_keyword, hat, comment)
|
||||||
|
elif RE_PROFILE_END.search(line):
|
||||||
|
depth -= 1
|
||||||
|
|
||||||
f_out.write(line)
|
f_out.write(line)
|
||||||
os.rename(temp_file.name, prof_filename)
|
os.rename(temp_file.name, prof_filename)
|
||||||
|
|
||||||
|
@@ -257,7 +257,7 @@ class AaTest_change_profile_flags(AaTestWithTempdir):
|
|||||||
dummy_profile_content = ' #include <abstractions/base>\n capability chown,\n /bar r,'
|
dummy_profile_content = ' #include <abstractions/base>\n capability chown,\n /bar r,'
|
||||||
prof_template = '%s%s%s {%s\n%s\n%s\n}\n'
|
prof_template = '%s%s%s {%s\n%s\n%s\n}\n'
|
||||||
old_prof = prof_template % (whitespace, profile, old_flags, comment, more_rules, dummy_profile_content)
|
old_prof = prof_template % (whitespace, profile, old_flags, comment, more_rules, dummy_profile_content)
|
||||||
new_prof = prof_template % (whitespace, profile, expected_flags, comment, expected_more_rules, dummy_profile_content)
|
new_prof = prof_template % ('', profile, expected_flags, comment, expected_more_rules, dummy_profile_content)
|
||||||
|
|
||||||
self.file = write_file(self.tmpdir, 'profile', old_prof)
|
self.file = write_file(self.tmpdir, 'profile', old_prof)
|
||||||
change_profile_flags(self.file, profile_name, flags_to_change, set_flag)
|
change_profile_flags(self.file, profile_name, flags_to_change, set_flag)
|
||||||
@@ -333,7 +333,7 @@ class AaTest_change_profile_flags(AaTestWithTempdir):
|
|||||||
def test_change_profile_flags_with_hat_03(self):
|
def test_change_profile_flags_with_hat_03(self):
|
||||||
self._test_change_profile_flags('/foo', 'flags=(complain)', 'audit', True, 'audit, complain',
|
self._test_change_profile_flags('/foo', 'flags=(complain)', 'audit', True, 'audit, complain',
|
||||||
more_rules='\n^foobar (attach_disconnected) { # comment\n}\n',
|
more_rules='\n^foobar (attach_disconnected) { # comment\n}\n',
|
||||||
expected_more_rules='\n^foobar flags=(attach_disconnected, audit) { # comment\n}\n'
|
expected_more_rules='\n ^foobar flags=(attach_disconnected, audit) { # comment\n}\n'
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_change_profile_flags_with_hat_04(self):
|
def test_change_profile_flags_with_hat_04(self):
|
||||||
|
Reference in New Issue
Block a user