SagerNet / SagerNet/sing-box

UDP packet bypassing routing

Open
#2,991 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Go
Stars
38.1k
Forks
4.6k
Avg merge
19d 15h
Merged PRs (30d)
1

Description

Packet connection (udp) only respect the first packet it received. For example if i set a rule to reject specific ip address or protocol it can be bypassed by sending a dummy packet to a valid address through UDP socket and then send a different packet to a different address through the same socket. Found out that bufio.copyPacketWaitWithPool blindly send any packets to any destination without applying routing rules.
if i set a breakpoint to the line inside bufio.copyPacketWaitWithPool with destinationConn.WritePacket(buffer, destination) i can clearly see that the sing-box itself is sending those packets. Also it's sad that it is not logged at all, even with trace level, the log contains only

INFO[0013] [3169077631 0ms] inbound/tun[tun-in]: inbound packet connection from 172.19.16.1:17852
INFO[0013] [3169077631 0ms] inbound/tun[tun-in]: inbound packet connection to 1.2.3.4:100
INFO[0013] [3169077631 0ms] outbound/direct[direct]: outbound packet connection

Example c# code to replicate, with having a remote udp listener on a 100.100.100.100:8585

using var udpClient = new UdpClient();
var legitEndpoint = new IPEndPoint(IPAddress.Parse("1.2.3.4"), 100);
var bannedEndpoint = new IPEndPoint(IPAddress.Parse("100.100.100.100"), 8585);
await udpClient.SendAsync(new byte[] { 100 }, fakeEndpoint);
await udpClient.SendAsync(new byte[] { 200 }, bannedEndpoint );
{
	"log": {
		"level": "trace"
	},
	"inbounds": [
		{
			"type": "tun",
			"tag": "tun-in",
			"interface_name": "tun0",
			"address": "172.19.16.1/30",
			"stack": "gvisor",
			"mtu": 9000,
			"auto_route": true
		}
	],
	"outbounds": [
		{
			"type": "direct",
			"tag": "direct"
		}
	],
	"route": {
		"final": "direct",
		"auto_detect_interface": true,
		"rules": [
			{
				"ip_cidr": "100.100.100.100/32",
				"action": "reject"
			}
		]
	}
}

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at bufio.copyPacketWaitWithPool and the destinationConn.WritePacket call, using the provided TUN configuration and C# UDP reproducer to trace the first and second packets. Check how routing rules are applied after a UDP socket is established and compare the result with the trace-level log. Done means the packet to 100.100.100.100 is rejected rather than sent, with behavior reflected in logs.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
networking
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.