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

abi <abi/4.0>,

include <tunables/global>

@{exec_path} = @{lib}/systemd/systemd-oomd
@{att} = /att/systemd-oomd/
profile systemd-oomd /{,usr/}lib{,exec,32,64}/systemd/systemd-oomd  flags=(attach_disconnected,attach_disconnected.path=@{att},complain) {
  include <abstractions/attached/base>
  include <abstractions/bus-system>
  include <abstractions/common/systemd>

  capability dac_override,
  capability kill,

  unix bind type=stream addr=@@{udbus}/bus/systemd-oomd/bus-api-oom,

  include <abstractions/bus/system/own>

  dbus bind bus=system name=org.freedesktop.oom1{,.*},
  dbus receive bus=system path=/org/freedesktop/oom1{,/**}
       interface=org.freedesktop.oom1{,.*}
       peer=(name="@{busname}"),
  dbus send bus=system path=/org/freedesktop/oom1{,/**}
       interface=org.freedesktop.oom1{,.*}
       peer=(name="{@{busname},org.freedesktop.DBus}"),
  dbus (send receive) bus=system path=/org/freedesktop/oom1{,/**}
       interface=org.freedesktop.DBus.Properties
       member={Get,GetAll,Set,PropertiesChanged}
       peer=(name="{@{busname},org.freedesktop.DBus}"),
  dbus receive bus=system path=/org/freedesktop/oom1{,/**}
       interface=org.freedesktop.DBus.Introspectable
       member=Introspect
       peer=(name="@{busname}"),
  dbus receive bus=system path=/org/freedesktop/oom1{,/**}
       interface=org.freedesktop.DBus.ObjectManager
       member=GetManagedObjects
       peer=(name="{@{busname},org.freedesktop.oom1{,.*}}"),
  dbus send bus=system path=/org/freedesktop/oom1{,/**}
       interface=org.freedesktop.DBus.ObjectManager
       member={InterfacesAdded,InterfacesRemoved}
       peer=(name="{@{busname},org.freedesktop.DBus}"),

  @{exec_path} mr,

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

  @{att}@{run}/systemd/notify w,
  @{att}@{run}/systemd/io.systemd.ManagedOOM rw,

  @{run}/systemd/io.system.ManagedOOM rw,
  @{run}/systemd/io.systemd.ManagedOOM rw,

  @{sys}/fs/cgroup/cgroup.controllers r,
  @{sys}/fs/cgroup/memory.* r,
  @{sys}/fs/cgroup/system.slice/memory.* r,
  @{sys}/fs/cgroup/user.slice/{,**/}memory.* r,

  @{PROC}/pressure/cpu r,
  @{PROC}/pressure/io r,
  @{PROC}/pressure/memory r,

  include if exists <local/systemd-oomd>
}

# vim:syntax=apparmor
