Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KCM: fix memory leak #7823

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 12 additions & 22 deletions src/responder/kcm/secrets/secrets.c
Original file line number Diff line number Diff line change
Expand Up @@ -979,7 +979,7 @@ errno_t sss_sec_put(struct sss_sec_req *req,
size_t secret_len)
{
struct ldb_message *msg;
struct ldb_val secret_val = { .data = NULL };
const struct ldb_val secret_val = { .length = secret_len, .data = secret };
bool erase_msg = false;
int ret;

Expand Down Expand Up @@ -1029,13 +1029,11 @@ 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;
}

/* `ldb_msg_add_value()` does NOT make a copy of secret_val::*data
* but rather copies a pointer under the hood.
* This is fine since no operations modifying this data are performed
* below and 'msg' is freed before function returns.
*/
ret = ldb_msg_add_value(msg, SEC_ATTR_SECRET, &secret_val, NULL);
if (ret != EOK) {
DEBUG(SSSDBG_OP_FAILURE,
Expand Down Expand Up @@ -1069,9 +1067,6 @@ errno_t sss_sec_put(struct sss_sec_req *req,

ret = EOK;
done:
if (secret_val.data != NULL) {
sss_erase_mem_securely(secret_val.data, secret_val.length);
}
if (erase_msg) {
db_result_erase_message_securely(msg, SEC_ATTR_SECRET);
}
Expand All @@ -1084,7 +1079,7 @@ errno_t sss_sec_update(struct sss_sec_req *req,
size_t secret_len)
{
struct ldb_message *msg;
struct ldb_val secret_val = { .data = NULL };
const struct ldb_val secret_val = { .length = secret_len, .data = secret };
bool erase_msg = false;
int ret;

Expand Down Expand Up @@ -1134,13 +1129,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 All @@ -1150,6 +1138,11 @@ errno_t sss_sec_update(struct sss_sec_req *req,
goto done;
}

/* `ldb_msg_add_value()` does NOT make a copy of secret_val::*data
* but rather copies a pointer under the hood.
* This is fine since no operations modifying this data are performed
* below and 'msg' is freed before function returns.
*/
ret = ldb_msg_add_value(msg, SEC_ATTR_SECRET, &secret_val, NULL);
if (ret != LDB_SUCCESS) {
DEBUG(SSSDBG_MINOR_FAILURE,
Expand All @@ -1174,9 +1167,6 @@ errno_t sss_sec_update(struct sss_sec_req *req,

ret = EOK;
done:
if (secret_val.data != NULL) {
sss_erase_mem_securely(secret_val.data, secret_val.length);
}
if (erase_msg) {
db_result_erase_message_securely(msg, SEC_ATTR_SECRET);
}
Expand Down
Loading