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

Thread-safe setting of variables #350

Closed
hillu opened this issue Oct 1, 2015 · 4 comments
Closed

Thread-safe setting of variables #350

hillu opened this issue Oct 1, 2015 · 4 comments

Comments

@hillu
Copy link
Contributor

hillu commented Oct 1, 2015

If I read libyara/rules.c correctly, when there are multiple threads
sharing a ruleset for scanning, there is no way to safely define
external variables.

I use variable definition to pass trivial information such as file or
process names that are used within some of my rules. Of course, I
could use one copy of the ruleset per thread, but in my use case that
would mean a much larger memory footprint. (Also, this would defeat
the purpose of YARA's support for multiple threads.)

The yr_rules_define_*_variable functions operate on a global
structure hanging off the YR_RULES struct without any locking. Even
though yr_rules_scan_mem_blocks eventually copies everything into
context.objects_table before starting the actual scan, I see no way
to ensure that the variables hanging off rules->externals_list_head
are concsistent -- even if I protect the individual
yr_rules_define_*_variable calls with a mutex.

Am I missing something?

@plusvic
Copy link
Member

plusvic commented Oct 1, 2015

You are right, yr_rules_define_*_variable aren't thread-safe. You can't assign a thread-specific value to a given variable, all threads share the same value which should be assigned by a single thread and shared by all threads calling yr_rules_scan_*.

As you said, the most important limitation here is that you can't pass values that depends on the file being scanned, like filename, or whatever. This could be solved by providing a separate structure to each thread to store variable values, but that would break existing code that assumes that once the main thread assigns value to a variable all other threads inherit that value.

@bmagistro
Copy link

I have hit this issue as well and will likely just be making multiple copies of the rules. Is there any chance the yr_rules_scan_* functions can be extended to include an (optional) variable list? Under the hood it may be possible to keep the calls the same and point this new argument to the rules/compiler structure's external variable list when it isn't present. The thinking being this might avoid breaking existing code while allowing an additional path to be added where variables could be defined per scan call. Yes this means that all variables would need to be defined at all times even if not in use but I see this as a issue for the caller and not an issue for yara to solve.

Thoughts? If this sounds like a possible approach I'd be willing to try and code some of this up but not sure how successful I'd be.

@hillu
Copy link
Contributor Author

hillu commented Oct 7, 2016

@bmagistro What you describe is pretty much the path I had in mind.

@bmagistro
Copy link

bmagistro commented Oct 10, 2016

@hillu I think I have something, I'm just now starting to test things using the new functions and providing the vars to the scan function.

Edit: It doesn't work (yet), it seems to work fine when not trying to pass an ext to the scan function, passing it to the scan function doesn't seem to match/apply. This is going to take a bit longer, it isn't as straight forward as I thought.

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

No branches or pull requests

3 participants