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

Unsoundness problem in Node::tree #169

Open
lwz23 opened this issue Dec 9, 2024 · 7 comments
Open

Unsoundness problem in Node::tree #169

lwz23 opened this issue Dec 9, 2024 · 7 comments
Labels
bug Something isn't working

Comments

@lwz23
Copy link

lwz23 commented Dec 9, 2024

Hello, thank you for your contribution in this project, I am scanning the unsoundness problem in rust project.
I notice the following code:

pub struct Node {
    // The actual tree we belong to. This is unsafe!!
    pub tree: *mut Slab<Node>,

    /// Our Id
    pub id: usize,
    /// Our parent's ID
    pub parent: Option<usize>,
................................
}
impl Node {
    pub fn tree(&self) -> &Slab<Node> {
        unsafe { &*self.tree }
    }
...........................
}

Considering that tree is a pub field, I assume that users can directly manipulate this field. This potential situation could result in self.tree being a null pointer, and directly dereferencing it might trigger undefined behavior (UB).
PoC:

extern crate blitz_dom;
extern crate style;

use blitz_dom::node::Node;
use std::ptr;
use style::{
    data::ElementData,
    properties::{parse_style_attribute, PropertyDeclarationBlock},
    servo_arc::Arc as ServoArc,
    shared_lock::{Locked, SharedRwLock},
    stylesheets::CssRuleType,
};
use blitz_dom::NodeData;
fn main() {
    let node = Node::new(ptr::null_mut(),0,SharedRwLock::new(),NodeData::Document);
    let _=node.tree();
}

If there is no external using for Node, maybe it should not be marked as pub, at least for its field should not be mark as pub.

@nicoburns
Copy link
Collaborator

Good point. I'll make it private. I think it was initially private, but was made public to support a cross-crate access that is no longer necessary.

@lwz23
Copy link
Author

lwz23 commented Dec 10, 2024

Hello again, I think maybe current fix is not sufficient :(
because I notice the following function:

pub fn new(tree: *mut Slab<Node>, id: usize, guard: SharedRwLock, data: NodeData) -> Self {

the user can still pass a Null pointer through the new method
maybe we should mark new method as private or add check to make sure the pointer is not null?

@nicoburns nicoburns reopened this Dec 10, 2024
@nicoburns nicoburns added the bug Something isn't working label Dec 12, 2024
@nicoburns
Copy link
Collaborator

I've made the new function private in e12a3c2. But I'll leave this open for now in case there are other issues. That tree pointer is definitely a source of unsafety I'd like to eliminate at some point. But Stylo's design makes that tricky.

@lwz23
Copy link
Author

lwz23 commented Dec 12, 2024

Thanks, I will keep search if there are other cases. If I found, I will report it in this issue.

@lwz23
Copy link
Author

lwz23 commented Dec 12, 2024

Also, given that this is an Unsound issue, I'm not sure if we should report it to RustSec?

@nicoburns
Copy link
Collaborator

Also, given that this is an Unsound issue, I'm not sure if we should report it to RustSec?

I think we should probably avoid that for now. Blitz doesn't have any real users yet, so they won't have run into it. And our wrapper libraries were not triggering the unsafety.

@lwz23
Copy link
Author

lwz23 commented Dec 12, 2024

Ok, I'm not quite familiar with the requirements of the report, thanks for clarification.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants