Skip to content

Commit 7d267e8

Browse files
authored
Sast CPP Flag integration Tests (#195)
1 parent 9a79fb6 commit 7d267e8

File tree

2 files changed

+164
-0
lines changed

2 files changed

+164
-0
lines changed

audit_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,39 @@ func addDummyPackageDescriptor(t *testing.T, hasPackageJson bool) {
473473

474474
// JAS
475475

476+
func TestXrayAuditSastCppFlagSimpleJson(t *testing.T) {
477+
output := testAuditC(t, string(format.SimpleJson), true)
478+
securityTestUtils.VerifySimpleJsonJasResults(t, output, 1, 0, 0, 0, 0, 0, 0, 0, 0)
479+
480+
}
481+
482+
func TestXrayAuditWithoutSastCppFlagSimpleJson(t *testing.T) {
483+
output := testAuditC(t, string(format.SimpleJson), false)
484+
securityTestUtils.VerifySimpleJsonJasResults(t, output, 0, 0, 0, 0, 0, 0, 0, 0, 0)
485+
}
486+
487+
// Helper for both C & Cpp Sast scans tests
488+
func testAuditC(t *testing.T, format string, enableCppFlag bool) string {
489+
cliToRun, cleanUp := securityTestUtils.InitTestWithMockCommandOrParams(t, getJasAuditMockCommand)
490+
defer cleanUp()
491+
securityTestUtils.InitSecurityTest(t, scangraph.GraphScanMinXrayVersion)
492+
tempDirPath, createTempDirCallback := coreTests.CreateTempDirWithCallbackAndAssert(t)
493+
defer createTempDirCallback()
494+
cProjectPath := filepath.Join(filepath.FromSlash(securityTestUtils.GetTestResourcesPath()), "projects", "package-managers", "c")
495+
// Copy the c project from the testdata to a temp dir
496+
assert.NoError(t, biutils.CopyDir(cProjectPath, tempDirPath, true, nil))
497+
prevWd := securityTestUtils.ChangeWD(t, tempDirPath)
498+
defer clientTests.ChangeDirAndAssert(t, prevWd)
499+
watchName, deleteWatch := securityTestUtils.CreateTestWatch(t, "audit-policy", "audit-watch", xrayUtils.High)
500+
defer deleteWatch()
501+
if enableCppFlag {
502+
unsetEnv := clientTests.SetEnvWithCallbackAndAssert(t, "JFROG_SAST_ENABLE_CPP", "1")
503+
defer unsetEnv()
504+
}
505+
args := []string{"audit", "--licenses", "--vuln", "--format=" + format, "--watches=" + watchName, "--fail=false"}
506+
return cliToRun.WithoutCredentials().RunCliCmdWithOutput(t, args...)
507+
}
508+
476509
func TestXrayAuditNotEntitledForJas(t *testing.T) {
477510
cliToRun, cleanUp := securityTestUtils.InitTestWithMockCommandOrParams(t, getNoJasAuditMockCommand)
478511
defer cleanUp()
@@ -483,6 +516,22 @@ func TestXrayAuditNotEntitledForJas(t *testing.T) {
483516
securityTestUtils.VerifySimpleJsonJasResults(t, output, 0, 0, 0, 0, 0, 0, 0, 0, 0)
484517
}
485518

519+
func getJasAuditMockCommand() components.Command {
520+
return components.Command{
521+
Name: docs.Audit,
522+
Flags: docs.GetCommandFlags(docs.Audit),
523+
Action: func(c *components.Context) error {
524+
auditCmd, err := cli.CreateAuditCmd(c)
525+
if err != nil {
526+
return err
527+
}
528+
// Disable Jas for this test
529+
auditCmd.SetUseJas(true)
530+
return progressbar.ExecWithProgress(auditCmd)
531+
},
532+
}
533+
}
534+
486535
func getNoJasAuditMockCommand() components.Command {
487536
return components.Command{
488537
Name: docs.Audit,
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/*
2+
Author: Hardik Shah
3+
4+
Web: http://hardik05.wordpress.com
5+
*/
6+
7+
//a vulnerable c program to explain common vulnerability types
8+
//fuzz with AFL
9+
10+
#include<stdio.h>
11+
#include<stdlib.h>
12+
#include<string.h>
13+
14+
struct Image
15+
{
16+
char header[4];
17+
int width;
18+
int height;
19+
char data[10];
20+
};
21+
22+
int ProcessImage(char* filename){
23+
FILE *fp;
24+
char ch;
25+
struct Image img;
26+
27+
fp = fopen(filename,"r"); //Statement 1
28+
29+
if(fp == NULL)
30+
{
31+
printf("\nCan't open file or file doesn't exist.\r\n");
32+
exit(0);
33+
}
34+
35+
36+
while(fread(img,sizeof(img),1,fp)>0)
37+
{
38+
//if(strcmp(img.header,"IMG")==0)
39+
//{
40+
printf("\n\tHeader\twidth\theight\tdata\t\r\n");
41+
42+
printf("\n\t%s\t%d\t%d\t%s\r\n",img.header,img.width,img.height,img.data);
43+
44+
45+
//integer overflow 0x7FFFFFFF+1=0
46+
//0x7FFFFFFF+2 = 1
47+
//will cause very large/small memory allocation.
48+
int size1 = img.width + img.height;
49+
char* buff1=(char*)malloc(size1);
50+
51+
//heap buffer overflow
52+
memcpy(buff1,img.data,sizeof(img.data));
53+
free(buff1);
54+
//double free
55+
if (size1/2==0){
56+
free(buff1);
57+
}
58+
else{
59+
//use after free
60+
if(size1/3 == 0){
61+
buff1[0]='a';
62+
}
63+
}
64+
65+
66+
//integer underflow 0-1=-1
67+
//negative so will cause very large memory allocation
68+
int size2 = img.width - img.height+100;
69+
//printf("Size1:%d",size1);
70+
char* buff2=(char*)malloc(size2);
71+
72+
//heap buffer overflow
73+
memcpy(buff2,img.data,sizeof(img.data));
74+
75+
//divide by zero
76+
int size3= img.width/img.height;
77+
//printf("Size2:%d",size3);
78+
79+
char buff3[10];
80+
char* buff4 =(char*)malloc(size3);
81+
something(buff4);
82+
memcpy(buff4,img.data,sizeof(img.data));
83+
84+
//OOBR read bytes past stack/heap buffer
85+
char OOBR = buff3[size3];
86+
char OOBR_heap = buff4[size3];
87+
88+
//OOBW write bytes past stack/heap buffer
89+
buff3[size3]='c';
90+
buff4[size3]='c';
91+
92+
if(size3>10){
93+
//memory leak here
94+
buff4=0;
95+
}
96+
else{
97+
free(buff4);
98+
}
99+
100+
free(buff2);
101+
//}
102+
//else
103+
// printf("invalid header\r\n");
104+
105+
}
106+
fclose(fp);
107+
108+
return 0;
109+
}
110+
111+
int main(int argc,char **argv)
112+
{
113+
ProcessImage(argv[1]);
114+
115+
}

0 commit comments

Comments
 (0)