[kernel/infra/list] Undefined Behavior in typed_ilist::test_list_push_and_pop_several due to sequential push reborrow invalidation

Aperta
#533 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
55/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
rust

Direzione di ricerca

Start in kernel/infra/src/list/typed_ilist.rs, read ListHead::insert_before and List::push, then run test_list_push_and_pop_several under Miri to confirm the reported diagnostic. Done means sequential pushes and the existing test pass without an aliasing Undefined Behavior report.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

bug

[!NOTE]
This issue is a follow-up to https://github.com/vivoblueos/kernel/issues/493. Per the maintainers' request to submit individual issues with minimal reproducible examples for each Undefined Behavior discovered during Miri dynamic analysis, we are submitting separate, focused reports with their corresponding unit tests and diagnostics.

Summary

In kernel/infra/src/list/typed_ilist.rs, List::push triggers an aliasing Undefined Behavior under Stacked Borrows when sequentially pushing elements into an intrusive list.

During insert_before, debug_assert_eq!(prev, Some(NonNull::from_mut(head))) generates a Unique function-entry reborrow on head. This invalidates the borrow stack tag associated with previous links, causing subsequent node dereferences to fail with invalid tag errors.

Test Case (kernel/infra/src/list/typed_ilist.rs)

The issue is triggered by the existing unit test test_list_push_and_pop_several:

#[test]
fn test_list_push_and_pop_several() {
    impl_simple_intrusive_adapter!(Node, A, node);
    struct A {
        node: ListHead<A, Node>,
        val: usize,
    }
    let mut l = List::<A, Node>::new();
    l.init();
    assert!(l.is_empty());
    let mut b0;
    let mut b1;
    {
        let mut val0 = A {
            node: ListHead::new(),
            val: 42,
        };
        let mut val1 = A {
            node: ListHead::new(),
            val: 43,
        };
        b0 = l.push(&mut val0).unwrap();
        b1 = l.push(&mut val1).unwrap();
        let mut index = 0;
        for v in l.iter() {
            match index {
                0 => {
                    assert_eq!(v.val, 42);
                }
                1 => {
                    assert_eq!(v.val, 43);
                }
                _ => {}
            }
            index += 1;
        }
    }
}

Undefined Behavior Diagnostic

Captured under standard Miri / Stacked Borrows:

test list::typed_ilist::tests::test_list_push_and_pop_several ... 
error: Undefined Behavior: trying to retag from <173764> for Unique permission at alloc54437[0x0],
but that tag does not exist in the borrow stack for this location
   --> /opt/sysroot/usr/local/lib/rustlib/src/rust/library/core/src/ptr/non_null.rs:483:18
    |
483 |         unsafe { &mut *self.as_ptr() }
    |                  ^^^^^^^^^^^^^^^^^^^ this error occurs as part of retag at alloc54437[0x0..0x10]
    |
    = help: this indicates a potential bug in the program: it performed an invalid operation, but the Stacked Borrows rules it violated are still experimental
    = help: see https://github.com/rust-lang/unsafe-code-guidelines/blob/master/wip/stacked-borrows.md for further information
help: <173764> was created by a SharedReadWrite retag at offsets [0x0..0x10]
   --> /opt/sysroot/usr/local/lib/rustlib/src/rust/library/core/src/ptr/non_null.rs:295:28
    |
295 |         unsafe { transmute(r as *mut T) }
    |                            ^
help: <173764> was later invalidated at offsets [0x0..0x10] by a Unique function-entry retag inside this call
   --> kernel/infra/src/list/typed_ilist.rs:155:41
    |
155 |             debug_assert_eq!(prev, Some(NonNull::from_mut(head)));
    |                                         ^^^^^^^^^^^^^^^^^^^^^^^
    = note: this is on thread `list::typed_ili`
    = note: stack backtrace:
            0: std::ptr::NonNull::<list::typed_ilist::ListHead<...>>::as_mut::<'_>
                at /opt/sysroot/usr/local/lib/rustlib/src/rust/library/core/src/ptr/non_null.rs:483:18: 483:37
            1: list::typed_ilist::ListHead::<...>::insert_before::{closure#0}
                at kernel/infra/src/list/typed_ilist.rs:179:41: 179:51
            2: std::option::Option::<...>::map_or::<...>
                at /opt/sysroot/usr/local/lib/rustlib/src/rust/library/core/src/option.rs:1228:24: 1228:28
            3: list::typed_ilist::ListHead::<...>::insert_before
                at kernel/infra/src/list/typed_ilist.rs:178:24: 180:15
            4: list::typed_ilist::List::<...>::push
                at kernel/infra/src/list/typed_ilist.rs:383:13: 383:58
            5: list::typed_ilist::tests::test_list_push_and_pop_several
                at kernel/infra/src/list/typed_ilist.rs:775:18: 775:35

Context & Root Cause

In ListHead::insert_before, reborrowing head via NonNull::from_mut(head) introduces a Unique permission retag over head. Because prev already points to the previous head and holds permissions derived earlier, establishing a new exclusive borrow of head invalidates active pointer provenance tags on previous list heads. When dereferencing self.as_mut() on the existing node, Miri rejects the operation because its tag was popped off the borrow stack.

Lingua principale
Rust
Stelle
1.3k
Fork
95
Merge medio
2g 12h
PR unite (30g)
29

Preparare l'ambiente

  • Include un Dockerfile o un file Docker Compose
  • Nessun modello di pull request
  • Nessuna guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di vivoblueos/kernel

Tutte le issue di vivoblueos/kernel

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.