Bug 11655 - qsort() not thread safe, results to division by zero
Summary: qsort() not thread safe, results to division by zero
Status: RESOLVED FIXED
Alias: None
Product: glibc
Classification: Unclassified
Component: libc (show other bugs)
Version: 2.12
: P2 normal
Target Milestone: ---
Assignee: Ulrich Drepper
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-06-02 08:49 UTC by Tero Mononen
Modified: 2014-06-30 17:53 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:
fweimer: security-


Attachments
qsort_r-for-thread-safety.patch (235 bytes, patch)
2010-12-09 05:31 UTC, Naohiro Ooiwa
Details | Diff
qsort.c (341 bytes, text/plain)
2010-12-10 12:51 UTC, Naohiro Ooiwa
Details
Corrected test case (original has several bugs; does not test what it alleges to test) (301 bytes, text/plain)
2011-04-06 21:45 UTC, Paul Pluzhnikov
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Tero Mononen 2010-06-02 08:49:15 UTC
Function qsort_r() is not thread-safe. This results to division by zero if two
threads call it at the same time, and both sort more than 1024 bytes of data. 

Function contains code fragment 
--
[A] if (phys_pages == 0) {
  phys_pages = sysconf()
  ...
  pagesize = sysconf()
}

[B] if (size / pagesize > phys_pages) 
--

The first thread detects phys_pages being zero on [A] and may not yet have
assigned pagesize, when the second thread enters the code, sees phys_pages being
nonzero at [A], and performs division by zero pagesize at [B].

BR
--
Tero Mononen <tmo@iki.fi>
Comment 1 Naohiro Ooiwa 2010-12-09 05:31:34 UTC
Created attachment 5152 [details]
qsort_r-for-thread-safety.patch

The same problem occurs in my environment too.

I tried to create a patch and 
I attached the same patch file as following.

Is my patch reasonable?
Could you please check the my patch ?

Thanks in advance,


diff --git a/stdlib/msort.c b/stdlib/msort.c
index 35cd4d0..2cc2abc 100644
--- a/stdlib/msort.c
+++ b/stdlib/msort.c
@@ -182,7 +182,7 @@ qsort_r (void *b, size_t n, size_t s, __compar_d_fn_t cmp, void *arg)
       static long int phys_pages;
       static int pagesize;
 
-      if (phys_pages == 0)
+      if (phys_pages == 0 || pagesize == 0)
        {
          phys_pages = __sysconf (_SC_PHYS_PAGES);
Comment 2 Ulrich Drepper 2010-12-09 17:13:50 UTC
I used a slightly cheaper patch.
Comment 3 Naohiro Ooiwa 2010-12-10 12:51:38 UTC
Created attachment 5154 [details]
qsort.c

The status is already RESOLVED, but just for information.
I created a test program to reproduce this bug.
You can reproduce by following step.

# gcc -pthread qsort.c -o qsort
# while [ 0 ] ; do ./qsort ; done
Floating point exception


It takes a few minutes to reproduce on my machine has 2 CPU and 2G memory.
Comment 4 Paul Pluzhnikov 2011-04-06 21:45:18 UTC
Created attachment 5648 [details]
Corrected test case (original has several bugs; does not test what it alleges to test)
Comment 5 Naohiro Ooiwa 2011-05-11 17:47:42 UTC
Thank you very much for correcting the test case.