-
Notifications
You must be signed in to change notification settings - Fork 16
(FACT-1717) Add VMware detector #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| #pragma once | ||
|
|
||
| #include <internal/detectors/result.hpp> | ||
| #include <internal/sources/cpuid_source.hpp> | ||
| #include <internal/sources/dmi_source.hpp> | ||
| #include <string> | ||
|
|
||
| namespace whereami { namespace detectors { | ||
|
|
||
| /** | ||
| * VMware detector function | ||
| * @param cpuid_source An instance of a CPUID data source | ||
| * @param dmi_source An instance of a DMI data source | ||
| * @return The VMware detection result | ||
| */ | ||
| result vmware(const sources::cpuid_base& cpuid_source, | ||
| const sources::dmi_base& dmi_source); | ||
|
|
||
| }} // namespace whereami::detectors |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| #include <internal/detectors/vmware_detector.hpp> | ||
| #include <internal/vm.hpp> | ||
| #include <leatherman/execution/execution.hpp> | ||
| #include <leatherman/util/regex.hpp> | ||
|
|
||
| using namespace whereami; | ||
| using namespace leatherman::util; | ||
| using namespace std; | ||
|
|
||
| /** | ||
| * Retrieve the VMware version based on a DMI BIOS address result, if possible | ||
| * @param address The value of the BIOS address from DMI | ||
| * @return The VMware version | ||
| */ | ||
| static string vmware_bios_address_to_version(int address) | ||
| { | ||
| const std::unordered_map<int, std::string> version_map { | ||
| { 0xe8480, "ESXi 2.5" }, | ||
| { 0xe7c70, "ESXi 3.0" }, | ||
| { 0xe66c0, "ESXi 3.5" }, | ||
| { 0xe7910, "ESXi 3.5" }, | ||
| { 0xea550, "ESXi 4.0" }, | ||
| { 0xea6c0, "ESXi 4.0" }, | ||
| { 0xea2e0, "ESXi 4.1" }, | ||
| { 0xe72c0, "ESXi 5.0" }, | ||
| { 0xea0c0, "ESXi 5.1" }, | ||
| { 0xea050, "ESXi 5.5" }, | ||
| { 0xe99e0, "ESXi 6.0" }, | ||
| { 0xea580, "ESXi 6.5" }, | ||
| { 0xea5e0, "Fusion 8.5" }, | ||
| }; | ||
|
|
||
| auto it = version_map.find(address); | ||
|
|
||
| if (it != version_map.end()) { | ||
| return it->second; | ||
| } | ||
| return {}; | ||
| } | ||
|
|
||
| namespace whereami { namespace detectors { | ||
|
|
||
| result vmware(const sources::cpuid_base& cpuid_source, | ||
| const sources::dmi_base& dmi_source) | ||
| { | ||
| result res {vm::vmware}; | ||
|
|
||
| if (dmi_source.manufacturer() == "VMware, Inc." || | ||
| cpuid_source.vendor() == "VMwareVMware") { | ||
| res.validate(); | ||
|
|
||
| auto bios_address = dmi_source.bios_address(); | ||
| if (!bios_address.empty()) { | ||
| auto value = stoi(bios_address, nullptr, 16); | ||
| res.set("version", vmware_bios_address_to_version(value)); | ||
| } | ||
| } | ||
|
|
||
| return res; | ||
| } | ||
|
|
||
| }} // namespace whereami::detectors |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| #include <catch.hpp> | ||
| #include <internal/detectors/vmware_detector.hpp> | ||
| #include "../fixtures/cpuid_fixtures.hpp" | ||
| #include "../fixtures/dmi_fixtures.hpp" | ||
|
|
||
| using namespace std; | ||
| using namespace whereami::detectors; | ||
| using namespace whereami::sources; | ||
| using namespace whereami::testing::cpuid; | ||
| using namespace whereami::testing::dmi; | ||
|
|
||
| SCENARIO("Using the VMware detector") { | ||
| WHEN("running as root on a Linux VMware guest") { | ||
| cpuid_fixture_values cpuid_source({ | ||
| {VENDOR_LEAF, register_fixtures::VENDOR_VMwareVMware}, | ||
| {HYPERVISOR_PRESENT, register_fixtures::HYPERVISOR_PRESENT}, | ||
| }); | ||
| dmi_fixture_values dmi_source({ | ||
| "0xea580", | ||
| "Phoenix Technologies LTD", | ||
| "Intel Corporation", | ||
| "440BX Desktop Reference Platform", | ||
| "VMware, Inc.", | ||
| "VMware Virtual Platform", | ||
| { | ||
| "[MS_VM_CERT/SHA1/27d66596a61c48dd3dc7216fd715126e33f59ae7]", | ||
| "Welcome to the Virtual Machine", | ||
| }}); | ||
| THEN("the result should be valid") { | ||
| auto res = vmware(cpuid_source, dmi_source); | ||
| REQUIRE(res.valid()); | ||
| } | ||
| THEN("the result should contain version metdata") { | ||
| auto res = vmware(cpuid_source, dmi_source); | ||
| REQUIRE(res.get<string>("version") == "ESXi 6.5"); | ||
| } | ||
| } | ||
|
|
||
| WHEN("running as a normal user on a Linux VMware guest") { | ||
| cpuid_fixture_values cpuid_source({ | ||
| {VENDOR_LEAF, register_fixtures::VENDOR_VMwareVMware}, | ||
| {HYPERVISOR_PRESENT, register_fixtures::HYPERVISOR_PRESENT}, | ||
| }); | ||
| dmi_fixture_values dmi_source({ | ||
| "", | ||
| "Phoenix Technologies LTD", | ||
| "Intel Corporation", | ||
| "440BX Desktop Reference Platform", | ||
| "VMware, Inc.", | ||
| "VMware Virtual Platform", | ||
| {}}); | ||
| THEN("it should return true") { | ||
| auto res = vmware(cpuid_source, dmi_source); | ||
| REQUIRE(res.valid()); | ||
| } | ||
| THEN("it should not contain version metdata") { | ||
| auto res = vmware(cpuid_source, dmi_source); | ||
| REQUIRE(res.get<string>("version") == ""); | ||
| } | ||
| } | ||
|
|
||
| WHEN("running in a Windows VMware guest") { | ||
| cpuid_fixture_values cpuid_source({ | ||
| {VENDOR_LEAF, register_fixtures::VENDOR_VMwareVMware}, | ||
| {HYPERVISOR_PRESENT, register_fixtures::HYPERVISOR_PRESENT}, | ||
| }); | ||
| dmi_fixture_empty dmi_source; | ||
| THEN("it should return true") { | ||
| REQUIRE(vmware(cpuid_source, dmi_source).valid()); | ||
| } | ||
| } | ||
|
|
||
| WHEN("running outside of VMware") { | ||
| cpuid_fixture_values cpuid_source({ | ||
| {VENDOR_LEAF, register_fixtures::VENDOR_KVMKVMKVM}, | ||
| {HYPERVISOR_PRESENT, register_fixtures::HYPERVISOR_PRESENT}, | ||
| }); | ||
| dmi_fixture_values dmi_source({ | ||
| "Other", | ||
| "Other", | ||
| "Other", | ||
| "Other", | ||
| "Other", | ||
| "Other", | ||
| {}}); | ||
| THEN("it should return false") { | ||
| REQUIRE_FALSE(vmware(cpuid_source, dmi_source).valid()); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this always available in the data from dmidecode, but just not always needed/useful for what we're trying to do? Or only under VMWare?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The BIOS address is always available from dmidecode (as long as the dmidecode output isn't totally empty), but as far as I've seen, VMware is the only place it's actually useful.
The DMI source object is getting pretty crowded as more single-case values like this are collected - I feel like there's probably a better way to organize this.