Skip to content

Commit 62442b0

Browse files
yannickmoyhg-anssi
authored andcommitted
Fix typos/language issues in chapter 6
1 parent e4f6b3b commit 62442b0

File tree

3 files changed

+62
-61
lines changed

3 files changed

+62
-61
lines changed

src/en/unsafe/ffi.md

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ fn main() {
7474

7575
Typing is the way Rust ensures memory safety. When interfacing with other
7676
languages, which may not offer the same guarantee, the choice of types in the
77-
binding is essential to maintain the memory safety.
77+
binding is essential to maintain memory safety.
7878

7979
### Data layout
8080

@@ -134,7 +134,7 @@ The following types are considered C-compatible:
134134
- an `Option<T>` where `T` is either
135135
- `core::ptr::NonNull<U>` and `U` is a `Sized` C-compatible type, then it is
136136
compatible to a `*const T` and `*mut T` pointer;
137-
- `core::num::NonZero*`, then is compatible to the corresponding integral
137+
- `core::num::NonZero*`, then is compatible with the corresponding integral
138138
primitive type;
139139
- a `repr(transparent)`-annotated `struct` with only one field, where that
140140
field has a C-compatible type.
@@ -146,9 +146,9 @@ The following types are not C-compatible:
146146
- Enums with fields,
147147
- Tuples (but `repr(C)` tuple structures are OK).
148148
149-
Some types are compatibles with some caveats:
149+
Some types are compatible with some caveats:
150150
151-
- Zero-sized types, which is really zero sized (which is let unspecified in C
151+
- Zero-sized types, which is really zero sized (which is left unspecified in C
152152
and contradicts the C++ specification),
153153
- `repr(C)`, `repr(C, Int)`, or `repr(Int)`-annotated enum with fields
154154
(see [RFC 2195]).
@@ -219,7 +219,7 @@ the C standard library.
219219
>
220220
> In a secure Rust development, when interfacing with foreign code that
221221
> uses platform-dependent types, such as C's `int` and `long`, Rust code must
222-
> use portable type aliases, such as provided by the standard library or the
222+
> use portable type aliases, such as the ones provided by the standard library or the
223223
> [libc] crate, rather than platform-specific types, except if
224224
> the binding is automatically generated for each platform (see Note below).
225225
@@ -255,23 +255,23 @@ the C-compatible types:
255255
- references,
256256
- function pointers,
257257
- enums,
258-
- floats (even if almost every language have the same understanding of what is
258+
- floats (even if almost all languages have the same understanding of what is
259259
a valid float),
260260
- compound types that contain a field of a non-robust type.
261261
262262
On the other hand, integer types (`u*`/`i*`), packed compound types that contain
263-
no non-robust fields, for instance are *robust types*.
263+
no non-robust fields, are instances of *robust types*.
264264
265265
Non-robust types are a difficulty when interfacing two languages. It revolves
266-
into deciding **which language of the two is responsible in asserting the
266+
around deciding **which language of the two is responsible for asserting the
267267
validity of boundary-crossing values** and how to do it.
268268
269269
> **Rule {{#check FFI-CKNONROBUST | Do not use unchecked non-robust foreign values}}**
270270
>
271271
> In a secure Rust development, there must not be any use of *unchecked* foreign
272272
> values of non-robust types.
273273
>
274-
> In other words, either Rust translates robust types to non-robust types
274+
> In other words, either Rust translates robust types into non-robust types
275275
> through explicit checking or the foreign side offers strong guarantees of the
276276
> validity of the value.
277277
@@ -283,7 +283,7 @@ validity of boundary-crossing values** and how to do it.
283283
> be done in Rust when possible.
284284
285285
Those generic rules are to be adapted to a specific foreign language or for the
286-
associated risks. Concerning languages, C is particularly unfit to offer
286+
associated risks. Concerning languages, C is particularly unfit for offering
287287
guarantees about validity. However, Rust is not the only language to offer
288288
strong guarantees. For instance, some C++ subset (without reinterpretation)
289289
allows developers to do lot of type checking. Because Rust natively separates
@@ -293,7 +293,7 @@ function references, and enums, and are discussed below.
293293
294294
> **Warning**
295295
>
296-
> Rust's `bool` has been made equivalent to C99's `_Bool` (aliased as `bool`
296+
> Rust `bool` has been made equivalent to C99's `_Bool` (aliased as `bool`
297297
> in `<stdbool.h>`) and C++'s `bool`. However, loading a value other than 0 and
298298
> 1 as a `_Bool`/`bool` is an undefined behavior *on both sides*.
299299
> Safe Rust ensures that. Standard-compliant C and C++ compilers ensure that no
@@ -305,7 +305,7 @@ function references, and enums, and are discussed below.
305305
#### References and pointers
306306
307307
Although they are allowed by the Rust compiler, the use of Rust references in
308-
FFI may break Rust's memory safety. Because their “unsafety” is more explicit,
308+
FFI may break Rust memory safety. Because their “unsafety” is more explicit,
309309
pointers are preferred over Rust references when binding to another language.
310310
311311
On the one hand, reference types are very non-robust: they allow only pointers
@@ -324,16 +324,16 @@ Rust references may be used reasonably with other C-compatible languages
324324
including C variants allowing for non-null type checking, e.g. Microsoft SAL
325325
annotated code.
326326
327-
On the other hand, Rust's *pointer types* may also lead to undefined behaviors
327+
On the other hand, Rust *pointer types* may also lead to undefined behaviors
328328
but are more verifiable, mostly against `std/core::ptr::null()` (C's `(void*)0`)
329-
but also in some context against a known valid memory range (particularly in
329+
but also in some contexts against a known valid memory range (particularly in
330330
embedded systems or kernel-level programming). Another advantage of using Rust
331331
pointers in FFI is that any load of the pointed value is clearly marked inside
332332
an `unsafe` block or function.
333333
334334
> **Recommendation {{#check FFI-NOREF | Do not use reference types but pointer types}}**
335335
>
336-
> In a secure Rust development, the Rust code should not use references types
336+
> In a secure Rust development, the Rust code should not use reference types
337337
> but pointer types.
338338
>
339339
> Exceptions include:
@@ -349,11 +349,11 @@ an `unsafe` block or function.
349349
350350
> **Rule {{#check FFI-CKREF | Do not use unchecked foreign references}}**
351351
>
352-
> In a secure Rust development, every foreign references that is transmitted to
352+
> In a secure Rust development, every foreign reference that is transmitted to
353353
> Rust through FFI must be **checked on the foreign side** either automatically
354354
> (for instance, by a compiler) or manually.
355355
>
356-
> Exceptions include Rust references in an opaque wrapping that is created
356+
> Exceptions include Rust references in an opaque wrapping that are created
357357
> and manipulated only from the Rust side and `Option`-wrapped references
358358
> (see Note below).
359359
@@ -365,7 +365,7 @@ an `unsafe` block or function.
365365
> pointer must check their validity beforehand.
366366
> In particular, pointers must be checked to be non-null before any use.
367367
>
368-
> Stronger approaches are advisable when possible. They includes checking
368+
> Stronger approaches are advisable when possible. They include checking
369369
> pointers against known valid memory range or tagging (or signing) pointers
370370
> (particularly applicable if the pointed value is only manipulated from Rust).
371371
@@ -415,7 +415,7 @@ int main() {
415415
#### Function pointers
416416
417417
Function pointers that cross FFI boundaries may ultimately lead to arbitrary code
418-
execution and represents a real security risks.
418+
execution and represent a real security risks.
419419
420420
> **Rule {{#check FFI-MARKEDFUNPTR | Mark function pointer types in FFI as `extern` and `unsafe`}}**
421421
>
@@ -458,8 +458,8 @@ possibilities:
458458
> In a secure Rust development, any foreign function pointer must be checked at
459459
> the FFI boundary.
460460
461-
When binding with C or even C++, one cannot guarantee easily the validity of the
462-
function pointer. C++ functors are not C-compatible.
461+
When binding with C or even C++, one cannot guarantee easily the validity of
462+
function pointers. C++ functors are not C-compatible.
463463
464464
#### Enums
465465
@@ -469,14 +469,14 @@ respect to the number of possible bit patterns of the same size. Mishandling an
469469
severe consequences on software security. Unfortunately, checking an `enum`
470470
value at the FFI boundary is not simple on both sides.
471471
472-
On the Rust side, it consists to actually use an integer type in the `extern`
473-
block declaration, a *robust* type, and then to perform a checked conversion
472+
On the Rust side, it consists in actually using an integer type in the `extern`
473+
block declaration, a *robust* type, and then performing a checked conversion
474474
to the enum type.
475475
476476
On the foreign side, it is possible only if the other language allows for
477477
stricter checks than plain C. `enum class` in C++ are for instance allowable.
478-
Note however that as for reference the actual `extern "C"` ABI of
479-
`enum class` is implementation defined and should be verified for each
478+
Note however that as for a reference, the actual `extern "C"` ABI of
479+
`enum class` is implementation-defined and should be verified for each
480480
environment.
481481
482482
> **Recommendation {{#check FFI-NOENUM | Do not use incoming Rust `enum` at FFI boundary}}**
@@ -490,8 +490,8 @@ environment.
490490
> Rust side,
491491
> - bound to safe enums in the foreign language, e.g. `enum class` types in C++.
492492
493-
Concerning fieldless enums, crates like [`num_derive`] or [`num_enum`] allows
494-
developer to easily provide safe conversion from integer to enumeration and may
493+
Concerning fieldless enums, crates like [`num_derive`] or [`num_enum`] allow
494+
developers to easily provide safe conversions from integer to enumeration and may
495495
be use to safely convert an integer (provided from a C `enum`) into a Rust enum.
496496
497497
[num_derive]: https://crates.io/crates/num_derive
@@ -517,7 +517,7 @@ extern "C" {
517517
}
518518
```
519519

520-
The not yet implemented [RFC 1861] proposes to facilitate the coding by allowing
520+
The not-yet-implemented [RFC 1861] proposes to facilitate this encoding by allowing
521521
to declare opaque types in `extern` blocks.
522522

523523
[RFC 1861]: https://rust-lang.github.io/rfcs/1861-extern-types.html
@@ -526,7 +526,7 @@ to declare opaque types in `extern` blocks.
526526
>
527527
> In a secure Rust development, when interfacing with C or C++, Rust types that
528528
> are to be considered opaque in C/C++ should be translated as incomplete
529-
> `struct` type (i,e., declared without definition) and be provided with
529+
> `struct` types (i,e., declared without definition) and be provided with
530530
> a dedicated constructor and destructor.
531531
532532
Example of opaque Rust type:
@@ -604,7 +604,7 @@ wrapper around the foreign type:
604604
> In a secure Rust development, any non-sensitive foreign piece of data that are
605605
> allocated and deallocated in the foreign language should be encapsulated in a
606606
> `Drop` type in such a way as to provide automatic deallocation in Rust,
607-
> through an automatic call to the foreing language deallocation routine.
607+
> through an automatic call to the foreign language deallocation routine.
608608
609609
A simple example of Rust wrapping over an external opaque type:
610610

@@ -664,18 +664,18 @@ impl Drop for Foo {
664664
> is not sufficient for sensitive deallocation (such as wiping sensitive data)
665665
> except if the code is guaranteed to never panic.
666666
>
667-
> For wiping sensitive data case, one could address the issue with a dedicated
667+
> For wiping sensitive data, one could address the issue with a dedicated
668668
> panic handler.
669669
670670
When the foreign language is the one exploiting Rust allocated resources, it is
671671
a lot more difficult to offer any guarantee.
672672

673-
In C for instance there is no easy way to check that the appropriate destructor
674-
is checked. A possible approach is to exploit callbacks to ensure that the
673+
In C for instance, there is no easy way to check that the appropriate destructor
674+
is called. A possible approach is to exploit callbacks to ensure that the
675675
reclamation is done.
676676

677677
The following Rust code is a **thread-unsafe** example of a C-compatible API
678-
that provide callback to ensure safe resource
678+
that provides a callback to ensure safe resource
679679
reclamation:
680680

681681
```rust,noplaypen

src/en/unsafe/generalities.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## *Unsafe* capacities
44

5-
Language capabilities can be extended using unsafe code. The full list of these capacities is given in the [Rust reference](https://doc.rust-lang.org/reference/unsafety.html). Notice the following one's.
5+
Language capabilities can be extended using unsafe code. The full list of these capacities is given in the [Rust reference](https://doc.rust-lang.org/reference/unsafety.html). Notice the following ones.
66

77
* Dereference a raw pointer
88
* Modify a static mutable variable
@@ -36,14 +36,14 @@ manipulations of memory pointers, the language provides the `unsafe` keyword.
3636

3737
> **Rule {{#check LANG-UNSAFE | Don't use unsafe blocks}}**
3838
>
39-
> For a secured development, the `unsafe` blocks must be avoided. Afterward,
39+
> In a secure Rust development, the `unsafe` blocks must be avoided. In the following,
4040
> we list the only cases where `unsafe` may be used, provided that they come
4141
> with a proper justification:
4242
>
43-
> - The Foreign Function Interface (FFI) of Rust allows for describing
43+
> - The Foreign Function Interface (FFI) of Rust allows describing
4444
> functions whose implementations are written in C, using the `extern "C"`
4545
> prefix. To use such a function, the `unsafe` keyword is required. “Safe”
46-
> wrapper shall be defined to safely and seamlessly call C code.
46+
> wrappers shall be defined to safely and seamlessly call C code.
4747
>
4848
> - For embedded device programming, registers and various other resources are
4949
> often accessed through a fixed memory address. In this case, `unsafe` blocks
@@ -121,14 +121,14 @@ Consequently, even *safe* function must be handled carefully in *unsafe* context
121121

122122
#### Example
123123

124-
Suppose one wants to propose an API find object of a given type in the memory.
124+
Suppose one wants to propose an API to find objects of a given type in the memory.
125125
This API could ask implementing the following trait
126126

127127
```rust
128128
trait Locatable {
129-
/// Find object of type `Self` in the buffer `buf`.
130-
/// Returns the index of the first byte representing
131-
/// an object of type `Self`
129+
/// Find objects of type `Self` in the buffer `buf`.
130+
/// Return the index of the first byte representing
131+
/// an object of type `Self`.
132132
fn locate_instance_into(buf: &[u8]) -> Option<usize>;
133133
}
134134
```
@@ -167,11 +167,11 @@ fn locate<T: Locatable + Clone>(start: *const u8, len: usize) -> Option<T> {
167167
This implementation is harmful for two reasons:
168168

169169
* If the `Locatable` implementation gives the wrong index, the `as_ref` function produce an *UB*.
170-
* If the `Locatable` implementation gives an outbound index, the subsequent buffer overflow is an *UB*.
170+
* If the `Locatable` implementation gives an out-of-bounds index, the subsequent buffer overflow is an *UB*.
171171

172172
</div>
173173

174-
For instance, the following `Locatable` implementation is wrong **but** it is of the responsibility of the API maker to take it into account.
174+
For instance, the following `Locatable` implementation is wrong **but** it is the responsibility of the API maker to take it into account.
175175

176176
```rust
177177
impl Locatable for bool {
@@ -181,7 +181,7 @@ impl Locatable for bool {
181181
}
182182
```
183183

184-
The following program produces *UB*.
184+
The following program produces a *UB*.
185185

186186
```rust,should_panic
187187
fn main() {
@@ -246,7 +246,7 @@ in case these *safe* functions have bad behavior.
246246
If they cannot protect their function against badly implemented *safe* functions/traits, they could either
247247

248248
* mark the function they *write* as `unsafe`: thus it is the user's responsibility to feed this function with correct arguments (by checking *unsafe* function documentation).
249-
* mark the traits they *use* as `unsafe` : thus it is user's responsibility to simplement the trait properly (again reading the trait documentation).
249+
* mark the traits they *use* as `unsafe` : thus it is user's responsibility to implement the trait properly (again reading the trait documentation).
250250

251251
#### Références
252252

src/en/unsafe/memory.md

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ forget(s); // Leak memory
3939
```
4040

4141
In particular, using `forget` may result in not releasing critical resources
42-
leading to deadlocks or not erasing sensitive data from the memory. That is why,
42+
leading to deadlocks or not erasing sensitive data from the memory. This is why
4343
`forget` is **unsecure**.
4444

4545
> **Rule {{#check MEM-FORGET | Do not use `forget`}}**
@@ -86,9 +86,9 @@ compiler to the developer.
8686
8787
## Raw pointers
8888
89-
This pointers are mainly used to use C pointer. They do not have the same protections
90-
than *smart pointers* and often have to be used in `unsafe` context. For instance, freeing
91-
raw pointer must be done manually without Rust warranties.
89+
These pointers are mainly used for C pointers. They do not have the same protections
90+
as *smart pointers* and often have to be used in `unsafe` context. For instance, freeing
91+
raw pointers must be done manually without Rust guaranties.
9292
9393
> **Rule {{#check MEM-NORAWPOINTER | Do no convert smart pointer into raw pointer in Rust without `unsafe`}}**
9494
>
@@ -118,12 +118,13 @@ raw pointer must be done manually without Rust warranties.
118118
> let _ = unsafe { Box::from_raw(raw_ptr) }; // will be freed
119119
> ```
120120
121-
Converse is true! That is `from_raw` should be call **only** on `into_raw`ed value. For instance,
122-
`Rc` smart pointer [explicitly request for this condition](https://doc.rust-lang.org/std/rc/struct.Rc.html#method.from_raw)
123-
and, for `Box` smart pointer, conversion of C pointer into `Box` is [discouraged](https://doc.rust-lang.org/std/boxed/index.html#memory-layout).
121+
The converse is also true! That is, `from_raw` should be call **only** on `into_raw`ed value. For instance,
122+
`Rc` smart pointers [explicitly request for this condition](https://doc.rust-lang.org/std/rc/struct.Rc.html#method.from_raw)
123+
and, for `Box` smart pointers, conversion of C pointers into `Box` is [discouraged](https://doc.rust-lang.org/std/boxed/index.html#memory-layout).
124124
125-
> **Règle {{#check MEM-INTOFROMRAW | Call `from_raw` *only* on `into_raw`ed value}}**
126-
> In a secure Rust development, `from_raw` should only be called on `into_raw`ed value
125+
> **Rule {{#check MEM-INTOFROMRAW | Call `from_raw` *only* on `into_raw`ed value}}**
126+
>
127+
> In a secure Rust development, `from_raw` should only be called on `into_raw`ed values.
127128
128129
<!-- -->
129130
@@ -151,7 +152,7 @@ and, for `Box` smart pointer, conversion of C pointer into `Box` is [discouraged
151152
## Uninitialized memory
152153
153154
By default, Rust forces all values to be initialized, preventing the use of
154-
uninitialized memory (except if using `std::mem::uninitialized` or
155+
uninitialized memory (except when using `std::mem::uninitialized` or
155156
`std::mem::MaybeUninit`).
156157
157158
> **Rule {{#check MEM-UNINIT | Do not use uninitialized memory}}**
@@ -170,14 +171,14 @@ The use of uninitialized memory may result in two distinct security issues:
170171
> `std::mem::MaybeUninit` is an improvement over `std::mem::uninitialized`.
171172
> Indeed, it makes dropping uninitialized values a lot more difficult.
172173
> However, it does not change the second issue: the non-drop of an initialized
173-
> memory is as much likely. It is problematic, in particular when considering
174+
> memory remains. It is problematic, in particular when considering
174175
> the use of `Drop` to erase sensitive memory.
175176
176177
## Cyclic reference counted pointers (`Rc` and `Arc`)
177178
178-
Combining [interior mutability](https://doc.rust-lang.org/reference/interior-mutability.html), recurcivity and reference counted pointer into type definitions is unsafe. It can produce memory leaks which can result in DDoS attack or secret leaks.
179+
Combining [interior mutability](https://doc.rust-lang.org/reference/interior-mutability.html), recursivity and reference counted pointer into type definitions is unsafe. It can produce memory leaks which can result in DDoS attacks or leaking secrets.
179180
180-
The following example show such a memory leak in safe Rust:
181+
The following example shows such a memory leak in safe Rust:
181182
182183
```rust
183184
use std::{cell::Cell, rc::Rc};
@@ -235,6 +236,6 @@ Hello, world!
235236
==153637== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
236237
```
237238

238-
> **Règle {{#check MEM-MUT-REC-RC | Avoid cyclic reference counted pointers}}**
239+
> **Rule {{#check MEM-MUT-REC-RC | Avoid cyclic reference counted pointers}}**
239240
>
240-
> Avoid recursive types whose recursivity use reference counted pointers together with interior mutability.
241+
> Avoid recursive types whose recursivity uses reference counted pointers together with interior mutability.

0 commit comments

Comments
 (0)