Fix undefined behavior in dsortf comparison function#500
Conversation
In the dsortf function, when both (*e1)->name[0] == '/' and (*e2)->name[0] == '/', a situation occurs where a < b but b < a, violating the transitivity requirement for comparison functions used by qsort as specified by the C standard. This leads to undefined behavior, which can cause incorrect sorting and memory corruption in glibc's qsort implementation, creating a potential security issue [1]. Fix the issue by returning 0 when both entries begin with '/', preventing the undefined behavior. [1] https://www.qualys.com/2024/01/30/qsort.txt
|
Thanks a lot for the PR! |
|
I think this can never happen in practice for |
|
Review from @rpluem on dev@ here - I clearly did not think about this as long as he did 😃 https://lists.apache.org/thread/135z7yn9hjk7rly4y1r1so5rqjymgfx8 |
|
Thank you for letting me know about this email! It seems I can't reply directly to this email because I don't have an @apache.org email, and the mailing list server blocks it. Hi Ruediger, On Thu, Jan 09, 2025 at 08:54:13AM +0100, Ruediger Pluem wrote:
Thanks for your review! I initially didn't think this through carefully. However, I'll include x = '/', y = '/', z = '/' dsortf(x, y) == -1 This leads to the conclusion x < y && y < z && z < x, which might also Regards, |
is transitive to avoid undefined behaviour, per: https://www.qualys.com/2024/01/30/qsort.txt Submitted by: Kuan-Wei Chiu <visitorckw gmail.com> Github: closes apache#500 git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@1922994 13f79535-47bb-0310-9956-ffa450edef68 (cherry picked from commit b5d2f5e)
In the dsortf function, when both (*e1)->name[0] == '/' and (*e2)->name[0] == '/', a situation occurs where a < b but b < a, violating the transitivity requirement for comparison functions used by qsort as specified by the C standard. This leads to undefined behavior, which can cause incorrect sorting and memory corruption in glibc's qsort implementation, creating a potential security issue [1].
Fix the issue by returning 0 when both entries begin with '/', preventing the undefined behavior.
[1] https://www.qualys.com/2024/01/30/qsort.txt