| 1 |
/********************************************************************** |
| 2 |
* pscan: http://www.striker.ottawa.on.ca/~aland/pscan/ |
| 3 |
* |
| 4 |
* Copyright (C) 2000,2007 Alan DeKok <[email protected]> |
| 5 |
* |
| 6 |
* This program is free software; you can redistribute it and/or modify |
| 7 |
* it under the terms of the GNU General Public License as published by |
| 8 |
* the Free Software Foundation; either version 2 of the License, or |
| 9 |
* (at your option) any later version. |
| 10 |
* |
| 11 |
* This program is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with this program; if not, write to the Free Software |
| 18 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
| 19 |
* |
| 20 |
**********************************************************************/ |
| 21 |
#include <stdio.h> |
| 22 |
#include <stdlib.h> |
| 23 |
#include <string.h> |
| 24 |
#include <errno.h> |
| 25 |
#include <errno.h> |
| 26 |
#include <assert.h> |
| 27 |
#include <unistd.h> |
| 28 |
|
| 29 |
#include "pscan.h" |
| 30 |
|
| 31 |
extern int yylex(); |
| 32 |
extern FILE *yyout, *yyin; |
| 33 |
int cur_lineno=1; |
| 34 |
|
| 35 |
/* |
| 36 |
* This function does nothing useful. |
| 37 |
*/ |
| 38 |
int yywrap() |
| 39 |
{ |
| 40 |
return(1); |
| 41 |
} |
| 42 |
|
| 43 |
static int verbose = 0; |
| 44 |
static int warnings = FALSE; |
| 45 |
static char *filename; |
| 46 |
|
| 47 |
#define FSM_MAX_STACK 8192 |
| 48 |
static parser_state_t fsm_stack[FSM_MAX_STACK]; |
| 49 |
static int stack_index = 0; |
| 50 |
parser_state_t *state = NULL; |
| 51 |
|
| 52 |
static int total_errors = 0; |
| 53 |
static int total_warnings = 0; |
| 54 |
|
| 55 |
/* |
| 56 |
* The statically defined list of problem functions, |
| 57 |
* common to most C libraries. |
| 58 |
*/ |
| 59 |
static problem_t problem_functions[] = { |
| 60 |
{ "vsprintf", 1}, |
| 61 |
{ "vfprintf", 1}, |
| 62 |
{ "vprintf", 0}, |
| 63 |
{ "vsnprintf", 2}, |
| 64 |
{ "snprintf", 2}, |
| 65 |
{ "sprintf", 1}, |
| 66 |
{ "fprintf", 1}, |
| 67 |
{ "fscanf", 1}, |
| 68 |
{ "printf", 0}, |
| 69 |
{ "scanf", 0}, |
| 70 |
{ "sscanf", 1}, |
| 71 |
{ "syslog", 1}, |
| 72 |
{ "setproctitle", 0}, |
| 73 |
{ "err", 1}, |
| 74 |
{ "verr", 1}, |
| 75 |
{ "errx", 1}, |
| 76 |
{ "verrx", 1}, |
| 77 |
{ "warn", 0}, |
| 78 |
{ "vwarn", 0}, |
| 79 |
{ "warnx", 0}, |
| 80 |
{ "vwarnx", 0}, |
| 81 |
|
| 82 |
{ NULL, 0} |
| 83 |
}; |
| 84 |
|
| 85 |
/* |
| 86 |
* User-supplied problem functions. |
| 87 |
* |
| 88 |
* Yes, this is only a static buffer, but I'm too lazy to code |
| 89 |
* a proper malloc/linked list replacement. Sue me. This works. |
| 90 |
*/ |
| 91 |
static problem_t user_problem_functions[MAX_USER_PROBLEMS]; |
| 92 |
static int user_problems = 0; |
| 93 |
|
| 94 |
/* |
| 95 |
* Print out a usage string. |
| 96 |
*/ |
| 97 |
static void usage(void) |
| 98 |
{ |
| 99 |
fprintf(stderr, "Usage: pscan [-vw] [-p problem_file] <files ...>\n"); |
| 100 |
fprintf(stderr, "Attempts to discover a number of common security abuses in C source files.\n\n"); |
| 101 |
fprintf(stderr, " -v Verbose mode. \n"); |
| 102 |
fprintf(stderr, " -w Show warnings when a variable is used as the format argument.\n"); |
| 103 |
fprintf(stderr, " -p file Read additional problem <function,offset> definitions from <file>.\n"); |
| 104 |
exit(1); |
| 105 |
} |
| 106 |
|
| 107 |
/* |
| 108 |
* Read a problem file. This consists of a function name, |
| 109 |
* and an offset of the format string. |
| 110 |
*/ |
| 111 |
void read_problem_file(const char *file) |
| 112 |
{ |
| 113 |
FILE *fp; |
| 114 |
char buffer[1024]; |
| 115 |
char name[1024]; |
| 116 |
int num, offset; |
| 117 |
int line; |
| 118 |
char *p; |
| 119 |
|
| 120 |
/* |
| 121 |
* Can we open the file that they gave us? |
| 122 |
*/ |
| 123 |
fp = fopen(file, "r"); |
| 124 |
if (!fp) { |
| 125 |
fprintf(stderr, "pscan: Error opening %s: %s\n", |
| 126 |
file, strerror(errno)); |
| 127 |
exit(1); |
| 128 |
} |
| 129 |
|
| 130 |
/* |
| 131 |
* Loop over the input file |
| 132 |
*/ |
| 133 |
line = 0; |
| 134 |
while (fgets(buffer, sizeof(buffer), fp)) { |
| 135 |
line++; |
| 136 |
|
| 137 |
/* |
| 138 |
* Sanity check the input buffer. |
| 139 |
*/ |
| 140 |
if (strchr(buffer, '\n') == NULL) { |
| 141 |
fprintf(stderr, "pscan: %s:%d: Input line too long\n", file, line); |
| 142 |
exit(1); |
| 143 |
} |
| 144 |
|
| 145 |
/* |
| 146 |
* Ignore leading whitespace. |
| 147 |
*/ |
| 148 |
for (p = buffer; (*p == ' ') || (*p == '\t'); p++) |
| 149 |
/* nothing */; |
| 150 |
|
| 151 |
/* |
| 152 |
* Skip blank lines and comments |
| 153 |
*/ |
| 154 |
if ((*p == '\n') || (*p == '#')) { |
| 155 |
continue; |
| 156 |
} |
| 157 |
|
| 158 |
/* |
| 159 |
* Check for stupid buffer over-flows. |
| 160 |
*/ |
| 161 |
if (user_problems >= MAX_USER_PROBLEMS) { |
| 162 |
fprintf(stderr, "pscan: Too many user-defined problem functions.\n"); |
| 163 |
exit(1); |
| 164 |
} |
| 165 |
|
| 166 |
/* |
| 167 |
* Scan for the filename & line. |
| 168 |
*/ |
| 169 |
num = sscanf(buffer, "%s%d", name, &offset); |
| 170 |
if (num != 2) { |
| 171 |
fprintf(stderr, "pscan: %s:%d: Expected 2 parameters, got %d\n", |
| 172 |
file, line, num); |
| 173 |
exit(1); |
| 174 |
} |
| 175 |
|
| 176 |
/* |
| 177 |
* Copy the function/offset into our data structure. |
| 178 |
*/ |
| 179 |
user_problem_functions[user_problems].function = strdup(name); |
| 180 |
user_problem_functions[user_problems].fmt_arg = offset; |
| 181 |
user_problems++; |
| 182 |
} |
| 183 |
|
| 184 |
fclose(fp); |
| 185 |
} |
| 186 |
|
| 187 |
/* |
| 188 |
* main, where everything happens. |
| 189 |
*/ |
| 190 |
int main(int argc,char **argv) |
| 191 |
{ |
| 192 |
int i; |
| 193 |
int argval; |
| 194 |
|
| 195 |
/* |
| 196 |
* Get command-line options. |
| 197 |
*/ |
| 198 |
while ((argval = getopt(argc, argv, "hp:vw")) != EOF) { |
| 199 |
switch (argval) { |
| 200 |
|
| 201 |
default: |
| 202 |
case 'h': |
| 203 |
usage(); |
| 204 |
break; |
| 205 |
|
| 206 |
case 'v': |
| 207 |
verbose= TRUE; |
| 208 |
break; |
| 209 |
|
| 210 |
case 'w': |
| 211 |
warnings = TRUE; |
| 212 |
break; |
| 213 |
|
| 214 |
case 'p': |
| 215 |
read_problem_file(optarg); |
| 216 |
break; |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
/* |
| 221 |
* Sanity check arguments, to be sure there's at least one file we |
| 222 |
* can open. |
| 223 |
*/ |
| 224 |
if (optind == argc) { |
| 225 |
usage(); |
| 226 |
} |
| 227 |
|
| 228 |
/* |
| 229 |
* Loop over the input files, scanning them for problems. |
| 230 |
*/ |
| 231 |
for (i = optind; i < argc; i++) { |
| 232 |
|
| 233 |
/* |
| 234 |
* Initialize the stack, throwing away anything from the last file. |
| 235 |
*/ |
| 236 |
stack_index = 0; |
| 237 |
state = &fsm_stack[0]; |
| 238 |
filename = argv[i]; |
| 239 |
|
| 240 |
/* |
| 241 |
* Initialize the current state. |
| 242 |
*/ |
| 243 |
state->problem = NULL; |
| 244 |
state->line = 0; |
| 245 |
state->args = -1; |
| 246 |
state->constant_string = -1; |
| 247 |
state->last_token = NOT_PROBLEMATIC; |
| 248 |
state->braces = 0; |
| 249 |
|
| 250 |
/* Open the source file for reading */ |
| 251 |
if ((yyin = fopen(filename, "r")) == NULL) { |
| 252 |
fprintf(stderr, "pscan: Error opening %s: %s.\n", filename, |
| 253 |
strerror(errno)); |
| 254 |
printf("%s\n", strerror(errno)); |
| 255 |
exit(1); |
| 256 |
} |
| 257 |
yyout = NULL; |
| 258 |
|
| 259 |
/* |
| 260 |
* Initialize our variables. |
| 261 |
*/ |
| 262 |
cur_lineno = 1; |
| 263 |
|
| 264 |
if (verbose) { |
| 265 |
printf("Scanning %s ...\n", filename); |
| 266 |
} |
| 267 |
|
| 268 |
/* |
| 269 |
* Let the lexer parse the whole file. |
| 270 |
*/ |
| 271 |
while (yylex()) |
| 272 |
; |
| 273 |
|
| 274 |
/* close the input file */ |
| 275 |
fclose(yyin); |
| 276 |
} |
| 277 |
|
| 278 |
/* |
| 279 |
* And finally, print out a summary of the total problems. |
| 280 |
*/ |
| 281 |
if (verbose) { |
| 282 |
if (total_errors != 0 ) { |
| 283 |
printf("Total problems identified: %d\n", total_errors); |
| 284 |
} |
| 285 |
|
| 286 |
if (warnings) { |
| 287 |
if (total_warnings != 0 ) { |
| 288 |
printf("Warnings: %d\n",total_warnings); |
| 289 |
} |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
if (total_errors!=0) |
| 294 |
exit(1); |
| 295 |
else |
| 296 |
exit(0); |
| 297 |
} |
| 298 |
|
| 299 |
/* |
| 300 |
* Check the number of arguments to the function, and was the LAST |
| 301 |
* argument a constant string? |
| 302 |
*/ |
| 303 |
void check_function(parser_state_t *state) |
| 304 |
{ |
| 305 |
int erroneous=0; |
| 306 |
|
| 307 |
assert(state != NULL); |
| 308 |
/* |
| 309 |
* This was a reference to a function WITHOUT an opening brace, |
| 310 |
* so it's not a function call. Ignore it. |
| 311 |
*/ |
| 312 |
if (state->args < 0) { |
| 313 |
return; |
| 314 |
} |
| 315 |
|
| 316 |
/* |
| 317 |
* The problem function has the SAME number of arguments as the |
| 318 |
* placement of the format argument. i.e. The LAST argument of the |
| 319 |
* function is the format string. |
| 320 |
* |
| 321 |
* If the last argument of the function is a constant string, then |
| 322 |
* there can't be any security problems, so don't complain. |
| 323 |
* |
| 324 |
* Otherwise, print out a complaint noting the source file, |
| 325 |
* line number, and function name. |
| 326 |
*/ |
| 327 |
if ((state->problem->fmt_arg == state->args) && |
| 328 |
(state->constant_string != state->problem->fmt_arg)) { |
| 329 |
printf("%s:%d SECURITY: %s call should have \"%%s\" as argument %d\n", |
| 330 |
filename, |
| 331 |
state->line, |
| 332 |
state->problem->function, |
| 333 |
state->problem->fmt_arg); |
| 334 |
erroneous=1; |
| 335 |
|
| 336 |
} |
| 337 |
|
| 338 |
if (warnings && |
| 339 |
(state->constant_string != state->problem->fmt_arg)) { |
| 340 |
printf("%s:%d Warning: %s uses non-constant string for format argument %d.\n", |
| 341 |
filename, |
| 342 |
state->line, |
| 343 |
state->problem->function, |
| 344 |
state->problem->fmt_arg); |
| 345 |
total_warnings++; |
| 346 |
} |
| 347 |
|
| 348 |
if (verbose) { |
| 349 |
/* |
| 350 |
* verbose =TRUE, print out more stuff. |
| 351 |
*/ |
| 352 |
printf("%s:%d FUNC %s ", filename, state->line, |
| 353 |
state->problem->function); |
| 354 |
if (state->problem->fmt_arg == state->args) { |
| 355 |
printf("Last argument is "); |
| 356 |
if (state->constant_string==state->args) { |
| 357 |
printf("constant string: OK\n"); |
| 358 |
} else { |
| 359 |
printf("variable or reference: BAD\n"); |
| 360 |
erroneous=1; |
| 361 |
} |
| 362 |
} else { |
| 363 |
printf("format string with %d parameters: OK\n", |
| 364 |
state->args - state->problem->fmt_arg); |
| 365 |
} |
| 366 |
} |
| 367 |
|
| 368 |
if (erroneous) total_errors++; |
| 369 |
|
| 370 |
} |
| 371 |
|
| 372 |
/* |
| 373 |
* Scan the current token to see if it's on the list of problem |
| 374 |
* functions that we care about. |
| 375 |
*/ |
| 376 |
parser_state_t *setup_checks(const char *name, parser_state_t *state) |
| 377 |
{ |
| 378 |
problem_t *problem; |
| 379 |
int i; |
| 380 |
|
| 381 |
/* |
| 382 |
* If there are user-defined problems, use them FIRST. |
| 383 |
* |
| 384 |
* This allows us to handle name conflicts with internally defined |
| 385 |
* problem functions |
| 386 |
*/ |
| 387 |
if (user_problems != 0) { |
| 388 |
/* |
| 389 |
* Loop over the list of problem functions, seeing if we have a match. |
| 390 |
*/ |
| 391 |
for (i = 0; i < user_problems; i++) { |
| 392 |
problem = &user_problem_functions[i]; |
| 393 |
|
| 394 |
/* |
| 395 |
* We have a match! Set up the current stack, and return. |
| 396 |
*/ |
| 397 |
if (strcmp(problem->function, name) == 0) { |
| 398 |
|
| 399 |
/* |
| 400 |
* Ignore user-defined problems with -1 as a format parameter. |
| 401 |
*/ |
| 402 |
if (problem->fmt_arg == -1) { |
| 403 |
return state; |
| 404 |
} |
| 405 |
|
| 406 |
if (state->last_token == PROBLEMATIC) { |
| 407 |
state = push_stack(state); |
| 408 |
} |
| 409 |
|
| 410 |
state->problem = problem; |
| 411 |
state->line = cur_lineno; |
| 412 |
state->braces = 0; |
| 413 |
state->args = -1; |
| 414 |
state->constant_string = -1; |
| 415 |
state->last_token = PROBLEMATIC; |
| 416 |
return state; |
| 417 |
} |
| 418 |
} /* end of loop over user defined problems */ |
| 419 |
} /* end of any user defined problems */ |
| 420 |
|
| 421 |
/* |
| 422 |
* Loop over the list of problem functions, seeing if we have a match. |
| 423 |
*/ |
| 424 |
for (problem = &problem_functions[0]; problem->function != NULL; problem++) { |
| 425 |
|
| 426 |
/* |
| 427 |
* We have a match! Set up the current stack, and return. |
| 428 |
*/ |
| 429 |
if (strcmp(problem->function, name) == 0) { |
| 430 |
if (state->last_token == PROBLEMATIC) { |
| 431 |
state = push_stack(state); |
| 432 |
} |
| 433 |
|
| 434 |
state->problem = problem; |
| 435 |
state->line = cur_lineno; |
| 436 |
state->braces = 0; |
| 437 |
state->args = -1; |
| 438 |
state->constant_string = -1; |
| 439 |
state->last_token = PROBLEMATIC; |
| 440 |
return state; |
| 441 |
} |
| 442 |
} |
| 443 |
|
| 444 |
return state; |
| 445 |
} |
| 446 |
|
| 447 |
/* |
| 448 |
* Pop an entry off of the stack, and return it to the caller. |
| 449 |
*/ |
| 450 |
parser_state_t *pop_stack(void) |
| 451 |
{ |
| 452 |
assert(stack_index >= 0); |
| 453 |
|
| 454 |
/* |
| 455 |
* This works around stupid state thingies. |
| 456 |
*/ |
| 457 |
if (stack_index == 0) { |
| 458 |
return &fsm_stack[stack_index]; |
| 459 |
} |
| 460 |
|
| 461 |
stack_index--; |
| 462 |
return &fsm_stack[stack_index]; |
| 463 |
} |
| 464 |
|
| 465 |
/* |
| 466 |
* Push an entry onto the stack, and return a new entry to use. |
| 467 |
*/ |
| 468 |
parser_state_t *push_stack(parser_state_t *state) |
| 469 |
{ |
| 470 |
assert(state == &fsm_stack[stack_index]); |
| 471 |
assert(stack_index < FSM_MAX_STACK); |
| 472 |
|
| 473 |
stack_index++; |
| 474 |
return &fsm_stack[stack_index]; |
| 475 |
} |