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

abi <abi/3.0>,

include <tunables/global>

@{exec_path} = @{bin}/arduino
profile arduino @{exec_path} {
  include <abstractions/base>
  include <abstractions/consoles>
  include <abstractions/dconf-write>
  include <abstractions/nameservice-strict>
  include <abstractions/gtk>
  include <abstractions/fonts>
  include <abstractions/fontconfig-cache-read>
  include <abstractions/freedesktop.org>
  include <abstractions/devices-usb>

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

  ptrace (read) peer=arduino//open,
  ptrace (read) peer=arduino-builder,

  @{exec_path} mr,

  @{sh_path}                rix,
  @{bin}/id                 rix,
  @{bin}/{,e}grep           rix,
  @{bin}/groups             rix,
  @{bin}/sed                rix,
  @{bin}/cat                rix,
  @{bin}/chmod              rix,
  @{bin}/avrdude            rix,

  @{bin}/xdg-open           rCx -> open,

  @{bin}/dpkg-architecture  rPx,
  @{bin}/arduino-builder    rPx,

  @{lib}/jvm/java-[0-9]*-openjdk-*/bin/java rix,
  @{lib}/jvm/java-[0-9]*-openjdk-*/lib/server/classes.jsa mr,
  /usr/share/java/*.jar r,
  /etc/java-[0-9]*-openjdk/** r,
  /etc/ssl/certs/java/cacerts r,
  owner @{HOME}/.java/fonts/*/ rw,
  owner @{HOME}/.java/fonts/*/fcinfo[0-9]*.tmp rw,
  owner @{HOME}/.java/fonts/*/fcinfo-*.properties rw,

  /usr/share/arduino/{,**} r,
  /usr/share/arduino-builder/{,**} r,

  /usr/share/doc/arduino/{,**} r,
  /usr/share/doc/arduino-core/{,**} r,

  owner @{HOME}/ r,
  owner @{HOME}/.arduino{,15}/{,**} rw,
  owner @{HOME}/Arduino/{,**} rw,
  owner @{HOME}/sketchbook/{,**} rw,

  owner @{HOME}/.Xauthority r,

        /tmp/ r,
  owner @{tmp}/cc*.{s,res,c,o,ld,le} rw,
  owner @{tmp}/hsperfdata_@{user}/ rw,
  owner @{tmp}/hsperfdata_@{user}/@{pid} rw,
  owner @{tmp}/untitled[0-9]*.tmp rw,
  owner @{tmp}/untitled[0-9]*.tmp/{,**} rw,
  owner @{tmp}/console[0-9]*.tmp rw,
  owner @{tmp}/console[0-9]*.tmp/{,**} rw,
  owner @{tmp}/build[0-9]*.tmp rw,
  owner @{tmp}/build[0-9]*.tmp/{,**} rw,
  owner @{tmp}/arduino_{build,cache}_[0-9]*/{,**} rw,
  owner @{tmp}/{library,package}_index.json*.tmp* rw,
  owner @{tmp}/arduino_modified_sketch_[0-9]*/{,**} rw,

  owner @{run}/lock/tmp* rw,
  owner @{run}/lock/LCK..ttyS[0-9]* rw,

  owner @{PROC}/@{pid}/fd/ r,
  owner @{PROC}/@{pid}/coredump_filter rw,
  owner @{PROC}/@{pid}/mountinfo r,
  owner @{PROC}/@{pid}/cgroup r,
  owner @{PROC}/@{pid}/stat r,
  # For java
       @{PROC}/@{pids}/stat r,
  #
  owner @{PROC}/@{pid}/cmdline r,
        @{PROC}/@{pid}/net/if_inet6 r,
        @{PROC}/@{pid}/net/ipv6_route r,

  /etc/fstab r,

  /etc/avrdude.conf r,

  @{sys}/fs/cgroup/{,**} r,
  @{sys}/class/tty/ r,
  @{sys}/devices/@{pci}/usb@{int}/**/{idVendor,idProduct,manufacturer,serial,product} r,

  /dev/ttyS@{int} rw,
  /dev/ttyACM@{int} rw,

  # Silencer
  deny /usr/share/arduino/** w,


  profile open {
    include <abstractions/base>
    include <abstractions/xdg-open>

    @{bin}/xdg-open mr,

    @{sh_path}             rix,
    @{bin}/{m,g,}awk       rix,
    @{bin}/readlink        rix,
    @{bin}/basename        rix,

    owner @{HOME}/ r,

    owner @{run}/user/@{uid}/ r,

    # Allowed apps to open
    @{lib}/firefox/firefox rPUx,
    @{bin}/spacefm         rPUx,

    # file_inherit
    owner @{HOME}/.xsession-errors w,

  }

  include if exists <local/arduino>
}

# vim:syntax=apparmor
