Commit Graph

152 Commits

Author SHA1 Message Date
Lennart Poettering
600e135a0b virt: detect "linux,dummy-virt" devicetree VMs
So apparently "linux,dummy-virt" is a devicetree in popular use by
various hypervisors, including crosvm:

e5d7a64d37/aarch64/src/fdt.rs (L692)

and qemu:

98c7362b1e/hw/arm/virt.c (L283)

and that's because the kernel ships support for that natively:

https://www.kernel.org/doc/Documentation/devicetree/bindings/arm/linux%2Cdummy-virt.yaml

It's explicitly for using in virtualization. Hence it's suitable for
detecting it as generic fallback.

This hence adds the check, similar to how we already look for one other
qemu-specific devicetree.

I ran into this while playing around with the new Pixel "Linux Terminal"
app from google which runs a Debian in a crosvm apparently. So far
systemd didn't recognize execution in it at all. Let's at least
recognize it as VM at all, even if this doesn't recognize it as
crosvm.
2025-03-07 15:18:08 +01:00
Lennart Poettering
4d53d76f5e virt: some modernizations
Reduce scope of certain allocations, add more debug logging, and improve
some log messages.
2025-03-07 15:11:09 +01:00
Yu Watanabe
e75372958d missing_threads.h: threads.h exists since glibc-2.28 2025-03-04 02:24:49 +09:00
Lennart Poettering
d6267b9b18 process-util: port pid_from_same_root_fs() to pidref, and port three places over to it 2025-01-16 11:55:21 +01:00
Mike Yuan
2f7cd6c70a namespace-util: correct assertion in namespace_is_init()
Follow-up for 18ead2b03d
2025-01-04 17:07:59 +01:00
Lennart Poettering
9948b4668c virt: drop userns detection heuristic
Now that we have an explicit userns check we can drop the heuristic for
it, given that it's kinda wrong (because mapping the full host UID range
into a userns is actually a thing people do).

Hence, just delete the code and only keep the userns inode check in
place.
2024-12-11 19:23:03 +01:00
Lennart Poettering
7f0a615ef8 virt: dont check for cgroupns anymore
Now that we have a reliable pidns check I don't think we really should
look for cgroupns anymore, it's too weak a check. I mean, if I myself
would implement a desktop app sandbox (like flatpak) I'd always enable
cgroupns, simply to hide the host cgroup hierarchy.

Hence drop the check.

I suggested adding this 4 years ago here:

https://github.com/systemd/systemd/pull/17902#issuecomment-745548306
2024-12-11 19:23:03 +01:00
Lennart Poettering
a2429f507c virt: make use of ns inode check in running_in_userns() and running_in_cgroupns() too 2024-11-23 00:14:20 +01:00
Luca Boccassi
193bf42ab0 detect-virt: check the inode number of the pid namespace
The indoe number of root pid namespace is hardcoded in the kernel to
0xEFFFFFFC since 3.8, so check the inode number of our pid namespace
if all else fails. If it's not 0xEFFFFFFC then we are in a pid
namespace, hence a container environment.

Fixes https://github.com/systemd/systemd/issues/35249

