Skip to content

Commit 7348db1

Browse files
kvbridgerseinvbri
andauthored
Taint example (#5)
* Add option for pthread modeling, default false. Update LITs Add option for pthread modeling set to a default of false. Uses must opt-in. Update LITs so all pass. * Add example taint analyis improvement demo test Also, add additional LIT test for pointer deref. --------- Co-authored-by: einvbri <[email protected]>
1 parent 6554b46 commit 7348db1

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// RUN: %clang_analyze_cc1 -Wno-strict-prototypes -Wno-error=implicit-int -verify %s \
2+
// RUN: -analyzer-checker=core,optin.taint.GenericTaint -DPTHREAD_MODEL=1 \
3+
// RUN: -analyzer-checker=debug.ExprInspection -analyzer-config model-pthreads=true
4+
5+
// RUN: %clang_analyze_cc1 -Wno-strict-prototypes -Wno-error=implicit-int -verify %s \
6+
// RUN: -analyzer-checker=core,optin.taint.GenericTaint -DNO_PTHREAD_MODEL=1 \
7+
// RUN: -analyzer-checker=debug.ExprInspection -analyzer-config model-pthreads=false
8+
9+
#define NULL ((void*) 0)
10+
typedef unsigned long int pthread_t;
11+
typedef struct __pthread_attr pthread_attr_t;
12+
int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg);
13+
14+
char *strcat( char *dest, const char *src );
15+
int scanf(const char*, ...);
16+
int system(const char *command);
17+
18+
void *thread_func(void *arg) {
19+
#ifdef PTHREAD_MODEL
20+
system( (char *) arg); // expected-warning {{Untrusted data is passed to a system call (CERT/STR02-C. Sanitize data passed to complex subsystems)}}
21+
#endif
22+
#ifdef NO_PTHREAD_MODEL
23+
system( (char *) arg); // expected-no-diagnostics
24+
#endif
25+
return NULL;
26+
}
27+
28+
// Command Injection Vulnerability Example
29+
void test(void) {
30+
char cmd[2048] = "/bin/cat ";
31+
char filename[1024];
32+
scanf (" %1023[^\n]", filename); // The attacker can inject a shell escape here
33+
strcat(cmd, filename);
34+
pthread_t p1;
35+
pthread_create(&p1, NULL, &thread_func, &cmd);
36+
}
37+
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// RUN: %clang_analyze_cc1 -Wno-strict-prototypes -Wno-error=implicit-int -verify %s \
2+
// RUN: -analyzer-checker=core -DPTHREAD_MODEL=1 \
3+
// RUN: -analyzer-checker=debug.ExprInspection -analyzer-config model-pthreads=true
4+
//
5+
// RUN: %clang_analyze_cc1 -Wno-strict-prototypes -Wno-error=implicit-int -verify %s \
6+
// RUN: -analyzer-checker=core -DNO_PTHREAD_MODEL=1 \
7+
// RUN: -analyzer-checker=debug.ExprInspection -analyzer-config model-pthreads=false
8+
9+
#define NULL ((void*) 0)
10+
11+
typedef unsigned long int pthread_t;
12+
typedef struct __pthread_attr pthread_attr_t;
13+
14+
int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg);
15+
16+
void clang_analyzer_checkInlined(int);
17+
void clang_analyzer_dump(int);
18+
19+
void* thread_function(void* arg)
20+
{
21+
// should expect to fail the test at this line if you set the checkInlined to true
22+
#ifdef PTHREAD_MODEL
23+
clang_analyzer_checkInlined(1); // expected-warning{{TRUE}}
24+
#endif
25+
int *ptr = (int *)arg;
26+
#ifdef PTHREAD_MODEL
27+
clang_analyzer_dump(*ptr); // expected-warning{{1 S32b}}
28+
#endif
29+
#ifdef NO_PTHREAD_MODEL
30+
clang_analyzer_dump(*ptr); // expected-warning-re{{reg_${{[[:digit:]]+}}<int Element{SymRegion{reg_${{[[:digit:]]+}}<void * arg>},0 S64b,int}}}
31+
#endif
32+
return NULL;
33+
}
34+
35+
int test()
36+
{
37+
int i = 1;
38+
pthread_t p1;
39+
pthread_create(&p1, NULL, &thread_function, &i);
40+
return 0;
41+
}
42+

0 commit comments

Comments
 (0)