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

secure_storage: add a ZMS-based implementation of the ITS store module #81235

Merged

Conversation

tomi-font
Copy link
Collaborator

@tomi-font tomi-font commented Nov 11, 2024

See the commit messages.

A follow-up PR will introduce the (static) allocation of psa_storage_uid_t values for end users of the PSA Secure Storage API.

@Laczen
Copy link
Collaborator

Laczen commented Nov 11, 2024

@tomi-font,
a. do not use storage_partition, this is already used for other purposes and cannot be used by two systems at the same time,
b. zms, nvs, or settings are not really a good fit for secure storage as they use more flash pages (at least 2) and ensure persistence, something that might not be required for secure storage,
c. the construction of the zms uid is fragile and might break as a result of firmware updates,

@Laczen
Copy link
Collaborator

Laczen commented Nov 12, 2024

@tomi-font I would advise on the following alternative approach:

  1. A config variable CONFIG_ITS_NUM_ASSETS is defined,
  2. A config variable CONFIG_ITS_FIRST_ASSET_ID is defined,

The id's CONFIG_ITS_FIRST_ASSET_ID until CONFIG_ITS_FIRST_ASSET_ID + CONFIG_ITS_NUM_ASSETS - 1 are used to store metadata (psa_its_id + uid.... + flags + nonce + tag),
The id's CONFIG_ITS_FIRST_ASSET_ID + CONFIG_ITS_NUM_ASSETS until CONFIG_ITS_FIRST_ASSET_ID + 2 * CONFIG_ITS_NUM_ASSETS -1 are used to store the data,

In the saving process the data is first written, then the metadata. In the reading process the metadata id's are searched for the correct psa_its_id, ... , a "reread" can be performed if needed (depending on the flags), followed by the read/validation of the data. At start any data that does not have a corresponding (or invalid) metadata can be removed.

This approach is also applicable to nvs, allows simultaneous use of nvs/... for secure storage, settings and other data storage (e.g when CONFIG_SETTINGS_NVS is defined the CONFIG_ITS_FIRST_ASSET_ID could be calculated as 0x8000 - 2 * CONFIG_ITS_NUM_ASSETS).

@tomi-font tomi-font force-pushed the secure_storage_its_store_zms branch 2 times, most recently from e778058 to fb58bc3 Compare November 13, 2024 09:35
@tomi-font
Copy link
Collaborator Author

a. do not use storage_partition, this is already used for other purposes and cannot be used by two systems at the same time

Good point. I was thinking about how to handle potential conflicts, but I think that I will just remove the possibility to use storage_partition.

b. zms, nvs, or settings are not really a good fit for secure storage as they use more flash pages (at least 2) and ensure persistence, something that might not be required for secure storage

This makes no sense.

c. the construction of the zms uid is fragile and might break as a result of firmware updates

No it's not. And related to that, I will address the topic of UID assignment (for secure storage) in a follow-up PR.

I would advise on the following alternative approach:
[...]

I don't want to reinvent the storage wheel, plus I don't see the advantage in what you're proposing. Might as well just use the settings-based implementation.
For what you're proposing, there would need to be a mechanism to allocate ID ranges to different users of a same partition, which we don't currently have.

@Laczen
Copy link
Collaborator

Laczen commented Nov 13, 2024

b. zms, nvs, or settings are not really a good fit for secure storage as they use more flash pages (at least 2) and ensure persistence, something that might not be required for secure storage

This makes no sense.

Ok if you think they are a good fit.

c. the construction of the zms uid is fragile and might break as a result of firmware updates

No it's not. And related to that, I will address the topic of UID assignment (for secure storage) in a follow-up PR.

No need to address this in a follow-up PR, do it now before it gets used.

I would advise on the following alternative approach:
[...]

I don't want to reinvent the storage wheel, plus I don't see the advantage in what you're proposing. Might as well just use the settings-based implementation. For what you're proposing, there would need to be a mechanism to allocate ID ranges to different users of a same partition, which we don't currently have.

There are already users that are using the NVS backend for both settings and data storage, the allocation of ID ranges could be formalized.

