7
7
"github.com/klauspost/cpuid"
8
8
"io/ioutil"
9
9
"os"
10
+ "os/user"
10
11
"time"
11
12
)
12
13
@@ -121,14 +122,50 @@ func checkSysInfo() bool {
121
122
return DoesFileContain (file , "VM00" )
122
123
}
123
124
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
+
124
156
/*
125
157
Public function returning true if a VM is detected.
126
158
If so, a non-empty string is also returned to tell how it was detected.
127
159
*/
128
160
func IsRunningInVirtualMachine () (bool , string ) {
129
161
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/
130
167
if cpuid .CPU .VM () {
131
- return true , "CPU Vendor (assembly instructions )"
168
+ return true , "CPU Vendor (cpuid space )"
132
169
}
133
170
134
171
if checkUML () {
@@ -147,5 +184,17 @@ func IsRunningInVirtualMachine() (bool, string) {
147
184
return true , "Kernel Ring Buffer (/dev/kmsg)"
148
185
}
149
186
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
+
150
199
return false , "nothing"
151
200
}
0 commit comments