[Reworked by Lennart, to make use of namespace_is_init()]
2024-11-23 00:14:20 +01:00
Integral
ddb8a639d5 tree-wide: replace for loop with FOREACH_ELEMENT or FOREACH_ARRAY macros (#34893) 2024-10-26 07:10:22 +09:00
Daan De Meyer
2701c2f67d Add $SYSTEMD_IN_CHROOT to override chroot detection
When running unprivileged, checking /proc/1/root doesn't work because
it requires privileges. Instead, let's add an environment variable so
the process that chroot's can tell (systemd) subprocesses whether
they're running in a chroot or not.
2024-08-16 10:11:29 +02:00
Lennart Poettering
cea9216a42 detect-virt: detect hyperv-enlightened qemu as qemu, not as hyperv
CPUID reporting hyperv should be taken with a grain of salt, and we
should prefer other mechanisms then.

Fixes: #28001
2024-04-20 12:10:42 +02:00
Friedrich Altheide
5e3c08d375 basic/virt: Fix virtualbox detection on proprietary system via board_vendor
Identify an virtualbox instance even if product_name, sys_vendor and bios_vendor reflect the
information of the real hardware, by checking if board_vendor == "Oracle Corporation"

This fixes #13429 again
The previous fix was removed in #21127
2024-03-24 18:02:42 +00:00
Zbigniew Jędrzejewski-Szmek
2dc65c32b2 virt: wrap comment, add missing punctuation 2024-02-27 18:32:21 +01:00
Heinrich Schuchardt
5041e771a8 detect-virt: allow detection via SMBIOS on RISC-V
SMBIOS support in QEMU for RISC-V is merged upstream.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
2024-02-23 17:29:20 +01:00
Heinrich Schuchardt
819874adc0 detect-virt: allow detection via device-tree on RISC-V
Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
2024-02-23 17:25:59 +01:00
mille-feuille
baa90b4b81 detect-virt: fix Google Compute Engine support
Follow-up for 9b0688f491
2024-02-09 12:12:33 +09:00
Black-Hole1
5a02a9adb2 virt: support detection of Apple Virtualization guests with cpuid
This is a supplement to #24419. On macOS Intel machines, detection needs to be done through cpuid.
In macOS, `dmi_vendors` detection is only applicable to M series.

Signed-off-by: Black-Hole1 <bh@bugs.cc>
2024-01-19 14:47:24 +09:00
Yu Watanabe
9b0688f491 virt: add Google Compute Engine support
See https://cloud.google.com/run/docs/container-contract#sandbox
2024-01-10 03:08:31 +09:00
Yu Watanabe
e701439998 virt: fix detection of avx2 and friends
To get the CPUID with EAX=7, we need explicitly set 0 to ECX.

From Intel® Architecture Instruction Set Extensions Programming
Reference and Related Specifications,
===
Leaf 07H output depends on the initial value in ECX.
If ECX contains an invalid sub leaf index, EAX/EBX/ECX/EDX return 0
===

Fixes #30822.
2024-01-08 16:04:17 +01:00
Mike Yuan
7312c422f0 basic/uid-range: add uid_map_read_one helper 2023-12-15 21:22:51 +08:00
Yu Watanabe
7636caf5b3 virt: also check if PID1 is in chroot
In PID1, running_in_chroot() is called by safety_checks(), but without
this change, the check did nothing.
2023-11-03 16:05:52 +00:00
Xiaotian Wu
f106a63952 LoongArch: Replace __loongarch64 with __loongarch_lp64
`__loongarch64` is no longer used for historical reasons and should be replaced by `__loongarch_lp64` in new code.

https://loongson.github.io/LoongArch-Documentation/LoongArch-toolchain-conventions-EN.html#_cc_preprocessor_built_in_macro_definitions
2023-08-09 20:44:57 +08:00
Yu Watanabe
627cdcc785 tree-wide: fix typos reported by Fossies Codespell report 2023-07-12 10:14:50 +09:00
Frantisek Sumsal
9cf7f7a82f virt: drop an unused assignment
As `v` gets overwritten by the following detect_vm_cpuid() call.

Resolves: CID#1509247
2023-07-02 12:22:41 +02:00
Lennart Poettering
563e684689 stat-util: rename files_same() → inode_same()
Let's be more accurate about what this function does: it checks whether
the underlying reported inode is the same. Internally, this already uses
a better named stat_inode_same() call, hence let's similarly name the
wrapping function following the same logic.

Similar for files_same_at() and path_equal_or_same_files().

No code changes, just some renaming.
2023-05-19 17:42:41 +02:00
Benjamin Herrenschmidt
aab896e213 virt: Further improve detection of EC2 metal instances
Commit f90eea7d18
virt: Improve detection of EC2 metal instances

Added support for detecting EC2 metal instances via the product
name in DMI by testing for the ".metal" suffix.

Unfortunately this doesn't cover all cases, as there are going to be
instance types where ".metal" is not a suffix (ie, .metal-16xl,
.metal-32xl, ...)

This modifies the logic to also allow those new forms.

Signed-off-by: Benjamin Herrenschmidt <benh@amazon.com>
2023-04-17 13:21:11 +01:00
Cristian Rodríguez
5545f336fd Include <threads.h> if possible to get thread_local definition
IN C23, thread_local is a reserved keyword and we shall therefore
do nothing to redefine it. glibc has it defined for older standard
version with the right conditions.

v2 by Yu Watanabe:
Move the definition to missing_threads.h like the way we define e.g.
missing syscalls or missing definitions, and include it by the users.

Co-authored-by: Yu Watanabe <watanabe.yu+github@gmail.com>
2023-03-06 10:04:39 +09:00
Frantisek Sumsal
8c7a6c742a virt: correctly detect QEMU emulated pSeries guests
Resolves: #26629
2023-03-01 16:52:27 +00:00
Benjamin Tissoires
a91078bc57 virt: fix container detection
Commit 1b86c7c59e ("virt: make virtualization enum a named type")
made the conversion from `if (!r)` to `if (v != VIRTUALIZATION_NONE)`.

However, the initial test was meaning "if r is null", IOW "if r IS
`VIRTUALIZATION_NONE`).

