Best practices for sharing memory with untrusted parties

Đang mở
#607 15 bình luận 1 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức phù hợp với người mới
25/100
Loại issue
Tài liệu
Độ rõ ràng
Cần làm rõ
Mức độ hoạt động
Ít trao đổi
Công nghệ
rust
Lĩnh vực
compilers

Hướng nghiên cứu

Read the linked unsafe-code-guidelines issues #152, #321, #350, and #606, along with Rust issues #154321 and PR #141260, to understand the competing volatile and atomic-access models. Done means reaching a documented consensus on safe best practices for sharing memory with untrusted parties.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

This was the starting point for https://github.com/rust-lang/unsafe-code-guidelines/issues/152. That got closed saying "volatile is for MMIO", which is further discussed in #321. However, since then we got "also volatile can access memory outside the AM" (https://github.com/rust-lang/rust/pull/141260) which changes the picture again IMO. The question also recently came up in https://github.com/rust-lang/rust/issues/154321.

I would say the safest (least AM/model-breaking) way to share memory with an untrusted party is to never make that memory part of the AM. This avoids any trouble related to memory models or compiler assumptions. If this isn't actual "memory" in the first place, there can't be any assumptions. And now that volatile accesses are allowed to access such "non-memory", this is actually a viable option. I think this also explains why people are naturally drawn towards using volatile for this kind of interaction. If LLVM changes volatile loads to no longer be willreturn (which @nikic says is on the table), this model even allows reads and writes on memory that might get unmapped at any time. (Alternatively, https://github.com/rust-lang/unsafe-code-guidelines/issues/606 discusses ways for such segfaults to be valid everywhere, that'd also implicitly cover volatile accesses.)

For a Rust program this means only using raw pointers, never creating a reference, and only using read_volatile and write_volatile. Those don't even have to be atomic; all concurrency behavior is hidden behind the opaqueness of volatile accesses. (Atomic volatile accesses are useful if the party those accesses are communicating with also has access to actual Rust memory. That's not the case for this discussion.) It currently also means never using offset or field projections but we could fix that (https://github.com/rust-lang/unsafe-code-guidelines/issues/350) -- the LLVM IR we generate is already okay here, we "just" have to update our Rust-level model.


The other model we have discussed in the past is to have that shared memory be actual Rust memory and model whatever the untrusted code does as atomic accesses. This means the Rust code must also use atomic accesses. This seems to work fine in practice but the model actually has a gaping hole -- mixed-size accesses are UB, so the untrusted code can still cause UB in the Rust code. The advantage of this model is that the Rust code is allowed to use shared references (as long as all bytes of the pointee type are inside an UnsafeCell).

Ngôn ngữ chính
Không có dữ liệu ngôn ngữ
Star
760
Fork
68
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của rust-lang/unsafe-code-guidelines

Tất cả issue của rust-lang/unsafe-code-guidelines

Issue tương tự

Thêm issue về Compilers

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.