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

New fuzz harness + refactoring into fuzz/ #2273

Merged
merged 3 commits into from
Oct 20, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -444,3 +444,6 @@ compile_commands.json

# Visual Studio Code
.vscode

# Uncrustify
uncrustify.cfg
40 changes: 40 additions & 0 deletions fuzz/C++/fuzz_XMLProfiles/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Copyright 2016 Proyectos y Sistemas de Mantenimiento SL (eProsima).
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

cmake_minimum_required(VERSION 2.8.12)

if(NOT CMAKE_VERSION VERSION_LESS 3.0)
cmake_policy(SET CMP0048 NEW)
endif()

project(fuzz_XMLProfiles)

# Find requirements
if(NOT fastcdr_FOUND)
find_package(fastcdr REQUIRED)
endif()

if(NOT foonathan_memory_FOUND)
find_package(foonathan_memory REQUIRED)
endif()

if(NOT fastrtps_FOUND)
find_package(fastrtps REQUIRED)
endif()

message(STATUS "Configuring fuzz_XMLProfiles...")
file(GLOB SOURCES_CXX "*.cxx")

add_executable(fuzz_XMLProfiles ${SOURCES_CXX} ${SOURCES_CPP})
target_link_libraries(fuzz_XMLProfiles fastrtps fastcdr foonathan_memory $ENV{LIB_FUZZING_ENGINE})
43 changes: 43 additions & 0 deletions fuzz/C++/fuzz_XMLProfiles/fuzz_XMLProfiles.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <fastrtps/Domain.h>
#include <fastrtps/xmlparser/XMLProfileManager.h>

#include "fuzz_utils.h"

using namespace eprosima;
using namespace eprosima::fastrtps;

static bool initialized = false;

