-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Undefined error handling logic when getting the first block of process memory and checking "iterator->last_error" #1671
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
Comments
These 2 uses of unassigned "last_error" are effectively detected by valgrind:
Adding 2 code changes makes valgring happy:
Such an assignment already exists in Lines 653 to 657 in a36b497
Lines 435 to 439 in e26c15e
Add:
If I got the point right :) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
We start by calling
yr_rules_scan_proc()
. It opens the iterator viayr_process_open_iterator()
, which allocates and fills the members of the structYR_MEMORY_BLOCK_ITERATOR
iterator, except forlast_error
:yara/libyara/proc.c
Lines 53 to 60 in e1360f6
Then it calls
yr_rules_scan_mem_blocks()
->yr_scanner_scan_mem_blocks()
, and the first block is returned via callback:yara/libyara/scanner.c
Line 463 in a36b497
In fact, the platform-specific
yr_process_get_first_memory_block()
is called, which almost directly callsyr_process_get_next_memory_block()
:yara/libyara/proc/windows.c
Lines 178 to 189 in 9ab96d1
The last one can return real block or NULL.
But depending on the platform, the function sets
iterator->last_error = ERROR_SUCCESS;
at the beginning of the function (Windows):yara/libyara/proc/windows.c
Line 145 in 9ab96d1
or only in case of real success (Linux):
yara/libyara/proc/linux.c
Line 402 in 9ab96d1
Those we got a situation where "iterator->last_error" is ERROR_SUCCESS even if return value is NULL (Windows):
yara/libyara/proc/windows.c
Line 175 in 9ab96d1
but also "iterator->last_error" is an unassigned variable (almost always not ERROR_SUCCESS) if the return value is NULL (Linux):
yara/libyara/proc/linux.c
Line 392 in 9ab96d1
This is the first interesting point.
Let's assume we got NULL when trying to get the first block, so we skip the
while (block != NULL)
loop and now check for errors:yara/libyara/scanner.c
Lines 503 to 506 in a36b497
Depending on the correct logic, we can exit here or continue execution here, this is the second point.
I got this error in the wild and caught it in the debugger:

The text was updated successfully, but these errors were encountered: