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

abi <abi/4.0>,

include <tunables/global>

@{exec_path} = @{lib}/flatpak-system-helper
profile flatpak-system-helper @{exec_path} {
  include <abstractions/base>
  include <abstractions/nameservice-strict>
  include <abstractions/p11-kit>
  include <abstractions/ssl_certs>

  capability chown,
  capability dac_override,
  capability fowner,
  capability net_admin,
  capability setgid,
  capability setuid,
  capability sys_nice,
  capability sys_ptrace,

  ptrace (read),

  #aa:dbus own bus=system name=org.freedesktop.Flatpak.SystemHelper

  @{exec_path} mr,

  @{bin}/bwrap         rPUx,
  @{bin}/gpg{,2}        rCx -> gpg,
  @{bin}/gpgconf        rCx -> gpg,
  @{bin}/gpgsm          rCx -> gpg,
  @{lib}/revokefs-fuse  rix,

  /etc/flatpak/{,**} r,
  /etc/machine-id r,

  /usr/share/flatpak/remotes.d/ r,
  /usr/share/flatpak/triggers/ r,
  /usr/share/mime/mime.cache r,

  /var/lib/flatpak/{,**} rwkl,
  /var/tmp/flatpak-cache-*/{,**} rw,

  owner /{var/,}tmp/#@{int} rw,
  owner /{var/,}tmp/ostree-gpg-@{rand6}/ rw,
  owner @{tmp}/ostree-gpg-@{rand6}/** rwkl -> /tmp/ostree-gpg-@{rand6}/**,

        @{PROC}/@{pid}/stat r,
  owner @{PROC}/@{pid}/fd/ r,
  owner @{PROC}/@{pid}/fdinfo/@{int} r,

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

    @{bin}/gpg{,2}  mr,
    @{bin}/gpgconf  mr,
    @{bin}/gpgsm    mr,

    @{lib}/{,gnupg/}scdaemon rix,
    @{bin}/gpg-agent rix,

    owner @{tmp}/ostree-gpg-@{rand6}/ r,
    owner @{tmp}/ostree-gpg-@{rand6}/** rwkl -> /tmp/ostree-gpg-@{rand6}/**,

    owner @{PROC}/@{pid}/fd/ r,
    owner @{PROC}/@{pid}/task/@{tid}/comm rw,

    include if exists <local/flatpak-system-helper_gpg>
  }

  include if exists <local/flatpak-system-helper>
}

# vim:syntax=apparmor
