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

Check input in rustls_error to avoid UB #195

Merged
merged 1 commit into from
Nov 9, 2021
Merged

Check input in rustls_error to avoid UB #195

merged 1 commit into from
Nov 9, 2021

Conversation

jsha
Copy link
Collaborator

@jsha jsha commented Nov 8, 2021

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".

Part of #152.

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".
@jsha jsha requested a review from djc November 8, 2021 01:58
Copy link
Member

@djc djc left a 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"
Copy link
Member

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.

@jsha
Copy link
Collaborator Author

jsha commented Nov 8, 2021 via email

@djc
Copy link
Member

djc commented Nov 8, 2021

Yeah, it's not exactly trivial. Maybe something like the enum builder declarative macro we use in rustls?

@jsha
Copy link
Collaborator Author

jsha commented Nov 9, 2021

I'm going to merge as-is, then file a separate issue for achieving this without a dependency.

@jsha jsha merged commit f4e370b into main Nov 9, 2021
@jsha jsha deleted the remove-error-ub branch November 9, 2021 01:57
@jsha
Copy link
Collaborator Author

jsha commented Nov 9, 2021

Follow-up issue: #198

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

Successfully merging this pull request may close these issues.

2 participants