Skip to content

Commit f416ee5

Browse files
authored
Merge pull request #22563 from trail-of-forks/bde-bdlde-conversion-models
cpp: Model BDE Base64 and hex pointer-buffer conversions
2 parents 585ce95 + 93e8c44 commit f416ee5

8 files changed

Lines changed: 377 additions & 0 deletions

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* Added taint-flow models for direct pointer-buffer conversions using `BloombergLP::bdlde::Base64Encoder`, `Base64Decoder`, `HexEncoder`, and `HexDecoder`.

‎cpp/ql/lib/semmle/code/cpp/models/Models.qll‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
private import implementations.Allocation
2+
private import implementations.Bdlde
23
private import implementations.Deallocation
34
private import implementations.Fopen
45
private import implementations.Fread
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Models direct pointer-buffer conversions in the BDE bdlde library.
3+
* See https://github.com/bloomberg/bde/tree/ec310b87e008199ecbdbc00a0b0264a53d806a0a/groups/bdl/bdlde.
4+
*/
5+
6+
import semmle.code.cpp.models.interfaces.Taint
7+
8+
/** A conversion whose input and output iterators are pointers. */
9+
private class BdldePointerConversion extends TaintFunction {
10+
int beginIndex;
11+
12+
BdldePointerConversion() {
13+
this.hasName("convert") and
14+
this.getDeclaringType()
15+
.hasQualifiedName("BloombergLP::bdlde",
16+
["Base64Encoder", "Base64Decoder", "HexEncoder", "HexDecoder"]) and
17+
(
18+
this.getNumberOfParameters() = 3 and beginIndex = 1
19+
or
20+
this.getNumberOfParameters() = 6 and beginIndex = 3
21+
) and
22+
// Check instantiated types. A generic template signature would also match
23+
// iterator objects and replace their bodies with an inapplicable summary.
24+
this.getParameter(0).getUnspecifiedType() instanceof PointerType and
25+
this.getParameter(beginIndex).getUnspecifiedType() instanceof PointerType and
26+
this.getParameter(beginIndex + 1).getUnspecifiedType() instanceof PointerType
27+
}
28+
29+
override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) {
30+
input.isParameterDeref(beginIndex) and output.isParameterDeref(0)
31+
}
32+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Minimal public declarations, with implementation bodies omitted to test the models.
2+
// https://github.com/bloomberg/bde/tree/ec310b87e008199ecbdbc00a0b0264a53d806a0a/groups/bdl/bdlde
3+
namespace BloombergLP {
4+
namespace bdlde {
5+
class Base64Encoder {
6+
public:
7+
template <class OUTPUT_ITERATOR, class INPUT_ITERATOR>
8+
int convert(OUTPUT_ITERATOR out, INPUT_ITERATOR begin, INPUT_ITERATOR end);
9+
template <class OUTPUT_ITERATOR, class INPUT_ITERATOR>
10+
int convert(OUTPUT_ITERATOR out, int *numOut, int *numIn,
11+
INPUT_ITERATOR begin, INPUT_ITERATOR end, int maxNumOut = -1);
12+
};
13+
class Base64Decoder {
14+
public:
15+
template <class OUTPUT_ITERATOR, class INPUT_ITERATOR>
16+
int convert(OUTPUT_ITERATOR out, INPUT_ITERATOR begin, INPUT_ITERATOR end);
17+
template <class OUTPUT_ITERATOR, class INPUT_ITERATOR>
18+
int convert(OUTPUT_ITERATOR out, int *numOut, int *numIn,
19+
INPUT_ITERATOR begin, INPUT_ITERATOR end, int maxNumOut = -1);
20+
};
21+
class HexEncoder {
22+
public:
23+
template <class OUTPUT_ITERATOR, class INPUT_ITERATOR>
24+
int convert(OUTPUT_ITERATOR out, INPUT_ITERATOR begin, INPUT_ITERATOR end);
25+
template <class OUTPUT_ITERATOR, class INPUT_ITERATOR>
26+
int convert(OUTPUT_ITERATOR out, int *numOut, int *numIn,
27+
INPUT_ITERATOR begin, INPUT_ITERATOR end, int maxNumOut = -1);
28+
};
29+
class HexDecoder {
30+
public:
31+
template <class OUTPUT_ITERATOR, class INPUT_ITERATOR>
32+
int convert(OUTPUT_ITERATOR out, INPUT_ITERATOR begin, INPUT_ITERATOR end);
33+
template <class OUTPUT_ITERATOR, class INPUT_ITERATOR>
34+
int convert(OUTPUT_ITERATOR out, int *numOut, int *numIn,
35+
INPUT_ITERATOR begin, INPUT_ITERATOR end, int maxNumOut = -1);
36+
};
37+
}
38+
}

