Skip to content

Commit

Permalink
C_Login: fix segfault when user pin not setup
Browse files Browse the repository at this point in the history
A command like:
pkcs11-tool --module /usr/lib/libtpm2_pkcs11.so --init-token --label tpmhsm --so-pin foo --pin bar

Will cause a C_Login event becuase --pin is specified. However, C_InitPIN
has not been called to initialize the userpin. This causes an NPD when
trying to load the user sealobjects public and private blobs.

Fixes: tpm2-software#563

Signed-off-by: William Roberts <william.c.roberts@intel.com>
  • Loading branch information
William Roberts committed Jul 31, 2020
1 parent 9c5abd2 commit 2330c4e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/lib/session_ctx.c
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,15 @@ CK_RV session_ctx_login(session_ctx *ctx, CK_USER_TYPE user, CK_BYTE_PTR pin, CK
return CKR_OK;
}

sealobject *sealobj = &tok->sealobject;
twist sealpub = is_user(user) ? sealobj->userpub : sealobj->sopub;
twist sealpriv = is_user(user) ? sealobj->userpriv : sealobj->sopriv;

/* Detect if PIN has not been set and thus missing the respective user seal object */
if (!sealpub) {
LOGE("User pin is not initialized, call C_InitPIN");
return CKR_USER_PIN_NOT_INITIALIZED;
}

CK_RV tmp = tpm_session_start(tok->tctx, tok->pobject.objauth, tok->pobject.handle);
if (tmp != CKR_OK) {
Expand All @@ -275,10 +284,6 @@ CK_RV session_ctx_login(session_ctx *ctx, CK_USER_TYPE user, CK_BYTE_PTR pin, CK
on_error_flush_session = true;

/* load seal object */
sealobject *sealobj = &tok->sealobject;
twist sealpub = is_user(user) ? sealobj->userpub : sealobj->sopub;
twist sealpriv = is_user(user) ? sealobj->userpriv : sealobj->sopriv;

uint32_t pobj_handle = tok->pobject.handle;
twist pobjauth = tok->pobject.objauth;

Expand Down
12 changes: 12 additions & 0 deletions test/integration/pkcs11-tool-init.sh.nosetup
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,16 @@ fi
pkcs11_tool --slot-index=0 --login --pin=mynewuserpin --read-object --type data --label dataobj1 -o objdata2
cmp objdata objdata2

#
# Negative test, ensure that this fails
#
trap - ERR

# Reproduce https://github.com/tpm2-software/tpm2-pkcs11/issues/563
pkcs11_tool --slot-index=1 --init-token --label tpmhsm --so-pin foo --pin bar
if [ $? -eq 0 ]; then
echo "Expected login to fail without user pin set"
exit 1
fi

exit 0

0 comments on commit 2330c4e

Please sign in to comment.