In my Packet Tunnel Provider, I'm setting the NEDNSSettings to localhost as I have a local DNS server listening on port 53 (this is a dns forwarder which conditionally forwards to different upstreams based on rules).
On iOS it works just fine, I'm able to listen on localhost:53 in the Network Extension, then set NEDNSSettings servers to "127.0.0.1".
However on macOS due to the port being under 1024, I get a Permission denied OS code 13 error. I'm assuming this is due to the Network Extension not running as root. Can this be changed?
This could be rectified if you could customize the port in NEDNSSettings, as the listener could be on port 5353, but it doesn't look like it is possible?
Just wondering if there is some other way to accomplish what I'm trying to do in the macOS Network Extension?
I'm assuming this is due to the Network Extension not running as root.
Yes and no.
macOS programs can bind to low-numbered ports without root privileges. Consider this:
let fd = try FileDescriptor.socket(AF_INET, SOCK_DGRAM, 0)
defer { try! fd.close() }
try fd.bind("0.0.0.0", 54)
IMPORTANT This is using my QSocket helpers; follow the link from Extra-ordinary Networking.
It runs just fine. I’m testing on macOS 14.6.1, but IIRC this restriction was lifted in macOS 10.14.
There are still restrictions though. For example:
let fd = try FileDescriptor.socket(AF_INET, SOCK_DGRAM, 0)
defer { try! fd.close() }
try fd.bind("127.0.0.1", 54)
This fails with EACCES
. I suspect that’s what you’re seeing.
Note I’m testing with port 54, rather than 53, because there are parts of the system that treat 53 specially. That doesn’t seem to be the case here. The above code works the same with port 53. However, I didn’t try putting this code into an NE provider (-:
Also, NE providers can run as root on macOS; you just have to package them as a system extension. See TN3134 Network Extension provider deployment for more about packaging and deployment.
In general, sysex packaging makes more sense on macOS because the networking stack is global to the whole computer. However, the appex packaging continues to exist for various reasons.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"