Debian Codegraph @

pscan.c

raw symbols ◀ ▶ SCIP
2 Debian patches touch this file
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"cxx . . $ `<file>/pscan.h`/
30
31 extern int yylexcxx . . $ yylex(d1b40a211a1a4e4c).();
32 extern FILEcxx . . $ FILE# *yyoutcxx . . $ yyout., *yyincxx . . $ yyin.;
•33 int cur_linenocxx . . $ cur_lineno.=1;
34
35 /*
36 * This function does nothing useful.
37 */
38 int yywrapcxx . . $ yywrap(d1b40a211a1a4e4c).()
39 {
40 return(1);
41 }
42
43 static int verbosecxx . . $ verbose. = 0;
44 static int warningscxx . . $ warnings. = FALSEcxx . . $ `pscan.h:25:9`!;
45 static char *filenamecxx . . $ filename.;
46
•47 #define FSM_MAX_STACKcxx . . $ `pscan.c:47:9`! 32768
48 static parser_state_tcxx . . $ parser_state_t# fsm_stackcxx . . $ fsm_stack.[FSM_MAX_STACKcxx . . $ `pscan.c:47:9`!];
49 static int stack_indexcxx . . $ stack_index. = 0;
50 parser_state_tcxx . . $ parser_state_t# *statecxx . . $ state. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
51
52 static int total_errorscxx . . $ total_errors. = 0;
53 static int total_warningscxx . . $ total_warnings. = 0;
54
55 /*
56 * The statically defined list of problem functions,
57 * common to most C libraries.
58 */
59 static problem_tcxx . . $ problem_t# problem_functionscxx . . $ 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 { NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, 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_tcxx . . $ problem_t# user_problem_functionscxx . . $ user_problem_functions.[MAX_USER_PROBLEMScxx . . $ `pscan.h:38:9`!];
92 static int user_problemscxx . . $ user_problems. = 0;
93
94 /*
95 * Print out a usage string.
96 */
97 static void usagecxx . . $ usage(49f6e7a06ebc5aa8).(void)
98 {
99 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Usage: pscan [-vw] [-p problem_file] <files ...>\n");
100 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Attempts to discover a number of common security abuses in C source files.\n\n");
•101 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, " -v Verbose mode. \n");
102 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, " -w Show warnings when a variable is used as the format argument.\n");
103 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, " -p file Read additional problem <function,offset> definitions from <file>.\n");
104 exitcxx . . $ exit(3ca292c338df9bb3).(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_filecxx . . $ read_problem_file(85c52e162fed56f9).(const char *file)
112 {
113 FILEcxx . . $ 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 = fopencxx . . $ fopen(82ec0787cd37e0a3).(file, "r");
124 if (!fp) {
125 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "pscan: Error opening %s: %s\n",
126 file, strerrorcxx . . $ strerror(ff224f080444ee45).(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!));
127 exitcxx . . $ exit(3ca292c338df9bb3).(1);
128 }
129
130 /*
131 * Loop over the input file
132 */
133 line = 0;
134 while (fgetscxx . . $ fgets(efb0aaba7ac866ae).(buffer, sizeof(buffer), fp)) {
135 line++;
136
137 /*
138 * Sanity check the input buffer.
139 */
140 if (strchrcxx . . $ strchr(2050848944e00d54).(buffer, '\n') == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
141 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "pscan: %s:%d: Input line too long\n", file, line);
142 exitcxx . . $ exit(3ca292c338df9bb3).(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_problemscxx . . $ user_problems. >= MAX_USER_PROBLEMScxx . . $ `pscan.h:38:9`!) {
162 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "pscan: Too many user-defined problem functions.\n");
163 exitcxx . . $ exit(3ca292c338df9bb3).(1);
164 }
165
166 /*
167 * Scan for the filename & line.
168 */
169 num = sscanfcxx . . $ sscanf(5e8a2c12db2ea735).(buffer, "%s%d", name, &offset);
170 if (num != 2) {
171 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "pscan: %s:%d: Expected 2 parameters, got %d\n",
172 file, line, num);
173 exitcxx . . $ exit(3ca292c338df9bb3).(1);
174 }
175
176 /*
177 * Copy the function/offset into our data structure.
178 */
179 user_problem_functionscxx . . $ user_problem_functions.[user_problemscxx . . $ user_problems.].functioncxx . . $ problem_t#function. = strdupcxx . . $ strdup(a8b8e32347182212).(name);
180 user_problem_functionscxx . . $ user_problem_functions.[user_problemscxx . . $ user_problems.].fmt_argcxx . . $ problem_t#fmt_arg. = offset;
181 user_problemscxx . . $ user_problems.++;
182 }
183
184 fclosecxx . . $ fclose(89bc1efcdac0ca22).(fp);
185 }
186
187 /*
188 * main, where everything happens.
189 */
190 int maincxx . . $ main(afcd3168fc1188a4).(int argc,char **argv)
191 {
192 int i;
193 int argval;
194
195 /*
196 * Get command-line options.
197 */
198 while ((argval = getoptcxx . . $ getopt(2483c31ff9bab884).(argc, argv, "hp:vw")) != EOFcxx . . $ `<external>/232824719582829767/stdio.h:105:9`!) {
199 switch (argval) {
200
201 default:
202 case 'h':
203 usagecxx . . $ usage(49f6e7a06ebc5aa8).();
204 break;
205
206 case 'v':
•207 verbosecxx . . $ verbose.= TRUEcxx . . $ `pscan.h:26:9`!;
208 break;
209
210 case 'w':
211 warningscxx . . $ warnings. = TRUEcxx . . $ `pscan.h:26:9`!;
212 break;
213
214 case 'p':
215 read_problem_filecxx . . $ read_problem_file(85c52e162fed56f9).(optargcxx . . $ 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 (optindcxx . . $ optind. == argc) {
225 usagecxx . . $ usage(49f6e7a06ebc5aa8).();
226 }
227
228 /*
229 * Loop over the input files, scanning them for problems.
230 */
231 for (i = optindcxx . . $ optind.; i < argc; i++) {
232
233 /*
234 * Initialize the stack, throwing away anything from the last file.
235 */
236 stack_indexcxx . . $ stack_index. = 0;
237 statecxx . . $ state. = &fsm_stackcxx . . $ fsm_stack.[0];
238 filenamecxx . . $ filename. = argv[i];
239
240 /*
241 * Initialize the current state.
242 */
243 statecxx . . $ state.->problemcxx . . $ parser_state_t#problem. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
244 statecxx . . $ state.->linecxx . . $ parser_state_t#line. = 0;
245 statecxx . . $ state.->argscxx . . $ parser_state_t#args. = -1;
246 statecxx . . $ state.->constant_stringcxx . . $ parser_state_t#constant_string. = -1;
247 statecxx . . $ state.->last_tokencxx . . $ parser_state_t#last_token. = NOT_PROBLEMATICcxx . . $ `pscan.h:30:9`!;
248 statecxx . . $ state.->bracescxx . . $ parser_state_t#braces. = 0;
249
250 /* Open the source file for reading */
251 if ((yyincxx . . $ yyin. = fopencxx . . $ fopen(82ec0787cd37e0a3).(filenamecxx . . $ filename., "r")) == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
252 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "pscan: Error opening %s: %s.\n", filenamecxx . . $ filename.,
253 strerrorcxx . . $ strerror(ff224f080444ee45).(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!));
254 printfcxx . . $ printf(581fddd72a8db8fc).("%s\n", strerrorcxx . . $ strerror(ff224f080444ee45).(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!));
255 exitcxx . . $ exit(3ca292c338df9bb3).(1);
256 }
257 yyoutcxx . . $ yyout. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
258
259 /*
260 * Initialize our variables.
261 */
•262 cur_linenocxx . . $ cur_lineno. = 1;
263
264 if (verbosecxx . . $ verbose.) {
265 printfcxx . . $ printf(581fddd72a8db8fc).("Scanning %s ...\n", filenamecxx . . $ filename.);
266 }
267
268 /*
269 * Let the lexer parse the whole file.
270 */
271 while (yylexcxx . . $ yylex(d1b40a211a1a4e4c).())
272 ;
273
274 /* close the input file */
275 fclosecxx . . $ fclose(89bc1efcdac0ca22).(yyincxx . . $ yyin.);
276 }
277
278 /*
279 * And finally, print out a summary of the total problems.
280 */
•281 if (verbosecxx . . $ verbose.) {
282 if (total_errorscxx . . $ total_errors. != 0 ) {
283 printfcxx . . $ printf(581fddd72a8db8fc).("Total problems identified: %d\n", total_errorscxx . . $ total_errors.);
•284 }
285
•286 if (warningscxx . . $ warnings.) {
287 if (total_warningscxx . . $ total_warnings. != 0 ) {
288 printfcxx . . $ printf(581fddd72a8db8fc).("Warnings: %d\n",total_warningscxx . . $ total_warnings.);
289 }
290 }
291 }
•292
293 if (total_errorscxx . . $ total_errors.!=0)
294 exitcxx . . $ exit(3ca292c338df9bb3).(1);
295 else
296 exitcxx . . $ exit(3ca292c338df9bb3).(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_functioncxx . . $ check_function(ae6e417ad3b37e2a).(parser_state_tcxx . . $ parser_state_t# *state)
304 {
•305 int erroneous=0;
306
•307 assertcxx . . $ `<external>/4398592474888995393/assert.h:118:11`!(state != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
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->argscxx . . $ parser_state_t#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->problemcxx . . $ parser_state_t#problem.->fmt_argcxx . . $ problem_t#fmt_arg. == state->argscxx . . $ parser_state_t#args.) &&
328 (state->constant_stringcxx . . $ parser_state_t#constant_string. != state->problemcxx . . $ parser_state_t#problem.->fmt_argcxx . . $ problem_t#fmt_arg.)) {
329 printfcxx . . $ printf(581fddd72a8db8fc).("%s:%d SECURITY: %s call should have \"%%s\" as argument %d\n",
330 filenamecxx . . $ filename.,
331 state->linecxx . . $ parser_state_t#line.,
332 state->problemcxx . . $ parser_state_t#problem.->functioncxx . . $ problem_t#function.,
333 state->problemcxx . . $ parser_state_t#problem.->fmt_argcxx . . $ problem_t#fmt_arg.);
•334 erroneous=1;
335
336 }
337
•338 if (warningscxx . . $ warnings. &&
339 (state->constant_stringcxx . . $ parser_state_t#constant_string. != state->problemcxx . . $ parser_state_t#problem.->fmt_argcxx . . $ problem_t#fmt_arg.)) {
340 printfcxx . . $ printf(581fddd72a8db8fc).("%s:%d Warning: %s uses non-constant string for format argument %d.\n",
341 filenamecxx . . $ filename.,
342 state->linecxx . . $ parser_state_t#line.,
343 state->problemcxx . . $ parser_state_t#problem.->functioncxx . . $ problem_t#function.,
344 state->problemcxx . . $ parser_state_t#problem.->fmt_argcxx . . $ problem_t#fmt_arg.);
345 total_warningscxx . . $ total_warnings.++;
346 }
347
•348 if (verbosecxx . . $ verbose.) {
349 /*
•350 * verbose =TRUE, print out more stuff.
351 */
352 printfcxx . . $ printf(581fddd72a8db8fc).("%s:%d FUNC %s ", filenamecxx . . $ filename., state->linecxx . . $ parser_state_t#line.,
353 state->problemcxx . . $ parser_state_t#problem.->functioncxx . . $ problem_t#function.);
354 if (state->problemcxx . . $ parser_state_t#problem.->fmt_argcxx . . $ problem_t#fmt_arg. == state->argscxx . . $ parser_state_t#args.) {
355 printfcxx . . $ printf(581fddd72a8db8fc).("Last argument is ");
•356 if (state->constant_stringcxx . . $ parser_state_t#constant_string.==state->argscxx . . $ parser_state_t#args.) {
357 printfcxx . . $ printf(581fddd72a8db8fc).("constant string: OK\n");
358 } else {
359 printfcxx . . $ printf(581fddd72a8db8fc).("variable or reference: BAD\n");
•360 erroneous=1;
361 }
362 } else {
363 printfcxx . . $ printf(581fddd72a8db8fc).("format string with %d parameters: OK\n",
364 state->argscxx . . $ parser_state_t#args. - state->problemcxx . . $ parser_state_t#problem.->fmt_argcxx . . $ problem_t#fmt_arg.);
365 }
366 }
•367
368 if (erroneous) total_errorscxx . . $ 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_tcxx . . $ parser_state_t# *setup_checkscxx . . $ setup_checks(22d96b5b65047842).(const char *name, parser_state_tcxx . . $ parser_state_t# *state)
377 {
378 problem_tcxx . . $ 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_problemscxx . . $ 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_problemscxx . . $ user_problems.; i++) {
392 problem = &user_problem_functionscxx . . $ user_problem_functions.[i];
393
394 /*
395 * We have a match! Set up the current stack, and return.
396 */
397 if (strcmpcxx . . $ strcmp(5ae29d726ae57f88).(problem->functioncxx . . $ problem_t#function., name) == 0) {
398
399 /*
400 * Ignore user-defined problems with -1 as a format parameter.
401 */
402 if (problem->fmt_argcxx . . $ problem_t#fmt_arg. == -1) {
403 return state;
404 }
405
406 if (state->last_tokencxx . . $ parser_state_t#last_token. == PROBLEMATICcxx . . $ `pscan.h:29:9`!) {
407 state = push_stackcxx . . $ push_stack(d44d929f23c0c6cd).(state);
408 }
409
410 state->problemcxx . . $ parser_state_t#problem. = problem;
•411 state->linecxx . . $ parser_state_t#line. = cur_linenocxx . . $ cur_lineno.;
412 state->bracescxx . . $ parser_state_t#braces. = 0;
413 state->argscxx . . $ parser_state_t#args. = -1;
414 state->constant_stringcxx . . $ parser_state_t#constant_string. = -1;
415 state->last_tokencxx . . $ parser_state_t#last_token. = PROBLEMATICcxx . . $ `pscan.h:29:9`!;
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_functionscxx . . $ problem_functions.[0]; problem->functioncxx . . $ problem_t#function. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; problem++) {
425
426 /*
427 * We have a match! Set up the current stack, and return.
428 */
429 if (strcmpcxx . . $ strcmp(5ae29d726ae57f88).(problem->functioncxx . . $ problem_t#function., name) == 0) {
430 if (state->last_tokencxx . . $ parser_state_t#last_token. == PROBLEMATICcxx . . $ `pscan.h:29:9`!) {
431 state = push_stackcxx . . $ push_stack(d44d929f23c0c6cd).(state);
432 }
433
434 state->problemcxx . . $ parser_state_t#problem. = problem;
•435 state->linecxx . . $ parser_state_t#line. = cur_linenocxx . . $ cur_lineno.;
436 state->bracescxx . . $ parser_state_t#braces. = 0;
437 state->argscxx . . $ parser_state_t#args. = -1;
438 state->constant_stringcxx . . $ parser_state_t#constant_string. = -1;
439 state->last_tokencxx . . $ parser_state_t#last_token. = PROBLEMATICcxx . . $ `pscan.h:29:9`!;
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_tcxx . . $ parser_state_t# *pop_stackcxx . . $ pop_stack(e311b5957e04fce2).(void)
451 {
452 assertcxx . . $ `<external>/4398592474888995393/assert.h:118:11`!(stack_index >= 0);
453
454 /*
455 * This works around stupid state thingies.
456 */
457 if (stack_indexcxx . . $ stack_index. == 0) {
458 return &fsm_stackcxx . . $ fsm_stack.[stack_indexcxx . . $ stack_index.];
459 }
460
461 stack_indexcxx . . $ stack_index.--;
462 return &fsm_stackcxx . . $ fsm_stack.[stack_indexcxx . . $ stack_index.];
463 }
464
465 /*
466 * Push an entry onto the stack, and return a new entry to use.
467 */
468 parser_state_tcxx . . $ parser_state_t# *push_stackcxx . . $ push_stack(d44d929f23c0c6cd).(parser_state_tcxx . . $ parser_state_t# *state)
469 {
470 assertcxx . . $ `<external>/4398592474888995393/assert.h:118:11`!(state == &fsm_stack[stack_index]);
471 assertcxx . . $ `<external>/4398592474888995393/assert.h:118:11`!(stack_index < FSM_MAX_STACKcxx . . $ `pscan.c:47:9`!);
472
473 stack_indexcxx . . $ stack_index.++;
474 return &fsm_stackcxx . . $ fsm_stack.[stack_indexcxx . . $ stack_index.];
475 }