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

include <tunables/global>

@{exec_path} = @{bin}/gdm{3,}
profile gdm @{exec_path} flags=(attach_disconnected) {
  include <abstractions/base>
  include <abstractions/bus-session>
  include <abstractions/bus-system>
  include <abstractions/nameservice-strict>
  include <abstractions/wutmp>

  capability chown,
  capability dac_override,
  capability dac_read_search,
  capability fsetid,
  capability kill,
  capability net_admin,
  capability sys_nice,
  capability sys_tty_config,

  network netlink raw,

  ptrace (read) peer=unconfined,

  signal (send) set=(term),

  unix (bind, listen) type=stream addr="@/tmp/dbus-@{rand8}",
  unix (send receive accept) type=stream addr="@/tmp/dbus-@{rand8}" peer=(label=gdm-session-worker, addr=none),

  #aa:dbus own bus=system name=org.gnome.DisplayManager

  #aa:dbus talk bus=system name=org.freedesktop.login1 label=systemd-logind
  #aa:dbus talk bus=system name=org.freedesktop.Accounts label=accounts-daemon

  dbus send bus=system path=/org/freedesktop/DBus
       interface=org.freedesktop.DBus
       member={GetConnectionUnixProcessID,GetConnectionUnixUser}
       peer=(name=org.freedesktop.DBus, label=dbus-system),

  @{exec_path} mr,

  @{sh_path}                       rix,
  @{bin}/chvt                      rix,
  @{bin}/pidof                     rPx,
  @{bin}/plymouth                  rPx,
  @{bin}/prime-switch             rPUx,
  @{bin}/sleep                     rix,
  @{bin}/systemd-cat               rPx,
  @{lib}/{,gdm/}gdm-session-worker rPx,
  /etc/gdm{3,}/PrimeOff/Default    rix,

  /usr/share/gdm{3,}/gdm.schemas r,
  /usr/share/wayland-sessions/*.desktop r,
  /usr/share/xsessions/*.desktop r,

  /etc/default/locale r,
  /etc/gdm{3,}/custom.conf r,
  /etc/gdm{3,}/daemon.conf r,
  /etc/locale.conf r,
  /etc/sysconfig/displaymanager r,
  /etc/sysconfig/windowmanager r,

  /var/lib/gdm{3,}/ rw,
  /var/lib/gdm{3,}/block-initial-setup rw,

  /var/log/gdm{3,}/ rw,

  owner @{GDM_HOME}/block-initial-setup rw,

        @{run}/gdm{3,}/greeter/ rw,
        @{run}/systemd/seats/seat@{int} r,
        @{run}/systemd/sessions/* r,
        @{run}/systemd/users/@{uid} r,
  owner @{run}/gdm{3,}.pid rw,
  owner @{run}/gdm{3,}/ rw,
  owner @{run}/gdm{3,}/custom.conf r,
  owner @{run}/gdm{3,}/dbus/ w,
  owner @{run}/gdm{3,}/dbus/dbus-@{rand8} w,
  owner @{run}/gdm{3,}/gdm.pid rw,

  @{run}/udev/data/+drm:card@{int}-* r,   # For screen outputs
  @{run}/udev/data/+pci:* r,              # Identifies all PCI devices (CPU, GPU, Network, Disks, USB, etc.)
  @{run}/udev/data/c226:@{int} r,         # for /dev/dri/card*

  @{run}/udev/tags/master-of-seat/ r,

  @{sys}/devices/**/uevent r,
  @{sys}/devices/@{pci}/boot_vga r,
  @{sys}/devices/virtual/tty/tty@{int}/active r,
  @{sys}/fs/cgroup/user.slice/user-@{uid}.slice/session-@{int}.scope/cgroup.events r,

        @{PROC}/@{pid}/cgroup r,
        @{PROC}/1/environ r,
        @{PROC}/cmdline r,
        @{PROC}/sys/kernel/osrelease r,
  owner @{PROC}/@{pid}/cmdline r,
  owner @{PROC}/@{pid}/fd/ r,

  /dev/tty rw,

  include if exists <local/gdm>
}

# vim:syntax=apparmor
