| 1 |
/* { dg-additional-options "-std=gnu17" } */ |
| 2 |
|
| 3 |
#include <stdio.h> |
| 4 |
|
| 5 |
/* Solaris needs this for <unistd.h> to declare getpass. */ |
| 6 |
#define __EXTENSIONS__ |
| 7 |
#include <unistd.h> |
| 8 |
|
| 9 |
#include <string.h> |
| 10 |
|
| 11 |
/* Declare getpass, in case unistd doesn't declare it. |
| 12 |
Parenthesize it, in case it's a macro. |
| 13 |
Don't use a prototype, to avoid const mismatches. */ |
| 14 |
extern char *(getpass) (); |
| 15 |
|
| 16 |
char test_1 (FILE *logfile) |
| 17 |
{ |
| 18 |
char *password = getpass (">"); /* { dg-message "\\(1\\) sensitive value acquired here" } */ |
| 19 |
fprintf (logfile, "got password %s\n", password); /* { dg-warning "sensitive value 'password' written to output file \\\[CWE-532\\\]" "warning" } */ |
| 20 |
/* { dg-message "\\(2\\) sensitive value 'password' written to output file; acquired at \\(1\\)" "event" { target *-*-* } .-1 } */ |
| 21 |
} |
| 22 |
|
| 23 |
char test_2 (FILE *logfile, int i) |
| 24 |
{ |
| 25 |
char *password = getpass (">"); /* { dg-message "\\(1\\) sensitive value acquired here" } */ |
| 26 |
fprintf (logfile, "got password[%i]: %s\n", i, password); /* { dg-warning "sensitive value 'password' written to output file \\\[CWE-532\\\]" } */ |
| 27 |
/* { dg-message "\\(2\\) sensitive value 'password' written to output file; acquired at \\(1\\)" "event" { target *-*-* } .-1 } */ |
| 28 |
} |
| 29 |
|
| 30 |
char test_3 (FILE *logfile) |
| 31 |
{ |
| 32 |
char *password = getpass (">"); /* { dg-message "\\(1\\) sensitive value acquired here" } */ |
| 33 |
printf ("got password %s\n", password); /* { dg-warning "sensitive value 'password' written to output file \\\[CWE-532\\\]" "warning" } */ |
| 34 |
/* { dg-message "\\(2\\) sensitive value 'password' written to output file; acquired at \\(1\\)" "event" { target *-*-* } .-1 } */ |
| 35 |
} |
| 36 |
|
| 37 |
char test_4 (FILE *logfile) |
| 38 |
{ |
| 39 |
char *password = getpass (">"); /* { dg-message "\\(1\\) sensitive value acquired here" } */ |
| 40 |
fwrite (password, strlen (password), 1, logfile); /* { dg-warning "sensitive value 'password' written to output file \\\[CWE-532\\\]" "warning" } */ |
| 41 |
/* { dg-message "\\(2\\) sensitive value 'password' written to output file; acquired at \\(1\\)" "event" { target *-*-* } .-1 } */ |
| 42 |
} |
| 43 |
|
| 44 |
static void called_by_test_5 (const char *value) |
| 45 |
{ |
| 46 |
printf ("%s", value); /* { dg-warning "sensitive value 'value' written to output file \\\[CWE-532\\\]" } */ |
| 47 |
} |
| 48 |
|
| 49 |
char test_5 (FILE *logfile) |
| 50 |
{ |
| 51 |
char *password = getpass (">"); |
| 52 |
called_by_test_5 (password); /* { dg-message "passing sensitive value 'password' in call to 'called_by_test_5' from 'test_5'" } */ |
| 53 |
} |
| 54 |
|
| 55 |
static char *called_by_test_6 (void) |
| 56 |
{ |
| 57 |
return getpass (">"); /* { dg-message "sensitive value acquired here" } */ |
| 58 |
} |
| 59 |
|
| 60 |
char test_6 (FILE *logfile) |
| 61 |
{ |
| 62 |
char *password = called_by_test_6 (); /* { dg-message "returning sensitive value to 'test_6' from 'called_by_test_6'" } */ |
| 63 |
printf ("%s", password); /* { dg-warning "sensitive value 'password' written to output file \\\[CWE-532\\\]" } */ |
| 64 |
} |
| 65 |
|
| 66 |
/* TODO: strdup etc, strcpy, memcpy, etc. */ |