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

include <tunables/global>

@{exec_path} = @{bin}/hw-probe
profile hw-probe @{exec_path} flags=(attach_disconnected) {
  include <abstractions/base>
  include <abstractions/perl>
  include <abstractions/X-strict>

  capability sys_admin,

  network inet dgram,
  network inet6 dgram,

  @{exec_path} rm,
  @{bin}/perl r,

  @{sh_path}             rix,
  @{bin}/{,e}grep        rix,
  @{bin}/{m,g,}awk       rix,
  @{bin}/dd              rix,
  @{bin}/efibootmgr      rix,
  @{bin}/efivar          rix,
  @{bin}/find            rix,
  @{bin}/md5sum          rix,
  @{bin}/pwd             rix,
  @{bin}/sleep           rix,
  @{bin}/sort            rix,
  @{bin}/tar             rix,
  @{bin}/uname           rix,

  @{bin}/acpi            rPx,
  @{bin}/amixer          rPx,
  @{bin}/aplay           rPx,
  @{bin}/biosdecode      rPx,
  @{bin}/cpuid           rPx,
  @{bin}/cpupower        rPx,
  @{bin}/curl            rCx -> curl,
  @{bin}/df              rPx,
  @{bin}/dkms            rPx,
  @{bin}/dmesg           rPx,
  @{bin}/dmidecode       rPx,
  @{bin}/dpkg            rPx -> child-dpkg,
  @{bin}/edid-decode     rPx,
  @{bin}/ethtool         rCx -> netconfig,
  @{bin}/fdisk           rPx,
  @{bin}/glxgears        rPx,
  @{bin}/glxinfo         rPx,
  @{bin}/hciconfig       rPx,
  @{bin}/hdparm          rPx,
  @{bin}/hwinfo          rPx,
  @{bin}/i2cdetect       rPx,
  @{bin}/ifconfig        rCx -> netconfig,
  @{bin}/inxi            rPx,
  @{bin}/iw              rCx -> netconfig,
  @{bin}/iwconfig        rCx -> netconfig,
  @{bin}/journalctl      rCx -> journalctl,
  @{bin}/killall         rCx -> killall,
  @{bin}/kmod            rix,
  @{bin}/lsb_release     rPx -> lsb_release,
  @{bin}/lsblk           rPx,
  @{bin}/lscpu           rPx,
  @{bin}/lspci           rPx,
  @{bin}/lsusb           rPx,
  @{bin}/memtester       rPx,
  @{bin}/nmcli           rPx,
  @{bin}/pacman          rCx -> pacman,
  @{bin}/rfkill          rPx,
  @{bin}/rpm             rCx -> rpm, 
  @{bin}/sensors         rPx,
  @{bin}/smartctl        rPx,
  @{bin}/systemctl       rCx -> systemctl,
  @{bin}/systemd-analyze rPx,
  @{bin}/udevadm         rCx -> udevadm,
  @{bin}/upower          rPx,
  @{bin}/uptime          rPx,
  @{bin}/usb-devices     rPx,
  @{bin}/xdpyinfo        rPx,
  @{bin}/xinput          rPx,
  @{bin}/xrandr          rPx,

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

  owner @{HOME}/HW_PROBE/{,**} rw,

  audit owner @{tmp}/*/ rw,
  owner @{tmp}/*/cpu_perf rw,

  @{sys}/class/drm/ r,
  @{sys}/class/power_supply/ r,
  @{sys}/devices/@{pci}/drm/card@{int}/*/edid r,
  @{sys}/devices/**/power_supply/*/uevent r,
  @{sys}/devices/virtual/dmi/id/* r,
  @{sys}/firmware/efi/efivars/ r,
  @{sys}/firmware/efi/efivars/* r,
  @{sys}/module/*/ r,
  @{sys}/module/*/{coresize,refcnt} r,
  @{sys}/module/*/holders/ r,

  @{PROC}/bus/input/devices r,
  @{PROC}/cmdline r,
  @{PROC}/interrupts r,
  @{PROC}/ioports r,
  @{PROC}/modules r,
  @{PROC}/scsi/scsi r,

  /dev/{,**} r,

  profile pacman flags=(attach_disconnected) {
    include <abstractions/base>
    include <abstractions/consoles>

    @{bin}/pacman mr,

    @{bin}/gpg      rPx -> pacman//gpg,
    @{bin}/gpgconf  rPx -> pacman//gpg,
    @{bin}/gpgsm    rPx -> pacman//gpg,

    /etc/pacman.conf r,
    /etc/pacman.d/{,**} r,

    /var/lib/pacman/{,**} r,

    include if exists <local/hw-probe_pacman>
  }

  profile rpm flags=(attach_disconnected) {
    include <abstractions/base>
    include <abstractions/consoles>

    capability dac_read_search,

    @{bin}/rpm mr,

    /var/ r,
    /var/lib/ r,
    /var/lib/rpm/ r,
    /var/lib/rpm/rpmdb.sqlite rk,
    /var/lib/rpm/rpmdb.sqlite-shm rwk,
    /var/lib/rpm/rpmdb.sqlite-wal rw,

    include if exists <local/hw-probe_rpm>
  }

  profile journalctl flags=(attach_disconnected) {
    include <abstractions/base>

    @{bin}/journalctl mr,

    /var/lib/dbus/machine-id r,
    /etc/machine-id r,

    @{run}/log/ rw,
    /{run,var}/log/journal/ r,
    /{run,var}/log/journal/@{hex32}/ r,
    /{run,var}/log/journal/@{hex32}/user-@{hex}.journal* r,
    /{run,var}/log/journal/@{hex32}/system.journal* r,
    /{run,var}/log/journal/@{hex32}/system@@{hex}.journal* r,

    owner @{PROC}/@{pid}/stat r,

    include if exists <local/hw-probe_journalctl>
  }

  profile killall flags=(attach_disconnected) {
    include <abstractions/base>

    capability sys_ptrace,

    ptrace (read),

    signal (send) set=(int, term, kill),

    @{bin}/killall mr,

    @{PROC}/ r,
    @{PROC}/@{pids}/stat r,

    include if exists <local/hw-probe_killall>
  }

  profile udevadm flags=(attach_disconnected) {
    include <abstractions/base>
    include <abstractions/app/udevadm>

    include if exists <local/hw-probe_udevadm>
  }

  profile netconfig flags=(attach_disconnected) {
    include <abstractions/base>

    # Not needed
    deny capability net_admin,
    deny capability net_raw,

    network inet dgram,
    network inet6 dgram,
    network ipx dgram,
    network ax25 dgram,
    network appletalk dgram,
    network netlink raw,

    @{bin}/iw       mr,
    @{bin}/ifconfig mr,
    @{bin}/iwconfig mr,
    @{bin}/ethtool  mr,

    owner @{PROC}/@{pid}/net/if_inet6 r,
    owner @{PROC}/@{pid}/net/dev r,

    include if exists <local/hw-probe_netconfig>
  }

  profile systemctl flags=(attach_disconnected) {
    include <abstractions/base>
    include <abstractions/app/systemctl>
  
    include if exists <local/hw-probe_systemctl>
  }

  include if exists <local/hw-probe>
}

# vim:syntax=apparmor
