NULL dereference in cJSONUtils_GenerateMergePatch on object members with a NULL key

オープン 初心者向け
#1,143 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
85/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
c
領域
data

調査の方向性

クラッシュは、generate_merge_patch 内でオブジェクトメンバーのキーに対して直接呼び出している strcmp にあります(cJSON_Utils.c の 1427 行付近)。この関数には cJSONUtils_GenerateMergePatch と cJSONUtils_GenerateMergePatchCaseSensitive の両方から到達します。498、538、662、1238 行で使われている NULL 安全な compare_strings ヘルパーと比較してください。issue の再現コードを作成してクラッシュを確認し、その後比較を compare_strings に切り替えて、パッチがクラッシュしなくなったことを確認してください。キーのないメンバーでクラッシュしないこと、そしてそのケースをカバーするユニットテストがあることが完了条件です。

索引モデルが issue の本文から書いたものです。

説明

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 through sort_list(), which compares keys with the NULL-safe compare_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 with compare_strings() at line 1238
  • compare_json(), called later in the same loop, uses compare_strings() at line 662
  • the applier side cJSONUtils_MergePatch() also survives NULL keys without crashing (cJSON_DeleteItemFromObject with 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 develop branch — the raw strcmp is present in both
  • CWE-476 (NULL Pointer Dereference)
主要言語
C
スター
13.1k
フォーク
3.6k
PR マージ指標
30日以内にマージされた PR はありません

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

DaveGamble/cJSON のほかの issue

DaveGamble/cJSON の issue をすべて見る

似ている issue

C の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。