Best practices for sharing memory with untrusted parties

Abierto
#607 15 comentarios 1 reacción 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
5/5
Tiempo estimado
Más de una semana
Aptitud para principiantes
25/100
Tipo de issue
Documentación
Claridad
Necesita aclaración
Estado de actividad
Tranquilo
Stack tecnológico
rust
Área
compilers

Línea de trabajo

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.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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).

Lenguaje dominante
Sin datos de lenguaje
Estrellas
760
Forks
68
Métricas de merge de PR
Sin PR fusionados en 30 d

Preparar el entorno

Este proyecto no incluye contenedor de desarrollo, Dockerfile ni guía de contribución, así que la configuración corre por tu cuenta: empieza por su README y consulta nuestra guía para la primera contribución para los pasos generales.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de rust-lang/unsafe-code-guidelines

Todos los issues de rust-lang/unsafe-code-guidelines

Issues similares

Más issues de Compilers

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.