github.com/metacubex/gvisor@v0.0.0-20240320004321-933faba989ec/pkg/sentry/socket/hostinet/stack_unsafe.go (about) 1 // Copyright 2021 The gVisor Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package hostinet 16 17 import ( 18 "runtime" 19 "unsafe" 20 21 "golang.org/x/sys/unix" 22 "github.com/metacubex/gvisor/pkg/abi/linux" 23 "github.com/metacubex/gvisor/pkg/hostarch" 24 "github.com/metacubex/gvisor/pkg/sentry/inet" 25 ) 26 27 func queryInterfaceFeatures(interfaces map[int32]inet.Interface) error { 28 fd, err := queryFD() 29 if err != nil { 30 return err 31 } 32 defer unix.Close(fd) 33 34 for idx, nic := range interfaces { 35 var ifr linux.IFReq 36 copy(ifr.IFName[:], nic.Name) 37 var gfeatures linux.EthtoolGFeatures 38 // Each feature block is sufficient to query 32 features, the linux 39 // kernel today supports upto 64 features per device. Technically it 40 // can support more in the future but this is sufficient for our use 41 // right now. 42 const ( 43 numFeatureBlocks = 2 44 ifrDataSz = unsafe.Sizeof(linux.EthtoolGFeatures{}) + numFeatureBlocks*unsafe.Sizeof(linux.EthtoolGetFeaturesBlock{}) 45 ) 46 featureBlocks := make([]linux.EthtoolGetFeaturesBlock, numFeatureBlocks) 47 b := make([]byte, ifrDataSz) 48 gfeatures.Cmd = uint32(linux.ETHTOOL_GFEATURES) 49 gfeatures.Size = numFeatureBlocks 50 gfeatures.MarshalBytes(b) 51 next := b[unsafe.Sizeof(linux.EthtoolGFeatures{}):] 52 for i := 0; i < numFeatureBlocks; i++ { 53 featureBlocks[i].MarshalBytes(next) 54 next = next[unsafe.Sizeof(linux.EthtoolGetFeaturesBlock{}):] 55 } 56 57 // Technically the next two lines are not safe as Go GC can technically move 58 // b to a new location and the pointer value stored in ifr.Data could point 59 // to random memory. But the reality today is that Go GC is not a moving GC 60 // so this is essentially safe as of today. 61 // 62 // TODO(b/209014118): Use Pin API when available in Go runtime to make this 63 // safe. 64 dataPtr := unsafe.Pointer(&b[0]) 65 hostarch.ByteOrder.PutUint64(ifr.Data[:8], uint64(uintptr(dataPtr))) 66 67 if _, _, errno := unix.Syscall(unix.SYS_IOCTL, uintptr(fd), unix.SIOCETHTOOL, uintptr(unsafe.Pointer(&ifr))); errno != 0 { 68 return errno 69 } 70 71 // Unmarshall the features back. 72 gfeatures.UnmarshalBytes(b) 73 next = b[unsafe.Sizeof(linux.EthtoolGFeatures{}):] 74 for i := 0; i < int(gfeatures.Size); i++ { 75 featureBlocks[i].UnmarshalBytes(next) 76 next = next[unsafe.Sizeof(linux.EthtoolGetFeaturesBlock{}):] 77 } 78 // Store the queried features. 79 iface := interfaces[idx] 80 iface.Features = make([]linux.EthtoolGetFeaturesBlock, gfeatures.Size) 81 copy(iface.Features, featureBlocks) 82 interfaces[idx] = iface 83 84 // This ensures b is not garbage collected before this point to ensure that 85 // the slice is not collected before the syscall returns and we copy out the 86 // data. 87 runtime.KeepAlive(b) 88 } 89 return nil 90 } 91 92 func queryFD() (int, error) { 93 // Try both AF_INET and AF_INET6 in case only one is supported. 94 var fd int 95 var err error 96 for _, family := range []int{unix.AF_INET6, unix.AF_INET} { 97 fd, err = unix.Socket(family, unix.SOCK_STREAM, 0) 98 if err == nil { 99 return fd, err 100 } 101 } 102 return fd, err 103 }