Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1a044a0

Browse files
committed
CPP: Add 'fread' to BufferAccess.qll.
1 parent fd63658 commit 1a044a0

3 files changed

Lines changed: 30 additions & 2 deletions

File tree

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,32 @@ class MemchrBA extends BufferAccess {
292292
}
293293
}
294294

295+
/**
296+
* Calls to fread.
297+
* fread(buffer, size, number, file)
298+
*/
299+
class FreadBA extends BufferAccess {
300+
FreadBA() {
301+
this.(FunctionCall).getTarget().getName() = "fread"
302+
}
303+
304+
override string getName() {
305+
result = this.(FunctionCall).getTarget().getName()
306+
}
307+
308+
override Expr getBuffer(string bufferDesc, int accessType) {
309+
result = this.(FunctionCall).getArgument(0) and
310+
bufferDesc = "destination buffer" and
311+
accessType = 2
312+
}
313+
314+
override int getSize() {
315+
result =
316+
this.(FunctionCall).getArgument(1).getValue().toInt() *
317+
this.(FunctionCall).getArgument(2).getValue().toInt()
318+
}
319+
}
320+
295321
/**
296322
* A array access on a buffer:
297323
* buffer[ix]

cpp/ql/test/query-tests/Security/CWE/CWE-119/semmle/tests/OverflowBuffer.expected

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@
5454
| tests.cpp:491:2:491:7 | access to array | This array indexing operation accesses a negative index -1 on the $@. | tests.cpp:474:21:474:26 | call to malloc | array |
5555
| tests.cpp:519:3:519:8 | call to memset | This 'memset' operation accesses 20 bytes but the $@ is only 10 bytes. | tests.cpp:502:15:502:20 | call to malloc | destination buffer |
5656
| tests.cpp:519:3:519:8 | call to memset | This 'memset' operation accesses 20 bytes but the $@ is only 10 bytes. | tests.cpp:510:16:510:21 | call to malloc | destination buffer |
57+
| tests.cpp:541:6:541:10 | call to fread | This 'fread' operation may access 101 bytes but the $@ is only 100 bytes. | tests.cpp:532:7:532:16 | charBuffer | destination buffer |
58+
| tests.cpp:546:6:546:10 | call to fread | This 'fread' operation may access 400 bytes but the $@ is only 100 bytes. | tests.cpp:532:7:532:16 | charBuffer | destination buffer |
5759
| tests_restrict.c:12:2:12:7 | call to memcpy | This 'memcpy' operation accesses 2 bytes but the $@ is only 1 byte. | tests_restrict.c:7:6:7:13 | smallbuf | source buffer |
5860
| unions.cpp:26:2:26:7 | call to memset | This 'memset' operation accesses 200 bytes but the $@ is only 100 bytes. | unions.cpp:21:10:21:11 | mu | destination buffer |
5961
| unions.cpp:30:2:30:7 | call to memset | This 'memset' operation accesses 200 bytes but the $@ is only 100 bytes. | unions.cpp:15:7:15:11 | small | destination buffer |

cpp/ql/test/query-tests/Security/CWE/CWE-119/semmle/tests/tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -538,12 +538,12 @@ void test20()
538538
// ...
539539
}
540540

541-
if (fread(charBuffer, sizeof(char), 101, fileSource) > 0) // BAD [NOT DETECTED]
541+
if (fread(charBuffer, sizeof(char), 101, fileSource) > 0) // BAD
542542
{
543543
// ...
544544
}
545545

546-
if (fread(charBuffer, sizeof(int), 100, fileSource) > 0) // BAD [NOT DETECTED]
546+
if (fread(charBuffer, sizeof(int), 100, fileSource) > 0) // BAD
547547
{
548548
// ...
549549
}

0 commit comments

Comments
 (0)