Skip to content

Commit dfda651

Browse files
author
Nikita Kraiouchkine
committed
ARR38-C: Update query
1 parent 4427b40 commit dfda651

File tree

2 files changed

+45
-45
lines changed

2 files changed

+45
-45
lines changed

Diff for: c/cert/src/rules/ARR38-C/LibraryFunctionArgumentOutOfBounds.ql

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @id c/cert/library-function-argument-out-of-bounds
33
* @name ARR38-C: Guarantee that library functions do not form invalid pointers
4-
* @description
4+
* @description
55
* @kind problem
66
* @precision high
77
* @problem.severity error
@@ -15,8 +15,9 @@ import cpp
1515
import codingstandards.c.cert
1616
import codingstandards.c.OutOfBounds
1717

18-
from OOB::BufferAccessLibraryFunctionCall fc,
19-
string message, Expr bufferArg, string bufferArgStr, Expr sizeOrOtherBufferArg, string otherStr
18+
from
19+
OOB::BufferAccessLibraryFunctionCall fc, string message, Expr bufferArg, string bufferArgStr,
20+
Expr sizeOrOtherBufferArg, string otherStr
2021
where
2122
not isExcluded(fc, OutOfBoundsPackage::libraryFunctionArgumentOutOfBoundsQuery()) and
2223
OOB::problems(fc, message, bufferArg, bufferArgStr, sizeOrOtherBufferArg, otherStr)

Diff for: c/common/src/codingstandards/c/OutOfBounds.qll

+41-42
Original file line numberDiff line numberDiff line change
@@ -176,16 +176,18 @@ module OOB {
176176
src_sz = 3 and
177177
dst_sz = -1
178178
or
179-
name =
180-
[
181-
"memcpy", "wmemcpy", "memmove", "wmemmove", "strncpy", "wcsncpy", "memcmp", "wmemcmp",
182-
"strncmp", "wcsncmp"
183-
] and
179+
name = ["memcpy", "wmemcpy", "memmove", "wmemmove", "memcmp", "wmemcmp", "strncmp", "wcsncmp"] and
184180
dst = 0 and
185181
src = 1 and
186182
src_sz = 2 and
187183
dst_sz = 2
188184
or
185+
name = ["strncpy", "wcsncpy"] and
186+
dst = 0 and
187+
src = 1 and
188+
src_sz = -1 and
189+
dst_sz = 2
190+
or
189191
name = ["bsearch", "qsort"] and
190192
dst = 1 and
191193
src = -1 and
@@ -359,7 +361,7 @@ module OOB {
359361
}
360362
}
361363

