package cloudinit import ( "crypto/rand" "encoding/base64" "encoding/binary" "encoding/hex" "encoding/json" "errors" "fmt" "net" "net/netip" "regexp" "slices" "strings" "unicode/utf8" "git.luoli.live/root/virt/agent/publicnetwork" "github.com/GehirnInc/crypt/sha512_crypt" "github.com/bgrewell/iso-kit/pkg/iso9660" ) const ( StartupScriptPath = "/var/lib/virt-cloud/startup.sh" StartupRunnerPath = "/var/lib/virt-cloud/startup-runner.sh" // GuestIdentityMetadata records which layer owns restored guest identity. GuestIdentityMetadata = "virt-cloud-guest-identity" // GuestIdentityDisk keeps credentials authoritative on the restored disk. GuestIdentityDisk = "disk" maxSeedBytes = 2 << 20 ) // StartupRunner deliberately records the attempt before starting the // administrator-provided script. Settings changes may rotate NoCloud's // instance identity, but success, failure, interruption, and reboot must all // remain at-most-once. const StartupRunner = `#!/bin/sh umask 077 state=/var/lib/virt-cloud/startup mkdir -p "$state" || exit 1 if ! mkdir "$state/started" 2>/dev/null; then [ -d "$state/started" ] && exit 0 exit 1 fi /bin/bash /var/lib/virt-cloud/startup.sh > "$state/output.log" 2>&1 result=$? printf '%s\n' "$result" > "$state/exit-code" exit "$result" ` var ( vmNamePattern = regexp.MustCompile(`^[A-Za-z0-9][A-Za-z0-9._-]{0,62}$`) hostLabel = regexp.MustCompile(`^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?$`) passwordHashRE = regexp.MustCompile(`^\$6\$rounds=100000\$[./0-9A-Za-z]{16}\$[./0-9A-Za-z]{86}$`) reservedIPv4Net = mustPrefixes( "0.0.0.0/8", "10.0.0.0/8", "100.64.0.0/10", "127.0.0.0/8", "169.254.0.0/16", "172.16.0.0/12", "192.0.0.0/24", "192.0.2.0/24", "192.168.0.0/16", "198.18.0.0/15", "198.51.100.0/24", "203.0.113.0/24", "224.0.0.0/4", "240.0.0.0/4", ) ) type Network struct { MAC string `json:"mac"` Public publicnetwork.Intent `json:"public_network"` InterfaceKey string `json:"interface_key,omitempty"` Private *PrivateNetwork `json:"private,omitempty"` } // PrivateNetwork is an optional routed VPC interface. The guest receives a // /32 address and one on-link route through the VPC gateway, so it never // assumes that the regional VPC is one shared broadcast domain. type PrivateNetwork struct { MAC string `json:"mac"` PrivateIP string `json:"private_ip"` CIDR string `json:"cidr"` Gateway string `json:"gateway"` InterfaceKey string `json:"interface_key,omitempty"` } type Request struct { UUID string Name string Hostname string GuestSSHPublicKey string RootPassword string StartupScript string Debian12NetworkFix bool // PreserveGuestCredentials is used only when restoring an existing system // disk. The new NoCloud seed must replace host/network identity without // overwriting the credentials already stored inside that disk. PreserveGuestCredentials bool Network Network } type Files struct { UserData []byte MetaData []byte NetworkConfig []byte } type writeFile struct { Path string `json:"path"` Content string `json:"content"` Owner string `json:"owner,omitempty"` Permissions string `json:"permissions"` } type rootUser struct { Name string `json:"name"` LockPasswd bool `json:"lock_passwd"` Shell string `json:"shell"` SSHAuthorizedKeys []string `json:"ssh_authorized_keys,omitempty"` HashedPassword string `json:"hashed_passwd,omitempty"` } // BuildFiles returns the exact three NoCloud files. Sensitive inputs are // consumed in memory: plaintext passwords are replaced with SHA-512 crypt // hashes and callers must not persist Request. func BuildFiles(request Request) (Files, error) { if err := validateRequest(request); err != nil { return Files{}, err } userData := map[string]any{ "updates": map[string]any{ "network": map[string]any{"when": []string{"boot"}}, }, } if request.PreserveGuestCredentials { // A restored system disk owns both its login credentials and its current // hostname. Supplying local-hostname here would allow cloud-init to // overwrite a hostname the guest administrator changed after deployment. userData["preserve_hostname"] = true } files := make([]writeFile, 0, 3) if !request.PreserveGuestCredentials { user := rootUser{Name: "root", Shell: "/bin/bash", LockPasswd: request.RootPassword == ""} if request.GuestSSHPublicKey != "" { user.SSHAuthorizedKeys = []string{request.GuestSSHPublicKey} } if request.RootPassword != "" { hash, err := hashPassword(request.RootPassword) if err != nil { return Files{}, err } user.HashedPassword = hash } passwordEnabled := request.RootPassword != "" sshd := "PermitRootLogin prohibit-password\nPasswordAuthentication no\nKbdInteractiveAuthentication no\n" if passwordEnabled { sshd = "PermitRootLogin yes\nPasswordAuthentication yes\nKbdInteractiveAuthentication no\n" } files = append(files, writeFile{ Path: "/etc/ssh/sshd_config.d/00-virt-cloud-root.conf", Content: sshd, Permissions: "0644", }) userData["users"] = []rootUser{user} userData["ssh_pwauth"] = passwordEnabled userData["disable_root"] = false if passwordEnabled { userData["chpasswd"] = map[string]any{ "expire": false, "users": []map[string]string{{ "name": "root", "password": user.HashedPassword, "type": "hash", }}, } } } if request.StartupScript != "" { files = append(files, writeFile{Path: StartupScriptPath, Content: request.StartupScript, Owner: "root:root", Permissions: "0600"}, writeFile{Path: StartupRunnerPath, Content: StartupRunner, Owner: "root:root", Permissions: "0700"}, ) userData["write_files"] = files userData["runcmd"] = [][]string{{"/bin/sh", StartupRunnerPath}} } else if len(files) != 0 { userData["write_files"] = files } if request.Debian12NetworkFix { userData["bootcmd"] = [][]string{{"/bin/sh", "-ec", strings.Join([]string{ "systemd-machine-id-setup", "netplan generate", "systemctl unmask systemd-networkd.service", "systemctl enable systemd-networkd.service", "systemctl start --no-block systemd-networkd.service", }, "\n")}} } metadata := map[string]any{ "instance-id": request.UUID, "virt-cloud-network-policy": "boot-v1", } if request.PreserveGuestCredentials { metadata[GuestIdentityMetadata] = GuestIdentityDisk } else { hostname := request.Hostname if hostname == "" { hostname = defaultHostname(request.Name) } metadata["local-hostname"] = hostname } if request.Debian12NetworkFix { metadata["dsmode"] = "local" } interfaceKey := request.Network.InterfaceKey if interfaceKey == "" { interfaceKey = "public0" } linkLocalCIDR, err := publicnetwork.LinkLocalIPv6CIDR(request.Network.MAC) if err != nil { return Files{}, err } ethernets := map[string]any{ interfaceKey: map[string]any{ "match": map[string]string{"macaddress": strings.ToLower(request.Network.MAC)}, "dhcp4": false, "dhcp6": false, "accept-ra": false, "link-local": []string{}, "addresses": []string{ request.Network.Public.IPv4.AddressCIDR, request.Network.Public.IPv6.AddressCIDR, linkLocalCIDR, }, "routes": []map[string]any{ publicDefaultRoute("0.0.0.0/0", request.Network.Public.IPv4), publicDefaultRoute("::/0", request.Network.Public.IPv6), }, "nameservers": map[string]any{"addresses": request.Network.Public.DNSServers}, }, } if private := request.Network.Private; private != nil { privateKey := private.InterfaceKey if privateKey == "" { privateKey = "private0" } ethernets[privateKey] = map[string]any{ "match": map[string]string{"macaddress": strings.ToLower(private.MAC)}, "dhcp4": false, "dhcp6": false, "accept-ra": false, "addresses": []string{private.PrivateIP + "/32"}, "routes": []map[string]any{{ "to": private.CIDR, "via": private.Gateway, "on-link": true, }}, } } networkConfig := map[string]any{ "version": 2, "ethernets": ethernets, } if request.Debian12NetworkFix { networkConfig["renderer"] = "networkd" } encodedUser, err := json.Marshal(userData) if err != nil { return Files{}, fmt.Errorf("encode NoCloud user-data: %w", err) } encodedMeta, err := json.Marshal(metadata) if err != nil { return Files{}, fmt.Errorf("encode NoCloud meta-data: %w", err) } encodedNetwork, err := json.Marshal(networkConfig) if err != nil { return Files{}, fmt.Errorf("encode NoCloud network-config: %w", err) } return Files{ UserData: append(append([]byte("#cloud-config\n"), encodedUser...), '\n'), MetaData: append(encodedMeta, '\n'), NetworkConfig: append(encodedNetwork, '\n'), }, nil } func BuildISO(request Request) ([]byte, error) { files, err := BuildFiles(request) if err != nil { return nil, err } image, err := iso9660.Create("CIDATA") if err != nil { return nil, fmt.Errorf("create NoCloud ISO: %w", err) } for name, content := range map[string][]byte{ "user-data": files.UserData, "meta-data": files.MetaData, "network-config": files.NetworkConfig, } { if err := image.AddFile(name, content); err != nil { return nil, fmt.Errorf("add NoCloud %s: %w", name, err) } } output := &boundedWriterAt{limit: maxSeedBytes} if err := image.Save(output); err != nil { return nil, fmt.Errorf("write NoCloud ISO: %w", err) } if len(output.data) == 0 || len(output.data) > maxSeedBytes { return nil, errors.New("NoCloud ISO size is invalid") } return output.data, nil } func validateRequest(request Request) error { if _, err := parseUUID(request.UUID); err != nil { return err } if !vmNamePattern.MatchString(request.Name) { return errors.New("VM name is invalid") } if request.Hostname != "" { if err := validateHostname(request.Hostname); err != nil { return err } } if err := validateSSHKey(request.GuestSSHPublicKey); err != nil { return err } if !request.PreserveGuestCredentials && request.RootPassword == "" && request.GuestSSHPublicKey == "" { return errors.New("root password or SSH public key is required") } if request.PreserveGuestCredentials && (request.RootPassword != "" || request.GuestSSHPublicKey != "" || request.StartupScript != "") { return errors.New("restored guest credentials and startup script must be preserved") } if request.RootPassword != "" { if err := validatePassword(request.RootPassword); err != nil { return err } } if !utf8.ValidString(request.StartupScript) || strings.ContainsRune(request.StartupScript, 0) || len(request.StartupScript) > 32<<10 { return errors.New("startup script must be valid NUL-free UTF-8 up to 32768 bytes") } return validateNetwork(request.Network) } func validateNetwork(value Network) error { hardware, err := net.ParseMAC(value.MAC) if err != nil || len(hardware) != 6 || hardware[0]&1 != 0 || allZero(hardware) { return errors.New("public interface MAC is invalid") } canonical, err := value.Public.Canonical() if err != nil { return fmt.Errorf("public network is invalid: %w", err) } if canonical.Contract != value.Public.Contract || canonical.NetworkSelector != value.Public.NetworkSelector || canonical.IPv4 != value.Public.IPv4 || canonical.IPv6 != value.Public.IPv6 || !slices.Equal(canonical.DNSServers, value.Public.DNSServers) { return errors.New("public network must use its canonical representation") } if value.InterfaceKey != "" && !regexp.MustCompile(`^[a-z][a-z0-9_-]{0,31}$`).MatchString(value.InterfaceKey) { return errors.New("interface key is invalid") } if value.Private != nil { if err := validatePrivateNetwork(*value.Private, strings.ToLower(value.MAC), value.InterfaceKey); err != nil { return err } } return nil } func publicDefaultRoute(destination string, address publicnetwork.AddressIntent) map[string]any { route := map[string]any{"to": destination, "via": address.Gateway} if address.GatewayOnLink { route["on-link"] = true } return route } func validatePrivateNetwork(value PrivateNetwork, publicMAC, publicKey string) error { hardware, err := net.ParseMAC(value.MAC) if err != nil || len(hardware) != 6 || hardware[0]&1 != 0 || allZero(hardware) || strings.EqualFold(value.MAC, publicMAC) { return errors.New("private interface MAC is invalid or conflicts with the public interface") } prefix, err := netip.ParsePrefix(value.CIDR) if err != nil || !prefix.Addr().Is4() || !prefix.Addr().IsPrivate() || prefix != prefix.Masked() || prefix.Bits() < 16 || prefix.Bits() > 28 { return errors.New("private cidr must be a canonical RFC1918 IPv4 /16 through /28 prefix") } address, addressErr := netip.ParseAddr(value.PrivateIP) gateway, gatewayErr := netip.ParseAddr(value.Gateway) if addressErr != nil || !address.Is4() || !prefix.Contains(address) || address == prefix.Addr() || address == lastAddress(prefix) || gatewayErr != nil || gateway != prefix.Addr().Next() || address == gateway { return errors.New("private address or gateway is invalid") } privateKey := value.InterfaceKey if privateKey == "" { privateKey = "private0" } if publicKey == "" { publicKey = "public0" } if !regexp.MustCompile(`^[a-z][a-z0-9_-]{0,31}$`).MatchString(privateKey) || privateKey == publicKey { return errors.New("private interface key is invalid or conflicts with the public interface") } return nil } func validateHostname(value string) error { if value != strings.ToLower(value) || len(value) < 1 || len(value) > 253 || strings.HasSuffix(value, ".") { return errors.New("hostname is invalid") } for _, label := range strings.Split(value, ".") { if !hostLabel.MatchString(label) { return errors.New("hostname is invalid") } } return nil } func defaultHostname(name string) string { value := strings.Trim(strings.Map(func(r rune) rune { if r >= 'a' && r <= 'z' || r >= '0' && r <= '9' || r == '-' { return r } if r >= 'A' && r <= 'Z' { return r + ('a' - 'A') } return '-' }, name), "-") if len(value) > 63 { value = value[:63] } return value } func validatePassword(value string) error { if len(value) < 12 || len(value) > 128 { return errors.New("root password must contain 12-128 printable ASCII characters") } var upper, lower, digit, symbol bool for _, char := range []byte(value) { if char < 0x21 || char > 0x7e { return errors.New("root password must contain 12-128 printable ASCII characters") } switch { case char >= 'A' && char <= 'Z': upper = true case char >= 'a' && char <= 'z': lower = true case char >= '0' && char <= '9': digit = true default: symbol = true } } if !upper || !lower || !digit || !symbol { return errors.New("root password must contain upper, lower, digit, and symbol characters") } return nil } func hashPassword(password string) (string, error) { saltBytes := make([]byte, 8) if _, err := rand.Read(saltBytes); err != nil { return "", fmt.Errorf("generate password salt: %w", err) } // SHA-crypt accepts only the crypt base64 alphabet. Hex is a uniformly // generated subset of that alphabet and always produces exactly 16 chars. salt := hex.EncodeToString(saltBytes) result, err := sha512_crypt.New().Generate([]byte(password), []byte("$6$rounds=100000$"+salt+"$")) if err != nil { return "", fmt.Errorf("hash root password: %w", err) } if !passwordHashRE.MatchString(result) { return "", errors.New("generated root password hash is invalid") } return result, nil } func validateSSHKey(value string) error { if value == "" { return nil } if value != strings.TrimSpace(value) || len(value) > 16384 || strings.ContainsAny(value, "\r\n\x00") { return errors.New("SSH public key must be one bounded OpenSSH line") } fields := strings.Fields(value) if len(fields) < 2 || len(fields) > 3 { return errors.New("SSH public key is invalid") } allowed := map[string]bool{ "ssh-ed25519": true, "ssh-rsa": true, "ecdsa-sha2-nistp256": true, "ecdsa-sha2-nistp384": true, "ecdsa-sha2-nistp521": true, } if !allowed[fields[0]] { return errors.New("SSH public key type is unsupported") } blob, err := base64.StdEncoding.DecodeString(fields[1]) if err != nil || len(blob) < 8 || len(blob) > 8192 { return errors.New("SSH public key payload is invalid") } embedded, rest, ok := sshString(blob) if !ok || string(embedded) != fields[0] || len(rest) == 0 { return errors.New("SSH public key type does not match its payload") } return nil } func sshString(value []byte) ([]byte, []byte, bool) { if len(value) < 4 { return nil, nil, false } size := int(binary.BigEndian.Uint32(value[:4])) if size < 1 || size > 8192 || 4+size > len(value) { return nil, nil, false } return value[4 : 4+size], value[4+size:], true } func parseUUID(value string) ([16]byte, error) { var result [16]byte if len(value) != 36 || value[8] != '-' || value[13] != '-' || value[18] != '-' || value[23] != '-' { return result, errors.New("UUID is invalid") } compact := strings.ReplaceAll(value, "-", "") decoded, err := hex.DecodeString(compact) if err != nil || len(decoded) != len(result) { return result, errors.New("UUID is invalid") } copy(result[:], decoded) if result[6]>>4 < 1 || result[6]>>4 > 5 || result[8]>>6 != 2 { return [16]byte{}, errors.New("UUID is invalid") } return result, nil } func isPublicIPv4(address netip.Addr) bool { if !address.Is4() || !address.IsGlobalUnicast() { return false } for _, prefix := range reservedIPv4Net { if prefix.Contains(address) { return false } } return true } func mustPrefixes(values ...string) []netip.Prefix { result := make([]netip.Prefix, 0, len(values)) for _, value := range values { result = append(result, netip.MustParsePrefix(value)) } return result } func lastAddress(prefix netip.Prefix) netip.Addr { bytes := prefix.Addr().As4() value := binary.BigEndian.Uint32(bytes[:]) value |= ^uint32(0) >> prefix.Bits() var result [4]byte binary.BigEndian.PutUint32(result[:], value) return netip.AddrFrom4(result) } func allZero(value []byte) bool { for _, item := range value { if item != 0 { return false } } return true } type boundedWriterAt struct { data []byte limit int } func (w *boundedWriterAt) WriteAt(value []byte, offset int64) (int, error) { if offset < 0 || offset > int64(w.limit) || len(value) > w.limit-int(offset) { return 0, errors.New("NoCloud ISO exceeds size limit") } end := int(offset) + len(value) if end > len(w.data) { w.data = append(w.data, make([]byte, end-len(w.data))...) } copy(w.data[int(offset):end], value) return len(value), nil }