77 "github.com/klauspost/cpuid"
88 "io/ioutil"
99 "os"
10+ "os/user"
1011 "time"
1112)
1213
@@ -121,14 +122,50 @@ func checkSysInfo() bool {
121122 return DoesFileContain (file , "VM00" )
122123}
123124
125+ /*
126+ Some virtualization technologies can be detected using /proc/device-tree
127+ */
128+ func checkDeviceTree () bool {
129+ deviceTreeBase := "/proc/device-tree"
130+
131+ if DoesFileExist (deviceTreeBase + "/hypervisor/compatible" ) {
132+ return true
133+ }
134+
135+ if DoesFileExist (deviceTreeBase + "/fw-cfg" ) {
136+ return true
137+ }
138+
139+ return false
140+ }
141+
142+ /*
143+ Some virtualization technologies can be detected using /proc/type
144+ */
145+ func checkHypervisorType () bool {
146+ return DoesFileExist ("/sys/hypervisor/type" )
147+ }
148+
149+ /*
150+ Xen can be detected thanks to /proc/xen
151+ */
152+ func checkXenProcFile () bool {
153+ return DoesFileExist ("/proc/xen" )
154+ }
155+
124156/*
125157Public function returning true if a VM is detected.
126158If so, a non-empty string is also returned to tell how it was detected.
127159*/
128160func IsRunningInVirtualMachine () (bool , string ) {
129161
162+ if currentUser , _ := user .Current (); currentUser != nil && currentUser .Uid != "0" {
163+ PrintWarning ("Unprivileged user detected, some techniques might not work" )
164+ }
165+
166+ // https://lwn.net/Articles/301888/
130167 if cpuid .CPU .VM () {
131- return true , "CPU Vendor (assembly instructions )"
168+ return true , "CPU Vendor (cpuid space )"
132169 }
133170
134171 if checkUML () {
@@ -147,5 +184,17 @@ func IsRunningInVirtualMachine() (bool, string) {
147184 return true , "Kernel Ring Buffer (/dev/kmsg)"
148185 }
149186
187+ if checkDeviceTree () {
188+ return true , "VM device tree (/proc/device-tree)"
189+ }
190+
191+ if checkHypervisorType () {
192+ return true , "Hypervisor type file (/sys/hypervisor/type)"
193+ }
194+
195+ if checkXenProcFile () {
196+ return true , "Xen proc file (/proc/xen)"
197+ }
198+
150199 return false , "nothing"
151200}
0 commit comments