NULL dereference in cJSONUtils_GenerateMergePatch on object members with a NULL key
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức phù hợp với người mới
- 85/100
Hướng nghiên cứu
Lỗi crash nằm ở lệnh strcmp không được bảo vệ, dùng trên các key của object member bên trong generate_merge_patch trong cJSON_Utils.c (khoảng dòng 1427). Hàm này được gọi tới từ cả cJSONUtils_GenerateMergePatch và cJSONUtils_GenerateMergePatchCaseSensitive. So sánh với helper an toàn với NULL là compare_strings, đang được dùng ở các dòng 498, 538, 662 và 1238. Tạo reproducer từ issue và xác nhận lỗi crash, sau đó chuyển phép so sánh sang compare_strings và kiểm tra patch không còn crash nữa. Hoàn thành khi không còn crash với member không có key và có unit test bao phủ trường hợp này.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
cJSONUtils_GenerateMergePatch() compares the keys of two object members with a plain strcmp at cJSON_Utils.c:1427 (v1.7.19 / current master):
static cJSON *generate_merge_patch(cJSON * const from, cJSON * const to, const cJSON_bool case_sensitive)
{
...
sort_object(from, case_sensitive); /* line 1410 */
sort_object(to, case_sensitive); /* line 1411 */
from_child = from->child;
to_child = to->child;
...
while (from_child || to_child) /* line 1420 */
{
int diff;
if (from_child != NULL)
{
if (to_child != NULL)
{
diff = strcmp(from_child->string, to_child->string); /* line 1427 */
}
...
The child pointers are checked for NULL, but their string members are not. An object member with a NULL key can be produced through the public API alone — cJSON_AddItemToArray() does no type checking and assigns no key, so it appends keyless children to any node, including objects:
cJSON *object = cJSON_CreateObject();
cJSON_AddItemToArray(object, cJSON_CreateString("value")); /* object->child->string stays NULL */
(cJSON is a fully public struct, so direct construction reaches the same state too.)
What makes this a defect rather than caller misuse is that the rest of this exact code path deliberately tolerates NULL keys:
- the
sort_object()calls two lines above go throughsort_list(), which compares keys with the NULL-safecompare_strings()(cJSON_Utils.c:498, 538) — a NULL-keyed member survives sorting create_patches(), the JSON-Patch counterpart doing the same object walk, compares keys withcompare_strings()at line 1238compare_json(), called later in the same loop, usescompare_strings()at line 662- the applier side
cJSONUtils_MergePatch()also survives NULL keys without crashing (cJSON_DeleteItemFromObjectwith a NULL name simply matches nothing)
generate_merge_patch is the only place in the file that compares object keys with raw strcmp.
Reproducer
#include <stdio.h>
#include "cJSON.h"
#include "cJSON_Utils.h"
int main(void)
{
/* two objects, each holding one keyless member (public API only) */
cJSON *from = cJSON_CreateObject();
cJSON *to = cJSON_CreateObject();
cJSON_AddItemToArray(from, cJSON_CreateString("a"));
cJSON_AddItemToArray(to, cJSON_CreateString("b"));
cJSON *patch = cJSONUtils_GenerateMergePatch(from, to);
printf("patch=%p\n", (void*)patch);
cJSON_Delete(patch);
cJSON_Delete(from);
cJSON_Delete(to);
return 0;
}
sort_object() passes, then line 1427 evaluates strcmp(NULL, NULL) and the process crashes. It also crashes with only one keyless member (strcmp(NULL, "b")), and via cJSONUtils_GenerateMergePatchCaseSensitive(), which shares the same code (cJSON_Utils.c:1482).
Suggested fix
Use the NULL-safe helper the sibling code already uses:
diff = compare_strings((unsigned char*)from_child->string, (unsigned char*)to_child->string, case_sensitive);
A unit test exercising a keyless object member would keep this fixed.
One related note, silently wrong rather than crashing, in case it is worth covering at the same time: in the diff < 0 / diff > 0 branches, from_child->string / to_child->string are passed to cJSON_AddItemToObject(), which returns false for a NULL key, so such a member is silently dropped from the generated patch.
Environment
- cJSON v1.7.19 (master) and the
developbranch — the rawstrcmpis present in both - CWE-476 (NULL Pointer Dereference)
- Ngôn ngữ chính
- C
- Star
- 13.1k
- Fork
- 3.6k
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Không có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của DaveGamble/cJSON
-
[BUG] cJSON_PrintBuffered(item, 0, ...) with custom hooks: ensure() reads 1 byte from a zero-sized buffer (memcpy(..., p->offset + 1))Có thể đã có người làm @ck0i đã nhận 19 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
DaveGamble/cJSON#1122 · 1 bình luận ·
-
pointer comparsion undefined behavior in cJSON_SetValuestringCó thể đã có người làm @kanishk25249-sudo đã nhận 18 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
DaveGamble/cJSON#1106 ·
-
Undefined-Behavior in function cJSON_SetNumberHelperCó thể đã có người làm @akashdeshmukh-git đã nhận 25 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
DaveGamble/cJSON#1100 · 1 reaction ·
-
CMake config files contain absolute install pathsCó thể đã có người làm @akashdeshmukh-git đã nhận 36 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 86/100
DaveGamble/cJSON#1094 · 9 bình luận · 1 reaction ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
DaveGamble/cJSON#1093 · 1 bình luận ·
Tất cả issue của DaveGamble/cJSON
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
Maintainer thường phản hồi trong vòng 2 ngày
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 62/100
libsndfile/libsndfile#1238 ·
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 88/100
quickjs-ng/quickjs#1959 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Claude:Bug GPT:Bug triage
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
open5gs/open5gs#5161 · 2 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Cyberpunk not listed in readmeĐang mở
Độ khó 1/5 1-3 giờ Mức phù hợp với người mới 72/100
Maintainer thường phản hồi trong vòng 2 ngày