Skip to content

Commit a9f8a53

Browse files
authored
Merge pull request #972 from geoffw0/rtl
CPP: Add support for the Rtl* functions in BufferAccess.ql
2 parents c31ccbc + 315133b commit a9f8a53

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

change-notes/1.20/analysis-cpp.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
| **Query** | **Expected impact** | **Change** |
1919
|----------------------------|------------------------|------------------------------------------------------------------|
2020
| Array argument size mismatch (`cpp/array-arg-size-mismatch`) | Fewer false positives | An exception has been added to this query for variable sized arrays. |
21+
| Call to memory access function may overflow buffer (`cpp/overflow-buffer`) | More correct results | This query now recognizes calls to `RtlCopyMemoryNonTemporal` and `RtlSecureZeroMemory`. |
2122
| Returning stack-allocated memory (`cpp/return-stack-allocated-memory`) | More correct results | Many more stack allocated expressions are now recognized. |
2223
| Suspicious add with sizeof (`cpp/suspicious-add-sizeof`) | Fewer false positives | Pointer arithmetic on `char * const` expressions (and other variations of `char *`) are now correctly excluded from the results. |
2324
| Suspicious pointer scaling (`cpp/suspicious-pointer-scaling`) | Fewer false positives | False positives involving types that are not uniquely named in the snapshot have been fixed. |

cpp/ql/src/semmle/code/cpp/security/BufferAccess.qll

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ abstract class BufferAccess extends Expr {
3232
* wmemcpy(dest, src, num)
3333
* memmove(dest, src, num)
3434
* wmemmove(dest, src, num)
35-
* mempcpy(dest, src, num);
36-
* wmempcpy(dest, src, num);
35+
* mempcpy(dest, src, num)
36+
* wmempcpy(dest, src, num)
37+
* RtlCopyMemoryNonTemporal(dest, src, num)
3738
*/
3839
class MemcpyBA extends BufferAccess {
3940
MemcpyBA() {
@@ -42,7 +43,8 @@ class MemcpyBA extends BufferAccess {
4243
this.(FunctionCall).getTarget().getName() = "memmove" or
4344
this.(FunctionCall).getTarget().getName() = "wmemmove" or
4445
this.(FunctionCall).getTarget().getName() = "mempcpy" or
45-
this.(FunctionCall).getTarget().getName() = "wmempcpy"
46+
this.(FunctionCall).getTarget().getName() = "wmempcpy" or
47+
this.(FunctionCall).getTarget().getName() = "RtlCopyMemoryNonTemporal"
4648
}
4749

4850
override string getName() {
@@ -264,6 +266,30 @@ class MemsetBA extends BufferAccess {
264266
}
265267
}
266268

269+
/**
270+
* Calls to `RtlSecureZeroMemory`.
271+
* RtlSecureZeroMemory(ptr, cnt)
272+
*/
273+
class ZeroMemoryBA extends BufferAccess {
274+
ZeroMemoryBA() {
275+
this.(FunctionCall).getTarget().getName() = "RtlSecureZeroMemory"
276+
}
277+
278+
override string getName() {
279+
result = this.(FunctionCall).getTarget().getName()
280+
}
281+
282+
override Expr getBuffer(string bufferDesc, int accessType) {
283+
result = this.(FunctionCall).getArgument(0) and
284+
bufferDesc = "destination buffer" and
285+
accessType = 1
286+
}
287+
288+
override int getSize() {
289+
result = this.(FunctionCall).getArgument(1).getValue().toInt()
290+
}
291+
}
292+
267293
/**
268294
* Calls to memchr and similar functions.
269295
* memchr(buffer, value, num)

0 commit comments

Comments
 (0)