Skip to content

Commit

Permalink
tuntap: use the owner / group parameters
Browse files Browse the repository at this point in the history
Set the tuntap owner / group via syscall on the opened file
descriptor.

Signed-off-by: Miguel Duarte Barroso <mdbarroso@redhat.com>
  • Loading branch information
maiqueb committed Aug 7, 2020
1 parent 98629f7 commit 2a7fe2c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
14 changes: 12 additions & 2 deletions link_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -1074,8 +1074,6 @@ func (h *Handle) linkModify(link Link, flags int) error {
}

if isTuntap {
// TODO: support user
// TODO: support group
if tuntap.Mode < unix.IFF_TUN || tuntap.Mode > unix.IFF_TAP {
return fmt.Errorf("Tuntap.Mode %v unknown", tuntap.Mode)
}
Expand Down Expand Up @@ -1121,6 +1119,18 @@ func (h *Handle) linkModify(link Link, flags int) error {
return fmt.Errorf("Tuntap IOCTL TUNSETIFF failed [%d], errno %v", i, errno)
}

_, _, errno = syscall.Syscall(syscall.SYS_IOCTL, uintptr(fd), syscall.TUNSETOWNER, uintptr(tuntap.Owner))
if errno != 0 {
cleanupFds(fds)
return fmt.Errorf("Tuntap IOCTL TUNSETOWNER failed [%d], errno %v", i, errno)
}

_, _, errno = syscall.Syscall(syscall.SYS_IOCTL, uintptr(fd), syscall.TUNSETGROUP, uintptr(tuntap.Group))
if errno != 0 {
cleanupFds(fds)
return fmt.Errorf("Tuntap IOCTL TUNSETGROUP failed [%d], errno %v", i, errno)
}

// Set the tun device to non-blocking before use. The below comment
// taken from:
//
Expand Down
22 changes: 22 additions & 0 deletions link_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2145,6 +2145,28 @@ func TestLinkAddDelTuntapMq(t *testing.T) {
Flags: TUNTAP_MULTI_QUEUE_DEFAULTS | TUNTAP_VNET_HDR})
}

func TestLinkAddDelTuntapOwnerGroup(t *testing.T) {
tearDown := setUpNetlinkTest(t)
defer tearDown()

if err := syscall.Mount("sysfs", "/sys", "sysfs", syscall.MS_RDONLY, ""); err != nil {
t.Fatal("Cannot mount sysfs")
}

defer func() {
if err := syscall.Unmount("/sys", 0); err != nil {
t.Fatal("Cannot umount /sys")
}
}()

testLinkAddDel(t, &Tuntap{
LinkAttrs: LinkAttrs{Name: "foo"},
Mode: TUNTAP_MODE_TAP,
Owner: 0,
Group: 0,
})
}

func TestVethPeerIndex(t *testing.T) {
tearDown := setUpNetlinkTest(t)
defer tearDown()
Expand Down

0 comments on commit 2a7fe2c

Please sign in to comment.