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

abi <abi/3.0>,

include <tunables/global>

@{exec_path} = @{lib}/apt/methods/gpgv
profile apt-methods-gpgv @{exec_path} {
  include <abstractions/base>
  include <abstractions/consoles>
  include <abstractions/nameservice-strict>

  # The "_apt" user is created by the postinst script of the "apt" package. It's the owner of the
  # dirs "/var/cache/apt/archives/partial/" and "/var/lib/apt/lists/partial/" . The "_apt" user is
  # used by APT to download packages, package list, and other things using APT methods as an
  # unprivileged user/group (_apt/nogroup).
  capability setgid,
  capability setuid,

  signal (receive) peer=apt-get,
  signal (receive) peer=apt,
  signal (receive) peer=aptitude,
  signal (receive) peer=packagekitd,
  signal (receive) peer=synaptic,

  @{exec_path} mr,

  # The following get "no new privs" so "rix" them
  @{bin}/apt-config        rix,
  @{bin}/apt-key           rix,
  @{bin}/dpkg              rix,
  @{bin}/find              rix,
  @{bin}/gpg-connect-agent rix,
  @{bin}/gpgconf           rix,
  @{bin}/gpgv              rix,

  @{bin}/{m,g,}awk rix,
  @{bin}/base64    rix,
  @{bin}/cat       rix,
  @{bin}/chmod     rix,
  @{bin}/cmp       rix,
  @{bin}/cp        rix,
  @{bin}/head      rix,
  @{bin}/mktemp    rix,
  @{bin}/readlink  rix,
  @{bin}/rm        rix,
  @{bin}/sed       rix,
  @{bin}/sort      rix,
  @{bin}/touch     rix,

  /usr/share/dpkg/cputable r,
  /usr/share/dpkg/tupletable r,
  /usr/share/keyrings/ r,
  /usr/share/keyrings/*.{gpg,asc} r,

  /etc/apt/apt.conf r,
  /etc/apt/apt.conf.d/{,*} r,
  /etc/apt/keyrings/ r,
  /etc/apt/keyrings/*.{gpg,asc} r,
  /etc/apt/trusted.gpg r,
  /etc/apt/trusted.gpg.d/{,*.{gpg,asc}} r,
  /etc/dpkg/dpkg.cfg r,
  /etc/dpkg/dpkg.cfg.d/{,*} r,

  # For shell pwd
  / r,
  /etc/ r,
  /root/ r,

        /var/lib/apt/lists/{,**} r,
        /var/lib/dpkg/arch r,
        /var/lib/extrepo/keys/*.{gpg,asc} r,
        /var/lib/ubuntu-advantage/apt-esm/{,**} rw,
  owner /var/lib/apt/lists/{,**} rw,
  owner /var/lib/apt/lists/partial/* rw,

  # For package building
  @{user_build_dirs}/** rwkl -> @{user_build_dirs}/**,

        /tmp/ r,
  owner @{tmp}/apt-key-gpghome.*/ rw,
  owner @{tmp}/apt-key-gpghome.*/** rwkl -> /tmp/apt-key-gpghome.*/**,
  owner @{tmp}/apt.{conf,sig,data}.* rw,

  @{PROC}/@{pid}/fd/ r,

  # file_inherit
  owner /dev/tty@{int} rw,
  /var/log/cron-apt/temp w,

  include if exists <local/apt-methods-gpgv>
}
