Repository navigation
KCM: deep-copy cred_blob in kcm_cred_dup to avoid use-after-free - #9646
Merged
Merged
Conversation
pbrezina
requested changes
Nov 24, 2026
| * a shallow pointer copy would dangle and trigger a use-after-free | ||
| * when the per-connection credential cache is reused by a | ||
| * subsequent GET_CRED_BY_UUID request. */ | ||
| len = sss_iobuf_get_len(crd->cred_blob); |
Member
There was a problem hiding this comment.
I think you should use sss_iobuf_get_size();
struct sss_iobuf {
uint8_t *data; /* Start of the data buffer */
size_t dp; /* Data pointer */ <--- get_len()
size_t size; /* Current data buffer size */ <--- get_size()
size_t capacity; /* Maximum capacity */
bool secure; /* Securely erased before freeing it */
};
Member
There was a problem hiding this comment.
The cred_blob is filled with:
ret = sss_iobuf_read(op_ctx->input,
creds_len,
sss_iobuf_get_data(state->cred_blob),
NULL);
if (ret != EOK) {
DEBUG(SSSDBG_OP_FAILURE,
"Cannot unmarshall input cred blob [%d]: %s\n",
ret, sss_strerror(ret));
goto immediate;
}
which reads from input iobuf and writes directly to the cred_blob->data without updating dp. This is confusing as hell and should probably get fixed as well. But for the sake of this PR using get_size() is sufficient.
Contributor
Author
There was a problem hiding this comment.
Thanks for the review. Updated and force-pushed.
Switched sss_iobuf_get_len() to sss_iobuf_get_size().
pbrezina
approved these changes
Nov 25, 2026
The memory backend (ccdb_mem_getbyname_send) returns ccache copies built via kcm_cc_dup(), which in turn calls kcm_cred_dup() for each credential. kcm_cred_dup() performed a shallow pointer copy of cred_blob, so the duplicate credential kept referencing the original ccache talloc iobuf. When a client issues GET_CRED_UUID_LIST, the credentials are stolen into the per-connection hash table (conn_data->creds) via kcm_creds_to_table(). If the same client subsequently issues DESTROY, the original ccache and its iobuf objects are freed, leaving dangling cred_blob pointers in conn_data->creds. A following GET_CRED_BY_UUID request then dereferences the freed iobuf in kcm_op_get_cred_by_uuid_reply(), causing a use-after-free read. Replace the shallow copy with sss_iobuf_init_readonly(), which allocates an independent buffer and memcpy()s the credential data. This matches the secdb backend, which already creates independent iobuf copies via sss_iobuf_read_iobuf() in bin_to_creds(). Use sss_iobuf_get_size() rather than sss_iobuf_get_len() to obtain the blob length: cred_blob is created via sss_iobuf_init_empty() + sss_iobuf_get_data() + sss_iobuf_read() from the input buffer, which writes directly to cred_blob->data without advancing dp. Therefore sss_iobuf_get_len() (which returns dp) would always return 0, producing an empty deep copy. sss_iobuf_get_size() returns the actual buffer size, matching the consumer kcm_op_get_cred_by_uuid_reply() which already uses get_size(). The use-after-free is only reachable with the non-default memory backend (kcm_db = memory); the default secdb backend is unaffected. Reviewed-by: Alejandro López <allopez@redhat.com> Reviewed-by: Pavel Březina <pbrezina@redhat.com>
Contributor
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
kcm_cred_dup() performed a shallow pointer copy of cred_blob:
The memory backend (ccdb_mem_getbyname_send) returns ccache copies built via kcm_cc_dup() → kcm_cred_dup(). The duplicated credential therefore shares the same talloc iobuf with the original ccache.
When a client issues the following sequence on the same connection:
This is a use-after-free read (CWE-416).
Fix
Replace the shallow pointer copy with sss_iobuf_init_readonly(), which allocates an independent talloc buffer and memcpy()s the credential data:
This makes the duplicate fully independent of the original ccache's lifetime.
Consistency with secdb backend
The default secdb backend is already safe — bin_to_creds() in kcmsrv_ccache_binary.c creates independent iobuf copies via sss_iobuf_read_iobuf():
This fix brings the memory backend to the same invariant.