Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent out of boundary write on malicious input #592

Merged
merged 3 commits into from May 7, 2020
Merged

Prevent out of boundary write on malicious input #592

merged 3 commits into from May 7, 2020

Conversation

stoeckmann
Copy link
Contributor

I have discovered a way to trigger an out of boundary write while parsing a huge json file through a malicious input source. It can be triggered if an attacker has control over the input stream or if a huge load during filesystem operations can be triggered.

Preparation:
$ dd if=/dev/zero of=poc.json bs=1 count=1 seek=2147483647

Code to exploit:

#include <json-c/json_util.h>
#include <unistd.h>
int main() {
  json_object_from_fd(STDIN_FILENO);
  return 0;
}

Proof of Concept:
(dd if=poc.json bs=4096; sleep 1; dd if=test.json bs=10) 2>/dev/null | ./test

Explanation:
The problem manifests itself in printbuf_memappend. On properly crafted values, p->bpos + size + 1 can overflow, which leads to the assumption that p->size is still large enough. In normal circumstances, this does not happen with json_object_from_fd due to its buffer size leading to proper detection. But if the parsed buffer chunk length is not a power of 2 (sleep 1 and bs=10 triggers this in my proof of concept), this overflow can be abused by an attacker to write past the memory boundary of p->buf.

My example simply crashes the program eventually. A proper attack can be controled in a way to not crash the system but simply write a few attacker controlled bytes outside the allocated area, allowing more sophisticated attacks against real world programs.

@coveralls
Copy link

coveralls commented May 2, 2020

Coverage Status

Coverage decreased (-0.2%) to 85.768% when pulling d07b910 on stoeckmann:oob into 8e3d3d5 on json-c:master.

linkhash.c Outdated Show resolved Hide resolved
arraylist.c Show resolved Hide resolved
linkhash.c Outdated Show resolved Hide resolved
printbuf.c Outdated Show resolved Hide resolved
printbuf.c Outdated Show resolved Hide resolved
If the assignment of stop overflows due to idx and count being
larger than SIZE_T_MAX in sum, out of boundary access could happen.

It takes invalid usage of this function for this to happen, but
I decided to add this check so array_list_del_idx is as safe against
bad usage as the other arraylist functions.
If a linkhash with a size of zero is created, then modulo operations
are prone to division by zero operations.

Purely protective measure against bad usage.
linkhash.c Outdated Show resolved Hide resolved
The data structures linkhash and printbuf are limited to 2 GB in size
due to a signed integer being used to track their current size.

If too much data is added, then size variable can overflow, which is
an undefined behaviour in C programming language.

Assuming that a signed int overflow just leads to a negative value,
like it happens on many sytems (Linux i686/amd64 with gcc), then
printbuf is vulnerable to an out of boundary write on 64 bit systems.
@hawicz
Copy link
Member

hawicz commented May 7, 2020

The changes look good, thanks!

@hawicz hawicz merged commit 31243e4 into json-c:master May 7, 2020
robimarko added a commit to sartura/openwrt that referenced this pull request May 12, 2020
This backports upstream fixes for the out of bounds write vulnerability in json-c.
It was reported and patches in this upstream PR: json-c/json-c#592

Addresses CVE-2020-12762

Signed-off-by: Robert Marko <robert.marko@sartura.hr>
Signed-off-by: Luka Perkov <luka.perkov@sartura.h>
jow- pushed a commit to openwrt/openwrt that referenced this pull request May 13, 2020
This backports upstream fixes for the out of bounds write vulnerability in json-c.
It was reported and patches in this upstream PR: json-c/json-c#592

Addresses CVE-2020-12762

Signed-off-by: Robert Marko <robert.marko@sartura.hr>
Signed-off-by: Luka Perkov <luka.perkov@sartura.hr>
[bump PKG_RELEASE]
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
aiamadeus pushed a commit to immortalwrt/immortalwrt that referenced this pull request May 13, 2020
This backports upstream fixes for the out of bounds write vulnerability in json-c.
It was reported and patches in this upstream PR: json-c/json-c#592

Addresses CVE-2020-12762

Signed-off-by: Robert Marko <robert.marko@sartura.hr>
Signed-off-by: Luka Perkov <luka.perkov@sartura.hr>
[bump PKG_RELEASE]
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
jow- pushed a commit to openwrt/openwrt that referenced this pull request May 14, 2020
This backports upstream fixes for the out of bounds write vulnerability in json-c.
It was reported and patches in this upstream PR: json-c/json-c#592

Addresses CVE-2020-12762

