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

Json read check json size #1709

Merged
merged 4 commits into from
Sep 20, 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
2 changes: 2 additions & 0 deletions src/iperf.h
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,8 @@ struct iperf_test

#define UDP_BUFFER_EXTRA 1024

#define MAX_PARAMS_JSON_STRING 8 * 1024

/* constants for command line arg sanity checks */
#define MB (1024 * 1024)
#define MAX_TCP_BUFFER (512 * MB)
Expand Down
56 changes: 31 additions & 25 deletions src/iperf_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -2799,35 +2799,41 @@ JSON_read(int fd)
* Then read the JSON into a buffer and parse it. Return a parsed JSON
* structure, NULL if there was an error.
*/
if (Nread(fd, (char*) &nsize, sizeof(nsize), Ptcp) >= 0) {
hsize = ntohl(nsize);
/* Allocate a buffer to hold the JSON */
strsize = hsize + 1; /* +1 for trailing NULL */
if (strsize) {
str = (char *) calloc(sizeof(char), strsize);
if (str != NULL) {
rc = Nread(fd, str, hsize, Ptcp);
if (rc >= 0) {
/*
* We should be reading in the number of bytes corresponding to the
* length in that 4-byte integer. If we don't the socket might have
* prematurely closed. Only do the JSON parsing if we got the
* correct number of bytes.
*/
if (rc == hsize) {
json = cJSON_Parse(str);
}
else {
printf("WARNING: Size of data read does not correspond to offered length\n");
}
}
}
free(str);
rc = Nread(fd, (char*) &nsize, sizeof(nsize), Ptcp);
if (rc == sizeof(nsize)) {
hsize = ntohl(nsize);
if (hsize > 0 && hsize <= MAX_PARAMS_JSON_STRING) {
/* Allocate a buffer to hold the JSON */
strsize = hsize + 1; /* +1 for trailing NULL */
if (strsize) {
str = (char *) calloc(sizeof(char), strsize);
if (str != NULL) {
rc = Nread(fd, str, hsize, Ptcp);
if (rc >= 0) {
/*
* We should be reading in the number of bytes corresponding to the
* length in that 4-byte integer. If we don't the socket might have
* prematurely closed. Only do the JSON parsing if we got the
* correct number of bytes.
*/
if (rc == hsize) {
json = cJSON_Parse(str);
}
else {
warning("JSON size of data read does not correspond to offered length");
}
}
free(str);
}
}
}
else {
printf("WARNING: Data length overflow\n");
warning("JSON data length overflow");
}
}
else {
warning("Failed to read JSON data size");
}
return json;
}

Expand Down
Loading