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

# Profile for 'systemd --user', not PID 1 but the user manager for any UID.
# It does not specify an attachment path because it is intended to be used only
# via "px -> systemd-user" exec transitions from the `systemd` profile.

# Only use this profile with a fully configured system. Otherwise it **WILL**
# break your computer. See https://apparmor.pujol.io/full-system-policy/.

# Distributions and other programs can add rules in the usr/systemd-user.d directory

abi <abi/3.0>,

include <tunables/global>

@{exec_path} = @{lib}/systemd/systemd
profile systemd-user flags=(attach_disconnected,mediate_deleted) {
  include <abstractions/base>
  include <abstractions/bus-session>
  include <abstractions/bus-system>
  include <abstractions/disks-read>
  include <abstractions/nameservice-strict>

  network netlink raw,

  signal (send) set=(term, cont, kill),
  signal (receive) set=(hup) peer=@{p_systemd},

  ptrace (read) peer=@{p_systemd},

  unix (bind) type=stream addr=@@{hex}/bus/systemd/bus-system,
  unix (bind) type=stream addr=@@{hex}/bus/systemd/bus-api-user,

  #aa:dbus own bus=session name=org.freedesktop.systemd1

  @{exec_path} mr,

  @{bin}/dbus-broker                         rpx -> dbus-session,
  @{bin}/dbus-broker-launch                  rpx -> dbus-session,
  @{bin}/dbus-daemon                         rpx -> dbus-session,
  @{lib}/dbus-1.0/dbus-daemon-launch-helper  rpx -> dbus-session,

  @{bin}/systemctl                  rCx -> systemctl,
  @{lib}/systemd/systemd-executor   rix,
  @{sh_path}                        rix, # Should be handled by default profile?
  @{bin}/grep                       rix,
  @{bin}/sleep                      rix,

  @{bin}/**                          Px,
  @{lib}/**                          Px,
  /opt/*/**                          Px,
  /usr/share/*/**                    Px,

  #aa:stack pipewire pipewire-media-session pipewire-pulse pulseaudio wireplumber
  @{bin}/pipewire                   rPx -> systemd-user//&pipewire,
  @{bin}/pipewire-media-session     rPx -> systemd-user//&pipewire-media-session,
  @{bin}/pipewire-pulse             rPx -> systemd-user//&pipewire-pulse,
  @{bin}/pulseaudio                 rPx -> systemd-user//&pulseaudio,
  @{bin}/wireplumber                rPx -> systemd-user//&wireplumber,

  /usr/ r,
  /usr/share/defaults/**.conf r,

  /etc/systemd/user.conf r,
  /etc/systemd/user.conf.d/{,**} r,
  /etc/systemd/user/{,**} r,

  / r,

  owner @{HOME}/.local/ w,

  owner @{user_config_dirs}/systemd/user/{,**} rw,

        @{run}/systemd/users/@{uid} r,
  owner @{run}/user/@{uid}/ rw,
  owner @{run}/user/@{uid}/** rwkl,

  @{run}/mount/utab r,
  @{run}/systemd/notify w,

  @{run}/udev/data/+backlight:* r,
  @{run}/udev/data/+leds:*backlight* r,
  @{run}/udev/data/+module:configfs r,
  @{run}/udev/data/+module:fuse r,
  @{run}/udev/data/b254:@{int} r,         # for /dev/zram*
  @{run}/udev/data/c4:@{int} r,           # For TTY devices
  @{run}/udev/data/c5:@{int}   r,         # for /dev/tty, /dev/console, /dev/ptmx
  @{run}/udev/data/c10:@{int}   r,        # For non-serial mice, misc features
  @{run}/udev/data/c@{dynamic}:@{int} r,  # For dynamic assignment range 234 to 254, 384 to 511
  @{run}/udev/data/n@{int} r,
  @{run}/udev/tags/systemd/ r,

  @{sys}/devices/virtual/dmi/id/bios_vendor r,

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

        @{PROC}/@{pid}/cmdline r,
        @{PROC}/@{pids}/cgroup r,
        @{PROC}/@{pids}/comm r,
        @{PROC}/@{pids}/stat r,
        @{PROC}/cmdline r,
        @{PROC}/pressure/* r,
        @{PROC}/swaps r,
        @{PROC}/sys/fs/nr_open r,
        @{PROC}/sys/kernel/osrelease r,
        @{PROC}/sys/kernel/overflowgid r,
        @{PROC}/sys/kernel/overflowuid r,
        @{PROC}/sys/kernel/pid_max r,
        @{PROC}/sys/kernel/threads-max r,
  owner @{PROC}/@{pid}/coredump_filter r,
  owner @{PROC}/@{pid}/fdinfo/@{int} r,
  owner @{PROC}/@{pid}/gid_map r,
  owner @{PROC}/@{pid}/mountinfo r,
  owner @{PROC}/@{pid}/oom_score_adj rw,
  owner @{PROC}/@{pid}/stat r,
  owner @{PROC}/@{pid}/task/@{tid}/comm rw,
  owner @{PROC}/@{pid}/uid_map r,
  owner @{PROC}/@{pids}/fd/ r,
  owner @{PROC}/@{pids}/oom_score_adj rw,

  /dev/tty rw,

  profile systemctl {
    include <abstractions/base>
    include <abstractions/app/systemctl>

    include if exists <usr/systemd-user_systemctl.d>
    include if exists <local/systemd-user_systemctl>
  }

  include if exists <usr/systemd-user.d>
  include if exists <local/systemd-user>
}
