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

abi <abi/3.0>,

include <tunables/global>

@{exec_path} = @{bin}/udevadm @{lib}/systemd/systemd-udevd
profile systemd-udevd @{exec_path} flags=(attach_disconnected,complain) {
  include <abstractions/base>
  include <abstractions/common/systemd>
  include <abstractions/consoles>
  include <abstractions/nameservice-strict>

  capability chown,
  capability dac_override,
  capability dac_read_search,
  capability fowner,
  capability fsetid,
  capability mknod,
  capability net_admin,
  capability sys_admin,
  capability sys_module,
  capability sys_ptrace,
  capability sys_rawio,
  capability sys_resource,

  ptrace read,

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

  @{exec_path} mrix,

  @{sh_path}              rix,
  @{coreutils_path}       rix,
  @{bin}/*-print-pci-ids  rix,
  @{bin}/alsactl          rPUx,
  @{bin}/ddcutil          rPx,
  @{bin}/dmsetup          rPUx,
  @{bin}/ethtool          rix,
  @{bin}/issue-generator  rPx,
  @{bin}/kmod             rPx,
  @{bin}/less             rPx -> child-pager,
  @{bin}/logger           rix,
  @{bin}/ls               rix,
  @{bin}/lvm              rPx,
  @{bin}/mknod            rix,
  @{bin}/more             rPx -> child-pager,
  @{bin}/multipath        rPx,
  @{bin}/nfsrahead        rix,
  @{bin}/nvidia-modprobe  rPx -> child-modprobe-nvidia,
  @{bin}/pager            rPx -> child-pager,
  @{bin}/perl             rix,
  @{bin}/setfacl          rix,
  @{bin}/sg_inq           rix,
  @{bin}/snap             rPUx,
  @{bin}/systemctl        rCx -> systemctl,
  @{bin}/systemd-run      rix,
  @{bin}/unshare          rix,

  @{lib}/crda/*                            rPUx,
  @{lib}/gdm-runtime-config                rPx,
  @{lib}/nfsrahead                         rPUx,
  @{lib}/open-iscsi/net-interface-handler  rPUx,
  @{lib}/pm-utils/power.d/*                rPUx,
  @{lib}/snapd/snap-device-helper          rPx,
  @{lib}/systemd/systemd-*                 rPx,
  @{lib}/udev/*                            rPUx,
  /usr/share/hplip/config_usb_printer.py   rPUx,

  /etc/console-setup/*.sh             rPUx,
  /etc/network/cloud-ifupdown-helper  rPUx,

  /etc/default/* r,
  /etc/machine-id r,
  /etc/nfs.conf rk,

  /etc/udev/{,**} r,
  /etc/udev/.#hwdb.bin* rw,
  /etc/udev/hwdb.bin rw,

  /etc/modprobe.d/ r,
  /etc/modprobe.d/*.conf r,

  /etc/systemd/network/ r,
  /etc/systemd/network/@{int2}-*.link r,

  @{run}/udev/ rw,
  @{run}/udev/** rwk,

  @{run}/credentials/systemd-udev-load-credentials.service/ r,
  @{run}/systemd/network/ r,
  @{run}/systemd/network/*.link rw,
  @{run}/systemd/notify rw,
  @{run}/systemd/seats/seat@{int} r,

  @{sys}/** rw,

        @{PROC}/@{pid}/mountinfo r,
        @{PROC}/@{pids}/cgroup r,
        @{PROC}/devices r,
        @{PROC}/driver/nvidia/gpus/ r,
        @{PROC}/driver/nvidia/gpus/*/information r,
        @{PROC}/driver/nvidia/params r,
        @{PROC}/pressure/* r,
        @{PROC}/sys/fs/nr_open r,
  owner @{PROC}/@{pid}/fd/ r,
  owner @{PROC}/@{pid}/loginuid r,
  owner @{PROC}/@{pid}/oom_score_adj rw,

  /dev/ rw,
  /dev/** rwk,

  profile systemctl flags=(attach_disconnected,complain) {
    include <abstractions/base>
    include <abstractions/app/systemctl>

    capability net_admin,
    capability sys_ptrace,

    # / r,

    @{PROC}/sys/kernel/cap_last_cap r,

    include if exists <local/systemd-udevd_systemctl>
  }

  include if exists <local/systemd-udevd>
}

# vim:syntax=apparmor
