Skip to content

Commit 99cf98a

Browse files
authored
Merge pull request #1709 from davidBar-On/JSON_read-check-JSON-size
Json read check json size
2 parents 62f3fb2 + dab301f commit 99cf98a

File tree

2 files changed

+33
-25
lines changed

2 files changed

+33
-25
lines changed

src/iperf.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,8 @@ struct iperf_test
436436

437437
#define UDP_BUFFER_EXTRA 1024
438438

439+
#define MAX_PARAMS_JSON_STRING 8 * 1024
440+
439441
/* constants for command line arg sanity checks */
440442
#define MB (1024 * 1024)
441443
#define MAX_TCP_BUFFER (512 * MB)

src/iperf_api.c

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2805,35 +2805,41 @@ JSON_read(int fd)
28052805
* Then read the JSON into a buffer and parse it. Return a parsed JSON
28062806
* structure, NULL if there was an error.
28072807
*/
2808-
if (Nread(fd, (char*) &nsize, sizeof(nsize), Ptcp) >= 0) {
2809-
hsize = ntohl(nsize);
2810-
/* Allocate a buffer to hold the JSON */
2811-
strsize = hsize + 1; /* +1 for trailing NULL */
2812-
if (strsize) {
2813-
str = (char *) calloc(sizeof(char), strsize);
2814-
if (str != NULL) {
2815-
rc = Nread(fd, str, hsize, Ptcp);
2816-
if (rc >= 0) {
2817-
/*
2818-
* We should be reading in the number of bytes corresponding to the
2819-
* length in that 4-byte integer. If we don't the socket might have
2820-
* prematurely closed. Only do the JSON parsing if we got the
2821-
* correct number of bytes.
2822-
*/
2823-
if (rc == hsize) {
2824-
json = cJSON_Parse(str);
2825-
}
2826-
else {
2827-
printf("WARNING: Size of data read does not correspond to offered length\n");
2828-
}
2829-
}
2830-
}
2831-
free(str);
2808+
rc = Nread(fd, (char*) &nsize, sizeof(nsize), Ptcp);
2809+
if (rc == sizeof(nsize)) {
2810+
hsize = ntohl(nsize);
2811+
if (hsize > 0 && hsize <= MAX_PARAMS_JSON_STRING) {
2812+
/* Allocate a buffer to hold the JSON */
2813+
strsize = hsize + 1; /* +1 for trailing NULL */
2814+
if (strsize) {
2815+
str = (char *) calloc(sizeof(char), strsize);
2816+
if (str != NULL) {
2817+
rc = Nread(fd, str, hsize, Ptcp);
2818+
if (rc >= 0) {
2819+
/*
2820+
* We should be reading in the number of bytes corresponding to the
2821+
* length in that 4-byte integer. If we don't the socket might have
2822+
* prematurely closed. Only do the JSON parsing if we got the
2823+
* correct number of bytes.
2824+
*/
2825+
if (rc == hsize) {
2826+
json = cJSON_Parse(str);
2827+
}
2828+
else {
2829+
warning("JSON size of data read does not correspond to offered length");
2830+
}
2831+
}
2832+
free(str);
2833+
}
2834+
}
28322835
}
28332836
else {
2834-
printf("WARNING: Data length overflow\n");
2837+
warning("JSON data length overflow");
28352838
}
28362839
}
2840+
else {
2841+
warning("Failed to read JSON data size");
2842+
}
28372843
return json;
28382844
}
28392845

0 commit comments

Comments
 (0)