/usr/lib/python3/dist-packages/apparmor/aamode.py is in python3-apparmor 2.10.95-0ubuntu2.6~14.04.4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 | # ----------------------------------------------------------------------
# Copyright (C) 2013 Kshitij Gupta <kgupta8592@gmail.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of version 2 of the GNU General Public
# License as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# ----------------------------------------------------------------------
import re
from apparmor.common import AppArmorBug
def AA_OTHER(mode):
other = set()
for i in mode:
other.add('::%s' % i)
return other
def AA_OTHER_REMOVE(mode):
other = set()
for i in mode:
if '::' in i:
other.add(i[2:])
return other
AA_MAY_EXEC = set('x')
AA_MAY_WRITE = set('w')
AA_MAY_READ = set('r')
AA_MAY_APPEND = set('a')
AA_MAY_LINK = set('l')
AA_MAY_LOCK = set('k')
AA_EXEC_MMAP = set('m')
AA_EXEC_UNSAFE = set(['execunsafe'])
AA_EXEC_INHERIT = set('i')
AA_EXEC_UNCONFINED = set('U')
AA_EXEC_PROFILE = set('P')
AA_EXEC_CHILD = set('C')
AA_EXEC_NT = set('N')
AA_LINK_SUBSET = set(['linksubset'])
AA_BARE_FILE_MODE = set(['bare_file_mode'])
#AA_OTHER_SHIFT = 14
#AA_USER_MASK = 16384 - 1
AA_EXEC_TYPE = (AA_MAY_EXEC | AA_EXEC_UNSAFE | AA_EXEC_INHERIT |
AA_EXEC_UNCONFINED | AA_EXEC_PROFILE | AA_EXEC_CHILD | AA_EXEC_NT)
ALL_AA_EXEC_TYPE = AA_EXEC_TYPE
MODE_HASH = {'x': AA_MAY_EXEC, 'X': AA_MAY_EXEC,
'w': AA_MAY_WRITE, 'W': AA_MAY_WRITE,
'r': AA_MAY_READ, 'R': AA_MAY_READ,
'a': AA_MAY_APPEND, 'A': AA_MAY_APPEND,
'l': AA_MAY_LINK, 'L': AA_MAY_LINK,
'k': AA_MAY_LOCK, 'K': AA_MAY_LOCK,
'm': AA_EXEC_MMAP, 'M': AA_EXEC_MMAP,
'i': AA_EXEC_INHERIT, 'I': AA_EXEC_INHERIT,
'u': AA_EXEC_UNCONFINED | AA_EXEC_UNSAFE, # Unconfined + Unsafe
'U': AA_EXEC_UNCONFINED,
'p': AA_EXEC_PROFILE | AA_EXEC_UNSAFE, # Profile + unsafe
'P': AA_EXEC_PROFILE,
'c': AA_EXEC_CHILD | AA_EXEC_UNSAFE, # Child + Unsafe
'C': AA_EXEC_CHILD,
'n': AA_EXEC_NT | AA_EXEC_UNSAFE,
'N': AA_EXEC_NT
}
LOG_MODE_RE = re.compile('^(r|w|l|m|k|a|x|ix|ux|px|pux|cx|nx|pix|cix|Ux|Px|PUx|Cx|Nx|Pix|Cix)+$')
MODE_MAP_SET = {"r", "w", "l", "m", "k", "a", "x", "i", "u", "p", "c", "n", "I", "U", "P", "C", "N"}
def str_to_mode(string):
if not string:
return set()
user, other = split_log_mode(string)
if not user:
user = other
mode = sub_str_to_mode(user)
#print(string, mode)
#print(string, 'other', sub_str_to_mode(other))
mode |= (AA_OTHER(sub_str_to_mode(other)))
#print (string, mode)
#print('str_to_mode:', mode)
return mode
def sub_str_to_mode(string):
mode = set()
for mode_char in string:
if mode_char in MODE_MAP_SET and MODE_HASH.get(mode_char, False):
mode |= MODE_HASH[mode_char]
else:
raise AppArmorBug("Mode string '%s' contains invalid char '%s'" % (string, mode_char))
return mode
def split_log_mode(mode):
#if the mode has a "::", then the left side is the user mode, and the right side is the other mode
#if not, then the mode is both the user and other mode
user = ''
other = ''
if "::" in mode:
try:
user, other = mode.split("::")
except ValueError as e:
raise AppArmorBug("Got ValueError '%s' when splitting %s" % (str(e), mode))
else:
user = mode
other = mode
return user, other
def mode_contains(mode, subset):
# w implies a
if mode & AA_MAY_WRITE:
mode |= AA_MAY_APPEND
if mode & (AA_OTHER(AA_MAY_WRITE)):
mode |= (AA_OTHER(AA_MAY_APPEND))
return (mode & subset) == subset
def contains(mode, string):
return mode_contains(mode, str_to_mode(string))
def validate_log_mode(mode):
if LOG_MODE_RE.search(mode):
return True
else:
return False
def hide_log_mode(mode):
mode = mode.replace('::', '')
return mode
def print_mode(mode):
user, other = split_mode(mode)
string = sub_mode_to_str(user) + '::' + sub_mode_to_str(other)
return string
def sub_mode_to_str(mode):
string = ''
# w(write) implies a(append)
if mode & AA_MAY_WRITE:
mode = mode - AA_MAY_APPEND
#string = ''.join(mode)
if mode & AA_EXEC_MMAP:
string += 'm'
if mode & AA_MAY_READ:
string += 'r'
if mode & AA_MAY_WRITE:
string += 'w'
if mode & AA_MAY_APPEND:
string += 'a'
if mode & AA_MAY_LINK:
string += 'l'
if mode & AA_MAY_LOCK:
string += 'k'
# modes P and C must appear before I and U else invalid syntax
if mode & (AA_EXEC_PROFILE | AA_EXEC_NT):
if mode & AA_EXEC_UNSAFE:
string += 'p'
else:
string += 'P'
if mode & AA_EXEC_CHILD:
if mode & AA_EXEC_UNSAFE:
string += 'c'
else:
string += 'C'
if mode & AA_EXEC_UNCONFINED:
if mode & AA_EXEC_UNSAFE:
string += 'u'
else:
string += 'U'
if mode & AA_EXEC_INHERIT:
string += 'i'
if mode & AA_MAY_EXEC:
string += 'x'
return string
def is_user_mode(mode):
user, other = split_mode(mode)
if user and not other:
return True
else:
return False
def split_mode(mode):
user = set()
for i in mode:
if not '::' in i:
user.add(i)
other = mode - user
other = AA_OTHER_REMOVE(other)
return user, other
def mode_to_str(mode):
mode = flatten_mode(mode)
return sub_mode_to_str(mode)
def flatten_mode(mode):
if not mode:
return set()
user, other = split_mode(mode)
mode = user | other
mode |= (AA_OTHER(mode))
return mode
def owner_flatten_mode(mode):
mode = flatten_mode(mode)
return mode
def mode_to_str_user(mode):
user, other = split_mode(mode)
string = ''
if not user:
user = set()
if not other:
other = set()
if user - other:
if other:
string = sub_mode_to_str(other) + '+'
string += 'owner ' + sub_mode_to_str(user - other)
elif is_user_mode(mode):
string = 'owner ' + sub_mode_to_str(user)
else:
string = sub_mode_to_str(flatten_mode(mode))
return string
def log_str_to_mode(profile, string, nt_name):
mode = str_to_mode(string)
# If contains nx and nix
#print (profile, string, nt_name)
if contains(mode, 'Nx'):
# Transform to px, cx
match = re.search('(.+?)//(.+?)', nt_name)
if match:
lprofile, lhat = match.groups()
tmode = 0
if lprofile == profile:
if mode & AA_MAY_EXEC:
tmode = str_to_mode('Cx::')
if mode & AA_OTHER(AA_MAY_EXEC):
tmode |= str_to_mode('Cx')
nt_name = lhat
else:
if mode & AA_MAY_EXEC:
tmode = str_to_mode('Px::')
if mode & AA_OTHER(AA_MAY_EXEC):
tmode |= str_to_mode('Px')
nt_name = lhat
mode = mode - str_to_mode('Nx')
mode |= tmode
return mode, nt_name
|