2021-04-02 19:12:15 +02:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
# Configure the apparmor.d package
|
2023-02-06 22:27:53 +01:00
|
|
|
# Copyright (C) 2021-2023 Alexandre Pujol <alexandre@pujol.io>
|
2021-04-02 19:12:15 +02:00
|
|
|
# SPDX-License-Identifier: GPL-2.0-only
|
|
|
|
|
2022-02-22 23:08:58 +01:00
|
|
|
set -eu
|
2022-02-16 14:29:53 +01:00
|
|
|
|
2022-06-14 20:39:04 +02:00
|
|
|
DISTRIBUTION="${DIST:-$(lsb_release --id --short)}"
|
2021-12-04 23:09:20 +01:00
|
|
|
readonly DISTRIBUTION="${DISTRIBUTION,,}"
|
2021-05-01 15:27:14 +02:00
|
|
|
readonly ROOT=.build
|
2021-04-02 19:12:15 +02:00
|
|
|
|
2022-05-02 18:28:26 +02:00
|
|
|
_die() { printf 'Error: %s\n' "$*" >&2 && exit 1; }
|
2021-12-04 23:09:20 +01:00
|
|
|
_warning() { printf ' Warning: %s\n' "$*" >&2; }
|
|
|
|
_title() { printf '%s\n' "$*" >&2; }
|
|
|
|
_msg() { printf ' - %s\n' "$*" >&2; }
|
2021-04-02 19:12:15 +02:00
|
|
|
|
2023-01-24 20:55:03 +01:00
|
|
|
# Process management function to run a function over all the profile files
|
2023-02-06 22:27:53 +01:00
|
|
|
# $1 The function to run
|
|
|
|
# $2 Usage message to print
|
|
|
|
process() {
|
|
|
|
local len nprof nproc fct="$1" msg="$2"
|
|
|
|
_msg "$msg"
|
2023-03-29 01:24:36 +02:00
|
|
|
mapfile -t files < <(find "${ROOT:?}/apparmor.d" -type f)
|
2023-01-24 20:55:03 +01:00
|
|
|
len="${#files[@]}"
|
|
|
|
nproc=$(nproc)
|
2023-03-29 01:24:36 +02:00
|
|
|
((nprof = len / nproc + 1))
|
2023-01-24 20:55:03 +01:00
|
|
|
start=0
|
|
|
|
end=$nprof
|
2023-03-29 01:24:36 +02:00
|
|
|
for ((ii = 0; ii < nproc; ii++)); do
|
2023-01-24 20:55:03 +01:00
|
|
|
$fct $start $end "${files[@]}" &
|
2023-03-29 01:24:36 +02:00
|
|
|
((start = end + 1))
|
|
|
|
((end = end + nprof))
|
2023-01-24 20:55:03 +01:00
|
|
|
done
|
|
|
|
wait
|
|
|
|
}
|
|
|
|
|
2021-12-02 13:35:13 +01:00
|
|
|
# Initialize a new clean apparmor.d build directory
|
|
|
|
initialize() {
|
2021-12-04 23:09:20 +01:00
|
|
|
rm -rf "${ROOT:?}"
|
|
|
|
rsync -a ./apparmor.d "$ROOT"
|
|
|
|
rsync -a ./root "$ROOT"
|
2021-04-02 19:12:15 +02:00
|
|
|
}
|
|
|
|
|
2021-12-04 23:09:20 +01:00
|
|
|
# Ignore profiles and files as defined in dists/ignore/
|
2021-12-02 13:35:13 +01:00
|
|
|
ignore() {
|
2021-12-04 23:09:20 +01:00
|
|
|
for name in main.ignore "$DISTRIBUTION.ignore"; do
|
2023-01-27 22:56:29 +01:00
|
|
|
[[ -f "dists/ignore/$name" ]] || continue
|
2021-12-04 23:09:20 +01:00
|
|
|
_msg "Ignore profiles/files in dists/ignore/$name"
|
|
|
|
while read -r profile; do
|
|
|
|
[[ "$profile" =~ ^\# ]] && continue
|
2021-12-05 20:42:42 +01:00
|
|
|
[[ -z "$profile" ]] && continue
|
2022-05-02 18:28:26 +02:00
|
|
|
if [[ -e "${ROOT:?}/$profile" ]]; then
|
2021-12-04 23:09:20 +01:00
|
|
|
rm -r "${ROOT:?}/$profile"
|
|
|
|
else
|
|
|
|
find "$ROOT/apparmor.d" -iname "$profile" -type f -exec rm {} \;
|
|
|
|
fi
|
|
|
|
done <"dists/ignore/$name"
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
# Synchronise all profiles in a new apparmor.d directory.
|
|
|
|
synchronise() {
|
|
|
|
_msg "Synchronise all profiles."
|
|
|
|
mv "${ROOT:?}/apparmor.d/groups/"*/* "${ROOT:?}/apparmor.d/"
|
|
|
|
rm -rf "${ROOT:?}/apparmor.d/groups/"
|
|
|
|
mv "${ROOT:?}/apparmor.d/profiles-"*-*/* "${ROOT:?}/apparmor.d/"
|
|
|
|
rm -rf "${ROOT:?}/apparmor.d/profiles-"*
|
2021-12-02 13:35:13 +01:00
|
|
|
}
|
2021-05-16 22:15:34 +02:00
|
|
|
|
2021-12-02 13:35:13 +01:00
|
|
|
# Set the distribution specificities
|
|
|
|
configure() {
|
2021-09-19 21:39:13 +02:00
|
|
|
case "$DISTRIBUTION" in
|
2023-03-29 01:24:36 +02:00
|
|
|
arch | endeavouros | cachyos | manjarolinux)
|
|
|
|
_msg "Configure libexec."
|
|
|
|
LIBEXEC="/{usr/,}lib"
|
|
|
|
sed -i -e '/Debian/d' "$ROOT/apparmor.d/tunables/etc.d/apparmor.d"
|
|
|
|
;;
|
2021-09-19 21:39:13 +02:00
|
|
|
|
2023-03-29 01:24:36 +02:00
|
|
|
debian | ubuntu | whonix | core)
|
|
|
|
mv "$ROOT/apparmor.d/abstractions/trash.d/complete" "$ROOT/apparmor.d/abstractions/trash"
|
|
|
|
case "$DISTRIBUTION" in
|
|
|
|
core)
|
|
|
|
mkdir -p $ROOT/root/usr/lib/systemd/system/systemd-udevd.service.d/
|
|
|
|
cp -a dists/core/systemd-udevd.service $ROOT/root/usr/lib/systemd/system/systemd-udevd.service.d/apparmor.conf
|
|
|
|
cp -a apparmor.d/groups/_full/systemd $ROOT/apparmor.d/systemd ;;
|
|
|
|
debian | whonix)
|
|
|
|
_msg "$DISTRIBUTION does not support abi 3.0 yet."
|
|
|
|
find "$ROOT/apparmor.d" -type f -exec sed -e '/abi /d' -i {} \;
|
|
|
|
cp -a dists/debian/abstractions/* $ROOT/apparmor.d/abstractions
|
|
|
|
cp -a dists/debian/tunables/* $ROOT/apparmor.d/tunables ;;
|
|
|
|
esac
|
2021-09-19 21:39:13 +02:00
|
|
|
|
2023-03-29 01:24:36 +02:00
|
|
|
_msg "Configure libexec."
|
|
|
|
LIBEXEC="/{usr/,}libexec"
|
|
|
|
sed -i -e '/Archlinux/d' "$ROOT/apparmor.d/tunables/etc.d/apparmor.d"
|
|
|
|
;;
|
2021-09-19 21:39:13 +02:00
|
|
|
|
2023-03-29 01:24:36 +02:00
|
|
|
opensuse)
|
|
|
|
LIBEXEC="/{usr/,}libexec"
|
|
|
|
sed -i -e '/Archlinux/d' "$ROOT/apparmor.d/tunables/etc.d/apparmor.d"
|
|
|
|
;;
|
2023-02-04 22:10:39 +01:00
|
|
|
|
2023-03-29 01:24:36 +02:00
|
|
|
*) _die "$DISTRIBUTION is not a supported distribution." ;;
|
2021-09-19 21:39:13 +02:00
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
2021-05-16 22:15:34 +02:00
|
|
|
# Set flags on some profile
|
2021-12-04 23:09:20 +01:00
|
|
|
flags() {
|
|
|
|
for name in main.flags "$DISTRIBUTION.flags"; do
|
|
|
|
_msg "Set profiles flags from dists/flags/$name"
|
|
|
|
|
|
|
|
while read -r profile; do
|
2022-05-02 18:28:26 +02:00
|
|
|
IFS=' ' read -r -a manifest <<<"$profile"
|
2022-02-22 23:08:58 +01:00
|
|
|
profile="${manifest[0]:-}" flags="${manifest[1]:-}"
|
2021-12-04 23:09:20 +01:00
|
|
|
|
|
|
|
[[ "$profile" =~ ^\# || -z "$profile" ]] && continue
|
|
|
|
path="${ROOT:?}/apparmor.d/$profile"
|
|
|
|
if [[ ! -f "$path" ]]; then
|
|
|
|
_warning "Profile $profile not found"
|
|
|
|
continue
|
|
|
|
fi
|
2021-09-27 21:32:30 +02:00
|
|
|
|
2021-12-04 23:09:20 +01:00
|
|
|
# If flags is set, overwrite profile flag
|
|
|
|
if [[ -n "$flags" ]]; then
|
|
|
|
# Remove all flags definition, then set manifest' flags
|
|
|
|
sed -e "s/flags=(.*)//" \
|
|
|
|
-e "s/ {$/ flags=(${flags//,/ }) {/" \
|
|
|
|
-i "$path"
|
|
|
|
fi
|
2021-12-02 13:35:13 +01:00
|
|
|
|
2021-12-04 23:09:20 +01:00
|
|
|
done <"dists/flags/$name"
|
|
|
|
done
|
2021-09-27 21:32:30 +02:00
|
|
|
}
|
|
|
|
|
2023-01-27 22:56:29 +01:00
|
|
|
# Resolve the variables in the profile attachments
|
|
|
|
_resolve_attachments() {
|
|
|
|
local path="$1"
|
|
|
|
declare -A variables
|
|
|
|
|
|
|
|
# Parse the variables in the profile hearder
|
|
|
|
variables=(
|
|
|
|
[libexec]="$LIBEXEC" [multiarch]="*-linux-gnu*"
|
2023-02-04 22:10:39 +01:00
|
|
|
[user_share_dirs]="/home/*/.local/share" [etc_ro]="/{usr/,}etc/"
|
2023-01-27 22:56:29 +01:00
|
|
|
)
|
|
|
|
mapfile -t lines < <(grep '^@{.*}[ ]*[+=][ ]*.*$' "$path")
|
|
|
|
for line in "${lines[@]}"; do
|
|
|
|
value="${line##*=}"
|
|
|
|
key="${line#^@{}"
|
|
|
|
key="${key%%\}*}"
|
|
|
|
key="${key/@{/}"
|
|
|
|
variables[$key]+="${value}"
|
|
|
|
done
|
|
|
|
[ -z ${variables[exec_path]+x} ] && return
|
|
|
|
|
|
|
|
# Resolve variable in profile attachments
|
|
|
|
entrypoint="${variables[exec_path]}"
|
|
|
|
while [[ "$entrypoint" =~ "@{".*"}" ]]; do
|
|
|
|
name=${entrypoint#*@\{}
|
|
|
|
name="${name%%\}*}"
|
|
|
|
value="${variables[$name]# }"
|
|
|
|
entrypoint="${entrypoint//@{${name}\}/${value}}"
|
|
|
|
done
|
|
|
|
entrypoint="${entrypoint# }"
|
|
|
|
|
|
|
|
# If needed nest the attachments
|
2023-03-29 01:24:36 +02:00
|
|
|
IFS=" " read -r -a attachments <<<"$entrypoint"
|
2023-01-27 22:56:29 +01:00
|
|
|
if [[ "${#attachments[@]}" -ge 2 ]]; then
|
|
|
|
res="/{"
|
|
|
|
for aare in "${attachments[@]}"; do
|
|
|
|
res+="${aare#/},"
|
|
|
|
done
|
|
|
|
entrypoint="${res%,}}"
|
|
|
|
fi
|
|
|
|
echo "$entrypoint"
|
|
|
|
}
|
|
|
|
|
2023-02-06 22:27:53 +01:00
|
|
|
# Remove variables in profile attachment to bypass userspace tools restriction
|
2023-01-27 22:56:29 +01:00
|
|
|
_userspace() {
|
2023-01-28 20:11:50 +01:00
|
|
|
local start="$1" end="$2"; shift 2
|
2023-01-27 22:56:29 +01:00
|
|
|
files=("$@")
|
|
|
|
ii="$start"
|
|
|
|
while [[ $ii -le $end && $ii -lt $len ]]; do
|
|
|
|
path="${files[$ii]}"
|
2023-03-29 01:24:36 +02:00
|
|
|
((ii = ii + 1))
|
2023-01-27 22:56:29 +01:00
|
|
|
[[ -f "$path" ]] || continue
|
|
|
|
entrypoint="$(_resolve_attachments "$path")"
|
|
|
|
[[ -z "$entrypoint" ]] && continue
|
|
|
|
name="$(basename "$path")"
|
|
|
|
sed -e "s;profile $name @{exec_path};profile $name ${entrypoint[*]};g" \
|
|
|
|
-i "$path"
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2023-02-06 22:27:53 +01:00
|
|
|
# Set complain flag on all profiles
|
2022-09-18 12:22:19 +02:00
|
|
|
_complain() {
|
|
|
|
local start="$1" end="$2"; shift 2
|
|
|
|
files=("$@")
|
|
|
|
ii="$start"
|
|
|
|
while [[ $ii -le $end && $ii -lt $len ]]; do
|
|
|
|
path="${files[$ii]}"
|
2023-03-29 01:24:36 +02:00
|
|
|
((ii = ii + 1))
|
2022-09-18 12:22:19 +02:00
|
|
|
[[ -f "$path" ]] || continue
|
2023-01-24 20:55:03 +01:00
|
|
|
mapfile -t flags < <(grep -o -m 1 'flags=(.*)' "$path" | cut -d '(' -f2 | cut -d ')' -f1)
|
|
|
|
[[ "${flags[*]}" =~ complain ]] && continue
|
|
|
|
flags+=(complain)
|
2021-09-27 21:32:30 +02:00
|
|
|
sed -e "s/flags=(.*)//" \
|
2023-01-24 20:55:03 +01:00
|
|
|
-e "s/ {$/ flags=(${flags[*]}) {/" \
|
2021-09-27 21:32:30 +02:00
|
|
|
-i "$path"
|
|
|
|
done
|
2022-09-18 12:22:19 +02:00
|
|
|
}
|
|
|
|
|
2021-12-04 23:09:20 +01:00
|
|
|
# Set AppArmor for full system policy
|
|
|
|
# See https://gitlab.com/apparmor/apparmor/-/wikis/FullSystemPolicy
|
|
|
|
full() {
|
2023-02-06 22:27:53 +01:00
|
|
|
_msg "Configure AppArmor for full system policy"
|
2023-01-24 21:17:00 +01:00
|
|
|
cp -a apparmor.d/groups/_full/init "$ROOT/apparmor.d/"
|
|
|
|
cp -a apparmor.d/groups/_full/systemd "$ROOT/apparmor.d/"
|
|
|
|
case "$DISTRIBUTION" in
|
2023-03-29 01:24:36 +02:00
|
|
|
arch | endeavouros | cachyos | manjarolinux)
|
|
|
|
cp -r root/usr/lib/initcpio root/usr/lib/systemd/ "$ROOT/root/usr/lib/"
|
|
|
|
;;
|
2023-01-24 21:17:00 +01:00
|
|
|
|
2023-03-29 01:24:36 +02:00
|
|
|
debian | ubuntu | whonix | core)
|
|
|
|
cp -r root/usr/share/initramfs-tools "$ROOT/root/usr/share/"
|
|
|
|
;;
|
2023-01-24 21:17:00 +01:00
|
|
|
|
2023-03-29 01:24:36 +02:00
|
|
|
*) _die "$DISTRIBUTION is not a supported distribution." ;;
|
2023-01-24 21:17:00 +01:00
|
|
|
esac
|
2021-12-04 23:09:20 +01:00
|
|
|
}
|
|
|
|
|
2021-04-02 19:12:15 +02:00
|
|
|
# Print help message
|
|
|
|
cmd_help() {
|
|
|
|
cat <<-_EOF
|
2023-03-29 01:24:36 +02:00
|
|
|
./configure [options] - Configure the apparmor.d package
|
2021-04-02 19:12:15 +02:00
|
|
|
|
2023-03-29 01:24:36 +02:00
|
|
|
Options:
|
|
|
|
-f, --full Set AppArmor for full system policy
|
|
|
|
-c, --complain Set complain flag on all profiles
|
|
|
|
-h, --help Print this help message and exit
|
2021-04-02 19:12:15 +02:00
|
|
|
_EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
main() {
|
2021-12-04 23:09:20 +01:00
|
|
|
local opts err
|
|
|
|
FULL=0
|
|
|
|
COMPLAIN=0
|
|
|
|
small_arg="cfh"
|
|
|
|
long_arg="complain,full,help"
|
2022-02-16 14:29:53 +01:00
|
|
|
opts="$(getopt -o $small_arg -l $long_arg -n "configure" -- "$@")"
|
2021-04-02 19:12:15 +02:00
|
|
|
err=$?
|
|
|
|
eval set -- "$opts"
|
|
|
|
while true; do case $1 in
|
2021-12-04 23:09:20 +01:00
|
|
|
-f|--full) FULL=1; shift ;;
|
|
|
|
-c|--complain) COMPLAIN=1; shift ;;
|
2021-04-02 19:12:15 +02:00
|
|
|
-h|--help) shift; cmd_help; exit 0 ;;
|
|
|
|
--) shift; break ;;
|
|
|
|
esac done
|
|
|
|
[[ $err -ne 0 ]] && { cmd_help; exit 1; }
|
|
|
|
|
2021-12-04 23:09:20 +01:00
|
|
|
_title "Set the configuration for $DISTRIBUTION."
|
2021-12-02 13:35:13 +01:00
|
|
|
initialize || _die "initializing build directory"
|
|
|
|
ignore || _die "removing ignored profiles"
|
|
|
|
synchronise || _die "merging profiles"
|
2023-01-24 20:55:03 +01:00
|
|
|
configure || _die "configuring distribution"
|
2023-02-06 22:27:53 +01:00
|
|
|
process _userspace 'Bypass userspace tools restriction' || _die "bypassing userspace"
|
2021-12-04 23:09:20 +01:00
|
|
|
flags || _die "settings flags"
|
2023-02-06 22:27:53 +01:00
|
|
|
[[ "$COMPLAIN" == 1 ]] && process _complain 'Set complain flag on all profiles'
|
2021-12-04 23:09:20 +01:00
|
|
|
[[ "$FULL" == 1 ]] && full
|
|
|
|
return 0
|
2021-04-02 19:12:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
main "$@"
|