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

abi <abi/4.0>,

include <tunables/global>

@{exec_path} = @{lib}/cockpit/cockpit-session
@{att} = /att/cockpit-session/
profile cockpit-session /{,usr/}lib{,exec,32,64}/cockpit/cockpit-session  flags=(attach_disconnected,attach_disconnected.path=@{att},complain) {
  include <abstractions/attached/base>
  include <abstractions/authentication>
  include <abstractions/bus-system>
  include <abstractions/nameservice-strict>
  include <abstractions/shells>

  capability audit_write,
  capability chown,
  capability dac_read_search,
  capability net_admin,
  capability setgid,
  capability setuid,
  capability sys_resource,

  network netlink raw,

  @{exec_path} mr,

  @{shells_path}         rix,
  @{bin}/cockpit-bridge  rpx,
  @{lib}/cockpit/cockpit-pcp  rpx,
  @{bin}/ssh-agent       rpx,
  @{bin}/ssh-add         rcx -> ssh-add,

  @{etc_ro}/environment r,
  @{etc_ro}/security/limits.d/{,*.conf} r,
  /etc/cockpit/disallowed-users r,
  /etc/group r,
  /etc/machine-id r,
  /etc/motd r,
  /etc/motd.d/ r,
  /etc/shells r,

  /var/log/btmp rw,
  /var/log/lastlog rw,
  /var/log/wtmp rwk,

  /var/lib/lastlog/ r,
  /var/lib/lastlog/lastlog2.db rwk,
  /var/lib/lastlog/lastlog2.db-journal rw,

  @{att}@{run}/systemd/sessions/*.ref rw,

  @{run}/cockpit/* r,
  @{run}/faillock/@{user} rwk,
  @{run}/motd.d/{,*} r,
  @{run}/utmp rwk,

        @{PROC}/@{pids}/fd/ r,
  owner @{PROC}/@{pid}/loginuid rw,
  owner @{PROC}/@{pid}/uid_map r,

  /dev/tty rw,

  profile ssh-add  flags=(attach_disconnected,attach_disconnected.path=@{att},complain) {
    include <abstractions/attached/base>

    @{bin}/ssh-add mr,

    owner @{HOME}/@{XDG_SSH_DIR}/{,*} r,

    owner @{tmp}/ssh-@{rand12}/ rw,
    owner @{tmp}/ssh-@{rand12}/agent.@{int} rw,

    include if exists <local/cockpit-session_ssh-add>
  }

  include if exists <local/cockpit-session>
}

# vim:syntax=apparmor
