#include <tunables/global>

profile tor /usr/bin/tor flags=(attach_disconnected) {
  #include <abstractions/base>

  # --- Capabilities (Dropped net_bind_service & dac_override) ---
  capability setgid,
  capability setuid,
  capability kill,       # Required for signaling worker threads

  # --- Network ---
  network inet stream,
  network inet dgram,
  network inet6 stream,
  network inet6 dgram,
  # network unix stream  <-- Dropped, using TCP for ControlPort/SocksPort

  # --- Executable ---
  /usr/bin/tor mr,

  # --- Configuration ---
  /etc/tor/ r,
  /etc/tor/torrc r,
  /etc/tor/torrc.edge r,
  /usr/share/tor/ r,
  /usr/share/tor/** r,

  # --- System Tor Data (Ghostnet/netmode) ---
  /var/lib/tor/ rw,
  /var/lib/tor/** rwk,
  /run/tor/ rw,
  /run/tor/** rwk,
  /var/run/tor/ rw,
  /var/run/tor/** rwk,
  /run/tor/tor.pid rw,
  /var/run/tor/tor.pid rw,
  /var/log/tor/notices.log rw,

  # --- Edge Tor Data (Edge netns) ---
  /var/lib/tor-edge/ rw,
  /var/lib/tor-edge/** rwk,
  /run/tor/tor-edge.pid rw,
  /var/run/tor/tor-edge.pid rw,
  /var/log/tor/edge-notices.log rw,

  # --- Shared system files needed by Tor ---
  /etc/ld.so.cache r,
  /etc/localtime r,
  /etc/hosts r,
  /etc/nsswitch.conf r,
  /etc/passwd r,
  /etc/group r,
  /dev/null rw,
  /dev/urandom r,
  /dev/random r,
  /proc/self/** r,

  # --- Strict Deny Rules ---
  # Deny execution of ALL external binaries.
  # Tor should never spawn a shell, curl, wget, etc.
  deny /** x,
  
  # Deny reading sensitive system data
  deny /etc/shadow r,
  deny /etc/gshadow r,
  deny /etc/ssh/** r,
  deny /root/** r,
  deny /home/** r,

  # Deny writing to system directories
  deny /bin/**   w,
  deny /sbin/**  w,
  deny /lib/**   w,
  deny /lib64/** w,
  deny /usr/**   w,
  deny /etc/**   w,
  deny /boot/**  w,

  # Deny privilege escalation capabilities
  deny capability sys_admin,
  deny capability sys_module,
  deny capability sys_rawio,
  deny capability sys_ptrace,
  deny capability mac_admin,
  deny capability mac_override,
}
