Bug 512019 - [PEM] Seg fault when using malformed PEM key
Summary: [PEM] Seg fault when using malformed PEM key
Keywords:
Status: CLOSED ERRATA
Alias: None
Product: Fedora
Classification: Fedora
Component: nss
Version: 11
Hardware: All
OS: Linux
medium
high
Target Milestone: ---
Assignee: Elio Maldonado Batiz
QA Contact: Fedora Extras Quality Assurance
URL:
Whiteboard:
Depends On:
Blocks: 501138 517000
TreeView+ depends on / blocked
 
Reported: 2009-07-15 23:09 UTC by Michael Cronenworth
Modified: 2016-01-21 11:25 UTC (History)
5 users (show)

Fixed In Version: 3.12.4-3.fc11
Doc Type: Bug Fix
Doc Text:
Clone Of:
: 555031 1300652 (view as bug list)
Environment:
Last Closed: 2009-09-24 05:16:20 UTC
Type: ---
Embargoed:


Attachments (Terms of Use)
test ssl certs (4.88 KB, application/x-gzip)
2009-07-15 23:09 UTC, Michael Cronenworth
no flags Details
proposed patch (526 bytes, patch)
2009-07-16 08:26 UTC, Kamil Dudka
no flags Details | Diff
regression test (1.70 KB, text/plain)
2009-07-16 09:11 UTC, Kamil Dudka
no flags Details
gdb backtrace (2.04 KB, text/plain)
2009-07-16 21:40 UTC, Michael Cronenworth
no flags Details
git patch (838 bytes, patch)
2009-08-21 09:12 UTC, Kamil Dudka
no flags Details | Diff

Description Michael Cronenworth 2009-07-15 23:09:14 UTC
Description of problem: I'm seeing a segfault when attempting to use a slightly malformed PEM key. The PEM key is legit except it does not contain any newline characters. The OpenSSL command line tool handles this appropriately by printing "unable to load Private Key" instead of seg faulting.


Version-Release number of selected component (if applicable): nss-3.12.3.99.3-2.11.3.fc11.i586


How reproducible: Always


Steps to Reproduce:
1. Remove the newlines in the attached key file.
2. Use key file.
3. Passphrase: test
  
Actual results: Segmentation fault


Expected results: "Invalid PEM key" or some other type of error message.

Comment 1 Michael Cronenworth 2009-07-15 23:09:53 UTC
Created attachment 353923 [details]
test ssl certs

...and I forgot the attachment...

Comment 2 Kamil Dudka 2009-07-16 07:59:09 UTC
Michael, thank you for the report! Could you please attach the malformed key file?

I haven't been able to reproduce the SEGFAULT yet, but valgrind reports "Conditional jump or move depends on uninitialised value(s)". So there is definitely something wrong happening.

Comment 3 Kamil Dudka 2009-07-16 08:18:57 UTC
Minimal example:

$ printf %s '-----BEGIN RSA PRIVATE KEY-----' > test-key.pem

Comment 4 Kamil Dudka 2009-07-16 08:26:49 UTC
Created attachment 353955 [details]
proposed patch

patch ready

Comment 5 Kamil Dudka 2009-07-16 09:11:23 UTC
Created attachment 353960 [details]
regression test

Comment 6 Michael Cronenworth 2009-07-16 16:31:26 UTC
Sorry, do you still need my malformed key? You can just take the "test.pem" from my first attachment and backspace through the newlines making one long line. Your minimal example should have the same effect though.

It looks like your patch should solve this. I'll test it when there's a build available.

Comment 7 Rob Crittenden 2009-07-16 16:46:38 UTC
+r