Signed-off-by: Robert Marko <robert.marko@sartura.hr>
Signed-off-by: Luka Perkov <luka.perkov@sartura.hr>
[bump PKG_RELEASE, rebase patches on top of json-c 0.12]
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
(backported from commit bc0288b)
jow- pushed a commit to openwrt/openwrt that referenced this pull request May 17, 2020
This backports upstream fixes for the out of bounds write vulnerability in json-c.
It was reported and patches in this upstream PR: json-c/json-c#592

Addresses CVE-2020-12762

Signed-off-by: Robert Marko <robert.marko@sartura.hr>
Signed-off-by: Luka Perkov <luka.perkov@sartura.hr>
[bump PKG_RELEASE, rebase patches on top of json-c 0.12]
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
(backported from commit bc0288b)
jollaman999 pushed a commit to jollaman999/openwrt that referenced this pull request Jul 10, 2020
This backports upstream fixes for the out of bounds write vulnerability in json-c.
It was reported and patches in this upstream PR: json-c/json-c#592

Addresses CVE-2020-12762

Signed-off-by: Robert Marko <robert.marko@sartura.hr>
Signed-off-by: Luka Perkov <luka.perkov@sartura.hr>
[bump PKG_RELEASE]
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
lunatickochiya pushed a commit to lunatickochiya/lunatic-lede that referenced this pull request Sep 6, 2020
This backports upstream fixes for the out of bounds write vulnerability in json-c.
It was reported and patches in this upstream PR: json-c/json-c#592

Addresses CVE-2020-12762

Signed-off-by: Robert Marko <robert.marko@sartura.hr>
Signed-off-by: Luka Perkov <luka.perkov@sartura.hr>
[bump PKG_RELEASE, rebase patches on top of json-c 0.12]
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
(backported from commit bc0288b)

Conflicts:
	package/libs/libjson-c/Makefile
lunatickochiya pushed a commit to lunatickochiya/lunatic-lede that referenced this pull request Sep 6, 2020
This backports upstream fixes for the out of bounds write vulnerability in json-c.
It was reported and patches in this upstream PR: json-c/json-c#592

Addresses CVE-2020-12762

Signed-off-by: Robert Marko <robert.marko@sartura.hr>
Signed-off-by: Luka Perkov <luka.perkov@sartura.hr>
[bump PKG_RELEASE, rebase patches on top of json-c 0.12]
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
(backported from commit bc0288b)

Conflicts:
	package/libs/libjson-c/Makefile

(cherry picked from commit a4c8f1d)
biliwala pushed a commit to biliwala/friendlywrt that referenced this pull request Oct 16, 2020
This backports upstream fixes for the out of bounds write vulnerability in json-c.
It was reported and patches in this upstream PR: json-c/json-c#592

Addresses CVE-2020-12762

Signed-off-by: Robert Marko <robert.marko@sartura.hr>
Signed-off-by: Luka Perkov <luka.perkov@sartura.hr>
[bump PKG_RELEASE, rebase patches on top of json-c 0.12]
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
(backported from commit bc0288b)
jpuhlman pushed a commit to MontaVista-OpenSourceTechnology/poky that referenced this pull request Nov 10, 2020
Source: poky
MR: 103736
Type: Integration
Disposition: Merged from poky
ChangeID: abe50ba1a3e14191f2418c986781c824293fa523
Description:

CVE-2020-12762-*.patch:
* fix a series of integer overflows adding checks in linkhash.c, printbuf.c,
  test4.c, test4.expected

json-c/json-c#592

Signed-off-by: Milan Shah <mshah@mvista.com>
Reviewed-by: Armin Kuster <akuster@mvista.com>
Signed-off-by: Jeremy A. Puhlman <jpuhlman@mvista.com>
ArtelMike pushed a commit to ArtelMike/openwrt-1 that referenced this pull request Jan 31, 2023
This backports upstream fixes for the out of bounds write vulnerability in json-c.
It was reported and patches in this upstream PR: json-c/json-c#592

Addresses CVE-2020-12762

Signed-off-by: Robert Marko <robert.marko@sartura.hr>
Signed-off-by: Luka Perkov <luka.perkov@sartura.hr>
[bump PKG_RELEASE, rebase patches on top of json-c 0.12]
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
(backported from commit d071916)
apple-ouyang added a commit to apple-ouyang/libfastjson that referenced this pull request Mar 14, 2023
reference: https://github.com/json-c/json-c/pull/592/files

I reproduce this CVE using the code from json-c/json-c#592

And it fix it and no more segmentation fault
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants