Skip to content

Commit

Permalink
util: Realloc buffer size for atomic safe read
Browse files Browse the repository at this point in the history
Port
1f4fffd
to sssd-2-8

Realloc and increase the buffer size when safe read returns more
than CHILD_MSG_CHUNK size bytes.

Reviewed-by: Alexey Tikhonov <atikhono@redhat.com>
Reviewed-by: Iker Pedrosa <ipedrosa@redhat.com>
Reviewed-by: Sumit Bose <sbose@redhat.com>

Reviewed-by: Alexey Tikhonov <atikhono@redhat.com>
  • Loading branch information
justin-stephenson authored and pbrezina committed Feb 26, 2024
1 parent a140037 commit 3332e10
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/util/atomic_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,11 @@ ssize_t sss_atomic_read_safe_s(int fd, void *buf, size_t buf_len, size_t *_len)
}

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

if (_len != NULL) {
Expand Down
18 changes: 16 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,8 +521,23 @@ 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);
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);
}
} else {
size = sss_atomic_read_s(state->fd, buf, CHILD_MSG_CHUNK);
}
Expand All @@ -532,7 +547,6 @@ static void _read_pipe_handler(struct tevent_context *ev,
"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 3332e10

Please sign in to comment.