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

abi <abi/4.0>,

include <tunables/global>

@{exec_path} = @{lib}/systemd/systemd-sysupdated
@{att} = /att/systemd-sysupdated/
profile systemd-sysupdated /{,usr/}lib{,exec,32,64}/systemd/systemd-sysupdated flags=(attach_disconnected,attach_disconnected.path=@{att}) {
  include <abstractions/attached/base>
  include <abstractions/bus-system>
  include <abstractions/disks-write>
  include <abstractions/attached/nameservice-strict>
  include <abstractions/ssl_certs>

  capability chown,
  capability dac_override,
  capability fowner,
  capability fsetid,
  capability linux_immutable,
  capability mknod,
  capability setfcap,
  capability setpcap,
  capability sys_admin,

  network inet dgram,
  network inet stream,
  network inet6 dgram,
  network inet6 stream,
  network netlink raw,

  mount              options=(rw rshared rslave)                                  -> /,
  mount fstype=erofs options=(ro nodev)          /att/systemd-sysupdated/dev/dm-@{int} -> @{run}/systemd/mount-rootfs/,
  mount fstype=erofs options=(ro nodev)          /dev/dm-@{int} -> @{run}/systemd/mount-rootfs/,

  umount @{run}/systemd/mount-rootfs/,

  signal (send receive) set=kill peer=sd//&systemd-sysupdated,

  unix, # TODO: improve me.

  #aa/dbus own bus=system name=org.freedesktop.sysupdate1
  include <abstractions/bus/system/own>
  dbus bind bus=system name=org.freedesktop.sysupdate1{,.*},
  dbus receive bus=system path=/org/freedesktop/sysupdate1{,/**}
       interface=org.freedesktop.sysupdate1{,.*}
       peer=(name="@{busname}"),
  dbus send bus=system path=/org/freedesktop/sysupdate1{,/**}
       interface=org.freedesktop.sysupdate1{,.*}
       peer=(name="{@{busname},org.freedesktop.DBus}"),
  # DBus.Properties: reply to properties request from anyone
  dbus (send receive) bus=system path=/org/freedesktop/sysupdate1{,/**}
       interface=org.freedesktop.DBus.Properties
       member={Get,GetAll,Set,PropertiesChanged}
       peer=(name="{@{busname},org.freedesktop.DBus}"),
  # DBus.Introspectable: allow clients to introspect the service
  dbus receive bus=system path=/org/freedesktop/sysupdate1{,/**}
       interface=org.freedesktop.DBus.Introspectable
       member=Introspect
       peer=(name="@{busname}"),
  # DBus.ObjectManager: allow clients to enumerate sources
  dbus receive bus=system path=/org/freedesktop/sysupdate1{,/**}
       interface=org.freedesktop.DBus.ObjectManager
       member=GetManagedObjects
       peer=(name="{@{busname},org.freedesktop.sysupdate1{,.*}}"),
  dbus send bus=system path=/org/freedesktop/sysupdate1{,/**}
       interface=org.freedesktop.DBus.ObjectManager
       member={InterfacesAdded,InterfacesRemoved}
       peer=(name="{@{busname},org.freedesktop.DBus}"),


  @{exec_path} mr,

  @{lib}/ r,
  @{bin}/gpg{,2}                   ix,
  @{lib}/systemd/systemd-pull      ix, # TODO: nnp stack
  @{lib}/systemd/systemd-sysupdate ix, # TODO: nnp stack
  @{bin}/gpg                       ix, # TODO: nnp stack

  @{etc_rw}/sysupdate.d/{,**} rw,
  /etc/os-release r,

  /usr/.systemd-sysext/ r,
  /usr/.systemd-sysext/backing r,

  @{att}/usr/** r,
  @{efi}/EFI/Linux/ r,
  @{efi}/EFI/Linux/* rw,
  /etc/ r,
  /usr/ r,
  /usr/local/lib/ r,

  / r,
  /var/lib/confexts.d/{,**} rw,
  /var/lib/confexts/{,**} rw,
  /var/lib/extensions.d/{,**} rw,
  /var/lib/extensions/{,**} rw,
  /var/lib/machines/{,**} rw,
  /var/lib/portables/{,**} rw,
  /var/lib/systemd/sysupdate/{,**} rw,

  owner /tmp/gpghome@{rand6}/{,*} rw,
  owner /tmp/sig@{rand6} rw,

  @{run}/ r,
  @{run}/systemd/sysupdate/notify/ r,
  @{run}/systemd/sysupdate/notify/io.systemd.BootControl rw,
  @{run}/systemd/sysupdate/notify/io.systemd.sysext rw,
  @{run}/systemd/mount-rootfs/{,**/} rw,

  @{sys}/fs/cgroup/system.slice/systemd-sysupdated.service/memory.pressure rw,
  @{sys}/devices/virtual/bdi/**/read_ahead_kb r,

  @{PROC}/@{pid}/cgroup r,
  @{PROC}/@{pid}/mountinfo r,
  @{PROC}/1/environ r,
  @{PROC}/cmdline r,
  @{PROC}/devices r,
  @{PROC}/pressure/cpu r,
  @{PROC}/pressure/io r,
  @{PROC}/pressure/memory r,
  @{PROC}/sys/kernel/osrelease r,

  /dev/loop-control rwk,

  include if exists <local/systemd-sysupdated>
}

# vim:syntax=apparmor
