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
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@ jobs:
bash tests/integration/test_fuzzer_pruned_corpus.sh
bash tests/integration/test_exit_codes.sh
bash tests/integration/test_cwe_mode_mapping.sh
bash tests/integration/test_model_input.sh
'

# ===========================================================
Expand Down
1 change: 1 addition & 0 deletions modules/backend/library/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ list(APPEND MAP2CHECK_C_LIB "BTree")
list(APPEND MAP2CHECK_C_LIB "ContainerBTree")
list(APPEND MAP2CHECK_C_LIB "ContainerRealloc")
list(APPEND MAP2CHECK_C_LIB "HeapLog")
list(APPEND MAP2CHECK_C_LIB "LibcInputModel")
list(APPEND MAP2CHECK_C_LIB "ListLog")
list(APPEND MAP2CHECK_C_LIB "Map2CheckFunctions")
list(APPEND MAP2CHECK_C_LIB "NonDetGeneratorNone")
Expand Down
351 changes: 351 additions & 0 deletions modules/backend/library/lib/LibcInputModel.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,351 @@
/**
* Copyright (C) 2014 - 2020 Map2Check tool
* This file is part of the Map2Check tool, and is made available under
* the terms of the GNU General Public License version 2.
*
* SPDX-License-Identifier: (GPL-2.0)
**/

/* LibcInputModel.c -- the program's input from the outside world, as
* nondeterministic values (--model-input).
*
* Without it, a call such as fgets(buf, n, stdin) is a real call: KLEE runs
* it concretely (stdin is empty, getenv finds nothing, connect is refused),
* AFL++ only varies the __VERIFIER_nondet_* values, and the program takes one
* path, the one with no input. A flaw that needs input is then never reached
* and the verdict is TRUE (Juliet CWE-761: 80 of 100 flawed programs).
*
* NonDetPass retargets the program's calls to these functions (input:
* map2check_input_fgets, ...). Each one delivers what the real function could
* deliver, with its content and length taken from map2check_non_det_*: KLEE
* makes them symbolic, AFL++ reads them from its input, the replay of a test
* reads them back. This is how --wasm models WASI's fd_read
* (WasmRuntimeStubs.c).
*
* The input a run can receive is bounded: map2check_input_max bytes over all
* calls (--input-bound; NonDetPass defines the global in the program). A TRUE
* with --model-input holds for inputs of at most that size. Every byte is a
* branch for the program that reads it, so the bound decides whether KLEE can
* exhaust the paths and prove a program safe; and a flaw that needs a longer
* input ("2147483647" for atoi) is out of reach below it. Numbers read by
* fscanf take no bytes: they get their whole range. */

#include <errno.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <wchar.h>

#include "../header/NonDetGenerator.h"

/* Strings with no bound of their own (%s, getenv) take at most this much. */
#define M2C_INPUT_MAX 16u

extern const unsigned map2check_input_max;
static unsigned input_left = 0;
static int input_started = 0;

static void input_start(void) {
if (input_started) return;
input_started = 1;
input_left = map2check_input_max;
}

/* A length in [0, max], max capped by what is left of the budget. */
static size_t input_length(size_t max) {
input_start();
if (max > input_left) max = input_left;
size_t length = map2check_non_det_unsigned() % (max + 1);
input_left -= (unsigned)length;
return length;
}

/* --- Character streams ---------------------------------------------------- */

char *map2check_input_fgets(char *s, int n, FILE *stream) {
(void)stream;
if (s == NULL || n <= 0) return NULL;
size_t length = input_length((size_t)n - 1);
if (length == 0) return NULL; /* end of file before any character */
for (size_t i = 0; i < length; i++) {
s[i] = (char)map2check_non_det_uchar();
if (s[i] == '\n') { /* fgets stops after a newline */
input_left += (unsigned)(length - i - 1);
length = i + 1;
break;
}
}
s[length] = '\0';
return s;
}

int map2check_input_fgetc(FILE *stream) {
(void)stream;
if (input_length(1) == 0) return EOF;
return (int)map2check_non_det_uchar();
}

int map2check_input_getchar(void) { return map2check_input_fgetc(stdin); }

