# 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
@{att} = /att/torbrowser-launcher/
profile torbrowser-launcher /{,usr/}bin/torbrowser-launcher  flags=(attach_disconnected,attach_disconnected.path=@{att},complain) {
  include <abstractions/attached/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}/{,e}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 flags=(attach_disconnected,attach_disconnected.path=@{att},complain) {
    include <abstractions/attached/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
