Skip to content
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

Validation layer: null pointer deref when struct contains handle #161

Closed
rpavlik opened this issue Feb 24, 2020 · 12 comments
Closed

Validation layer: null pointer deref when struct contains handle #161

rpavlik opened this issue Feb 24, 2020 · 12 comments
Assignees
Labels
bug Something isn't working in layers synced to gitlab Synchronized to OpenXR internal GitLab

Comments

@rpavlik
Copy link
Contributor

rpavlik commented Feb 24, 2020

In the generated code below, there is no check for non-null value before performing value->actionSet

XrResult ValidateXrStruct(GenValidUsageXrInstanceInfo *instance_info, const std::string &command_name,
                          std::vector<GenValidUsageXrObjectInfo>& objects_info, bool check_members,
                          const XrActiveActionSet* value) {
    XrResult xr_result = XR_SUCCESS;
    // If we are not to check the rest of the members, just return here.
    if (!check_members || XR_SUCCESS != xr_result) {
        return xr_result;
    }
    {
        // writeValidateInlineHandleValidation
        ValidateXrHandleResult handle_result = VerifyXrActionSetHandle(&value->actionSet);
        if (handle_result != VALIDATE_XR_HANDLE_SUCCESS) {
            // Not a valid handle or NULL (which is not valid in this case)
            std::ostringstream oss;
            oss << "Invalid XrActionSet handle \"actionSet\" ";
            oss << HandleToHexString(value->actionSet);
            CoreValidLogMessage(instance_info, "VUID-XrActiveActionSet-actionSet-parameter",
                                VALID_USAGE_DEBUG_SEVERITY_ERROR, command_name,
                                objects_info, oss.str());
            return XR_ERROR_HANDLE_INVALID;
        }
    }
    // Everything checked out properly
    return xr_result;
}
@rpavlik rpavlik added bug Something isn't working in layers labels Feb 24, 2020
@bradgrantham-lunarg
Copy link

Thanks. Do you have a test program?

@rpavlik
Copy link
Contributor Author

rpavlik commented Feb 26, 2020

No, this was found via static analysis with clang/CodeChecker.

@bradgrantham-lunarg
Copy link

bradgrantham-lunarg commented Feb 27, 2020

I think I see. In

XrResult ValidateXrStruct(GenValidUsageXrInstanceInfo *instance_info, const std::string &command_name, std::vector<GenValidUsageXrObjectInfo>& objects_info, bool check_members, const XrActionsSyncInfo* value)

There's a check that activeActionSets != nullptr if countActiveActionSets > 0, but it only sets xr_result and nothing later tests that result. So this is similar to #160 in that it needs to gate the next call on xr_result == XR_SUCCESS or just return the validation error.

I'll put this in my queue.

(In the event that activeActionSets == nullptr && countActiveActionSets > 0, at least at the present time there will be a validation error printed.)

@rpavlik
Copy link
Contributor Author

rpavlik commented Feb 27, 2020

No, I don't think that's quite right.

So this addition to hello_xr causes a warning from the validation layer ("XR_ERROR_VALIDATION_FAILURE in xrSyncActions: (syncInfo->countActiveActionSets == 0)"), but it doesn't error. (Is that intended?)

