Bug 1267903 - malloc() signal leak in lexsave()
malloc() signal leak in lexsave()
Status: CLOSED CURRENTRELEASE
Product: Red Hat Enterprise Linux 6
Classification: Red Hat
Component: zsh (Show other bugs)
6.7
Unspecified Unspecified
urgent Severity high
: rc
: ---
Assigned To: Kamil Dudka
Jan Kepler
: EasyFix, Patch, ZStream
Depends On: 1267912
Blocks: 1254457 1269889 1273805
  Show dependency treegraph
 
Reported: 2015-10-01 07:05 EDT by Filip Krska
Modified: 2016-09-14 09:13 EDT (History)
7 users (show)

See Also:
Fixed In Version: zsh-4.3.11-5.el6
Doc Type: Bug Fix
Doc Text:
Cause: Signal handlers were enabled during the execution of malloc() in the implementation of lexsave(). Consequence: If a signal was received during the execution of malloc() and its handler attempted to allocate or free memory, the zsh process ended up in a deadlock and became unresponsive. Fix: Signal handlers are no longer enabled during the execution of malloc(). Instead of that, signals are queued for processing upon the return from malloc(). Result: The deadlock no longer occurs.
Story Points: ---
Clone Of:
: 1267912 1273805 (view as bug list)
Environment:
Last Closed: 2016-09-14 09:13:05 EDT
Type: Bug
Regression: ---
Mount Type: ---
Documentation: ---
CRM:
Verified Versions:
Category: ---
oVirt Team: ---
RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: ---


Attachments (Terms of Use)
proposed fix (1.73 KB, patch)
2015-10-20 12:31 EDT, Kamil Dudka
fkrska: review+
Details | Diff

  None (edit)
Description Filip Krska 2015-10-01 07:05:49 EDT
Description of problem:

zsh deadlocks in futex-wait state when signal hit in malloc() called by lexsave():


#0  __lll_lock_wait_private () at ../nptl/sysdeps/unix/sysv/linux/x86_64/lowlevellock.S:97
97	2:	movl	%edx, %eax
(gdb) bt
#0  __lll_lock_wait_private () at ../nptl/sysdeps/unix/sysv/linux/x86_64/lowlevellock.S:97
#1  0x0000003eb427d0a0 in _L_lock_5189 () from /lib64/libc-2.12.so
#2  0x0000003eb42789fb in _int_free (av=0x3eb458fe80, p=0x2174940, have_lock=0) at malloc.c:4959
...
#7  0x0000000000474495 in zhandler (sig=17) at signals.c:584
#8  <signal handler called>
#9  _int_malloc (av=0x3eb458fe80, bytes=<value optimized out>) at malloc.c:4538
#10 0x0000003eb427a991 in __libc_malloc (bytes=264) at malloc.c:3664
#11 0x000000000044a96b in lexsave () at lex.c:252

Version-Release number of selected component (if applicable):

zsh-4.3.11-4.el6.x86_64

How reproducible:

happens in production environment, not reproduced in labs so far

Steps to Reproduce:
1.
2.
3.

Actual results:

zsh deadlocks in futex-wait state

Expected results:

no deadlocks

Additional info:

The bug appears not to affect upstream master, where the function

lexsave(void)
 ... 1x malloc, 1x zalloc

was rewritten to

lex_context_save(struct lex_stack *ls, int toplevel)
  ... no *alloc at all

Recheck of any possible malloc() signal leaks in current RHEL 6 zsh code needed.
Comment 1 Kamil Dudka 2015-10-01 07:30:30 EDT
(In reply to Filip Krska from comment #0)
> Description of problem:
> 
> zsh deadlocks in futex-wait state when signal hit in malloc() called by
> lexsave():
> 
> 
> #0  __lll_lock_wait_private () at
> ../nptl/sysdeps/unix/sysv/linux/x86_64/lowlevellock.S:97
> 97	2:	movl	%edx, %eax
> (gdb) bt
> #0  __lll_lock_wait_private () at
> ../nptl/sysdeps/unix/sysv/linux/x86_64/lowlevellock.S:97
> #1  0x0000003eb427d0a0 in _L_lock_5189 () from /lib64/libc-2.12.so
> #2  0x0000003eb42789fb in _int_free (av=0x3eb458fe80, p=0x2174940,
> have_lock=0) at malloc.c:4959
> ...
> #7  0x0000000000474495 in zhandler (sig=17) at signals.c:584
> #8  <signal handler called>
> #9  _int_malloc (av=0x3eb458fe80, bytes=<value optimized out>) at
> malloc.c:4538
> #10 0x0000003eb427a991 in __libc_malloc (bytes=264) at malloc.c:3664
> #11 0x000000000044a96b in lexsave () at lex.c:252

I agree that replacing malloc() by zalloc() will prevent the above backtrace.

> Recheck of any possible malloc() signal leaks in current RHEL 6 zsh code
> needed.

It is unfortunately not that easy.  Calling [z]free() can also cause such a deadlock.  Upstream discouraged from wrapping [z]free() by signal queuing globally because it could hide unprotected accesses to the global state and result in wrong behavior (which is actually worse than the deadlock).

The handling of signals is still not perfect even in the latest upstream version of zsh.  See bug #1198671 for the list of related upstream fixes.  Unfortunately, new bug reports are still coming on the upstream mailing list.
Comment 6 Kamil Dudka 2015-10-20 12:22:49 EDT
(In reply to Filip Krska from comment #0)
> Recheck of any possible malloc() signal leaks in current RHEL 6 zsh code
> needed.

I have found one more occurrence (still present in the latest upstream):

--- a/Src/mem.c
+++ b/Src/mem.c
@@ -1289,18 +1289,23 @@ zsfree(char *p)
 MALLOC_RET_T
 realloc(MALLOC_RET_T p, MALLOC_ARG_T size)
 {
     struct m_hdr *m = (struct m_hdr *)(((char *)p) - M_ISIZE), *mp, *mt;
     char *r;
     int i, l = 0;

     /* some system..., see above */
-    if (!p && size)
-       return (MALLOC_RET_T) malloc(size);
+    if (!p && size) {
+       queue_signals();
+       r = malloc(size);
+       unqueue_signals();
+       return (MALLOC_RET_T) r;
+    }
+
     /* and some systems even do this... */
     if (!p || !size)
        return (MALLOC_RET_T) p;

     queue_signals();  /* just queue signals caught rather than handling them */

     /* check if we are reallocating a small block, if we do, we have
        to compute the size of the block from the sort of block it is in */
Comment 7 Kamil Dudka 2015-10-20 12:31 EDT
Created attachment 1084827 [details]
proposed fix

Filip. could you please review the attached patch?
Comment 8 Filip Krska 2015-10-21 03:22:26 EDT
Comment on attachment 1084827 [details]
proposed fix

Good catch IMHO, I'd add that chunk as well. Thanks
Comment 10 Kamil Dudka 2015-10-21 05:13:22 EDT
(In reply to Kamil Dudka from comment #6)
> I have found one more occurrence (still present in the latest upstream):

Sent upstream:

http://www.zsh.org/mla/workers/2015/msg02822.html
Comment 14 Marcel Kolaja 2016-09-14 09:13:05 EDT
The fix for this bug has been delivered in RHEL 6.7.z and this component has not been updated in RHEL 6.8. RHEL 6.8 contains the fix from RHEL 6.7.z. Therefore, this bug has been closed as CURRENTRELEASE.

Note You need to log in before you can comment on or make changes to this bug.