sandlock
sandlock copied to clipboard
Support UDP port remapping
Currently sandlock only supports TCP for virtual port remapping.
Hi, I'd like to take this. From reading the code (not reproduced yet), bind, getsockname and connect already remap UDP, but the send* handlers don't. PortMap is also keyed by port only, so TCP and UDP mappings collide.
Plan:
- Split
PortMapby protocol. - Translate the destination port in the send handlers, after the policy check.
- Leave receive untrapped and document that the source shows the real port.
Two questions:
- Should
port_mappings()and the other public APIs stay TCP-only, with a separate UDP accessor? - Is the receive limitation acceptable?
Thanks for taking this. Your reading of the code is correct. Some notes on the plan:
- Protocol-keyed
PortMap: yes. It also fixes/proc/net/{tcp,udp}, which currently render from the same shared map. - Send-side translation: the send handlers (and connect) are only trapped when there's a destination policy or unix-connect supervision, and
sendtogoes straight to the kernel when there's no destination policy. With port remap alone, your translation would never run. Trap them whenport_remapis on, and send on the child's behalf whenever a loopback remap applies. Please also check that connected UDP gets remapped when port remap is the only thing enabled. - Receive: documenting it isn't enough as the end state. Resolvers like glibc's and dig check that a reply's source matches the address they queried, so an unrewritten
:40000source breaks the main use case. recvfrom/recvmsg are already trapped for netlink, so there's a hook. Keep the rewrite cheap: only inet sockets with a matching remap. Doing it in a follow-up PR is fine.
On your questions:
- API: no separate UDP accessor. We don't keep backward compatibility before 1.0, so change
port_mappings()to return mappings per protocol and update FFI, Python and Go together. - Receive limitation: OK for a first PR, but please plan the follow-up and include a test that checks the reply's source address.