# 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>

@{exec_path} = @{bin}/ollama
@{att} = /att/ollama/
profile ollama /{,usr/}bin/ollama  flags=(attach_disconnected,attach_disconnected.path=@{att},complain) {
  include <abstractions/attached/base>
  include <abstractions/attached/consoles>
  include <abstractions/graphics-full>
  include <abstractions/nameservice-strict>
  include <abstractions/ssl_certs>

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

  @{exec_path} mrix,

  /tmp/ollama@{int}/runners/*/*                mr,
  /tmp/ollama@{int}/runners/*/ollama_*_server  rix, # TODO: rpx and remove graphics from here.

  /usr/ r,
  /usr/local/ r,
  /usr/local/lib/ r,

  @{lib}/ollama/ r,
  @{lib}/ollama/*.so mr,

  owner /var/lib/ollama/ rw,
  owner /var/lib/ollama/** rwlk,

  owner @{HOME}/.ollama/{,*} rw,

        @{tmp}/ r,
  owner @{tmp}/@{int}.bin rw,
  owner @{tmp}/ollama@{int}/{,**} rw,
  owner @{tmp}/ollama@{int}/runners/{,**} mr,

  @{sys}/devices/@{pci}/drm/card@{int}/ r,
  @{sys}/devices/@{pci}/drm/card@{int}/*/ r,
  @{sys}/devices/@{pci}/mem_info_vram_total r,
  @{sys}/devices/@{pci}/mem_info_vram_used r,
  @{sys}/devices/@{pci}/numa_node r,
  @{sys}/devices/system/node/node@{int}/cpumap r,


        @{PROC}/devices r,
        @{PROC}/sys/net/core/somaxconn r,
  owner @{PROC}/@{pid}/cgroup r,
  owner @{PROC}/@{pid}/cmdline r,
  owner @{PROC}/@{pid}/mountinfo r,
  owner @{PROC}/@{pid}/task/@{tid}/comm w,

  include if exists <local/ollama>
}

# vim:syntax=apparmor
