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

[SILABS] lock storage refactor #37849

Open
wants to merge 10 commits into
base: master
Choose a base branch
from

Conversation

mykrupp
Copy link
Contributor

@mykrupp mykrupp commented Mar 3, 2025

The original silabs lock-app implementation was a basic example and did not take in to account RAM and NVM3 efficiency.

This refactor

  • reduces the RAM usage of the lock-app by removing static arrays of users/credentials
  • Using KVS to create individual nvm3 entries for each user, credential and schedule rather than directly storing multi-dimensional arrays into NVM3, reducing NVM3 max object size. This allows allows the app to have nvm3 storage increase only as the amount of data being stored increases, rather than storing partially empty arrays in storage.
  • Adds new storage structs, LockUserInfo and LockCredentialInfo, which don't include the Span types that are received from door-lock-server to reduce storage size further.
  • Improves security by segmenting user/credential/schedule storage in nvm3

Testing

Tested by running the TC_DRLK test cases in pipeline that runs silabs hardware.

@mergify mergify bot mentioned this pull request Mar 3, 2025
Copy link

github-actions bot commented Mar 3, 2025

PR #37849: Size comparison from ba8b539 to 29a9f25

Full report (74 builds for bl602, bl702, bl702l, cc13x4_26x4, cc32xx, cyw30739, efr32, esp32, linux, nrfconnect, nxp, psoc6, qpg, stm32, telink, tizen)
platform target config section ba8b539 29a9f25 change % change
bl602 lighting-app bl602+mfd+littlefs+rpc FLASH 1096866 1096866 0 0.0
RAM 94842 94842 0 0.0
bl702 lighting-app bl702+eth FLASH 651842 651842 0 0.0
RAM 33509 33509 0 0.0
bl702+wifi FLASH 829114 829114 0 0.0
RAM 22233 22233 0 0.0
bl706+mfd+rpc+littlefs FLASH 1061508 1061508 0 0.0
RAM 32157 32157 0 0.0
bl702l contact-sensor-app bl702l+mfd+littlefs FLASH 892352 892352 0 0.0
RAM 26896 26896 0 0.0
lighting-app bl702l+mfd+littlefs FLASH 975248 975248 0 0.0
RAM 24644 24644 0 0.0
cc13x4_26x4 lighting-app LP_EM_CC1354P10_6 FLASH 817232 817232 0 0.0
RAM 120272 120272 0 0.0
lock-ftd LP_EM_CC1354P10_6 FLASH 826152 826152 0 0.0
RAM 125368 125368 0 0.0
pump-app LP_EM_CC1354P10_6 FLASH 773036 773036 0 0.0
RAM 113740 113740 0 0.0
pump-controller-app LP_EM_CC1354P10_6 FLASH 757312 757312 0 0.0
RAM 113948 113948 0 0.0
cc32xx air-purifier CC3235SF_LAUNCHXL FLASH 540774 540774 0 0.0
RAM 205128 205128 0 0.0
lock CC3235SF_LAUNCHXL FLASH 574890 574890 0 0.0
RAM 205376 205376 0 0.0
cyw30739 light CYW30739B2-P5-EVK-01 unknown 2040 2040 0 0.0
FLASH 658941 658941 0 0.0
RAM 75412 75412 0 0.0
CYW30739B2-P5-EVK-02 unknown 2040 2040 0 0.0
FLASH 678801 678801 0 0.0
RAM 78052 78052 0 0.0
CYW30739B2-P5-EVK-03 unknown 2040 2040 0 0.0
FLASH 678801 678801 0 0.0
RAM 78052 78052 0 0.0
CYW930739M2EVB-02 unknown 2040 2040 0 0.0
FLASH 635725 635725 0 0.0
RAM 70480 70480 0 0.0
light-switch CYW30739B2-P5-EVK-01 unknown 2040 2040 0 0.0
FLASH 619181 619181 0 0.0
RAM 71652 71652 0 0.0
CYW30739B2-P5-EVK-02 unknown 2040 2040 0 0.0
FLASH 638817 638817 0 0.0
RAM 74196 74196 0 0.0
CYW30739B2-P5-EVK-03 unknown 2040 2040 0 0.0
FLASH 638817 638817 0 0.0
RAM 74196 74196 0 0.0
lock CYW30739B2-P5-EVK-01 unknown 2040 2040 0 0.0
FLASH 638669 638669 0 0.0
RAM 74660 74660 0 0.0
CYW30739B2-P5-EVK-02 unknown 2040 2040 0 0.0
FLASH 658393 658393 0 0.0
RAM 77204 77204 0 0.0
CYW30739B2-P5-EVK-03 unknown 2040 2040 0 0.0
FLASH 658393 658393 0 0.0
RAM 77204 77204 0 0.0
thermostat CYW30739B2-P5-EVK-01 unknown 2040 2040 0 0.0
FLASH 615009 615009 0 0.0
RAM 68748 68748 0 0.0
CYW30739B2-P5-EVK-02 unknown 2040 2040 0 0.0
FLASH 634869 634869 0 0.0
RAM 71388 71388 0 0.0
CYW30739B2-P5-EVK-03 unknown 2040 2040 0 0.0
FLASH 634869 634869 0 0.0
RAM 71388 71388 0 0.0
efr32 lock-app BRD4187C FLASH 939760 940384 624 0.1
RAM 159920 132112 -27808 -17.4
BRD4338a FLASH 732744 733504 760 0.1
RAM 234828 207028 -27800 -11.8
window-app BRD4187C FLASH 1032200 1032200 0 0.0
RAM 128024 128024 0 0.0
esp32 all-clusters-app c3devkit DRAM 98736 98736 0 0.0
FLASH 1591774 1591774 0 0.0
IRAM 83820 83820 0 0.0
m5stack DRAM 117516 117516 0 0.0
FLASH 1558614 1558614 0 0.0
IRAM 117039 117039 0 0.0
linux air-purifier-app debug unknown 4752 4752 0 0.0
FLASH 2653579 2653579 0 0.0
RAM 112304 112304 0 0.0
all-clusters-app debug unknown 5560 5560 0 0.0
FLASH 5974000 5974000 0 0.0
RAM 516568 516568 0 0.0
all-clusters-minimal-app debug unknown 5456 5456 0 0.0
FLASH 5300002 5300002 0 0.0
RAM 222488 222488 0 0.0
bridge-app debug unknown 5472 5472 0 0.0
FLASH 4652350 4652350 0 0.0
RAM 201344 201344 0 0.0
camera-app debug unknown 5456 5456 0 0.0
FLASH 4675486 4675486 0 0.0
RAM 195792 195792 0 0.0
chip-tool debug unknown 6112 6112 0 0.0
FLASH 13305263 13305263 0 0.0
RAM 603456 603456 0 0.0
chip-tool-ipv6only arm64 unknown 21976 21976 0 0.0
FLASH 11498120 11497544 -576 -0.0
RAM 656136 656136 0 0.0
fabric-admin debug unknown 5800 5800 0 0.0
FLASH 11570025 11570025 0 0.0
RAM 603240 603240 0 0.0
fabric-bridge-app debug unknown 4720 4720 0 0.0
FLASH 4455916 4455916 0 0.0
RAM 188168 188168 0 0.0
fabric-sync debug unknown 4976 4976 0 0.0
FLASH 5572901 5572901 0 0.0
RAM 471600 471600 0 0.0
lighting-app debug+rpc+ui unknown 6184 6192 8 0.1
FLASH 5519137 5519697 560 0.0
RAM 205168 205168 0 0.0
lock-app debug unknown 5424 5424 0 0.0
FLASH 4692168 4692168 0 0.0
RAM 192344 192344 0 0.0
ota-provider-app debug unknown 4760 4760 0 0.0
FLASH 4314346 4314346 0 0.0
RAM 181000 181000 0 0.0
ota-requestor-app debug unknown 4712 4712 0 0.0
FLASH 4444698 4444698 0 0.0
RAM 185488 185488 0 0.0
shell debug unknown 4240 4240 0 0.0
FLASH 2982604 2982604 0 0.0
RAM 145688 145688 0 0.0
thermostat-no-ble arm64 unknown 9448 9448 0 0.0
FLASH 4141928 4141464 -464 -0.0
RAM 229840 229840 0 0.0
tv-app debug unknown 5752 5752 0 0.0
FLASH 5911749 5911749 0 0.0
RAM 595032 595032 0 0.0
tv-casting-app debug unknown 5320 5320 0 0.0
FLASH 11475725 11475725 0 0.0
RAM 718672 718672 0 0.0
nrfconnect all-clusters-app nrf52840dk_nrf52840 FLASH 913684 913684 0 0.0
RAM 142909 142909 0 0.0
nrf7002dk_nrf5340_cpuapp FLASH 904428 904428 0 0.0
RAM 125245 125245 0 0.0
all-clusters-minimal-app nrf52840dk_nrf52840 FLASH 850464 850464 0 0.0
RAM 141271 141271 0 0.0
nxp contact k32w0+release FLASH 587456 587456 0 0.0
RAM 70980 70980 0 0.0
mcxw71+release FLASH 601272 601272 0 0.0
RAM 63096 63096 0 0.0
light k32w0+release FLASH 613188 613188 0 0.0
RAM 70268 70268 0 0.0
k32w1+release FLASH 685896 685896 0 0.0
RAM 48584 48584 0 0.0
lock mcxw71+release FLASH 750112 750112 0 0.0
RAM 67500 67500 0 0.0
psoc6 all-clusters cy8ckit_062s2_43012 FLASH 1658236 1658236 0 0.0
RAM 212344 212344 0 0.0
all-clusters-minimal cy8ckit_062s2_43012 FLASH 1562476 1562476 0 0.0
RAM 208560 208560 0 0.0
light cy8ckit_062s2_43012 FLASH 1441276 1441276 0 0.0
RAM 197296 197296 0 0.0
lock cy8ckit_062s2_43012 FLASH 1470164 1470164 0 0.0
RAM 224960 224960 0 0.0
qpg lighting-app qpg6105+debug FLASH 663852 663852 0 0.0
RAM 105156 105156 0 0.0
lock-app qpg6105+debug FLASH 622312 622312 0 0.0
RAM 99768 99768 0 0.0
stm32 light STM32WB5MM-DK FLASH 459920 459920 0 0.0
RAM 141472 141472 0 0.0
telink bridge-app tl7218x FLASH 669270 669270 0 0.0
RAM 90752 90752 0 0.0
contact-sensor-app tlsr9528a_retention FLASH 622132 622132 0 0.0
RAM 31488 31488 0 0.0
light-app-ota-compress-lzma-shell-factory-data tl3218x FLASH 760918 760918 0 0.0
RAM 40420 40420 0 0.0
light-app-ota-shell-factory-data tl7218x FLASH 754026 754026 0 0.0
RAM 97540 97540 0 0.0
light-switch-app-ota-compress-lzma-factory-data tl7218x_retention FLASH 681076 681076 0 0.0
RAM 52192 52192 0 0.0
light-switch-app-ota-compress-lzma-shell-factory-data tlsr9528a FLASH 709634 709634 0 0.0
RAM 73400 73400 0 0.0
light-switch-app-ota-shell-factory-data tl3218x_retention FLASH 702184 702184 0 0.0
RAM 37664 37664 0 0.0
lighting-app-ota-factory-data tlsr9118bdk40d FLASH 601750 601750 0 0.0
RAM 137360 137360 0 0.0
lighting-app-ota-rpc-factory-data-4mb tlsr9518adk80d FLASH 789042 789042 0 0.0
RAM 96388 96388 0 0.0
tizen all-clusters-app arm unknown 5144 5144 0 0.0
FLASH 1770780 1770780 0 0.0
RAM 94152 94152 0 0.0
chip-tool-ubsan arm unknown 11492 11492 0 0.0
FLASH 18984886 18984886 0 0.0
RAM 8306668 8306668 0 0.0

