project-chip / connectedhomeip

Matter (formerly Project CHIP) creates more connections between more objects, simplifying development for manufacturers and increasing compatibility for consumers, guided by the Connectivity Standards Alliance.
https://buildwithmatter.com
Apache License 2.0
7.21k stars 1.91k forks source link

PersistentStorageDelegate SyncGetKeyValue not returning CHIP_ERROR_BUFFER_TOO_SMALL #16958

Closed mlepage-google closed 1 year ago

mlepage-google commented 2 years ago

Problem

Using chip::Server::GetPersistentStorage on Linux (e.g. in chip-all-clusters-app), whatever storage implementation is being used under the hood, it isn't returning a CHIP_ERROR_BUFFER_TOO_SMALL error (as documented) if calling SyncGetKeyValue with a buffer too small; it just silently truncates the data it gets.

E.g. I wrote 4 bytes and passed in a buffer of size 2, and got back no error, size 2 (but because it actually held 4 bytes, I should have received an error).

Proposed Solution

Audit code to ensure implementation is following the published API.

bzbarsky-apple commented 2 years ago

I expect this is src/include/platform/KvsPersistentStorageDelegate.h which calls into KeyValueStoreManager::Get, which eventually lands in src/platform/Linux/KeyValueStoreManagerImpl.cpp where we have, in _Get:

    if ((err != CHIP_NO_ERROR) && (err != CHIP_ERROR_BUFFER_TOO_SMALL))
    {
        return err;
    }

and otherwise we copy however much data we can and return CHIP_NO_ERROR.

That is definitely a violation of the KeyValueStoreManager API documentation.

This varies by platform as follows:

@mlepage-google Maybe we need separate issues here for the different buggy platforms?

bzbarsky-apple commented 2 years ago

I suspect we need to fix this to behave correctly, because various other code will not work right if this is wrong and we can end up with serious security problems.

kkasperczyk-no commented 2 years ago

@bzbarsky-apple I would say it should be fine for Zephyr/nRFConnect platform. The Get_ implementation requests value read from Zephyr subsys and then destination buffer size is verified in the Zephyr callback: https://github.com/project-chip/connectedhomeip/blob/master/src/platform/Zephyr/KeyValueStoreManagerImpl.cpp#L72. Result assigned in callback is then returned by the Get_ method: https://github.com/project-chip/connectedhomeip/blob/master/src/platform/Zephyr/KeyValueStoreManagerImpl.cpp#L125

tehampson commented 1 year ago

Finding based on survey results in general

Based on the responses to the survey these are conclusion we have made for matter 1.0:

Consideration to explore for PersistentStorageDelegate post 1.0:

Uses of PersistentStorageDelegate in SDK post 1.0 to explore:


The ability for platforms to quickly audit their PersistentStorageDelegate has been created. Based on https://github.com/project-chip/connectedhomeip/projects/86#card-83751232, many platforms have passed the audit. At this point it is on the platforms themselves to pass the audit. If there is a platform not adhering definitions and failing the audit, a separate issue can be created for that platform specifically.