# 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 services files and to encapsulate stacked services profiles (hence the
# short name).
#
# It aims at reducing the size of the `systemd-user` profile.
#
# !!! info
#
#     Distributions and other programs can add rules in the `usr/sdu.d` directory
#

abi <abi/4.0>,

include <tunables/global>

@{exec_path} = @{bin}/systemd-executor
@{att} = /att/sdu/
profile sdu flags=(attach_disconnected,attach_disconnected.path=@{att},mediate_deleted) {
  include <abstractions/attached/base>
  include <abstractions/audio-server>
  include <abstractions/bus-session>
  include <abstractions/bus-system>
  include <abstractions/attached/consoles>
  include <abstractions/attached/nameservice-strict>
  include <abstractions/xdg-desktop>
  include <abstractions/sys/dmi>

  network netlink raw,

  change_profile,

  ptrace read,

  unix type=dgram peer=(label=@{p_systemd_user}),

  #aa/dbus talk bus=system name=org label="*"
  # Unix: allow connection to the profile
  unix type=stream peer=(label="*"),
  # org: send and receive anything to the interface on the specific peer label
  dbus (send receive) bus=system path=/org{,/**}
       interface=org{,.*}
       peer=(name="{@{busname},org{,.*},org.freedesktop.DBus}", label="*"),
  dbus send bus=system path=/org{,/**}
       interface=org{,.*}
       peer=(name="org{,.*}"),
  # DBus.Properties: read and send properties
  dbus (send receive) bus=system path=/org{,/**}
       interface=org.freedesktop.DBus.Properties
       member={Get,GetAll,Set,PropertiesChanged}
       peer=(name="{@{busname},org{,.*},org.freedesktop.DBus}", label="*"),
  # DBus.Introspectable: allow service introspection
  dbus send bus=system path=/org{,/**}
       interface=org.freedesktop.DBus.Introspectable
       member=Introspect
       peer=(name="{@{busname},org{,.*},org.freedesktop.DBus}", label="*"),
  # DBus.ObjectManager: allow clients to enumerate sources
  dbus send bus=system path=/org{,/**}
       interface=org.freedesktop.DBus.ObjectManager
       member=GetManagedObjects
       peer=(name="{@{busname},org{,.*},org.freedesktop.DBus}", label="*"),
  dbus receive bus=system path=/org{,/**}
       interface=org.freedesktop.DBus.ObjectManager
       member={InterfacesAdded,InterfacesRemoved}
       peer=(name="{@{busname},org{,.*},org.freedesktop.DBus}", label="*"),

  dbus bus=session,

  @{exec_path} mr,

  @{bin}/**                                       mpx,
  @{sbin}/**                                      mpx,
  @{lib}/**                                        px,
  /etc/cron.*/*                                    px,
  /opt/*/**                                        px,
  /usr/share/*/**                                  px,

  # 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,

  / r,
  @{bin}/* r,
  @{sbin}/* r,
  /usr/share/** r,

  owner @{desktop_local_dirs}/ w,
  owner @{desktop_local_dirs}/state/ w,
  owner @{desktop_local_dirs}/state/wireplumber/{,**} rw,

  owner @{run}/user/@{uid}/pipewire-@{int} rw,
  owner @{run}/user/@{uid}/pipewire-@{int}-manager rw,
  owner @{run}/user/@{uid}/pipewire-@{int}-manager.lock rwk,
  owner @{run}/user/@{uid}/pipewire-@{int}.lock rwk,
  owner @{run}/user/@{uid}/pulse/pid rw,

  owner @{user_state_dirs}/wireplumber/ rw,
  owner @{user_state_dirs}/wireplumber/stream-properties rw,
  owner @{user_state_dirs}/wireplumber/stream-properties.@{rand6} rw,

  @{run}/systemd/users/@{uid} r,
  @{run}/systemd/users/@{int} r,

  @{run}/udev/data/c14:@{int} r,          # Open Sound System (OSS)
  @{run}/udev/data/c81:@{int} r,          # For video4linux
  @{run}/udev/data/c116:@{int} r,         # For ALSA
  @{run}/udev/data/c@{dynamic}:@{int} r,  # For dynamic assignment range 234 to 254, 384 to 511

  @{sys}/bus/ r,
  @{sys}/bus/media/devices/ r,
  @{sys}/devices/@{pci}/video4linux/video@{int}/uevent r,
  @{sys}/devices/**/device:*/{,**/}path r,
  @{sys}/devices/**/sound/**/pcm_class r,
  @{sys}/devices/**/sound/**/uevent r,
  @{sys}/devices/system/node/ r,
  @{sys}/devices/system/node/node@{int}/meminfo r,
  @{sys}/devices/virtual/sound/seq/uevent r,
  @{sys}/devices/virtual/sound/timer/uevent r,

        @{sys}/module/apparmor/parameters/enabled r,
  owner @{sys}/fs/cgroup/user.slice/user-@{uid}.slice/user@@{uid}.service/{,**} rw,

        @{PROC}/pressure/* r,
        @{PROC}/sys/fs/nr_open r,
  owner @{PROC}/@{pid}/attr/apparmor/exec w,
  owner @{PROC}/@{pid}/fd/ r,
  owner @{PROC}/@{pid}/oom_score_adj rw,
  owner @{PROC}/@{pid}/task/@{tid}/comm rw,

  /dev/kmsg w,

  deny capability net_admin,

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

    @{sh_path}         mr,
    @{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
