libzmq CVE semgrep_id:flawfinder.strlen-1.wcslen-1._tcslen-1._mbslen-1:769:769
Function does not handle null terminated strings properly
Ссылка на строку:
https://github.com/zeromq/libzmq/-/blob/master/tests/test_socks.cpp#L769
Уязвимость
Название: функция не обрабатывает строки, не завершённые нулём.
Описание: функция strlen
и её аналоги не работают со строками, которые не завершаются нулём. Это может привести к чтению за пределами буфера и аварийному завершению работы приложения из-за доступа к непредназначенным областям памяти.
Варианты исправления
-
Использование
strnlen
вместоstrlen
: функцияstrnlen
позволяет указать максимальную длину строки, что предотвращает чтение за пределы буфера.Пример кода:
TEST_ASSERT_SUCCESS_ERRNO(res); TEST_ASSERT_EQUAL(strnlen(value, MAX_LEN) + 1, res_len); TEST_ASSERT(strcmp(buffer, value) == 0);
где
MAX_LEN
— это максимально допустимая длина строки. -
Использование более безопасных версий функций в CRT (C Runtime Library): если приложение разрабатывается с использованием CRT, можно воспользоваться более безопасными версиями функций.
Пример:
TEST_ASSERT_SUCCESS_ERRNO(res); TEST_ASSERT_EQUAL(strnlen_s(value, MAX_LEN) + 1, res_len); TEST_ASSERT(strcmp(buffer, value) == 0);
Ссылки на статьи
Описание:
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": "6d7162d8578b2600c484f0b9c002398008c2ff22939f7bf20967cccfc2cb1067",
"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:769:769",
"severity": "High",
"scanner": {
"id": "semgrep",
"name": "Semgrep"
},
"location": {
"file": "repo/tests/test_socks.cpp",
"start_line": 769
},
"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"
}
]
}
```