Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Segmentation Fault using OpenSC with pam_pkcs11 (pkcs11_inspect) #1244

Closed
thukk opened this issue Jan 18, 2018 · 13 comments · Fixed by #1252
Closed

Segmentation Fault using OpenSC with pam_pkcs11 (pkcs11_inspect) #1244

thukk opened this issue Jan 18, 2018 · 13 comments · Fixed by #1252

Comments

@thukk
Copy link

thukk commented Jan 18, 2018

Problem Description

Last week, I was able to use a token that OpenSC identifies as SmartCard-HSM. I would at least get to the PIN request. This week, I rebuilt a machine and reinstalled OpenSC 0.17 using the default options. Now, I get a segmentation fault in pam_pkcs11 using the exact same token.

Steps to reproduce

Install OpenSC and pam_pkcs11. Token identifies as SmartCard-HSM by default.

pam_pkcs11.conf is set up to use opensc.
module is set to 'module = "/usr/lib/opensc-pkcs11.so";' (if left without the absolute path, the module is not found)

Run pkcs11_inspect. Segmentation Fault.

@thukk thukk changed the title Segmentation Fault using SmartCard-HSM Segmentation Fault using OpenSC with pam_pkcs11 (pkcs11_inspect) Jan 18, 2018
@thukk
Copy link
Author

thukk commented Jan 18, 2018

Here is the GBD output:

segfault.txt

@thukk
Copy link
Author

thukk commented Jan 18, 2018

for what its worth, if I force opensc to use the coolkey driver, i do not get a seg fault...

@frankmorgner
Copy link
Member

Which version of SC-HSM are you using? Could you write out the APDUs (debug = 3; in opensc.conf)?

@thukk
Copy link
Author

thukk commented Jan 19, 2018

To my knowledge, i am not running SC-HSM. The card is a SafeNet SC650.

@frankmorgner
Copy link
Member

At least OpenSC is thinking you're using a SC-HSM. Please send a debug log from something like OPENSC_DEBUG=3 pkcs11-tool --test --login.

@thukk
Copy link
Author

thukk commented Jan 19, 2018

OpenSCDebug.txt

FWIW, I had to specify the module explicitly...

@dengert
Copy link
Member

dengert commented Jan 19, 2018

card-sc-hsm.c issues SELECT AID for 00 A4 04 00 0B E8 2B 06 01 04 01 81 C3 1F 02 01 Le=00 with return 9000 sc_hsm_match_card`:

 239         sc_path_set(&path, SC_PATH_TYPE_DF_NAME, sc_hsm_aid.value, sc_hsm_aid.len, 0, 0);
 240         r = sc_hsm_select_file(card, &path, NULL);

But in sc_hsm_init

1611         sc_path_set(&path, SC_PATH_TYPE_DF_NAME, sc_hsm_aid.value, sc_hsm_aid.len, 0, 0);
1612         if (sc_hsm_select_file_ex(card, &path, 0, &file) == SC_SUCCESS
1613                         && file && file->prop_attr && file->prop_attr_len >= 5) {

where it does some additional checking of the response. Even that code is questionable.

The card may assume this was a valid select file, as there is no requirement to support AID and it did not ask for any response data.

I think the sc_match_card should request a response and check it closely to make sure this is a
SmartCard-HSM card.

More issues with the SELECT AID vs SELECT FILE to make sure we really have the correct card.

@thukk
Copy link
Author

thukk commented Jan 19, 2018

Forcing coolkey allows me to use pkcs11_inspect and see the cn and pwent data.

But, if I do pkcs11 --test --login, I get a pkcs11 function error: C_SignFinal failed: rv = CKR_ARGUMENTS_BAD(0x7).

I read on another forum that someone was getting the same error in 0.16 using slackware and they fixed it by increasing the max send and receive size to 511 and 512, respectively. I tried that and got the same error, I even tried increasing it to max size.

I am guess this is because OpenSC doesn't fully support the SafeNet SC650 card, even using coolkey libraries.

frankmorgner added a commit to frankmorgner/OpenSC that referenced this issue Jan 19, 2018
@frankmorgner
Copy link
Member

I fixed the segmentation fault in the pull request.

From the product sheet it looks like your token is a java card that can load other applets, in particular it is possible that it contains the SC-HSM applet. If that should not be the case, then you should report to SafeNet that your card erroneously reports the existence of the SC-HSM applet. You should ask what kind of PKI applet exists on your card so that forcing an internal card driver allows you using the token.

@frankmorgner
Copy link
Member

@thukk could you check if your issue is fixed with #1252?

@CardContact
Copy link
Member

I don't quite understand why the other card issues a SW1/SW2=9000 in response to a SELECT with unknown FID or AID. This is severe bug in the other card implementation.

However what puzzles me, is that the driver tries to select the sc-hsm applet even though the ATR does not match the ATR whitelist.

I have a feeling that the issue is caused by 3e7f7e6.

@frankmorgner
Copy link
Member

@CardContact the whitelist is a shortcut to avoid the selection (to be faster). If it doesn't match then sc_hsm_match_card still issues the SELECT.

@dengert
Copy link
Member

dengert commented Jan 31, 2018

With a card not in the match list, such as Yubikey the atr are matched twice.
../../../src/src/libopensc/card.c:234:sc_connect_card: trying driver 'sc-hsm'

../../../src/src/libopensc/card.c:273:sc_connect_card: trying driver 'sc-hsm'
On this one, the SELECT AID is done:
00 A4 04 00 0B E8 2B 06 01 04 01 81 C3 1F 02 01 00
Luckily the Yubico returns 6A 82

This is an example of another driver using the SELECT AID causing interference because the older Yubikey loses the login state.

So the bug looks like it is the user's card should not have returned 9000.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants