2
# ----------------------------------------------------------------------
3
# Copyright (c) 2017 Canonical Ltd. (All rights reserved)
5
# This program is free software; you can redistribute it and/or
6
# modify it under the terms of version 2 of the GNU General Public
7
# License published by the Free Software Foundation.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program. If not, see <http://www.gnu.org/licenses/>.
16
# ----------------------------------------------------------------------
18
APPARMOR_FUNCTIONS=/lib/apparmor/rc.apparmor.functions
19
APPARMORFS=/sys/kernel/security/apparmor
20
PROFILES="${APPARMORFS}/profiles"
21
REMOVE="${APPARMORFS}/.remove"
30
local msg="usage: ${progname} [options]\n
31
Remove profiles unknown to the system
34
-h, --help Show this help message and exit
35
-n Dry run; don't remove profiles"
37
if [ "$rc" -ne 0 ] ; then
46
if [ "$#" -gt 1 ] ; then
48
elif [ "$#" -eq 1 ] ; then
49
if [ "$1" = "-h" -o "$1" = "--help" ] ; then
51
elif [ "$1" = "-n" ] ; then
59
# We can't use a -r test here because while $PROFILES is world-readable,
60
# apparmorfs may still return EACCES from open()
62
# We have to do this check because error checking awk's getline() below is
63
# tricky and, as is, results in an infinite loop when apparmorfs returns an
65
if ! IFS= read line < "$PROFILES" ; then
66
echo "ERROR: Unable to read apparmorfs profiles file" 1>&2
68
elif [ ! -w "$REMOVE" ] ; then
69
echo "ERROR: Unable to write to apparmorfs remove file" 1>&2
73
# Clean out running profiles not associated with the current profile
74
# set, excluding the libvirt dynamically generated profiles.
75
# Note that we reverse sort the list of profiles to remove to
76
# ensure that child profiles (e.g. hats) are removed before the
77
# parent. We *do* need to remove the child profile and not rely
78
# on removing the parent profile when the profile has had its
79
# child profile names changed.
80
profiles_names_list | awk '
82
while (getline < "'${PROFILES}'" ) {
83
str = sub(/ \((enforce|complain)\)$/, "", $0);
84
if (match($0, /^libvirt-[0-9a-f\-]+$/) == 0)
89
{ if (length(arr[$0]) > 0) { delete arr[$0] } }
93
if (length(arr[key]) > 0) {
94
printf("%s\n", arr[key])
97
' | LC_COLLATE=C sort -r | \
98
while IFS= read profile ; do
99
if [ "$DRY_RUN" -ne 0 ]; then
100
echo "Would remove '${profile}'"
102
echo "Removing '${profile}'"
103
echo -n "$profile" > "${REMOVE}"
107
# will not catch all errors, but still better than nothing