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 1680963

Summary: nss/p11-kit: SIGSEGV when an executable use nss and it is linked to the p11-kit library
Product: Red Hat Enterprise Linux 8 Reporter: Stefano Garzarella <sgarzare>
Component: p11-kitAssignee: Daiki Ueno <dueno>
Status: CLOSED CURRENTRELEASE QA Contact: BaseOS QE Security Team <qe-baseos-security>
Severity: high Docs Contact:
Priority: urgent    
Version: 8.1CC: coli, dueno, hhan, hkario, mmcgrath, nmavrogi, ssorce, szidek, timao, toneata, wchadwic, yafu
Target Milestone: rcKeywords: Triaged, ZStream
Target Release: 8.0Flags: ssorce: mirror+
Hardware: Unspecified   
OS: Unspecified   
Whiteboard:
Fixed In Version: Doc Type: If docs needed, set a value
Doc Text:
Story Points: ---
Clone Of:
: 1690500 (view as bug list) Environment:
0day
Last Closed: 2019-08-05 08:22:19 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: 1682503    
Bug Blocks: 1654696, 1690500    

Description Stefano Garzarella 2019-02-25 15:20:35 UTC
Description of problem:
Since NSS automatically load the p11-kit library, if an application is linked
to the p11-kit library (eg. using gnutls pkg-config like QEMU) and it uses NSS,
there is a strange behaviour when it calls two times or more
NSS_Init()/NSS_Shutdown().

NSS_Init() loads the p11-kit module and than calls C_GetFunctionList().
This function, among other things, calls init_globals_unlocked() that initializes
some global variables that are freed by free_modules_when_no_refs_unlocked()
usually called when the module is unloaded.

If p11-kit is not linked, the p11_proxy_module_cleanup() is called at every
NSS_Shutdown() (during the dlclose()), but if p11-kit is linked, it is called
only when the application exit and the free_modules_when_no_refs_unlocked()
is called multiple times at the end of the application, because in the
C_GetFunctionList() is created a list of State, causing the segfault.

I discovered this behaviour while debugging a QEMU BZ (https://bugzilla.redhat.com/show_bug.cgi?id=1654696.)
and I wrote a simple application to replicate the issue.

Version-Release number of selected component (if applicable):
nss (3.41.0-5.el8)
p11-kit (0.23.14-4.el8)

How reproducible:
100%

Steps to Reproduce:
1. vim nss_test.c

#include<stdlib.h>
#include<stdio.h>
#include<seccomon.h>
#include<nss.h>

int main() {
    SECStatus rv;

    for (int i = 0; i < 2; i++) {
        rv = NSS_NoDB_Init("/tmp");
        if (rv != SECSuccess) {
            fprintf(stderr, "NSS_Init failed");
            exit(-1);
        }

        rv = NSS_Shutdown();
        if (rv != SECSuccess) {
            fprintf(stderr, "NSS_Shutdown failed");
            exit(-1);
        }
    }

    return 0;
}

2. gcc -o nss_test nss_test.c `pkg-config --cflags nss` `pkg-config --libs nss` -lp11-kit

3. ./nss_test && echo "OK"

Actual results:
[1]    182629 segmentation fault (core dumped)  ./nss_test

Core was generated by `./nss_test'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x00007f65406b0fc3 in next_entry (iter=0x7fff6bf7dc20) at common/dict.c:70
(gdb) bt
#0  0x00007f65406b0fc3 in next_entry (iter=0x7fff6bf7dc20) at common/dict.c:70
#1  p11_dict_next (iter=0x7fff6bf7dc20, key=0x7fff6bf7dc18, value=0x0) at common/dict.c:84
#2  0x00007f6540632994 in free_modules_when_no_refs_unlocked () at p11-kit/modules.c:806
#3  0x00007f6540634989 in p11_modules_release_inlock_reentrant (
    modules=modules@entry=0x23b4080) at p11-kit/modules.c:1900
#4  0x00007f6540634f9d in p11_kit_modules_release (modules=0x23b4080)
    at p11-kit/modules.c:2295
#5  0x00007f654061dffb in p11_proxy_module_cleanup () at p11-kit/proxy.c:1733
#6  0x00007f654061b0ad in _p11_kit_fini () at ./common/init.h:61
#7  0x00007f65409bd1e6 in _dl_fini () at dl-fini.c:138
#8  0x00007f6540463ccc in __run_exit_handlers (status=0, listp=0x7f65405e9738 <__exit_funcs>,
    run_list_atexit=run_list_atexit@entry=true, run_dtors=run_dtors@entry=true) at exit.c:108
#9  0x00007f6540463e00 in __GI_exit (status=<optimized out>) at exit.c:139
#10 0x00007f654044d41a in __libc_start_main (main=0x401156 <main>, argc=1,
    argv=0x7fff6bf7df08, init=<optimized out>, fini=<optimized out>,
    rtld_fini=<optimized out>, stack_end=0x7fff6bf7def8) at ../csu/libc-start.c:342
#11 0x000000000040109e in _start ()

Expected results:
OK

Additional info:
The following patch fix the issue, but I'm not sure if it is a correct approach:

diff --git a/p11-kit/modules.c b/p11-kit/modules.c
index 0299eda..35e9137 100644
--- a/p11-kit/modules.c
+++ b/p11-kit/modules.c
@@ -800,11 +800,13 @@ free_modules_when_no_refs_unlocked (void)
        Module *mod;
        p11_dictiter iter;
 
-       /* Check if any modules have a ref count */
-       p11_dict_iterate (gl.modules, &iter);
-       while (p11_dict_next (&iter, (void **)&mod, NULL)) {
-               if (mod->ref_count)
-                       return;
+       if (gl.modules) {
+               /* Check if any modules have a ref count */
+               p11_dict_iterate (gl.modules, &iter);
+               while (p11_dict_next (&iter, (void **)&mod, NULL)) {
+                       if (mod->ref_count)
+                               return;
+               }
        }
 
        p11_dict_free (gl.unmanaged_by_funcs);

Comment 2 Simo Sorce 2019-02-25 17:14:34 UTC
Daiki,
this does look like a potential blocker, please investigate and escalate as needed.

Comment 5 Stefano Garzarella 2019-02-27 11:48:39 UTC
I sent a pull request upstream to fix this issue: https://github.com/p11-glue/p11-kit/pull/217