mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-07-06 12:06:49 +00:00
virtcontainers: update context id of vsock to uint64
The CID of VSock needs to be change to uint64. Otherwise that leads to an endianess issue. For more details see https://github.com/kata-containers/runtime/issues/947 Remove the uint64 introduced by #984 Fixes: #958 Signed-off-by: Alice Frosi <afrosi@de.ibm.com>
This commit is contained in:
parent
9dee04a314
commit
deb6f16d82
@ -73,7 +73,7 @@ type KataAgentConfig struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type kataVSOCK struct {
|
type kataVSOCK struct {
|
||||||
contextID uint32
|
contextID uint64
|
||||||
port uint32
|
port uint32
|
||||||
vhostFd *os.File
|
vhostFd *os.File
|
||||||
}
|
}
|
||||||
|
@ -422,7 +422,7 @@ func (q *qemuArchBase) appendVSockPCI(devices []govmmQemu.Device, vsock kataVSOC
|
|||||||
devices = append(devices,
|
devices = append(devices,
|
||||||
govmmQemu.VSOCKDevice{
|
govmmQemu.VSOCKDevice{
|
||||||
ID: fmt.Sprintf("vsock-%d", vsock.contextID),
|
ID: fmt.Sprintf("vsock-%d", vsock.contextID),
|
||||||
ContextID: uint64(vsock.contextID),
|
ContextID: vsock.contextID,
|
||||||
VHostFD: vsock.vhostFd,
|
VHostFD: vsock.vhostFd,
|
||||||
DisableModern: q.nestedRun,
|
DisableModern: q.nestedRun,
|
||||||
},
|
},
|
||||||
|
@ -249,14 +249,14 @@ func TestQemuAddDeviceSerialPortDev(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestQemuAddDeviceKataVSOCK(t *testing.T) {
|
func TestQemuAddDeviceKataVSOCK(t *testing.T) {
|
||||||
contextID := uint32(3)
|
contextID := uint64(3)
|
||||||
port := uint32(1024)
|
port := uint32(1024)
|
||||||
vHostFD := os.NewFile(1, "vsock")
|
vHostFD := os.NewFile(1, "vsock")
|
||||||
|
|
||||||
expectedOut := []govmmQemu.Device{
|
expectedOut := []govmmQemu.Device{
|
||||||
govmmQemu.VSOCKDevice{
|
govmmQemu.VSOCKDevice{
|
||||||
ID: fmt.Sprintf("vsock-%d", contextID),
|
ID: fmt.Sprintf("vsock-%d", contextID),
|
||||||
ContextID: uint64(contextID),
|
ContextID: contextID,
|
||||||
VHostFD: vHostFD,
|
VHostFD: vHostFD,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -22,7 +22,10 @@ const ioctlVhostVsockSetGuestCid = 0x4008AF60
|
|||||||
|
|
||||||
var ioctlFunc = ioctl
|
var ioctlFunc = ioctl
|
||||||
|
|
||||||
var maxUInt uint32 = 1<<32 - 1
|
// maxUInt represents the maximum valid value for the context ID.
|
||||||
|
// The upper 32 bits of the CID are reserved and zeroed.
|
||||||
|
// See http://stefanha.github.io/virtio/
|
||||||
|
var maxUInt uint64 = 1<<32 - 1
|
||||||
|
|
||||||
func ioctl(fd uintptr, request int, arg1 uint64) error {
|
func ioctl(fd uintptr, request int, arg1 uint64) error {
|
||||||
if _, _, errno := unix.Syscall(
|
if _, _, errno := unix.Syscall(
|
||||||
@ -51,15 +54,15 @@ func ioctl(fd uintptr, request int, arg1 uint64) error {
|
|||||||
// - Reduce the probability of a *DoS attack*, since other processes don't know whatis the initial context ID
|
// - Reduce the probability of a *DoS attack*, since other processes don't know whatis the initial context ID
|
||||||
// used by findContextID to find a context ID available
|
// used by findContextID to find a context ID available
|
||||||
//
|
//
|
||||||
func FindContextID() (*os.File, uint32, error) {
|
func FindContextID() (*os.File, uint64, error) {
|
||||||
// context IDs 0x0, 0x1 and 0x2 are reserved, 0x3 is the first context ID usable.
|
// context IDs 0x0, 0x1 and 0x2 are reserved, 0x3 is the first context ID usable.
|
||||||
var firstContextID uint32 = 0x3
|
var firstContextID uint64 = 0x3
|
||||||
var contextID = firstContextID
|
var contextID = firstContextID
|
||||||
|
|
||||||
// Generate a random number
|
// Generate a random number
|
||||||
n, err := rand.Int(rand.Reader, big.NewInt(int64(maxUInt)))
|
n, err := rand.Int(rand.Reader, big.NewInt(int64(maxUInt)))
|
||||||
if err == nil && n.Int64() >= int64(firstContextID) {
|
if err == nil && n.Int64() >= int64(firstContextID) {
|
||||||
contextID = uint32(n.Int64())
|
contextID = uint64(n.Int64())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Open vhost-vsock device to check what context ID is available.
|
// Open vhost-vsock device to check what context ID is available.
|
||||||
@ -72,14 +75,14 @@ func FindContextID() (*os.File, uint32, error) {
|
|||||||
|
|
||||||
// Looking for the first available context ID.
|
// Looking for the first available context ID.
|
||||||
for cid := contextID; cid <= maxUInt; cid++ {
|
for cid := contextID; cid <= maxUInt; cid++ {
|
||||||
if err := ioctlFunc(vsockFd.Fd(), ioctlVhostVsockSetGuestCid, uint64(cid)); err == nil {
|
if err := ioctlFunc(vsockFd.Fd(), ioctlVhostVsockSetGuestCid, cid); err == nil {
|
||||||
return vsockFd, cid, nil
|
return vsockFd, cid, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Last chance to get a free context ID.
|
// Last chance to get a free context ID.
|
||||||
for cid := contextID - 1; cid >= firstContextID; cid-- {
|
for cid := contextID - 1; cid >= firstContextID; cid-- {
|
||||||
if err := ioctlFunc(vsockFd.Fd(), ioctlVhostVsockSetGuestCid, uint64(cid)); err == nil {
|
if err := ioctlFunc(vsockFd.Fd(), ioctlVhostVsockSetGuestCid, cid); err == nil {
|
||||||
return vsockFd, cid, nil
|
return vsockFd, cid, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -26,7 +26,7 @@ func TestFindContextID(t *testing.T) {
|
|||||||
maxUInt = orgMaxUInt
|
maxUInt = orgMaxUInt
|
||||||
}()
|
}()
|
||||||
VHostVSockDevicePath = "/dev/null"
|
VHostVSockDevicePath = "/dev/null"
|
||||||
maxUInt = uint32(1000000)
|
maxUInt = uint64(1000000)
|
||||||
|
|
||||||
f, cid, err := FindContextID()
|
f, cid, err := FindContextID()
|
||||||
assert.Nil(f)
|
assert.Nil(f)
|
||||||
|
Loading…
Reference in New Issue
Block a user