Skip to content

Commit 274eb1e

Browse files
committed
Add package Banned for C
1 parent 2122a3d commit 274eb1e

File tree

107 files changed

+4039
-13
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

107 files changed

+4039
-13
lines changed

.vscode/tasks.json

+1
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@
163163
"type": "pickString",
164164
"options": [
165165
"Allocations",
166+
"Banned",
166167
"BannedFunctions",
167168
"BannedSyntax",
168169
"BannedTypes",

c/cert/src/rules/ENV33-C/DoNotCallSystem.md

+301
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @id c/cert/do-not-call-system
3+
* @name ENV33-C: Do not call system()
4+
* @description Use of the 'system()' function may result in exploitable vulnerabilities.
5+
* @kind problem
6+
* @precision very-high
7+
* @problem.severity error
8+
* @tags external/cert/id/env33-c
9+
* security
10+
* external/cert/obligtion/rule
11+
*/
12+
13+
import cpp
14+
import codingstandards.c.cert
15+
import semmle.code.cpp.security.CommandExecution
16+
17+
from FunctionCall call, SystemFunction target
18+
where
19+
not isExcluded(call, BannedPackage::doNotCallSystemQuery()) and
20+
call.getTarget() = target and
21+
// Exclude calls to `system` with a `NULL` pointer, because it is allowed to determine the presence of a command processor.
22+
(target.getName() = "system" implies not call.getAnArgument().(Literal).getValue() = "0")
23+
select call, "Call to banned function $@.", target, target.getName()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
| test.c:10:3:10:8 | call to system | Call to banned function $@. | test.c:4:5:4:10 | system | system |
2+
| test.c:12:8:12:12 | call to popen | Call to banned function $@. | test.c:6:7:6:11 | popen | popen |
3+
| test.c:20:3:20:8 | call to system | Call to banned function $@. | test.c:4:5:4:10 | system | system |
4+
| test.c:21:3:21:8 | call to system | Call to banned function $@. | test.c:4:5:4:10 | system | system |
5+
| test.c:22:3:22:7 | call to popen | Call to banned function $@. | test.c:6:7:6:11 | popen | popen |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/ENV33-C/DoNotCallSystem.ql
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
c/common/test/rules/systemused/SystemUsed.ql

c/cert/test/rules/ENV33-C/test.c

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
typedef struct _FILE FILE;
2+
#define NULL (void *)0
3+
4+
int system(const char *);
5+
void abort(void);
6+
FILE *popen(const char *, const char *);
7+
8+
void f1(const char *p1) {
9+
FILE *l1;
10+
system(p1); // NON_COMPLIANT
11+
abort();
12+
l1 = popen("ls *", "r"); // NON_COMPLIANT
13+
}
14+
15+
void f2() {
16+
const int *l1 = NULL;
17+
18+
system(0); // COMPLIANT
19+
system(NULL); // COMPLIANT
20+
system(l1); // NON_COMPLIANT
21+
system("ls -la"); // NON_COMPLIANT
22+
popen(NULL, NULL); // NON_COMPLIANT
23+
}

c/common/test/includes/standard-library/stdarg.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ extern "C" {
99

1010
#include <bits/alltypes.h>
1111

12-
#define va_start(v,l) __builtin_va_start(v,l)
13-
#define va_end(v) __builtin_va_end(v)
14-
#define va_arg(v,l) __builtin_va_arg(v,l)
15-
#define va_copy(d,s) __builtin_va_copy(d,s)
12+
#define va_start(v, l) __builtin_va_start(v, l)
13+
#define va_end(v) __builtin_va_end(v)
14+
#define va_arg(v, l) __builtin_va_arg(v, l)
15+
#define va_copy(d, s) __builtin_va_copy(d, s)
1616

1717
#ifdef __cplusplus
1818
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| test.c:6:13:6:22 | ... , ... | Use of banned ',' expression. |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// GENERATED FILE - DO NOT MODIFY
2+
import codingstandards.cpp.rules.commaoperatorused.CommaOperatorUsed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include <stdlib.h>
2+
int f1();
3+
4+
void f2() {
5+
int l1 = 10;
6+
int l2 = (l1++, ++l1); // NON_COMPLIANT
7+
f1(); // COMPLIANT
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @id c/misra/comma-operator-should-not-be-used
3+
* @name RULE-12-3: The comma operator should not be used
4+
* @description Use of the comma operator may affect the readability of the code.
5+
* @kind problem
6+
* @precision very-high
7+
* @problem.severity recommendation
8+
* @tags external/misra/id/rule-12-3
9+
* readability
10+
* external/misra/obligation/advisory
11+
*/
12+
13+
import cpp
14+
import codingstandards.c.misra
15+
import codingstandards.cpp.rules.commaoperatorused.CommaOperatorUsed
16+
17+
class CommaOperatorShouldNotBeUsedQuery extends CommaOperatorUsedSharedQuery {
18+
CommaOperatorShouldNotBeUsedQuery() {
19+
this = BannedPackage::commaOperatorShouldNotBeUsedQuery()
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @id c/misra/features-of-stdargh-used
3+
* @name RULE-17-1: The features of <stdarg.h> shall not be used
4+
* @description The use of the features of '<stdarg.h> may result in undefined behaviour.
5+
* @kind problem
6+
* @precision very-high
7+
* @problem.severity error
8+
* @tags external/misra/id/rule-17-1
9+
* correctness
10+
* external/misra/obligation/required
11+
*/
12+
13+
import cpp
14+
import codingstandards.c.misra
15+
16+
from Locatable use, string name, string kind
17+
where
18+
not isExcluded(use, BannedPackage::featuresOfStdarghUsedQuery()) and
19+
(
20+
exists(VarArgsExpr va | use = va and name = va.toString() and kind = "built-in operation")
21+
or
22+
exists(Variable v |
23+
v.getType().getName() = "va_list" and
24+
name = "va_list" and
25+
use = v and
26+
kind = "type"
27+
)
28+
)
29+
select use, "Use of banned " + kind + " " + name + "."
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @id c/misra/union-keyword-should-not-be-used
3+
* @name RULE-19-2: The union keyword should not be used
4+
* @description The use of 'union' may result in undefined behaviour.
5+
* @kind problem
6+
* @precision very-high
7+
* @problem.severity warning
8+
* @tags external/misra/id/rule-19-2
9+
* correctness
10+
* external/misra/obligation/advisory
11+
*/
12+
13+
import cpp
14+
import codingstandards.c.misra
15+
16+
from Union u
17+
where not isExcluded(u, BannedPackage::unionKeywordShouldNotBeUsedQuery())
18+
select u, "Use of banned 'union' keyword."
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @id c/misra/standard-library-time-and-date-functions-used
3+
* @name RULE-21-10: The Standard Library time and date functions shall not be used
4+
* @description The use of date and time functions may result in undefined behaviour.
5+
* @kind problem
6+
* @precision very-high
7+
* @problem.severity error
8+
* @tags external/misra/id/rule-21-10
9+
* correctness
10+
* external/misra/obligation/required
11+
*/
12+
13+
import cpp
14+
import codingstandards.c.misra
15+
16+
from Function f, FunctionCall fc
17+
where
18+
not isExcluded(fc, BannedPackage::standardLibraryTimeAndDateFunctionsUsedQuery()) and
19+
(
20+
fc.getTarget() = f and
21+
(
22+
f.getFile().getBaseName() = "time.h"
23+
or
24+
f.getName() = "wcsftime" and
25+
f.getFile().getBaseName() = "wchar.h"
26+
)
27+
)
28+
select fc, "Call to banned function $@.", f, f.getName()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @id c/misra/standard-header-file-tgmathh-used
3+
* @name RULE-21-11: The standard header file <tgmath.h> shall not be used
4+
* @description The use of the header file '<tgmath.h>' may result in undefined behaviour.
5+
* @kind problem
6+
* @precision very-high
7+
* @problem.severity error
8+
* @tags external/misra/id/rule-21-11
9+
* correctness
10+
* external/misra/obligation/required
11+
*/
12+
13+
import cpp
14+
import codingstandards.c.misra
15+
16+
from Macro m, MacroInvocation mi
17+
where
18+
not isExcluded(mi, BannedPackage::standardHeaderFileTgmathhUsedQuery()) and
19+
mi.getMacro() = m and
20+
m.getFile().getBaseName() = "tgmath.h"
21+
select mi, "Call to banned macro $@.", m, m.getName()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* @id c/misra/exception-handling-features-of-fenvh-used
3+
* @name RULE-21-12: The exception handling features of <fenv.h> should not be used
4+
* @description The use of the exception handling features of '<fenv.h>' may result in undefined
5+
* behaviour.
6+
* @kind problem
7+
* @precision very-high
8+
* @problem.severity warning
9+
* @tags external/misra/id/rule-21-12
10+
* correctness
11+
* external/misra/obligation/advisory
12+
*/
13+
14+
import cpp
15+
import codingstandards.c.misra
16+
17+
class FPExceptionHandlingFunction extends Function {
18+
FPExceptionHandlingFunction() {
19+
this.hasName([
20+
"feclearexcept", "fegetexceptflag", "feraiseexcept", "fesetexceptflag", "fetestexcept"
21+
]) and
22+
this.getFile().getBaseName() = "fenv.h"
23+
}
24+
}
25+
26+
class FPExceptionHandlingMacro extends Macro {
27+
FPExceptionHandlingMacro() {
28+
this.hasName([
29+
"FE_INEXACT", "FE_DIVBYZERO", "FE_UNDERFLOW", "FE_OVERFLOW", "FE_INVALID", "FE_ALL_EXCEPT"
30+
]) and
31+
this.getFile().getBaseName() = "fenv.h"
32+
}
33+
}
34+
35+
from Locatable call, Locatable def, string name, string kind
36+
where
37+
not isExcluded(call, BannedPackage::exceptionHandlingFeaturesOfFenvhUsedQuery()) and
38+
(
39+
exists(FPExceptionHandlingFunction f |
40+
def = f and
41+
call = f.getACallToThisFunction() and
42+
name = f.getName() and
43+
kind = "function"
44+
)
45+
or
46+
exists(FPExceptionHandlingMacro m |
47+
def = m and
48+
call = m.getAnInvocation() and
49+
name = m.getName() and
50+
kind = "macro"
51+
)
52+
)
53+
select call, "Call to banned " + kind + " $@.", def, name
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @id c/misra/system-of-stdlibh-used
3+
* @name RULE-21-21: The Standard Library function system of <stdlib.h> shall not be used
4+
* @description They use of the 'system()' function from '<stdlib.h> may result in exploitable
5+
* vulnerabilities.
6+
* @kind problem
7+
* @precision very-high
8+
* @problem.severity error
9+
* @tags external/misra/id/rule-21-21
10+
* security
11+
* external/misra/obligation/required
12+
*/
13+
14+
import cpp
15+
import codingstandards.c.misra
16+
17+
from FunctionCall call, Function target
18+
where
19+
not isExcluded(call, BannedPackage::systemOfStdlibhUsedQuery()) and
20+
call.getTarget() = target and
21+
target.hasGlobalOrStdName("system")
22+
select call, "Call to banned function $@.", target, target.getName()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @id c/misra/memory-alloc-dealloc-functions-of-stdlibh-used
3+
* @name RULE-21-3: The memory allocation and deallocation functions of <stdlib.h> shall not be used
4+
* @description The use of memory allocation and deallocation in '<stdlib.h>' may result in
5+
* undefined behaviour.
6+
* @kind problem
7+
* @precision very-high
8+
* @problem.severity error
9+
* @tags external/misra/id/rule-21-3
10+
* correctness
11+
* security
12+
* external/misra/obligation/required
13+
*/
14+
15+
import cpp
16+
import codingstandards.c.misra
17+
import cpp
18+
import codingstandards.c.misra
19+
import semmle.code.cpp.models.interfaces.Allocation
20+
import semmle.code.cpp.models.interfaces.Deallocation
21+
22+
from Expr e, string type
23+
where
24+
not isExcluded(e, BannedPackage::memoryAllocDeallocFunctionsOfStdlibhUsedQuery()) and
25+
(
26+
e.(FunctionCall).getTarget().(AllocationFunction).requiresDealloc() and
27+
type = "allocation"
28+
or
29+
e instanceof DeallocationExpr and
30+
not e.(FunctionCall).getTarget() instanceof AllocationFunction and
31+
type = "deallocation"
32+
)
33+
select e, "Use of banned dynamic memory " + type + "."
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* @id c/misra/standard-header-file-used-setjmph
3+
* @name RULE-21-4: The standard header file shall not be used <setjmp.h>
4+
* @description The use of features of '<setjmp.h>' may result in undefined behaviour.
5+
* @kind problem
6+
* @precision very-high
7+
* @problem.severity error
8+
* @tags external/misra/id/rule-21-4
9+
* correctness
10+
* external/misra/obligation/required
11+
*/
12+
13+
import cpp
14+
import codingstandards.c.misra
15+
16+
class SetJmp extends Macro {
17+
SetJmp() {
18+
this.hasName("setjmp") and
19+
this.getFile().getAbsolutePath().matches("%setjmp.h")
20+
}
21+
}
22+
23+
class LongJmp extends Function {
24+
LongJmp() {
25+
this.hasName("longjmp") and
26+
this.getFile().getAbsolutePath().matches("%setjmp.h")
27+
}
28+
}
29+
30+
from Locatable use, Locatable feature, string name
31+
where
32+
not isExcluded(use, BannedPackage::standardHeaderFileUsedSetjmphQuery()) and
33+
(
34+
exists(SetJmp setjmp |
35+
feature = setjmp and
36+
use = setjmp.getAnInvocation() and
37+
name = "setjmp"
38+
)
39+
or
40+
exists(LongJmp longjmp |
41+
feature = longjmp and
42+
use = longjmp.getACallToThisFunction() and
43+
name = "longjmp"
44+
)
45+
)
46+
select use, "Use of $@.", feature, name
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @id c/misra/standard-header-file-used-signalh
3+
* @name RULE-21-5: The standard header file shall not be used <signal.h>
4+
* @description The use of features of '<signal.h>' may result in undefined behaviour.
5+
* @kind problem
6+
* @precision very-high
7+
* @problem.severity error
8+
* @tags external/misra/id/rule-21-5
9+
* correctness
10+
* external/misra/obligation/required
11+
*/
12+
13+
import cpp
14+
import codingstandards.c.misra
15+
16+
from Function f, FunctionCall fc
17+
where
18+
not isExcluded(fc, BannedPackage::standardHeaderFileUsedSignalhQuery()) and
19+
fc.getTarget() = f and
20+
f.getFile().getBaseName() = "signal.h"
21+
select fc, "Call to banned function $@.", f, f.getName()

0 commit comments

Comments
 (0)