Have example/jsonptr self-impose a sandbox

This commit is contained in:
Nigel Tao committed 2020-03-06 09:07:59 +11:00
1 parent e65c861207
commit fe0cbbd6e9
2 files changed
+83 -24

No files matched your search

+80 -22
View File
@@ -37,6 +37,11 @@ The core JSON implementation is also written in the Wuffs programming language
(and then transpiled to C/C++), which is memory-safe but also guards against (and then transpiled to C/C++), which is memory-safe but also guards against
integer arithmetic overflows. integer arithmetic overflows.
For defense in depth, on Linux, this program also self-imposes a
SECCOMP_MODE_STRICT sandbox before reading (or otherwise processing) its input
or writing its output. Under this sandbox, the only permitted system calls are
read, write, exit and sigreturn.
All together, this program aims to safely handle untrusted JSON files without All together, this program aims to safely handle untrusted JSON files without
fear of security bugs such as remote code execution. fear of security bugs such as remote code execution.
@@ -60,9 +65,9 @@ $CXX jsonptr.cc && ./a.out < ../../test/data/github-tags.json; rm -f a.out
for a C++ compiler $CXX, such as clang++ or g++. for a C++ compiler $CXX, such as clang++ or g++.
*/ */
#include <inttypes.h> #include <errno.h>
#include <stdio.h>
#include <string.h> #include <string.h>
#include <unistd.h>
// Wuffs ships as a "single file C library" or "header file library" as per // Wuffs ships as a "single file C library" or "header file library" as per
// https://github.com/nothings/stb/blob/master/docs/stb_howto.txt // https://github.com/nothings/stb/blob/master/docs/stb_howto.txt
@@ -88,6 +93,14 @@ for a C++ compiler $CXX, such as clang++ or g++.
// program to generate a stand-alone C++ file. // program to generate a stand-alone C++ file.
#include "../../release/c/wuffs-unsupported-snapshot.c" #include "../../release/c/wuffs-unsupported-snapshot.c"
#if defined(__linux__)
#include <linux/prctl.h>
#include <linux/seccomp.h>
#include <sys/prctl.h>
#include <sys/syscall.h>
#define WUFFS_EXAMPLE_USE_SECCOMP
#endif
#define TRY(error_msg) \ #define TRY(error_msg) \
do { \ do { \
const char* z = error_msg; \ const char* z = error_msg; \
@@ -112,12 +125,12 @@ static const char* usage =
"duplicate keys.\n" "duplicate keys.\n"
"\n" "\n"
"Formatted means that arrays' and objects' elements are indented, each\n" "Formatted means that arrays' and objects' elements are indented, each\n"
"on its own line. Configure this with the -compact, -indent=N (for N\n" "on its own line. Configure this with the -c / -compact, -i=N / -indent=N\n"
"ranging from 0 to 8) and -tabs flags.\n" "(for N ranging from 0 to 8) and -t / -tabs flags.\n"
"\n" "\n"
"The -query=etc flag gives an optional JSON Pointer query, to print only\n" "The -q=etc or -query=etc flag gives an optional JSON Pointer query, to\n"
"a subset of the input. For example, given RFC 6901 section 5's [sample\n" "print a subset of the input. For example, given RFC 6901 section 5's\n"
"JSON value](https://tools.ietf.org/rfc/rfc6901.txt), this command:\n" "[sample input](https://tools.ietf.org/rfc/rfc6901.txt), this command:\n"
" jsonptr -query=/foo/1 < rfc-6901-json-pointer.json\n" " jsonptr -query=/foo/1 < rfc-6901-json-pointer.json\n"
"will print:\n" "will print:\n"
" \"baz\"\n" " \"baz\"\n"
@@ -137,10 +150,16 @@ static const char* usage =
"is also greedy, following the first match for each fragment without\n" "is also greedy, following the first match for each fragment without\n"
"back-tracking. For example, the \"/foo/bar\" query will fail if the root\n" "back-tracking. For example, the \"/foo/bar\" query will fail if the root\n"
"object has multiple \"foo\" children but the first one doesn't have a\n" "object has multiple \"foo\" children but the first one doesn't have a\n"
"\"bar\" child, even if later ones do."; "\"bar\" child, even if later ones do.\n"
"\n"
"The -fail-if-unsandboxed flag causes the program to exit if it does not\n"
"self-impose a sandbox. On Linux, it self-imposes a SECCOMP_MODE_STRICT\n"
"sandbox, regardless of this flag.";
// ---- // ----
bool sandboxed = false;
#define MAX_INDENT 8 #define MAX_INDENT 8
#define INDENT_SPACES_STRING " " #define INDENT_SPACES_STRING " "
#define INDENT_TABS_STRING "\t\t\t\t\t\t\t\t" #define INDENT_TABS_STRING "\t\t\t\t\t\t\t\t"
@@ -418,6 +437,7 @@ struct {
char** remaining_argv; char** remaining_argv;
bool compact; bool compact;
bool fail_if_unsandboxed;
size_t indent; size_t indent;
char* query_c_string; char* query_c_string;
bool tabs; bool tabs;
@@ -451,6 +471,10 @@ parse_flags(int argc, char** argv) {
flags.compact = true; flags.compact = true;
continue; continue;
} }
if (!strcmp(arg, "fail-if-unsandboxed")) {
flags.fail_if_unsandboxed = true;
continue;
}
if (!strncmp(arg, "i=", 2) || !strncmp(arg, "indent=", 7)) { if (!strncmp(arg, "i=", 2) || !strncmp(arg, "indent=", 7)) {
while (*arg++ != '=') { while (*arg++ != '=') {
} }
@@ -507,6 +531,9 @@ initialize_globals(int argc, char** argv) {
ctx = context::none; ctx = context::none;
TRY(parse_flags(argc, argv)); TRY(parse_flags(argc, argv));
if (flags.fail_if_unsandboxed && !sandboxed) {
return "main: unsandboxed";
}
if (flags.remaining_argc > 0) { if (flags.remaining_argc > 0) {
return usage; return usage;
} }
@@ -524,6 +551,10 @@ initialize_globals(int argc, char** argv) {
// ---- // ----
// ignore_return_value suppresses errors from -Wall -Werror.
static void //
ignore_return_value(int ignored) {}
const char* // const char* //
read_src() { read_src() {
if (src.meta.closed) { if (src.meta.closed) {
@@ -533,27 +564,37 @@ read_src() {
if (src.meta.wi >= src.data.len) { if (src.meta.wi >= src.data.len) {
return "main: src buffer is full"; return "main: src buffer is full";
} }
size_t n = fread(src.data.ptr + src.meta.wi, sizeof(uint8_t), while (true) {
src.data.len - src.meta.wi, stdin); const int stdin_fd = 0;
src.meta.wi += n; ssize_t n =
src.meta.closed = feof(stdin); read(stdin_fd, src.data.ptr + src.meta.wi, src.data.len - src.meta.wi);
if ((n == 0) && !src.meta.closed) { if (n >= 0) {
return "main: read error"; src.meta.wi += n;
src.meta.closed = n == 0;
break;
} else if (errno != EINTR) {
return strerror(errno);
}
} }
return nullptr; return nullptr;
} }
const char* // const char* //
flush_dst() { flush_dst() {
size_t n = dst.meta.wi - dst.meta.ri; while (true) {
if (n > 0) { size_t n = dst.meta.wi - dst.meta.ri;
size_t i = fwrite(dst.data.ptr + dst.meta.ri, sizeof(uint8_t), n, stdout); if (n == 0) {
dst.meta.ri += i; break;
if (i != n) { }
return "main: write error"; const int stdout_fd = 1;
ssize_t i = write(stdout_fd, dst.data.ptr + dst.meta.ri, n);
if (i >= 0) {
dst.meta.ri += i;
} else if (errno != EINTR) {
return strerror(errno);
} }
dst.compact();
} }
dst.compact();
return nullptr; return nullptr;
} }
@@ -888,6 +929,9 @@ end_of_data:
} }
// Check that we've exhausted the input. // Check that we've exhausted the input.
if ((src.meta.ri == src.meta.wi) && !src.meta.closed) {
TRY(read_src());
}
if ((src.meta.ri < src.meta.wi) || !src.meta.closed) { if ((src.meta.ri < src.meta.wi) || !src.meta.closed) {
return "main: valid JSON followed by further (unexpected) data"; return "main: valid JSON followed by further (unexpected) data";
} }
@@ -916,7 +960,9 @@ compute_exit_code(const char* status_msg) {
status_msg = "main: internal error: error message is too long"; status_msg = "main: internal error: error message is too long";
n = strnlen(status_msg, 2047); n = strnlen(status_msg, 2047);
} }
fprintf(stderr, "%s\n", status_msg); const int stderr_fd = 2;
ignore_return_value(write(stderr_fd, status_msg, n));
ignore_return_value(write(stderr_fd, "\n", 1));
// Return an exit code of 1 for regular (forseen) errors, e.g. badly // Return an exit code of 1 for regular (forseen) errors, e.g. badly
// formatted or unsupported input. // formatted or unsupported input.
// //
@@ -933,6 +979,11 @@ compute_exit_code(const char* status_msg) {
int // int //
main(int argc, char** argv) { main(int argc, char** argv) {
#if defined(WUFFS_EXAMPLE_USE_SECCOMP)
prctl(PR_SET_SECCOMP, SECCOMP_MODE_STRICT);
sandboxed = true;
#endif
const char* z = main1(argc, argv); const char* z = main1(argc, argv);
if (wrote_to_dst) { if (wrote_to_dst) {
const char* z1 = write_dst("\n", 1); const char* z1 = write_dst("\n", 1);
@@ -940,5 +991,12 @@ main(int argc, char** argv) {
z = z ? z : (z1 ? z1 : z2); z = z ? z : (z1 ? z1 : z2);
} }
int exit_code = compute_exit_code(z); int exit_code = compute_exit_code(z);
#if defined(WUFFS_EXAMPLE_USE_SECCOMP)
// Call SYS_exit explicitly, instead of calling SYS_exit_group implicitly by
// either calling _exit or returning from main. SECCOMP_MODE_STRICT allows
// only SYS_exit.
syscall(SYS_exit, exit_code);
#endif
return exit_code; return exit_code;
} }
+3 -2
View File
@@ -248,8 +248,9 @@ main(int argc, char** argv) {
int exit_code = compute_exit_code(main1(argc, argv)); int exit_code = compute_exit_code(main1(argc, argv));
#if defined(WUFFS_EXAMPLE_USE_SECCOMP) #if defined(WUFFS_EXAMPLE_USE_SECCOMP)
// Call SYS_exit explicitly instead of SYS_exit_group implicitly. // Call SYS_exit explicitly, instead of calling SYS_exit_group implicitly by
// SECCOMP_MODE_STRICT allows only the former. // either calling _exit or returning from main. SECCOMP_MODE_STRICT allows
// only SYS_exit.
syscall(SYS_exit, exit_code); syscall(SYS_exit, exit_code);
#endif #endif
return exit_code; return exit_code;