-
Notifications
You must be signed in to change notification settings - Fork 34
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
Conversation
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; |
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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);
When you create a patch, use Indent as TAB. |
I have modified it in accordance with your comments |
Why do you think that this issue is security vulnerablities? 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. |
There was a problem hiding this 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.
I verified it according to your sample1.c example, added safety detection parameters, and prompted heap-use-after-free during actual operation |
compilation parameters: operation result: |
62199b6
to
42a2191
Compare
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. |
When I was using this library, I used a security detection tool to detect a memory leak problem