cgroup: ensure stable pid sorting in cmppid()
PATCH 1/1 cgroup: ensure stable pid sorting in cmppid()
The subtraction-based comparator (a - b) in cmppid() can overflow for large pid_t differences, producing incorrect sign values. This breaks qsort() ordering guarantees and may cause unstable or wrong sort results in pidlist output.
Replace with a three-way comparison idiom:
(a > b) - (a < b)
This reliably returns -1, 0, or +1 without overflow, ensuring correct and stable qsort() behavior for all pid_t values.
--- kernel/cgroup/cgroup-v1.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
kernel/cgroup/cgroup-v1.c +4 -1
@@ -281,7 +281,10 @@ static int pidlist_uniq(pid_t *list, int length)*/ static int cmppid (const void *a,const void *b) { -return *(pid_t *)a - *(pid_t *)b; +pid_t pa = *(pid_t *)a; +pid_t pb = *(pid_t *)b; + +return (pa > pb) - (pa < pb); } static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
Signature · 1 line
2.34.1
Tejun Heo
maintainerHello,
Quoted 4 lines · Kaushlendra Kumar
The subtraction-based comparator (a - b) in
cmppid()can overflow for largepid_tdifferences, producing incorrect sign values. This breaksqsort()ordering guarantees and may cause unstable or wrong sort results in pidlist output.
Can you give examples of such an overflow? What values would cause that?
Thanks.
Signature · 1 line
tejun
Quoted 3 lines
Can you give examples of such an overflow? What values would cause that?
pid_t is a signed 32-bit integer. Consider:
a = 2147483647 (INT_MAX, 0x7FFFFFFF)
b = -1
a - b = 2147483647 - (-1) = 2147483648
This overflows signed int32, wrapping to a big negative value.
In practice, pid_t values in Linux are positive , so this overflow cannot happen with real PIDs today. However, the subtraction pattern is a known antipattern for comparison functions, and using the three-way idiom is the safer.(less, greater and equal)
If you prefer, I can adjust the commit message to note that this is a correctness hardening rather than a fix for a currently triggerable bug.
BR,
Kaushlendra
Tejun Heo
maintainerQuoted 5 lines · Kaushlendra
Quoted 2 lines
Can you give examples of such an overflow? What values would cause that?
pid_tis a signed 32-bit integer. Consider:
Quoted 8 lines
a = 2147483647 (
INT_MAX,0x7FFFFFFF)
b = -1a - b = 2147483647 - (-1) = 2147483648
This overflows signed int32, wrapping to a big negative value.
In practice,
pid_tvalues in Linux are positive
and limited to PID_MAX_LIMIT (4mil).
Quoted 4 lines
, so this overflow cannot happen with real PIDs today. However, the subtraction pattern is a known antipattern for comparison functions, and using the three-way idiom is the safer.(less, greater and equal)
It's a bigger anti pattern to complicate code for non-existent problems.
Thanks.
Signature · 1 line
tejun