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

core: tee_svc.c: allow to pass non-NULL memref of size 0 #6405

Merged

Conversation

vincent-mailhol
Copy link
Contributor

When calling TEE_InvokeTACommand() with a non-NULL memref of size zero, function tee_svc_copy_param() will return
TEE_ERROR_BAD_PARAMETERS resulting in a panic later on.

In regard to the TEE_PARAM_TYPE_MEMREF_{INPUT,OUTPUT,INOUT} types, the Global Platform specification says [1]:

params[i].memref.size SHALL be initialized with a memory buffer that is accessible with the access rights described in the type. The buffer can be NULL, in which case size SHALL be set to 0 .

Note that the specification only specifies that NULL implies a size of zero. The reciprocal: "a size of zero implies NULL" is undefined.

It is even more confusing considering that non-NULL buffer with a size of zero are explicitly allowed in the client API [2]:

This design allows a non-NULL buffer with a size of 0 bytes to allow trivial integration with any implementations of the C library malloc, in which is valid to allocate a zero byte buffer and receive a non-NULL pointer which may not be de-referenced in return.

This could become a pitfall if a TA forwards a memref received from the REE to another TA.

Allow the TAs to pass non-NULL memref of size zero to other TAs through TEE_InvokeTACommand() by changing the non-NULL pointer into a NULL one in such a case.

[1] TEE Internal Core API Specification – Public Release v1.3.1,
§4.9.4 "Operation Parameters in the Internal Client API"
Table 4-15: "Interpretation of params[i] on Entry to Internal Client API"

[2] TEE Client API Specification v1.0,
§4.5.4 TEEC_RegisterSharedMemory,
paragraph "Implementers' Notes"

@jenswi-linaro
Copy link
Contributor

I do not often have to say this, but the commit message is a bit long with all the citations from the specs. It takes a while to see what you're trying to achieve.

Just describing the what and why should be enough in this case. It's OK with a few references, but save those after the what and why.

Please add a test case for this. A new case 1041 if you can't find a relevant one to extend.

@vincent-mailhol
Copy link
Contributor Author

I do not often have to say this, but the commit message is a bit long with all the citations from the specs. It takes a while to see what you're trying to achieve.

Just describing the what and why should be enough in this case. It's OK with a few references, but save those after the what and why.

I amended the commit description (I left the PR text untouched). Please let me know if this addresses your concerns.

Please add a test case for this. A new case 1041 if you can't find a relevant one to extend.

It took me a bit of time to figure out that you were referring to another code base. I never used optee_test before. Now I am trying to figure out how to compile it (c.f. OP-TEE/optee_test#702). I can write the test, but please be patient.

@jforissier
Copy link
Contributor

It took me a bit of time to figure out that you were referring to another code base. I never used optee_test before. Now I am trying to figure out how to compile it (c.f. OP-TEE/optee_test#702). I can write the test, but please be patient.

You can easily create a full development environment as explained here: https://optee.readthedocs.io/en/latest/building/devices/qemu.html#qemu-v8

Copy link
Contributor

@etienne-lms etienne-lms left a comment

Choose a reason for hiding this comment

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

I agree with this change.

As Jens said, a test in optee_test would be nice (for maintenance purpose) and commit message could be simplified. Suggestion:

core: tee_svc.c: allow to pass non-NULL memref of size 0

Allows TAs to pass non-NULL memref of size zero to other TAs by
changing the non-NULL pointer into a NULL one in such a case.
GP TEE Internal Core API does not forbid such memref parameter
whereas the previous implementation generated a
TEE_ERROR_BAD_PARAMETERS error code when converting such memref
buffer pointer into a physical memory address.

This change is specifically needed to allow a TA to forward a REE
client memref for which GP TEE Client API explicitly allows such
non-NULL address zero sized memref. It also makes the TA implementation
more flexible when dealing with its own memref.

Note: this change impacts both TEE_OpenTASession() and
TEE_InvokeTACommand().

@vincent-mailhol
Copy link
Contributor Author

You can easily create a full development environment as explained here: https://optee.readthedocs.io/en/latest/building/devices/qemu.html#qemu-v8

@jforissier Thanks for the suggestion. I am aware of this. I could use it but I am not familiar with repo which makes it hard for me to modify (and I have no plan to invest time into studying repo). I am using the different modules my way. It takes a bit more time to onboard a new piece to the project, but I enjoy the flexibility. I solved my last issues, so now I am fine.

As Jens said, a test in optee_test would be nice

@etienne-lms Yes, as I commented, I will do it, but be patient. I did not plan for this extra step when submitting my patch, and, as always, time being a scarce resource, I prefer not to commit on when I will do it.

Thanks for the rephrasing. I like it. I did small changes:

@jforissier
Copy link
Contributor

You can easily create a full development environment as explained here: https://optee.readthedocs.io/en/latest/building/devices/qemu.html#qemu-v8

@jforissier Thanks for the suggestion. I am aware of this. I could use it but I am not familiar with repo which makes it hard for me to modify (and I have no plan to invest time into studying repo). I am using the different modules my way. It takes a bit more time to onboard a new piece to the project, but I enjoy the flexibility. I solved my last issues, so now I am fine.

Good to know 👍 Actually you don't have to know anything about repo. Just use it to clone everything, but then you can use git in each project (and make in build/ to build & run of course) and not care about any repo workflow anymore. That's what I do.

@jbech-linaro
Copy link
Contributor

jbech-linaro commented Oct 31, 2023

You can easily create a full development environment as explained here: https://optee.readthedocs.io/en/latest/building/devices/qemu.html#qemu-v8

@jforissier Thanks for the suggestion. I am aware of this. I could use it but I am not familiar with repo which makes it hard for me to modify (and I have no plan to invest time into studying repo). I am using the different modules my way. It takes a bit more time to onboard a new piece to the project, but I enjoy the flexibility. I solved my last issues, so now I am fine.

Good to know 👍 Actually you don't have to know anything about repo. Just use it to clone everything, but then you can use git in each project (and make in build/ to build & run of course) and not care about any repo workflow anymore. That's what I do.

Yes, very few steps, like this. Although I'd just use the QEMU v8 manifest (like Jerome linked to above) nowadays instead of default v7.

@jenswi-linaro
Copy link
Contributor

I did not plan for this extra step when submitting my patch, and, as always, time being a scarce resource, I prefer not to commit on when I will do it.

We're usually not merging untested code. It's also easier to review code if there's an example that exercises it and shows that it works.

@vincent-mailhol
Copy link
Contributor Author

I did not plan for this extra step when submitting my patch, and, as always, time being a scarce resource, I prefer not to commit on when I will do it.

We're usually not merging untested code. It's also easier to review code if there's an example that exercises it and shows that it works.

Refer to my sentence just before the one you quoted:

Yes, as I commented, I will do it, but be patient.

I am doing this on my free time, so please forgive if my progress is slow.

If you think this is taking too long, maybe I can close the pull request and reopen it when ready?

vincent-mailhol added a commit to vincent-mailhol/optee_test that referenced this pull request Nov 17, 2023
Add a subtest to assert that the implementation allows to forward
non-NULL memref from a TA to another TA.

Regression 1016 already contains tests related to forwarding memref
between TAs. Thus extend this existing test with the subtest described
above instead of writing a new one.

The Global Platform specification allows this, however, at the time of
writing, optee-os will panic. A fix is proposed at [1].

[1] core: tee_svc.c: allow to pass non-NULL memref of size 0
Link: OP-TEE/optee_os#6405

Signed-off-by: Vincent Mailhol <mailhol.vincent@wanadoo.fr>
@vincent-mailhol
Copy link
Contributor Author

@jenswi-linaro and @etienne-lms, as promised here is a test: OP-TEE/optee_test#708. Of course, merging this PR is a prerequisite for the test to success.

vincent-mailhol added a commit to vincent-mailhol/optee_test that referenced this pull request Nov 17, 2023
Add a subtest to assert that the implementation allows to forward
non-NULL memref from a TA to another TA.

Regression 1016 already contains tests related to forwarding memref
between TAs. Thus extend this existing test with the subtest described
above instead of writing a new one.

The Global Platform specification allows this, however, at the time of
writing, optee-os will panic. A fix is proposed at [1].

[1] core: tee_svc.c: allow to pass non-NULL memref of size 0
Link: OP-TEE/optee_os#6405

Signed-off-by: Vincent Mailhol <mailhol.vincent@wanadoo.fr>
Copy link
Contributor

@etienne-lms etienne-lms left a comment

Choose a reason for hiding this comment

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

Hi Vincent, thanks for the optee_test counter part. It will help this P-R to land.

Reviewed-by: Etienne Carriere <etienne.carriere@foss.st.com> for this change.

@jforissier, could you enable CI test for this P-R and for the related optee_test P-R OP-TEE/optee_test#708?

vincent-mailhol added a commit to vincent-mailhol/optee_test that referenced this pull request Nov 19, 2023
Add a subtest to assert that the implementation allows to forward
non-NULL memref from a TA to another TA.

Regression 1016 already contains tests related to forwarding memref
between TAs. Thus extend this existing test with the subtest described
above instead of writing a new one.

The Global Platform specification allows this, however, at the time of
writing, optee-os will panic. A fix is proposed at [1].

[1] core: tee_svc.c: allow to pass non-NULL memref of size 0
Link: OP-TEE/optee_os#6405

Signed-off-by: Vincent Mailhol <mailhol.vincent@wanadoo.fr>
Allow TAs to pass non-NULL memref of size zero to other TAs by
changing the non-NULL pointer into a NULL one in such a case. GP TEE
Internal Core API does not forbid such memref parameter [1] whereas
the previous implementation generated a TEE_ERROR_BAD_PARAMETERS error
code when converting such memref buffer pointer into a physical memory
address.

This change is specifically needed to allow a TA to forward a REE
client memref for which GP TEE Client API explicitly allows such
non-NULL address zero sized memref [2]. It also makes the TA
implementation more flexible when dealing with its own memref.

[1] TEE Internal Core API Specification – Public Release v1.3.1,
    §4.9.4 "Operation Parameters in the Internal Client API"
    Table 4-15: "Interpretation of params[i] on Entry to Internal Client
    API"

[2] TEE Client API Specification v1.0, §4.5.4 TEEC_RegisterSharedMemory,
    paragraph "Implementers' Notes"

Signed-off-by: Vincent Mailhol <mailhol.vincent@wanadoo.fr>
Reviewed-by: Etienne Carriere <etienne.carriere@foss.st.com>
vincent-mailhol added a commit to vincent-mailhol/optee_test that referenced this pull request Nov 19, 2023
Add a subtest to assert that the implementation allows to forward
non-NULL memref from a TA to another TA.

Regression 1016 already contains tests related to forwarding memref
between TAs. Thus extend this existing test with the subtest described
above instead of writing a new one.

The Global Platform specification allows this, however, at the time of
writing, optee-os will panic. A fix is proposed at [1].

[1] core: tee_svc.c: allow to pass non-NULL memref of size 0
Link: OP-TEE/optee_os#6405

Signed-off-by: Vincent Mailhol <mailhol.vincent@wanadoo.fr>
Reviewed-by: Etienne Carriere <etienne.carriere@foss.st.com>
vincent-mailhol added a commit to vincent-mailhol/optee_test that referenced this pull request Nov 20, 2023
Add a subtest to assert that the implementation allows to forward
non-NULL memref from a TA to another TA.

Regression 1016 already contains tests related to forwarding memref
between TAs. Thus extend this existing test with the subtest described
above instead of writing a new one.

The Global Platform specification allows this, however, at the time of
writing, optee-os will panic. A fix is proposed at [1].

[1] core: tee_svc.c: allow to pass non-NULL memref of size 0
Link: OP-TEE/optee_os#6405

Signed-off-by: Vincent Mailhol <mailhol.vincent@wanadoo.fr>
Reviewed-by: Etienne Carriere <etienne.carriere@foss.st.com>
vincent-mailhol added a commit to vincent-mailhol/optee_test that referenced this pull request Nov 20, 2023
Add a subtest to assert that the implementation allows to forward
non-NULL memref from a TA to another TA.

Regression 1016 already contains tests related to forwarding memref
between TAs. Thus extend this existing test with the subtest described
above instead of writing a new one.

The Global Platform specification allows this, however, at the time of
writing, optee-os will panic. A fix is proposed at [1].

[1] core: tee_svc.c: allow to pass non-NULL memref of size 0
Link: OP-TEE/optee_os#6405

Signed-off-by: Vincent Mailhol <mailhol.vincent@wanadoo.fr>
Reviewed-by: Etienne Carriere <etienne.carriere@foss.st.com>
Reviewed-by: Jens Wiklander <jens.wiklander@linaro.org>
@jforissier jforissier merged commit 4527964 into OP-TEE:master Nov 20, 2023
8 checks passed
jforissier pushed a commit to OP-TEE/optee_test that referenced this pull request Nov 20, 2023
Add a subtest to assert that the implementation allows to forward
non-NULL memref from a TA to another TA.

Regression 1016 already contains tests related to forwarding memref
between TAs. Thus extend this existing test with the subtest described
above instead of writing a new one.

The Global Platform specification allows this, however, at the time of
writing, optee-os will panic. A fix is proposed at [1].

[1] core: tee_svc.c: allow to pass non-NULL memref of size 0
Link: OP-TEE/optee_os#6405

Signed-off-by: Vincent Mailhol <mailhol.vincent@wanadoo.fr>
Reviewed-by: Etienne Carriere <etienne.carriere@foss.st.com>
Reviewed-by: Jens Wiklander <jens.wiklander@linaro.org>
@vincent-mailhol vincent-mailhol deleted the svc-allow-memref-of-size-0 branch November 20, 2023 23:42
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.

None yet

5 participants