Mastodon Politics, Power, and Science: Exploring how to sandbox in my framework.

Wednesday, August 13, 2025

Exploring how to sandbox in my framework.



#create a directory and change into it.

mkdir jail_test

cd jail_test


# setup new group and user.
sudo groupadd agent_workflow_group

sudo useradd -r -g agent_workflow_group -M -s /sbin/nologin agent_workflow

# check to see if the new agent is on the end of the list
less /etc/passwd


# build the jail directory

mkdir test

# add a test file put something in it
vi test/hello.world  

# change the owner to be the agent so we can see the contents
sudo chown -r agent_workflow:agent_workflow_group test/

# edit the file you need

# check the file to make sure it is doing what it says it is

vi jail_test.py


# run the program.  

# In order to drop privledges we need extra at first

sudo $(which python) jail_test.py 

 


Successfully found UID for user 'agent_workflow': 994


--- BEFORE JAILING ---

Current UID: 0 (0 = root)

Listing '/' with Python:

['proc', 'var', 'lib64', 'vmlinuz', 'vmlinuz.old', 'tmp', 'dev', 'bin', 'lost+found', 'etc', 'boot', 'media', '.cache', 'sys', 'root', 'run', 'home', 'initrd.img', 'mnt', 'srv', 'initrd.img.old', 'lib', 'usr', 'sbin', 'opt']

--------------------


--- ATTEMPTING TO JAIL AND DROP PRIVILEGES ---

-> Chroot to './test' successful.

-> Changed directory to new root '/' successful.

-> Privileges permanently dropped to user 'agent_workflow'.


--- AFTER JAILING ---

Current UID: 994 (Should now be 994)

Listing '/' with Python:

['hello.world']

--------------------


--- ATTEMPTING TO RECLAIM ROOT PRIVILEGES ---

-> SUCCESS: Blocked from reclaiming root privileges.

-> Error message: [Errno 1] Operation not permitted

-> Current UID remains: 994

--------------------


$ cat jail_test2.py 


# simple_jail_test.py

import os

import sys

import pwd


# We must start as root to chroot and setuid

if os.geteuid() != 0:

    print("This script must be run with sudo.")

    sys.exit(1)


JAIL_PATH = "./test"

WORKER_USER = "agent_workflow"


# --- PRE-JAIL ---

# We must get the user's UID *before* we chroot, because after the jail,

# the script won't be able to see /etc/passwd to look up the user.

try:

    worker_uid = pwd.getpwnam(WORKER_USER).pw_uid

    print(f"Successfully found UID for user '{WORKER_USER}': {worker_uid}")

except KeyError:

    print(f"FATAL ERROR: User '{WORKER_USER}' not found. Please create it first.")

    sys.exit(1)


print("\n--- BEFORE JAILING ---")

print(f"Current UID: {os.getuid()} (0 = root)")

print("Listing '/' with Python:")

print(os.listdir('/'))

print("-" * 20)


# --- THE JAILING CEREMONY ---

try:

    print(f"\n--- ATTEMPTING TO JAIL AND DROP PRIVILEGES ---")

    

    # 1. Chroot into the jail

    os.chroot(JAIL_PATH)

    print(f"-> Chroot to '{JAIL_PATH}' successful.")

    

    # 2. Change to the new root directory

    os.chdir('/')

    print("-> Changed directory to new root '/' successful.")

    

    # 3. Permanently drop root privileges

    os.setuid(worker_uid)

    print(f"-> Privileges permanently dropped to user '{WORKER_USER}'.")

    

except Exception as e:

    print(f"-> Jailing FAILED: {e}")

    sys.exit(1)


# --- POST-JAIL ---

print("\n--- AFTER JAILING ---")

print(f"Current UID: {os.getuid()} (Should now be {worker_uid})")

print("Listing '/' with Python:")


# This code is now running as the powerless 'agent_workflow' user,

# trapped inside the './test' directory.

print(os.listdir('/'))

print("-" * 20)



# --- THE CRITICAL TEST ---

print("\n--- ATTEMPTING TO RECLAIM ROOT PRIVILEGES ---")

try:

    # Attempt to set the user ID back to 0 (root)

    os.setuid(0)

    

    # If this line is ever reached, the sandbox is broken.

    print("!!! SECURITY FAILURE: Successfully changed back to root!")

    print(f"!!! Current UID: {os.getuid()}")

    

except PermissionError as e:

    # This is the expected and correct outcome.

    print(f"-> SUCCESS: Blocked from reclaiming root privileges.")

    print(f"-> Error message: {e}")

    print(f"-> Current UID remains: {os.getuid()}")

    

except Exception as e:

    print(f"-> An unexpected error occurred: {e}")


print("-" * 20)

No comments:

Post a Comment

Progress on the campaign manager

You can see that you can build tactical maps automatically from the world map data.  You can place roads, streams, buildings. The framework ...