Skip to content

Commit

Permalink
KCM: fix memory leak
Browse files Browse the repository at this point in the history
`ldb_msg_add_value()` makes a copy under the hood, so there is no need
to make intermediate copy of `secret` argument.
This copy - `secret_val.data` - was left hanging on `sss_sec_ctx`, effectively
resulting in a memory leak.

This is a backport of SSSD#7823

:fixes:'sssd_kcm' memory leak was fixed.
  • Loading branch information
alexey-tikhonov committed Feb 5, 2025
1 parent 60b1ae4 commit 22c2d4c
Showing 1 changed file with 2 additions and 16 deletions.
18 changes: 2 additions & 16 deletions src/responder/kcm/secrets/secrets.c
Original file line number Diff line number Diff line change
Expand Up @@ -953,7 +953,7 @@ errno_t sss_sec_put(struct sss_sec_req *req,
size_t secret_len)
{
struct ldb_message *msg;
struct ldb_val secret_val;
const struct ldb_val secret_val = { .length = secret_len, .data = secret };
int ret;

if (req == NULL || secret == NULL) {
Expand Down Expand Up @@ -1002,13 +1002,6 @@ errno_t sss_sec_put(struct sss_sec_req *req,
goto done;
}

secret_val.length = secret_len;
secret_val.data = talloc_memdup(req->sctx, secret, secret_len);
if (!secret_val.data) {
ret = ENOMEM;
goto done;
}

ret = ldb_msg_add_value(msg, SEC_ATTR_SECRET, &secret_val, NULL);
if (ret != EOK) {
DEBUG(SSSDBG_OP_FAILURE,
Expand Down Expand Up @@ -1050,7 +1043,7 @@ errno_t sss_sec_update(struct sss_sec_req *req,
size_t secret_len)
{
struct ldb_message *msg;
struct ldb_val secret_val;
const struct ldb_val secret_val = { .length = secret_len, .data = secret };
int ret;

if (req == NULL || secret == NULL) {
Expand Down Expand Up @@ -1099,13 +1092,6 @@ errno_t sss_sec_update(struct sss_sec_req *req,
goto done;
}

secret_val.length = secret_len;
secret_val.data = talloc_memdup(req->sctx, secret, secret_len);
if (!secret_val.data) {
ret = ENOMEM;
goto done;
}

/* FIXME - should we have a lastUpdate timestamp? */
ret = ldb_msg_add_empty(msg, SEC_ATTR_SECRET, LDB_FLAG_MOD_REPLACE, NULL);
if (ret != LDB_SUCCESS) {
Expand Down

0 comments on commit 22c2d4c

Please sign in to comment.