File: aa-remove-unknown

package info (click to toggle)
apparmor 4.1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 30,096 kB
  • sloc: ansic: 24,943; python: 24,914; cpp: 9,074; sh: 8,166; yacc: 2,061; makefile: 1,923; lex: 1,215; pascal: 1,147; perl: 1,033; ruby: 365; lisp: 282; exp: 250; java: 212; xml: 159
file content (117 lines) | stat: -rwxr-xr-x 3,228 bytes parent folder | download | duplicates (2)
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
#!/bin/sh
# ----------------------------------------------------------------------
#    Copyright (c) 2017 Canonical Ltd. (All rights reserved)
#
#    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 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.
#
#    You should have received a copy of the GNU General Public License
#    along with this program. If not, see <http://www.gnu.org/licenses/>.
# ----------------------------------------------------------------------

APPARMOR_FUNCTIONS=/lib/apparmor/rc.apparmor.functions
APPARMORFS=/sys/kernel/security/apparmor
PROFILES="${APPARMORFS}/profiles"
REMOVE="${APPARMORFS}/.remove"

DRY_RUN=0

. $APPARMOR_FUNCTIONS

usage() {
	local progname="$0"
	local rc="$1"
	local msg="usage: ${progname} [options]

Remove profiles unknown to the system

Options:
 -h, --help	Show this help message and exit
 -n		Dry run; don't remove profiles"

	if [ "$rc" -ne 0 ] ; then
		echo "$msg" 1>&2
	else
		echo "$msg"
	fi

	exit "$rc"
}

if [ "$#" -gt 1 ] ; then
	usage 1
elif [ "$#" -eq 1 ] ; then
	if [ "$1" = "-h" ] || [ "$1" = "--help" ] ; then
		usage 0
	elif [ "$1" = "-n" ] ; then
		DRY_RUN=1
	else
		usage 1
	fi
fi


# We can't use a -r test here because while $PROFILES is world-readable,
# apparmorfs may still return EACCES from open()
#
# We have to do this check because error checking awk's getline() below is
# tricky and, as is, results in an infinite loop when apparmorfs returns an
# error from open().
if ! true < "$PROFILES" ; then
	echo "ERROR: Unable to read apparmorfs profiles file" 1>&2
	exit 1
elif [ ! -w "$REMOVE" ] ; then
	echo "ERROR: Unable to write to apparmorfs remove file" 1>&2
	exit 1
fi

# Clean out running profiles not associated with the current profile
# set, excluding the libvirt dynamically generated profiles.
# Note that we reverse sort the list of profiles to remove to
# ensure that child profiles (e.g. hats) are removed before the
# parent. We *do* need to remove the child profile and not rely
# on removing the parent profile when the profile has had its
# child profile names changed.

# shellcheck disable=SC2086
LOADED_PROFILES=$("$PARSER" -N $PROFILE_DIRS) || {
	ret=$?
	echo 'apparmor_parser exited with failure, aborting.' >&2
	exit $ret
}

echo "$LOADED_PROFILES" | awk '
BEGIN {
  while (getline < "'${PROFILES}'" ) {
    sub(/ \((enforce|complain|prompt|kill|unconfined)\)$/, "", $0);
    if (match($0, /^libvirt-[0-9a-f\-]+$/) == 0)
      arr[$0] = $0
  }
}

{ if (length(arr[$0]) > 0) { delete arr[$0] } }

END {
  for (key in arr)
    if (length(arr[key]) > 0) {
      printf("%s\n", arr[key])
    }
}
' | LC_COLLATE=C sort -r | \
	while IFS= read -r profile ; do
		if [ "$DRY_RUN" -ne 0 ]; then
			echo "Would remove '${profile}'"
		else
			echo "Removing '${profile}'"
			echo -n "$profile" > "${REMOVE}"
		fi
	done

# will not catch all errors, but still better than nothing
exit $?