8000 fuzz: add libFuzzer harnesses and OSS-Fuzz build script for nstun and mount options by phenomenon0 · Pull Request #418 · google/nsjail · GitHub
[go: up one dir, main page]

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
45 changes: 42 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ CC ?= gcc
CXX ?= g++

# pkg-config for protobuf can be expensive/slow
# Skip pkg-config for: clean, indent, kafel_init, and kafel-only builds
ifneq ($(filter-out clean indent kafel_init kafel/libkafel.a,$(MAKECMDGOALS)),)
# Skip pkg-config for: clean, indent, kafel_init, kafel-only, and standalone fuzz builds
ifneq ($(filter-out clean indent kafel_init kafel/libkafel.a fuzz fuzz-smoke,$(MAKECMDGOALS)),)
PROTOBUF_CFLAGS := $(shell pkg-config --cflags protobuf)
PROTOBUF_LIBS := $(shell pkg-config --libs protobuf)
else ifeq ($(MAKECMDGOALS),)
Expand Down Expand Up @@ -106,7 +106,7 @@ kafel/libkafel.a: kafel_init

# Utilities
clean:
$(RM) core Makefile.bak $(OBJS) $(SRCS_PB_CXX) $(SRCS_PB_H) $(SRCS_PB_O) $(BIN) $(TEST_BINS)
$(RM) core Makefile.bak $(OBJS) $(SRCS_PB_CXX) $(SRCS_PB_H) $(SRCS_PB_O) $(BIN) $(TEST_BINS) $(FUZZ_BINS) $(FUZZ_SMOKE_BINS)
ifneq ("$(wildcard kafel/Makefile)","")
+$(MAKE) -C kafel clean
endif
Expand Down Expand Up @@ -345,6 +345,45 @@ tests/unotify_stats_test: tests/unotify_stats_test.cc unotify/stats.cc unotify/s
tests/nstun_encap_test: tests/nstun_encap_test.cc nstun/encap.cc nstun/encap.h nstun/net_defs.h
$(CXX) $(filter-out -c,$(CXXFLAGS)) tests/nstun_encap_test.cc nstun/encap.cc -o $@

FUZZ_CXX ?= clang++
FUZZ_OUT ?= fuzz
LIB_FUZZING_ENGINE ?= -fsanitize=fuzzer,address,undefined
FUZZ_CXXFLAGS = -O2 -g -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -I. -std=c++20 -fno-exceptions -Wall -Wextra -Werror -Wno-unused -Wno-unused-parameter
FUZZ_BINS = $(FUZZ_OUT)/nstun_packet_fuzzer $(FUZZ_OUT)/nstun_encap_fuzzer $(FUZZ_OUT)/mnt_options_fuzzer
FUZZ_SMOKE_BINS = $(FUZZ_OUT)/nstun_packet_smoke $(FUZZ_OUT)/nstun_encap_smoke $(FUZZ_OUT)/mnt_options_smoke

.PHONY: fuzz fuzz-smoke
fuzz: $(FUZZ_BINS)

$(FUZZ_OUT)/nstun_packet_fuzzer: fuzz/nstun_packet_fuzzer.cc nstun/ip.cc nstun/udp.cc nstun/icmp.cc nstun/encap.cc
@mkdir -p $(FUZZ_OUT)
$(FUZZ_CXX) $(FUZZ_CXXFLAGS) $(LIB_FUZZING_ENGINE) fuzz/nstun_packet_fuzzer.cc nstun/ip.cc nstun/udp.cc nstun/icmp.cc nstun/encap.cc -o $@

$(FUZZ_OUT)/nstun_encap_fuzzer: fuzz/nstun_encap_fuzzer.cc nstun/encap.cc
@mkdir -p $(FUZZ_OUT)
$(FUZZ_CXX) $(FUZZ_CXXFLAGS) $(LIB_FUZZING_ENGINE) fuzz/nstun_encap_fuzzer.cc nstun/encap.cc -o $@

$(FUZZ_OUT)/mnt_options_fuzzer: fuzz/mnt_options_fuzzer.cc mnt_options.h
@mkdir -p $(FUZZ_OUT)
$(FUZZ_CXX) $(FUZZ_CXXFLAGS) $(LIB_FUZZING_ENGINE) fuzz/mnt_options_fuzzer.cc -o $@

fuzz-smoke: $(FUZZ_SMOKE_BINS)
$(FUZZ_OUT)/nstun_packet_smoke fuzz/corpus/nstun_packet
$(FUZZ_OUT)/nstun_encap_smoke fuzz/corpus/nstun_encap
$(FUZZ_OUT)/mnt_options_smoke fuzz/corpus/mnt_options

$(FUZZ_OUT)/nstun_packet_smoke: fuzz/nstun_packet_fuzzer.cc fuzz/standalone_driver.cc nstun/ip.cc nstun/udp.cc nstun/icmp.cc nstun/encap.cc
@mkdir -p $(FUZZ_OUT)
$(CXX) $(FUZZ_CXXFLAGS) fuzz/nstun_packet_fuzzer.cc fuzz/standalone_driver.cc nstun/ip.cc nstun/udp.cc nstun/icmp.cc nstun/encap.cc -o $@

