Fix: FFI return code handling#46
Merged
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR hardens the PAM Rust bindings by preventing UB when PAM (or the conversation callback) returns a c_int that doesn’t correspond to a defined PamResultCode enum discriminant.
Changes:
- Update PAM FFI function bindings to return
c_intinstead ofPamResultCode. - Add
TryFrom<c_int>and an internalPamResultCode::from_raw(c_int)helper to safely convert raw return codes (mapping unknowns toPAM_SYSTEM_ERR). - Apply safe conversion at call sites in
module.rsandconv.rs.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| pam/src/module.rs | Switch PAM FFI return types to c_int and convert via PamResultCode::from_raw at call sites. |
| pam/src/conv.rs | Update conversation callback return type to c_int and convert via PamResultCode::from_raw. |
| pam/src/constants.rs | Implement safe conversion from raw c_int to PamResultCode (including handling unknown values). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The PAM API returns a
c_intcode for most functions. The current FFI bindings interpret this as aPamResultCodeenum that's#[repr(C)], but only defines enum values from 0 to 31 inclusive. Any code returned outside of this range is UB (e.g., https://users.rust-lang.org/t/guarding-against-invalid-enum-values-arriving-via-ffi/33482). This fixes that. Invalid codes are squashed into aPAM_SYSTEM_ERRwhere appropriate.This also updates
set_datato:pam_set_datareturns an error&mut selfon the PAM handle to prevent being called whileget_datareferences still exist. I.e., it mutates, so requiremut.'staticon theTbeing stored to ensure that the T doesn't secretly borrow anything that might be dropped while PAM still holds it