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

Invalid memory approach (heap-use-after-free) #18

Merged
merged 2 commits into from
May 13, 2021

Conversation

gaoxiang-ut
Copy link
Contributor

When I was using this library, I used a security detection tool to detect a memory leak problem

@Joungkyun Joungkyun self-requested a review May 10, 2021 15:07
@Joungkyun Joungkyun added the bug label May 10, 2021
src/chardet.cpp Outdated
@@ -150,16 +155,19 @@ CHARDET_API short detect_r (const char *buf, size_t buflen, DetectObj ** obj) {
ret = det->getCharsetName ();
delete det;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the original code 160 line is approached to the removed variable, the 156 line must be removed.

src/chardet.cpp Outdated
}

(*obj)->encoding = (char *) strdup(ret);
(*obj)->confidence = det->getConfidence();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think missing following code lilne:

(*obj)->bom = det->getIsBOM ();

src/chardet.cpp Outdated
@@ -114,15 +114,20 @@ CHARDET_API short detect_handledata_r (Detect ** det, const char * buf, size_t b

ret = (*det)->detect->getCharsetName ();

if ( ! ret )
if ( ! ret ){
delete det;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The det variable used by the detect_handledata_r function was generated by the detect_init function. Therefore, depending on the return value of detect_Handledata_r, it must be deleted by the detect_destroy function outside the detect_handledata_r.

Therefore, the detect_dataHandle_r function modification is not required as my thought.

For example, you must be processed as follows:

d = detect_init ();
detect_reset (&d);

obj = detect_obj_init ();
swith (detect_handledata_r (&d, strings, string_length, &obj)) {
    case CHARDET_OUT_OF_MEMORY :
    case CHARDET_NULL_OBJECT :
        detect_obj_free (&obj);
        detect_destroy (&d);
        exit (1);
}
detect_obj_free (&obj);
detect_destroy (&d);

The detect_handledata_r function is provided for use in the loop. Therefore, the example of the following example is better:

d = detect_init ();
if ( !d ) exit (1);

while ( condition ) {
    detect_reset (&d);
    obj = detect_obj_init ();

    swith (detect_handledata_r (&d, strings, string_length, &obj)) {
        case CHARDET_OUT_OF_MEMORY :
        case CHARDET_NULL_OBJECT :
            detect_obj_free (&obj);
            continue;
    }

    printf (
        "encoding: %s, confidence: %f, exist BOM: %d\n",
        obj->encoding, obj->confidence, obj->bom
    );
    detect_obj_free (&obj);
}
detect_destroy (&d);

@Joungkyun Joungkyun changed the title Fix security vulnerabilities Invalid memory approach May 10, 2021
@Joungkyun
Copy link
Owner

When you create a patch, use Indent as TAB.

@Joungkyun Joungkyun self-assigned this May 10, 2021
@Joungkyun Joungkyun added this to the 1.0.6 milestone May 10, 2021
@gaoxiang-ut
Copy link
Contributor Author

I have modified it in accordance with your comments

@gaoxiang-ut gaoxiang-ut changed the title Invalid memory approach libchardet security vulnerabilities May 11, 2021
@Joungkyun
Copy link
Owner

Why do you think that this issue is security vulnerablities?
151 line has been freed memory of det, and it is a problem that you are accessing freeed det from the 160 line.

151  delete det;
...
160 (*obj)->bom = det->getIsBOM ();

If you can insert a code that can be attacked by this freed memory address, it will be a security issue, but it seems that there is no path to insert the code into that memory.
So this problem is that SEGFAULT can be occured by accessing invalid memory rather than secure issues.

Copy link
Owner

@Joungkyun Joungkyun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The patch seems that there is no problem.
However, I asked to ask for the indentation of the code before.
libchardet uses tabs instead of white spaces for indentation.

@gaoxiang-ut
Copy link
Contributor Author

I verified it according to your sample1.c example, added safety detection parameters, and prompted heap-use-after-free during actual operation

@gaoxiang-ut
Copy link
Contributor Author

compilation parameters:
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -fsanitize=address -O2")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -fsanitize=address -O2")

operation result:
==20703==ERROR: AddressSanitizer: heap-use-after-free on address 0x608000001ad0 at pc 0x0000004057b1 bp 0x7ffc3a433c80 sp 0x7ffc3a433c78
READ of size 4 at 0x608000001ad0 thread T0
#0 0x4057b0 in detect_r /home/gaoxiang/Work_GX/dvp/demo/libchardetDemo/src/chardet.cpp:177
#1 0x402cab in main /home/gaoxiang/Work_GX/dvp/demo/libchardetDemo/main.cpp:47
#2 0x7fe54c5e309a in __libc_start_main ../csu/libc-start.c:308
#3 0x403699 in _start (/home/gaoxiang/Work_GX/dvp/demo/build-libchardetDemo-unknown-Debug/libchardetDemo+0x403699)

@gaoxiang-ut gaoxiang-ut force-pushed the master branch 3 times, most recently from 62199b6 to 42a2191 Compare May 12, 2021 02:55
@Joungkyun Joungkyun changed the title libchardet security vulnerabilities Invalid memory approach (heap-use-after-free) May 13, 2021
@Joungkyun
Copy link
Owner

I will change this PR title. Do not change again. I think the issue title should summarize the contents of the issue of the issue. Security vulnerabilities are too broad.
Regardless of this, I think it is very grateful to your patch.

@Joungkyun Joungkyun merged commit 70c7500 into Joungkyun:master May 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants