Skip to content

pkcs11: fix undefined behavior in attribute initialization (compound literals)#821

Open
sahesaha wants to merge 1 commit into
OP-TEE:masterfrom
sahesaha:minkipc_pkcs
Open

pkcs11: fix undefined behavior in attribute initialization (compound literals)#821
sahesaha wants to merge 1 commit into
OP-TEE:masterfrom
sahesaha:minkipc_pkcs

Conversation

@sahesaha

Copy link
Copy Markdown

Problem:
Incorrect attribute values are observed in test_create_destroy_session_objects,
CKA_TOKEN = 0xe0 (expected CK_FALSE / 0)
The issue appears with default toolchain optimizations, even though no explicit -O2 is set. It disappears when forcing -O0, indicating optimization-dependent undefined behavior.

Root cause
Attributes are initialized using compound literals, e.g.:
C&(CK_BBOOL){CK_FALSE}
These create temporary objects whose addresses are stored and reused.
Their lifetime and storage are not guaranteed, and under optimization the compiler may reuse or overlap them, leading to invalid values.

Why not fix via -O0
Disabling optimization is not a valid solution:

Hides undefined behavior instead of fixing it
Not portable across compilers/toolchains
Impacts performance in security-sensitive code

Fix
Replace compound literals with static variables having stable storage:
e.g. static const CK_BBOOL ck_false = CK_FALSE;

@sahesaha

Copy link
Copy Markdown
Author

Hi @etienne-lms , could you review this PR?

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 this pull request may close these issues.

1 participant