Skip to content

JDK-6676643: Improve current C_GetAttributeValue native implementation #3709

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

Closed
wants to merge 4 commits into from

Conversation

valeriepeng
Copy link
Contributor

@valeriepeng valeriepeng commented Apr 27, 2021

Anyone can help review this somewhat trivial fix? The main change is inside src/jdk.crypto.cryptoki/share/native/libj2pkcs11/p11_objmgmt.c. This is to help better troubleshooting by reporting the type of unavailable attributes in PKCS11 exception message when C_GetAttributeValue(...) call failed. The java file changes are just cleanup for consolidating the CKR_* constants definition into PKCS11Exception class.

Thanks,
Valerie


Progress

  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue
  • Change must be properly reviewed

Issue

  • JDK-6676643: Improve current C_GetAttributeValue native implementation

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.java.net/jdk pull/3709/head:pull/3709
$ git checkout pull/3709

Update a local copy of the PR:
$ git checkout pull/3709
$ git pull https://git.openjdk.java.net/jdk pull/3709/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 3709

View PR using the GUI difftool:
$ git pr show -t 3709

Using diff file

Download this PR as a diff file:
https://git.openjdk.java.net/jdk/pull/3709.diff

Include mechanism info into PKCS11 exception message and remove
duplicated constant definitions in PKCS11Constants and PKCS11Exception
classes.
@bridgekeeper
Copy link

bridgekeeper bot commented Apr 27, 2021

👋 Welcome back valeriep! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk openjdk bot added the rfr Pull request is ready for review label Apr 27, 2021
@openjdk
Copy link

openjdk bot commented Apr 27, 2021

@valeriepeng The following label will be automatically applied to this pull request:

  • security

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command.

@openjdk openjdk bot added the security security-dev@openjdk.org label Apr 27, 2021
@mlbridge
Copy link

mlbridge bot commented Apr 27, 2021

Webrevs


if (rv != CKR_OK) {
if (rv == CKR_ATTRIBUTE_SENSITIVE || rv == CKR_ATTRIBUTE_TYPE_INVALID) {
msg = malloc(80); // should be more than sufficient
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm worried about asserting that 80 is sufficient especially as extremely large numbers of attributes could be retrieved and the limit check later on seems a bit lax.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

80 should be sufficient, as we only include attribute type of those UNAVAILABLE ones. For example, there may be 6 attributes specified, but realistically only one or two being unavailable. Are you aware of any case which a large number of attributes are unavailable? I added the limit check to ensure no out-of-bound writes. In the worst case, the reported types may be incomplete, i.e. up to 80 bytes. Otherwise, we may need two passes, one to find out the number of unavailable attributes, and one to allocate and set the content. Or, do you have other suggestion?

// format msg w/ attribute(s) whose value is unavailable
temp1 = msg;
temp2 = msg + 80;
for (i = 0; i < ckAttributesLength && temp1 < temp2; i++) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that this loop will append at most 11 bytes to the string each time (is this correct?), if so, we can make the check temp1 < temp2 - 12 to count the final null and avoid running off the end with a buffer overflow.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I apologize. This counting code is correct and doesn't have any of the issues I claimed. snprintf takes care of everything and I just missed it last night.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's alright, thanks for checking and review.

if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) {

if (rv != CKR_OK) {
if (rv == CKR_ATTRIBUTE_SENSITIVE || rv == CKR_ATTRIBUTE_TYPE_INVALID) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the PKCS#11v2.40 spec, CKR_BUFFER_TOO_SMALL should be handled in the same special ways as these too (in that it isn't a "true error").

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this particular call, the pValue field is null, it's meant to query the exact length of the specified attribute. Thus, CKR_BUFFER_TOO_SMALL should not be returned.
Afterwards, we then allocate the buffer based on this queried result, so CKR_BUFFER_TOO_SMALL should also not occur.
So, based on the current API usage, CKR_BUFFER_TOO_SMALL should not happen.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All my concerns are addressed then. So, while my review doesn't count towards acceptance of this change, everything LGTM.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your review still. Good to have an extra pair of eyes to look over the changes...

* CKR_OK. Otherwise, it returns the returnValue as a jLong.
* This function generates a PKCS#11Exception with the returnValue as the
* errorcode. If the returnValue is not CKR_OK. The functin returns 0, if the
* returnValue is CKR_OK. Otherwise, it returns the returnValue as a jLong.
*
* @param env - used to call JNI funktions and to get the Exception class
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NitPick: here and above some German seems to have slipped in. I think we want "functions"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't notice the German, just copy-n-paste. Will fix the typos.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other PKCS#11 JNI sources have the same German typos, I prefer to leave them out of this. Will fix them when they are touched in other fixes.

Copy link
Member

@XueleiFan XueleiFan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me except a few trivial comments.

};

private static void register(String name, long errorCode) {
errorMap.put(errorCode, name);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I may check the put() return value and throw error for duplicated items, just in case duplicated copy-and-past register.

// for unknown PKCS11 return values, just use hex as its string
if (res == null) {
res = "0x" + Functions.toFullHexString((int)errorCode);
errorMap.put(errorCode, res);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice if the errorMap is immutable once the known error codes get registered.

register("CKR_FUNCTION_REJECTED", 0x00000200L);
register("CKR_TOKEN_RESOURCE_EXCEEDED", 0x00000201L);
register("CKR_OPERATION_CANCEL_FAILED", 0x00000202L);
register("CKR_VENDOR_DEFINED", 0x80000000L);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See below comment, I may have the hash map unmodifiable after the registration.

@openjdk
Copy link

openjdk bot commented May 13, 2021

@valeriepeng This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

6676643: Improve current C_GetAttributeValue native implementation

Reviewed-by: xuelei

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been 314 new commits pushed to the master branch:

As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details.

➡️ To integrate this PR with the above commit message to the master branch, type /integrate in a new comment.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label May 13, 2021
Comment on lines 210 to 212
long value() {
return value;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it is safe to use the 'value' variable directly for enum. The value() method may be not necessary. Just a personal preference, ignore it if you like the current way.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I like it too. Will update.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. I have no more comment.

@valeriepeng
Copy link
Contributor Author

/integrate

@openjdk openjdk bot closed this May 14, 2021
@openjdk openjdk bot added integrated Pull request has been integrated and removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels May 14, 2021
@openjdk
Copy link

openjdk bot commented May 14, 2021

@valeriepeng Since your change was applied there have been 315 commits pushed to the master branch:

Your commit was automatically rebased without conflicts.

Pushed as commit 7ab6dc8.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

@valeriepeng valeriepeng deleted the JDK-6676643 branch May 14, 2021 23:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
integrated Pull request has been integrated security security-dev@openjdk.org
Development

Successfully merging this pull request may close these issues.

3 participants