Bug: Missing NULL check for cJSON_malloc in FindPointerFromObjectTo leads to crash under OOM / allocation failure
Los mantenedores suelen responder en 12 días
@JIgar-maheshwari-dev ya está trabajando en esto.
Desde el 22/7/2026.
- #1055 de @JIgar-maheshwari-dev — abierto
Evaluación
- Dificultad
- 3/5
- Tiempo estimado
- 1-2 días
- Aptitud para principiantes
- 62/100
Línea de trabajo
Comienza en cJSON_Utils.c, en cJSONUtils_FindPointerFromObjectTo y sus funciones auxiliares recursivas, y luego ejecuta el PoC de custom-hook proporcionado con un fallo de asignación dirigido a full_pointer. Rastrea cada cJSON_malloc utilizado para construir la ruta; la tarea está terminada cuando las asignaciones fallidas devuelven NULL sin pasar un búfer NULL a sprintf, y el PoC deja de fallar.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
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, ...);
- Lenguaje dominante
- C
- Estrellas
- 13k
- Forks
- 3.5k
- Métricas de merge de PR
- Sin PR fusionados en 30 d
Preparar el entorno
- Sin Dockerfile ni archivo de Docker Compose
- Sin plantilla de pull request
- Leer la guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de DaveGamble/cJSON
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 68/100
DaveGamble/cJSON#1106 ·
Los mantenedores suelen responder en 12 días
-
Undefined-Behavior in function cJSON_SetNumberHelperPosiblemente ocupada Un pull request vinculado a esta issue está abierto o ya se fusionó. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 70/100
DaveGamble/cJSON#1100 · 1 reacción ·
Los mantenedores suelen responder en 12 días
-
CMake config files contain absolute install pathsPosiblemente ocupada Un pull request vinculado a esta issue está abierto o ya se fusionó. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 86/100
DaveGamble/cJSON#1094 · 9 comentarios · 1 reacción ·
Los mantenedores suelen responder en 12 días
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 68/100
DaveGamble/cJSON#1093 ·
Los mantenedores suelen responder en 12 días
-
Use-after-free in cJSONUtils_ApplyPatches when a patch removes the patch arrayPosiblemente ocupada @iliasabk la tomó hace 34 días. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 82/100
DaveGamble/cJSON#1082 ·
Los mantenedores suelen responder en 12 días
Todos los issues de DaveGamble/cJSON
Issues similares
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 76/100
polhenarejos/pico-fido#302 ·
-
开发板墙缺少 ESP32_S31_MOSAICOAbierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 67/100
espressif/esp-vision#16 · 1 comentario ·
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 74/100
libsdl-org/SDL#16577 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 66/100
Los mantenedores suelen responder en 1 día
-
halt_error adds a trailing newline for non-string input, contrary to the manualPosiblemente ocupada @Huang-404-Q la tomó hoy. Abierto
Dificultad 1/5 1-3 horas Aptitud para principiantes 88/100
Los mantenedores suelen responder en 2 días