傳遞給 realpath() 的緩沖區應該至少為 PATH_MAX 字節。
C 標準庫函數 realpath() 有兩個參數。第一個參數指明了需要轉換成標準格式的文件名。第二個參數指明了輸出緩沖區。不論標準化的文件名長度如何,realpath() 都不會將多于 PATH_MAX 字節的數據寫入到輸出緩沖區。
一些程序員錯誤地認為:通過分配 PATH_MAX 長度的緩沖區,肯定會有足夠的空間來存放所有可能在系統中找到的文件名。但是,PATH_MAX 只是界定了一次調用可以傳遞給內核的最長的相對路徑。在大多數 Unix 和 Linux 系統上,想要確定路徑的最大長度并不容易。
[1] Standards Mapping - OWASP Top 10 2004 - (OWASP 2004) A5 Buffer Overflow
[2] Standards Mapping - Security Technical Implementation Guide Version 3 - (STIG 3) APP3590.1 CAT I
[3] Standards Mapping - Security Technical Implementation Guide Version 3.4 - (STIG 3.4) APP3590.1 CAT I
[4] Standards Mapping - Common Weakness Enumeration - (CWE) CWE ID 249, CWE ID 560
[5] Standards Mapping - Payment Card Industry Data Security Standard Version 1.2 - (PCI 1.2) Requirement 6.3.1.1
[6] Standards Mapping - Payment Card Industry Data Security Standard Version 2.0 - (PCI 2.0) Requirement 6.5.2
[7] Standards Mapping - Payment Card Industry Data Security Standard Version 1.1 - (PCI 1.1) Requirement 6.5.5
[8] The GNU C Library Reference Manual The GNU Software Foundation