Skip to content

Unsoundness problem in Node::tree #169

Open
@lwz23

Description

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions