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

tee_api: check for session->ctx before dereferencing it #60

Closed
wants to merge 1 commit into from

Conversation

lorc
Copy link
Contributor

@lorc lorc commented Oct 19, 2016

We need to check if session->ctx is valid pointer befor dereferncing it.
Otherways it will lead to segfalut which is not desired behaviour for
library users.
This fill fix issue with segfaulting xtest when TEEC_OpenSession fails.

Signed-off-by: Volodymyr Babchuk vlad.babchuk@gmail.com

We need to check if session->ctx is valid pointer befor dereferncing it.
Otherways it will lead to segfalut which is not desired behaviour for
library users.
This fill fix issue with segfaulting xtest when TEEC_OpenSession fails.

Signed-off-by: Volodymyr Babchuk <vlad.babchuk@gmail.com>
@jforissier
Copy link
Contributor

Why do this? The client API is not supposed to be called with an invalid session or return an error status if that happens. I mean, why check ctx against zero when there are so many invalid virtual address values... In fact rather than adding tests, I would rather remove the NULL checks on the session pointer, except in TEE_CloseSession() for convenience, like free() accepts a NULL pointer. I'll check the GP spec and comment further tomorrow.

@lorc
Copy link
Contributor Author

lorc commented Oct 19, 2016

@jforissier , yes, that is the case. You can feed free() with NULL pointer and nothing bad will happen.
But in this case whole application crashes. In my opinion, library (even incorrectly used) should not crash application. It can return errors, write warnings to stderr, maybe even trigger asserts, but it should not cause segfault. It should be foolproof.

So, I have exactly opposite idea: lets leave NULL checks for session pointer and also lets add field with magic value to session structure. This will ensure that library always works with correctly created session. What do you think?

@jforissier
Copy link
Contributor

@lorc there is no way you can make the client API 'foolproof', reliably, and safely. Adding a magic value to the session structure will only catch a limited number of invalid sessions. What if the session pointer itself is bogus? Even with your proposed magic number. you'll still crash when de-referencing the pointer to read the magic value.
Parameter validation by APIs is a somewhat debated topic, you can find many references on the Internet. This one for instance: https://blogs.msdn.microsoft.com/larryosterman/2004/05/18/should-i-check-the-parameters-to-my-function/. It's about the Windows environment, so it's not entirely applicable to our discussion, but it shows that adding validation steps may add more issues than it actually solves.

I checked the GP spec, and there is nothing wrong with our current behavior. Only TEEC_CloseSession() is expected to check for session == NULL and do nothing in this case. Passing invalid parameters falls in the category called "Programmer Error", for which: "the implementation is not required to gracefully handle the error or even behave consistently, but MAY choose to generate a programmer visible response. [...] the Implementation MUST still guarantee the stability and security of the TEE and the shared communication subsystem in the rich environment because these modules are shared amongst all Client Applications and must not be affected by the misbehavior of a single Client Application".

So I stand on my position: no new validity checks, and let's keep the NULL checks on the session pointer that we already have because they're cheap, safe, perhaps moderately helpful, and compliant with the spec.

PS: you mentioned segfaults in xtest when TEEC_OpenSession() fails, I wouldn't mind a patch to fix that ;)

@jenswi-linaro
Copy link
Contributor

You're right Jerome. It doesn't become more robust with these added checks, what we do get is more unpredictable behavior. I think everyone prefers an early consistent crash compared to a late one that needs certain stuff to happen first.

@lorc lorc closed this Oct 20, 2016
@lorc
Copy link
Contributor Author

lorc commented Oct 20, 2016

@jforissier Okay, I see your point. I'll rework xtest then.

@jforissier
Copy link
Contributor

@lorc thank you.

etienne-lms added a commit to etienne-lms/optee_client that referenced this pull request Jan 31, 2020
Library ckteec will implement the PKCS#11 API using the PKCS11 trusted
application executing in OP-TEE as backend token.

Implement  pkcs11.h header file that partially covers the PKCS#11
specification. Resources initially planned to be supported are defined.
The header will need to be updated with remaining PKCS#11 definition
when related support will be implemented.

Signed-off-by: Etienne Carriere <etienne.carriere@linaro.org>

Check patch issues:

ERROR: need consistent spacing around '*' (ctx:WxV)
OP-TEE#86: FILE: libckteec/include/pkcs11.h:29:
+typedef CK_BYTE *CK_BYTE_PTR;
                 ^




WARNING: Prefer 'unsigned long' over 'unsigned long int' as the int is unnecessary
OP-TEE#48: FILE: libckteec/include/pkcs11.h:22:
+typedef unsigned long int      CK_ULONG;

ERROR: "foo *                   bar" should be "foo *bar"
OP-TEE#60: FILE: libckteec/include/pkcs11.h:34:
+typedef void *                 CK_VOID_PTR;

WARNING: do not add new typedefs
#424: FILE: libckteec/include/pkcs11.h:398:
+typedef struct CK_AES_CBC_ENCRYPT_DATA_PARAMS  CK_AES_CBC_ENCRYPT_DATA_PARAMS;

WARNING: space prohibited between function name and open parenthesis '('
#693: FILE: libckteec/include/pkcs11.h:667:
+typedef CK_RV (* CK_NOTIFY)(CK_SESSION_HANDLE hSession,

ERROR: space prohibited after that '*' (ctx:BxW)
#693: FILE: libckteec/include/pkcs11.h:667:
+typedef CK_RV (* CK_NOTIFY)(CK_SESSION_HANDLE hSession,

WARNING: Missing a blank line after declarations
#708: FILE: libckteec/include/pkcs11.h:682:
+       CK_VERSION version;
+       CK_RV (*C_Initialize)(CK_VOID_PTR pInitArgs);

WARNING: space prohibited between function name and open parenthesis '('
#712: FILE: libckteec/include/pkcs11.h:686:
+       CK_RV (*C_GetSlotList)(CK_BBOOL tokenPresent,
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.

3 participants