detect/cip: add check for sscanf return-value - v5#10433
detect/cip: add check for sscanf return-value - v5#104330xEniola wants to merge 1 commit intoOISF:masterfrom
Conversation
Found by CodeQL. Cf https://codeql.github.com/codeql-query-help/cpp/cpp-missing-check-scanf/ The sscanf function returns the number of input items successfully matched and assigned. We have to ensure that all subsequent uses of scanf output arguments occur in a branch of an if statement (or similar), in which it is known that the corresponding scanf call has in fact read all possible items from its input. Hence, we always have to check if sscanf runs successfully by comparing the return value to a numerical constant. Ticket: 6753
|
Can you show some examples of Suricata's output with malformed rules that display this error? |
When I tested using unit tests, I found out with all the inputs I tried, the checks in the |
Had to remove the other |
|
I feel I also need to change the error message to I'm also wondering why |
Minor suggestion: could instead of can not
I'm guessing that this might be to ensure the input is a list of integers comma-separated. But not sure. |
Yes, could will be better. I tried comma seperated inputs, and it did not work though. |
Can you elaborate on how did you try that? |
I did it via the unittests, but I guess I did it wrong before; I tried it again now, and it works. |
| } | ||
|
|
||
| sscanf(token, "%2" SCNu8, &var); | ||
| if (sscanf(token, "%2" SCNu8, &var) != 1) { |
There was a problem hiding this comment.
it looks like the original code has some serious issues, c5cf296 broke things by turning the input array into a uint8_t array, even though for class and attribute we're supposed to have a uint16_t values. So that would need fixing.
In general, on further inspection, it's unclear why we have the sscanf at all. We already have num, and it's value is validated to be in range, so we can safely cast it to uint8_t for service and uint16_t for the other two.
cc @catenacyber
There was a problem hiding this comment.
Complementing this: a good thing to have to go along the next PR for this would be a Suricata-verify PR that used the cip.class and cip.attribute keywords with values that would not fit a u8 so we check that this is working as expected.
There was a problem hiding this comment.
Indeed thanks for the analysis Victor.
We can remove the call to sscanf and use num instead of var
There was a problem hiding this comment.
So to remove sscanf and do input[i++] = num; then.
And do an SV test.
There was a problem hiding this comment.
Yes, and pay attention to use the right integer types uint16 and uint8 ;-)
There was a problem hiding this comment.
I meant on the Suricata patch
There was a problem hiding this comment.
Oh! I thought I'm only to modify the sscanf part and the input[i++] = var part.
There was a problem hiding this comment.
I think some compilers will warn on input[i++] = num;
There was a problem hiding this comment.
So to change the definition if the input array to uint16_t then.
|
Stale PR |
Link to redmine ticket: https://redmine.openinfosecfoundation.org/issues/6753
Previous PR: #10418
Describe changes: