You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
DTM-5257: Keep exported key alongside _raw.key for TA provisioning - #88
When TA provisioning (SoC provisioning) was enabled, SecKey_Provision stored SoC vendor keys only as raw bytes in .key and returned early. Old builds could no longer load that file (rollback broke), and keys provisioned by old builds were in exported format, so TA reads failed after an upgrade until the device was re-provisioned.
Store both formats instead:
.key (+ .keyinfo/.verify) keeps the exported format, so builds without TA provisioning can still load the key after a rollback.
_raw.key holds the raw data used by the TA provisioning readers.
The exported key is written first and _raw.key last (atomically, via a temporary file), so _raw.key only exists when both copies were stored successfully. No soc_prov_api change is needed: upgrading to a TA-provisioning build requires a factory reset and re-download from FKPS, which writes both files. A raw write failure is now returned to the caller, and a rejected provision leaves the existing files untouched.
SecKey_Provision: no early return for SoC keys; write _raw.key after normal provisioning succeeds.
SecKey_Delete: also remove _raw.key.
store_raw_data and all TA readers (Netflix KDE/KDH/KDW, Apple MFi, PlayReady, Widevine) use the new SEC_KEY_RAW_FILENAME_PATTERN.
When TA provisioning (SoC provisioning) was enabled, SecKey_Provision
stored SoC vendor keys only as raw bytes in <id>.key and returned early.
Old builds could no longer load that file (rollback broke), and keys
provisioned by old builds were in exported format, so TA reads failed
after an upgrade until the device was re-provisioned.
Store both formats instead:
- <id>.key (+ .keyinfo/.verify) keeps the exported format, so builds
without TA provisioning can still load the key after a rollback.
- <id>_raw.key holds the raw data used by the TA provisioning readers.
The exported key is written first and _raw.key last, so _raw.key only
exists when both copies are good and can be used by soc_prov_api as the
"ready for TA provisioning" marker. A raw write failure is now returned
to the caller, and a rejected provision leaves the existing files
untouched.
- SecKey_Provision: no early return for SoC keys; write _raw.key after
normal provisioning succeeds.
- SecKey_Delete: also remove <id>_raw.key.
- store_raw_data and all TA readers (Netflix KDE/KDH/KDW, Apple MFi,
PlayReady, Widevine) use the new SEC_KEY_RAW_FILENAME_PATTERN.
- Add testKeyProvisionSocKeepsExportedAndRaw.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The reason will be displayed to describe this comment to others. Learn more.
🟡 Changes recommended
Raw-key deletion can report success while leaving credentials behind, and failed direct writes can leave partial credential files that falsely signal provisioning readiness.
You are seeing this message because GitHub Code Scanning has recently been set up for this repository, or this pull request contains the workflow file for the Code Scanning tool.
What Enabling Code Scanning Means:
The 'Security' tab will display more code scanning analysis results (e.g., for the default branch).
Depending on your configuration and choice of analysis tool, future pull requests will be annotated with code scanning analysis results.
You will be able to see the analysis results for the pull request's branch on this overview once the scans have completed and the checks have passed.
For more information about GitHub Code Scanning, check out the documentation.
SecUtils_WriteFile does not delete the file when a write fails midway
(for example when /opt is full), so a failed SecKey_Provision could
leave a truncated <id>_raw.key behind. The presence of _raw.key is the
"fully provisioned for TA" marker, so remove it when store_raw_data
fails, the same way store_key_data cleans up its files on failure.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The reason will be displayed to describe this comment to others. Learn more.
🔵 Needs a closer look
Critical issues remain: non-atomic _raw.key writes can expose incomplete provisioning as ready, and unchecked raw-file deletion can report success while leaving the credential and readiness marker behind.
Avoid persistent raw markers for RAM-only provisioning
src/sec_adapter_key.c:637
For SEC_STORAGELOC_RAM and SEC_STORAGELOC_RAM_SOFT_WRAPPED, store_key_data removes the existing disk key and keeps the exported copy only in RAM, but this call still writes a persistent _raw.key. Provisioning can therefore succeed with a TA-ready marker but no exported <id>.key for rollback. Restrict raw-marker publication to successful file-backed provisioning so RAM provisioning stays RAM-only.
Addresses the Copilot review comments on PR rdkcentral#88.
store_raw_data: the presence of <id>_raw.key tells soc_prov_api the key is
ready for TA provisioning, so it must never exist partially written. A crash
or power loss during the write (the upgrade boot is exactly when this runs)
previously left a truncated _raw.key that looked ready. The raw key is now
written and verified as <id>_raw.key.tmp, renamed atomically to
<id>_raw.key, and the directory is fsynced. On any failure the temporary file
is removed and no _raw.key is published.
SecKey_Delete: removal of _raw.key is now counted in keys_found and
keys_deleted. A raw key that cannot be removed returns
SEC_RESULT_ITEM_NON_REMOVABLE instead of SUCCESS, and deleting a leftover raw
key with no exported key returns SUCCESS instead of NO_SUCH_ITEM. A leftover
_raw.key.tmp from an interrupted write is also removed.
testKeyProvisionSocKeepsExportedAndRaw checks that no temporary file is left
after provisioning and that SecKey_Delete removes a leftover _raw.key and
_raw.key.tmp and reports success.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The reason will be displayed to describe this comment to others. Learn more.
🟡 Changes recommended
Critical key-lifecycle flaws can leave readiness markers without valid exported keys after power loss or RAM provisioning, while temporary-file cleanup failures may be reported as success.
Avoid publishing raw-key markers without an exported key
src/sec_adapter_key.c:638
For SEC_STORAGELOC_RAM and SEC_STORAGELOC_RAM_SOFT_WRAPPED, normal provisioning deletes any existing disk copy and stores the exported key only in RAM, but store_raw_data() still publishes _raw.key on disk. A successful call therefore leaves a readiness marker without the exported .key required for rollback, and processor release deletes the raw credential too. Either persist both copies for SoC provisioning or reject unsupported storage locations before modifying an existing key.
Invalidate the readiness marker before securely erasing key copies
src/sec_adapter_key.c:1212
During deletion or reprovisioning, the exported key is erased before the raw readiness marker, and SecUtils_RmFile() zeroes and fsyncs the raw file before unlinking it. A power loss in this interval can leave _raw.key present with zeroed data or no exported counterpart, falsely signaling that provisioning is complete. Atomically rename the marker to a non-marker path and fsync the directory before modifying either copy, then securely erase the renamed file; abort if marker invalidation fails.
Count a leftover _raw.key.tmp in the deletion result just like other key
files. This ensures deleting a temp-only key returns SUCCESS and a failed
temp-file removal is reported as ITEM_NON_REMOVABLE.
Add a regression test for deleting a temp-only raw key file.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The reason will be displayed to describe this comment to others. Learn more.
🔵 Needs a closer look
Human assessment is needed because critical crash-consistency and storage-location risks can leave a persistent readiness marker without a usable exported key, undermining rollback guarantees.
soc_prov_api is not changed by this fix; the _raw.key name matters because
the TA provisioning readers load it.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The reason will be displayed to describe this comment to others. Learn more.
🔵 Needs a closer look
Human assessment is needed because re-provisioning can erase key data before invalidating the readiness marker, allowing a power loss to leave corrupt key material appearing ready.
Include <memory> explicitly instead of relying on transitive includes, fixing the ARM GCC build with Widevine provisioning enabled.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The reason will be displayed to describe this comment to others. Learn more.
🔵 Needs a closer look
Human assessment is needed because a critical power-loss window during reprovisioning can leave a durable, zero-filled raw key whose presence falsely signals that both key copies are complete.
The reason will be displayed to describe this comment to others. Learn more.
Built and ran this locally against the tasecureapi reference TA with -DENABLE_SOC_PROVISION_WIDEVINE=ON -DENABLE_SVP: 1209/1209 pass, including testKeyProvisionSocKeepsExportedAndRaw. The approach (exported copy first, raw copy published atomically last) looks right to me. A few recommendations follow. With the two inline code suggestions below applied, the suite still passes 1209/1209.
CI doesn't exercise this change.ci.yaml only builds, and all ENABLE_SOC_PROVISION_* options are OFF, so the SoC path in SecKey_Provision/store_raw_data and the new test are compiled out. (That's also why CI didn't catch the ARM <memory> issue.) Could we add a CI build, ideally with a test run, using -DENABLE_SOC_PROVISION_WIDEVINE=ON?
Power-loss window on delete/re-provision. See the inline suggestion on SecKey_Delete.
NULL app_dir / RAM storage location. See the inline comments on store_raw_data and SecKey_Provision.
Upgrade from earlier TA-enabled builds. A device provisioned by a build that wrote raw bytes into <id>.key (with no .keyinfo) has no _raw.key after this change, so TA provisioning fails until it's re-provisioned. The description says that upgrade requires a factory reset. Can we confirm no such build reached the field? If one did, a one-time migration would cover it: for a SoC ID where <id>.key exists without .keyinfo, rename it to <id>_raw.key.
Nit on the description. "A rejected provision leaves the existing files untouched" only holds when process_key_container rejects the data. Once store_key_data runs, it calls SecKey_Delete first, so a later storage or raw-write failure doesn't preserve the previous files. Also, if only the raw write fails, SecKey_Provision returns failure while the new exported key stays stored. A retry fixes it, but it's worth stating.
The reason will be displayed to describe this comment to others. Learn more.
SecUtils_RmFile zero-fills the file (fsynced) and then unlinks it (the unlink isn't fsynced). If power is lost between those two steps, <id>_raw.key survives zero-filled, and its presence is what means both copies are good. This also runs on every re-provision, because store_key_data calls SecKey_Delete first.
Suggestion: retract the raw key atomically by renaming it to _raw.key.tmp and fsyncing the directory, then wipe the tmp file through the existing tmp cleanup. The deletion accounting stays the same, and testKeyProvisionSocKeepsExportedAndRaw passes unchanged (verified locally).
Ideally this block would also move above the <id>.key removal, so the raw key is retracted before the exported key is wiped. A power loss in between would then leave no raw key and a valid exported key, instead of a raw key next to a zeroed exported key. That part is outside the diff hunk, so it isn't in the suggestion.
The reason will be displayed to describe this comment to others. Learn more.
store_raw_data formats processorHandle->app_dir without a NULL check. With SEC_STORAGELOC_RAM, store_key_data succeeds without app_dir, so this can be reached with app_dir == NULL, and snprintf("%s", NULL) is undefined behavior.
Suggested change
chartmp_file_name[SEC_MAX_FILE_PATH_LEN];
intdir_fd=-1;
chartmp_file_name[SEC_MAX_FILE_PATH_LEN];
intdir_fd=-1;
if (processorHandle->app_dir==NULL) {
SEC_LOG_ERROR("Cannot write raw key because app_dir is NULL");
The reason will be displayed to describe this comment to others. Learn more.
location isn't honored for the raw copy. With SEC_STORAGELOC_RAM, the exported key goes to RAM, but store_raw_data ignores location and still writes <id>_raw.key to app_dir. After a reboot, _raw.key exists with no exported <id>.key, so the "both copies were stored" invariant in the comment above doesn't hold. Is RAM provisioning of SoC IDs expected? If not, consider limiting the raw copy to SEC_STORAGELOC_FILE/SEC_STORAGELOC_FILE_SOFT_WRAPPED, or rejecting RAM for SoC IDs. If it is expected, it's worth documenting.
- provisioning_ta: free provisioning structs with free*Provisioning
helpers so inner key/cert buffers are not leaked (mhabrat)
- TA readers: calloc structs and free partially built structs on
failure paths
- SecKey_Provision: log object id on store_raw_data failure (mhabrat);
reject non-FILE storage for SoC vendor-specific keys (riwoh)
- store_raw_data: guard against NULL app_dir (riwoh)
- SecKey_Delete: retract _raw.key first via rename + dir fsync before
removing <id>.key, to close the power-loss window (riwoh)
- test: SoC provision to RAM is rejected and raw file is untouched
- ci: add soc-provision-widevine job building with
ENABLE_SOC_PROVISION_WIDEVINE=ON (riwoh)
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The reason will be displayed to describe this comment to others. Learn more.
🔵 Needs a closer look
Human assessment is needed because crash-durability failures in provisioning and deletion can leave zero-filled raw key files that TA readers treat as completed provisioning.
GCC 11 on x86-64 warned in testKeyProvisionSocKeepsExportedAndRaw:
- -Wformat: SEC_OBJECTID_WV_KEY is an unsigned long long literal, but
SEC_OBJECTID_PATTERN expects SEC_OBJECTID (uint64_t, unsigned long).
- -Wformat-truncation: the .tmp name appended to a full-size buffer.
Pass the ID as SEC_OBJECTID and build both file names from the pattern.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The reason will be displayed to describe this comment to others. Learn more.
🔵 Needs a closer look
Human assessment is needed because critical crash-consistency risks in provisioning cleanup and key deletion can leave durable, zero-filled raw-key files that readers consume as provisioning data.
sa_process_command unpacked each command's arguments with several
va_arg() calls inside one function call. C leaves the evaluation order
of function arguments unspecified, and GCC on x86-64 evaluates them
right to left, so the arguments arrived reversed. The first visible
failure was sa_key_derive receiving the KDF algorithm (0) as rights:
"sa_key_derive: NULL rights" while SecProcessor_GetInstance derives
SEC_OBJECTID_STORE_AES_KEY, so the test binary failed at startup in
the soc-provision-widevine CI job (ubuntu-22.04, GCC 11).
Read each va_arg into a local variable in parameter order before the
call, for all 25 commands that take more than one argument. Types and
order of the va_arg reads are unchanged.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The reason will be displayed to describe this comment to others. Learn more.
🟡 Changes recommended
Critical crash-safety issues can leave durable zero-filled raw keys after failed publication or deletion, and store_key_data ignores deletion errors before replacing exported files.
This reverts commit fb04739.
Restore all original sa_process_command cases, including SA_KEY_DERIVE,
while the cause of the CI failure is investigated further.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The reason will be displayed to describe this comment to others. Learn more.
🟡 Changes recommended
Critical error paths can wipe raw keys without durably retracting their published filenames, allowing a crash to leave zero-filled credentials available to readers.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When TA provisioning (SoC provisioning) was enabled, SecKey_Provision stored SoC vendor keys only as raw bytes in .key and returned early. Old builds could no longer load that file (rollback broke), and keys provisioned by old builds were in exported format, so TA reads failed after an upgrade until the device was re-provisioned.
Store both formats instead:
The exported key is written first and _raw.key last (atomically, via a temporary file), so _raw.key only exists when both copies were stored successfully. No soc_prov_api change is needed: upgrading to a TA-provisioning build requires a factory reset and re-download from FKPS, which writes both files. A raw write failure is now returned to the caller, and a rejected provision leaves the existing files untouched.