Repository navigation
[autobackport: sssd-2-13] KCM: deep-copy cred_blob in kcm_cred_dup to avoid use-after-free - #9682
Conversation
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> (cherry picked from commit aefd105)
|
The pull request was accepted by @alexey-tikhonov with the following PR CI status: 🟢 CodeQL (success) There are unsuccessful or unfinished checks. Make sure that the failures are not related to this pull request before merging. |
2a17967 to
67109f1
Compare
This is an automatic backport of PR#9646 KCM: deep-copy cred_blob in kcm_cred_dup to avoid use-after-free to branch sssd-2-13, created by @kkzhsh.
Please make sure this backport is correct.
Note
The commits were cherry-picked without conflicts.
You can push changes to this pull request
Original commits
aefd105 - KCM: deep-copy cred_blob in kcm_cred_dup to avoid use-after-free
Backported commits
Original Pull Request Body
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.