# 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} = @{bin}/ubuntu-advantage
profile ubuntu-advantage @{exec_path} {
  include <abstractions/base>
  include <abstractions/common/apt>
  include <abstractions/consoles>
  include <abstractions/nameservice-strict>
  include <abstractions/python>
  include <abstractions/ssl_certs>

  capability dac_read_search,
  capability setgid,
  capability setuid,

  network inet stream,
  network inet6 stream,
  network inet dgram,
  network inet6 dgram,
  network netlink raw,

  signal (send) set=int peer=apt-methods-http,

  @{exec_path} mr,

  @{bin}/ischroot                       rix,

  @{bin}/apt                            rPx,
  @{bin}/apt-cache                      rPx,
  @{bin}/apt-config                     rPx,
  @{bin}/apt-get                        rPx,
  @{bin}/dpkg                           rPx -> child-dpkg,
  @{bin}/ps                             rPx,
  @{bin}/snap                          rPUx,
  @{bin}/systemctl                      rCx ->  systemctl,
  @{bin}/systemd-detect-virt            rPx,
  @{bin}/ubuntu-distro-info             rPx,
  @{lib}/apt/apt-helper                 rix,
  @{lib}/apt/methods/http{,s}           rPx,
  @{lib}/ubuntu-advantage/apt-esm-hook  rPx,

  /etc/apt/auth.conf.d/{,**} rw,
  /etc/apt/trusted.gpg.d/{,**} rw,
  /etc/apt/sources.list.d/{,**} rw,
  /etc/ubuntu-advantage/{,**} r,
  /var/lib/ubuntu-advantage/{,**} rw,

  /etc/machine-id r,

  owner @{tmp}/tmp[0-9a-z]*/apt.conf r,
  owner @{tmp}/[0-9a-z]*{,/} rw,
  owner @{tmp}/[0-9a-z]*/apt-helper-output rw,

  @{run}/ubuntu-advantage/{,**} rw,

        @{PROC}/version_signature r,
        @{PROC}/@{pids}/mountinfo r,
        @{PROC}/@{pids}/mounts r,
  owner @{PROC}/@{pid}/fd/ r,

  profile systemctl {
    include <abstractions/base>

    capability sys_resource,

    ptrace (read),

    @{bin}/systemctl mr,

    @{bin}/systemd-tty-ask-password-agent rix,

    owner @{run}/systemd/ask-password/ rw,
    owner @{run}/systemd/ask-password-block/* rw,

          @{PROC}/1/environ r,
          @{PROC}/1/sched r,
          @{PROC}/cmdline r,
          @{PROC}/sys/kernel/osrelease r,
    owner @{PROC}/@{pid}/fd/ r,
    owner @{PROC}/@{pid}/stat r,

    /dev/kmsg w,

    include if exists <local/ubuntu-advantage_systemctl>
  }

  include if exists <local/ubuntu-advantage>
}

# vim:syntax=apparmor
