Skip to content

KCM: deep-copy cred_blob in kcm_cred_dup to avoid use-after-free - #9646

Merged
alexey-tikhonov merged 1 commit into
SSSD:masterfrom
kkzhsh:PR_M4
Nov 25, 2026
Merged

alexey-tikhonov merged 1 commit into
SSSD:masterfrom
kkzhsh:PR_M4

Conversation

@kkzhsh

@kkzhsh kkzhsh commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

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.

@pbrezina pbrezina left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Otherwise LGTM. Thank you.

Comment thread src/responder/kcm/kcmsrv_ccache.c Outdated
* 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);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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 */
};

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the review. Updated and force-pushed.
Switched sss_iobuf_get_len() to sss_iobuf_get_size().

@aplopez aplopez left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

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>
@sssd-bot

Copy link
Copy Markdown
Contributor

The pull request was accepted by @aplopez with the following PR CI status:


🟢 CodeQL (success)
🟢 osh-diff-scan:fedora-rawhide-x86_64:upstream (success)
🟢 rpm-build:centos-stream-10-x86_64:upstream (success)
🟢 rpm-build:fedora-43-x86_64:upstream (success)
🟢 rpm-build:fedora-44-x86_64:upstream (success)
🟢 rpm-build:fedora-45-x86_64:upstream (success)
🟢 rpm-build:fedora-rawhide-x86_64:upstream (success)
🟢 testing-farm:centos-stream-10-x86_64:system (success)
🟢 testing-farm:fedora-43-x86_64:system (success)
🟢 testing-farm:fedora-44-x86_64:system (success)
🟢 testing-farm:fedora-45-x86_64:system (success)
🟢 testing-farm:fedora-rawhide-x86_64:system (success)
🟢 Build / freebsd (success)
🔴 Build / make-distcheck (failure)
➖ Coverity scan / coverity (skipped)
🟢 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.

@alexey-tikhonov
alexey-tikhonov merged commit aefd105 into SSSD:master Nov 25, 2026
1 check passed
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.

5 participants