Skip to content

Commit 05a4798

Browse files
author
dilanbhalla
committed
working on implementing pr fixes
1 parent d201c4b commit 05a4798

File tree

2 files changed

+91
-21
lines changed

2 files changed

+91
-21
lines changed

cpp/ql/src/experimental/Security/CWE/CWE-359/PrivateCleartextBufferWrite.ql

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,61 @@
99
* external/cwe/cwe-312
1010
*/
1111

12+
1213
import cpp
1314
import semmle.code.cpp.security.BufferWrite
14-
import semmle.code.cpp.security.TaintTracking
1515
import experimental.semmle.code.cpp.security.PrivateData
16-
import TaintedWithPath
16+
import semmle.code.cpp.dataflow.TaintTracking
17+
18+
/** A call to any method whose name suggests that it encodes or encrypts the parameter. */
19+
class ProtectSanitizer extends DataFlow::ExprNode {
20+
ProtectSanitizer() {
21+
exists(Function m, string s |
22+
this.getExpr().(FunctionCall).getTarget() = m and
23+
m.getName().regexpMatch("(?i).*" + s + ".*")
24+
|
25+
s = "protect" or s = "encode" or s = "encrypt"
26+
)
27+
}
28+
}
1729

18-
class Configuration extends TaintTrackingConfiguration {
19-
override predicate isSink(Element tainted) { exists(BufferWrite w | w.getASource() = tainted) }
30+
class BufferConfig extends TaintTracking::Configuration {
31+
BufferConfig() {
32+
this = "Buffer store configuration"
33+
}
34+
override predicate isSource(DataFlow::Node source) { source.asExpr() instanceof PrivateDataExpr }
35+
override predicate isSink(DataFlow::Node sink) { exists(BufferWrite w | sink.asExpr() = w.getDest()) }
36+
override predicate isSanitizer(DataFlow::Node node) { node instanceof ProtectSanitizer }
2037
}
2138

2239
from
23-
BufferWrite w, Expr taintedArg, Expr taintSource, PathNode sourceNode, PathNode sinkNode,
24-
string taintCause, PrivateDataExpr dest
40+
BufferWrite w, BufferConfig b, Expr taintedArg, DataFlow::Node source, DataFlow::Node sink
2541
where
26-
taintedWithPath(taintSource, taintedArg, sourceNode, sinkNode) and
27-
isUserInput(taintSource, taintCause) and
28-
w.getASource() = taintedArg and
29-
dest = w.getDest()
30-
select w, sourceNode, sinkNode,
31-
"This write into buffer '" + dest.toString() + "' may contain unencrypted data from $@",
32-
taintSource, "user input (" + taintCause + ")"
42+
b.hasFlow(source, sink) and
43+
w.getASource() = taintedArg
44+
select w, source, sink, "This write into this buffer may contain unencrypted data"
45+
46+
47+
48+
49+
// import cpp
50+
// import semmle.code.cpp.security.BufferWrite
51+
// import semmle.code.cpp.security.TaintTracking
52+
// import experimental.semmle.code.cpp.security.PrivateData
53+
// import TaintedWithPath
54+
55+
// class Configuration extends TaintTrackingConfiguration {
56+
// override predicate isSink(Element tainted) { exists(BufferWrite w | w.getASource() = tainted) }
57+
// }
58+
59+
// from
60+
// BufferWrite w, Expr taintedArg, Expr taintSource, PathNode sourceNode, PathNode sinkNode,
61+
// string taintCause, PrivateDataExpr dest
62+
// where
63+
// taintedWithPath(taintSource, taintedArg, sourceNode, sinkNode) and
64+
// isUserInput(taintSource, taintCause) and
65+
// w.getASource() = taintedArg and
66+
// dest = w.getDest()
67+
// select w, sourceNode, sinkNode,
68+
// "This write into buffer '" + dest.toString() + "' may contain unencrypted data from $@",
69+
// taintSource, "user input (" + taintCause + ")"
Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,54 @@
11
/**
22
* @name Cleartext storage of sensitive information in file
3-
* @description Storing sensitive information in cleartext can expose it
3+
* @description Storing private data in cleartext can expose it
44
* to an attacker.
5-
* @kind problem
5+
* @kind path-problem
66
* @problem.severity warning
77
* @id cpp/private-cleartext-storage-file
88
* @tags security
99
* external/cwe/cwe-313
1010
*/
1111

12+
1213
import cpp
1314
import experimental.semmle.code.cpp.security.PrivateData
1415
import semmle.code.cpp.security.FileWrite
16+
import semmle.code.cpp.dataflow.TaintTracking
17+
18+
/** A call to any method whose name suggests that it encodes or encrypts the parameter. */
19+
class ProtectSanitizer extends DataFlow::ExprNode {
20+
ProtectSanitizer() {
21+
exists(Function m, string s |
22+
this.getExpr().(FunctionCall).getTarget() = m and
23+
m.getName().regexpMatch("(?i).*" + s + ".*")
24+
|
25+
s = "protect" or s = "encode" or s = "encrypt"
26+
)
27+
}
28+
}
29+
30+
class FileConfig extends TaintTracking::Configuration {
31+
FileConfig() {
32+
this = "File write configuration"
33+
}
34+
override predicate isSource(DataFlow::Node source) { source.asExpr() instanceof PrivateDataExpr }
35+
override predicate isSink(DataFlow::Node sink) { exists(FileWrite w | sink.asExpr() = w.getASource()) }
36+
override predicate isSanitizer(DataFlow::Node node) { node instanceof ProtectSanitizer }
37+
}
38+
39+
from FileConfig b, DataFlow::Node source, DataFlow::Node sink
40+
where b.hasFlow(source, sink)
41+
select sink, "This file write may contain unencrypted data"
42+
43+
44+
45+
// import cpp
46+
// import experimental.semmle.code.cpp.security.PrivateData
47+
// import semmle.code.cpp.security.FileWrite
1548

16-
from FileWrite w, PrivateDataExpr source, Expr dest
17-
where
18-
source = w.getASource() and
19-
dest = w.getDest()
20-
select w, "This write into file '" + dest.toString() + "' may contain unencrypted data from $@",
21-
source, "this source."
49+
// from FileWrite w, PrivateDataExpr source, Expr dest
50+
// where
51+
// source = w.getASource() and
52+
// dest = w.getDest()
53+
// select w, "This write into file '" + dest.toString() + "' may contain unencrypted data from $@",
54+
// source, "this source."

0 commit comments

Comments
 (0)