Skip to content

Commit

Permalink
Merge pull request #22644 from wazuh/enhancement/22570-vuldet-unit-te…
Browse files Browse the repository at this point in the history
…st-fail

Added UNIX socket path boundary check
  • Loading branch information
Dwordcito committed Mar 26, 2024
2 parents e774672 + 4d1f0eb commit 2edfda7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/shared_modules/utils/socketWrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,14 @@ class UnixAddress final : public SockAddress<UnixAddress>
UnixAddress& address(const std::string& path)
{
m_unixAddr.sun_family = AF_UNIX;
if (path.size() >= sizeof(m_unixAddr.sun_path))
{
throw std::runtime_error {"Error setting socket path (too long)"};
}

std::copy(path.begin(), path.end(), m_unixAddr.sun_path);
m_unixAddr.sun_path[path.size()] = '\0';

return *this;
}
};
Expand Down
10 changes: 9 additions & 1 deletion src/shared_modules/utils/tests/socketWrapper_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ TEST_F(SocketWrapperTest, SocketWrapperTestInstance)
Socket<OSWrapper> socketWrapper;
}

TEST_F(SocketWrapperTest, UnixAddressPathToLong)
{
constexpr auto MAX_SUN_PATH = 108;
std::string path(MAX_SUN_PATH + 10, 'a');
EXPECT_THROW(UnixAddress::builder().address(path).build(), std::runtime_error);
}

TEST_F(SocketWrapperTest, ConnectSuccess)
{
Socket<OSWrapper> socketWrapper;
Expand Down Expand Up @@ -248,7 +255,8 @@ TEST_F(SocketWrapperTest, DISABLED_ReadPartialBody)
[&data, &header](int, void* buffer, size_t, int)
{
std::copy(header.begin(), header.end(), (char*)buffer);
std::copy(data.begin(), data.begin() + data.size() / 2, (char*)buffer + HEADER_FIELD_SIZE);
std::copy(
data.begin(), data.begin() + data.size() / 2, (char*)buffer + HEADER_FIELD_SIZE);
}),
Return(packetSize)))
.WillOnce(DoAll(Invoke(
Expand Down

0 comments on commit 2edfda7

Please sign in to comment.