Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
3 changes: 2 additions & 1 deletion cpp/ql/test/examples/BadLocking/AV Rule 107.qlref
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
jsf/4.13 Functions/AV Rule 107.ql
query: jsf/4.13 Functions/AV Rule 107.ql
postprocess: utils/test/InlineExpectationsTestQuery.ql
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
Best Practices/Hiding/LocalVariableHidesGlobalVariable.ql
query: Best Practices/Hiding/LocalVariableHidesGlobalVariable.ql
postprocess: utils/test/InlineExpectationsTestQuery.ql
6 changes: 3 additions & 3 deletions cpp/ql/test/examples/BadLocking/UnintendedDeclaration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ void test1()

void test2()
{
Lock<Mutex> myLock(); // BAD (interpreted as a function declaration, this does nothing)
Lock<Mutex> myLock(); // $ Alert[cpp/function-in-block] // BAD (interpreted as a function declaration, this does nothing)

// ...
}
Expand All @@ -62,14 +62,14 @@ void test3()

void test4()
{
Lock<Mutex>(myMutex); // BAD (creates an uninitialized variable called `myMutex`, probably not intended)
Lock<Mutex>(myMutex); // $ Alert[cpp/local-variable-hides-global-variable] // BAD (creates an uninitialized variable called `myMutex`, probably not intended)

// ...
}

