You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/behavior-not-considered-unsafe.md
+51-9Lines changed: 51 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,13 +6,6 @@ r[not-unsafe]
6
6
7
7
Rust does not consider the following behaviors _[unsafe]_, though a programmer may (or should) find them undesirable, unexpected, or erroneous.
8
8
9
-
r[not-unsafe.resource-leaks]
10
-
- Leaks of memory and other resources
11
-
r[not-unsafe.abort]
12
-
- Exiting without calling destructors
13
-
r[not-unsafe.aslr-bypass]
14
-
- Exposing randomized executable base addresses through pointer leaks
15
-
16
9
r[not-unsafe.deadlocks]
17
10
## Deadlocks and livelocks
18
11
@@ -62,6 +55,52 @@ In general, determining whether a program has deadlocked requires to solve the [
62
55
r[not-unsafe.livelocks]
63
56
Livelocks are a related issue where no real progress is made in a group of tasks, yet they technically continue to run. For instance, using non-blocking synchronization primitives like spinlocks or atomic variables can quickly lead to livelocks. This is in opposition to deadlocks, where tasks are blocked on resource acquisition, which is relatively easy to discern. Therefore, livelocks are much harder to detect than deadlocks, but equally undesirable.
64
57
58
+
r[not-unsafe.resource-leaks]
59
+
## Leaks of memory and other resources
60
+
61
+
A leaking resource is one that is never freed, even when it is no longer needed. Among the resources commonly leaked are memory, file descriptors, and networking sockets. Resource leaks may cause the program to accumulate unused resources until certain constraints by the environment are reached, such as the system running out of memory (OOM). In this case, a variety of non-normal behavior can occur, such as program termination by the operating system, common for OOM conditions, or failure to acquire further resources of the given type, common for file descriptors and sockets.
62
+
63
+
> [!EXAMPLE]
64
+
> Using reference counting via [Rc](`std::rc::Rc`), it is possible to construct a reference cycle, where two data structures point at each other via `Rc`. Since both hold a reference to the other, the `Rc`s always have a reference count of at least 1, and their inner values are never dropped: a memory leak. Note that while implementing the following scenario requires some unsafe code in the standard library, the safety conditions are never violated, and the program is correct as far as `Rc`'s and `RefCell`'s unsafe code is concerned.
> // a and b are never deallocated, even when dropped here.
84
+
> }
85
+
> ```
86
+
87
+
r[not-unsafe.abort]
88
+
## Exitingwithoutcallingdestructors
89
+
90
+
When [panicking], aprogrammayabortexecution; thatis, exitimmediatelywithoutfurtheractivities.Thereareseveralinstancesinwhichabortonpaniccanhappen, suchasadoublepanicorwithacompileroptiontoalwaysuse the abort strategy.
91
+
92
+
Unlike unwinding, which runs [destructors] for each object in each unwound frame, aborting does not run any destructors in any frame.Any code relying on certain destructors to run might therefore not function correctly.
93
+
94
+
r[not-unsafe.aslr-bypass]
95
+
## Exposing randomized executable base addresses through pointer leaks
96
+
97
+
[Address space layout randomization] (ASLR) is a security mitigation which moves program segments to randomized (virtual) memory addresses each time the program is started.Without knowing the absolute positions of code or data in a program, this complicates binary exploitation.
98
+
99
+
> [!NOTE]
100
+
> Whether the executable has ASLR support depends on the target, compiler flags, linker options, and more.Embedded targets usually do not support ASLR at all, while it is commonplace for "normal" targets on modern operating systems.Rust does not guarantee ASLR support, though the compiler and target documentation should specify their ASLR support.
101
+
102
+
When a pointer is leaked that has a fixed known offset to a segment base address, such as a [function pointer], an attacker can derive the base address regardless of ASLR, usually bypassing its protections.A leak may occur in many ways, such as by displaying the pointer’s numeric value to the user.
103
+
65
104
r[not-unsafe.integer-overflow]
66
105
## Integer overflow
67
106
@@ -103,13 +142,16 @@ Safe code may impose extra logical constraints that can be checked at neither co
103
142
> }
104
143
> }
105
144
> ```
106
-
107
-
Another example are data structures like [`BinaryHeap`](`alloc::collections::binary_heap::BinaryHeap`), [`BTreeMap`](`alloc::collections::btree_map::BTreeMap`), [`BTreeSet`](`alloc::collections::btree_set::BTreeSet`), [`HashMap`](`std::collections::HashMap`), and [`HashSet`](`std::collections::HashSet`), which describe constraints on the modification of their keys while they are in the data structure. Violating such constraints is not considered unsafe, yet the program is considered erroneous and its behavior unpredictable.
0 commit comments