m/selinux.fcontext_get_policy allow long filespecs

The previous logic of matching the output of `semanage fcontext --list` did not
allow for filespecs that were longer than 49 characters. This was due to the
output of the semanage tool not conforming to the expected output.

We used to expect that the after the filespec would be at least two spaces.
However, with long filespecs there is only a single space separating it and the
next field (the file type).

This modifies the regular expression that we use to match the line to accept one
or more spaces as field delimeters.

However, this causes problems when we attempt to split the three fields into a
python dictionary. We cannot use the same logic as previously of using the field
delimeter as the file type field itself can contain a space. Instead we use a
separate regular expression to parse the line into its component parts.

Fixes #45784.
This commit is contained in:
Ollie Armstrong 2018-01-30 18:05:44 +00:00 committed by rallytime
parent d20ff89414
commit a830a6e819
No known key found for this signature in database
GPG key ID: E8F1A4B90D0DEA19

View file

@ -453,7 +453,7 @@ def fcontext_get_policy(name, filetype=None, sel_type=None, sel_user=None, sel_l
'''
if filetype:
_validate_filetype(filetype)
re_spacer = '[ ]{2,}'
re_spacer = '[ ]+'
cmd_kwargs = {'spacer': re_spacer,
'filespec': re.escape(name),
'sel_user': sel_user or '[^:]+',
@ -466,11 +466,14 @@ def fcontext_get_policy(name, filetype=None, sel_type=None, sel_user=None, sel_l
current_entry_text = __salt__['cmd.shell'](cmd, ignore_retcode=True)
if current_entry_text == '':
return None
ret = {}
current_entry_list = re.split(re_spacer, current_entry_text)
ret['filespec'] = current_entry_list[0]
ret['filetype'] = current_entry_list[1]
ret.update(_context_string_to_dict(current_entry_list[2]))
parts = re.match(r'^({filespec}) +([a-z ]+) (.*)$'.format(**{'filespec': re.escape(name)}), current_entry_text)
ret = {
'filespec': parts.group(1),
'filetype': parts.group(2),
}
ret.update(_context_string_to_dict(parts.group(3)))
return ret