void test5()
{
Lock<Mutex> myLock(Mutex); // BAD (interpreted as a function declaration, this does nothing)
Lock<Mutex> myLock(Mutex); // $ Alert[cpp/function-in-block] // BAD (interpreted as a function declaration, this does nothing)

// ...
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
experimental/Security/CWE/CWE-020/NoCheckBeforeUnsafePutUser.ql
query: experimental/Security/CWE/CWE-020/NoCheckBeforeUnsafePutUser.ql
postprocess: utils/test/InlineExpectationsTestQuery.ql
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
experimental/Security/CWE/CWE-020/LateCheckOfFunctionArgument.ql
query: experimental/Security/CWE/CWE-020/LateCheckOfFunctionArgument.ql
postprocess: utils/test/InlineExpectationsTestQuery.ql
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ void workFunction_0(char *s) {
char buf[80], buf1[8];
if(len<0) return;
memset(buf,0,len); //GOOD
memset(buf1,0,len1); //BAD
memset(buf1,0,len1); // $ Alert //BAD
if(len1<0) return;
}
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
experimental/Security/CWE/CWE-078/WordexpTainted.ql
query: experimental/Security/CWE/CWE-078/WordexpTainted.ql
postprocess: utils/test/InlineExpectationsTestQuery.ql
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ enum {

int wordexp(const char *restrict s, wordexp_t *restrict p, int flags);

int main(int argc, char** argv) {
int main(int argc, char** argv) { // $ Source
char *filePath = argv[2];

{
// BAD: the user string is injected directly into `wordexp` which performs command substitution

wordexp_t we;
wordexp(filePath, &we, 0);
wordexp(filePath, &we, 0); // $ Alert
}

{
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
experimental/Security/CWE/CWE-1041/FindWrapperFunctions.ql
query: experimental/Security/CWE/CWE-1041/FindWrapperFunctions.ql
postprocess: utils/test/InlineExpectationsTestQuery.ql
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ void myFclose(FILE * fmy)
int main(int argc, char *argv[])
{
fe = fopen("myFile.txt", "wt");
fclose(fe); // BAD
fclose(fe); // $ Alert // BAD
fe = fopen("myFile.txt", "wt");
myFclose(fe); // GOOD
return 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
experimental/Security/CWE/CWE-1126/DeclarationOfVariableWithUnnecessarilyWideScope.ql
query: experimental/Security/CWE/CWE-1126/DeclarationOfVariableWithUnnecessarilyWideScope.ql
postprocess: utils/test/InlineExpectationsTestQuery.ql
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ void workFunction_0(char *s) {
while(intIndex > 2)
{
buf[intIndex] = 1;
int intIndex; // BAD
int intIndex; // $ Alert // BAD
intIndex--;
}
intIndex = 10;
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
experimental/Security/CWE/CWE-1240/CustomCryptographicPrimitive.ql
query: experimental/Security/CWE/CWE-1240/CustomCryptographicPrimitive.ql
postprocess: utils/test/InlineExpectationsTestQuery.ql
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ int strlen(const char *string);

// the following function is homebrew crypto written for this test. This is a bad algorithm
// on multiple levels and should never be used in cryptography.
void encryptString(char *string, unsigned int key) {
void encryptString(char *string, unsigned int key) { // $ Alert
char *ptr = string;
int len = strlen(string);

Expand All @@ -27,7 +27,7 @@ void encryptString(char *string, unsigned int key) {

// the following function is homebrew crypto written for this test. This is a bad algorithm
// on multiple levels and should never be used in cryptography.
void MyEncrypt(const unsigned int *dataIn, unsigned int *dataOut, unsigned int dataSize, unsigned int key[2]) {
void MyEncrypt(const unsigned int *dataIn, unsigned int *dataOut, unsigned int dataSize, unsigned int key[2]) { // $ Alert
unsigned int state[2];
unsigned int t;

Expand All @@ -48,7 +48,7 @@ void MyEncrypt(const unsigned int *dataIn, unsigned int *dataOut, unsigned int d
// the following function resembles an implementation of the AES "mix columns"
// step. It is not accurate, efficient or safe and should never be used in
// cryptography.
void mix_columns(const uint8_t inputs[4], uint8_t outputs[4]) {
void mix_columns(const uint8_t inputs[4], uint8_t outputs[4]) { // $ Alert
// The "mix columns" step takes four bytes as inputs. Each byte represents a
// polynomial with 8 one-bit coefficients, e.g. input bits 00001101
// represent the polynomial x^3 + x^2 + 1. Arithmetic is reduced modulo
Expand Down Expand Up @@ -80,7 +80,7 @@ void mix_columns(const uint8_t inputs[4], uint8_t outputs[4]) {
// the following function resembles initialization of an S-box as may be done
// in an implementation of DES, AES and other encryption algorithms. It is not
// accurate, efficient or safe and should never be used in cryptography.
void init_aes_sbox(unsigned char data[256]) {
void init_aes_sbox(unsigned char data[256]) { // $ Alert
// initialize `data` in a loop using lots of ^, ^= and << operations and
// a few fixed constants.
unsigned int state = 0x12345678;
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
experimental/Security/CWE/CWE-125/DangerousWorksWithMultibyteOrWideCharacters.ql
query: experimental/Security/CWE/CWE-125/DangerousWorksWithMultibyteOrWideCharacters.ql
postprocess: utils/test/InlineExpectationsTestQuery.ql
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ static void badTest1(const char* ptr)
int ret;
int len;
len = strlen(ptr);
for (wchar_t wc; (ret = mbtowc(&wc, ptr, 4)) > 0; len-=ret) { // BAD:we can get unpredictable results
for (wchar_t wc; (ret = mbtowc(&wc, ptr, 4)) > 0; len-=ret) { // $ Alert // BAD:we can get unpredictable results
wprintf(L"%lc", wc);
ptr += ret;
}
Expand All @@ -73,7 +73,7 @@ static void badTest2(const char* ptr)
int ret;
int len;
len = strlen(ptr);
for (wchar_t wc; (ret = mbtowc(&wc, ptr, sizeof(wchar_t))) > 0; len-=ret) { // BAD:we can get unpredictable results
for (wchar_t wc; (ret = mbtowc(&wc, ptr, sizeof(wchar_t))) > 0; len-=ret) { // $ Alert // BAD:we can get unpredictable results
wprintf(L"%lc", wc);
ptr += ret;
}
Expand Down Expand Up @@ -103,7 +103,7 @@ static void badTest3(const char* ptr,int wc_len)
len = wc_len;
wchar_t *wc = new wchar_t[wc_len];
while (*ptr && len > 0) {
ret = mbtowc(wc, ptr, MB_CUR_MAX); // BAD
ret = mbtowc(wc, ptr, MB_CUR_MAX); // $ Alert // BAD
if (ret <0)
break;
if (ret == 0 || ret > len)
Expand All @@ -120,7 +120,7 @@ static void badTest4(const char* ptr,int wc_len)
len = wc_len;
wchar_t *wc = new wchar_t[wc_len];
while (*ptr && len > 0) {
ret = mbtowc(wc, ptr, 16); // BAD
ret = mbtowc(wc, ptr, 16); // $ Alert // BAD
if (ret <0)
break;
if (ret == 0 || ret > len)
Expand All @@ -137,7 +137,7 @@ static void badTest5(const char* ptr,int wc_len)
len = wc_len;
wchar_t *wc = new wchar_t[wc_len];
while (*ptr && len > 0) {
ret = mbtowc(wc, ptr, sizeof(wchar_t)); // BAD
ret = mbtowc(wc, ptr, sizeof(wchar_t)); // $ Alert // BAD
if (ret <0)
break;
if (ret == 0 || ret > len)
Expand All @@ -155,7 +155,7 @@ static void badTest6(const char* ptr,int wc_len)
len = wc_len;
wchar_t *wc = new wchar_t[wc_len];
while (*ptr && wc_len > 0) {
ret = mbtowc(wc, ptr, wc_len); // BAD
ret = mbtowc(wc, ptr, wc_len); // $ Alert // BAD
if (ret <0)
if (checkErrors()) {
++ptr;
Expand All @@ -178,7 +178,7 @@ static void badTest7(const char* ptr,int wc_len)
len = wc_len;
wchar_t *wc = new wchar_t[wc_len];
while (*ptr && wc_len > 0) {
ret = mbtowc(wc, ptr, len); // BAD
ret = mbtowc(wc, ptr, len); // $ Alert // BAD
if (ret <0)
break;
if (ret == 0 || ret > len)
Expand All @@ -194,7 +194,7 @@ static void badTest8(const char* ptr,wchar_t *wc)
int len;
len = strlen(ptr);
while (*ptr && len > 0) {
ret = mbtowc(wc, ptr, len); // BAD
ret = mbtowc(wc, ptr, len); // $ Alert // BAD
if (ret <0)
break;
if (ret == 0 || ret > len)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ void* calloc (size_t num, size_t size);
void* malloc (size_t size);

static void badTest1(void *src, int size) {
WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)src, -1, (LPSTR)src, size, 0, 0); // BAD
MultiByteToWideChar(CP_ACP, 0, (LPCSTR)src, -1, (LPCWSTR)src, 30); // BAD
WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)src, -1, (LPSTR)src, size, 0, 0); // $ Alert // BAD
MultiByteToWideChar(CP_ACP, 0, (LPCSTR)src, -1, (LPCWSTR)src, 30); // $ Alert // BAD
}
void goodTest2(){
wchar_t src[] = L"0123456789ABCDEF";
Expand All @@ -42,7 +42,7 @@ void goodTest2(){
static void badTest2(){
wchar_t src[] = L"0123456789ABCDEF";
char dst[16];
WideCharToMultiByte(CP_UTF8, 0, src, -1, dst, 16, NULL, NULL); // BAD
WideCharToMultiByte(CP_UTF8, 0, src, -1, dst, 16, NULL, NULL); // $ Alert // BAD
printf("%s\n", dst);
}
static void goodTest3(){
Expand All @@ -55,7 +55,7 @@ static void badTest3(){
char src[] = "0123456789ABCDEF";
int size = MultiByteToWideChar(CP_UTF8, 0, src,sizeof(src),NULL,0);
wchar_t * dst = (wchar_t*)calloc(size + 1, 1);
MultiByteToWideChar(CP_UTF8, 0, src, -1, dst, size+1); // BAD
MultiByteToWideChar(CP_UTF8, 0, src, -1, dst, size+1); // $ Alert // BAD
}
static void goodTest4(){
char src[] = "0123456789ABCDEF";
Expand All @@ -67,13 +67,13 @@ static void badTest4(){
char src[] = "0123456789ABCDEF";
int size = MultiByteToWideChar(CP_UTF8, 0, src,sizeof(src),NULL,0);
wchar_t * dst = (wchar_t*)malloc(size + 1);
MultiByteToWideChar(CP_UTF8, 0, src, -1, dst, size+1); // BAD
MultiByteToWideChar(CP_UTF8, 0, src, -1, dst, size+1); // $ Alert // BAD
}
static int goodTest5(void *src){
return WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)src, -1, 0, 0, 0, 0); // GOOD
}
static int badTest5 (void *src) {
return WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)src, -1, 0, 3, 0, 0); // BAD
return WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)src, -1, 0, 3, 0, 0); // $ Alert // BAD
}
static void goodTest6(WCHAR *src)
{
Expand All @@ -90,6 +90,6 @@ static void goodTest6(WCHAR *src)
static void badTest6(WCHAR *src)
{
char dst[5] ="";
WideCharToMultiByte(CP_ACP, 0, src, -1, dst, 260, 0, 0); // BAD
WideCharToMultiByte(CP_ACP, 0, src, -1, dst, 260, 0, 0); // $ Alert // BAD
printf("%s\n", dst);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ size_t mbsrtowcs(wchar_t *wcstr,const char *mbstr,size_t count, mbstate_t *mbsta


static void badTest1(void *src, int size) {
mbstowcs((wchar_t*)src,(char*)src,size); // BAD
mbstowcs((wchar_t*)src,(char*)src,size); // $ Alert // BAD
_locale_t locale;
_mbstowcs_l((wchar_t*)src,(char*)src,size,locale); // BAD
_mbstowcs_l((wchar_t*)src,(char*)src,size,locale); // $ Alert // BAD
mbstate_t *mbstate;
mbsrtowcs((wchar_t*)src,(char*)src,size,mbstate); // BAD
mbsrtowcs((wchar_t*)src,(char*)src,size,mbstate); // $ Alert // BAD
}
static void goodTest2(){
char src[] = "0123456789ABCDEF";
Expand All @@ -32,7 +32,7 @@ static void goodTest2(){
static void badTest2(){
char src[] = "0123456789ABCDEF";
wchar_t dst[16];
mbstowcs(dst, src,16); // BAD
mbstowcs(dst, src,16); // $ Alert // BAD
printf("%s\n", dst);
}
static void goodTest3(){
Expand All @@ -45,7 +45,7 @@ static void badTest3(){
char src[] = "0123456789ABCDEF";
int size = mbstowcs(NULL, src,NULL);
wchar_t * dst = (wchar_t*)calloc(size + 1, 1);
mbstowcs(dst, src,size+1); // BAD
mbstowcs(dst, src,size+1); // $ Alert // BAD
}
static void goodTest4(){
char src[] = "0123456789ABCDEF";
Expand All @@ -57,13 +57,13 @@ static void badTest4(){
char src[] = "0123456789ABCDEF";
int size = mbstowcs(NULL, src,NULL);
wchar_t * dst = (wchar_t*)malloc(size + 1);
mbstowcs(dst, src,size+1); // BAD
mbstowcs(dst, src,size+1); // $ Alert // BAD
}
static int goodTest5(void *src){
return mbstowcs(NULL, (char*)src,NULL); // GOOD
}
static int badTest5 (void *src) {
return mbstowcs(NULL, (char*)src,3); // BAD
return mbstowcs(NULL, (char*)src,3); // $ Alert // BAD
}
static void goodTest6(void *src){
wchar_t dst[5];
Expand All @@ -77,6 +77,6 @@ static void goodTest6(void *src){
}
static void badTest6(void *src){
wchar_t dst[5];
mbstowcs(dst, (char*)src,260); // BAD
mbstowcs(dst, (char*)src,260); // $ Alert // BAD
printf("%s\n", dst);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ static size_t badTest1(unsigned char *src){
int cb = 0;
unsigned char dst[50];
while( cb < sizeof(dst) )
dst[cb++]=*src++; // BAD
dst[cb++]=*src++; // $ Alert // BAD
return _mbclen(dst);
}
static void goodTest2(unsigned char *src){
Expand All @@ -33,7 +33,7 @@ static void badTest2(unsigned char *src){
unsigned char dst[50];
while( cb < sizeof(dst) )
{
_mbccpy(dst+cb,src); // BAD
_mbccpy(dst+cb,src); // $ Alert // BAD
cb+=_mbclen(src);
src=_mbsinc(src);
}
Expand All @@ -44,5 +44,5 @@ static void goodTest3(){
}
static void badTest3(){
wchar_t name[50];
name[sizeof(name) - 1] = L'\0'; // BAD
name[sizeof(name) - 1] = L'\0'; // $ Alert // BAD
}
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
experimental/Security/CWE/CWE-190/AllocMultiplicationOverflow.ql
query: experimental/Security/CWE/CWE-190/AllocMultiplicationOverflow.ql
postprocess: utils/test/InlineExpectationsTestQuery.ql
Loading
Loading