public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] Fix tcache count maximum
@ 2019-04-15 14:40 Wilco Dijkstra
  2019-04-29 21:19 ` Carlos O'Donell
  0 siblings, 1 reply; 19+ messages in thread
From: Wilco Dijkstra @ 2019-04-15 14:40 UTC (permalink / raw)
  To: libc-alpha; +Cc: nd, DJ Delorie

There are 2 issues with tcache count: the tcache counts[] array
is a char, which may be signed and has a very small range and thus
may overflow.  When setting it, there is no overflow check.

Eg. export GLIBC_TUNABLES=glibc.malloc.tcache_count=4096
leads to various crashes in benchtests:

Running /build/glibc/benchtests/bench-strcoll
bench-strcoll: malloc.c:2949: tcache_get: Assertion `tcache->counts[tc_idx] > 0' failed.
Aborted

Btw is this kind of crash regarded as a security risk? It's easy to do a
denial of service this way if you're able to set an environment variable.

ChangeLog:
2019-04-15  Wilco Dijkstra  <wdijkstr@arm.com>

        * malloc/malloc.c (tcache_perthread_struct): Use an
        unsigned short for counts array.
        (MAX_TCACHE_COUNT): New define.
        (do_set_tcache_count): Only update if count is small enough.

--

diff --git a/malloc/malloc.c b/malloc/malloc.c
index 801ba1f499b566e677b763fc84f8ba86f4f7ccd0..4db7283cc27118cd7d39410febf7be8f7633780a 100644
--- a/malloc/malloc.c
+++ b/malloc/malloc.c
@@ -2915,10 +2915,12 @@ typedef struct tcache_entry
    time), this is for performance reasons.  */
 typedef struct tcache_perthread_struct
 {
-  char counts[TCACHE_MAX_BINS];
+  unsigned short counts[TCACHE_MAX_BINS];
   tcache_entry *entries[TCACHE_MAX_BINS];
 } tcache_perthread_struct;
 
+#define MAX_TCACHE_COUNT 65535	/* Maximum value of counts[] entries.  */
+
 static __thread bool tcache_shutting_down = false;
 static __thread tcache_perthread_struct *tcache = NULL;
 
@@ -5114,8 +5116,11 @@ do_set_tcache_max (size_t value)
 static __always_inline int
 do_set_tcache_count (size_t value)
 {
-  LIBC_PROBE (memory_tunable_tcache_count, 2, value, mp_.tcache_count);
-  mp_.tcache_count = value;
+  if (value <= MAX_TCACHE_COUNT)
+    {
+      LIBC_PROBE (memory_tunable_tcache_count, 2, value, mp_.tcache_count);
+      mp_.tcache_count = value;
+    }
   return 1;
 }
 

^ permalink raw reply	[flat|nested] 19+ messages in thread

end of thread, other threads:[~2019-05-10 16:30 UTC | newest]

Thread overview: 19+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-04-15 14:40 [PATCH] Fix tcache count maximum Wilco Dijkstra
2019-04-29 21:19 ` Carlos O'Donell
2019-05-07 14:30   ` Wilco Dijkstra
2019-05-07 18:26     ` DJ Delorie
2019-05-08 16:53     ` Carlos O'Donell
2019-05-08 16:56       ` DJ Delorie
2019-05-08 17:19         ` Wilco Dijkstra
2019-05-08 18:31           ` DJ Delorie
2019-05-10 12:22             ` Wilco Dijkstra
2019-05-08 19:25         ` Carlos O'Donell
2019-05-08 17:35       ` Wilco Dijkstra
2019-05-08 19:27         ` Carlos O'Donell
2019-05-08 20:33           ` Wilco Dijkstra
2019-05-08 20:54             ` Carlos O'Donell
2019-05-09 12:29               ` Wilco Dijkstra
2019-05-09 15:58                 ` DJ Delorie
2019-05-10 12:08                   ` Wilco Dijkstra
2019-05-10 16:30                     ` DJ Delorie
2019-05-08 21:00             ` Carlos O'Donell

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).