Skip to content

Commit

Permalink
Merge pull request #295 from microsoft/nlexceptbug
Browse files Browse the repository at this point in the history
Fix use-after-free violation in NetlinkException
  • Loading branch information
abeltrano authored Jun 21, 2024
2 parents e63c8c5 + 786b46c commit 090673f
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 8 deletions.
9 changes: 8 additions & 1 deletion src/linux/libnl-helpers/NetlinkErrorCategory.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ NetlinkErrorCategory::default_error_condition(int error) const noexcept
std::error_code
make_netlink_error_code(int error)
{
return std::error_code(error, NetlinkErrorCategory());
return std::error_code(error, GetNetlinkErrorCategory());
}

std::error_code
Expand All @@ -44,4 +44,11 @@ MakeNetlinkErrorCode(int error)
return make_netlink_error_code(error);
}

const NetlinkErrorCategory&
GetNetlinkErrorCategory()
{
static NetlinkErrorCategory instance;
return instance;
}

} // namespace Microsoft::Net::Netlink
Original file line number Diff line number Diff line change
Expand Up @@ -42,26 +42,34 @@ struct NetlinkErrorCategory :

/**
* @brief Constructs a std::error_code from a netlink error code.
*
*
* This function uses the std naming convention.
*
*
* @param error The netlink error code. Must be positive.
* @return std::error_code
* @return std::error_code
*/
[[nodiscard]] std::error_code
make_netlink_error_code(int error);

/**
* @brief Alias for the make_netlink_error_code function.
*
*
* This function uses the project naming convention.
*
* @param error
* @return std::error_code
*
* @param error
* @return std::error_code
*/
[[nodiscard]] std::error_code
MakeNetlinkErrorCode(int error);

/**
* @brief Get the NetlinkErrorCategory object instance.
*
* @return const NetlinkErrorCategory&
*/
[[nodiscard]] const NetlinkErrorCategory&
GetNetlinkErrorCategory();

} // namespace Microsoft::Net::Netlink

#endif // NETLINK_ERROR_CATEGORY_HXX
1 change: 1 addition & 0 deletions tests/unit/linux/libnl-helpers/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ target_sources(libnl-helpers-test-unit
Main.cxx
TestNetlink80211Interface.cxx
TestNetlink80211ProtocolState.cxx
TestNetlinkException.cxx
TestNetlinkRoute.cxx
)

Expand Down
14 changes: 14 additions & 0 deletions tests/unit/linux/libnl-helpers/TestNetlinkException.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

#include <catch2/catch_test_macros.hpp>
#include <microsoft/net/netlink/NetlinkException.hxx>
#include <netlink/errno.h>

TEST_CASE("NetlinkException", "[linux][libnl-helpers]")
{
using Microsoft::Net::Netlink::NetlinkException;

SECTION("Does not crash")
{
REQUIRE_THROWS_AS(throw NetlinkException(NLE_BAD_SOCK, "test"), NetlinkException);
}
}

0 comments on commit 090673f

Please sign in to comment.