blob: 5e9edb9dd5094136751a00b1fc4ad073e69ae695 (
plain)
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
|
#!/bin/sh
log_write() {
local facility=kern.$1
logger -t AppArmor -p $facility "$2"
}
AA_STATUS=/usr/sbin/aa-status
SECURITYFS=/sys/kernel/security
SFS_MOUNTPOINT="${SECURITYFS}/apparmor"
PARSER=/sbin/apparmor_parser
PARSER_OPTS=
ADDITIONAL_PROFILE_DIR=
[ -d /etc/apparmor.d ] && PROFILE_DIRS=/etc/apparmor.d ||
log_write warning "Unable to find profiles: /etc/apparmor.d"
[ -n "$ADDITIONAL_PROFILE_DIR" ] && [ -d "$ADDITIONAL_PROFILE_DIR" ] &&
PROFILE_DIRS="$PROFILE_DIRS $ADDITIONAL_PROFILE_DIR"
dir_is_empty() {
[ "$(du -s $1 | cut -f 1)" -eq 0 ] && return 0 || return 1
}
profiles_loaded_count() {
[ -f ${SFS_MOUNTPOINT}/profiles ] &&
return $(cat "${SFS_MOUNTPOINT}/profiles" | wc -l) || return 0
}
is_profiles_loaded() {
[ -f ${SFS_MOUNTPOINT}/profiles ] && {
rc=$(cat "${SFS_MOUNTPOINT}/profiles" | wc -l)
[ "$rc" -ne 0 ] && return 0 || return 1
}
return 1
}
is_container_with_internal_policy() {
local ns_stacked_path="${SFS_MOUNTPOINT}/.ns_stacked"
local ns_name_path="${SFS_MOUNTPOINT}/.ns_name"
local ns_stacked
local ns_name
if ! [ -f "$ns_stacked_path" ] || ! [ -f "$ns_name_path" ]; then
return 1
fi
read -r ns_stacked < "$ns_stacked_path"
if [ "$ns_stacked" != "yes" ]; then
return 1
fi
# LXD and LXC set up AppArmor namespaces starting with "lxd-" and
# "lxc-", respectively. Return non-zero for all other namespace
# identifiers.
read -r ns_name < "$ns_name_path"
if [ "${ns_name#lxd-*}" = "$ns_name" ] && \
[ "${ns_name#lxc-*}" = "$ns_name" ]; then
return 1
fi
return 0
}
skip_profile() {
local profile="$1"
if [ "${profile%.rpmnew}" != "$profile" ] || \
[ "${profile%.rpmsave}" != "$profile" ] || \
[ "${profile%.orig}" != "$profile" ] || \
[ "${profile%.rej}" != "$profile" ] || \
[ "${profile%\~}" != "$profile" ] ; then
return 1
fi
# Silently ignore the dpkg, pacman, ipk and xbps files
if [ "${profile%.dpkg-new}" != "$profile" ] || \
[ "${profile%.dpkg-old}" != "$profile" ] || \
[ "${profile%.dpkg-dist}" != "$profile" ] || \
[ "${profile%.dpkg-bak}" != "$profile" ] || \
[ "${profile%.dpkg-remove}" != "$profile" ] || \
[ "${profile%.ipk}" != "$profile" ] || \
[ "${profile%.ipk-new}" != "$profile" ] || \
[ "${profile%.ipk-old}" != "$profile" ] || \
[ "${profile%.ipk-dist}" != "$profile" ] || \
[ "${profile%.ipk-bak}" != "$profile" ] || \
[ "${profile%.ipk-remove}" != "$profile" ] || \
[ "${profile%.pacsave}" != "$profile" ] || \
[ "${profile%.pacnew}" != "$profile" ] ; then
return 2
fi
$(echo "$profile" | grep -E -q '^.+\.new-[0-9\.]+_[0-9]+$'); [ "$?" -eq 0 ] && return 2
return 0
}
__parse_profiles_dir() {
local parser_cmd="$1"
local profile_dir="$2"
local status=0
[ -x "$PARSER" ] || {
log_write err "Unable to execute AppArmor parser"
return 1
}
[ -d "$profile_dir" ] || {
log_write warning "AppArmor profiles not found: $profile_dir"
return 1
}
dir_is_empty "$profile_dir"; [ "$?" -eq 0 ] && {
log_write err "No profiles found in $profile_dir"
return 1
}
local nprocs=$(cat /proc/cpuinfo |grep "processor\t:"|wc -l)
local rc=0
local xargs_args=""
[ "$nprocs" -ge 2 ] && xargs_args="--max-procs=$nprocs"
"$PARSER" $PARSER_OPTS "$parser_cmd" -- "$profile_dir" || {
for profile in "$profile_dir"/*; do
skip_profile "$profile"
skip=$?
[ "$skip" -ne 0 ] && {
[ "$skip" -ne 2 ] && log_write info "Skipped loading profile $profile"
continue
}
[ -f "$profile" ] || continue
echo "$profile"
done | \
# Use xargs to parallelize calls to the parser over all CPUs
/usr/libexec/xargs-findutils -n1 -d"\n" $xargs_args \
"$PARSER" $PARSER_OPTS "$parser_cmd" --
[ "$?" -ne 0 ] && {
rc=1
log_write err "At least one profile failed to load"
}
}
return $rc
}
parse_profiles() {
case "$1" in
load)
PARSER_CMD="--add"
PARSER_MSG="Loading profiles"
;;
reload)
PARSER_CMD="--replace"
PARSER_MSG="Reloading profiles"
;;
*)
log_write err "Unknown parameter $1"
log_write info "parse_profiles parameter must be either 'load' or 'reload'"
return 1
;;
esac
log_write info "$PARSER_MSG"
[ -w "$SFS_MOUNTPOINT/.load" ] || {
log_write err "${SFS_MOUNTPOINT}/.load not writable"
return 1
}
[ -f "$PARSER" ] || {
log_write err "AppArmor parser not found"
return 1
}
# run parser on all profiles
local rc=0
for profile_dir in $PROFILE_DIRS; do
__parse_profiles_dir "$PARSER_CMD" "$profile_dir" || rc=$?
done
return $rc
}
is_apparmor_loaded() {
is_securityfs_mounted; [ "$?" -eq 0 ] || {
mount_securityfs
}
[ -f "${SFS_MOUNTPOINT}/profiles" ] && return 0
[ -d /sys/module/apparmor ] && return 0 || return 1
}
is_securityfs_mounted() {
[ -d "$SECURITYFS" ] && {
grep -q securityfs /proc/filesystems && grep -q securityfs /proc/mounts
return $?
}
return 1
}
mount_securityfs() {
local rc=0
grep -q securityfs /proc/filesystems; [ "$?" -eq 0 ] && {
mount -t securityfs securityfs "$SECURITYFS"
rc=$?
[ "$rc" -eq 0 ] && log_write info "Mounting securityfs" ||
log_write err "Failed to mount securityfs"
}
return $rc
}
apparmor_start() {
local announced=0
is_securityfs_mounted; [ "$?" -ne 0 ] && {
log_write info "Starting AppArmor"
announced=1
mount_securityfs; [ "$?" -eq 0 ] || return $?
}
is_apparmor_loaded; [ "$?" -eq 0 ] || {
[ "$announced" -eq 0 ] && log_write info "Starting AppArmor"
announced=1
log_write err "AppArmor kernel support is not present"
return 1
}
[ -d /var/lib/apparmor ] || mkdir -p /var/lib/apparmor > /dev/null
is_profiles_loaded; [ "$?" -eq 0 ] || {
[ "$announced" -eq 0 ] && log_write info "Starting AppArmor"
announced=1
parse_profiles load
return $?
} || {
parse_profiles reload
return $?
}
}
remove_profiles() {
log_write info "Unloading profiles"
is_apparmor_loaded; [ "$?" -eq 0 ] || {
log_write err "AppArmor kernel support is not present"
return 1
}
[ -w "$SFS_MOUNTPOINT/.remove" ] || {
log_write err "${SFS_MOUNTPOINT}/.remove not writable"
return 1
}
[ -x "$PARSER" ] || {
log_write err "Unable to execute AppArmor parser"
return 1
}
local rc=0
sed -e "s/ (\(enforce\|complain\))$//" "$SFS_MOUNTPOINT/profiles" | \
LC_COLLATE=C sort | grep -v // | {
while read -r profile ; do
printf "%s" "$profile" > "$SFS_MOUNTPOINT/.remove"
result=$?
[ "$result" -eq 0 ] || rc=$result
done
}
return $rc
}
apparmor_stop() {
is_apparmor_loaded; [ "$?" -eq 0 ] || return 1
is_profiles_loaded; [ "$?" -eq 0 ] && {
log_write info "Stopping AppArmor"
remove_profiles
return $?
} || return 0
}
apparmor_restart() {
is_profiles_loaded; [ "$?" -eq 0 ] || {
apparmor_start
return $?
}
is_apparmor_loaded; [ "$?" -eq 0 ] || {
apparmor_start
return $?
}
log_write info "Restarting AppArmor"
parse_profiles reload
return $?
}
apparmor_reload() {
is_profiles_loaded; [ "$?" -eq 0 ] || {
apparmor_start
return $?
}
is_apparmor_loaded; [ "$?" -eq 0 ] || {
apparmor_start
return $?
}
log_write info "Reloading AppArmor"
parse_profiles reload
return $?
}
apparmor_list_profiles() {
is_apparmor_loaded; [ "$?" -eq 0 ] || {
echo "AppArmor kernel support is not present"
return 1
}
[ -x "$PARSER" ] || {
echo "Unable to execute AppArmor parser"
return 1
}
# run parser on all profiles
for profile_dir in $PROFILE_DIRS; do
[ -d "$profile_dir" ] || {
echo "AppArmor profiles not found: $profile_dir"
continue
}
for profile in "$profile_dir"/*; do
if skip_profile "$profile" && [ -f "$profile" ] ; then
LIST_ADD=$("$PARSER" -N "$profile" )
[ "$?" -eq 0 ] && echo "$LIST_ADD"
fi
done
done
return 0
}
apparmor_status() {
is_apparmor_loaded; [ "$?" -eq 0 ] || {
echo "AppArmor kernel support is not present"
return 1
}
[ -x "$AA_STATUS" ] && {
"$AA_STATUS" --verbose
return $?
}
echo "AppArmor is enabled."
echo "Install apparmor-utils to receive more detailed status"
echo "information or examine $SFS_MOUNTPOINT directly."
return 0
}
|