# apparmor.d - Full set of apparmor profiles
# Copyright (C) 2022-2024 Alexandre Pujol <alexandre@pujol.io>
# SPDX-License-Identifier: GPL-2.0-only
# LOGPROF-SUGGEST: no
# NEEDS-VARIABLE: name
# NEEDS-VARIABLE: domain
# NEEDS-VARIABLE: lib_dirs
# NEEDS-VARIABLE: config_dirs
# NEEDS-VARIABLE: cache_dirs

# A full set of rules for all chromium based browsers. It works as a *function*
# and requires some variables to be provided as *arguments* and set in the
# header of the calling profile.
#
# !!! quote "[apparmor.d/groups/browsers/chromium](https://github.com/roddhjav/apparmor.d/blob/e979fe05b06f525e5a65c767b4eabe5600147355/apparmor.d/groups/browsers/chromium#L10-L14)"
#
#     ```
#     @{name} = chromium
#     @{domain} = org.chromium.Chromium
#     @{lib_dirs} = @{lib}/chromium
#     @{config_dirs} = @{user_config_dirs}/chromium
#     @{cache_dirs} = @{user_cache_dirs}/chromium
#     ```
#
# If your application requires chromium to run use [`common/chromium`](#commonchromium)
# or [`common/electron`](#commonelectron) instead.

  abi <abi/4.0>,

  include <abstractions/audio-client>
  include <abstractions/avahi-observe>
  include <abstractions/bluetooth-observe>
  include <abstractions/bus/session/org.freedesktop.FileManager1>
  include <abstractions/bus/session/org.gnome.SessionManager>
  include <abstractions/bus/system/org.bluez>
  include <abstractions/camera>
  include <abstractions/common/chromium>
  include <abstractions/cups-client>
  include <abstractions/dconf-write>
  include <abstractions/desktop>
  include <abstractions/devices-u2f>
  include <abstractions/devices-usb-read>
  include <abstractions/fontconfig-cache-read>
  include <abstractions/graphics>
  include <abstractions/mpris>
  include <abstractions/attached/nameservice-strict>
  include <abstractions/pcscd>
  include <abstractions/screen-inhibit>
  include <abstractions/screensaver>
  include <abstractions/secrets-service>
  include <abstractions/ssl_certs>
  include <abstractions/thumbnails-cache-read>
  include <abstractions/uim>
  include <abstractions/upower-observe>
  include <abstractions/user-download-strict>
  include <abstractions/user-read-strict>

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

  signal send set=(term, kill) peer=keepassxc-proxy,
  signal receive peer=@{profile_name}//&@{profile_name}//crashpad_handler,

  ptrace trace peer=@{profile_name},

  ptrace read peer=browserpass,
  ptrace read peer=chrome-gnome-shell,
  ptrace read peer=gnome-browser-connector-host,
  ptrace read peer=keepassxc-proxy,
  ptrace read peer=lsb_release,
  ptrace read peer=plasma-browser-integration-host,
  ptrace read peer=xdg-settings,
  ptrace read peer=kwin_wayland_wrapper,

  @{lib_dirs}/{,**} r,
  @{lib_dirs}/*.so* mr,
  @{lib_dirs}/WidevineCdm/_platform_specific/linux_*/libwidevinecdm.so mr,

  # Desktop integration
  @{bin}/lsb_release        px,
  @{bin}/xdg-desktop-menu   px -> &xdg-desktop-menu,
  @{bin}/xdg-email          px,
  @{bin}/xdg-icon-resource  px -> &xdg-icon-resource,
  @{bin}/xdg-mime          rix,
  @{bin}/xdg-open           px -> child-open-any,
  @{bin}/xdg-settings      rix,

  # Installing/removing extensions, applications, and stacked xdg menus
  @{sh_path}        rix,
  @{coreutils_path}  ix,

  # For storing passwords externally
  @{bin}/keepassxc-proxy    rix, # as a temporary solution - see issue #128
  @{bin}/browserpass        px,

  # Gnome shell integration
  @{bin}/chrome-gnome-shell            px,
  @{bin}/gnome-browser-connector-host  px,

  # Plasma integration
  @{bin}/plasma-browser-integration-host px,

  /usr/share/@{name}/{,**} r,
  /usr/share/chromium/extensions/{,**} r,
  /usr/share/mozilla/extensions/{,**} r,
  /usr/share/webext/{,**} r,

  /etc/@{name}/{,**} r,
  /etc/fstab r,

  / r,
  owner @{HOME}/ r,

  owner @{user_cache_dirs}/gtk-3.0/**/*.cache r,
  owner @{user_config_dirs}/gtk-3.0/servers r,

  owner @{user_share_dirs}/icons/hicolor/.xdg-icon-resource-dummy w,

  owner @{config_dirs}/ rw,
  owner @{config_dirs}/** rwk,
  owner @{config_dirs}/WidevineCdm/*/_platform_specific/linux_*/libwidevinecdm.so mrw,

  owner @{cache_dirs}/ rw,
  owner @{cache_dirs}/** rwk,

  owner @{user_config_dirs}/kioslaverc r,
  owner @{user_config_dirs}/menus/applications-merged/ r,
  owner @{user_config_dirs}/menus/applications-merged/*.menu rw,
  owner @{user_config_dirs}/mimeapps.list{,.new} rw,

  # For importing data (bookmarks, cookies, etc) from Firefox
  # owner @{HOME}/.mozilla/firefox/profiles.ini r,
  # owner @{HOME}/.mozilla/firefox/*/ r,
  # owner @{HOME}/.mozilla/firefox/*/compatibility.ini r,
  # owner @{HOME}/.mozilla/firefox/*/search{,-metadata}.json r,
  # owner @{HOME}/.mozilla/firefox/*/.parentlock rwk,
  # owner @{HOME}/.mozilla/firefox/*/{places,cookies,favicons,formhistory,}.sqlite{,-wal,-shm,-journal} rwk,
  # owner @{HOME}/.mozilla/firefox/*/{cert9,key4}.db rwk,
  # owner @{HOME}/.mozilla/firefox/*/logins.json r,

        /tmp/ r,
        /var/tmp/ r,
  owner @{tmp}/{,.}@{domain}.*/{,**} rw,
  owner @{tmp}/@{name}-crashlog-@{int}-@{int}.txt rw,
  owner @{tmp}/cache/Default/ rw,
  owner @{tmp}/cache/Default/** rwk,
  owner @{tmp}/scoped_dir@{rand6}/{,**} rw,
  owner @{tmp}/tmp.@{rand10} rw,
  owner @{tmp}/tmp.@{rand6} rw,
  owner @{tmp}/tmp.@{rand6}/ rw,
  owner @{tmp}/tmp.@{rand6}/** rwk,

  owner @{run}/user/@{uid}/app/org.keepassxc.KeePassXC/org.keepassxc.KeePassXC.BrowserServer rw,
  owner @{run}/user/@{uid}/org.keepassxc.KeePassXC.BrowserServer rw,

  @{run}/udev/data/c13:@{int}  r,         # for /dev/input/*

  @{sys}/bus/ r,
  @{sys}/bus/**/devices/ r,
  @{sys}/class/**/ r,
  @{sys}/devices/**/uevent r,

  # List processes in /proc
  @{PROC}/ r,

  # Allow reading the memory map of any processes for introspection and debugging
  @{PROC}/@{pid}/maps r,

  # Process status in one line (pid, state, ppid, CPU time, threads, etc.)
  @{PROC}/@{pid}/stat r,

  # Memory usage in pages (total, resident, shared, text, data)
  @{PROC}/@{pid}/statm r,

  # Human-readable process status (name, state, UIDs, memory, capabilities)
  @{PROC}/@{pid}/status r,

  @{PROC}/pressure/cpu r,
  @{PROC}/pressure/io r,
  @{PROC}/pressure/memory r,

  # Human-readable thread status
  @{PROC}/@{pid}/task/@{tid}/status r,

  # Limits for how many inotify instances, watches, and pending events a user can have.
  @{PROC}/sys/fs/inotify/max_queued_events r,
  @{PROC}/sys/fs/inotify/max_user_instances r,
  @{PROC}/sys/fs/inotify/max_user_watches r,

  # Get the ptrace restrictions level
  @{PROC}/sys/kernel/yama/ptrace_scope r,

  # Exposes virtual memory statistics (page faults, swap activity, allocation counts)
  @{PROC}/vmstat r,

  # Allow reading cgroup membership information for process introspection
  owner @{PROC}/@{pid}/cgroup r,

  # Clearing the referenced bits in a process's page table entries provides a method to
  # measure approximately how much memory a process is using.
  owner @{PROC}/@{pid}/clear_refs w,

  # Allow reading command line arguments for process identification
  owner @{PROC}/@{pid}/cmdline r,
  owner @{PROC}/@{pid}/comm r,

  # Allow reading our own environment variables
  owner @{PROC}/@{pid}/environ r,

  # Allow listing file descriptors
        @{PROC}/@{pid}/fd/ r,
  owner @{PROC}/@{pid}/fd/ r,

  # Chromium content api unfortunately needs these for normal operation
  owner @{PROC}/@{pid}/fd/@{int} w,

  # Shows the process's current resource limits (soft/hard), the ulimit value.
  owner @{PROC}/@{pid}/limits r,

  # Allow reading info about the physical mapping of virtual pages
  owner @{PROC}/@{pid}/mem r,

  # This is an information leak but disallowing it leads to developer confusion
  # when using the chromium content api file chooser due to a (harmless) glib
  # warning and the noisy AppArmor denial.
  owner @{PROC}/@{pid}/mounts r,
  owner @{PROC}/@{pid}/mountinfo r,

  # Allow reading of smaps_rollup, which is a summary of the memory use of a process
  owner @{PROC}/@{pid}/smaps_rollup r,

  # Reads of oom_adj and oom_score_adj are safe
  owner @{PROC}/@{pid}/oom_adj r,
  owner @{PROC}/@{pid}/oom_score_adj r,

  # This allows raising the OOM score of other processes owned by the user.
  owner @{PROC}/@{pid}/oom_score_adj w,

  # Per man(5) proc, the kernel enforces that a thread may only modify its comm
  # value or those in its thread group.
  owner @{PROC}/@{pid}/task/@{tid}/comm rw,

  # Provide statistical information about our own processes/threads
  owner @{PROC}/@{pid}/task/ r,
  owner @{PROC}/@{pid}/task/@{tid}/stat r,
  owner @{PROC}/@{pid}/task/@{tid}/status r,

        /dev/ r,
        /dev/tty rw,
  owner /dev/tty@{u8} rw,

  # Silencer
  deny @{lib_dirs}/** w,
  deny @{user_share_dirs}/gvfs-metadata/* r,

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

    capability sys_ptrace,

    signal send peer=@{name},

    ptrace read peer=@{name},
    ptrace trace peer=@{name},

    unix (send receive) type=seqpacket peer=(label=@{name}),

    @{lib_dirs}/chrome_crashpad_handler mrix,
    @{lib_dirs}/@{name}_crashpad_handler mrix,

    owner "@{config_dirs}/{,*/}Crash Reports/**" rwk,

    @{sys}/devices/system/cpu/cpufreq/policy@{int}/scaling_cur_freq r,
    @{sys}/devices/system/cpu/cpufreq/policy@{int}/scaling_max_freq r,

          @{PROC}/@{pid}/maps r,
          @{PROC}/@{pid}/status r,
          @{PROC}/sys/kernel/yama/ptrace_scope r,
    owner @{PROC}/@{pid}/fd/ r,
    owner @{PROC}/@{pid}/mem r,
    owner @{PROC}/@{pid}/stat r,
    owner @{PROC}/@{pid}/task/ r,
    owner @{PROC}/@{pid}/task/@{tid}/comm r,

    include if exists <abstractions/app/chromium_crashpad_handler.d>
  }

  include if exists <abstractions/app/chromium.d>

# vim:syntax=apparmor