If on the other hand it is needed that the secure storage has its own backend (as implied by the PR), a slightly modified copy of the flash backend used in tf-m would be more appropriate.

If the settings-based implementation fits the need, why propose a different alternative?

@tomi-font
Copy link
Collaborator Author

No need to address this in a follow-up PR, do it now before it gets used.

What I am talking about is not specific to what is added in this PR. It's about the PSA Secure Storage API overall. And the rationale for not including it as part of this PR is to keep the scope of a PR focused on one topic.

There are already users that are using the NVS backend for both settings and data storage, the allocation of ID ranges could be formalized.

I agree that it could and even should be formalized.

If on the other hand it is needed that the secure storage has its own backend (as implied by the PR)

No, it's not needed. This PR is just to allow direct use of a partition for more control and efficiency of the storage. For the majority of cases it will actually still default to using settings because storage_partition will be defined but not secure_storage_partition.

@Laczen
Copy link
Collaborator

Laczen commented Nov 14, 2024

No, it's not needed. This PR is just to allow direct use of a partition for more control and efficiency of the storage. For the majority of cases it will actually still default to using settings because storage_partition will be defined but not secure_storage_partition.

If it is worth using a partition for more control and efficiency of storage it would be good to target as much users as possible. NVS is still much broader used (it is more efficient for most users) and has a large install base. It doesn't seem a good choice to leave these users without a possible more efficient storage solution.

Splitting up the storage into metadata (uid, caller uid, flags, (optional) nonce, (optional) tag) and the data itself would give the following advantages:

  1. No limitation is build into the uid,
  2. No limitation is build into the caller uid,
  3. The stack usage would be reduced as it is no longer required to create the combination of metadata and data before writing to storage,
  4. It can be used by both nvs and zms,

Copy link
Contributor

@rghaddab rghaddab left a comment

Choose a reason for hiding this comment

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

Thank you for adding ZMS backend to secure_storage.
Some few comments but all minors.
Also maybe add a separate commit where you clang-format old code to get rid of github warnings

subsys/secure_storage/src/its/store/zms.c Show resolved Hide resolved
subsys/secure_storage/src/its/store/zms.c Show resolved Hide resolved
subsys/secure_storage/src/its/store/zms.c Outdated Show resolved Hide resolved
@tomi-font
Copy link
Collaborator Author

Also maybe add a separate commit where you clang-format old code to get rid of github warnings

I'm not a fan of the suggestions. Plus they're just notices. (And have sparked some debate on Discord.) They can be hidden with the a key. 🙂

@tomi-font tomi-font force-pushed the secure_storage_its_store_zms branch from fb58bc3 to 34ddbcf Compare November 14, 2024 11:54
@tomi-font tomi-font marked this pull request as ready for review November 14, 2024 11:54
@tomi-font tomi-font requested a review from rghaddab November 14, 2024 11:54
@tomi-font tomi-font assigned tomi-font and unassigned de-nordic Nov 14, 2024
@tomi-font tomi-font force-pushed the secure_storage_its_store_zms branch 4 times, most recently from fcb9c32 to ec6f856 Compare November 15, 2024 09:12
rghaddab
rghaddab previously approved these changes Nov 22, 2024
Copy link
Contributor

@rghaddab rghaddab left a comment

Choose a reason for hiding this comment

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

Only some minor comments that you are free to take into account or not

subsys/secure_storage/Kconfig.its_store Show resolved Hide resolved
The sector size impacts the runtime behavior of ZMS and restricts the maximum
ITS entry data size (which is the sector size minus ZMS and ITS overhead).
Changing it will result in loss of existing data stored on a partition.
It must be a multiple of the flash page size.
Copy link
Contributor

Choose a reason for hiding this comment

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

only for a certain type of flash (that requires erase)

Copy link
Collaborator

Choose a reason for hiding this comment

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

only for a certain type of flash (that requires erase)

I would rather say "for devices that require erase". There are devices that have erase and have paging (only for that purpose) but you can just write them without explicitly erasing first and without loosing written data in process.

