libzmq CVE semgrep_id:flawfinder.strncpy-1:106:106
Function does not handle null terminated strings or invalid pointers properly
Ссылка на строку:
https://github.com/zeromq/libzmq/-/blob/master/src/norm_engine.cpp#L106
Описание уязвимости
Уязвимость заключается в том, что функция strncpy
не обрабатывает должным образом строки, которые не завершаются нулём. Это может привести к переполнению буфера и другим проблемам с безопасностью.
Варианты исправления
-
Использование
snprintf
: эта функция обеспечивает более безопасную работу со строками, так как позволяет контролировать размер записываемых данных и гарантирует добавление нулевого символа в конец строки.Пример кода:
addrLen = 255; snprintf(addr, addrLen + 1, "%s", addrPtr);
-
Использование безопасных версий функций в C Runtime Library (CRT): если разработка ведётся для CRT, можно использовать более безопасные версии функций, например,
strncpy_s
.Пример кода:
addrLen = 255; strncpy_s(addr, addrLen + 1, addrPtr, addrLen);
Ссылки на статьи:
Описание:
The `strncpy` family of functions do not properly handle strings that are not null terminated. It is recommended to use more secure alternatives such as `snprintf`.For more information please see: https://linux.die.net/man/3/snprintf
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/strncpy-s-strncpy-s-l-wcsncpy-s-wcsncpy-s-l-mbsncpy-s-mbsncpy-s-l?view=msvc-170
Исходный JSON:
{
"id": "68f0ecb5d00bdc45030715e5a2f4359b5dee06b1ec0111799fc830abf0a9ac54",
"category": "sast",
"name": "Function does not handle null terminated strings or invalid pointers properly",
"description": "The `strncpy` family of functions do not properly handle strings that are not null terminated.\nIt is recommended to use more secure alternatives such as `snprintf`.\n\nFor more information please see: https://linux.die.net/man/3/snprintf\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/strncpy-s-strncpy-s-l-wcsncpy-s-wcsncpy-s-l-mbsncpy-s-mbsncpy-s-l?view=msvc-170\n",
"cve": "semgrep_id:flawfinder.strncpy-1:106:106",
"severity": "High",
"scanner": {
"id": "semgrep",
"name": "Semgrep"
},
"location": {
"file": "repo/src/norm_engine.cpp",
"start_line": 106
},
"identifiers": [
{
"type": "semgrep_id",
"name": "flawfinder.strncpy-1",
"value": "flawfinder.strncpy-1"
},
{
"type": "cwe",
"name": "CWE-120",
"value": "120",
"url": "https://cwe.mitre.org/data/definitions/120.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 - strncpy",
"value": "strncpy"
}
]
}
```