pscan/0040775000076400007640000000000007307206645011131 5ustar alandalandpscan/COPYING0100664000076400007640000004311007131410265012146 0ustar alandaland GNU GENERAL PUBLIC LICENSE Version 2, June 1991 Copyright (C) 1989, 1991 Free Software Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. This General Public License applies to most of the Free Software Foundation's software and to any other program whose authors commit to using it. (Some other Free Software Foundation software is covered by the GNU Library General Public License instead.) You can apply it to your programs, too. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for this service if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. Finally, any free program is threatened constantly by software patents. We wish to avoid the danger that redistributors of a free program will individually obtain patent licenses, in effect making the program proprietary. To prevent this, we have made it clear that any patent must be licensed for everyone's free use or not licensed at all. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any derivative work under copyright law: that is to say, a work containing the Program or a portion of it, either verbatim or with modifications and/or translated into another language. (Hereinafter, translation is included without limitation in the term "modification".) Each licensee is addressed as "you". Activities other than copying, distribution and modification are not covered by this License; they are outside its scope. The act of running the Program is not restricted, and the output from the Program is covered only if its contents constitute a work based on the Program (independent of having been made by running the Program). Whether that is true depends on what the Program does. 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this License and to the absence of any warranty; and give any other recipients of the Program a copy of this License along with the Program. You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. 2. You may modify your copy or copies of the Program or any portion of it, thus forming a work based on the Program, and copy and distribute such modifications or work under the terms of Section 1 above, provided that you also meet all of these conditions: a) You must cause the modified files to carry prominent notices stating that you changed the files and the date of any change. b) You must cause any work that you distribute or publish, that in whole or in part contains or is derived from the Program or any part thereof, to be licensed as a whole at no charge to all third parties under the terms of this License. c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the most ordinary way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this License. (Exception: if the Program itself is interactive but does not normally print such an announcement, your work based on the Program is not required to print an announcement.) These requirements apply to the modified work as a whole. If identifiable sections of that work are not derived from the Program, and can be reasonably considered independent and separate works in themselves, then this License, and its terms, do not apply to those sections when you distribute them as separate works. But when you distribute the same sections as part of a whole which is a work based on the Program, the distribution of the whole must be on the terms of this License, whose permissions for other licensees extend to the entire whole, and thus to each and every part regardless of who wrote it. Thus, it is not the intent of this section to claim rights or contest your rights to work written entirely by you; rather, the intent is to exercise the right to control the distribution of derivative or collective works based on the Program. In addition, mere aggregation of another work not based on the Program with the Program (or with a work based on the Program) on a volume of a storage or distribution medium does not bring the other work under the scope of this License. 3. You may copy and distribute the Program (or a work based on it, under Section 2) in object code or executable form under the terms of Sections 1 and 2 above provided that you also do one of the following: a) Accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, b) Accompany it with a written offer, valid for at least three years, to give any third party, for a charge no more than your cost of physically performing source distribution, a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, c) Accompany it with the information you received as to the offer to distribute corresponding source code. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form with such an offer, in accord with Subsection b above.) The source code for a work means the preferred form of the work for making modifications to it. For an executable work, complete source code means all the source code for all modules it contains, plus any associated interface definition files, plus the scripts used to control compilation and installation of the executable. However, as a special exception, the source code distributed need not include anything that is normally distributed (in either source or binary form) with the major components (compiler, kernel, and so on) of the operating system on which the executable runs, unless that component itself accompanies the executable. If distribution of executable or object code is made by offering access to copy from a designated place, then offering equivalent access to copy the source code from the same place counts as distribution of the source code, even though third parties are not compelled to copy the source along with the object code. 4. You may not copy, modify, sublicense, or distribute the Program except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense or distribute the Program is void, and will automatically terminate your rights under this License. However, parties who have received copies, or rights, from you under this License will not have their licenses terminated so long as such parties remain in full compliance. 5. You are not required to accept this License, since you have not signed it. However, nothing else grants you permission to modify or distribute the Program or its derivative works. These actions are prohibited by law if you do not accept this License. Therefore, by modifying or distributing the Program (or any work based on the Program), you indicate your acceptance of this License to do so, and all its terms and conditions for copying, distributing or modifying the Program or works based on it. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. You are not responsible for enforcing compliance by third parties to this License. 7. If, as a consequence of a court judgment or allegation of patent infringement or for any other reason (not limited to patent issues), conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot distribute so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not distribute the Program at all. For example, if a patent license would not permit royalty-free redistribution of the Program by all those who receive copies directly or indirectly through you, then the only way you could satisfy both it and this License would be to refrain entirely from distribution of the Program. If any portion of this section is held invalid or unenforceable under any particular circumstance, the balance of the section is intended to apply and the section as a whole is intended to apply in other circumstances. It is not the purpose of this section to induce you to infringe any patents or other property right claims or to contest validity of any such claims; this section has the sole purpose of protecting the integrity of the free software distribution system, which is implemented by public license practices. Many people have made generous contributions to the wide range of software distributed through that system in reliance on consistent application of that system; it is up to the author/donor to decide if he or she is willing to distribute software through any other system and a licensee cannot impose that choice. This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License. 8. If the distribution and/or use of the Program is restricted in certain countries either by patents or by copyrighted interfaces, the original copyright holder who places the Program under this License may add an explicit geographical distribution limitation excluding those countries, so that distribution is permitted only in or among countries not thus excluded. In such case, this License incorporates the limitation as if written in the body of this License. 9. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies a version number of this License which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of this License, you may choose any version ever published by the Free Software Foundation. 10. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Also add information on how to contact you by electronic and paper mail. If the program is interactive, make it output a short notice like this when it starts in an interactive mode: Gnomovision version 69, Copyright (C) year name of author Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, the commands you use may be called something other than `show w' and `show c'; they could even be mouse-clicks or menu items--whatever suits your program. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the program, if necessary. Here is a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the program `Gnomovision' (which makes passes at compilers) written by James Hacker. , 1 April 1989 Ty Coon, President of Vice This General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Library General Public License instead of this License. pscan/Makefile0100664000076400007640000000141407131427563012565 0ustar alandaland# Copyright (C) 2000 Alan DeKok # This Makefile is free software; Alan DeKok # gives unlimited permission to copy and/or distribute it, # with or without modifications, as long as this notice is preserved. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY, to the extent permitted by law; without # even the implied warranty of MERCHANTABILITY or FITNESS FOR A # PARTICULAR PURPOSE. # # These are what I use on Linux. Your mileage may vary. #CC=gcc -Wall -g #LEX=flex pscan: scanner.yy.o pscan.o $(CC) scanner.yy.o pscan.o -o pscan scanner.yy.o: scanner.yy.c $(CC) -c scanner.yy.c -o scanner.yy.o scanner.yy.c: scanner.l $(LEX) -t scanner.l > scanner.yy.c pscan.o: pscan.c pscan.h clean: rm -f *.o *~ pscan scanner.yy.c core pscan/README0100664000076400007640000000604507136073012012001 0ustar alandalandCopyright (C) 2000 Alan DeKok This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA ---------------------------------------------------------------------- 'pscan' is a program which attempts to scan C source files for common function abuses, which often lead to buffer overflows. See the included file 'test.c' for examples of good, and bad programming practice. For the latest version, see: http://www.striker.ottawa.on.ca/~aland/pscan/ The scan works by looking for a one of a list of problem functions, and applying the following rule: IF the last parameter of the function is the format string, AND the format string is NOT a static string, THEN complain. See 'test.c' for examples. Once pscan has found the problems, it's up to you to examine the source code, and correct the (possible) security breach. You can build pscan by typing: make If you don't have lex or yacc installed, it probably won't work. You'll have to figure out how to build it yourself, as I'm too lazy to write an autoconf for a ~100 line C program. Once pscan is built, you can test it by typing: ./pscan test.c You should see it complain about a number of errors, giving the source file, line number, and problematic function name. If there are any errors found, pscan exits with status 1. Typing: echo $? should get you a '1'. Now try: ./pscan ./pscan.c You shouldn't see any errors, and the exit code will be 0 (zero). You can scan multiple C source files with one run of pscan, by specifying multiple filenames on the command line: ./pscan ./*.c If ANY problems are found, they are printed out, and pscan exits with status 1. If you don't trust it, pscan has a verbose option: -v. ./pscan -vv ./test.c The output should make itself clear. You can supply additional definitions of problem functions through the '-p' command line option. See wu-ftpd.pscan for details. As of version 1.2, pscan now also prints out warnings when non-constant strings are used as format parameters. This behaviour can be enabled by using the '-w' command line parameter. In addition to the problem C library functions, many programs define their own functions with similar security problems. The included shell script: find_formats.sh tries to discover C function prototypes for the problem functions. It won't find them all, but it will find many of the most common ones. Alan DeKok July 21, 2000. pscan/find_formats.sh0100775000076400007640000000264607131410265014136 0ustar alandaland#!/bin/sh ###################################################################### # Copyright (C) 2000 Alan DeKok # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or (at # your option) any later version. # # This program is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, # USA ###################################################################### # # This is a quick & dirty shell script which roots through the # given files looking for function prototypes using variable # arguments, and a 'char' string, which we assume to be a format. # # There's no guarantee that this will catch ALL of the problem # functions, (e.g. multi-line prototypes will be missed), but it # will catch the most common ones. # # You'll have to turn the output into pscan rules by hand. # ###################################################################### grep -n '\.\.\.' $@ | grep ';' | grep 'char' pscan/pscan.c0100664000076400007640000002524207144535463012405 0ustar alandaland/********************************************************************** * pscan: http://www.striker.ottawa.on.ca/~aland/pscan/ * * Copyright (C) 2000 Alan DeKok * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA * **********************************************************************/ static const char rcsid[] = "$Id: pscan.c,v 1.6 2000/08/10 14:24:51 aland Exp $"; #include #include #include #include #include #include #include #include "pscan.h" extern int yylex(); extern int yylineno; extern FILE *yyout, *yyin; /* * This function does nothing useful. */ int yywrap() { return(1); } static int verbose = 0; static int warnings = FALSE; static char *filename; #define FSM_MAX_STACK 8192 static parser_state_t fsm_stack[FSM_MAX_STACK]; static int stack_index = 0; parser_state_t *state = NULL; static int total_errors = 0; static int total_warnings = 0; /* * The statically defined list of problem functions, * common to most C libraries. */ static problem_t problem_functions[] = { { "vsprintf", 1}, { "vfprintf", 1}, { "vprintf", 0}, { "vsnprintf", 2}, { "snprintf", 2}, { "sprintf", 1}, { "fprintf", 1}, { "fscanf", 1}, { "printf", 0}, { "scanf", 0}, { "sscanf", 1}, { "syslog", 1}, { "setproctitle", 0}, { "err", 1}, { "verr", 1}, { "errx", 1}, { "verrx", 1}, { "warn", 0}, { "vwarn", 0}, { "warnx", 0}, { "vwarnx", 0}, { NULL, 0} }; /* * User-supplied problem functions. * * Yes, this is only a static buffer, but I'm too lazy to code * a proper malloc/linked list replacement. Sue me. This works. */ static problem_t user_problem_functions[MAX_USER_PROBLEMS]; static int user_problems = 0; /* * Print out a usage string. */ static void usage(void) { fprintf(stderr, "Usage: pscan [-vw] [-p problem_file] \n"); fprintf(stderr, "Attempts to discover a number of common security abuses in C source files.\n\n"); fprintf(stderr, " -v Verbose mode. Can be use multiple times for more output.\n"); fprintf(stderr, " -w Show warnings when a variable is used as the format argument.\n"); fprintf(stderr, " -p file Read additional problem definitions from .\n"); exit(1); } /* * Read a problem file. This consists of a function name, * and an offset of the format string. */ void read_problem_file(const char *file) { FILE *fp; char buffer[1024]; char name[1024]; int num, offset; int line; char *p; /* * Can we open the file that they gave us? */ fp = fopen(file, "r"); if (!fp) { fprintf(stderr, "pscan: Error opening %s: %s\n", file, strerror(errno)); exit(1); } /* * Loop over the input file */ line = 0; while (fgets(buffer, sizeof(buffer), fp)) { line++; /* * Sanity check the input buffer. */ if (strchr(buffer, '\n') == NULL) { fprintf(stderr, "pscan: %s:%d: Input line too long\n", file, line); exit(1); } /* * Ignore leading whitespace. */ for (p = buffer; (*p == ' ') || (*p == '\t'); p++) /* nothing */; /* * Skip blank lines and comments */ if ((*p == '\n') || (*p == '#')) { continue; } /* * Check for stupid buffer over-flows. */ if (user_problems >= MAX_USER_PROBLEMS) { fprintf(stderr, "pscan: Too many user-defined problem functions.\n"); exit(1); } /* * Scan for the filename & line. */ num = sscanf(buffer, "%s%d", name, &offset); if (num != 2) { fprintf(stderr, "pscan: %s:%d: Expected 2 parameters, got %d\n", file, line, num); exit(1); } /* * Copy the function/offset into our data structure. */ user_problem_functions[user_problems].function = strdup(name); user_problem_functions[user_problems].fmt_arg = offset; user_problems++; } fclose(fp); } /* * main, where everything happens. */ int main(int argc,char **argv) { int i; int argval; /* * Get command-line options. */ while ((argval = getopt(argc, argv, "hp:vw")) != EOF) { switch (argval) { default: case 'h': usage(); break; case 'v': verbose++; break; case 'w': warnings = TRUE; break; case 'p': read_problem_file(optarg); break; } } /* * Sanity check arguments, to be sure there's at least one file we * can open. */ if (optind == argc) { usage(); } /* * Loop over the input files, scanning them for problems. */ for (i = optind; i < argc; i++) { /* * Initialize the stack, throwing away anything from the last file. */ stack_index = 0; state = &fsm_stack[0]; filename = argv[i]; /* * Initialize the current state. */ state->problem = NULL; state->line = 0; state->args = -1; state->constant_string = -1; state->last_token = NOT_PROBLEMATIC; state->braces = 0; /* Open the source file for reading */ if ((yyin = fopen(filename, "r")) == NULL) { fprintf(stderr, "pscan: Error opening %s: %s.\n", filename, strerror(errno)); printf("%s\n", strerror(errno)); exit(1); } yyout = NULL; /* * Initialize our variables. */ yylineno = 1; if (verbose) { printf("Scanning %s ...\n", filename); } /* * Let the lexer parse the whole file. */ while (yylex()) ; /* close the input file */ fclose(yyin); } /* * And finally, print out a summary of the total problems. */ if (total_errors != 0) { if (verbose) { if (warnings) { printf("Warnings: %d\n", total_warnings); } printf("Total problems identified: %d\n", total_errors); } exit(1); } exit(0); } /* * Check the number of arguments to the function, and was the LAST * argument a constant string? */ void check_function(parser_state_t *state) { assert(state != NULL); /* * This was a reference to a function WITHOUT an opening brace, * so it's not a function call. Ignore it. */ if (state->args < 0) { return; } if (verbose == 0) { /* * The problem function has the SAME number of arguments as the * placement of the format argument. i.e. The LAST argument of the * function is the format string. * * If the last argument of the function is a constant string, then * there can't be any security problems, so don't complain. * * Otherwise, print out a complaint noting the source file, * line number, and function name. */ if ((state->problem->fmt_arg == state->args) && (state->constant_string != state->problem->fmt_arg)) { printf("%s:%d SECURITY: %s call should have \"%%s\" as argument %d\n", filename, state->line, state->problem->function, state->problem->fmt_arg); total_errors++; } else if (warnings && (state->constant_string != state->problem->fmt_arg)) { printf("%s:%d Warning: %s uses non-constant string for format argument %d.\n", filename, state->line, state->problem->function, state->problem->fmt_arg); total_warnings++; } } else { /* * verbose = 1, print out more stuff. */ printf("%s:%d FUNC %s ", filename, state->line, state->problem->function); if (state->problem->fmt_arg == state->args) { printf("Last argument is "); if (state->constant_string) { printf("constant string: OK\n"); } else { printf("variable or reference: BAD\n"); total_errors++; } } else { printf("format string with %d parameters: OK\n", state->args - state->problem->fmt_arg); } } } /* * Scan the current token to see if it's on the list of problem * functions that we care about. */ parser_state_t *setup_checks(const char *name, parser_state_t *state) { problem_t *problem; int i; /* * If there are user-defined problems, use them FIRST. * * This allows us to handle name conflicts with internally defined * problem functions */ if (user_problems != 0) { /* * Loop over the list of problem functions, seeing if we have a match. */ for (i = 0; i < user_problems; i++) { problem = &user_problem_functions[i]; /* * We have a match! Set up the current stack, and return. */ if (strcmp(problem->function, name) == 0) { /* * Ignore user-defined problems with -1 as a format parameter. */ if (problem->fmt_arg == -1) { return state; } if (state->last_token == PROBLEMATIC) { state = push_stack(state); } state->problem = problem; state->line = yylineno; state->braces = 0; state->args = -1; state->constant_string = -1; state->last_token = PROBLEMATIC; return state; } } /* end of loop over user defined problems */ } /* end of any user defined problems */ /* * Loop over the list of problem functions, seeing if we have a match. */ for (problem = &problem_functions[0]; problem->function != NULL; problem++) { /* * We have a match! Set up the current stack, and return. */ if (strcmp(problem->function, name) == 0) { if (state->last_token == PROBLEMATIC) { state = push_stack(state); } state->problem = problem; state->line = yylineno; state->braces = 0; state->args = -1; state->constant_string = -1; state->last_token = PROBLEMATIC; return state; } } return state; } /* * Pop an entry off of the stack, and return it to the caller. */ parser_state_t *pop_stack(void) { assert(stack_index >= 0); /* * This works around stupid state thingies. */ if (stack_index == 0) { return &fsm_stack[stack_index]; } stack_index--; return &fsm_stack[stack_index]; } /* * Push an entry onto the stack, and return a new entry to use. */ parser_state_t *push_stack(parser_state_t *state) { assert(state == &fsm_stack[stack_index]); assert(stack_index < FSM_MAX_STACK); stack_index++; return &fsm_stack[stack_index]; } pscan/pscan.h0100664000076400007640000000413107131411156012370 0ustar alandaland#ifndef PSCAN_H #define PSCAN_H /********************************************************************** * pscan: http://www.striker.ottawa.on.ca/~aland/pscan/ * * Copyright (C) 2000 Alan DeKok * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA * * $Id: pscan.h,v 1.2 2000/07/07 17:25:02 aland Exp $ * **********************************************************************/ #ifndef FALSE #define FALSE (0) #define TRUE (!FALSE) #endif #define PROBLEMATIC TRUE #define NOT_PROBLEMATIC FALSE /* * The maximum number of user-defined problem functions which may be * read from a .pscan problem definition file. */ #define MAX_USER_PROBLEMS 2048 typedef struct problem_t { const char *function; int fmt_arg; } problem_t; typedef struct parser_state_t { const problem_t *problem; int line; /* source file line at which we found the problem function */ int args; /* number of arguments used by the function */ int constant_string; /* is the last argument a constant string? */ int last_token; /* was the last token problematic, or not? */ int braces; /* the number of braces... */ } parser_state_t; extern parser_state_t *state; extern parser_state_t *pop_stack(void); extern parser_state_t *push_stack(parser_state_t *current); extern void check_function(parser_state_t *state); extern parser_state_t *setup_checks(const char *yytext, parser_state_t *state); #endif /* PSCAN_H */ pscan/scanner.l0100664000076400007640000001261007134652701012730 0ustar alandaland%option yylineno %{ #include "pscan.h" static void skip_strings(char literal); %} %x comment %x strings reserved "default"|"struct"|"void"|"for"|"if"|"else"|"while"|"do"|"return"|"case"|"switch"|"break"|"auto"|"continue"|"goto"|"sizeof"|"static"|"typedef"|"union"|"volatile" vartype "char"|"double"|"enum"|"extern"|"float"|"int"|"long"|"register"|"short"|"signed"|"unsigned"|"const" cprep "include"|"define"|"if"|"else"|"endif"|"ifdef"|"ifndef" %% {reserved} state->last_token = NOT_PROBLEMATIC; {vartype} state->last_token = NOT_PROBLEMATIC; "#"{cprep} state->last_token = NOT_PROBLEMATIC; NULL { if ((state->last_token == PROBLEMATIC) && (state->constant_string < 0)) { state->constant_string = state->args; } } \" { if (state->last_token == PROBLEMATIC) { if (state->constant_string < 0) { state->constant_string = state->args; } } skip_strings('"'); } \' skip_strings('\''); \/\/.*$ /* skip C++ style comments */ [a-zA-Z_][_a-zA-Z0-9]* state = setup_checks(yytext, state); [ \t]+ /* eat up whitespace */ \( { if (state->args < 0) state->args = 0; state->braces++; if (state->braces > 1) { state = push_stack(state); state->last_token = NOT_PROBLEMATIC; state->braces = 1; state->args = -1; } } \, if (state->last_token == PROBLEMATIC) { if (state->braces != 0) { state->args++; } else { state->last_token = NOT_PROBLEMATIC; } } \) if (state->last_token == PROBLEMATIC) { check_function(state); state->last_token = NOT_PROBLEMATIC; } else if (state->braces != 0) { state->braces--; if (state->braces == 0) { state = pop_stack(); } } . { if ((state->last_token == PROBLEMATIC) && (state->braces == 0)) { state->last_token = NOT_PROBLEMATIC; } } "\n"|"\r" /* ignore LF's and CR's */ "/*" BEGIN(comment); [^*\n]* /* eat anything that's not a '*' */ "*"+[^*/\n]* /* eat up '*'s not followed by '/'s */ \n /* do nothing */ "*"+"/" BEGIN(INITIAL); %% /********************************************************************** * pscan: http://www.striker.ottawa.on.ca/~aland/pscan/ * * Copyright (C) 2000 Alan DeKok * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA * **********************************************************************/ static const char rcsid[] = "$Id: scanner.l,v 1.4 2000/07/17 18:51:45 aland Exp $"; /* static */ void skip_strings(char literal) { int c; while ((c = input()) != literal) { switch (c) { case '\\': c = input(); if (c == '\\') continue; if (c == EOF) return; if (c != literal) unput(c); break; case EOF: return; default: break; } } } pscan/test.c0100664000076400007640000001036007134702373012246 0ustar alandaland/********************************************************************** * pscan: http://www.striker.ottawa.on.ca/~aland/pscan/ * * Copyright (C) 2000 Alan DeKok * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA * * $Id: test.c,v 1.4 2000/07/17 22:13:15 aland Exp $ * **********************************************************************/ #error Do not compile this file! /* * This file consists of a number of sample snippets of C code. * Run it through the scanner by doing: * * ./pscan ./test.c * * You should see a number of problems reported. */ /* * This may be a problem. */ fprintf(stderr, variable); /* problematic */ /* * This MIGHT be a problem, depending on where the 'format' * string comes from, and what it's value is. */ fprintf(stderr, format, variable1, variable2); /* * This is safer. */ fprintf(stderr, "%s", variable); /* OK */ /* * Constant strings can't be modified externally, so they're OK. */ sprintf(buffer, "string"); /* OK */ /* * If you're a dumb enough programmer to put something like this * in the source, you get what you deserve. But it isn't an * externally exploitable security hole. */ sprintf(buffer, "%s"); /* OK */ /* * The variable may contain formatting commands! */ sprintf(buffer, variable); /* problematic */ /* * This is the safe way of doing it. */ sprintf(buffer, "%s", variable); /* OK */ /* * The first sprintf is OK, but the second one has a problem. * This is a check for nested security problems. */ sprintf(buffer, "%d", sprintf(buffer1, variable)); /* problematic! */ /* * strerror(errno) isn't a problem function, and snprintf has lots * of arguments after the format string, so this is OK. */ snprintf(buffer, sizeof(buffer), "test: Error opening %s: %s\n", filename, strerror(errno)); /* OK */ /* * Multi-line sequences get checked, too. This one should be OK. */ snprintf(buffer, sizeof(buffer), "test: Error opening %s: %s\n", filename, strerror(errno)); /* also OK */ /* * This multi-line sequence shouldn't be OK. */ sprintf(buffer, variable); /* problematic */ /* * Lots of arguments after the format string. It's up to your C * compiler to see if you're using the right number of arguments for * the format string. */ sprintf(buffer, "%s %s %s", one, two, three); /* OK */ /* * Nested braces should be OK. */ printf((variable ? "%4" : "%3s"), string); /* OK */ /* * User-supplied format strings are OK, I guess... */ printf((variable ? fmt1 : fmt2), string3); /* OK */ /* * There's still only one argument for printf, that's a problem. */ printf((variable ? string1 : string2)); /* problematic */ // sprintf(buffer, variable); C++ comments get ignored, for good or for bad. /* sprintf(buffer, variable); these comments get ignored, too */ /* * This next bit of code is from the wu-ftp source. It's OK, but it * gets flagged because the parser isn't smart enough to check for * previous, safe, uses of strings. */ sprintf(s, "PASV port %i assigned to %s", i, remoteident); syslog(LOG_DEBUG, s); /* * The following are references to the functions, but not actual * function calls, so they're OK. */ void *foo[] = {snprintf, fprintf}; /* OK */ /* * Your program may define a problem function in one file, * and use a variable of the same name in another file. We don't * want to complain about uses of those variables. * * I know this won't work in a real C program, but it's a way of faking * such a variable reference, to ensure that pscan ignores it. */ fprintf[1] = 1; /* OK */ /* * NetBSD allows err(1,NULL). We should, too. */ err(1, NULL); pscan/wu-ftpd.pscan0100664000076400007640000000273707131411607013542 0ustar alandaland###################################################################### # Copyright (C) 2000 Alan DeKok # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or (at # your option) any later version. # # This program is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, # USA ###################################################################### # # This is an additional definition file for wu-ftpd, as of v2.6.1 # Use with: # # pscan -p wu-ftpd.pscan *.c # # # The format of this file is: # # function_name format_offset # # e.g. wu-ftpd has the following prototypes: # # ./wu-ftpd-2.6.1/src/proto.h:179:void reply(int, char *fmt,...); # ./wu-ftpd-2.6.1/src/proto.h:180:void lreply(int, char *fmt,...); # ./wu-ftpd-2.6.1/src/proto.h:198:void setproctitle(const char *fmt,...); # # So the function 'reply' has argument 0 (zero) of 'int', and # argument 1 of the format. That's where the '1', below, comes from. # reply 1 lreply 1 setproctitle 0