diff --git a/src/tests/hello_xr/openxr_program.cpp b/src/tests/hello_xr/openxr_program.cpp
index 4e8976d..c2ae201 100644
--- a/src/tests/hello_xr/openxr_program.cpp
+++ b/src/tests/hello_xr/openxr_program.cpp
@@ -554,6 +554,11 @@ struct OpenXrProgram : IOpenXrProgram {
         InitializeActions();
         CreateVisualizedSpaces();
 
+        XrActionsSyncInfo info{XR_TYPE_ACTIONS_SYNC_INFO, nullptr};
+        info.activeActionSets = nullptr;
+        info.countActiveActionSets = 0;
+        xrSyncActions(m_session, &info);
+
         {
             XrReferenceSpaceCreateInfo referenceSpaceCreateInfo = GetXrReferenceSpaceCreateInfo(m_options->AppSpace);
             CHECK_XRCMD(xrCreateReferenceSpace(m_session, &referenceSpaceCreateInfo, &m_appSpace));

while this change will trigger the bad behavior of this bug:

diff --git a/src/tests/hello_xr/openxr_program.cpp b/src/tests/hello_xr/openxr_program.cpp
index 4e8976d..504a975 100644
--- a/src/tests/hello_xr/openxr_program.cpp
+++ b/src/tests/hello_xr/openxr_program.cpp
@@ -554,6 +554,11 @@ struct OpenXrProgram : IOpenXrProgram {
         InitializeActions();
         CreateVisualizedSpaces();
 
+        XrActionsSyncInfo info{XR_TYPE_ACTIONS_SYNC_INFO, nullptr};
+        info.activeActionSets = nullptr;
+        info.countActiveActionSets = 5; // this is a lie
+        xrSyncActions(m_session, &info);
+
         {
             XrReferenceSpaceCreateInfo referenceSpaceCreateInfo = GetXrReferenceSpaceCreateInfo(m_options->AppSpace);
             CHECK_XRCMD(xrCreateReferenceSpace(m_session, &referenceSpaceCreateInfo, &m_appSpace));

resulting in

[15:22:46.498][Info   ] Available reference spaces: 3
fish: “build/src/tests/hello_xr/hello_…” terminated by signal SIGSEGV (Address boundary error)

@bradgrantham-lunarg
Copy link

In what way is it "not quite right"?

If I apply your second patch (activeActionSets = nullptr and countActiveActionSets = 5) I get the message:

[VALID_ERROR | VUID-XrActionsSyncInfo-activeActionSets-parameter | xrSyncActions]: Structure XrActionsSyncInfo member countActiveActionSets is NULL, but value->countActiveActionSets is greater than 0
  Objects:
   [0] - XrSession (0x0000000000000002)

like I said it would. Does that not print out for you?

Then I get a crash, like the static check implies. That's because the pointer is checked but then the code continues on because xr_result is set and then never examined.

If I add if(xr_result != XR_SUCCESS) return xr_result; in ValidateXrStruct after if (0 != value->countActiveActionSets && nullptr == value->activeActionSets) { ... } and I wrap your call to xrSyncActions with CHECK_XRCMD then hello_xr closes down after a validation error. That's what I expect. Would you expect something different?

I think we need, like #160, to figure out whether to check xr_result after every test or just plain return. Do you disagree?

(The behavior from the layer on syncInfo->countActiveActionSets == 0 might be muddled. It's not an error, but I think it's probably not what apps want, so it gets a validation message. I would argue in any case that's not part of this bug.)

@rpavlik
Copy link
Contributor Author

rpavlik commented Mar 2, 2020

I don't think I got that message (or any message?) but perhaps I didn't have the layer configured right. As long as you see what's happening with the second patch, then we're good - that's much clearer than my prose from last week.

@bradgrantham-lunarg
Copy link

I did have to set the environment variable for logging to stdout. I think (grepping for "Env", not in front of my running build at the moment) it's XR_CORE_VALIDATION_EXPORT_TYPE=text. Do we maybe need a wiki page on GH explaining how to run the validation layers for us and for others?

@rpavlik
Copy link
Contributor Author

rpavlik commented Mar 3, 2020

Yeah, that or a readme. Any reason why logging to stdout isn't default?

@rpavlik-bot
Copy link
Collaborator

An issue (number 1322) has been filed to correspond to this issue in the internal Khronos GitLab.

If you have a Khronos account, you can access that issue at KHR:openxr/openxr#1322.

@rpavlik-bot rpavlik-bot added the synced to gitlab Synchronized to OpenXR internal GitLab label Mar 23, 2020
@rpavlik
Copy link
Contributor Author

rpavlik commented Apr 2, 2020

fwiw, if checking gets moved up, I'd prefer to convert the use of a pointer to a reference: lacking use of gsl::not_null passing a reference instead of a pointer makes this status clear.

@bradgrantham-lunarg
Copy link

Yeah, that or a readme. Any reason why logging to stdout isn't default?

It turns out there is a readme already. I didn't know it was there either.
https://github.com/KhronosGroup/OpenXR-SDK-Source/blob/master/src/api_layers/README_core_validation.md

rpavlik added a commit that referenced this issue May 29, 2020
-   Registry
    -   Add an author ID, and reserve a vendor extension for Huawei.
        (OpenXR-Docs/#46)
    -   Reserve vendor extensions for future LunarG overlay and input
        focus functionality. (internal MR 1720)
    -   Reserve vendor extensions for Microsoft. (internal MR 1723)
    -   Add XR_EXT_hand_tracking multi-vendor extension. (internal MR
        1554, internal issue 1266, internal issue 1267, internal issue
        1268, internal issue 1269)
    -   Add XR_HUAWEI_controller_interaction vendor extension.
        (OpenXR-Docs/#47)
    -   Add XR_MNDX_egl_enable provisional vendor extension.
        (OpenXR-Docs/#48)
    -   Add XR_MSFT_spatial_graph_bridge vendor extension. (internal MR
        1730)
    -   Add XR_MSFT_secondary_view_configuration and
        XR_MSFT_first_person_observer vendor extensions. (internal MR
        1731)
    -   Add XR_MSFT_hand_mesh_tracking vendor extension. (internal MR
        1736)
    -   Fix missing space in XML definition of
        XrSpatialAnchorCreateInfoMSFT. (internal MR 1742, internal issue
        1351, OpenXR-SDK-Source/#187)
    -   Update a number of contacts for author/vendor tags. (internal MR
        1788, internal issue 1326)
-   SDK
    -   Replaced usage of the _DEBUG macro with NDEBUG. (internal MR
        1756)
    -   Allow disabling of std::filesystem usage via CMake, and detect
        if it’s available and what its requirements are.
        (OpenXR-SDK-Source/#192, OpenXR-SDK-Source/#188)
    -   CI: Modifications to Azure DevOps build pipeline. Now builds UWP
        loader DLLs in addition to Win32 loader DLLs. No longer builds
        static loader libraries due to linkability concerns. Re-arranged
        release artifact zip to distinguish architecture from 32-bit or
        64-bit.
    -   Loader: Replace global static initializers with functions that
        return static locals. With this change, code that includes
        OpenXR doesn’t have to page in this code and initialize these
        during startup. (OpenXR-SDK-Source/#173)
    -   Loader: Unload runtime when xrCreateInstance fails. (internal MR
        1778)
    -   Loader: Add “info”-level debug messages listing all the places
        that we look for the OpenXR active runtime manifest.
        (OpenXR-SDK-Source/#190)
    -   Validation Layer: Fix crash in dereferencing a nullptr optional
        array handle when the count > 0. (internal MR 1709,
        OpenXR-SDK-Source/#161, internal issue 1322)
    -   Validation Layer: Fix static analysis error and possible loss of
        validation error. (internal MR 1715, OpenXR-SDK-Source/#160,
        internal issue 1321)
    -   Validation Layer: Simplify some generated code, and minor
        performance improvements. (OpenXR-SDK-Source/#176)
    -   API Dump Layer: Fix crash in dereferencing a nullptr while
        constructing a std::string. (internal MR 1712,
        OpenXR-SDK-Source/#162, internal issue 1323)
    -   hello_xr: Fix releasing a swapchain image with the incorrect
        image layout. (internal MR 1755)
    -   hello_xr: Prefer VK_LAYER_KHRONOS_validation over
        VK_LAYER_LUNARG_standard_validation when available. (internal MR
        1755)
    -   hello_xr: Optimizations to D3D12 plugin to avoid GPU pipeline
        stall. (internal MR 1770) (OpenXR-SDK-Source/#175)
    -   hello_xr: Fix build with Vulkan headers 1.2.136.
        (OpenXR-SDK-Source/#181, OpenXR-SDK-Source/#180, internal issue
        1347)
    -   hello_xr: Fix build with Visual Studio 16.6.
        (OpenXR-SDK-Source/#186, OpenXR-SDK-Source/#184)
@rpavlik
Copy link
Contributor Author

rpavlik commented May 31, 2020

Fix released in 1.0.9

@rpavlik rpavlik closed this as completed May 31, 2020
rhabacker pushed a commit to rhabacker/OpenXR-SDK-Source that referenced this issue Nov 16, 2022
-   Registry
    -   Add an author ID, and reserve a vendor extension for Huawei.
        (OpenXR-Docs/KhronosGroup#46)
    -   Reserve vendor extensions for future LunarG overlay and input
        focus functionality. (internal MR 1720)
    -   Reserve vendor extensions for Microsoft. (internal MR 1723)
    -   Add XR_EXT_hand_tracking multi-vendor extension. (internal MR
        1554, internal issue 1266, internal issue 1267, internal issue
        1268, internal issue 1269)
    -   Add XR_HUAWEI_controller_interaction vendor extension.
        (OpenXR-Docs/KhronosGroup#47)
    -   Add XR_MNDX_egl_enable provisional vendor extension.
        (OpenXR-Docs/KhronosGroup#48)
    -   Add XR_MSFT_spatial_graph_bridge vendor extension. (internal MR
        1730)
    -   Add XR_MSFT_secondary_view_configuration and
        XR_MSFT_first_person_observer vendor extensions. (internal MR
        1731)
    -   Add XR_MSFT_hand_mesh_tracking vendor extension. (internal MR
        1736)
    -   Fix missing space in XML definition of
        XrSpatialAnchorCreateInfoMSFT. (internal MR 1742, internal issue
        1351, OpenXR-SDK-Source/KhronosGroup#187)
    -   Update a number of contacts for author/vendor tags. (internal MR
        1788, internal issue 1326)
-   SDK
    -   Replaced usage of the _DEBUG macro with NDEBUG. (internal MR
        1756)
    -   Allow disabling of std::filesystem usage via CMake, and detect
        if it’s available and what its requirements are.
        (OpenXR-SDK-Source/KhronosGroup#192, OpenXR-SDK-Source/KhronosGroup#188)
    -   CI: Modifications to Azure DevOps build pipeline. Now builds UWP
        loader DLLs in addition to Win32 loader DLLs. No longer builds
        static loader libraries due to linkability concerns. Re-arranged
        release artifact zip to distinguish architecture from 32-bit or
        64-bit.
    -   Loader: Replace global static initializers with functions that
        return static locals. With this change, code that includes
        OpenXR doesn’t have to page in this code and initialize these
        during startup. (OpenXR-SDK-Source/KhronosGroup#173)
    -   Loader: Unload runtime when xrCreateInstance fails. (internal MR
        1778)
    -   Loader: Add “info”-level debug messages listing all the places
        that we look for the OpenXR active runtime manifest.
        (OpenXR-SDK-Source/KhronosGroup#190)
    -   Validation Layer: Fix crash in dereferencing a nullptr optional
        array handle when the count > 0. (internal MR 1709,
        OpenXR-SDK-Source/KhronosGroup#161, internal issue 1322)
    -   Validation Layer: Fix static analysis error and possible loss of
        validation error. (internal MR 1715, OpenXR-SDK-Source/KhronosGroup#160,
        internal issue 1321)
    -   Validation Layer: Simplify some generated code, and minor
        performance improvements. (OpenXR-SDK-Source/KhronosGroup#176)
    -   API Dump Layer: Fix crash in dereferencing a nullptr while
        constructing a std::string. (internal MR 1712,
        OpenXR-SDK-Source/KhronosGroup#162, internal issue 1323)
    -   hello_xr: Fix releasing a swapchain image with the incorrect
        image layout. (internal MR 1755)
    -   hello_xr: Prefer VK_LAYER_KHRONOS_validation over
        VK_LAYER_LUNARG_standard_validation when available. (internal MR
        1755)
    -   hello_xr: Optimizations to D3D12 plugin to avoid GPU pipeline
        stall. (internal MR 1770) (OpenXR-SDK-Source/KhronosGroup#175)
    -   hello_xr: Fix build with Vulkan headers 1.2.136.
        (OpenXR-SDK-Source/KhronosGroup#181, OpenXR-SDK-Source/KhronosGroup#180, internal issue
        1347)
    -   hello_xr: Fix build with Visual Studio 16.6.
        (OpenXR-SDK-Source/KhronosGroup#186, OpenXR-SDK-Source/KhronosGroup#184)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working in layers synced to gitlab Synchronized to OpenXR internal GitLab
Projects
None yet
Development

No branches or pull requests

3 participants