libzmq CVE semgrep_id:flawfinder.strncpy-1:680:680
Function does not handle null terminated strings or invalid pointers properly
Ссылка на строку:
https://github.com/zeromq/libzmq/-/blob/master/src/msg.cpp#L680
Описание уязвимости
Уязвимость заключается в том, что функция strncpy
не обрабатывает должным образом строки, которые не завершаются нулём. Это может привести к ошибкам и потенциальным уязвимостям в безопасности.
Варианты исправления
-
Использовать
snprintf
вместоstrncpy
. Функцияsnprintf
обеспечивает более безопасную работу со строками.Пример кода:
} else { snprintf(_u.base.group.sgroup.group, sizeof(_u.base.group.sgroup.group), "%s", group_); }
-
Если разработка ведётся для C Runtime Library (CRT), использовать более безопасные версии функций, например,
strncpy_s
.Пример кода:
} else { strncpy_s(_u.base.group.sgroup.group, sizeof(_u.base.group.sgroup.group), group_, length_); }
Ссылки на статьи:
- О функции
snprintf
: https://linux.die.net/man/3/snprintf. - О более безопасных версиях функций для CRT: 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.
- Информация о CWE-120: https://cwe.mitre.org/data/definitions/120.html.
Описание:
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": "3a0ade82425701647bbf31377ce14c504c7be98fedf4e87b200319eaed15fe0d",
"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:680:680",
"severity": "High",
"scanner": {
"id": "semgrep",
"name": "Semgrep"
},
"location": {
"file": "repo/src/msg.cpp",
"start_line": 680
},
"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"
}
]
}
```