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

# *Part of the `systemd-user` profile.*
#
# `sdu` is a profile for **s**ystem**d**-executor run as **u**ser. It is used to
# run all service files and to encapsulate stacked service profiles (hence the
# short name). By design, this profile must be as broad as all the profiles it
# runs combined, so it uses the `unconfined` flag. As for `sd`, it also avoids
# the NNP restriction, so user services do not have to be stacked with `sdu`.
#
# It aims to reduce the size of the `systemd-user` profile.
#
# !!! note
#
#     To reduce the number of artificially stacked profiles, this profile is
#     currently **not used** in FSP mode, as `systemd-user` purposely runs
#     systemd-executor unconfined.
#

abi <abi/4.0>,

include <tunables/global>

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

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

  # Shell based user unit services
  @{sh_path}                                       cx -> shell,

  # Dbus needs to be started without environment scrubbing
  @{bin}/dbus-broker                               px -> dbus-session,
  @{bin}/dbus-broker-launch                        px -> dbus-session,
  @{bin}/dbus-daemon                               px -> dbus-session,
  @{lib}/dbus-1.0/dbus-daemon-launch-helper        px -> dbus-session,

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

    @{sh_path}         mr,
    @{bin}/{m,g,}awk  rix,
    @{bin}/gpgconf     px,
    @{bin}/systemctl   px -> sdu//systemctl,

    include if exists <local/sdu_shell>
  }

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

    owner @{run}/user/@{uid}/systemd/private rw,

    deny capability net_admin,

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

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

# vim:syntax=apparmor
