p11-glue / p11-kit

Provides a way to load and enumerate PKCS#11 modules.
https://p11-glue.github.io/p11-glue/p11-kit.html
Other
151 stars 95 forks source link

[Question] Alignment guarantees for CK_ATTRIBUTE #626

Open H5117 opened 8 months ago

H5117 commented 8 months ago

PKCS#11 2.40 and later describes a CK_ATTRIBUTE in the following way:

typedef struct CK_ATTRIBUTE {
  CK_ATTRIBUTE_TYPE type;
  CK_VOID_PTR pValue;
  CK_ULONG ulValueLen;
} CK_ATTRIBUTE;

Note that pValue is a “void” pointer, facilitating the passing of arbitrary values. 
Both the application and Cryptoki library MUST ensure that the pointer can be safely cast to the expected type (i.e., without word-alignment errors).

And "the application" is defined in the following way:

Application       Any computer program that calls the Cryptoki interface.

But API of p11-kit for URIs in the gray zone here, because parsing a PKCS#11 URI does not require to call Cryptoki interface.

So, the question: what alignment guarantees does p11-kit provide for CK_ATTRIBUTEs returned by p11_kit_uri_get_attributes() or p11_kit_uri_get_attribute()? Can I pass these attributes to C_FindObjectsInit() safely or should I repack them to ensure that *pValue is properly aligned?

ZoltanFridrich commented 8 months ago

When setting attributes of a p11-kit URI, all attribute values are allocated with malloc() which "returns a pointer that is suitably aligned for any object type with fundamental alignment". So I guess the p11_kit_uri_get_attributes() and p11_kit_uri_get_attribute() functions should have the same allignment guarantee, ie. it should be safe to pass them to lets say C_FindObjectsInit().

H5117 commented 8 months ago

OK, i. e. for now it is safe. But what about future? Is there some policy in p11-kit that guarantees it won't change?

I am going to use this assumption in the code for unlocking a root volume with a PKCS#11 token, and with wrong alignment a system will fail to boot.

ZoltanFridrich commented 8 months ago

It is unlikely to change, but I don't think that currently there is any policy that guarantees this in p11-kit. We might add a guarantee. I will discuss this with @ueno

H5117 commented 7 months ago

Any decision on this?

ueno commented 7 months ago

As far as I recall, we reached the consensus that the API should guarantee the alignment. It may not be possible alone, if the applications set arbitrary attribute through p11_kit_uri_set_attribute; we can add the guidance to the documentation.