Skip to content

Fix out-of-bounds access possibility in safe code. #18

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

Merged
merged 2 commits into from
Dec 3, 2018

Conversation

SimonSapin
Copy link
Contributor

With the id and tree fields of NodeRef and NodeMut being public, it was possible to assign to them. For example, it was possible to build a NodeMut for large ID/index in a small tree/Vec. Since some APIs use unchecked indexing, this would let users of this library cause out-of-bounds access in a Vec without writing unsafe code themselves.

This commit fixes that issue by making the fields private and instead providing read-only access via accessor methods. Now the fields can only be set by the ego-tree crate, which can make sure to only ever use an ID that is in-bounds for a given tree.

With the `id` and `tree` fields of `NodeRef` and `NodeMut` being public,
it was possible to assign to them.
For example, it was possible to build a `NodeMut`
for large ID/index in a small tree/Vec.
Since some APIs use unchecked indexing, this would let users of this library
cause out-of-bounds access in a `Vec` without writing `unsafe` code themselves.

This deprecates the fields,
and instead providing read-only access via accessor methods.

This lets dependent crates realize that there is a problem
they might need to fix.
@causal-agent
Copy link
Collaborator

Yikes. If I remember correctly this used to be the API but at some point I switched to public fields thinking that NodeId being opaque was enough.

With the `id` and `tree` fields of `NodeRef` and `NodeMut` being public,
it was possible to assign to them.
For example, it was possible to build a `NodeMut`
for large ID/index in a small tree/Vec.
Since some APIs use unchecked indexing, this would let users of this library
cause out-of-bounds access in a `Vec` without writing `unsafe` code themselves.

This commit fixes that issue by making the fields private
and instead providing read-only access via accessor methods.
Now the fields can only be set by the `ego-tree` crate,
which can make sure to only ever use an ID that is in-bounds for a given tree.
@SimonSapin
Copy link
Contributor Author

I’ve added an intermediate commit with a SemVer-compatible version that keeps the fields public but deprecates them. Publishing both commits might be best to do now.

@causal-agent
Copy link
Collaborator

Great idea. Thanks

@causal-agent causal-agent merged commit e164eec into rust-scraper:master Dec 3, 2018
@SimonSapin SimonSapin deleted the out-of-bounds branch December 3, 2018 18:16
@causal-agent
Copy link
Collaborator

0.5.1 and 0.6.0 published

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