$(FUZZ_OUT)/nstun_encap_smoke: fuzz/nstun_encap_fuzzer.cc fuzz/standalone_driver.cc nstun/encap.cc
@mkdir -p $(FUZZ_OUT)
$(CXX) $(FUZZ_CXXFLAGS) fuzz/nstun_encap_fuzzer.cc fuzz/standalone_driver.cc nstun/encap.cc -o $@

$(FUZZ_OUT)/mnt_options_smoke: fuzz/mnt_options_fuzzer.cc fuzz/standalone_driver.cc mnt_options.h
@mkdir -p $(FUZZ_OUT)
$(CXX) $(FUZZ_CXXFLAGS) fuzz/mnt_options_fuzzer.cc fuzz/standalone_driver.cc -o $@

# Dependencies (Generated by makedepend)
# DO NOT DELETE THIS LINE -- make depend depends on it.

Expand Down
27 changes: 27 additions & 0 deletions fuzz/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# nsjail Fuzzing Harnesses (libFuzzer / OSS-Fuzz)

Standalone, dependency-free fuzz targets for untrusted input parsers in `nsjail` (no `protobuf` or `libnl` required):

| Target | Entry Points Covered |
|---|---|
| `fuzz/nstun_packet_fuzzer.cc` | `nstun::handle_ip4`, `nstun::handle_ip6`, IPv6 extension-header chain (`skip_ipv6_ext_headers`), `nstun::handle_udp4/6`, `nstun::handle_icmp4/6`, and ICMP error/echo reply construction (both raw and structure-aware checksum-fixup modes; zero real host sockets). |
| `fuzz/nstun_encap_fuzzer.cc` | `nstun::parse_socks5_auth_reply`, `nstun::parse_socks5_connect_reply`, `nstun::find_end_of_headers`, `nstun::parse_http_connect_reply`, and `nstun::socks5_udp_unwrap_for_flow` (IPv4 and IPv6). |
| `fuzz/mnt_options_fuzzer.cc` | `mnt::extractGenericMountOptions` flag isolation and idempotency invariants. |

## Building & Running Locally

### With libFuzzer + ASan/UBSan (Clang)
```bash
make fuzz
./fuzz/nstun_packet_fuzzer fuzz/corpus/nstun_packet -max_total_time=60
./fuzz/nstun_encap_fuzzer -dict=fuzz/nstun_encap.dict fuzz/corpus/nstun_encap -max_total_time=60
./fuzz/mnt_options_fuzzer fuzz/corpus/mnt_options -max_total_time=60
```

### Standalone Seed Corpus Smoke Test (GCC or Clang, no libFuzzer required)
```bash
make fuzz-smoke
```

### OSS-Fuzz
`infra/oss-fuzz/build.sh` compiles all three targets using `$CXX`, `$CXXFLAGS`, and `$LIB_FUZZING_ENGINE`, installs `nstun_encap_fuzzer.dict`, and packages `fuzz/corpus/*` into `$OUT/*_seed_corpus.zip`.
Binary file added fuzz/corpus/mnt_options/basic_vfs_flags
Binary file not shown.
Binary file added fuzz/corpus/mnt_options/empty_commas
Binary file not shown.
Binary file added fuzz/corpus/mnt_options/fs_only
Binary file not shown.
Binary file added fuzz/corpus/mnt_options/toggle_rw_exec
Binary file not shown.
2 changes: 2 additions & 0 deletions fuzz/corpus/nstun_encap/http_connect_10_204
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
HTTP/1.0 204

3 changes: 3 additions & 0 deletions fuzz/corpus/nstun_encap/http_connect_200
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
HTTP/1.1 200 Connection Established
Proxy-Agent: test

Binary file added fuzz/corpus/nstun_encap/socks5_auth_ok
Binary file not shown.
Binary file added fuzz/corpus/nstun_encap/socks5_connect_domain
Binary file not shown.
Binary file added fuzz/corpus/nstun_encap/socks5_connect_v4
Binary file not shown.
Binary file added fuzz/corpus/nstun_encap/socks5_connect_v6
Binary file not shown.
Binary file added fuzz/corpus/nstun_encap/socks5_udp_v4
Binary file not shown.
Binary file added fuzz/corpus/nstun_encap/socks5_udp_v6
Binary file not shown.
Binary file added fuzz/corpus/nstun_packet/ipv4_icmp_echo_host
Binary file not shown.
Binary file added fuzz/corpus/nstun_packet/ipv4_udp_raw
Binary file not shown.
Binary file added fuzz/corpus/nstun_packet/ipv4_udp_reject
Binary file not shown.
Binary file added fuzz/corpus/nstun_packet/ipv6_ah_udp_reject
Binary file not shown.
Binary file added fuzz/corpus/nstun_packet/ipv6_ext_icmp_echo
Binary file not shown.
Binary file added fuzz/corpus/nstun_packet/ipv6_fragment_drop
Binary file not shown.
40 changes: 40 additions & 0 deletions fuzz/mnt_options_fuzzer.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mount.h>

#include <string>

#include "mnt_options.h"

extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
if (size > 65536) {
return 0;
}

uintptr_t initial_flags = 0;
if (size >= sizeof(uintptr_t)) {
memcpy(&initial_flags, data, sizeof(initial_flags));
data += sizeof(uintptr_t);
size -= sizeof(uintptr_t);
}

std::string options(reinterpret_cast<const char*>(data), size);
uintptr_t flags = initial_flags;
std::string fs_opts = mnt::extractGenericMountOptions(options, &flags);

constexpr uintptr_t kGenericMask = MS_RDONLY | MS_NOSUID | MS_NODEV | MS_NOEXEC;
if ((flags & ~kGenericMask) != (initial_flags & ~kGenericMask)) {
abort();
}

/* Idempotency invariant: remaining fs_opts must contain no generic options. */
uintptr_t flags_second = flags;
std::string fs_opts_second = mnt::extractGenericMountOptions(fs_opts, &flags_second);
if (flags_second != flags || fs_opts_second != fs_opts) {
abort();
}

return 0;
}
14 changes: 14 additions & 0 deletions fuzz/nstun_encap.dict
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# libFuzzer dictionary for nstun proxy encapsulation parsers (SOCKS5 + HTTP CONNECT)
http10="HTTP/1.0 "
http11="HTTP/1.1 "
status200="200 OK\r\n\r\n"
status204="204\r\n\r\n"
status407="407 Proxy Authentication Required\r\n\r\n"
crlf2="\r\n\r\n"
lf2="\n\n"
socks5_auth="\x05\x00"
socks5_v4="\x05\x00\x00\x01"
socks5_dom="\x05\x00\x00\x03"
socks5_v6="\x05\x00\x00\x04"
socks5_udp_v4="\x00\x00\x00\x01"
socks5_udp_v6="\x00\x00\x00\x04"
102 changes: 102 additions & 0 deletions fuzz/nstun_encap_fuzzer.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#include <arpa/inet.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

#include <span>
#include <vector>

#include "logs.h"
#include "nstun/encap.h"
#include "nstun/udp_peer.h"

namespace logs {

void logMsg(enum llevel_t, const char*, int, bool, const char*, ...) {}

enum llevel_t getLogLevel() {
return ERROR;
}

} // namespace logs

namespace {

volatile uint8_t g_sink = 0;

void verify_subspan(std::span<const uint8_t> parent, std::span<const uint8_t> child) {
if (child.empty()) {
return;
}
const uint8_t* p_begin = parent.data();
const uint8_t* p_end = p_begin + parent.size();
const uint8_t* c_begin = child.data();
const uint8_t* c_end = c_begin + child.size();
if (c_begin < p_begin || c_end > p_end || c_begin > c_end) {
abort();
}
g_sink ^= c_begin[0] ^ c_end[-1];
}

struct MockUdpFlow {
bool is_ipv6 = false;
uint32_t orig_dest_ip4 = 0;
uint8_t orig_dest_ip6[nstun::IPV6_ADDR_LEN] = {};
uint16_t orig_dest_port = 0;
};

} // namespace

extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
if (size > 65536) {
return 0;
}

std::span<const uint8_t> span(data, size);

/* 1. SOCKS5 auth reply */
(void)nstun::parse_socks5_auth_reply(span);

/* 2. SOCKS5 connect reply */
nstun::Socks5Reply reply = {};
if (nstun::parse_socks5_connect_reply(span, &reply)) {
g_sink ^= reply.atyp ^ static_cast<uint8_t>(reply.bind_port & 0xFF);
}

/* 3. HTTP CONNECT header framing & status-line reply */
std::vector<uint8_t> vec(data, data + size);
size_t hdr_end = nstun::find_end_of_headers(vec);
if (hdr_end > vec.size()) {
abort();
}
(void)nstun::parse_http_connect_reply(vec);

/* 4. SOCKS5 UDP unwrap (IPv4 and IPv6 flows matching wire bytes when present) */
MockUdpFlow flow4;
flow4.is_ipv6 = false;
if (size >= sizeof(nstun::socks5_udp_hdr)) {
nstun::socks5_udp_hdr hdr = {};
memcpy(&hdr, data, sizeof(hdr));
flow4.orig_dest_ip4 = hdr.dst_ip4;
flow4.orig_dest_port = ntohs(hdr.dst_port);
}
std::span<const uint8_t> payload_out;
if (nstun::socks5_udp_unwrap_for_flow(flow4, span, &payload_out)) {
verify_subspan(span, payload_out);
}

MockUdpFlow flow6;
flow6.is_ipv6 = true;
if (size >= sizeof(nstun::socks5_udp_hdr6)) {
nstun::socks5_udp_hdr6 hdr6 = {};
memcpy(&hdr6, data, sizeof(hdr6));
memcpy(flow6.orig_dest_ip6, hdr6.dst_ip6, sizeof(flow6.orig_dest_ip6));
flow6.orig_dest_port = ntohs(hdr6.dst_port);
}
if (nstun::socks5_udp_unwrap_for_flow(flow6, span, &payload_out)) {
verify_subspan(span, payload_out);
}

return 0;
}
Loading
0