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

7.0.x/too long json/v1 #12204

Merged
merged 2 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
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
19 changes: 17 additions & 2 deletions src/output-json.c
Original file line number Diff line number Diff line change
Expand Up @@ -987,8 +987,23 @@ int OutputJsonBuilderBuffer(JsonBuilder *js, OutputJsonThreadCtx *ctx)
}

size_t jslen = jb_len(js);
if (MEMBUFFER_OFFSET(*buffer) + jslen >= MEMBUFFER_SIZE(*buffer)) {
MemBufferExpand(buffer, jslen);
DEBUG_VALIDATE_BUG_ON(jb_len(js) > UINT32_MAX);
size_t remaining = MEMBUFFER_SIZE(*buffer) - MEMBUFFER_OFFSET(*buffer);
if (jslen >= remaining) {
size_t expand_by = jslen + 1 - remaining;
if (MemBufferExpand(buffer, (uint32_t)expand_by) < 0) {
if (!ctx->too_large_warning) {
/* Log a warning once, and include enough of the log
* message to hopefully identify the event_type. */
char partial[120];
size_t partial_len = MIN(sizeof(partial), jslen);
memcpy(partial, jb_ptr(js), partial_len - 1);
partial[partial_len - 1] = '\0';
SCLogWarning("Formatted JSON EVE record too large, will be dropped: %s", partial);
ctx->too_large_warning = true;
}
return 0;
}
}

MemBufferWriteRaw((*buffer), jb_ptr(js), jslen);
Expand Down
1 change: 1 addition & 0 deletions src/output-json.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ typedef struct OutputJsonThreadCtx_ {
OutputJsonCtx *ctx;
LogFileCtx *file_ctx;
MemBuffer *buffer;
bool too_large_warning;
} OutputJsonThreadCtx;

json_t *SCJsonString(const char *val);
Expand Down
5 changes: 5 additions & 0 deletions src/util-buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ int MemBufferExpand(MemBuffer **buffer, uint32_t expand_by) {
return -1;
}

/* Adjust expand_by to next multiple of 4k. */
if (expand_by % 4096 != 0) {
expand_by = expand_by - (expand_by % 4096) + 4096;
}

size_t total_size = (*buffer)->size + sizeof(MemBuffer) + expand_by;

MemBuffer *tbuffer = SCRealloc(*buffer, total_size);
Expand Down
Loading