Debian Codegraph @

vmcheck/vmcheck.go

raw symbols SCIP
1 // Copyright 2016-2017 VMware, Inc. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 //go:generate go run asm.go -out vmcheck_amd64.s -arch amd64
16 //go:generate go run asm.go -out vmcheck_386.s -arch 386
17
18 package vmcheck
19
20 import (
21 "encoding/binary"
22
23 "github.com/vmware/vmw-guestinfo/bdoor"
24 )
25
26 type platform struct {
27 cpuid func(uint32, uint32) (uint32, uint32, uint32, uint32)
28 accessPorts func() error
29 knock func() (bool, error)
30 }
31
32 var defaultPlatform = &platform{
33 cpuid: cpuid_low,
34 accessPorts: openPortsAccess,
35 knock: bdoorKnock,
36 }
37
38 // From https://github.com/intel-go/cpuid/blob/master/cpuidlow_amd64.s
39 // Get the CPU ID low level leaf values.
40 func cpuid_low(arg1, arg2 uint32) (eax, ebx, ecx, edx uint32)
41
42 func bdoorKnock() (bool, error) {
43 bp := &bdoor.BackdoorProto{}
44
45 bp.CX.AsUInt32().SetWord(bdoor.CommandGetVersion)
46 out := bp.InOut()
47 // if there is no device, we get back all 1s
48 return (0xffffffff != out.AX.AsUInt32().Word()) && (0 != out.AX.AsUInt32().Word()), nil
49 }
50
51 func (p *platform) isVirtualWorld(ignoreAccessErrors bool) (bool, error) {
52 // Test the HV bit is set
53 if !p.isVirtualCPU() {
54 return false, nil
55 }
56
57 // Test if backdoor port is available.
58 return p.hypervisorPortCheck(ignoreAccessErrors)
59 }
60
61 func (p *platform) isVirtualCPU() bool {
62 HV := uint32(1 << 31)
63 _, _, c, _ := p.cpuid(0x1, 0)
64 if (c & HV) != HV {
65 return false
66 }
67
68 _, b, c, d := p.cpuid(0x40000000, 0)
69
70 buf := make([]byte, 12)
71 binary.LittleEndian.PutUint32(buf, b)
72 binary.LittleEndian.PutUint32(buf[4:], c)
73 binary.LittleEndian.PutUint32(buf[8:], d)
74
75 if string(buf) != "VMwareVMware" {
76 return false
77 }
78
79 return true
80 }
81
82 // hypervisorPortCheck tests the availability of the backdoor port
83 // to the hypervisor, opportunistically tweaking I/O access level first.
84 func (p *platform) hypervisorPortCheck(ignoreAccessErrors bool) (bool, error) {
85 // Privilege level 3 to access all ports above 0x3ff
86 if err := p.accessPorts(); err != nil && !ignoreAccessErrors {
87 return false, err
88 }
89
90 return p.knock()
91 }
92
93 // IsVirtualCPU checks if the cpu is a virtual CPU running on ESX. It checks for
94 // the HV bit in the ECX register of the CPUID leaf 0x1. Intel and AMD CPUs
95 // reserve this bit to indicate if the CPU is running in a HV. See
96 // https://en.wikipedia.org/wiki/CPUID#EAX.3D1:_Processor_Info_and_Feature_Bits
97 // for details. If this bit is set, the reserved cpuid levels are used to pass
98 // information from the HV to the guest. In ESX, this is the repeating string
99 // "VMwareVMware".
100 func IsVirtualCPU() bool {
101 return defaultPlatform.isVirtualCPU()
102 }
103
104 // isVirtualWorld returns `true` if running in a VM and the backdoor is available.
105 // It also tries to elevate I/O privileges for the calling thread, which in
106 // some cases may be forbidden by the system (e.g Linux in `kernel_lockdown` mode
107 // does not allow `iopl` calls); the `ignoreAccessErrors` parameter allows
108 // to control library behavior in order to treat such errors as non-fatal.
109 func IsVirtualWorld(ignoreAccessErrors bool) (bool, error) {
110 return defaultPlatform.isVirtualWorld(ignoreAccessErrors)
111 }