‎cpp/ql/test/library-tests/dataflow/bdlde/flow.expected‎

Whitespace-only changes.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import cpp
2+
import utils.test.dataflow.FlowTestCommon
3+
import semmle.code.cpp.ir.dataflow.TaintTracking
4+
5+
module Config implements DataFlow::ConfigSig {
6+
predicate isSource(DataFlow::Node node) {
7+
node.asExpr().(FunctionCall).getTarget().hasName("source")
8+
}
9+
10+
predicate isSink(DataFlow::Node node) {
11+
exists(FunctionCall call |
12+
call.getTarget().hasName("sink") and node.asExpr() = call.getArgument(0)
13+
)
14+
}
15+
}
16+
17+
module Flow = TaintTracking::Global<Config>;
18+
19+
import MakeTest<IRFlowTest<Flow>>
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
#include "bdlde.h"
2+
3+
char source();
4+
void sink(char);
5+
6+
// Reduced iterator access bodies: the object instantiations must remain
7+
// analyzable rather than being replaced by pointer-buffer summaries.
8+
struct InputIterator {
9+
char value;
10+
char operator*() const { return value; }
11+
};
12+
13+
struct OutputIterator {
14+
OutputIterator &operator*() { return *this; }
15+
void operator=(char value) { sink(value); } // $ ir
16+
};
17+
18+
namespace BloombergLP {
19+
namespace bdlde {
20+
template <class OUT, class IN>
21+
int Base64Encoder::convert(OUT out, IN begin, IN end) {
22+
*out = *begin;
23+
return 0;
24+
}
25+
26+
template <class OUT, class IN>
27+
int Base64Encoder::convert(OUT out, int *numOut, int *numIn, IN begin, IN end, int limit) {
28+
*out = *begin;
29+
return 0;
30+
}
31+
template <class OUT, class IN>
32+
int Base64Decoder::convert(OUT out, IN begin, IN end) {
33+
*out = *begin;
34+
return 0;
35+
}
36+
37+
template <class OUT, class IN>
38+
int Base64Decoder::convert(OUT out, int *numOut, int *numIn, IN begin, IN end, int limit) {
39+
*out = *begin;
40+
return 0;
41+
}
42+
template <class OUT, class IN>
43+
int HexEncoder::convert(OUT out, IN begin, IN end) {
44+
*out = *begin;
45+
return 0;
46+
}
47+
48+
template <class OUT, class IN>
49+
int HexEncoder::convert(OUT out, int *numOut, int *numIn, IN begin, IN end, int limit) {
50+
*out = *begin;
51+
return 0;
52+
}
53+
template <class OUT, class IN>
54+
int HexDecoder::convert(OUT out, IN begin, IN end) {
55+
*out = *begin;
56+
return 0;
57+
}
58+
59+
template <class OUT, class IN>
60+
int HexDecoder::convert(OUT out, int *numOut, int *numIn, IN begin, IN end, int limit) {
61+
*out = *begin;
62+
return 0;
63+
}
64+
}
65+
}
66+
67+
void iteratorBase64Encoder() {
68+
BloombergLP::bdlde::Base64Encoder converter;
69+
InputIterator begin = {source()}, end = {0};
70+
char shortOutput[4] = {};
71+
converter.convert(shortOutput, begin, end);
72+
sink(shortOutput[0]); // $ ir
73+
int numOut = 0, numIn = 0;
74+
char longOutput[4] = {};
75+
converter.convert(longOutput, &numOut, &numIn, begin, end);
76+
sink(longOutput[0]); // $ ir
77+
}
78+
79+
void iteratorBase64Decoder() {
80+
BloombergLP::bdlde::Base64Decoder converter;
81+
InputIterator begin = {source()}, end = {0};
82+
char shortOutput[4] = {};
83+
converter.convert(shortOutput, begin, end);
84+
sink(shortOutput[0]); // $ ir
85+
int numOut = 0, numIn = 0;
86+
char longOutput[4] = {};
87+
converter.convert(longOutput, &numOut, &numIn, begin, end);
88+
sink(longOutput[0]); // $ ir
89+
}
90+
91+
void iteratorHexEncoder() {
92+
BloombergLP::bdlde::HexEncoder converter;
93+
InputIterator begin = {source()}, end = {0};
94+
char shortOutput[4] = {};
95+
converter.convert(shortOutput, begin, end);
96+
sink(shortOutput[0]); // $ ir
97+
int numOut = 0, numIn = 0;
98+
char longOutput[4] = {};
99+
converter.convert(longOutput, &numOut, &numIn, begin, end);
100+
sink(longOutput[0]); // $ ir
101+
}
102+
103+
void iteratorHexDecoder() {
104+
BloombergLP::bdlde::HexDecoder converter;
105+
InputIterator begin = {source()}, end = {0};
106+
char shortOutput[4] = {};
107+
converter.convert(shortOutput, begin, end);
108+
sink(shortOutput[0]); // $ ir
109+
int numOut = 0, numIn = 0;
110+
char longOutput[4] = {};
111+
converter.convert(longOutput, &numOut, &numIn, begin, end);
112+
sink(longOutput[0]); // $ ir
113+
}
114+
115+
template <class CONVERTER>
116+
void outputIterator() {
117+
CONVERTER converter;
118+
char input[] = {source()};
119+
OutputIterator out;
120+
converter.convert(out, input, input + 1);
121+
int numOut = 0, numIn = 0;
122+
converter.convert(out, &numOut, &numIn, input, input + 1);
123+
}
124+
125+
void testOutputIterators() {
126+
outputIterator<BloombergLP::bdlde::Base64Encoder>();
127+
outputIterator<BloombergLP::bdlde::Base64Decoder>();
128+
outputIterator<BloombergLP::bdlde::HexEncoder>();
129+
outputIterator<BloombergLP::bdlde::HexDecoder>();
130+
}
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
#include "bdlde.h"
2+
3+
char source();
4+
void sink(char);
5+
6+
void testBase64Encoder() {
7+
BloombergLP::bdlde::Base64Encoder converter;
8+
char input[] = {source(), 'A', 'A', 'A'};
9+
const char *begin = input;
10+
char shortOutput[32] = {};
11+
converter.convert(shortOutput, input, input + 4);
12+
sink(shortOutput[0]); // $ ir
13+
14+
int numOut = 0, numIn = 0;
15+
char defaultLimitOutput[32] = {};
16+
converter.convert(defaultLimitOutput, &numOut, &numIn, begin, begin + 4);
17+
sink(defaultLimitOutput[0]); // $ ir
18+
19+
char explicitLimitOutput[32] = {};
20+
converter.convert(explicitLimitOutput, &numOut, &numIn, begin, begin + 4, 32);
21+
sink(explicitLimitOutput[0]); // $ ir
22+
}
23+
24+
void testBase64EncoderNonInputs() {
25+
BloombergLP::bdlde::Base64Encoder converter;
26+
const char input[] = "AAAA";
27+
int numOut = source(), numIn = source();
28+
char output[32] = {};
29+
converter.convert(output, &numOut, &numIn, input, input + 4);
30+
sink(output[0]); // No flow from the pre-call values of the output counters.
31+
32+
char limitedOutput[32] = {};
33+
converter.convert(limitedOutput, &numOut, &numIn, input, input + 4, source());
34+
sink(limitedOutput[0]); // The output limit does not supply output bytes.
35+
}
36+
37+
void testBase64Decoder() {
38+
BloombergLP::bdlde::Base64Decoder converter;
39+
char input[] = {source(), 'A', 'A', 'A'};
40+
const char *begin = input;
41+
char shortOutput[32] = {};
42+
converter.convert(shortOutput, input, input + 4);
43+
sink(shortOutput[0]); // $ ir
44+
45+
int numOut = 0, numIn = 0;
46+
char defaultLimitOutput[32] = {};
47+
converter.convert(defaultLimitOutput, &numOut, &numIn, begin, begin + 4);
48+
sink(defaultLimitOutput[0]); // $ ir
49+
50+
char explicitLimitOutput[32] = {};
51+
converter.convert(explicitLimitOutput, &numOut, &numIn, begin, begin + 4, 32);
52+
sink(explicitLimitOutput[0]); // $ ir
53+
}
54+
55+
void testBase64DecoderNonInputs() {
56+
BloombergLP::bdlde::Base64Decoder converter;
57+
const char input[] = "AAAA";
58+
int numOut = source(), numIn = source();
59+
char output[32] = {};
60+
converter.convert(output, &numOut, &numIn, input, input + 4);
61+
sink(output[0]); // No flow from the pre-call values of the output counters.
62+
63+
char limitedOutput[32] = {};
64+
converter.convert(limitedOutput, &numOut, &numIn, input, input + 4, source());
65+
sink(limitedOutput[0]); // The output limit does not supply output bytes.
66+
}
67+
68+
void testHexEncoder() {
69+
BloombergLP::bdlde::HexEncoder converter;
70+
char input[] = {source(), 'A', 'A', 'A'};
71+
const char *begin = input;
72+
char shortOutput[32] = {};
73+
converter.convert(shortOutput, input, input + 4);
74+
sink(shortOutput[0]); // $ ir
75+
76+
int numOut = 0, numIn = 0;
77+
char defaultLimitOutput[32] = {};
78+
converter.convert(defaultLimitOutput, &numOut, &numIn, begin, begin + 4);
79+
sink(defaultLimitOutput[0]); // $ ir
80+
81+
char explicitLimitOutput[32] = {};
82+
converter.convert(explicitLimitOutput, &numOut, &numIn, begin, begin + 4, 32);
83+
sink(explicitLimitOutput[0]); // $ ir
84+
}
85+
86+
void testHexEncoderNonInputs() {
87+
BloombergLP::bdlde::HexEncoder converter;
88+
const char input[] = "AAAA";
89+
int numOut = source(), numIn = source();
90+
char output[32] = {};
91+
converter.convert(output, &numOut, &numIn, input, input + 4);
92+
sink(output[0]); // No flow from the pre-call values of the output counters.
93+
94+
char limitedOutput[32] = {};
95+
converter.convert(limitedOutput, &numOut, &numIn, input, input + 4, source());
96+
sink(limitedOutput[0]); // The output limit does not supply output bytes.
97+
}
98+
99+
void testHexDecoder() {
100+
BloombergLP::bdlde::HexDecoder converter;
101+
char input[] = {source(), 'A', 'A', 'A'};
102+
const char *begin = input;
103+
char shortOutput[32] = {};
104+
converter.convert(shortOutput, input, input + 4);
105+
sink(shortOutput[0]); // $ ir
106+
107+
int numOut = 0, numIn = 0;
108+
char defaultLimitOutput[32] = {};
109+
converter.convert(defaultLimitOutput, &numOut, &numIn, begin, begin + 4);
110+
sink(defaultLimitOutput[0]); // $ ir
111+
112+
char explicitLimitOutput[32] = {};
113+
converter.convert(explicitLimitOutput, &numOut, &numIn, begin, begin + 4, 32);
114+
sink(explicitLimitOutput[0]); // $ ir
115+
}
116+
117+
void testHexDecoderNonInputs() {
118+
BloombergLP::bdlde::HexDecoder converter;
119+
const char input[] = "AAAA";
120+
int numOut = source(), numIn = source();
121+
char output[32] = {};
122+
converter.convert(output, &numOut, &numIn, input, input + 4);
123+
sink(output[0]); // No flow from the pre-call values of the output counters.
124+
125+
char limitedOutput[32] = {};
126+
converter.convert(limitedOutput, &numOut, &numIn, input, input + 4, source());
127+
sink(limitedOutput[0]); // The output limit does not supply output bytes.
128+
}
129+
130+
// Inherited calls still target the method declared in Base64Encoder.
131+
struct DerivedEncoder : BloombergLP::bdlde::Base64Encoder {};
132+
133+
void testInheritedConvert() {
134+
DerivedEncoder converter;
135+
char input[] = {source()};
136+
char output[32] = {};
137+
converter.convert(output, input, input + 1);
138+
sink(output[0]); // $ ir
139+
}
140+
141+
// A different method that hides convert must not inherit the summary.
142+
struct HidingEncoder : BloombergLP::bdlde::Base64Encoder {
143+
template <class OUTPUT_ITERATOR, class INPUT_ITERATOR>
144+
int convert(OUTPUT_ITERATOR out, INPUT_ITERATOR begin, INPUT_ITERATOR end);
145+
};
146+
147+
void testHiddenConvert() {
148+
HidingEncoder converter;
149+
char input[] = {source()};
150+
char output[32] = {};
151+
converter.convert(output, input, input + 1);
152+
sink(output[0]); // No modeled flow for the hiding method.
153+
}

0 commit comments

Comments
 (0)