Skip to content

[autobackport: sssd-2-13] KCM: deep-copy cred_blob in kcm_cred_dup to avoid use-after-free - #9682

Merged
alexey-tikhonov merged 1 commit into
SSSD:sssd-2-13from
sssd-bot:SSSD-sssd-backport-pr9646-to-sssd-2-13
Nov 25, 2026
Merged

alexey-tikhonov merged 1 commit into
SSSD:sssd-2-13from
sssd-bot:SSSD-sssd-backport-pr9646-to-sssd-2-13

Conversation

@sssd-bot

Copy link
Copy Markdown
Contributor

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

git remote add sssd-bot git@github.com:sssd-bot/sssd.git
git fetch sssd-bot refs/heads/SSSD-sssd-backport-pr9646-to-sssd-2-13
git checkout SSSD-sssd-backport-pr9646-to-sssd-2-13
git push sssd-bot SSSD-sssd-backport-pr9646-to-sssd-2-13 --force

Original commits
aefd105 - KCM: deep-copy cred_blob in kcm_cred_dup to avoid use-after-free

Backported commits

  • 2a17967 - KCM: deep-copy cred_blob in kcm_cred_dup to avoid use-after-free

Original Pull Request Body

Problem

kcm_cred_dup() performed a shallow pointer copy of cred_blob:

dup->cred_blob = crd->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:

  1. GET_CRED_UUID_LIST — credentials are stolen into the per-connection hash table (conn_data->creds) via kcm_creds_to_table()
  2. DESTROY — the original ccache and its iobuf objects are freed
  3. GET_CRED_BY_UUID — the per-connection cache is reused, dereferencing the freed iobuf in kcm_op_get_cred_by_uuid_reply():
cred_blob = kcm_cred_get_creds(crd);          // crd->cred_blob is dangling
ret = sss_iobuf_write_len(reply, sss_iobuf_get_data(cred_blob),   // UAF read
                         sss_iobuf_get_size(cred_blob));

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:

len = sss_iobuf_get_len(crd->cred_blob);
dup->cred_blob = sss_iobuf_init_readonly(dup,
                                        sss_iobuf_get_data(crd->cred_blob),
                                        len,
                                        sss_iobuf_get_secure(crd->cred_blob));

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():

ret = sss_iobuf_read_iobuf(NULL, buf, &cred_blob);   // independent copy
crd = kcm_cred_new(mem_ctx, uuid, cred_blob);

This fix brings the memory backend to the same invariant.

@sssd-bot
sssd-bot requested review from aplopez and pbrezina November 25, 2026 14:25
@alexey-tikhonov alexey-tikhonov added the no-backport This should go to target branch only. label 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>
(cherry picked from commit aefd105)
@sssd-bot

Copy link
Copy Markdown
Contributor Author

The pull request was accepted by @alexey-tikhonov with the following PR CI status:


🟢 CodeQL (success)
🟢 rpm-build:centos-stream-10-x86_64:upstream (success)
🟢 rpm-build:fedora-44-x86_64:upstream (success)
🟢 rpm-build:fedora-45-x86_64:upstream (success)
🟢 testing-farm:centos-stream-10-x86_64:system (success)
🟢 testing-farm:fedora-44-x86_64:system (success)
🔴 testing-farm:fedora-45-x86_64:system (failure)
🟢 Build / freebsd (success)
🟢 Build / make-distcheck (success)
🟢 Static code analysis / codeql (success)
🟢 Static code analysis / files-changed (success)
➖ Static code analysis / polarion (skipped)
🟢 Static code analysis / pre-commit (success)
➖ Static code analysis / python-system-tests (skipped)


There are unsuccessful or unfinished checks. Make sure that the failures are not related to this pull request before merging.

@sssd-bot
sssd-bot force-pushed the SSSD-sssd-backport-pr9646-to-sssd-2-13 branch from 2a17967 to 67109f1 Compare November 25, 2026 18:15
@alexey-tikhonov
alexey-tikhonov merged commit 386bba5 into SSSD:sssd-2-13 Nov 25, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Accepted no-backport This should go to target branch only.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants