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

# TODO: Similar with virtqemud. Could be merged?

abi <abi/3.0>,

include <tunables/global>

@{exec_path} = @{bin}/virtstoraged
profile virtstoraged @{exec_path} flags=(attach_disconnected) {
  include <abstractions/base>
  include <abstractions/nameservice-strict>

  capability dac_read_search,

  network netlink raw,

  ptrace (read) peer=virtqemud,
  ptrace (read) peer=unconfined,

  @{exec_path} mr,

  @{bin}/qemu-system*  rUx, # TODO: Integration with virt-aa-helper
  @{bin}/qemu-img      rUx, # TODO: Integration with virt-aa-helper

  /etc/libvirt/**/ r,
  /etc/libvirt/libvirt.conf r,

  # For disk images
  @{MOUNTS}/ r,
  @{user_img_dirs}/{,**} r,

  # System VM images
  /var/lib/libvirt/images/{,**} rw,

  # User VM images
  owner @{user_share_dirs}/ r,
  owner @{user_share_dirs}/libvirt/{,**} rw,
  owner @{user_vm_dirs}/{,**} rw,

  owner @{user_config_dirs}/libvirt/storage/{,**} rw,

  owner @{user_share_dirs}/gnome-boxes/images/{,*} rw,
  owner @{user_share_dirs}/images/{,*} rw,

  owner @{run}/user/@{uid}/libvirt/common/ rw,
  owner @{run}/user/@{uid}/libvirt/common/system.token rwk,
  owner @{run}/user/@{uid}/libvirt/virtstoraged* w,
  owner @{run}/user/@{uid}/libvirt/virtstoraged.pid rwk,
  owner @{run}/user/@{uid}/libvirt/storage/{,**} rwk,

  owner @{run}/libvirt/common/system.token rwk,
  owner @{run}/libvirt/storage/{,**} rwk,
  owner @{run}/virtstoraged.pid rwk,

  @{run}/systemd/inhibit/[0-9]*.ref rw,
  @{run}/utmp rwk,

  @{sys}/devices/system/node/ r,
  @{sys}/devices/system/node/node@{int}/meminfo r,

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

  include if exists <local/virtstoraged>
}

# vim:syntax=apparmor
