sandlock icon indicating copy to clipboard operation
sandlock copied to clipboard

Support UDP port remapping

Open congwang-mk opened this issue 8 months ago • 2 comments

Currently sandlock only supports TCP for virtual port remapping.

congwang-mk avatar Mar 17 '26 20:03 congwang-mk

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:

  1. Split PortMap by protocol.
  2. Translate the destination port in the send handlers, after the policy check.
  3. 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?

brosier01 avatar Nov 02 '26 12:11 brosier01

Thanks for taking this. Your reading of the code is correct. Some notes on the plan:

  1. Protocol-keyed PortMap: yes. It also fixes /proc/net/{tcp,udp}, which currently render from the same shared map.
  2. Send-side translation: the send handlers (and connect) are only trapped when there's a destination policy or unix-connect supervision, and sendto goes straight to the kernel when there's no destination policy. With port remap alone, your translation would never run. Trap them when port_remap is 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.
  3. 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 :40000 source 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.

congwang-mk avatar Nov 03 '26 00:11 congwang-mk