1 // RUN: %clang_analyze_cc1 -triple i686-unknown-linux -analyzer-checker=alpha.security.MmapWriteExec -analyzer-config alpha.security.MmapWriteExec:MmapProtExec=1 -analyzer-config alpha.security.MmapWriteExec:MmapProtRead=4 -DUSE_ALTERNATIVE_PROT_EXEC_DEFINITION -verify %s
2 // RUN: %clang_analyze_cc1 -triple x86_64-unknown-apple-darwin10 -analyzer-checker=alpha.security.MmapWriteExec -verify %s
4 #define PROT_WRITE 0x02
5 #ifndef USE_ALTERNATIVE_PROT_EXEC_DEFINITION
10 #define PROT_READ 0x04
12 #define MAP_PRIVATE 0x0002
13 #define MAP_ANON 0x1000
14 #define MAP_FIXED 0x0010
15 #define NULL ((void *)0)
17 typedef __typeof(sizeof(int)) size_t;
18 void *mmap(void *, size_t, int, int, int, long);
19 int mprotect(void *, size_t, int);
23 void *a
= mmap(NULL
, 16, PROT_READ
| PROT_EXEC
, MAP_PRIVATE
| MAP_ANON
, -1, 0); // no-warning
24 void *b
= mmap(a
, 16, PROT_READ
| PROT_WRITE
, MAP_PRIVATE
| MAP_FIXED
| MAP_ANON
, -1, 0); // no-warning
25 void *c
= mmap(NULL
, 32, PROT_READ
| PROT_WRITE
| PROT_EXEC
, MAP_PRIVATE
| MAP_ANON
, -1, 0); // expected-warning{{Both PROT_WRITE and PROT_EXEC flags are set. This can lead to exploitable memory regions, which could be overwritten with malicious code}}
33 void *(*callm
)(void *, size_t, int, int, int, long);
35 int prot
= PROT_WRITE
| PROT_EXEC
;
36 (void)callm(NULL
, 1024, prot
, MAP_PRIVATE
| MAP_ANON
, -1, 0); // expected-warning{{Both PROT_WRITE and PROT_EXEC flags are set. This can lead to exploitable memory regions, which could be overwritten with malicious code}}
41 void *p
= mmap(NULL
, 1024, PROT_WRITE
, MAP_PRIVATE
| MAP_ANON
, -1, 0); // no-warning
42 int m
= mprotect(p
, 1024, PROT_WRITE
| PROT_EXEC
); // expected-warning{{Both PROT_WRITE and PROT_EXEC flags are set. This can lead to exploitable memory regions, which could be overwritten with malicious code}}
46 // gh62285: no crash on non concrete arg 'prot'
47 void *gh62285(void *addr
, int prot
)
49 return mmap(addr
, 1, prot
, 1, 1, 1);