ton CVE semgrep_id:flawfinder.strlen-1.wcslen-1._tcslen-1._mbslen-1:286:286
Function does not handle null terminated strings properly
Ссылка на строку:
https://dev.gitsec.ru/crypto/ton/-/blob/master/tdutils/td/utils/port/stacktrace.cpp#L286
Уязвимость
Функция strlen не обрабатывает строки, которые не завершаются нулём. Это может привести к чтению за пределами буфера и сбою приложения из-за доступа к непредназначенным областям памяти.
Варианты исправления
Вместо strlen рекомендуется использовать strnlen, так как можно указать максимальное количество символов для проверки. Пример исправленного кода:
strcpy(buf_ptr, ": [");
>>> buf_ptr += strnlen(buf_ptr, MAX_BUFFER_SIZE); // MAX_BUFFER_SIZE — максимально допустимый размер буфера
_ui64toa(td::uint64(symbol->Address), buf_ptr, 16);
Если разработка ведётся для 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.
Описание:
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": "3a4e2e64f472069351b9db6d8d9c36a73fcb04bc47188c2c03b5736f93490d59",
"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:286:286",
"severity": "High",
"scanner": {
"id": "semgrep",
"name": "Semgrep"
},
"location": {
"file": "tdutils/td/utils/port/stacktrace.cpp",
"start_line": 286
},
"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"
}
]
}
```