Note: This bug is displayed in read-only format because the product is no longer active in Red Hat Bugzilla.
RHEL Engineering is moving the tracking of its product development work on RHEL 6 through RHEL 9 to Red Hat Jira (issues.redhat.com). If you're a Red Hat customer, please continue to file support cases via the Red Hat customer portal. If you're not, please head to the "RHEL project" in Red Hat Jira and file new tickets here. Individual Bugzilla bugs in the statuses "NEW", "ASSIGNED", and "POST" are being migrated throughout September 2023. Bugs of Red Hat partners with an assigned Engineering Partner Manager (EPM) are migrated in late September as per pre-agreed dates. Bugs against components "kernel", "kernel-rt", and "kpatch" are only migrated if still in "NEW" or "ASSIGNED". If you cannot log in to RH Jira, please consult article #7032570. That failing, please send an e-mail to the RH Jira admins at rh-issues@redhat.com to troubleshoot your issue as a user management inquiry. The email creates a ServiceNow ticket with Red Hat. Individual Bugzilla bugs that are migrated will be moved to status "CLOSED", resolution "MIGRATED", and set with "MigratedToJIRA" in "Keywords". The link to the successor Jira issue will be found under "Links", have a little "two-footprint" icon next to it, and direct you to the "RHEL project" in Red Hat Jira (issue links are of type "https://issues.redhat.com/browse/RHEL-XXXX", where "X" is a digit). This same link will be available in a blue banner at the top of the page informing you that that bug has been migrated.

Bug 1267903

Summary: malloc() signal leak in lexsave()
Product: Red Hat Enterprise Linux 6 Reporter: Filip Krska <fkrska>
Component: zshAssignee: Kamil Dudka <kdudka>
Status: CLOSED CURRENTRELEASE QA Contact: Jan Kepler <jkejda>
Severity: high Docs Contact:
Priority: urgent    
Version: 6.7CC: dkutalek, fkrska, jkejda, jkurik, kdudka, rwilliam, thozza
Target Milestone: rcKeywords: EasyFix, Patch, ZStream
Target Release: ---   
Hardware: Unspecified   
OS: Unspecified   
Whiteboard:
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 13:13:05 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:
Bug Depends On: 1267912    
Bug Blocks: 1254457, 1269889, 1273805    
Attachments:
Description Flags
proposed fix fkrska: review+

Description Filip Krska 2015-10-01 11:05:49 UTC
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 11:30:30 UTC
(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 16:22:49 UTC
(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 16:31:40 UTC
Created attachment 1084827 [details]
proposed fix

Filip. could you please review the attached patch?

Comment 8 Filip Krska 2015-10-21 07:22:26 UTC
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 09:13:22 UTC
(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 13:13:05 UTC
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.