libzmq CVE semgrep_id:flawfinder.strlen-1.wcslen-1._tcslen-1._mbslen-1:91:91
Function does not handle null terminated strings properly
Ссылка на строку:
https://github.com/zeromq/libzmq/-/blob/master/tests/test_pubsub_topics_count.cpp#L91
Описание уязвимости
Уязвимость связана с неправильным обращением с null-терминированными строками. В коде используется функция strlen
, которая не проверяет, является ли строка null-терминированной. Это может привести к чтению за пределами буфера и аварийному завершению работы приложения из-за доступа к непредназначенным областям памяти.
Варианты исправления
Для устранения уязвимости рекомендуется использовать функцию strnlen
вместо strlen
. Функция strnlen
позволяет указать максимальное количество символов для проверки, что делает её более безопасной.
Пример исправления:
TEST_ASSERT_SUCCESS_ERRNO(
zmq_setsockopt(subscriber, ZMQ_SUBSCRIBE, "ab", strnlen("ab", 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.
- https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/strnlen-strnlen-s?view=msvc-170 — информация о более безопасных версиях функций для CRT.
Описание:
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": "ed9cde5b1f5c7d92345826f4bf082329679dd8daf3161fba5d7b2584542cad77",
"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:91:91",
"severity": "High",
"scanner": {
"id": "semgrep",
"name": "Semgrep"
},
"location": {
"file": "repo/tests/test_pubsub_topics_count.cpp",
"start_line": 91
},
"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"
}
]
}
```