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

API: YR_COMPILER errors lead to undefined behavior #47

Closed
Orycterope opened this issue Sep 16, 2021 · 2 comments
Closed

API: YR_COMPILER errors lead to undefined behavior #47

Orycterope opened this issue Sep 16, 2021 · 2 comments

Comments

@Orycterope
Copy link
Contributor

I found myself debugging an infinite loop in a rule that yara compiled. The reason was that the rule which emitted the buggy rule was invalid, compiler::add_rules returned an error, but because I'm doing a best effort, I ignored it, expecting yara would just throw it away, and kept adding other rules, called compiler::compile_rules, and used the obtain rules to do a scan.

The problem was that for the invalid rule, yara compiled it to bytecode that made no sense, like it completely gave up in the middle of the compilation, hence the infinite loop. But it was still executed by the scan.

Turns out reading the doc more closely, it says that

The yr_compiler_add_file(), yr_compiler_add_fd(), and yr_compiler_add_string() functions return the number of errors found in the source code. If the rules are correct they will return 0. If any of these functions return an error the compiler can't used anymore, neither for adding more rules nor getting the compiled rules.

This is not at all obvious in the api that the compiler provides, and right now it is too easy to end up with undefined behavior if you don't read the yara docs very closely.

I believe all the add_rules_* functions should take the compiler by move, like compiler::compile_rules does, instead of &mut self, and return a Result<Compiler, YaraError>, where Ok is the same compiler with the rule successfully added. That way it will be impossible for the user to use a Compiler after one of the yara functions returned an error.

I can do a PR for that, but it will be a breaking change. I wanted your opinion on it before doing it.

@Orycterope Orycterope changed the title API: YR_COMPILER errors leads to undefined behavior API: YR_COMPILER errors lead to undefined behavior Sep 16, 2021
@ikrivosheev
Copy link
Contributor

@Orycterope I think this is a good solution.

@Hugal31
Copy link
Owner

Hugal31 commented Sep 17, 2021

I think it is a good solution too.

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