The test is wrong and this can lead to false detection of the container
environment (when calling `systemctl exit`).

For example, https://gitlab.freedesktop.org/whot/libevdev/-/jobs/34207974
is calling `systemctl exit 0`, and systemd terminates with the exit code
`130`.

Fixing that typo makes `systemctl exit 0` returns `0`.

Fixes: 1b86c7c59e.
2023-01-05 21:45:53 +01:00
Neil Moore
d833ed781f virt: Support detection of LMHS SRE guests 2022-12-02 10:29:14 +09:00
Zbigniew Jędrzejewski-Szmek
1a25a77f3e basic/virt: treat missing /proc as sign of being in a chroot
The logic of running_in_chroot() has been the same since the introduction of
this function in b4f10a5e89: if /proc is not
mounted, the function returns -ENOENT and all callers treat this as false. But
that might be the most common case of chrooted calls, esp. in all the naïve
chroots that were done with the chroot binary without additional setup.

(In particular rpm executes all scriptlets in a chroot without bothering to set
up /proc or /sys, and we have codepaths in sysusers and tmpfiles to support
running in such an environment.)

This change effectively shortcircuits various calls to udevadm, downgrades
logging in tmpfiles, and disables all verbs marked with VERB_ONLINE_ONLY in
systemctl. detect-virt -r is also affected:

$ sudo chroot /var/lib/machines/rawhide
before> systemd-detect-virt -r && echo OK
Failed to check for chroot() environment: No such file or directory
after> systemd-detect-virt -r && echo OK
OK
2022-11-09 10:10:59 +01:00
Yu Watanabe
72715f3d13 virt: fix alignment
Follow-up for f555830674.
2022-08-25 16:23:05 +00:00
Zhaofeng Li
f555830674 virt: Support detection of Apple Virtualization.framework guests 2022-08-24 09:34:54 +02:00
Fei Li
c15d1ac2c4 virt: detect KubeVirt instance
Kubevirt is currently technically based on KVM (but not xen yet[1]).
The systemd-detect-virt command, used to differentiate the current
virtualization environment, works fine on x86 relying on CPUID, while
fails to get the correct value (none instead of kvm) on aarch64.

Let's fix this by adding a new 'vendor[KubeVirt] = kvm' classification
considering the sys_vendor is always KubeVirt.

[1] https://groups.google.com/g/kubevirt-dev/c/C6cUgzTOsVg

Signed-off-by: Fei Li <lifei.shirley@bytedance.com>
2022-07-29 11:10:29 +02:00
Yu Watanabe
5c86cec1f3 virt: align tables 2022-07-11 14:13:33 +02:00
Yu Watanabe
840a49f3dc virt: fix detection of Parallels virtualization
If Parallels virtualization is detected from DMI, then trust that over CPUID.

Fixes issue caused by 28b1a3eac2.

Fixes #23856.
2022-06-29 14:03:54 +02:00
Zbigniew Jędrzejewski-Szmek
a9d178d28b basic/virt: use STRV_FOREACH instead of iteration with ELEMENTSOF
I think it's a bit simpler, we don't have two indexes.
2022-05-08 22:47:50 +02:00
w30023233
01d9fbccdd virt: detect OpenStack Nova instance 2022-04-20 02:32:33 +09:00
Lennart Poettering
0ee2d5b2a9 virt: move array iterators to smaller scope, and use right type 2022-04-04 11:36:26 +02:00
Lennart Poettering
1b86c7c59e virt: make virtualization enum a named type
These days we have a mechanism for safely returning errnos in enum
types, via definining -ERRNO_MAX as one special enu value. Let's use
that for Virtualization.

