Bug: Missing NULL check for cJSON_malloc in FindPointerFromObjectTo leads to crash under OOM / allocation failure
Maintainer thường phản hồi trong vòng 12 ngày
@JIgar-maheshwari-dev đang làm issue này rồi.
Từ ngày 22/7/2026.
- #1055 của @JIgar-maheshwari-dev — đang mở
Đánh giá
- Độ khó
- 3/5
- Thời gian dự kiến
- 1-2 ngày
- Mức phù hợp với người mới
- 62/100
Hướng nghiên cứu
Bắt đầu trong cJSON_Utils.c, tại cJSONUtils_FindPointerFromObjectTo và các hàm trợ giúp đệ quy của nó, sau đó chạy PoC custom-hook được cung cấp với lỗi cấp phát nhắm vào full_pointer. Theo dõi từng cJSON_malloc được sử dụng để ghép đường dẫn; hoàn tất khi các lần cấp phát thất bại trả về NULL mà không truyền bộ đệm NULL cho sprintf, và PoC không còn bị crash.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Issue Description:
A memory allocation vulnerability was identified in cJSON_Utils.c. In the internal logic used by cJSONUtils_FindPointerFromObjectTo, a buffer allocated via cJSON_malloc (or hooks.allocate) is passed directly into sprintf (or similar string formatting functions) without validating if the allocation was successful.
Under memory pressure or custom hook limits where malloc returns NULL, this omission causes a Null Pointer Dereference (Segmentation Fault), compromising the stability of applications relying on cJSON_Utils.
Vulnerability Analysis:
The vulnerability resides in the internal recursive helper functions of cJSON_Utils.c responsible for path resolution (such as FindPointerFromObjectTo traversing array/object elements):
// Static/Internal logic inside cJSON_Utils.c
...
full_pointer = (char*)cJSON_malloc(strlen(pointer) + length + 1);
// Vulnerability: Missing check for full_pointer == NULL
sprintf(full_pointer, "%s/%s", pointer, ...);
...
Root Cause:
Dynamic Path Assembly: When cJSONUtils_FindPointerFromObjectTo recursively constructs the JSON Pointer path for deeply nested objects or arrays, it dynamically allocates a new string buffer (full_pointer) using cJSON_malloc.
Missing Boundary Check: The code blindly assumes that cJSON_malloc will always succeed. If the system runs out of memory, or if a custom cJSON_Hooks allocator purposefully returns NULL, full_pointer remains NULL.
Crash via sprintf: Passing a NULL destination pointer into sprintf causes an immediate write segmentation fault (SIGSEGV).
Steps to Reproduce:
Proof of Concept (PoC)
The following code simulates an Out-Of-Memory (OOM) or restricted allocation condition by utilizing cJSON_InitHooks to intercept and deliberately fail the specific allocation sequence inside cJSONUtils_FindPointerFromObjectTo:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cJSON.h"
#include "cJSON_Utils.h"
static int g_alloc_count = 0;
static int g_fail_at = 3; // Target allocation sequence used for full_pointer
static void* tracking_malloc(size_t sz)
{
int idx = g_alloc_count++;
if (idx == g_fail_at) {
fprintf(stderr, " [HOOK] blocking malloc #%d (size=%zu) -> NULL\n", idx, sz);
return NULL; // Simulate allocation failure
}
return malloc(sz);
}
int main(void)
{
printf("[PoC] Unchecked cJSON_malloc return value in FindPointer\n");
/* Inject custom malloc hook */
cJSON_Hooks hooks = { tracking_malloc, free };
cJSON_InitHooks(&hooks);
/* Construct nested structure: [ [ 42 ] ] */
cJSON *root = cJSON_CreateArray();
cJSON *inner = cJSON_CreateArray();
cJSON_AddItemToArray(root, inner);
cJSON *target = cJSON_CreateNumber(42);
cJSON_AddItemToArray(inner, target);
printf("[*] Calling cJSONUtils_FindPointerFromObjectTo()...\n");
/* Triggering vulnerability */
char *ptr = cJSONUtils_FindPointerFromObjectTo(root, target);
if (ptr != NULL) {
free(ptr);
}
cJSON_Delete(root);
cJSON_InitHooks(NULL);
return 0;
}
Expected Result:
If compiled normally, when the execution hits malloc #3, the hook returns NULL. cJSON_Utils will immediately crash with a Segmentation Fault inside sprintf due to writing to a zero page address.
(Note: Depending on the specific version and compiler optimizations, the precise allocation index g_fail_at may vary between 3 or 4 to hit the exact target buffer. A crash reliably occurs once the exact path allocation is targeted).
Suggested Fix
Add proper error handling after every cJSON_malloc call within cJSON_Utils.c. If an allocation fails, the function should gracefully abort the recursion and bubble up an error (e.g., return NULL):
full_pointer = (char*)cJSON_malloc(strlen(pointer) + length + 1);
if (full_pointer == NULL)
{
// Clean up if necessary and return error status
return NULL;
}
sprintf(full_pointer, "%s/%s", pointer, ...);
- 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
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
DaveGamble/cJSON#1122 ·
Maintainer thường phản hồi trong vòng 12 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
DaveGamble/cJSON#1106 ·
Maintainer thường phản hồi trong vòng 12 ngày
-
Undefined-Behavior in function cJSON_SetNumberHelperCó thể đã có người làm Có pull request liên kết đang mở hoặc đã được merge. Đ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 ·
Maintainer thường phản hồi trong vòng 12 ngày
-
CMake config files contain absolute install pathsCó thể đã có người làm @akashdeshmukh-git đã nhận 7 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 ·
Maintainer thường phản hồi trong vòng 12 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
DaveGamble/cJSON#1093 ·
Maintainer thường phản hồi trong vòng 12 ngày
Tất cả issue của DaveGamble/cJSON
Issue tương tự
-
vim.filetype.match() prefixes a URL buffer name with the cwd before passing it to filetype functionsĐang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 73/100
Maintainer thường phản hồi trong vòng 1 ngày
-
stream_curl: retry_count never resets after a successful reconnect, so long sequential streams die after 5 dropsCó thể đã có người làm Có pull request liên kết đang mở hoặc đã được merge. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
mpv-player/mpv#18728 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
followup Prio 📌
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 66/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
cyrusimap/cyrus-imapd#6640 ·
Maintainer thường phản hồi trong vòng 2 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 60/100
Maintainer thường phản hồi trong vòng 1 ngày