size_t map2check_input_fread(void *ptr, size_t size, size_t nmemb,
FILE *stream) {
(void)stream;
if (ptr == NULL || size == 0 || nmemb == 0) return 0;
input_start();
size_t max_items = input_left / size;
if (max_items > nmemb) max_items = nmemb;
size_t items = map2check_non_det_unsigned() % (max_items + 1);
input_left -= (unsigned)(items * size);
unsigned char *bytes = (unsigned char *)ptr;
for (size_t i = 0; i < items * size; i++) bytes[i] = map2check_non_det_uchar();
return items;
}

/* --- Wide character streams ----------------------------------------------- */

wchar_t *map2check_input_fgetws(wchar_t *ws, int n, FILE *stream) {
(void)stream;
if (ws == NULL || n <= 0) return NULL;
size_t length = input_length((size_t)n - 1);
if (length == 0) return NULL;
for (size_t i = 0; i < length; i++) {
ws[i] = (wchar_t)map2check_non_det_uint();
if (ws[i] == L'\n') {
input_left += (unsigned)(length - i - 1);
length = i + 1;
break;
}
}
ws[length] = L'\0';
return ws;
}

wint_t map2check_input_fgetwc(FILE *stream) {
(void)stream;
if (input_length(1) == 0) return WEOF;
return (wint_t)map2check_non_det_uint();
}

wint_t map2check_input_getwchar(void) { return map2check_input_fgetwc(stdin); }

/* --- Formatted input ------------------------------------------------------ */

/* Assigns every conversion of the format a nondeterministic value, or
* returns EOF having assigned none. A partial match is not modelled. Strings
* (%s, %[, %c) take their length from the input budget. */
static int input_vscanf(const char *format, va_list ap) {
if ((int)map2check_non_det_int() < 0) return EOF;
int assigned = 0;
for (const char *f = format; f != NULL && *f != '\0'; f++) {
if (*f != '%') continue;
f++;
if (*f == '%') continue;
int suppress = 0;
if (*f == '*') {
suppress = 1;
f++;
}
size_t width = 0;
while (*f >= '0' && *f <= '9') width = width * 10 + (size_t)(*f++ - '0');
int length = 0; /* -2 hh, -1 h, 0, 1 l, 2 ll/j/q, 3 z/t, 4 L */
for (;; f++) {
if (*f == 'h') length = length == -1 ? -2 : -1;
else if (*f == 'l') length = length == 1 ? 2 : 1;
else if (*f == 'j' || *f == 'q') length = 2;
else if (*f == 'z' || *f == 't') length = 3;
else if (*f == 'L') length = 4;
else break;
}
char conversion = *f;
if (conversion == '\0') break;
if (conversion == '[') { /* skip the set: a leading ] belongs to it */
f++;
if (*f == '^') f++;
if (*f == ']') f++;
while (*f != '\0' && *f != ']') f++;
if (*f == '\0') break;
}
if (suppress) continue;
switch (conversion) {
case 'd':
case 'i':
case 'u':
case 'o':
case 'x':
case 'X':
if (length == -2) *va_arg(ap, char *) = (char)map2check_non_det_char();
else if (length == -1) *va_arg(ap, short *) = map2check_non_det_short();
else if (length == 1) *va_arg(ap, long *) = map2check_non_det_long();
else if (length == 2)
*va_arg(ap, long long *) = map2check_non_det_longlong();
else if (length == 3)
*va_arg(ap, size_t *) = map2check_non_det_size_t();
else *va_arg(ap, int *) = map2check_non_det_int();
break;
case 'f':
case 'F':
case 'e':
case 'E':
case 'g':
case 'G':
case 'a':
case 'A':
if (length == 1) *va_arg(ap, double *) = map2check_non_det_double();
else if (length == 4)
*va_arg(ap, long double *) = map2check_non_det_double();
else *va_arg(ap, float *) = map2check_non_det_float();
break;
case 'c': {
size_t count = width == 0 ? 1 : width;
if (length == 1) {
wchar_t *ws = va_arg(ap, wchar_t *);
for (size_t i = 0; i < count; i++)
ws[i] = (wchar_t)map2check_non_det_uint();
} else {
char *s = va_arg(ap, char *);
for (size_t i = 0; i < count; i++)
s[i] = (char)map2check_non_det_uchar();
}
break;
}
case 's':
case '[': {
/* Without a width a real %s has no bound: the budget gives one. */
size_t count = input_length(width == 0 ? M2C_INPUT_MAX : width);
if (length == 1) {
wchar_t *ws = va_arg(ap, wchar_t *);
for (size_t i = 0; i < count; i++)
ws[i] = (wchar_t)map2check_non_det_uint();
ws[count] = L'\0';
} else {
char *s = va_arg(ap, char *);
for (size_t i = 0; i < count; i++)
s[i] = (char)map2check_non_det_uchar();
s[count] = '\0';
}
break;
}
case 'p':
*va_arg(ap, void **) = NULL;
break;
case 'n':
*va_arg(ap, int *) = 0;
continue; /* %n is not counted as an assignment */
default:
continue;
}
assigned++;
}
return assigned;
}

int map2check_input_fscanf(FILE *stream, const char *format, ...) {
(void)stream;
va_list ap;
va_start(ap, format);
int result = input_vscanf(format, ap);
va_end(ap);
return result;
}

int map2check_input_scanf(const char *format, ...) {
va_list ap;
va_start(ap, format);
int result = input_vscanf(format, ap);
va_end(ap);
return result;
}

/* --- File descriptors and sockets ----------------------------------------- */

ssize_t map2check_input_read(int fd, void *buf, size_t count) {
(void)fd;
if (buf == NULL) return 0;
size_t length = input_length(count);
unsigned char *bytes = (unsigned char *)buf;
for (size_t i = 0; i < length; i++) bytes[i] = map2check_non_det_uchar();
return (ssize_t)length; /* 0 is end of file, or the peer closing */
}

ssize_t map2check_input_recv(int fd, void *buf, size_t len, int flags) {
(void)flags;
return map2check_input_read(fd, buf, len);
}

ssize_t map2check_input_recvfrom(int fd, void *buf, size_t len, int flags,
struct sockaddr *from, socklen_t *fromlen) {
(void)flags;
(void)from;
(void)fromlen;
return map2check_input_read(fd, buf, len);
}

/* A peer is always there: the connection succeeds, and accept returns a new
* descriptor at once. Without this, connect is refused and the input never
* read, and accept waits for a client that never comes until the time
* limit. */
int map2check_input_connect(int fd, const struct sockaddr *addr,
socklen_t len) {
(void)fd;
(void)addr;
(void)len;
return 0;
}

int map2check_input_bind(int fd, const struct sockaddr *addr, socklen_t len) {
(void)fd;
(void)addr;
(void)len;
return 0;
}

int map2check_input_listen(int fd, int backlog) {
(void)fd;
(void)backlog;
return 0;
}

int map2check_input_accept(int fd, struct sockaddr *addr, socklen_t *len) {
(void)addr;
(void)len;
int peer = dup(fd);
return peer >= 0 ? peer : fd;
}

/* A file opened for reading exists. When the real one does not, the stream
* is /dev/null: the reads are modelled above, whatever the stream, and the
* program gets a FILE it can close. */
FILE *map2check_input_fopen(const char *path, const char *mode) {
FILE *file = fopen(path, mode);
if (file == NULL && mode[0] == 'r' && strchr(mode, '+') == NULL)
file = fopen("/dev/null", mode);
return file;
}

/* --- Environment ---------------------------------------------------------- */

/* Unset, or a nondeterministic string. Four buffers in turn, so that a
* program may hold a few values at once; each ends in enough zero bytes for
* a wchar_t reading (Juliet's wide variants call getenv on Linux). */
#define M2C_ENV_SLOTS 4
static char env_values[M2C_ENV_SLOTS][M2C_INPUT_MAX + sizeof(wchar_t)];
static unsigned env_next = 0;

char *map2check_input_getenv(const char *name) {
(void)name;
if (map2check_non_det_bool()) return NULL;
char *value = env_values[env_next++ % M2C_ENV_SLOTS];
size_t length = input_length(M2C_INPUT_MAX);
memset(value, 0, M2C_INPUT_MAX + sizeof(wchar_t));
for (size_t i = 0; i < length; i++) {
value[i] = (char)map2check_non_det_uchar();
if (value[i] == '\0') break; /* the string ends there */
}
return value;
}
Loading
Loading