1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
|
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <values.h>
#include "arch.h"
#include "debug.h"
#include "files.h"
#include "maps.h"
#include "net.h"
#include "pathnames.h"
#include "random.h"
#include "sanitise.h"
#include "shm.h"
#include "syscall.h"
#include "tables.h"
#include "trinity.h" // num_online_cpus
static int get_cpu(void)
{
int i;
i = rnd() % 100;
switch (i) {
case 0: return -1;
case 1: return rnd() % 4096;
case 2: return INT_MAX;
case 3 ... 98:
return rnd() % num_online_cpus;
}
return 0;
}
static unsigned long handle_arg_address(struct syscallrecord *rec, unsigned int argnum)
{
unsigned long addr = 0;
if (argnum == 1)
return (unsigned long) get_address();
if (RAND_BOOL())
return (unsigned long) get_address();
/* Half the time, we look to see if earlier args were also ARG_ADDRESS,
* and munge that instead of returning a new one from get_address() */
addr = find_previous_arg_address(rec, argnum);
switch (rnd() % 4) {
case 0: break; /* return unmodified */
case 1: addr++;
break;
case 2: addr+= sizeof(int);
break;
case 3: addr+= sizeof(long);
break;
}
return addr;
}
static unsigned long handle_arg_range(struct syscallentry *entry, unsigned int argnum)
{
unsigned long i;
unsigned long low = 0, high = 0;
switch (argnum) {
case 1: low = entry->low1range;
high = entry->hi1range;
break;
case 2: low = entry->low2range;
high = entry->hi2range;
break;
case 3: low = entry->low3range;
high = entry->hi3range;
break;
case 4: low = entry->low4range;
high = entry->hi4range;
break;
case 5: low = entry->low5range;
high = entry->hi5range;
break;
case 6: low = entry->low6range;
high = entry->hi6range;
break;
}
if (high == 0) {
outputerr("%s forgets to set hirange!\n", entry->name);
BUG("Fix syscall definition!\n");
}
i = (unsigned long) rand64() % high;
if (i < low) {
i += low;
i &= high;
}
return i;
}
static void get_num_and_values(struct syscallentry *entry, unsigned int argnum,
unsigned int *num, const unsigned long **values)
{
switch (argnum) {
case 1: *num = entry->arg1list.num;
*values = entry->arg1list.values;
break;
case 2: *num = entry->arg2list.num;
*values = entry->arg2list.values;
break;
case 3: *num = entry->arg3list.num;
*values = entry->arg3list.values;
break;
case 4: *num = entry->arg4list.num;
*values = entry->arg4list.values;
break;
case 5: *num = entry->arg5list.num;
*values = entry->arg5list.values;
break;
case 6: *num = entry->arg6list.num;
*values = entry->arg6list.values;
break;
default:
unreachable();
}
if (*num == 0)
BUG("ARG_OP/LIST with 0 args. What?\n");
if (*values == NULL)
BUG("ARG_OP/LIST with no values.\n");
}
/*
* Get a single entry from the list of values.
*/
static unsigned long handle_arg_op(struct syscallentry *entry, unsigned int argnum)
{
const unsigned long *values = NULL;
unsigned int num = 0;
unsigned long op = 0;
get_num_and_values(entry, argnum, &num, &values);
op = values[rnd() % num];
return op;
}
/*
* OR a random number of bits from the list of values into a bitmask, and return it.
*/
static unsigned long handle_arg_list(struct syscallentry *entry, unsigned int argnum)
{
unsigned long mask = 0;
unsigned int num = 0;
const unsigned long *values = NULL;
get_num_and_values(entry, argnum, &num, &values);
if (RAND_BOOL())
num = min(num, 3U);
mask = set_rand_bitmask(num, values);
return mask;
}
static unsigned long handle_arg_iovec(struct syscallentry *entry, struct syscallrecord *rec, unsigned int argnum)
{
unsigned long num_entries;
if (RAND_BOOL())
num_entries = 1;
else
num_entries = RAND_RANGE(1, 256);
switch (argnum) {
case 1: if (entry->arg2type == ARG_IOVECLEN)
rec->a2 = num_entries;
break;
case 2: if (entry->arg3type == ARG_IOVECLEN)
rec->a3 = num_entries;
break;
case 3: if (entry->arg4type == ARG_IOVECLEN)
rec->a4 = num_entries;
break;
case 4: if (entry->arg5type == ARG_IOVECLEN)
rec->a5 = num_entries;
break;
case 5: if (entry->arg6type == ARG_IOVECLEN)
rec->a6 = num_entries;
break;
}
return (unsigned long) alloc_iovec(num_entries);
}
static unsigned long handle_arg_sockaddr(struct syscallentry *entry, struct syscallrecord *rec, unsigned int argnum)
{
struct sockaddr *sockaddr = NULL;
socklen_t sockaddrlen = 0;
generate_sockaddr((struct sockaddr **)&sockaddr, &sockaddrlen, PF_NOHINT);
switch (argnum) {
case 1: if (entry->arg2type == ARG_SOCKADDRLEN)
rec->a2 = sockaddrlen;
break;
case 2: if (entry->arg3type == ARG_SOCKADDRLEN)
rec->a3 = sockaddrlen;
break;
case 3: if (entry->arg4type == ARG_SOCKADDRLEN)
rec->a4 = sockaddrlen;
break;
case 4: if (entry->arg5type == ARG_SOCKADDRLEN)
rec->a5 = sockaddrlen;
break;
case 5: if (entry->arg6type == ARG_SOCKADDRLEN)
rec->a6 = sockaddrlen;
break;
case 6:
break;
}
return (unsigned long) sockaddr;
}
static unsigned long handle_arg_mode_t(void)
{
unsigned int i, count;
mode_t mode = 0, op = 0;
count = rnd() % 9;
for (i = 0; i < count; i++) {
unsigned int j;
j = rnd() % 16;
switch (j) {
case 0: op = S_IRWXU; break;
case 1: op = S_IRUSR; break;
case 2: op = S_IWUSR; break;
case 3: op = S_IXUSR; break;
case 4: op = S_IRWXG; break;
case 5: op = S_IRGRP; break;
case 6: op = S_IWGRP; break;
case 7: op = S_IXGRP; break;
case 8: op = S_IRWXO; break;
case 9: op = S_IROTH; break;
case 10: op = S_IWOTH; break;
case 11: op = S_IXOTH; break;
case 12: op = S_ISUID; break;
case 13: op = S_ISGID; break;
case 14: op = S_ISVTX; break;
}
if (RAND_BOOL())
mode |= op;
else
mode &= ~op;
}
return mode;
}
enum argtype get_argtype(struct syscallentry *entry, unsigned int argnum)
{
enum argtype argtype = 0;
switch (argnum) {
case 1: argtype = entry->arg1type;
break;
case 2: argtype = entry->arg2type;
break;
case 3: argtype = entry->arg3type;
break;
case 4: argtype = entry->arg4type;
break;
case 5: argtype = entry->arg5type;
break;
case 6: argtype = entry->arg6type;
break;
}
return argtype;
}
static unsigned long fill_arg(struct syscallrecord *rec, unsigned int argnum)
{
struct syscallentry *entry;
unsigned int call;
enum argtype argtype;
call = rec->nr;
entry = syscalls[call].entry;
if (argnum > entry->num_args)
return 0;
argtype = get_argtype(entry, argnum);
switch (argtype) {
case ARG_UNDEFINED:
if (RAND_BOOL())
return (unsigned long) rand64();
return (unsigned long) get_writable_address(page_size);
case ARG_FD:
if (RAND_BOOL()) {
unsigned int i;
/* If this is the 2nd or more ARG_FD, make it unique */
for (i = 0; i < argnum; i++) {
enum argtype arg;
arg = get_argtype(entry, i);
if (arg == ARG_FD)
return get_new_random_fd();
}
}
return get_random_fd();
case ARG_LEN:
return (unsigned long) get_len();
case ARG_ADDRESS:
return handle_arg_address(rec, argnum);
case ARG_NON_NULL_ADDRESS:
return (unsigned long) get_non_null_address();
case ARG_MMAP:
return (unsigned long) get_map();
case ARG_PID:
return (unsigned long) get_pid();
case ARG_RANGE:
return handle_arg_range(entry, argnum);
case ARG_OP: /* Like ARG_LIST, but just a single value. */
return handle_arg_op(entry, argnum);
case ARG_LIST:
return handle_arg_list(entry, argnum);
case ARG_CPU:
return (unsigned long) get_cpu();
case ARG_PATHNAME:
return (unsigned long) generate_pathname();
case ARG_IOVEC:
return handle_arg_iovec(entry, rec, argnum);
case ARG_IOVECLEN:
case ARG_SOCKADDRLEN:
/* We already set the len in the ARG_IOVEC/ARG_SOCKADDR case
* So here we just return what we had set there. */
return get_argval(rec, argnum);
case ARG_SOCKADDR:
return handle_arg_sockaddr(entry, rec, argnum);
case ARG_MODE_T:
return handle_arg_mode_t();
case ARG_SOCKETINFO:
return (unsigned long) get_rand_socketinfo();
}
BUG("unreachable!\n");
}
void generic_sanitise(struct syscallrecord *rec)
{
struct syscallentry *entry;
unsigned int call;
call = rec->nr;
entry = syscalls[call].entry;
if (entry->arg1type != 0)
rec->a1 = fill_arg(rec, 1);
if (entry->arg2type != 0)
rec->a2 = fill_arg(rec, 2);
if (entry->arg3type != 0)
rec->a3 = fill_arg(rec, 3);
if (entry->arg4type != 0)
rec->a4 = fill_arg(rec, 4);
if (entry->arg5type != 0)
rec->a5 = fill_arg(rec, 5);
if (entry->arg6type != 0)
rec->a6 = fill_arg(rec, 6);
}
void generic_free_arg(struct syscallrecord *rec)
{
struct syscallentry *entry;
unsigned int i, call;
call = rec->nr;
entry = syscalls[call].entry;
for_each_arg(entry, i) {
enum argtype argtype;
argtype = get_argtype(entry, i);
if (argtype == ARG_IOVEC)
free((void *) get_argval(rec, i));
}
}
void generate_syscall_args(struct syscallrecord *rec)
{
struct syscallentry *entry;
lock(&rec->lock);
//TODO: instead of rand64() do a rand arg type
entry = syscalls[rec->nr].entry;
rec->state = PREP;
if (entry->arg1type == ARG_UNDEFINED)
rec->a1 = (unsigned long) rand64();
if (entry->arg2type == ARG_UNDEFINED)
rec->a2 = (unsigned long) rand64();
if (entry->arg3type == ARG_UNDEFINED)
rec->a3 = (unsigned long) rand64();
if (entry->arg4type == ARG_UNDEFINED)
rec->a4 = (unsigned long) rand64();
if (entry->arg5type == ARG_UNDEFINED)
rec->a5 = (unsigned long) rand64();
if (entry->arg6type == ARG_UNDEFINED)
rec->a6 = (unsigned long) rand64();
generic_sanitise(rec);
if (entry->sanitise)
entry->sanitise(rec);
unlock(&rec->lock);
}
|