Skip to content

Commit 48e44a2

Browse files
authored
Merge pull request #137 from rancher-sandbox/validate-networks
Add validation to network.vde[*].name fields
2 parents 7457f68 + 5f4d187 commit 48e44a2

File tree

5 files changed

+37
-16
lines changed

5 files changed

+37
-16
lines changed

pkg/cidata/cidata.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
"github.com/AkihiroSuda/lima/pkg/iso9660util"
1616
"github.com/AkihiroSuda/lima/pkg/limayaml"
1717
"github.com/AkihiroSuda/lima/pkg/localpathutil"
18-
"github.com/AkihiroSuda/lima/pkg/qemu"
18+
"github.com/AkihiroSuda/lima/pkg/qemu/qemuconst"
1919
"github.com/AkihiroSuda/lima/pkg/sshutil"
2020
"github.com/AkihiroSuda/lima/pkg/store/filenames"
2121
"github.com/opencontainers/go-digest"
@@ -72,7 +72,8 @@ func GenerateISO9660(instDir, name string, y *limayaml.LimaYAML) error {
7272
args.Mounts = append(args.Mounts, expanded)
7373
}
7474

75-
args.Networks = append(args.Networks, Network{MACAddress: qemu.SlirpMACAddress, Name: "eth0"})
75+
slirpMACAddress := limayaml.MACAddress(instDir)
76+
args.Networks = append(args.Networks, Network{MACAddress: slirpMACAddress, Name: qemuconst.SlirpNICName})
7677
for _, vde := range y.Network.VDE {
7778
args.Networks = append(args.Networks, Network{MACAddress: vde.MACAddress, Name: vde.Name})
7879
}

pkg/limayaml/defaults.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ import (
1010
"github.com/AkihiroSuda/lima/pkg/guestagent/api"
1111
)
1212

13+
func MACAddress(uniqueID string) string {
14+
// TODO: combine the uniqueID with the host machineID to create a globally unique hash
15+
sha := sha256.Sum256([]byte(uniqueID))
16+
// According to https://gitlab.com/wireshark/wireshark/-/blob/master/manuf
17+
// no well-known MAC addresses start with 0x22.
18+
hw := append(net.HardwareAddr{0x22}, sha[0:5]...)
19+
return hw.String()
20+
}
21+
1322
func FillDefault(y *LimaYAML, filePath string) {
1423
y.Arch = resolveArch(y.Arch)
1524
for i := range y.Images {
@@ -62,12 +71,7 @@ func FillDefault(y *LimaYAML, filePath string) {
6271
vde := &y.Network.VDE[i]
6372
if vde.MACAddress == "" {
6473
// every interface in every limayaml file must get its own unique MAC address
65-
uniqueID := fmt.Sprintf("%s#%d", filePath, i)
66-
sha := sha256.Sum256([]byte(uniqueID))
67-
// According to https://gitlab.com/wireshark/wireshark/-/blob/master/manuf
68-
// no well-known MAC addresses start with 0x22.
69-
hw := append(net.HardwareAddr{0x22}, sha[0:5]...)
70-
vde.MACAddress = hw.String()
74+
vde.MACAddress = MACAddress(fmt.Sprintf("%s#%d", filePath, i))
7175
}
7276
if vde.Name == "" {
7377
vde.Name = "vde" + strconv.Itoa(i)

pkg/limayaml/validate.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"strings"
1010

1111
"github.com/AkihiroSuda/lima/pkg/localpathutil"
12+
"github.com/AkihiroSuda/lima/pkg/qemu/qemuconst"
1213
"github.com/docker/go-units"
1314
"github.com/pkg/errors"
1415
)
@@ -159,6 +160,7 @@ func Validate(y LimaYAML) error {
159160
// Not validating that the various GuestPortRanges and HostPortRanges are not overlapping. Rules will be
160161
// processed sequentially and the first matching rule for a guest port determines forwarding behavior.
161162
}
163+
networkName := make(map[string]int)
162164
for i, vde := range y.Network.VDE {
163165
field := fmt.Sprintf("network.vde[%d]", i)
164166
if vde.URL == "" {
@@ -190,6 +192,20 @@ func Validate(y LimaYAML) error {
190192
return errors.Errorf("field `%s.macAddress` must be a 48 bit (6 bytes) MAC address; actual length of %q is %d bytes", field, vde.MACAddress, len(hw))
191193
}
192194
}
195+
// FillDefault() will make sure that vde.Name is not the empty string
196+
if len(vde.Name) >= 16 {
197+
return errors.Errorf("field `%s.be less than 16 bytes, but is %d bytes: %q", field, len(vde.Name), vde.Name)
198+
}
199+
if strings.ContainsAny(vde.Name, " \t\n/") {
200+
return errors.Errorf("field `%s.be must not contain whitespace or slashes", field)
201+
}
202+
if vde.Name == qemuconst.SlirpNICName {
203+
return errors.Errorf("field `%s.name` must not be set to %q because it is reserved for slirp", field, qemuconst.SlirpNICName)
204+
}
205+
if prev, ok := networkName[vde.Name]; ok {
206+
return errors.Errorf("field `%s.name` value %q has already been used by field `network.vde[%d].name`", field, vde.Name, prev)
207+
}
208+
networkName[vde.Name] = i
193209
}
194210
return nil
195211
}

pkg/qemu/qemu.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,6 @@ func appendArgsIfNoConflict(args []string, k, v string) []string {
130130
}
131131
return append(args, k, v)
132132
}
133-
134-
const (
135-
SlirpMACAddress = "22:11:11:11:11:11"
136-
)
137-
138133
func Cmdline(cfg Config) (string, []string, error) {
139134
y := cfg.LimaYAML
140135
exe, args, err := getExe(y.Arch)
@@ -200,10 +195,10 @@ func Cmdline(cfg Config) (string, []string, error) {
200195
// Network
201196
// CIDR is intentionally hardcoded to 192.168.5.0/24, as each of QEMU has its own independent slirp network.
202197
args = append(args, "-netdev", fmt.Sprintf("user,id=net0,net=192.168.5.0/24,hostfwd=tcp:127.0.0.1:%d-:22", y.SSH.LocalPort))
203-
args = append(args, "-device", "virtio-net-pci,netdev=net0,mac="+SlirpMACAddress)
198+
args = append(args, "-device", "virtio-net-pci,netdev=net0,mac="+limayaml.MACAddress(cfg.InstanceDir))
204199
for i, vde := range y.Network.VDE {
205-
args = append(args, "-netdev", fmt.Sprintf("vde,id=net%d,sock=%s", i+1, vde.URL))
206-
args = append(args, "-device", fmt.Sprintf("virtio-net-pci,netdev=net%d,mac=%s", i+1, vde.MACAddress))
200+
args = append(args, "-netdev", fmt.Sprintf("vde,id=net%d,sock=%s", i+1, vde.URL))
201+
args = append(args, "-device", fmt.Sprintf("virtio-net-pci,netdev=net%d,mac=%s", i+1, vde.MACAddress))
207202
}
208203

209204
// virtio-rng-pci accelerates starting up the OS, according to https://wiki.gentoo.org/wiki/QEMU/Options

pkg/qemu/qemuconst/qemuconst.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package qemuconst
2+
3+
const (
4+
SlirpNICName = "eth0"
5+
)

0 commit comments

Comments
 (0)