Skip to content

Commit

Permalink
virtcontainers: update context id of vsock to uint64
Browse files Browse the repository at this point in the history
The CID of VSock needs to be change to uint64. Otherwise that leads to
an endianess issue. For more details see
kata-containers#947

Fixes: kata-containers#958

Signed-off-by: Alice Frosi <afrosi@de.ibm.com>
  • Loading branch information
Alice Frosi committed Dec 4, 2018
1 parent e537d37 commit c63e16f
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 9 deletions.
2 changes: 1 addition & 1 deletion virtcontainers/kata_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ type KataAgentConfig struct {
}

type kataVSOCK struct {
contextID uint32
contextID uint64
port uint32
vhostFd *os.File
}
Expand Down
2 changes: 1 addition & 1 deletion virtcontainers/qemu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ func TestQemuAddDeviceSerialPortDev(t *testing.T) {
}

func TestQemuAddDeviceKataVSOCK(t *testing.T) {
contextID := uint32(3)
contextID := uint64(3)
port := uint32(1024)
vHostFD := os.NewFile(1, "vsock")

Expand Down
15 changes: 9 additions & 6 deletions virtcontainers/utils/utils_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ const ioctlVhostVsockSetGuestCid = 0x4008AF60

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 {
if _, _, errno := unix.Syscall(
Expand Down Expand Up @@ -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
// 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.
var firstContextID uint32 = 0x3
var firstContextID uint64 = 0x3
var contextID = firstContextID

// Generate a random number
n, err := rand.Int(rand.Reader, big.NewInt(int64(maxUInt)))
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.
Expand All @@ -72,14 +75,14 @@ func FindContextID() (*os.File, uint32, error) {

// Looking for the first available context ID.
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
}
}

// Last chance to get a free context ID.
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
}
}
Expand Down
2 changes: 1 addition & 1 deletion virtcontainers/utils/utils_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestFindContextID(t *testing.T) {
maxUInt = orgMaxUInt
}()
VHostVSockDevicePath = "/dev/null"
maxUInt = uint32(1000000)
maxUInt = uint64(1000000)

f, cid, err := FindContextID()
assert.Nil(f)
Expand Down

0 comments on commit c63e16f

Please sign in to comment.