Skip to content

Commit 96e08d9

Browse files
committed
Add validation to network.vde[*].name fields
Signed-off-by: Jan Dubois <[email protected]>
1 parent 7457f68 commit 96e08d9

File tree

4 files changed

+28
-9
lines changed

4 files changed

+28
-9
lines changed

pkg/cidata/cidata.go

Lines changed: 2 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,7 @@ 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+
args.Networks = append(args.Networks, Network{MACAddress: qemuconst.SlirpMACAddress, Name: qemuconst.SlirpNICName})
7676
for _, vde := range y.Network.VDE {
7777
args.Networks = append(args.Networks, Network{MACAddress: vde.MACAddress, Name: vde.Name})
7878
}

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: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/AkihiroSuda/lima/pkg/downloader"
1313
"github.com/AkihiroSuda/lima/pkg/iso9660util"
1414
"github.com/AkihiroSuda/lima/pkg/limayaml"
15+
"github.com/AkihiroSuda/lima/pkg/qemu/qemuconst"
1516
"github.com/AkihiroSuda/lima/pkg/store/filenames"
1617
"github.com/docker/go-units"
1718
"github.com/mattn/go-shellwords"
@@ -131,10 +132,6 @@ func appendArgsIfNoConflict(args []string, k, v string) []string {
131132
return append(args, k, v)
132133
}
133134

134-
const (
135-
SlirpMACAddress = "22:11:11:11:11:11"
136-
)
137-
138135
func Cmdline(cfg Config) (string, []string, error) {
139136
y := cfg.LimaYAML
140137
exe, args, err := getExe(y.Arch)
@@ -200,10 +197,10 @@ func Cmdline(cfg Config) (string, []string, error) {
200197
// Network
201198
// CIDR is intentionally hardcoded to 192.168.5.0/24, as each of QEMU has its own independent slirp network.
202199
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)
200+
args = append(args, "-device", "virtio-net-pci,netdev=net0,mac="+qemuconst.SlirpMACAddress)
204201
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))
202+
args = append(args, "-netdev", fmt.Sprintf("vde,id=net%d,sock=%s", i+1, vde.URL))
203+
args = append(args, "-device", fmt.Sprintf("virtio-net-pci,netdev=net%d,mac=%s", i+1, vde.MACAddress))
207204
}
208205

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

pkg/qemu/qemuconst/qemuconst.go

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

0 commit comments

Comments
 (0)