HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/libsystemd/sd-json/json-stream.c
Git blob: 883b59a30136a1b40916481d59f22ed194f7ed76
Lines 1001–1437 of 1,437
memcpy(mempcpy(n, s->output_buffer + s->output_buffer_index, s->output_buffer_size), text, sz + dsz);
if (FLAGS_SET(s->flags, JSON_STREAM_OUTPUT_BUFFER_SENSITIVE))
s->output_buffer = erase_and_free(s->output_buffer);
else
free(s->output_buffer);
s->output_buffer = n;
s->output_buffer_size = new_size;
s->output_buffer_index = 0;
}
if (sd_json_variant_is_sensitive_recursive(m))
s->flags |= JSON_STREAM_OUTPUT_BUFFER_SENSITIVE;
else
text = mfree(text); /* Skip the erase_and_free() destructor declared above */
return 0;
}
static int json_stream_format_queue(JsonStream *s) {
int r;
assert(s);
/* Drain entries out of the output queue and format them into the output buffer.
* Stop if there are unwritten output_fds or if the next item carries fds but
* the output buffer is non-empty, since adding more would corrupt the fd boundary. */
while (s->output_queue) {
assert(s->n_output_queue > 0);
if (s->n_output_fds > 0)
return 0;
JsonStreamQueueItem *q = s->output_queue;
/* If the next item carries fds but the output buffer still holds bytes from
* a prior fast-path enqueue or a partial write, we must not concatenate its
* JSON into that same buffer: the subsequent sendmsg() in json_stream_write()
* would attach the fds to the combined bytes and break the message-to-fd boundary.
* Stop here and let json_stream_write() drain the buffer first; the next write()
* call will pull this item into a clean buffer.
*
* On AF_UNIX SOCK_STREAM, separate sendmsg() calls alone are not sufficient:
* recvmsg() may combine preceding data with the next SCM_RIGHTS-bearing send.
* The receive side must also stop at message boundaries to keep the association. */
if (q->n_fds > 0 && s->output_buffer_size > 0)
return 0;
_cleanup_free_ int *array = NULL;
if (q->n_fds > 0) {
array = newdup(int, q->fds, q->n_fds);
if (!array)
return -ENOMEM;
}
r = json_stream_format_json(s, q->data);
if (r < 0)
return r;
free_and_replace(s->output_fds, array);
s->n_output_fds = q->n_fds;
q->n_fds = 0;
LIST_REMOVE(queue, s->output_queue, q);
if (!s->output_queue)
s->output_queue_tail = NULL;
s->n_output_queue--;
json_stream_queue_item_free(q);
}
return 0;
}
int json_stream_enqueue_full(JsonStream *s, sd_json_variant *m, const int fds[], size_t n_fds) {
assert(s);
assert(m);
assert(fds || n_fds == 0);
/* Fast path: no fds and no items currently queued — append directly into the
* output buffer to avoid the queue allocation. */
if (n_fds == 0 && !s->output_queue)
return json_stream_format_json(s, m);
if (s->n_output_queue >= s->queue_max)
return -ENOBUFS;
JsonStreamQueueItem *q = json_stream_queue_item_new(m, fds, n_fds);
if (!q)
return -ENOMEM;
LIST_INSERT_AFTER(queue, s->output_queue, s->output_queue_tail, q);
s->output_queue_tail = q;
s->n_output_queue++;
return 0;
}
/* ===== Write side ===== */
int json_stream_write(JsonStream *s) {
ssize_t n;
int r;
assert(s);
if (FLAGS_SET(s->flags, JSON_STREAM_CONNECTING))
return 0;
if (FLAGS_SET(s->flags, JSON_STREAM_WRITE_DISCONNECTED))
return 0;
/* Drain the deferred queue into the output buffer if possible */
r = json_stream_format_queue(s);
if (r < 0)
return r;
if (s->output_buffer_size == 0)
return 0;
assert(s->output_fd >= 0);
if (s->n_output_fds > 0) {
struct iovec iov = {
.iov_base = s->output_buffer + s->output_buffer_index,
.iov_len = s->output_buffer_size,
};
struct msghdr mh = {
.msg_iov = &iov,
.msg_iovlen = 1,
.msg_controllen = CMSG_SPACE(sizeof(int) * s->n_output_fds),
};
mh.msg_control = alloca0(mh.msg_controllen);
struct cmsghdr *control = CMSG_FIRSTHDR(&mh);
control->cmsg_len = CMSG_LEN(sizeof(int) * s->n_output_fds);
control->cmsg_level = SOL_SOCKET;
control->cmsg_type = SCM_RIGHTS;
memcpy(CMSG_DATA(control), s->output_fds, sizeof(int) * s->n_output_fds);
n = sendmsg(s->output_fd, &mh, MSG_DONTWAIT|MSG_NOSIGNAL);
} else if (FLAGS_SET(s->flags, JSON_STREAM_PREFER_WRITE))
n = write(s->output_fd, s->output_buffer + s->output_buffer_index, s->output_buffer_size);
else
n = send(s->output_fd, s->output_buffer + s->output_buffer_index, s->output_buffer_size, MSG_DONTWAIT|MSG_NOSIGNAL);
if (n < 0) {
if (ERRNO_IS_TRANSIENT(errno))
return 0;
if (ERRNO_IS_DISCONNECT(errno)) {
s->flags |= JSON_STREAM_WRITE_DISCONNECTED;
return 1;
}
return -errno;
}
if (FLAGS_SET(s->flags, JSON_STREAM_OUTPUT_BUFFER_SENSITIVE))
explicit_bzero_safe(s->output_buffer + s->output_buffer_index, n);
s->output_buffer_size -= n;
if (s->output_buffer_size == 0) {
s->output_buffer_index = 0;
s->flags &= ~JSON_STREAM_OUTPUT_BUFFER_SENSITIVE;
} else
s->output_buffer_index += n;
close_many(s->output_fds, s->n_output_fds);
s->n_output_fds = 0;
/* Refresh activity timestamp on real progress (and rearm the time source if attached
* to an event loop). */
s->last_activity = json_stream_now(s);
json_stream_rearm_time_source(s);
return 1;
}
/* ===== Read side ===== */
/* Peek at the socket data to find the delimiter and return a read size that won't consume past it.
* Besides preserving data for protocol handoff, this keeps SCM_RIGHTS from a later message from being
* received with an earlier message that carries no file descriptors. Falls back to byte-by-byte for
* non-socket fds where MSG_PEEK is not available. */
static ssize_t json_stream_peek_message_boundary(JsonStream *s, void *p, size_t rs) {
assert(s);
if ((s->flags & (JSON_STREAM_BOUNDED_READS|JSON_STREAM_ALLOW_FD_PASSING_INPUT)) == 0)
return rs;
/* Packet sockets already preserve the FD boundary. Shortening their reads would truncate data. */
if (s->input_socket_type != SOCK_STREAM && !FLAGS_SET(s->flags, JSON_STREAM_BOUNDED_READS))
return rs;
if (FLAGS_SET(s->flags, JSON_STREAM_PREFER_READ))
return 1;
ssize_t peeked = recv(s->input_fd, p, rs, MSG_PEEK|MSG_DONTWAIT);
if (peeked < 0) {
/* Let the actual read classify disconnections and update the stream state. */
if (ERRNO_IS_DISCONNECT(errno))
return rs;
if (!ERRNO_IS_TRANSIENT(errno))
return -errno;
/* Transient error: shouldn't happen but fall back to byte-by-byte */
return 1;
}
/* EOF: the real recv() will also see it; what we return here doesn't matter */
if (peeked == 0)
return rs;
size_t dsz = json_stream_delimiter_size(s);
/* Include the buffered suffix in case the delimiter straddles the old and new data. */
size_t rescan = MIN(s->input_buffer_size, dsz - 1);
void *delim = memmem_safe((char*) p - rescan, rescan + peeked, s->delimiter ?: "\0", dsz);
if (delim)
return (char*) delim + dsz - (char*) p;
return peeked;
}
int json_stream_read(JsonStream *s) {
struct iovec iov;
struct msghdr mh;
ssize_t rs;
ssize_t n;
void *p;
assert(s);
if (FLAGS_SET(s->flags, JSON_STREAM_CONNECTING))
return 0;
if (s->input_buffer_unscanned > 0)
return 0;
if (FLAGS_SET(s->flags, JSON_STREAM_READ_DISCONNECTED))
return 0;
if (s->input_buffer_size >= s->buffer_max)
return -ENOBUFS;
assert(s->input_fd >= 0);
if (MALLOC_SIZEOF_SAFE(s->input_buffer) <= s->input_buffer_index + s->input_buffer_size) {
size_t add;
add = MIN(s->buffer_max - s->input_buffer_size, s->read_chunk);
if (s->input_buffer_index == 0 &&
(!FLAGS_SET(s->flags, JSON_STREAM_INPUT_SENSITIVE) || s->input_buffer_size == 0)) {
if (!GREEDY_REALLOC(s->input_buffer, s->input_buffer_size + add))
return -ENOMEM;
} else {
char *b;
b = new(char, s->input_buffer_size + add);
if (!b)
return -ENOMEM;
memcpy(b, s->input_buffer + s->input_buffer_index, s->input_buffer_size);
if (FLAGS_SET(s->flags, JSON_STREAM_INPUT_SENSITIVE))
s->input_buffer = erase_and_free(s->input_buffer);
else
free(s->input_buffer);
s->input_buffer = b;
s->input_buffer_index = 0;
}
}
p = s->input_buffer + s->input_buffer_index + s->input_buffer_size;
rs = MALLOC_SIZEOF_SAFE(s->input_buffer) - (s->input_buffer_index + s->input_buffer_size);
/* Preserve message boundaries when receiving file descriptors or preparing a protocol upgrade. */
rs = json_stream_peek_message_boundary(s, p, rs);
if (rs < 0)
return json_stream_log_errno(s, (int) rs, "Failed to peek message boundary: %m");
if (FLAGS_SET(s->flags, JSON_STREAM_ALLOW_FD_PASSING_INPUT)) {
iov = IOVEC_MAKE(p, rs);
if (!s->input_control_buffer) {
s->input_control_buffer_size = CMSG_SPACE(sizeof(int) * JSON_STREAM_FDS_MAX);
s->input_control_buffer = malloc(s->input_control_buffer_size);
if (!s->input_control_buffer)
return -ENOMEM;
}
mh = (struct msghdr) {
.msg_iov = &iov,
.msg_iovlen = 1,
.msg_control = s->input_control_buffer,
.msg_controllen = s->input_control_buffer_size,
};
n = RET_NERRNO(recvmsg(s->input_fd, &mh, MSG_DONTWAIT|MSG_CMSG_CLOEXEC));
if (n >= 0 && FLAGS_SET(mh.msg_flags, MSG_TRUNC)) {
cmsg_close_all(&mh);
return -EXFULL;
}
if (n >= 0 && FLAGS_SET(mh.msg_flags, MSG_CTRUNC)) {
/* SCM_RIGHTS got truncated — typically because an LSM (e.g. SELinux)
* denied the fd transfer. Drop the partial fds and continue with the
* data bytes: the request handler will surface a clean error to the
* peer when it tries to peek the missing fd, instead of us tearing
* the connection down silently and leaving the caller waiting. */
json_stream_log(s, "SCM_RIGHTS truncated on inbound message, dropping received file descriptors.");
cmsg_close_all(&mh);
mh.msg_controllen = 0;
}
} else if (FLAGS_SET(s->flags, JSON_STREAM_PREFER_READ))
n = RET_NERRNO(read(s->input_fd, p, rs));
else
n = RET_NERRNO(recv(s->input_fd, p, rs, MSG_DONTWAIT));
if (ERRNO_IS_NEG_TRANSIENT(n))
return 0;
if (ERRNO_IS_NEG_DISCONNECT(n)) {
s->flags |= JSON_STREAM_READ_DISCONNECTED;
return 1;
}
if (n < 0)
return n;
if (n == 0) { /* EOF */
if (FLAGS_SET(s->flags, JSON_STREAM_ALLOW_FD_PASSING_INPUT))
cmsg_close_all(&mh);
s->flags |= JSON_STREAM_READ_DISCONNECTED;
return 1;
}
if (FLAGS_SET(s->flags, JSON_STREAM_ALLOW_FD_PASSING_INPUT)) {
struct cmsghdr *cmsg;
cmsg = cmsg_find(&mh, SOL_SOCKET, SCM_RIGHTS, (socklen_t) -1);
if (cmsg) {
size_t add;
/* fds are only allowed with the first byte of a message; receiving them
* mid-stream is a protocol violation. */
if (s->input_buffer_size != 0) {
cmsg_close_all(&mh);
return -EPROTO;
}
add = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
if (add > INT_MAX - s->n_input_fds) {
cmsg_close_all(&mh);
return -EBADF;
}
if (!GREEDY_REALLOC(s->input_fds, s->n_input_fds + add)) {
cmsg_close_all(&mh);
return -ENOMEM;
}
memcpy_safe(s->input_fds + s->n_input_fds, CMSG_TYPED_DATA(cmsg, int), add * sizeof(int));
s->n_input_fds += add;
}
}
/* Also rescan the last delimiter_size - 1 bytes already buffered, so a delimiter split
* across reads can be found. */
size_t rescan = MIN(s->input_buffer_size, json_stream_delimiter_size(s) - 1);
s->input_buffer_size += n;
s->input_buffer_unscanned = rescan + n;
return 1;
}
/* ===== Parse ===== */
int json_stream_parse(JsonStream *s, sd_json_variant **ret) {
char *begin, *e;
size_t sz;
int r;
assert(s);
assert(ret);
if (s->input_buffer_unscanned == 0) {
*ret = NULL;
return 0;
}
assert(s->input_buffer_unscanned <= s->input_buffer_size);
assert(s->input_buffer_index + s->input_buffer_size <= MALLOC_SIZEOF_SAFE(s->input_buffer));
begin = s->input_buffer + s->input_buffer_index;
size_t dsz = json_stream_delimiter_size(s);
e = memmem_safe(begin + s->input_buffer_size - s->input_buffer_unscanned, s->input_buffer_unscanned, s->delimiter ?: "\0", dsz);
if (!e) {
s->input_buffer_unscanned = 0;
*ret = NULL;
return 0;
}
sz = e - begin + dsz;
/* For non-NUL delimiters (e.g. "\r\n" for QMP) sd_json_parse() needs a NUL-terminated
* string; overwrite the first delimiter byte with NUL in place. For NUL delimiters
* this is a no-op since the byte is already '\0'. */
if (s->delimiter)
*e = '\0';
r = sd_json_parse(begin, SD_JSON_PARSE_MUST_BE_OBJECT, ret, /* reterr_line= */ NULL, /* reterr_column= */ NULL);
if (FLAGS_SET(s->flags, JSON_STREAM_INPUT_SENSITIVE))
explicit_bzero_safe(begin, sz);
if (r < 0) {
/* Unrecoverable parse failure: drop all buffered data. */
s->input_buffer_index = s->input_buffer_size = s->input_buffer_unscanned = 0;
return json_stream_log_errno(s, r, "Failed to parse JSON object: %m");
}
if (DEBUG_LOGGING) {
_cleanup_(erase_and_freep) char *censored_text = NULL;
/* Suppress sensitive fields in the debug output */
r = sd_json_variant_format(*ret, /* flags= */ SD_JSON_FORMAT_CENSOR_SENSITIVE, &censored_text);
if (r >= 0)
json_stream_log(s, "Received message: %s", censored_text);
}
s->input_buffer_size -= sz;
if (s->input_buffer_size == 0)
s->input_buffer_index = 0;
else
s->input_buffer_index += sz;
s->input_buffer_unscanned = s->input_buffer_size;
return 1;
}