No change in behaviour, just some typesafety improvement.
2022-04-04 11:36:26 +02:00
Lennart Poettering
0f534758d1 virt: rework kvm with hyperv enlightenment checks a bit
Let's avoid extending the virtualization with an "alias" entry that has
the same string assigned as another.

The only reason this was done was to make the patch small that added a
second CPUID vendor string for kvm to the vm_table[] array. Let's
instead rework the array to use struct elements that match up strings
with ids. Given the array was previously mostly sparse this should be a
general improvement.

Fixes: #22950
Follow-up for: #22945
2022-04-04 11:28:56 +02:00
Brett Holman
fbe0430015 Identify kvm + hv_passthrough as "kvm" 2022-04-03 19:21:24 +09:00
Lennart Poettering
16fa47463c virt: use read_virtual_file() for reading /proc/self/setgroups 2022-03-31 16:34:41 +02:00
Lennart Poettering
b2a331f2e0 virt: simplify userns_has_mapping() by using fscanf() instead of scanf()
And while we are at it, also fix propagation of an uninitialized errno
error.
2022-03-31 16:34:41 +02:00
Richard Neill
ea583ed5a3 virt: Fix Xen Dom0 detection logic to no longer report as VM
Fixes regression introduced in 599be274c1

Moving the Xen check before the CPUID check, in order to handle the case where
a Xen domain is nested within a hypervisor which can be detected by via the
CPUID check, had an unintended consequence of causing Dom0 to report as a Xen
VM when it is not nested.

This patch stops further checks once it has been determined that Dom0 is not
nested within another hypervisor, meaning that the non-nested case matches its
previous logic (where it does not report as a VM).

Also, tidy the conditionals for the Xen and UML checks by removing handling of
a VIRTUALIZATION_VM_OTHER result, which has no code path.

Fixes #22511
2022-02-16 18:05:28 +01:00
Xiaotian Wu
eebbd595f0 virt: add detection for LoongArch 64bit 2021-12-22 11:07:55 +08:00
Zbigniew Jędrzejewski-Szmek
af3b864d76 Define FOREACH_DIRENT through FOREACH_DIRENT_ALL
As in the previous commit, 'de' is used as the iterator variable name.
2021-12-15 22:50:00 +01:00
Boqun Feng
76eec06499 virt: Fix the detection for Hyper-V VMs
Use product_version instead of product_name in DMI table and the string
"Hyper-V" to avoid misdetection.

Fixes: #21468

Signed-off-by: Boqun Feng <boqun.feng@gmail.com>
2021-11-23 13:23:33 +01:00
Greg Zuro
cfee6b9551 change indicator used for later versions of VirtualBox (#21127)
Detection of VirtualBox is accomplished in the existing code by *either* `innotek GmbH`
or `Oracle Corporation` existing in any of:

- /sys/class/dmi/id/product_name
- /sys/class/dmi/id/sys_vendor
- /sys/class/dmi/id/board_vendor
- /sys/class/dmi/id/bios_vendor

With Oracle's physical servers, both `/sys/class/dmi/id/sys_vendor` and
`/sys/class/dmi/id/board_vendor` contain `Oracle Corporation`, so those
servers are detected as `oracle` (VirtualBox).

VirtualBox has the following values in the latest versions:

- /sys/class/dmi/id/product_name: `VirtualBox`
- /sys/class/dmi/id/sys_vendor: `innotek GmbH`
- /sys/class/dmi/id/board_vendor: `Oracle Corporation`
- /sys/class/dmi/id/bios_vendor: `innotek GmbH`

Presumably the existing check for `innotek GmbH` is meant to detect
older versions of VirtualBox, while changing the second checked value
from `Oracle Corporation` to `VirtualBox` will reliably detect later and future
versions.
2021-10-27 13:02:37 +09:00