@@ -377,7 +376,7 @@ bool LockManager::GetUser(chip::EndpointId endpointId, uint16_t userIndex, Ember
uint16_t credentialSize = static_cast<uint16_t>(sizeof(CredentialStruct) * userInStorage.currentCredentialCount);
Copy link
Contributor

Choose a reason for hiding this comment

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

If userInStorage.currentCredentialCount is larger than kMaxCredentialsPerUser for some reason (e.g. firmware update changed the max value), you get a buffer overflow here. Of course in that situation you have other problems too, like not being able to load all the credentials.... But it's bad if some random bit-flip somewhere in storage leads to out of bounds writes in code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good call. Added a check for userInStorage.currentCredentialCount <= kMaxCredentialsPerUser to prevent a buffer overflow in this case

@mykrupp mykrupp force-pushed the silabs_lock_refactor branch from 9432b6e to 158d766 Compare March 5, 2025 21:18
Copy link

github-actions bot commented Mar 5, 2025

PR #37849: Size comparison from cf45d5d to 158d766

Full report (75 builds for bl602, bl702, bl702l, cc13x4_26x4, cc32xx, cyw30739, efr32, esp32, linux, nrfconnect, nxp, psoc6, qpg, stm32, telink, tizen)
platform target config section cf45d5d 158d766 change % change
bl602 lighting-app bl602+mfd+littlefs+rpc FLASH 1096882 1096882 0 0.0
RAM 94842 94842 0 0.0
bl702 lighting-app bl702+eth FLASH 651856 651856 0 0.0
RAM 33509 33509 0 0.0
bl702+wifi FLASH 829128 829128 0 0.0
RAM 22233 22233 0 0.0
bl706+mfd+rpc+littlefs FLASH 1061524 1061524 0 0.0
RAM 32157 32157 0 0.0
bl702l contact-sensor-app bl702l+mfd+littlefs FLASH 892368 892368 0 0.0
RAM 26896 26896 0 0.0
lighting-app bl702l+mfd+littlefs FLASH 975264 975264 0 0.0
RAM 24644 24644 0 0.0
cc13x4_26x4 lighting-app LP_EM_CC1354P10_6 FLASH 817224 817224 0 0.0
RAM 120272 120272 0 0.0
lock-ftd LP_EM_CC1354P10_6 FLASH 826144 826144 0 0.0
RAM 125368 125368 0 0.0
pump-app LP_EM_CC1354P10_6 FLASH 773028 773028 0 0.0
RAM 113740 113740 0 0.0
pump-controller-app LP_EM_CC1354P10_6 FLASH 757288 757288 0 0.0
RAM 113948 113948 0 0.0
cc32xx air-purifier CC3235SF_LAUNCHXL FLASH 540790 540790 0 0.0
RAM 205128 205128 0 0.0
lock CC3235SF_LAUNCHXL FLASH 574890 574890 0 0.0
RAM 205376 205376 0 0.0
cyw30739 light CYW30739B2-P5-EVK-01 unknown 2040 2040 0 0.0
FLASH 658925 658925 0 0.0
RAM 75412 75412 0 0.0
CYW30739B2-P5-EVK-02 unknown 2040 2040 0 0.0
FLASH 678785 678785 0 0.0
RAM 78052 78052 0 0.0
CYW30739B2-P5-EVK-03 unknown 2040 2040 0 0.0
FLASH 678785 678785 0 0.0
RAM 78052 78052 0 0.0
CYW930739M2EVB-02 unknown 2040 2040 0 0.0
FLASH 635709 635709 0 0.0
RAM 70480 70480 0 0.0
light-switch CYW30739B2-P5-EVK-01 unknown 2040 2040 0 0.0
FLASH 619165 619165 0 0.0
RAM 71652 71652 0 0.0
CYW30739B2-P5-EVK-02 unknown 2040 2040 0 0.0
FLASH 638801 638801 0 0.0
RAM 74196 74196 0 0.0
CYW30739B2-P5-EVK-03 unknown 2040 2040 0 0.0
FLASH 638801 638801 0 0.0
RAM 74196 74196 0 0.0
lock CYW30739B2-P5-EVK-01 unknown 2040 2040 0 0.0
FLASH 638653 638653 0 0.0
RAM 74660 74660 0 0.0
CYW30739B2-P5-EVK-02 unknown 2040 2040 0 0.0
FLASH 658377 658377 0 0.0
RAM 77204 77204 0 0.0
CYW30739B2-P5-EVK-03 unknown 2040 2040 0 0.0
FLASH 658377 658377 0 0.0
RAM 77204 77204 0 0.0
thermostat CYW30739B2-P5-EVK-01 unknown 2040 2040 0 0.0
FLASH 614993 614993 0 0.0
RAM 68748 68748 0 0.0
CYW30739B2-P5-EVK-02 unknown 2040 2040 0 0.0
FLASH 634853 634853 0 0.0
RAM 71388 71388 0 0.0
CYW30739B2-P5-EVK-03 unknown 2040 2040 0 0.0
FLASH 634853 634853 0 0.0
RAM 71388 71388 0 0.0
efr32 lock-app BRD4187C FLASH 939760 940392 632 0.1
RAM 159920 132112 -27808 -17.4
BRD4338a FLASH 733376 734120 744 0.1
RAM 234840 207032 -27808 -11.8
window-app BRD4187C FLASH 1032264 1032264 0 0.0
RAM 128024 128024 0 0.0
esp32 all-clusters-app c3devkit DRAM 98704 98704 0 0.0
FLASH 1593236 1593236 0 0.0
IRAM 83820 83820 0 0.0
m5stack DRAM 117492 117492 0 0.0
FLASH 1559946 1559946 0 0.0
IRAM 117039 117039 0 0.0
linux air-purifier-app debug unknown 4752 4752 0 0.0
FLASH 2653835 2653835 0 0.0
RAM 112304 112304 0 0.0
all-clusters-app debug unknown 5560 5560 0 0.0
FLASH 5975316 5975316 0 0.0
RAM 515608 515608 0 0.0
all-clusters-minimal-app debug unknown 5456 5456 0 0.0
FLASH 5311924 5311924 0 0.0
RAM 222648 222648 0 0.0
bridge-app debug unknown 5472 5472 0 0.0
FLASH 4625648 4625648 0 0.0
RAM 200984 200984 0 0.0
camera-app debug unknown 5456 5456 0 0.0
FLASH 4675742 4675742 0 0.0
RAM 195792 195792 0 0.0
camera-controller debug unknown 5776 5776 0 0.0
FLASH 11279431 11279431 0 0.0
RAM 594048 594048 0 0.0
chip-tool debug unknown 6112 6112 0 0.0
FLASH 13292411 13292411 0 0.0
RAM 602944 602944 0 0.0
chip-tool-ipv6only arm64 unknown 21992 21992 0 0.0
FLASH 11488152 11488152 0 0.0
RAM 655536 655536 0 0.0
fabric-admin debug unknown 5800 5800 0 0.0
FLASH 11573059 11573059 0 0.0
RAM 602728 602728 0 0.0
fabric-bridge-app debug unknown 4720 4720 0 0.0
FLASH 4456604 4456604 0 0.0
RAM 188168 188168 0 0.0
fabric-sync debug unknown 4976 4976 0 0.0
FLASH 5573557 5573557 0 0.0
RAM 470864 470864 0 0.0
lighting-app debug+rpc+ui unknown 6192 6192 0 0.0
FLASH 5519953 5519953 0 0.0
RAM 205168 205168 0 0.0
lock-app debug unknown 5424 5424 0 0.0
FLASH 4692424 4692424 0 0.0
RAM 192344 192344 0 0.0
ota-provider-app debug unknown 4760 4760 0 0.0
FLASH 4314602 4314602 0 0.0
RAM 181000 181000 0 0.0
ota-requestor-app debug unknown 4712 4712 0 0.0
FLASH 4444922 4444922 0 0.0
RAM 185488 185488 0 0.0
shell debug unknown 4240 4240 0 0.0
FLASH 2952188 2952188 0 0.0
RAM 145424 145424 0 0.0
thermostat-no-ble arm64 unknown 9448 9448 0 0.0
FLASH 4141672 4141672 0 0.0
RAM 229808 229808 0 0.0
tv-app debug unknown 5752 5752 0 0.0
FLASH 5912405 5912405 0 0.0
RAM 594296 594296 0 0.0
tv-casting-app debug unknown 5320 5320 0 0.0
FLASH 11463053 11463053 0 0.0
RAM 718208 718208 0 0.0
nrfconnect all-clusters-app nrf52840dk_nrf52840 FLASH 914784 914784 0 0.0
RAM 142881 142881 0 0.0
nrf7002dk_nrf5340_cpuapp FLASH 907992 907992 0 0.0
RAM 125221 125221 0 0.0
all-clusters-minimal-app nrf52840dk_nrf52840 FLASH 851604 851604 0 0.0
RAM 141243 141243 0 0.0
nxp contact k32w0+release FLASH 587440 587440 0 0.0
RAM 70980 70980 0 0.0
mcxw71+release FLASH 601264 601264 0 0.0
RAM 63096 63096 0 0.0
light k32w0+release FLASH 613172 613172 0 0.0
RAM 70268 70268 0 0.0
k32w1+release FLASH 685888 685888 0 0.0
RAM 48584 48584 0 0.0
lock mcxw71+release FLASH 750104 750104 0 0.0
RAM 67500 67500 0 0.0
psoc6 all-clusters cy8ckit_062s2_43012 FLASH 1660284 1660284 0 0.0
RAM 212320 212320 0 0.0
all-clusters-minimal cy8ckit_062s2_43012 FLASH 1564588 1564588 0 0.0
RAM 208536 208536 0 0.0
light cy8ckit_062s2_43012 FLASH 1441324 1441324 0 0.0
RAM 197296 197296 0 0.0
lock cy8ckit_062s2_43012 FLASH 1470212 1470212 0 0.0
RAM 224960 224960 0 0.0
qpg lighting-app qpg6105+debug FLASH 663852 663852 0 0.0
RAM 105156 105156 0 0.0
lock-app qpg6105+debug FLASH 622312 622312 0 0.0
RAM 99768 99768 0 0.0
stm32 light STM32WB5MM-DK FLASH 459928 459928 0 0.0
RAM 141472 141472 0 0.0
telink bridge-app tl7218x FLASH 664564 664564 0 0.0
RAM 90712 90712 0 0.0
contact-sensor-app tlsr9528a_retention FLASH 622146 622146 0 0.0
RAM 31488 31488 0 0.0
light-app-ota-compress-lzma-shell-factory-data tl3218x FLASH 760920 760920 0 0.0
RAM 40420 40420 0 0.0
light-app-ota-shell-factory-data tl7218x FLASH 754028 754028 0 0.0
RAM 97540 97540 0 0.0
light-switch-app-ota-compress-lzma-factory-data tl7218x_retention FLASH 681078 681078 0 0.0
RAM 52192 52192 0 0.0
light-switch-app-ota-compress-lzma-shell-factory-data tlsr9528a FLASH 709636 709636 0 0.0
RAM 73400 73400 0 0.0
light-switch-app-ota-shell-factory-data tl3218x_retention FLASH 702186 702186 0 0.0
RAM 37664 37664 0 0.0
lighting-app-ota-factory-data tlsr9118bdk40d FLASH 601756 601756 0 0.0
RAM 138640 138640 0 0.0
lighting-app-ota-rpc-factory-data-4mb tlsr9518adk80d FLASH 789044 789044 0 0.0
RAM 96388 96388 0 0.0
tizen all-clusters-app arm unknown 5152 5152 0 0.0
FLASH 1780052 1780052 0 0.0
RAM 94152 94152 0 0.0
chip-tool-ubsan arm unknown 11500 11500 0 0.0
FLASH 18967150 18967150 0 0.0
RAM 8299328 8299328 0 0.0

WeekDayScheduleInfo weekDayScheduleInStorage;
YearDayScheduleInfo yearDayScheduleInStorage;
HolidayScheduleInfo holidayScheduleInStorage;
CredentialStruct mCredentials[kMaxCredentialsPerUser];
Copy link
Contributor

Choose a reason for hiding this comment

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

What is the intended purpose of having these variables as members of the class rather than having them as local variables in the members that uses them?
As it is now, the memory for all of these will always be allocated with the static LockManager, whereas if they would be declared in the members where they are used, the memory would only be allocated on the stack when they are needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will move the schedule storage to local variables in schedule functions, but the user/credential items return spans which need consistent storage somewhere.

This could be revisited if we decide to change EmberAfPluginDoorLockCredentialInfo and EmberAfPluginDoorLockUserInfo to have their own storage

Copy link
Contributor

Choose a reason for hiding this comment

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

We can, but I think this will force us to rely on shared pointers in the meantime to ensure the caller of methods where we pass a pointer to our buffers will be done with the buffers when the next call happens.

This runs the risk of being equally tedious if we want to ensure the memory access is safe.


user.userStatus = userInDb.userStatus;
if (UserStatusEnum::kAvailable == user.userStatus)
error = chip::Server::GetInstance().GetPersistentStorage().SyncGetKeyValue(userKey.KeyName(), &userInStorage, size);
Copy link
Contributor

Choose a reason for hiding this comment

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

This is an example, here we only need userInStorage and mCredential in this method, yet all the schedules will nonetheless take memory during and after this method is called.

Copy link
Contributor

Choose a reason for hiding this comment

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

The outparam takes pointers into the delegate's storage, so... Not many options here.

Copy link
Contributor

Choose a reason for hiding this comment

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

We were considering refactoring the doorlock server to make the caller own the memory buffer and copy into it rather than force the LockManager to own the buffer here.

Another option would be shared pointers that would be freed once the caller's pointer goes out of scope. Not ideal either.

// Else if KVS read was successful
else if (error == CHIP_NO_ERROR)
{
user.credentials = chip::Span<const CredentialStruct>{ mCredentials, userInStorage.currentCredentialCount };
Copy link
Contributor

Choose a reason for hiding this comment

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

Given the struct EmberAfPluginDoorLockUserInfo's line:

chip::Span<const CredentialStruct> credentials; /**< Credentials that are associated with user (without data).*/

I can see that we DO want mCredentials as a static member of the LockManager, however, we should consider protection for concurrent access.

@@ -410,7 +425,7 @@ bool LockManager::SetUser(chip::EndpointId endpointId, uint16_t userIndex, chip:

VerifyOrReturnValue(IsValidUserIndex(userIndex), false);

auto & userInStorage = mLockUsers[userIndex];
VerifyOrReturnValue(kInvalidEndpointId != endpointId, false);
Copy link
Contributor

Choose a reason for hiding this comment

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

On the subject of endpoint verification, since we want to have a single Manager for all endpoint, there should be a check that another operation on another endpoint is not ongoing since mCredential is allocated at the Manager level.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For now I've added checks to each function for kInvalidEndpointId. I will add support for InitEndpoint for an arbitrary endpoint in a subsequent PR


chip::StorageKeyName key = LockCredentialEndpoint(credentialIndex, credentialType, endpointId);

error = chip::Server::GetInstance().GetPersistentStorage().SyncGetKeyValue(key.KeyName(), &credentialInStorage, size);
Copy link
Contributor

Choose a reason for hiding this comment

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

same comment as userInStorage, credentialInStorage doesn't appear to need to be a member of the LockManager as all its value are copied into the & credential reference passed to this method.


if (DlCredentialStatus::kAvailable == credential.status)
{
ChipLogDetail(Zcl, "Found unoccupied credential ");
return true;
}
credential.credentialType = credentialInStorage.credentialType;
credential.credentialData = credentialInStorage.credentialData;
credential.credentialData = chip::ByteSpan{ credentialInStorage.credentialData, credentialInStorage.credentialDataSize };
Copy link
Contributor

Choose a reason for hiding this comment

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

And here again we commit to keep uint8_t credentialData[kMaxCredentialSize]; Because EmberAfPluginDoorLockCredentialInfo is designed in a way that forces it.

I suggest we at least move this array to be static like mCredentials so we can only keep these two arrays to be static. Than being said, I am tempted to say that instead we should just refactor EmberAfPluginDoorLockCredentialInfo and EmberAfPluginDoorLockUserInfo so they they hold their own memories...

Otherwise we need to figure out a way to know when credential becomes out of scope.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Any function callback that returns a span needs memory allocated somewhere. We could ask in slack if people are ok with refactoring EmberAfPluginDoorLockCredentialInfo and EmberAfPluginDoorLockUserInfo to hold their own storage

credentialInStorage.credentialType = credentialType;
credentialInStorage.createdBy = creator;
credentialInStorage.lastModifiedBy = modifier;
memcpy(credentialInStorage.credentialData, credentialData.data(), credentialInStorage.credentialDataSize);
Copy link
Contributor

Choose a reason for hiding this comment

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

This is one of the reasons why I am not a fan having this as a static structure... the last added credentials will remain in RAM, which is potentially a security risk. In this case I suggest to clear the credentialInStorage.credentialData once we have saved it in flash but we need a better solution that ensures that we do not keep this data allocated once its owner is done using it.

@mykrupp mykrupp force-pushed the silabs_lock_refactor branch from 3899de2 to 18ad77e Compare March 6, 2025 19:39
Copy link

github-actions bot commented Mar 6, 2025

PR #37849: Size comparison from cfcfef3 to 18ad77e

Full report (14 builds for cc13x4_26x4, cc32xx, nrfconnect, qpg, stm32, tizen)
platform target config section cfcfef3 18ad77e change % change
cc13x4_26x4 lighting-app LP_EM_CC1354P10_6 FLASH 817224 817224 0 0.0
RAM 120272 120272 0 0.0
lock-ftd LP_EM_CC1354P10_6 FLASH 826144 826144 0 0.0
RAM 125368 125368 0 0.0
pump-app LP_EM_CC1354P10_6 FLASH 773028 773028 0 0.0
RAM 113740 113740 0 0.0
pump-controller-app LP_EM_CC1354P10_6 FLASH 757288 757288 0 0.0
RAM 113948 113948 0 0.0
cc32xx air-purifier CC3235SF_LAUNCHXL FLASH 540790 540790 0 0.0
RAM 205128 205128 0 0.0
lock CC3235SF_LAUNCHXL FLASH 574890 574890 0 0.0
RAM 205376 205376 0 0.0
nrfconnect all-clusters-app nrf52840dk_nrf52840 FLASH 914784 914784 0 0.0
RAM 142881 142881 0 0.0
nrf7002dk_nrf5340_cpuapp FLASH 907992 907992 0 0.0
RAM 125221 125221 0 0.0
all-clusters-minimal-app nrf52840dk_nrf52840 FLASH 851604 851604 0 0.0
RAM 141243 141243 0 0.0
qpg lighting-app qpg6105+debug FLASH 663852 663852 0 0.0
RAM 105156 105156 0 0.0
lock-app qpg6105+debug FLASH 622312 622312 0 0.0
RAM 99768 99768 0 0.0
stm32 light STM32WB5MM-DK FLASH 459928 459928 0 0.0
RAM 141472 141472 0 0.0
tizen all-clusters-app arm unknown 5152 5152 0 0.0
FLASH 1780068 1780068 0 0.0
RAM 94152 94152 0 0.0
chip-tool-ubsan arm unknown 11500 11500 0 0.0
FLASH 18967150 18967150 0 0.0
RAM 8299328 8299328 0 0.0

…eInStorage from class members to local function variables
@mykrupp mykrupp marked this pull request as ready for review March 6, 2025 20:23
@mykrupp mykrupp requested a review from a team as a code owner March 6, 2025 20:23
Copy link

github-actions bot commented Mar 6, 2025

PR #37849: Size comparison from cfcfef3 to 7bd0669

Full report (75 builds for bl602, bl702, bl702l, cc13x4_26x4, cc32xx, cyw30739, efr32, esp32, linux, nrfconnect, nxp, psoc6, qpg, stm32, telink, tizen)
platform target config section cfcfef3 7bd0669 change % change
bl602 lighting-app bl602+mfd+littlefs+rpc FLASH 1096882 1096882 0 0.0
RAM 94842 94842 0 0.0
bl702 lighting-app bl702+eth FLASH 651856 651856 0 0.0
RAM 33509 33509 0 0.0
bl702+wifi FLASH 829128 829128 0 0.0
RAM 22233 22233 0 0.0
bl706+mfd+rpc+littlefs FLASH 1061524 1061524 0 0.0
RAM 32157 32157 0 0.0
bl702l contact-sensor-app bl702l+mfd+littlefs FLASH 892368 892368 0 0.0
RAM 26896 26896 0 0.0
lighting-app bl702l+mfd+littlefs FLASH 975264 975264 0 0.0
RAM 24644 24644 0 0.0
cc13x4_26x4 lighting-app LP_EM_CC1354P10_6 FLASH 817224 817224 0 0.0
RAM 120272 120272 0 0.0
lock-ftd LP_EM_CC1354P10_6 FLASH 826144 826144 0 0.0
RAM 125368 125368 0 0.0
pump-app LP_EM_CC1354P10_6 FLASH 773028 773028 0 0.0
RAM 113740 113740 0 0.0
pump-controller-app LP_EM_CC1354P10_6 FLASH 757288 757288 0 0.0
RAM 113948 113948 0 0.0
cc32xx air-purifier CC3235SF_LAUNCHXL FLASH 540790 540790 0 0.0
RAM 205128 205128 0 0.0
lock CC3235SF_LAUNCHXL FLASH 574890 574890 0 0.0
RAM 205376 205376 0 0.0
cyw30739 light CYW30739B2-P5-EVK-01 unknown 2040 2040 0 0.0
FLASH 658925 658925 0 0.0
RAM 75412 75412 0 0.0
CYW30739B2-P5-EVK-02 unknown 2040 2040 0 0.0
FLASH 678785 678785 0 0.0
RAM 78052 78052 0 0.0
CYW30739B2-P5-EVK-03 unknown 2040 2040 0 0.0
FLASH 678785 678785 0 0.0
RAM 78052 78052 0 0.0
CYW930739M2EVB-02 unknown 2040 2040 0 0.0
FLASH 635709 635709 0 0.0
RAM 70480 70480 0 0.0
light-switch CYW30739B2-P5-EVK-01 unknown 2040 2040 0 0.0
FLASH 619165 619165 0 0.0
RAM 71652 71652 0 0.0
CYW30739B2-P5-EVK-02 unknown 2040 2040 0 0.0
FLASH 638801 638801 0 0.0
RAM 74196 74196 0 0.0
CYW30739B2-P5-EVK-03 unknown 2040 2040 0 0.0
FLASH 638801 638801 0 0.0
RAM 74196 74196 0 0.0
lock CYW30739B2-P5-EVK-01 unknown 2040 2040 0 0.0
FLASH 638653 638653 0 0.0
RAM 74660 74660 0 0.0
CYW30739B2-P5-EVK-02 unknown 2040 2040 0 0.0
FLASH 658377 658377 0 0.0
RAM 77204 77204 0 0.0
CYW30739B2-P5-EVK-03 unknown 2040 2040 0 0.0
FLASH 658377 658377 0 0.0
RAM 77204 77204 0 0.0
thermostat CYW30739B2-P5-EVK-01 unknown 2040 2040 0 0.0
FLASH 614993 614993 0 0.0
RAM 68748 68748 0 0.0
CYW30739B2-P5-EVK-02 unknown 2040 2040 0 0.0
FLASH 634853 634853 0 0.0
RAM 71388 71388 0 0.0
CYW30739B2-P5-EVK-03 unknown 2040 2040 0 0.0
FLASH 634853 634853 0 0.0
RAM 71388 71388 0 0.0
efr32 lock-app BRD4187C FLASH 939760 940456 696 0.1
RAM 159920 132080 -27840 -17.4
BRD4338a FLASH 733376 734248 872 0.1
RAM 234840 207000 -27840 -11.9
window-app BRD4187C FLASH 1032264 1032264 0 0.0
RAM 128024 128024 0 0.0
esp32 all-clusters-app c3devkit DRAM 98704 98704 0 0.0
FLASH 1593152 1593152 0 0.0
IRAM 83820 83820 0 0.0
m5stack DRAM 117492 117492 0 0.0
FLASH 1559858 1559858 0 0.0
IRAM 117039 117039 0 0.0
linux air-purifier-app debug unknown 4752 4752 0 0.0
FLASH 2653845 2653845 0 0.0
RAM 112304 112304 0 0.0
all-clusters-app debug unknown 5560 5560 0 0.0
FLASH 5975326 5975326 0 0.0
RAM 515608 515608 0 0.0
all-clusters-minimal-app debug unknown 5456 5456 0 0.0
FLASH 5311934 5311934 0 0.0
RAM 222648 222648 0 0.0
bridge-app debug unknown 5472 5472 0 0.0
FLASH 4625658 4625658 0 0.0
RAM 200984 200984 0 0.0
camera-app debug unknown 5456 5456 0 0.0
FLASH 4675752 4675752 0 0.0
RAM 195792 195792 0 0.0
camera-controller debug unknown 5776 5776 0 0.0
FLASH 11279431 11279431 0 0.0
RAM 594048 594048 0 0.0
chip-tool debug unknown 6112 6112 0 0.0
FLASH 13292411 13292411 0 0.0
RAM 602944 602944 0 0.0
chip-tool-ipv6only arm64 unknown 21992 21992 0 0.0
FLASH 11488152 11488152 0 0.0
RAM 655536 655536 0 0.0
fabric-admin debug unknown 5800 5800 0 0.0
FLASH 11573059 11573059 0 0.0
RAM 602728 602728 0 0.0
fabric-bridge-app debug unknown 4720 4720 0 0.0
FLASH 4456614 4456614 0 0.0
RAM 188168 188168 0 0.0
fabric-sync debug unknown 4976 4976 0 0.0
FLASH 5573573 5573573 0 0.0
RAM 470864 470864 0 0.0
lighting-app debug+rpc+ui unknown 6192 6192 0 0.0
FLASH 5519953 5519953 0 0.0
RAM 205168 205168 0 0.0
lock-app debug unknown 5424 5424 0 0.0
FLASH 4692434 4692434 0 0.0
RAM 192344 192344 0 0.0
ota-provider-app debug unknown 4760 4760 0 0.0
FLASH 4314612 4314612 0 0.0
RAM 181000 181000 0 0.0
ota-requestor-app debug unknown 4712 4712 0 0.0
FLASH 4444932 4444932 0 0.0
RAM 185488 185488 0 0.0
shell debug unknown 4240 4240 0 0.0
FLASH 2952204 2952204 0 0.0
RAM 145424 145424 0 0.0
thermostat-no-ble arm64 unknown 9448 9448 0 0.0
FLASH 4141688 4141688 0 0.0
RAM 229808 229808 0 0.0
tv-app debug unknown 5752 5752 0 0.0
FLASH 5912421 5912421 0 0.0
RAM 594296 594296 0 0.0
tv-casting-app debug unknown 5320 5320 0 0.0
FLASH 11463069 11463069 0 0.0
RAM 718208 718208 0 0.0
nrfconnect all-clusters-app nrf52840dk_nrf52840 FLASH 914784 914784 0 0.0
RAM 142881 142881 0 0.0
nrf7002dk_nrf5340_cpuapp FLASH 907992 907992 0 0.0
RAM 125221 125221 0 0.0
all-clusters-minimal-app nrf52840dk_nrf52840 FLASH 851604 851604 0 0.0
RAM 141243 141243 0 0.0
nxp contact k32w0+release FLASH 587440 587440 0 0.0
RAM 70980 70980 0 0.0
mcxw71+release FLASH 602640 602640 0 0.0
RAM 63120 63120 0 0.0
light k32w0+release FLASH 613172 613172 0 0.0
RAM 70268 70268 0 0.0
k32w1+release FLASH 686656 686656 0 0.0
RAM 72032 72032 0 0.0
lock mcxw71+release FLASH 751416 751416 0 0.0
RAM 67532 67532 0 0.0
psoc6 all-clusters cy8ckit_062s2_43012 FLASH 1660300 1660300 0 0.0
RAM 212320 212320 0 0.0
all-clusters-minimal cy8ckit_062s2_43012 FLASH 1564588 1564588 0 0.0
RAM 208536 208536 0 0.0
light cy8ckit_062s2_43012 FLASH 1441340 1441340 0 0.0
RAM 197296 197296 0 0.0
lock cy8ckit_062s2_43012 FLASH 1470212 1470212 0 0.0
RAM 224960 224960 0 0.0
qpg lighting-app qpg6105+debug FLASH 663852 663852 0 0.0
RAM 105156 105156 0 0.0
lock-app qpg6105+debug FLASH 622312 622312 0 0.0
RAM 99768 99768 0 0.0
stm32 light STM32WB5MM-DK FLASH 459928 459928 0 0.0
RAM 141472 141472 0 0.0
telink bridge-app tl7218x FLASH 664564 664564 0 0.0
RAM 90712 90712 0 0.0
contact-sensor-app tlsr9528a_retention FLASH 622146 622146 0 0.0
RAM 31488 31488 0 0.0
light-app-ota-compress-lzma-shell-factory-data tl3218x FLASH 760920 760920 0 0.0
RAM 40420 40420 0 0.0
light-app-ota-shell-factory-data tl7218x FLASH 754028 754028 0 0.0
RAM 97540 97540 0 0.0
light-switch-app-ota-compress-lzma-factory-data tl7218x_retention FLASH 681078 681078 0 0.0
RAM 52192 52192 0 0.0
light-switch-app-ota-compress-lzma-shell-factory-data tlsr9528a FLASH 709636 709636 0 0.0
RAM 73400 73400 0 0.0
light-switch-app-ota-shell-factory-data tl3218x_retention FLASH 702186 702186 0 0.0
RAM 37664 37664 0 0.0
lighting-app-ota-factory-data tlsr9118bdk40d FLASH 601756 601756 0 0.0
RAM 138640 138640 0 0.0
lighting-app-ota-rpc-factory-data-4mb tlsr9518adk80d FLASH 789044 789044 0 0.0
RAM 96388 96388 0 0.0
tizen all-clusters-app arm unknown 5152 5152 0 0.0
FLASH 1780068 1780068 0 0.0
RAM 94152 94152 0 0.0
chip-tool-ubsan arm unknown 11500 11500 0 0.0
FLASH 18967150 18967150 0 0.0
RAM 8299328 8299328 0 0.0

Copy link
Contributor

@bzbarsky-apple bzbarsky-apple left a comment

Choose a reason for hiding this comment

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

I didn't finish reading all the changes, but there are already too many security bugs in the parts I read so far....


user.userStatus = userInDb.userStatus;
if (UserStatusEnum::kAvailable == user.userStatus)
error = chip::Server::GetInstance().GetPersistentStorage().SyncGetKeyValue(userKey.KeyName(), &userInStorage, size);
Copy link
Contributor

Choose a reason for hiding this comment

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

The outparam takes pointers into the delegate's storage, so... Not many options here.

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.

3 participants