Skip to content

Commit

Permalink
util: Realloc buffer size for atomic safe read
Browse files Browse the repository at this point in the history
Realloc and increase the buffer size when safe read returns more
than CHILD_MSG_CHUNK size bytes.

This handles multiple passkey mappings returned from the krb5 child
in kerberos pre-authentication.
  • Loading branch information
justin-stephenson committed Sep 22, 2023
1 parent 44f7f44 commit 328f31d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/util/atomic_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ ssize_t sss_atomic_read_safe_s(int fd, void *buf, size_t buf_len, size_t *_len)
}

if (ulen > buf_len) {
return ERANGE;
*_len = ulen;
errno = ERANGE;
return -1;
}

if (_len != NULL) {
Expand Down
20 changes: 18 additions & 2 deletions src/util/child_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ static void _read_pipe_handler(struct tevent_context *ev,
struct _read_pipe_state *state;
ssize_t size;
errno_t err;
uint8_t buf[CHILD_MSG_CHUNK];
uint8_t *buf;
size_t len = 0;

state = tevent_req_data(req, struct _read_pipe_state);
Expand All @@ -521,18 +521,34 @@ static void _read_pipe_handler(struct tevent_context *ev,
return;
}

buf = talloc_array(state, uint8_t, CHILD_MSG_CHUNK);
if (buf == NULL) {
tevent_req_error(req, ENOMEM);
return;
}

if (state->safe) {
size = sss_atomic_read_safe_s(state->fd, buf, CHILD_MSG_CHUNK, &len);
} else {
size = sss_atomic_read_s(state->fd, buf, CHILD_MSG_CHUNK);
}

if (size == -1 && errno == ERANGE) {
buf = talloc_realloc(state, buf, uint8_t, len);
if(!buf) {
tevent_req_error(req, ENOMEM);
return;
}

size = sss_atomic_read_s(state->fd, buf, len);
}

if (size == -1) {
err = errno;
DEBUG(SSSDBG_CRIT_FAILURE,
"read failed [%d][%s].\n", err, strerror(err));
tevent_req_error(req, err);
return;

} else if (size > 0) {
state->buf = talloc_realloc(state, state->buf, uint8_t,
state->len + size);
Expand Down

0 comments on commit 328f31d

Please sign in to comment.