I'm trying to write a tun/tap program in Rust. Since I don't want it to run as root I've added CAP_NET_ADMIN to the binary's capabilities:

$sudo setcap cap_net_admin=eip target/release/tunnel
$getcap target/release/tunnel
target/release/tunnel = cap_net_admin+eip

However, this is not working. Everything I've read says that this is the only capability required to create tuns, but the program gets an EPERM on the ioctl. In strace, I see this error:

openat(AT_FDCWD, "/dev/net/tun", O_RDWR|O_CLOEXEC) = 3
fcntl(3, F_GETFD)                       = 0x1 (flags FD_CLOEXEC)
ioctl(3, TUNSETIFF, 0x7ffcdac7c7c0)     = -1 EPERM (Operation not permitted)

I've verified that the binary runs successfully with full root permissions, but I don't want this to require sudo to run. Why is CAP_NET_ADMIN not sufficient here?

For reference, I'm on Linux version 4.15.0-45 there are only a few ways I see that this ioctl can return EPERM in the kernel (https://elixir.bootlin.com/linux/v4.15/source/drivers/net/tun.c#L2194) and at least one of them seems to be satisfied. I'm not sure how to probe the others:

if (!capable(CAP_NET_ADMIN))
    return -EPERM;
if (tun_not_capable(tun))
    return -EPERM;
if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
    return -EPERM;
  • if that's not it, please complete your question with the exact parameters you're passing to ioctl(TUNSETIFF) via the ifreq struct. – mosvy Mar 17 at 7:13

I guess that the filesystem your target/release/tunnel binary resides on is mounted with the nosuid option. That will also affect file capabilities, not just the setuid bit.

Also, you won't be able to strace a set-capabilities or setuid binary as you do -- the kernel will ignore the file capabilities when calling execve() from a ptraced process:

$ getcap tapy
tapy = cap_net_admin+eip
$ ./tapy
tapy: {tap1}
$ strace -e trace=ioctl ./tapy
ioctl(3, TUNSETIFF, 0x7ffdc5b2fef0)     = -1 EPERM (Operation not permitted)
tapy: ioctl TUNSETIFF: Operation not permitted
+++ exited with 1 +++

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.