//go:build linux && !android package permissions import ( "fmt" "os" "unsafe" "github.com/fosrl/newt/logger" "golang.org/x/sys/unix" ) const ( // TUN device constants tunDevice = "/dev/net/tun" ifnamsiz = 16 iffTun = 0x0001 iffNoPi = 0x1000 tunSetIff = 0x400454ca ) // ifReq is the structure for TUNSETIFF ioctl type ifReq struct { Name [ifnamsiz]byte Flags uint16 _ [22]byte // padding to match kernel structure } // CheckNativeInterfacePermissions checks if the process has sufficient // permissions to create a native TUN interface on Linux. // This requires either root privileges (UID 0) or CAP_NET_ADMIN capability. func CheckNativeInterfacePermissions() error { logger.Debug("Checking native interface permissions on Linux") // Check if running as root if os.Geteuid() == 0 { logger.Debug("Running as root, sufficient permissions for native TUN interface") return nil } // Check for CAP_NET_ADMIN capability caps := unix.CapUserHeader{ Version: unix.LINUX_CAPABILITY_VERSION_3, Pid: 0, // 0 means current process } var data [2]unix.CapUserData if err := unix.Capget(&caps, &data[0]); err != nil { logger.Debug("Failed to get capabilities: %v, will try creating test TUN", err) } else { // CAP_NET_ADMIN is capability bit 12 const CAP_NET_ADMIN = 12 if data[0].Effective&(1<