libzmq CVE semgrep_id:flawfinder.strlen-1.wcslen-1._tcslen-1._mbslen-1:79:79
Function does not handle null terminated strings properly
Ссылка на строку:
https://github.com/zeromq/libzmq/-/blob/master/tests/test_tcp_accept_filter.cpp#L79
Описание уязвимости
Уязвимость связана с неправильным обращением с null-терминированными строками. В коде используется функция strlen
, которая не проверяет, является ли строка null-терминированной. Это может привести к чтению за пределами буфера и сбоям в работе приложения из-за доступа к непредназначенным областям памяти.
Варианты исправления
Для устранения уязвимости рекомендуется использовать функцию strnlen
вместо strlen
. Функция strnlen
позволяет указать максимальный размер строки, что предотвращает чтение за её пределами.
Пример исправленного кода:
TEST_ASSERT_SUCCESS_ERRNO(zmq_setsockopt(
socket, ZMQ_TCP_ACCEPT_FILTER, empty_filter, strnlen(empty_filter, MAX_FILTER_LENGTH)));
где MAX_FILTER_LENGTH
— это константа, определяющая максимально допустимую длину строки empty_filter
.
Если разработка ведётся для C Runtime Library (CRT), следует использовать более безопасные версии функций. Подробнее можно ознакомиться по ссылке: https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/strnlen-strnlen-s?view=msvc-170.
Ссылки на статьи
-
https://linux.die.net/man/3/strnlen — документация по функции
strnlen
. - https://cwe.mitre.org/data/definitions/126.html — описание CWE-126.
- https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/strnlen-strnlen-s?view=msvc-170 — информация о более безопасных версиях функций для CRT.
Issue в GitLab
## Уязвимость: неправильное обращение с null-терминированными строками
**Описание**
В файле `repo/tests/test_tcp_accept_filter.cpp` на строке 79 используется функция `strlen`, которая не проверяет, является ли строка null-терминированной. Это может привести к чтению за пределами буфера и сбоям в работе приложения.
**Уровень серьёзности**
Высокий
**Категория**
SAST
**Идентификатор CWE**
CWE-126
**Ссылки**
- https://cwe.mitre.org/data/definitions/126.html
- https://linux.die.net/man/3/strnlen
- https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/strnlen-strnlen-s?view=msvc-170
**Исправление**
Заменить `strlen` на `strnlen`, указав максимальный размер строки:
```cpp
TEST_ASSERT_SUCCESS_ERRNO(zmq_setsockopt(
socket, ZMQ_TCP_ACCEPT_FILTER, empty_filter, strnlen(empty_filter, MAX_FILTER_LENGTH)));
где MAX_FILTER_LENGTH
— константа, определяющая максимально допустимую длину строки empty_filter
.
<hr><details><summary>Описание: </summary>The `strlen` family of functions does not handle strings that are not null
terminated. This can lead to buffer over reads and cause the application to
crash by accessing unintended memory locations. It is recommended that `strnlen`
be used instead as a `maxlen` value can be provided.
For more information please see: https://linux.die.net/man/3/strnlen
If developing for C Runtime Library (CRT), more secure versions of these functions should be
used, see:
https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/strnlen-strnlen-s?view=msvc-170
</details><details><summary>Исходный JSON: </summary><pre><code lang='json'>{
"id": "48a871fe35a175385e0aeb1e6e26cfa3680ada9f6fdce96cdd3915e605d7f95f",
"category": "sast",
"name": "Function does not handle null terminated strings properly",
"description": "The `strlen` family of functions does not handle strings that are not null\nterminated. This can lead to buffer over reads and cause the application to\ncrash by accessing unintended memory locations. It is recommended that `strnlen`\nbe used instead as a `maxlen` value can be provided.\n\nFor more information please see: https://linux.die.net/man/3/strnlen\n\nIf developing for C Runtime Library (CRT), more secure versions of these functions should be\nused, see:\nhttps://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/strnlen-strnlen-s?view=msvc-170\n",
"cve": "semgrep_id:flawfinder.strlen-1.wcslen-1._tcslen-1._mbslen-1:79:79",
"severity": "High",
"scanner": {
"id": "semgrep",
"name": "Semgrep"
},
"location": {
"file": "repo/tests/test_tcp_accept_filter.cpp",
"start_line": 79
},
"identifiers": [
{
"type": "semgrep_id",
"name": "flawfinder.strlen-1.wcslen-1._tcslen-1._mbslen-1",
"value": "flawfinder.strlen-1.wcslen-1._tcslen-1._mbslen-1"
},
{
"type": "cwe",
"name": "CWE-126",
"value": "126",
"url": "https://cwe.mitre.org/data/definitions/126.html"
},
{
"type": "owasp",
"name": "A03:2021 - Injection",
"value": "A03:2021"
},
{
"type": "owasp",
"name": "A1:2017 - Injection",
"value": "A1:2017"
},
{
"type": "flawfinder_func_name",
"name": "Flawfinder - strlen",
"value": "strlen"
},
{
"type": "flawfinder_func_name",
"name": "Flawfinder - wcslen",
"value": "wcslen"
},
{
"type": "flawfinder_func_name",
"name": "Flawfinder - _tcslen",
"value": "_tcslen"
},
{
"type": "flawfinder_func_name",
"name": "Flawfinder - _mbslen",
"value": "_mbslen"
}
]
}
```</code></pre></details>
/severity high