sandlock icon indicating copy to clipboard operation
sandlock copied to clipboard

TCP port remapping fails with EADDRINUSE when virtual port is already in use

Open ghazariann opened this issue 8 months ago • 0 comments

When port_remap=True and the virtual port is already bound by another process, the sandbox child gets OSError: [Errno 98] Address already in use instead of being transparently remapped to a free port.

To reproduce:

import socket
from sandlock.policy import Policy
from sandlock.sandbox import Sandbox

code = (
    "import socket; "
    "s = socket.socket(socket.AF_INET, socket.SOCK_STREAM); "
    "s.bind(('127.0.0.1', 8080)); "
    "print(s.getsockname()[1]); "
    "s.close()"
)

holder = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
holder.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
holder.bind(("127.0.0.1", 8080))
try:
    result = Sandbox(Policy(port_remap=True)).run(["python3", "-c", code])
finally:
    holder.close()

Expected: sandbox succeeds and getsockname() returns 8080

Actual: OSError: [Errno 98] Address already in use

Image

Environment: Linux 6.6, Python 3.13

ghazariann avatar Mar 23 '26 12:03 ghazariann