# apparmor.d - Full set of apparmor profiles
# Copyright (C) 2025 Alexandre Pujol <alexandre@pujol.io>
# SPDX-License-Identifier: GPL-2.0-only

#aa:lint ignore=too-wide

# *Part of the `systemd` (as PID 1) profile.*
#
# `sd` is a profile for **s**ystem**d**-executor run as root. It is used to run
# all service files and to encapsulate stacked service profiles (hence the short
# name, to keep the security attribute easy to read). By design, this profile
# must be as broad as all the profiles it runs combined, so it uses the
#  `unconfined` flag.
#
# The `unconfined` flag also avoids the AppArmor *No New Privileges* (NNP)
# restriction. Many services run with `NoNewPrivileges=yes`, and under NNP a
# confined task can only transition to a profile stacked on its current one
# (`sd//&<service>`). An unconfined profile is exempt from this restriction,
# so services can transition to their own profile without being stacked with `sd`.
#
# It aims to reduce the size of the `systemd` profile.
#

abi <abi/4.0>,

include <tunables/global>

@{exec_path} = @{lib}/systemd/systemd-executor
@{att} = /att/sd/
profile sd flags=(unconfined) {
  include <abstractions/attached/base>

  @{exec_path} mr,

  # Systemd user: systemd --user
  @{lib}/systemd/systemd                           px -> systemd-user,

  # Unit services using systemctl
  @{bin}/systemctl                                 cx -> systemctl,

  # Unit services
  @{bin}/kill                                      cx -> kill,

  profile systemctl flags=(attach_disconnected,attach_disconnected.path=@{att},mediate_deleted) {
    include <abstractions/attached/base>
    include <abstractions/app/systemctl>

    capability net_admin,

    include if exists <usr/sd_systemctl.d>
    include if exists <local/sd_systemctl>
  }

  profile kill flags=(attach_disconnected,attach_disconnected.path=@{att},mediate_deleted) {
    include <abstractions/attached/base>

    signal send,

    @{bin}/kill mr,

    include if exists <local/sd_kill>
  }

  include if exists <usr/sd.d>
  include if exists <local/sd>
}

# vim:syntax=apparmor