extern "C" int LLVMFuzzerTestOneInput(
const uint8_t* data,
size_t size)
{
if (!initialized)
{
ignore_stdout();
initialized = true;
}

if (!size)
{
return EXIT_FAILURE;
}

const char* filename = buf_to_file(data, size);

if (filename == NULL)
{
return EXIT_FAILURE;
}

// TODO change this to a func. taking buf + len (or C string)
// to avoid using `buf_to_file`
xmlparser::XMLProfileManager::loadXMLFile(filename);

if (delete_file(filename) != 0)
{
return EXIT_FAILURE;
}

return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?xml version="1.0" encoding="UTF-8" ?>
<dds xmlns="http://www.eprosima.com/XMLSchemas/fastRTPS_Profiles" >
<profiles>
<participant profile_name="participant_profile">
<domainId>80</domainId>
<rtps>
<builtin>
<discovery_config>
<leaseDuration>
<sec>DURATION_INFINITY</sec>
</leaseDuration>
</discovery_config>
</builtin>
</rtps>
</participant>

<publisher profile_name="publisher_profile">
<topic>
<name>xml_profiles_topic</name>
<dataType>XMLProfilesExample</dataType>
<kind>NO_KEY</kind>
<historyQos>
<kind>KEEP_LAST</kind>
<depth>20</depth>
</historyQos>
</topic>
<qos>
<reliability>
<kind>RELIABLE</kind>
</reliability>
</qos>
<historyMemoryPolicy>PREALLOCATED</historyMemoryPolicy>
</publisher>

<subscriber profile_name="subscriber_profile">
<topic>
<name>xml_profiles_topic</name>
<dataType>XMLProfilesExample</dataType>
<kind>NO_KEY</kind>
<historyQos>
<kind>KEEP_LAST</kind>
<depth>20</depth>
</historyQos>
</topic>
<qos>
<reliability>
<kind>RELIABLE</kind>
</reliability>
</qos>
<historyMemoryPolicy>PREALLOCATED</historyMemoryPolicy>
</subscriber>

</profiles>
</dds>
95 changes: 95 additions & 0 deletions fuzz/C++/fuzz_XMLProfiles/fuzz_utils.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#include "fuzz_utils.h"

#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

extern "C" int ignore_stdout(
void)
{
int fd = open("/dev/null", O_WRONLY);
if (fd == -1)
{
warn("open(\"/dev/null\") failed");
return -1;
}

int ret = 0;
if (dup2(fd, STDOUT_FILENO) == -1)
{
warn("failed to redirect stdout to /dev/null\n");
ret = -1;
}

if (close(fd) == -1)
{
warn("close");
ret = -1;
}

return ret;
}

extern "C" int delete_file(
const char* pathname)
{
int ret = unlink(pathname);
if (ret == -1)
{
warn("failed to delete \"%s\"", pathname);
}

free((void*)pathname);

return ret;
}

extern "C" char* buf_to_file(
const uint8_t* buf,
size_t size)
{
char* pathname = strdup("/dev/shm/fuzz-XXXXXX");
if (pathname == NULL)
{
return NULL;
}

int fd = mkstemp(pathname);
if (fd == -1)
{
warn("mkstemp(\"%s\")", pathname);
free(pathname);
return NULL;
}

size_t pos = 0;
while (pos < size)
{
int nbytes = write(fd, &buf[pos], size - pos);
if (nbytes <= 0)
{
if (nbytes == -1 && errno == EINTR)
{
continue;
}
warn("write");
goto err;
}
pos += nbytes;
}

if (close(fd) == -1)
{
warn("close");
goto err;
}

return pathname;

err:
delete_file(pathname);
return NULL;
}
33 changes: 33 additions & 0 deletions fuzz/C++/fuzz_XMLProfiles/fuzz_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Helpers functions for fuzz targets.

#ifndef FUZZ_UTILS_H_
#define FUZZ_UTILS_H_

#include <stddef.h>
#include <stdint.h>

// Redirect stdout to /dev/null. Useful to ignore output from verbose fuzz
// target functions.
//
// Return 0 on success, -1 otherwise.
extern "C" int ignore_stdout(
void);

// Delete the file passed as argument and free the associated buffer. This
// function is meant to be called on buf_to_file return value.
//
// Return 0 on success, -1 otherwise.
extern "C" int delete_file(
const char* pathname);

// Write the data provided in buf to a new temporary file. This function is
// meant to be called by LLVMFuzzerTestOneInput() for fuzz targets that only
// take file names (and not data) as input.
//
// Return the path of the newly created file or NULL on error. The caller should
// eventually free the returned buffer (see delete_file).
extern "C" char* buf_to_file(
const uint8_t* buf,
size_t size);

#endif // FUZZ_UTILS_H_
26 changes: 26 additions & 0 deletions fuzz/C++/fuzz_processCDRMsg/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
cmake_minimum_required(VERSION 2.8.12)

if(NOT CMAKE_VERSION VERSION_LESS 3.0)
cmake_policy(SET CMP0048 NEW)
endif()

project(fuzz_processCDRMsg)

# Find requirements
if(NOT fastcdr_FOUND)
find_package(fastcdr REQUIRED)
endif()

if(NOT foonathan_memory_FOUND)
find_package(foonathan_memory REQUIRED)
endif()

if(NOT fastrtps_FOUND)
find_package(fastrtps REQUIRED)
endif()

message(STATUS "Configuring fuzz_processCDRMsg...")
file(GLOB SOURCES_CXX "fuzz_*.cxx")

add_executable(fuzz_processCDRMsg ${SOURCES_CXX})
target_link_libraries(fuzz_processCDRMsg fastrtps fastcdr foonathan_memory $ENV{LIB_FUZZING_ENGINE})
42 changes: 42 additions & 0 deletions fuzz/C++/fuzz_processCDRMsg/fuzz_processCDRMsg.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdarg.h>
#include <string.h>

#include <fastrtps/rtps/messages/MessageReceiver.h>
#include <fastdds/rtps/attributes/RTPSParticipantAttributes.h>

#define MIN_SIZE 256
#define MAX_SIZE 64000

using namespace eprosima::fastrtps;
using namespace eprosima::fastrtps::rtps;

static const Locator_t remoteLocator;
static const Locator_t recvLocator;

extern "C" int LLVMFuzzerTestOneInput(
const uint8_t* data,
size_t size)
{
if (size < MIN_SIZE || size > MAX_SIZE)
MiguelCompany marked this conversation as resolved.
Show resolved Hide resolved
{
return 0;
}

MessageReceiver* rcv = new MessageReceiver(NULL, MAX_SIZE);

CDRMessage_t msg(0);
msg.wraps = true;
msg.buffer = const_cast<octet*>(data);
msg.length = size;
msg.max_size = size;
msg.reserved_size = size;

// TODO: Should we unlock in case UnregisterReceiver is called from callback ?
rcv->processCDRMsg(remoteLocator, recvLocator, &msg);
delete rcv;

return 0;
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
RTPSNDDSPING
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
18 changes: 18 additions & 0 deletions fuzz/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright 2016 Proyectos y Sistemas de Mantenimiento SL (eProsima).
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set(fastrtps_FOUND TRUE)

add_subdirectory(C++/fuzz_XMLProfiles)
add_subdirectory(C++/fuzz_processCDRMsg)