# apparmor.d - Full set of apparmor profiles
# Copyright (C) 2018-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}/gsmartcontrol
profile gsmartcontrol @{exec_path} {
  include <abstractions/base>
  include <abstractions/gtk>
  include <abstractions/fonts>
  include <abstractions/fontconfig-cache-read>
  include <abstractions/freedesktop.org>
  include <abstractions/nameservice-strict>

  capability dac_read_search,

  # Needed?
  deny capability sys_nice,

  @{exec_path} mr,

  @{bin}/smartctl rPx,
  @{bin}/xterm     rCx -> terminal,

  # When gsmartcontrol is run as root, it wants to exec dbus-launch, and hence it creates the two
  # following root processes:
  #  dbus-launch --autolaunch e0a30ad97cd6421c85247839ccef9db2 --binary-syntax --close-stderr
  #  /usr/bin/dbus-daemon --syslog-only --fork --print-pid 5 --print-address 7 --session
  #
  # Should this be allowed? Gsmartcontrol works fine without this.
  #@{bin}/dbus-launch        rCx -> dbus,
  #@{bin}/dbus-send          rCx -> dbus,
  deny @{bin}/dbus-launch rx,
  deny @{bin}/dbus-send   rx,

  owner @{user_config_dirs}/gsmartcontrol/ rw,
  owner @{user_config_dirs}/gsmartcontrol/gsmartcontrol.conf rw,

  # As it's started as root
  @{HOME}/.Xauthority r,

  # For saving SMART raport
  owner /root/ r,
  owner /root/**.txt w,

  owner @{PROC}/@{pid}/fd/ r,
  owner @{PROC}/@{pid}/mountinfo r,
  owner @{PROC}/@{pid}/mounts r,
  owner @{PROC}/partitions r,
  owner @{PROC}/devices r,
  owner @{PROC}/scsi/scsi r,
  owner @{PROC}/scsi/sg/devices r,

  /etc/fstab r,

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

  # The Help menu (and links in it) requires access to a web browser. Since gsmartcontrol is run as
  # root (even when used sudo or gsmartcontrol-root), the web browser will also be run as root and
  # hence this behavior should be blocked.
  deny @{lib}/@{multiarch}/glib-[0-9]*/gio-launch-desktop rx,


  profile dbus {
    include <abstractions/base>
    include <abstractions/nameservice-strict>

    @{bin}/dbus-launch   mr,
    @{bin}/dbus-send     mr,
    @{bin}/dbus-daemon rPUx,

    # for dbus-launch
    owner @{HOME}/.dbus/session-bus/@{hex}-[0-9] w,

    @{HOME}/.Xauthority r,
  }

  profile terminal {
    include <abstractions/base>
    include <abstractions/consoles>
    include <abstractions/nameservice-strict>
    include <abstractions/freedesktop.org>

    capability setuid,
    capability setgid,
    capability fsetid,

    @{bin}/xterm mr,

    /usr/sbin/update-smart-drivedb rPx,

    owner @{HOME}/.Xauthority r,

    /etc/shells r,

    /etc/X11/app-defaults/XTerm-color r,
    /etc/X11/app-defaults/XTerm r,
    /etc/X11/cursors/*.theme r,

    /usr/include/X11/bitmaps/vlines2 r,

    /dev/ptmx rw,

  }

  include if exists <local/gsmartcontrol>
}