Comment 8 Elio Maldonado Batiz 2009-07-16 19:55:47 UTC
(In reply to comment #6) A scratch build is now available
http://koji.fedoraproject.org/koji/taskinfo?taskID=1480334

Comment 9 Michael Cronenworth 2009-07-16 21:40:39 UTC
Created attachment 354050 [details]
gdb backtrace

The new build seems to bypass the original key SEGFAULT, but now I'm getting a SEGFAULT in another area of nss.

Comment 10 Kamil Dudka 2009-07-16 22:06:56 UTC
(In reply to comment #9)
> The new build seems to bypass the original key SEGFAULT, but now I'm getting a
> SEGFAULT in another area of nss.  

Thanks for your feedback! Willing to open separate bug with some steps to reproduce?

Comment 11 Kamil Dudka 2009-07-16 22:14:46 UTC
Just for summary, the updated package should fail correctly with the malformed key, does it actually?

If yes, I am presuming you are not using the malformed key and then it seems to be not related to the patch and/or the original bug report.

Comment 12 Elio Maldonado Batiz 2009-07-16 22:29:59 UTC
For what's worth, here is scratch build with optimizations turned off
http://koji.fedoraproject.org/koji/taskinfo?taskID=1480590

Comment 13 Michael Cronenworth 2009-07-16 22:31:41 UTC
(In reply to comment #11)
> Just for summary, the updated package should fail correctly with the malformed
> key, does it actually?
> 

I did a little more testing. I can only reproduce this new SEGFAULT with my custom program. If I use my test-libcurl testcase from bug 453612 I cannot reproduce the new SEGFAULT. This bug is FIXED, but I do not have enough info for a new bug on the new SEGFAULT as of yet. I'll try out Elio's new build and open a new bug when I find the time. Thanks.

Comment 14 Kamil Dudka 2009-07-17 07:24:20 UTC
If you manage to narrow your custom program to a minimal example, it will definitely accelerate the bug fix. Looking at the backtrace I can see you are using curl. Then please report the version of curl, too.

Maybe you could try the latest rawhide curl which has slightly improved NSS-related code. I am also going to build new release of rawhide curl next week.

Comment 15 Kamil Dudka 2009-08-21 09:12:35 UTC
Created attachment 358208 [details]
git patch

Comment 16 Fedora Update System 2009-09-11 18:48:11 UTC
nss-3.12.4-1.fc11 has been submitted as an update for Fedora 11.
http://admin.fedoraproject.org/updates/nss-3.12.4-1.fc11

Comment 17 Fedora Update System 2009-09-15 07:47:12 UTC
nss-3.12.4-1.fc11 has been pushed to the Fedora 11 testing repository.  If problems still persist, please make note of it in this bug report.
 If you want to test the update, you can install it with 
 su -c 'yum --enablerepo=updates-testing update nss'.  You can provide feedback for this update here: http://admin.fedoraproject.org/updates/F11/FEDORA-2009-9593

Comment 18 Fedora Update System 2009-09-16 04:09:15 UTC
nss-3.12.4-2.fc11 has been submitted as an update for Fedora 11.
http://admin.fedoraproject.org/updates/nss-3.12.4-2.fc11

Comment 19 Fedora Update System 2009-09-16 20:35:59 UTC
nss-3.12.4-2.fc11 has been pushed to the Fedora 11 testing repository.  If problems still persist, please make note of it in this bug report.
 If you want to test the update, you can install it with 
 su -c 'yum --enablerepo=updates-testing update nss'.  You can provide feedback for this update here: http://admin.fedoraproject.org/updates/F11/FEDORA-2009-9687

Comment 20 Fedora Update System 2009-09-17 22:03:48 UTC
nss-3.12.4-3.fc11 has been submitted as an update for Fedora 11.
http://admin.fedoraproject.org/updates/nss-3.12.4-3.fc11?_csrf_token=d6eee54945d598c83d89d6481b2a20cf972bc76b

Comment 21 Fedora Update System 2009-09-17 23:28:52 UTC
nss-3.12.3.99.3-2.10.6.fc10 has been submitted as an update for Fedora 10.
http://admin.fedoraproject.org/updates/nss-3.12.3.99.3-2.10.6.fc10?_csrf_token=316f4db77f425c9db8051b00e270dfcd1200be13

Comment 22 Fedora Update System 2009-09-19 00:17:23 UTC
nss-3.12.3.99.3-2.10.6.fc10 has been pushed to the Fedora 10 testing repository.  If problems still persist, please make note of it in this bug report.
 If you want to test the update, you can install it with 
 su -c 'yum --enablerepo=updates-testing update nss'.  You can provide feedback for this update here: http://admin.fedoraproject.org/updates/F10/FEDORA-2009-9790

Comment 23 Fedora Update System 2009-09-19 00:18:30 UTC
nss-3.12.4-3.fc11 has been pushed to the Fedora 11 testing repository.  If problems still persist, please make note of it in this bug report.
 If you want to test the update, you can install it with 
 su -c 'yum --enablerepo=updates-testing update nss'.  You can provide feedback for this update here: http://admin.fedoraproject.org/updates/F11/FEDORA-2009-9794

Comment 24 Fedora Update System 2009-09-24 05:15:58 UTC
nss-3.12.3.99.3-2.10.6.fc10 has been pushed to the Fedora 10 stable repository.  If problems still persist, please make note of it in this bug report.

Comment 25 Fedora Update System 2009-09-24 05:16:45 UTC
nss-3.12.4-3.fc11 has been pushed to the Fedora 11 stable repository.  If problems still persist, please make note of it in this bug report.

Comment 26 tehmasp 2016-01-19 21:36:15 UTC
This is still an issue. Just had libnss3.so segfault on CentOS 7.1 system after slapd attempted to do a TLS cert exchange with a key file as follows:

  -----BEGIN PRIVATE KEY-----
  key
  -----END PRIVATE KEY-----


system/issue details:

CentOS Linux release 7.2.1511 (Core)

nss-3.19.1-19.el7_2.x86_64
openldap-2.4.40-8.el7.x86_64
openldap-clients-2.4.40-8.el7.x86_64
openldap-servers-2.4.40-8.el7.x86_64

kernel: slapd[2648]: segfault at 0 ip 00007f0af036c25c sp 00007f0ac0ea6610 error 4 in libnss3.so[7f0af0340000+11e000]

Comment 27 tehmasp 2016-01-19 21:38:25 UTC
CentOS 7.2 to be correct; typo on my first sentence.

Once I repaired the key file with a valid key - slapd TLS works like normal BTW. No more issues but definitely a reproducible segfault w/ details above.

Thanks.
Tehmasp Chaudhri
tehmasp

Comment 28 Kamil Dudka 2016-01-20 12:41:44 UTC
I am not able to repeat the crash.  Does curl crash if you pass such a key file to the --key option (together with --cert and an https:// URL)?

Comment 29 tehmasp 2016-01-20 15:55:44 UTC
This isn't reproducible via curl. I verified just now and curl does not crash. But specifically when slapd (openldap) is running on a server and using the bad key file then something in the code path causes a segfault in libnss3.so.

There's some edge case in libnss (in conjunction w/ how slapd w/ TLS) causes a segfault.

My setup is an openldap provider server (w/ bad slapd.key) and an openldap consumer server. When the consumer connects to the provider server which is using the key example above it crashes the openldap provider server.

Here are the journal provider slapd.service logs: 

Jan 19 21:01:20 gcestg-openldap-provider-use1c-01 systemd[1]: Started OpenLDAP Server Daemon.
Jan 19 21:01:27 gcestg-openldap-provider-use1c-01 slapd[867]: conn=1000 fd=28 ACCEPT from IP=10.240.0.19:43420 (IP=0.0.0.0:10389)
Jan 19 21:01:27 gcestg-openldap-provider-use1c-01 slapd[867]: conn=1000 op=0 EXT oid=1.3.6.1.4.1.1466.20037
Jan 19 21:01:27 gcestg-openldap-provider-use1c-01 slapd[867]: conn=1000 op=0 STARTTLS
Jan 19 21:01:27 gcestg-openldap-provider-use1c-01 slapd[867]: conn=1000 op=0 RESULT oid= err=0 text=
Jan 19 21:01:29 gcestg-openldap-provider-use1c-01 systemd[1]: slapd.service: main process exited, code=killed, status=11/SEGV
Jan 19 21:01:29 gcestg-openldap-provider-use1c-01 systemd[1]: Unit slapd.service entered failed state.
Jan 19 21:01:29 gcestg-openldap-provider-use1c-01 systemd[1]: slapd.service failed.


///

Thus, we've got 2 issues IMO (and this may not be the best place to capture all of this) but libnss3.so segfaults and an important piece of software in the RedHat ecosystem - ldap - segfaults badly when consumer servers connect to it. Would hate for someone else to hit up against this issue. Let me know how I can help more. If I somehow find the time I might be able to create a multi-VM vagrant env for repro purposes. Again, we're using the latest CentOS 7.2 w/ updates as of yesterday.

Cheers,
Tehmasp

Comment 30 Rob Crittenden 2016-01-20 16:27:34 UTC
So it fails the first time the key is used? It doesn't fail on server startup? (I'm not familiar with openssl cert/key handling).

A stacktrace would be incredibly helpful. I'm not sure how the value of "key" could get past the base64 decoding.

Comment 31 tehmasp 2016-01-20 18:03:04 UTC
Correct; it fails exactly after the ACCEPT from the consumer server. I tested via telnet connecting to the provider server on port 10389 (albeit w/o TLS) and that ACCEPT was all good. As soon as the consumer connects and after the ACCEPT line in the provider's journal slapd.service log the server crashes. Something w/ decoding the key like you say would be my guess as well.

Comment 32 Rob Crittenden 2016-01-20 18:20:37 UTC
Any chance on getting a stacktrace of the core?

Comment 33 tehmasp 2016-01-20 18:28:32 UTC
Let me work on this - I need to recreate the environment now as the previous servers are now in active use :)

Comment 34 tehmasp 2016-01-20 21:04:45 UTC
OK - had to learn how to enable coredumps w/ systemd :)

got a couple of coredumps with a bunch of gdb info - particularly:

(gdb) bt
(gdb) bt full
(gdb) info threads
(gdb) thread apply all bt
(gdb) thread apply all bt full


gdb trace w/o debuginfo installed: https://gist.github.com/tehmaspc/1b238c1766549fc5f72f

gdb trace w/ debuginfo installed: https://gist.github.com/tehmaspc/67e7c80efb63e1d6fada


Thanks,
Tehmasp Chaudhri

Comment 35 tehmasp 2016-01-20 21:06:33 UTC
BTW - this new recreated environment (provider/consumer server) was built w/ my same config management (Salt) tools; same everything to allay any concerns w/ these environments. Reproduced the segfault exactly the same way.

Comment 36 Rob Crittenden 2016-01-20 21:47:41 UTC
That seems to confirm my suspicions. The bad base64 data isn't being detected and when it is eventually pulled apart as an RSA key it blows up spectacularly.

Comment 37 tehmasp 2016-01-21 02:56:40 UTC
@Rob - cool; I'll leave it in your court for now. Let me know if I can help further!

Cheers,
Tehmasp Chaudhri

Comment 38 Kamil Dudka 2016-01-21 11:25:22 UTC
I have created bug #1300652 to track the newly discovered issue separately.


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