@tomi-font tomi-font dismissed stale reviews from de-nordic and rghaddab via 36429bb November 27, 2024 10:43
@tomi-font tomi-font force-pushed the secure_storage_its_store_zms branch from 4e601e3 to 36429bb Compare November 27, 2024 10:43
@tomi-font tomi-font force-pushed the secure_storage_its_store_zms branch 2 times, most recently from d996816 to e853d2f Compare November 28, 2024 07:08
@tomi-font tomi-font requested a review from Laczen November 28, 2024 07:09
It becomes the new default when the secure_storage_its_partition
devicetree chosen property is defined as it is a preferred alternative.

See the help message of the
`CONFIG_SECURE_STORAGE_ITS_STORE_IMPLEMENTATION_ZMS` Kconfig option
for more information.

Signed-off-by: Tomi Fontanilles <[email protected]>
Add a test scenario for the ITS store module implementation that uses
ZMS for storage.
Reorganize the others at the same time.

Signed-off-by: Tomi Fontanilles <[email protected]>
Remove the hard restriction on CONFIG_SECURE_STORAGE_ITS_MAX_DATA_SIZE.
SETTINGS_MAX_VAL_LEN is in practice not used by any settings backend
implementation.

Signed-off-by: Tomi Fontanilles <[email protected]>
Output a CMake error when the ITS store module is enabled but no
implementation ended up enabled (due to unfulfilled prerequisites).
This is to make it more clear than undefined references at link time.
Not a fatal error because CMake cannot fail for the twister filtering
to work on the tests.

Signed-off-by: Tomi Fontanilles <[email protected]>
…tition

Align with the behavior of the settings subsystem.

Signed-off-by: Tomi Fontanilles <[email protected]>
Use %zu for size_t.

Signed-off-by: Tomi Fontanilles <[email protected]>
Align the debug logging with that of the ZMS-based implementation.

Signed-off-by: Tomi Fontanilles <[email protected]>
@tomi-font tomi-font force-pushed the secure_storage_its_store_zms branch from e853d2f to 28c984e Compare December 2, 2024 11:22
@tomi-font
Copy link
Collaborator Author

Rebased to fix a conflict in overlay-default_transform.conf (now overlay-transform_default.conf).

@tomi-font
Copy link
Collaborator Author

@Laczen Following the contribution process, I am dismissing your review as you have just nacked this PR on the premise that the implementation is not compliant with the specification, which is something that has already been discussed and approved in the security working group. Plus this subsystem is still new and experimental; improvements are to come which will make it more usable.
Adding the DNM label for one business day.

@tomi-font tomi-font added the DNM This PR should not be merged (Do Not Merge) label Dec 16, 2024
@Laczen
Copy link
Collaborator

Laczen commented Dec 16, 2024

@tomi-font, I will be short on this:

  1. The proposal will not allow zephyr to function as a key store for other devices, which is the reason a uint64 uid was created in the first place,
  2. The proposal forces the use of zms which is suboptimal for most devices,
  3. The proposal will force an extra 2 sectors for a solution using psa crypto and secure storage, effectively increasing the flash usage of psa crypto with 8kB in most applications (without taking into account the flash requirement for the zms system),
  4. The only reason I can see for the proposed solution is to have faster access to secure storage information, but you might be surprised by the fact that this fast access can be quite slow when zms (or nvs which has the same properties is in a proces of garbage collection. Any subsystem that needs access to a key fast (because it needs to respond in a timely fashion) cannot count on the key access being fast.
  5. A storage solution can sometimes be a pain, it lives independently of images and cannot be easily modified. Even when the system is called experimental this cannot be applied to something that is independent of an image.

The responsability of the addition of this solution is entirely yours. I have given my feedback on the proposal as well as a means to remove its shortcomings.

As a last remark I would advise you to modify the psa crypto test to also validate using a vendor key as this is the most basic requirement.

@tomi-font tomi-font removed the DNM This PR should not be merged (Do Not Merge) label Dec 17, 2024
@fabiobaltieri fabiobaltieri added area: Input Input Subsystem and Drivers and removed area: Input Input Subsystem and Drivers labels Dec 17, 2024
@fabiobaltieri fabiobaltieri merged commit 23a6a78 into zephyrproject-rtos:main Dec 17, 2024
29 of 30 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants