~talkless/apparmor/fix_user_download_nonlatin

« back to all changes in this revision

Viewing changes to utils/aa-remove-unknown

  • Committer: Tyler Hicks
  • Date: 2017-03-24 05:08:01 UTC
  • Revision ID: tyhicks@canonical.com-20170324050801-6p7c40m8d44ase9c
utils: Add aa-remove-unknown utility to unload unknown profiles

https://launchpad.net/bugs/1668892

This patch creates a new utility, with the code previously used in the
init script 'restart' action, that removes unknown profiles which are
not found in /etc/apparmor.d/. The functionality was removed from the
common init script code in the fix for CVE-2017-6507.

The new utility prints a message containing the name of each unknown
profile before the profiles are removed. It also supports a dry run mode
so that an administrator can check which profiles will be removed before
unloading any unknown profiles.

If you backport this utility with the fix for CVE-2017-6507 to an
apparmor 2.10 release and your backported aa-remove-unknown utility is
sourcing the upstream rc.apparmor.functions file, you'll want to include
the following bug fix to prevent the aa-remove-unknown utility from
removing child profiles that it shouldn't remove:

  r3440 - Fix: parser: incorrect output of child profile names

Signed-off-by: Tyler Hicks <tyhicks@canonical.com>
Acked-by: Seth Arnold <seth.arnold@canonical.com>
Acked-by: John Johansen <john.johansen@canonical.com>

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/bin/sh
 
2
# ----------------------------------------------------------------------
 
3
#    Copyright (c) 2017 Canonical Ltd. (All rights reserved)
 
4
#
 
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.
 
8
#
 
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.
 
13
#
 
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
# ----------------------------------------------------------------------
 
17
 
 
18
APPARMOR_FUNCTIONS=/lib/apparmor/rc.apparmor.functions
 
19
APPARMORFS=/sys/kernel/security/apparmor
 
20
PROFILES="${APPARMORFS}/profiles"
 
21
REMOVE="${APPARMORFS}/.remove"
 
22
 
 
23
DRY_RUN=0
 
24
 
 
25
. $APPARMOR_FUNCTIONS
 
26
 
 
27
usage() {
 
28
        local progname="$1"
 
29
        local rc="$2"
 
30
        local msg="usage: ${progname} [options]\n
 
31
Remove profiles unknown to the system
 
32
 
 
33
Options:
 
34
 -h, --help     Show this help message and exit
 
35
 -n             Dry run; don't remove profiles"
 
36
 
 
37
        if [ "$rc" -ne 0 ] ; then
 
38
                echo "$msg" 1>&2
 
39
        else
 
40
                echo "$msg"
 
41
        fi
 
42
 
 
43
        exit "$rc"
 
44
}
 
45
 
 
46
if [ "$#" -gt 1 ] ; then
 
47
        usage "$0" 1
 
48
elif [ "$#" -eq 1 ] ; then
 
49
        if [ "$1" = "-h" -o "$1" = "--help" ] ; then
 
50
                usage "$0" 0
 
51
        elif [ "$1" = "-n" ] ; then
 
52
                DRY_RUN=1
 
53
        else
 
54
                usage "$0" 1
 
55
        fi
 
56
fi
 
57
 
 
58
 
 
59
# We can't use a -r test here because while $PROFILES is world-readable,
 
60
# apparmorfs may still return EACCES from open()
 
61
#
 
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
 
64
# error from open().
 
65
if ! IFS= read line < "$PROFILES" ; then
 
66
        echo "ERROR: Unable to read apparmorfs profiles file" 1>&2
 
67
        exit 1
 
68
elif [ ! -w "$REMOVE" ] ; then
 
69
        echo "ERROR: Unable to write to apparmorfs remove file" 1>&2
 
70
        exit 1
 
71
fi
 
72
 
 
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 '
 
81
BEGIN {
 
82
  while (getline < "'${PROFILES}'" ) {
 
83
    str = sub(/ \((enforce|complain)\)$/, "", $0);
 
84
    if (match($0, /^libvirt-[0-9a-f\-]+$/) == 0)
 
85
      arr[$str] = $str
 
86
  }
 
87
}
 
88
 
 
89
{ if (length(arr[$0]) > 0) { delete arr[$0] } }
 
90
 
 
91
END {
 
92
  for (key in arr)
 
93
    if (length(arr[key]) > 0) {
 
94
      printf("%s\n", arr[key])
 
95
    }
 
96
}
 
97
' | LC_COLLATE=C sort -r | \
 
98
        while IFS= read profile ; do
 
99
                if [ "$DRY_RUN" -ne 0 ]; then
 
100
                        echo "Would remove '${profile}'"
 
101
                else
 
102
                        echo "Removing '${profile}'"
 
103
                        echo -n "$profile" > "${REMOVE}"
 
104
                fi
 
105
        done
 
106
 
 
107
# will not catch all errors, but still better than nothing
 
108
exit $?