keepcalculator

06 Numbers and Powers

OS-Level Sandboxing: Limit AI Agent Access

Card No. 06·66916 · Updated · Standard formula, shown below

OS-level sandboxing confines an AI agent to a tightly controlled environment, restricting which files, network resources, and system calls it can touch. This guide shows you how to set up such a sandbox on Linux, macOS, or Windows, giving you a practical layer of protection against accidental damage or malicious behavior from an AI you run locally.

What You’ll Need

  • A computer running Linux (with systemd), macOS, or Windows 10/11 Pro or Enterprise.
  • Administrator (root) access to install software and create users.
  • Basic comfort with the command line (for Linux/macOS) or PowerShell (for Windows).
  • A specific AI agent binary or script you intend to run (e.g., a Python-based agent or a compiled model runner).

Step-by-Step

Linux (using systemd and AppArmor)

  1. Create a dedicated system user. Run sudo useradd -r -m -d /home/aiagent aiagent. This creates a system account with no login shell, home directory at /home/aiagent.
  2. Create a sandbox directory. Make a folder the agent can write to: sudo mkdir -p /srv/aiagent/data and give ownership to the new user: sudo chown aiagent:aiagent /srv/aiagent/data.
  3. Write a systemd service file. Create /etc/systemd/system/aiagent.service with content like:
    Description=AI Agent Sandboxed Service
    After=network.target
    
    [Service]
    User=aiagent
    Group=aiagent
    WorkingDirectory=/srv/aiagent
    ExecStart=/usr/bin/python3 /srv/aiagent/agent.py
    NoNewPrivileges=yes
    PrivateTmp=yes
    PrivateDevices=yes
    ProtectSystem=strict
    ProtectHome=yes
    ReadWritePaths=/srv/aiagent/data
    RestrictAddressFamilies=AF_INET AF_INET6
    RestrictRealtime=yes
    MemoryMax=2G
    
    [Install]
    WantedBy=multi-user.target
  4. Enable and start the service. Run sudo systemctl daemon-reload, then sudo systemctl enable --now aiagent.
  5. Apply an AppArmor profile (optional but recommended). Create /etc/apparmor.d/aiagent with a profile that restricts file access further, then run sudo apparmor_parser -r /etc/apparmor.d/aiagent and add AppArmorProfile=aiagent to the service file.

macOS (using sandbox-exec)

  1. Create a sandbox profile file. Write a file, e.g., aiagent.sb, with:
    (version 1)
    (deny default)
    (allow network-outbound)
    (allow file-read*)
    (allow file-write* (subpath "/Users/you/aiagent-data"))
    (allow process*)
    (allow sysctl-read)
  2. Run the agent under the sandbox. Use sandbox-exec -f aiagent.sb /path/to/agent. This restricts file writes to the specified directory and blocks network access except outbound connections.
  3. Note: sandbox-exec is deprecated but still works on recent macOS versions. For stricter control, consider using a dedicated macOS app sandbox via Xcode, but that requires more setup.

Windows (using AppContainer or Windows Sandbox)

  1. Use Windows Sandbox (easiest). Enable it via Control Panel > Programs > Turn Windows features on or off > Windows Sandbox. Restart. Then launch Windows Sandbox and run your AI agent inside that virtualized environment. It is isolated from the host.
  2. For a more persistent solution, create a low-privilege user. Open PowerShell as Administrator and run New-LocalUser -Name 'aiagent' -NoPassword. Then create a folder for the agent and grant only that user write permissions.
  3. Run the agent as that user. Use runas /user:aiagent "cmd /c your_agent_command". This limits file access to what that user can reach.
  4. For network restrictions, use Windows Firewall. Create an outbound rule that blocks the agent’s executable from making network connections unless you explicitly allow specific IPs or ports.

Common Mistakes

  • Forgetting to set NoNewPrivileges=yes on Linux. Without this, the agent could escalate privileges via setuid binaries.
  • Giving the sandboxed user write access to the agent’s own code directory. If the agent is compromised, it could modify its own script and persist. Keep code read-only.
  • Allowing all network access. Many agents need internet, but you should restrict to specific domains or ports if possible. On Linux, use IPAddressAllow in the service file.
  • Not testing the sandbox before running the real agent. Run a harmless command like whoami or touch testfile inside the sandbox to verify it works as expected.
  • On Windows, using the default user profile. If you run the agent as your own user, it can read your documents and browser history. Always create a separate user.

Tips

  • Start with the most restrictive policy and open up only what breaks. This is easier than trying to close holes after the fact.
  • Use a separate network namespace on Linux. Add PrivateNetwork=yes to the systemd service to give the agent no network at all, then use a proxy or a virtual interface if it needs outbound access.
  • Monitor the sandbox logs. On Linux, use journalctl -u aiagent to see what the agent is doing. Unexpected file access attempts will show up as denials.
  • Consider using a full VM or container (like Docker) for extra isolation. OS-level sandboxing is lighter but not as strong as a hypervisor. Combine both if you need high security.
  • Keep the sandbox user’s shell set to /usr/sbin/nologin on Linux to prevent interactive logins.

Troubleshooting

  • The agent cannot write files. Check the permissions on the data directory. On Linux, ensure the ReadWritePaths in the service file matches the actual path. On macOS, verify the subpath in the sandbox profile is correct.
  • The agent cannot connect to the internet. On Linux, if you set IPAddressAllow, make sure you included the necessary IPs. If you used PrivateNetwork=yes, you must disable it or set up a virtual network. On Windows, check the firewall rule.
  • The service fails to start on Linux. Run sudo systemctl status aiagent to see the error. Common issues include missing ExecStart path or the user not having execute permission on the script.
  • On macOS, sandbox-exec gives a syntax error. Ensure the profile file is well-formed. Use sandbox-exec -n to test the profile without running a command.
  • On Windows, the agent runs but has no network. If you created a firewall block rule, check the scope. You might have blocked all outbound traffic, including DNS. Allow DNS (port 53) separately.

Frequently Asked Questions

  1. Can I use OS-level sandboxing to completely prevent an AI from leaking data? No. It limits what the agent can access, but if the agent has internet access, it could exfiltrate data it can read. Combine with network egress filtering and consider a full VM for high-security needs.
  2. Is systemd sandboxing enough for a malicious AI? It provides a strong baseline, but it is not a security boundary as strong as a hypervisor. For truly untrusted code, use a VM.
  3. Does sandboxing affect AI performance? Minimally. The overhead is negligible for CPU and memory, but network restrictions might slow down model downloads or API calls.
  4. Can I use AppArmor and systemd together? Yes, they complement each other. systemd handles resource limits and namespace isolation, while AppArmor restricts file paths and capabilities.
  5. What is the easiest way on Windows? Windows Sandbox is the simplest and most isolated option. It is a lightweight VM that resets on each launch.
  6. Will the agent be able to access the GPU? On Linux, you may need to add DeviceAllow rules for the GPU device. On Windows Sandbox, GPU access is limited. For GPU-heavy agents, consider a container with GPU passthrough instead.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →