Ejemplo n.º 1
0
def delete_path_duplicates(profile, profile_other, allow, same_profile=True):
    deleted = []
    # Check if any individual rule makes any rule superfluous
    for rule in profile[allow]['path'].keys():
        for entry in profile_other[allow]['path'].keys():
            if rule == entry:
                # Check the modes
                cm = profile[allow]['path'][rule]['mode']
                am = profile[allow]['path'][rule]['audit']
                # If modes of rule are a superset of rules implied by entry we can safely remove it
                if apparmor.mode_contains(cm, profile_other[allow]['path'][entry]['mode']) and apparmor.mode_contains(am, profile_other[allow]['path'][entry]['audit']):
                    if not same_profile:
                        deleted.append(entry)
                continue
            if re_match_include(rule) or re_match_include(entry):
                continue
            # Check if the rule implies entry
            if apparmor.matchliteral(rule, entry):
                # Check the modes
                cm = profile[allow]['path'][rule]['mode']
                am = profile[allow]['path'][rule]['audit']
                # If modes of rule are a superset of rules implied by entry we can safely remove it
                if apparmor.mode_contains(cm, profile_other[allow]['path'][entry]['mode']) and apparmor.mode_contains(am, profile_other[allow]['path'][entry]['audit']):
                    deleted.append(entry)

    for entry in deleted:
        profile_other[allow]['path'].pop(entry)

    return len(deleted)
Ejemplo n.º 2
0
def delete_path_duplicates(profile, profile_other, allow, same_profile=True):
    deleted = []
    # Check if any individual rule makes any rule superfluous
    for rule in profile[allow]['path'].keys():
        for entry in profile_other[allow]['path'].keys():
            if rule == entry:
                # Check the modes
                cm = profile[allow]['path'][rule]['mode']
                am = profile[allow]['path'][rule]['audit']
                # If modes of rule are a superset of rules implied by entry we can safely remove it
                if apparmor.mode_contains(
                        cm, profile_other[allow]['path'][entry]
                    ['mode']) and apparmor.mode_contains(
                        am, profile_other[allow]['path'][entry]['audit']):
                    if not same_profile:
                        deleted.append(entry)
                continue
            if re_match_include(rule) or re_match_include(entry):
                continue
            # Check if the rule implies entry
            if apparmor.matchliteral(rule, entry):
                # Check the modes
                cm = profile[allow]['path'][rule]['mode']
                am = profile[allow]['path'][rule]['audit']
                # If modes of rule are a superset of rules implied by entry we can safely remove it
                if apparmor.mode_contains(
                        cm, profile_other[allow]['path'][entry]
                    ['mode']) and apparmor.mode_contains(
                        am, profile_other[allow]['path'][entry]['audit']):
                    deleted.append(entry)

    for entry in deleted:
        profile_other[allow]['path'].pop(entry)

    return len(deleted)
Ejemplo n.º 3
0
 def load_variables(self, prof_path):
     """Loads the variables for the given profile"""
     if os.path.isfile(prof_path):
         with open_file_read(prof_path) as f_in:
             for line in f_in:
                 line = line.strip()
                 # If any includes, load variables from them first
                 match = re_match_include(line)
                 if match:
                     new_path = self.PROF_DIR + '/' + match
                     self.load_variables(new_path)
                 else:
                     # Remove any comments
                     if '#' in line:
                         line = line.split('#')[0].rstrip()
                     # Expected format is @{Variable} = value1 value2 ..
                     if line.startswith('@') and '=' in line:
                         if '+=' in line:
                             line = line.split('+=')
                             try:
                                 self.severity['VARIABLES'][line[0]] += [i.strip('"') for i in line[1].split()]
                             except KeyError:
                                 raise AppArmorException("Variable %s was not previously declared, but is being assigned additional value in file: %s" % (line[0], prof_path))
                         else:
                             line = line.split('=')
                             if line[0] in self.severity['VARIABLES'].keys():
                                 raise AppArmorException("Variable %s was previously declared in file: %s" % (line[0], prof_path))
                             self.severity['VARIABLES'][line[0]] = [i.strip('"') for i in line[1].split()]
Ejemplo n.º 4
0
 def load_variables(self, prof_path):
     """Loads the variables for the given profile"""
     if os.path.isfile(prof_path):
         with open_file_read(prof_path) as f_in:
             for line in f_in:
                 line = line.strip()
                 # If any includes, load variables from them first
                 match = re_match_include(line)
                 if match:
                     new_path = match
                     if not new_path.startswith('/'):
                         new_path = self.PROF_DIR + '/' + match
                     self.load_variables(new_path)
                 else:
                     # Remove any comments
                     if '#' in line:
                         line = line.split('#')[0].rstrip()
                     # Expected format is @{Variable} = value1 value2 ..
                     if line.startswith('@') and '=' in line:
                         if '+=' in line:
                             line = line.split('+=')
                             try:
                                 self.severity['VARIABLES'][line[0]] += [
                                     i.strip('"') for i in line[1].split()
                                 ]
                             except KeyError:
                                 raise AppArmorException(
                                     "Variable %s was not previously declared, but is being assigned additional value in file: %s"
                                     % (line[0], prof_path))
                         else:
                             line = line.split('=')
                             if line[0] in self.severity['VARIABLES'].keys(
                             ):
                                 raise AppArmorException(
                                     "Variable %s was previously declared in file: %s"
                                     % (line[0], prof_path))
                             self.severity['VARIABLES'][line[0]] = [
                                 i.strip('"') for i in line[1].split()
                             ]
 def _run_test(self, params, expected):
     with self.assertRaises(expected):
         re_match_include(params)
 def _run_test(self, params, expected):
     self.assertEqual(re_match_include(params), expected)
Ejemplo n.º 7
0
 def _run_test(self, params, expected):
     with self.assertRaises(expected):
         re_match_include(params)
Ejemplo n.º 8
0
 def _run_test(self, params, expected):
     self.assertEqual(re_match_include(params), expected)