|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +set -eu |
| 4 | + |
| 5 | +# This check verifies that interfaces that grant access to /proc/self/mountinfo |
| 6 | +# (directly or not) also have a prioritized override (see basePrioritizedSnippets |
| 7 | +# in interfaces/apparmor/template.go for why this is necessary). We do this |
| 8 | +# by looking for a "AddPrioritizedSnippet(*, apparmor.MountInfoKey, ...)" call |
| 9 | +# for any such interfaces. |
| 10 | + |
| 11 | +missing_override="" |
| 12 | + |
| 13 | +for f in interfaces/builtin/*.go; do |
| 14 | + if [[ "$f" == *"_test.go" ]]; then |
| 15 | + continue |
| 16 | + fi |
| 17 | + |
| 18 | + out=$(awk ' |
| 19 | + # if a prioritized override is present, we can stop early |
| 20 | + /AddPrioritizedSnippet\(.* apparmor\.MountInfoKey/ { m=""; exit } |
| 21 | +
|
| 22 | + # We look for the following types of rules: |
| 23 | + # * Explicit mountinfo rules. For example, any combination of: |
| 24 | + # owner /proc/self/mountinfo r, |
| 25 | + # @{PROC}/@{pid} rw, |
| 26 | + # /proc/1234/mountinfo |
| 27 | + # * References to the "mountInfoSnippet" variable which contains |
| 28 | + # the same rules as the previous item |
| 29 | + # * Broad /proc grants: |
| 30 | + # owner @{PROC}/** r, |
| 31 | + # /proc/** rw, |
| 32 | + # * Relevant "allow" rules: |
| 33 | + # allow file |
| 34 | + # allow all |
| 35 | +
|
| 36 | + /^[[:space:]]*(owner[[:space:]]+)?(\/proc|@\{PROC\})\/(self|@\{pid\}|[0-9]+)\/mountinfo[[:space:]][a-z]*r[a-z]*,/ || |
| 37 | + /^[[:space:]]*(owner[[:space:]]+)?(\/proc|@\{PROC\})\/\*\*[[:space:]][a-z]*r[a-z]*,/ || |
| 38 | + /^[[:space:]]*allow[[:space:]]+(file|all),[[:space:]]*$/ || |
| 39 | + /mountInfoSnippet/ { m = m " " NR ":" $0 ORS } |
| 40 | + END { if (m != "") printf " - %s:\n%s", FILENAME, m } |
| 41 | + ' "$f") |
| 42 | + [ -n "$out" ] && missing_override+="${out}"$'\n' |
| 43 | +done |
| 44 | + |
| 45 | +if [ -n "$missing_override" ]; then |
| 46 | + echo "" >&2 |
| 47 | + echo "These interfaces allow access to /proc/*/mountinfo (specifically or not)" >&2 |
| 48 | + echo "without adding the allow rule as a prioritized snippet" >&2 |
| 49 | + echo "(see basePrioritizedSnippets in interfaces/apparmor/template.go):" >&2 |
| 50 | + printf "%s" "$missing_override" >&2 |
| 51 | + exit 1 |
| 52 | +fi |
0 commit comments