#include <tunables/global>

# socat for AM·OS Edge Relays.
# Spawned by edge-relays.sh via `su -s /bin/sh socat -c ...`
# Runs inside the edge-relay netns as the unprivileged socat user.
profile socat /usr/bin/socat flags=(attach_disconnected) {
  #include <abstractions/base>
  #include <abstractions/nameservice>

  # socat runs as the 'socat' user. It binds to port 10000+ (unprivileged).
  # Therefore, it needs NO capabilities (no net_bind_service, no setuid).
  # It only needs to open network sockets.

  # Only TCP over IPv4 is needed for the SOCKS4A relay.
  network inet stream,

  # --- Binary + libraries --------------------------------------------
  /usr/bin/socat mr,
  /lib64/ld-linux-x86-64.so.2 mr,
  /lib64/libc.so.6 mr,
  /lib64/libm.so.6 mr,
  /lib64/libdl.so.2 mr,
  /lib64/libpthread.so.0 mr,
  /lib64/libssl.so.3 mr,
  /lib64/libcrypto.so.3 mr,
  /lib64/libz.so.1* mr,
  /lib64/libwrap.so.0* mr,  # If compiled with TCP Wrappers
  /etc/ld.so.cache r,

  # --- Runtime + Devices ---------------------------------------------
  /dev/null rw,
  /dev/urandom r,
  /dev/random r,

  # socat's child processes might try to read basic network config
  /etc/hosts r,
  /etc/nsswitch.conf r,
  /etc/resolv.conf r,
  /etc/passwd r,
  /etc/group r,

  # --- Kernel interfaces ---------------------------------------------
  /proc/self/** r,
  /proc/sys/net/ipv4/ip_local_port_range r,

  # --- Explicit Denials ----------------------------------------------

  # 1. Blanket denial: socat must NEVER execute any other binary.
  # This prevents an attacker from using socat's EXEC: feature to spawn a shell.
  deny /** x,

  # 2. Prevent reading sensitive files.
  deny /etc/shadow r,
  deny /etc/gshadow r,
  deny /etc/ssh/** r,
  deny /root/** r,
  deny /home/** r,

  # 3. Prevent 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 /root/**  w,
  deny /var/**   w,

  # 4. Dangerous 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,
}