@@ -22,7 +22,7 @@ const (
22
22
23
23
// AddVSockRegistryKey makes a vsock server running on the host acceessible in guests.
24
24
func AddVSockRegistryKey (port int ) error {
25
- rootKey , err := getGuestCommunicationServicesKey ()
25
+ rootKey , err := getGuestCommunicationServicesKey (true )
26
26
if err != nil {
27
27
return err
28
28
}
@@ -58,7 +58,7 @@ func AddVSockRegistryKey(port int) error {
58
58
59
59
// RemoveVSockRegistryKey removes entries created by AddVSockRegistryKey.
60
60
func RemoveVSockRegistryKey (port int ) error {
61
- rootKey , err := getGuestCommunicationServicesKey ()
61
+ rootKey , err := getGuestCommunicationServicesKey (true )
62
62
if err != nil {
63
63
return err
64
64
}
@@ -79,7 +79,7 @@ func RemoveVSockRegistryKey(port int) error {
79
79
80
80
// IsVSockPortFree determines if a VSock port has been registiered already.
81
81
func IsVSockPortFree (port int ) (bool , error ) {
82
- rootKey , err := getGuestCommunicationServicesKey ()
82
+ rootKey , err := getGuestCommunicationServicesKey (false )
83
83
if err != nil {
84
84
return false , err
85
85
}
@@ -148,7 +148,7 @@ func GetDistroID(name string) (string, error) {
148
148
149
149
// GetRandomFreeVSockPort gets a list of all registered VSock ports and returns a non-registered port.
150
150
func GetRandomFreeVSockPort (min , max int ) (int , error ) {
151
- rootKey , err := getGuestCommunicationServicesKey ()
151
+ rootKey , err := getGuestCommunicationServicesKey (false )
152
152
if err != nil {
153
153
return 0 , err
154
154
}
@@ -186,11 +186,19 @@ func GetRandomFreeVSockPort(min, max int) (int, error) {
186
186
return tree [0 ].offset + v , nil
187
187
}
188
188
189
- func getGuestCommunicationServicesKey () (registry.Key , error ) {
189
+ // getGuestCommunicationServicesKey returns the HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Virtualization\GuestCommunicationServices
190
+ // registry key for use in other operations.
191
+ //
192
+ // allowWrite is configurable because setting it to true requires Administrator access.
193
+ func getGuestCommunicationServicesKey (allowWrite bool ) (registry.Key , error ) {
194
+ var registryPermissions uint32 = registry .READ
195
+ if allowWrite {
196
+ registryPermissions = registry .WRITE | registry .READ
197
+ }
190
198
rootKey , err := registry .OpenKey (
191
199
registry .LOCAL_MACHINE ,
192
200
guestCommunicationsPrefix ,
193
- registry . WRITE | registry . READ ,
201
+ registryPermissions ,
194
202
)
195
203
if err != nil {
196
204
return 0 , fmt .Errorf (
0 commit comments