-
Notifications
You must be signed in to change notification settings - Fork 30
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
Check input in rustls_error to avoid UB #195
Conversation
The input parameter for rustls_error was `rustls_result`. However, in Rust it's undefined behavior for an enum to hold an invalid value. That meant that if C passed an invalid value to rustls_error, UB would result. This changes the input parameter to be a uint, and relies on a macro from the num_enum crate to check the value of that input parameter. If the input is invalid, we emit the error for "InvalidParameter".
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.
Seems okay.
@@ -14,6 +14,7 @@ libc = "0.2" | |||
sct = "0.7" | |||
rustls-pemfile = "0.2.1" | |||
log = "0.4.14" | |||
num_enum = "0.5.4" |
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.
IMO the extra dependency for this is overkill. In this case it seems fine to write a custom TryFrom
impl based on the repr type.
I was reluctant to take the extra dep, too. The problem is, the list of
error codes is quite long, and I want to programmatically guarantee that
the TryFrom impl is complete (covers all the numbers) and accurate (maps to
the correct enum variant).
When I think of ways to do that in Rust, they mostly wind up looking like
proc macros. And somebody has already written the appropriate proc macro.
I'm interested in other ways to achieve the same goal, though. Do you have
a way in mind to ensure completeness and accuracy here?
|
Yeah, it's not exactly trivial. Maybe something like the enum builder declarative macro we use in rustls? |
I'm going to merge as-is, then file a separate issue for achieving this without a dependency. |
Follow-up issue: #198 |
The input parameter for rustls_error was
rustls_result
. However, inRust it's undefined behavior for an enum to hold an invalid value. That
meant that if C passed an invalid value to rustls_error, UB would
result.
This changes the input parameter to be a uint, and relies on a macro
from the num_enum crate to check the value of that input parameter. If
the input is invalid, we emit the error for "InvalidParameter".
Part of #152.