libzmq CVE semgrep_id:flawfinder.strlen-1.wcslen-1._tcslen-1._mbslen-1:763:763
Function does not handle null terminated strings properly
Ссылка на строку:
https://github.com/zeromq/libzmq/-/blob/master/tests/test_socks.cpp#L763
Описание уязвимости
Уязвимость связана с неправильным обращением с null-терминированными строками. В коде используется функция strlen
, которая не проверяет, является ли строка null-терминированной. Это может привести к чтению за пределами буфера и аварийному завершению работы приложения из-за доступа к непредназначенным областям памяти.
Варианты исправления
Чтобы устранить уязвимость, рекомендуется использовать функцию strnlen
вместо strlen
. Функция strnlen
позволяет указать максимальный размер строки, что предотвращает чтение за её пределами.
Пример исправленного кода:
res = zmq_setsockopt(sub, opt, value, strnlen(value, MAX_LEN));
где MAX_LEN
— это максимально допустимый размер строки.
Если разработка ведётся для 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.
Исходный код с уязвимостью
fprintf (stderr, "test_string_opt_ok: testing %s\n", msg);
res = zmq_setsockopt (sub, opt, value, strlen (value));
TEST_ASSERT_SUCCESS_ERRNO (res);
Файл с уязвимостью: repo/tests/test_socks.cpp
, строка 763.
Описание:
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
Исходный JSON:
{
"id": "d6720356026c34b3eb30c13c15c9e55209df759011012a2e6e9ada9cdcd8f977",
"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:763:763",
"severity": "High",
"scanner": {
"id": "semgrep",
"name": "Semgrep"
},
"location": {
"file": "repo/tests/test_socks.cpp",
"start_line": 763
},
"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"
}
]
}
```