Require 'unsafe' keyword for custom implementations of nom traits#90
Open
progval wants to merge 4 commits intofflorent:masterfrom
Open
Require 'unsafe' keyword for custom implementations of nom traits#90progval wants to merge 4 commits intofflorent:masterfrom
progval wants to merge 4 commits intofflorent:masterfrom
Conversation
`nom_locate` progressively advances through a fragment by slicing it,
but expects to be able to go backward by as much as it advanced. This is
normally fine, but custom implementations of `nom` types could cause
UB by implementing slicing incorrectly.
After this commit, they will need to implement the unsafe trait
`RewindableFragment`, putting the burden of soundness on these
implementations.
stephaneyfx provides an example of a maliciously constructed fragment type
exercising this behavior:
> This function is called from public and safe functions like get_line_beginning. It assumes that the current fragment is part of a larger fragment and attempts to read before the beginning of the current fragment. This assumption may be incorrect as demonstrated by the following program that exhibits UB without unsafe and outputs garbage (which can change on every run).
```rust
use nom::{AsBytes, InputTake, Offset, Slice};
use nom_locate::LocatedSpan;
use std::{
cell::Cell,
ops::{RangeFrom, RangeTo},
rc::Rc,
};
struct EvilInput<'a>(Rc<Cell<&'a [u8]>>);
impl<'a> AsBytes for EvilInput<'a> {
fn as_bytes(&self) -> &[u8] {
self.0.get()
}
}
impl Offset for EvilInput<'_> {
fn offset(&self, second: &Self) -> usize {
self.as_bytes().offset(second.as_bytes())
}
}
impl Slice<RangeFrom<usize>> for EvilInput<'_> {
fn slice(&self, range: RangeFrom<usize>) -> Self {
Self(Rc::new(Cell::new(self.0.get().slice(range))))
}
}
impl Slice<RangeTo<usize>> for EvilInput<'_> {
fn slice(&self, range: RangeTo<usize>) -> Self {
Self(Rc::new(Cell::new(self.0.get().slice(range))))
}
}
fn main() {
let new_input = [32u8];
let original_input = [33u8; 3];
let evil_input = EvilInput(Rc::new(Cell::new(&original_input)));
let span = LocatedSpan::new(evil_input).take_split(2).0;
span.fragment().0.set(&new_input);
let beginning = span.get_line_beginning();
dbg!(beginning);
dbg!(new_input.as_ptr() as usize - beginning.as_ptr() as usize);
}
```
Example output:
```
[src/main.rs:43] beginning = [
201,
127,
32,
]
[src/main.rs:44] new_input.as_ptr() as usize - beginning.as_ptr() as usize = 2
```
Needed by doctests
2c7e2eb to
99dd411
Compare
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.
nom_locateprogressively advances through a fragment by slicing it, but expects to be able to go backward by as much as it advanced. This is normally fine, but custom implementations ofnomtypes could cause UB by implementing slicing incorrectly.After this commit, they will need to implement the unsafe trait
RewindableFragment, putting the burden of soundness on these implementations.@stephaneyfx provides an example of a maliciously constructed fragment type exercising this behavior in GH-88.
I tried to keep constraint to the bare minimum, but it probably makes sense to require the trait to be implemented when calling
LocatedSpan::new/LocatedSpan::new_extraand not having to care later, too. Thought?@AngelOfSol @ThePerkinrex @zertosh I believe you all have crates with custom types that implement the
nomtraits. Would this work for you? (Of course I'll do a major version bump when pushing this change to crates.io)Closes GH-88.