362-
/**
364+
/**
363365
* A `BufferAccessLibraryFunction` modelling `strncmp`
364366
*/
365367
class StrncmpLibraryFunction extends BufferAccessLibraryFunction {
@@ -662,14 +664,11 @@ module OOB {
662664

663665
override Expr getSizeExpr() { none() }
664666

665-
override int getFixedSize() {
666-
result = this.(StringLiteral).getOriginalLength()
667-
}
667+
override int getFixedSize() { result = this.(StringLiteral).getOriginalLength() }
668668

669669
override predicate isNotNullTerminated() { none() }
670670
}
671671

672-
673672
class PointerToObjectSourceOrSizeToBufferAccessFunctionConfig extends DataFlow::Configuration {
674673
PointerToObjectSourceOrSizeToBufferAccessFunctionConfig() {
675674
this = "PointerToObjectSourceOrSizeToBufferAccessFunctionConfig"
@@ -838,11 +837,11 @@ module OOB {
838837
* from a pattern used for calculating the size of the buffer being accessed.
839838
*/
840839
predicate isBufferSizeOffsetOfGVN(
841-
Expr bufferArg, Expr bufferSizeArg,
842-
int sourceSizeExprOffset, int sizeMult, int sizeArgOffset, int bufferArgOffset,
843-
BufferAccessLibraryFunctionCall fc) {
840+
Expr bufferArg, Expr bufferSizeArg, int sourceSizeExprOffset, int sizeMult, int sizeArgOffset,
841+
int bufferArgOffset, BufferAccessLibraryFunctionCall fc
842+
) {
844843
exists(DynamicAllocationSource source, Expr sourceSizeExpr, Expr sourceSizeExprBase |
845-
(
844+
(
846845
bufferArg = fc.getWriteArg() and
847846
bufferSizeArg = fc.getWriteSizeArg(sizeMult)
848847
or
@@ -917,34 +916,34 @@ module OOB {
917916
BufferAccessLibraryFunctionCall fc, string message, Expr bufferArg, string bufferArgStr,
918917
Expr sizeOrOtherBufferArg, string otherStr
919918
) {
920-
exists(int bufferArgSize, int sizeArgValue |
921-
isSizeArgGreaterThanBufferSize(bufferArg, sizeOrOtherBufferArg, bufferArgSize, sizeArgValue,
922-
fc) and
923-
bufferArgStr = bufferArgType(fc, bufferArg) and
924-
message =
925-
"The size of the $@ passed to " + fc.getTarget().getName() + " is " + bufferArgSize +
926-
" bytes, but the " + "$@ is " + sizeArgValue + " bytes." and
927-
otherStr = "size argument"
928-
)
929-
or
930-
isMandatoryBufferArgNull(bufferArg, fc) and
931-
message = "The $@ passed to " + fc.getTarget().getName() + " is null." and
919+
exists(int bufferArgSize, int sizeArgValue |
920+
isSizeArgGreaterThanBufferSize(bufferArg, sizeOrOtherBufferArg, bufferArgSize, sizeArgValue,
921+
fc) and
932922
bufferArgStr = bufferArgType(fc, bufferArg) and
933-
otherStr = "" and
934-
sizeOrOtherBufferArg = bufferArg
935-
or
936-
isNullTerminatorMissingFromBufferArg(bufferArg, _, fc) and
937-
message = "The $@ passed to " + fc.getTarget().getName() + " is not null terminated." and
938-
bufferArgStr = bufferArgType(fc, bufferArg) and
939-
otherStr = "" and
940-
sizeOrOtherBufferArg = bufferArg
941-
or
942-
isReadBufferSizeGreaterThanWriteBufferSize(bufferArg, sizeOrOtherBufferArg, fc) and
943923
message =
944-
"The size of the $@ passed to " + fc.getTarget().getName() + " is greater than the " +
945-
"size of the $@." and
946-
bufferArgStr = "read buffer" and
947-
otherStr = "write buffer"
948-
// ADD IN GVN
949-
}
924+
"The size of the $@ passed to " + fc.getTarget().getName() + " is " + bufferArgSize +
925+
" bytes, but the " + "$@ is " + sizeArgValue + " bytes." and
926+
otherStr = "size argument"
927+
)
928+
or
929+
isMandatoryBufferArgNull(bufferArg, fc) and
930+
message = "The $@ passed to " + fc.getTarget().getName() + " is null." and
931+
bufferArgStr = bufferArgType(fc, bufferArg) and
932+
otherStr = "" and
933+
sizeOrOtherBufferArg = bufferArg
934+
or
935+
isNullTerminatorMissingFromBufferArg(bufferArg, _, fc) and
936+
message = "The $@ passed to " + fc.getTarget().getName() + " is not null terminated." and
937+
bufferArgStr = bufferArgType(fc, bufferArg) and
938+
otherStr = "" and
939+
sizeOrOtherBufferArg = bufferArg
940+
or
941+
isReadBufferSizeGreaterThanWriteBufferSize(bufferArg, sizeOrOtherBufferArg, fc) and
942+
message =
943+
"The size of the $@ passed to " + fc.getTarget().getName() + " is greater than the " +
944+
"size of the $@." and
945+
bufferArgStr = "read buffer" and
946+
otherStr = "write buffer"
947+
// ADD IN GVN
948+
}
950949
}

0 commit comments

Comments
 (0)