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

abi <abi/4.0>,

include <tunables/global>

@{lib_dirs} = @{user_share_dirs}/torbrowser/tbb/@{arch}/tor-browser/Browser/ @{HOME}/.tb/tor-browser/Browser/

@{exec_path} = @{bin}/torbrowser-launcher
profile torbrowser-launcher @{exec_path} flags=(attach_disconnected) {
  include <abstractions/base>
  include <abstractions/dconf-write>
  include <abstractions/desktop>
  include <abstractions/graphics>
  include <abstractions/nameservice-strict>
  include <abstractions/python>
  include <abstractions/ssl_certs>

  network inet dgram,
  network inet stream,
  network inet6 dgram,
  network inet6 stream,

  network netlink raw,

  @{exec_path} mrix,

  @{sh_path}      rix,
  @{bin}/file      ix,
  @{bin}/gpg{,2}   Cx -> gpg,
  @{bin}/gpgconf   Cx -> gpg,
  @{bin}/gpgsm     Cx -> gpg,
  @{bin}/grep      ix,
  @{bin}/sed       ix,
  @{bin}/tail      ix,

  @{lib_dirs}/execdesktop       ix,
  @{lib_dirs}/start-tor-browser Px, # torbrowser-start
  @{user_share_dirs}/torbrowser/tbb/@{arch}/tor-browser/start-tor-browser.desktop ix,

  /usr/share/file/** r,
  /usr/share/torbrowser-launcher/{,**} r,

  owner @{user_cache_dirs}/torbrowser/{,**/} rw,
  owner @{user_cache_dirs}/torbrowser/download/** rw,
  owner @{user_cache_dirs}/torbrowser/torbrowser.gpg rw,

  owner @{user_config_dirs}/torbrowser/{,**/} rw,
  owner @{user_config_dirs}/torbrowser/settings.json rw,

  owner @{user_share_dirs}/torbrowser/{,**} rw,

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

  /dev/tty rw,

  profile gpg {
    include <abstractions/base>

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

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

    owner @{HOME}/ r,
    owner @{HOME}/@{XDG_GPG_DIR}/ r,
    owner @{HOME}/@{XDG_GPG_DIR}/gpg.conf r,
    owner @{HOME}/@{XDG_GPG_DIR}/gpgsm.conf r,

    owner @{user_share_dirs}/torbrowser/ r,
    owner @{user_share_dirs}/torbrowser/gnupg_homedir/ rw,
    owner @{user_share_dirs}/torbrowser/gnupg_homedir/** rwkl -> @{HOME}/@{XDG_GPG_DIR}/**,

    owner @{run}/user/@{uid}/ r,
    owner @{run}/user/@{uid}/gnupg/ r,
    owner @{run}/user/@{uid}/gnupg/d.@{rand}/ rw,
    owner @{run}/user/@{uid}/gnupg/d.@{rand}/S.gpg-agent{,.ssh,.browser,.extra} rw,
    owner @{run}/user/@{uid}/gnupg/S.scdaemon rw,

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

    include if exists <local/torbrowser-launcher_gpg>
  }

  include if exists <local/torbrowser-launcher>
}

# vim:syntax=apparmor
