1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
use serde::Deserialize;
use serde::Serialize;
use crate::fd::OpenFileId;
use crate::pid::DetTid;
/// Identity of a Linux memory address space (`mm_struct`).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct MmId {
creator: DetTid,
generation: u64,
}
impl MmId {
/// Deterministic task that created this model address-space incarnation.
/// This is an equality binding, not kernel memory-management attestation.
pub const fn creator(self) -> DetTid {
self.creator
}
/// Exact monotonic model exec generation, including initial generation zero.
pub const fn generation(self) -> u64 {
self.generation
}
/// Create the initial address space owned by a task.
pub const fn initial(creator: DetTid) -> Self {
Self {
creator,
generation: 0,
}
}
/// Inherit an address space for `CLONE_VM`, otherwise create a fresh identity.
pub const fn for_clone(parent: Self, child: DetTid, shares_vm: bool) -> Self {
if shares_vm {
parent
} else {
Self::initial(child)
}
}
/// Create the replacement address space installed by exec.
pub fn for_exec(self, creator: DetTid) -> Self {
Self {
creator,
// Exec generations must remain monotonic even when Linux changes a
// non-leader caller's TID to the process leader's TID.
generation: self.generation + 1,
}
}
}
/// Identity of an object that backs a process-shared memory mapping.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum SharedMemoryObjectId {
/// Anonymous shared mapping, identified by its deterministic allocation.
Anonymous {
/// Address space in which the mapping was created.
origin: MmId,
/// Per-address-space mapping sequence.
sequence: u64,
},
/// File-backed shared mapping.
File {
/// Device containing the file.
device: u64,
/// Inode of the file.
inode: u64,
},
/// Open-file-description fallback used when inode metadata is unavailable.
OpenFile { id: OpenFileId },
}
/// Identity of a futex word.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum FutexID {
/// Process-private futex, keyed by its address space and virtual address.
Private { mm: MmId, address: usize },
/// Process-shared futex, keyed by its backing object and byte offset.
Shared {
object: SharedMemoryObjectId,
offset: u64,
},
}
impl FutexID {
/// Create a private futex key.
pub const fn private(mm: MmId, address: usize) -> Self {
Self::Private { mm, address }
}
/// Create a process-shared futex key.
pub const fn shared(object: SharedMemoryObjectId, offset: u64) -> Self {
Self::Shared { object, offset }
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn clone_vm_controls_private_futex_aliasing() {
let parent = MmId::initial(DetTid::from_raw(10));
let child = DetTid::from_raw(11);
assert_eq!(
FutexID::private(parent, 0x1000),
FutexID::private(MmId::for_clone(parent, child, true), 0x1000),
"CLONE_VM tasks should use the same private futex key"
);
assert_ne!(
FutexID::private(parent, 0x1000),
FutexID::private(MmId::for_clone(parent, child, false), 0x1000),
"a copied address space should not alias the parent's private futex"
);
assert_ne!(
FutexID::private(parent, 0x1000),
FutexID::private(parent.for_exec(DetTid::from_raw(10)), 0x1000),
"exec should replace the private futex namespace"
);
}
#[test]
fn exec_generation_never_repeats_when_caller_identity_changes() {
let leader = DetTid::from_raw(10);
let worker = DetTid::from_raw(11);
let initial = MmId::initial(leader);
let worker_exec = initial.for_exec(worker);
let leader_exec = worker_exec.for_exec(leader);
assert_ne!(worker_exec, initial);
assert_ne!(leader_exec, initial);
assert_ne!(leader_exec, worker_exec);
}
#[test]
fn shared_futexes_alias_by_object_and_offset() {
let object = SharedMemoryObjectId::File {
device: 10,
inode: 20,
};
assert_eq!(
FutexID::shared(object, 64),
FutexID::shared(object, 64),
"virtual-address aliases of one backing offset must share a key"
);
assert_ne!(
FutexID::shared(object, 64),
FutexID::shared(object, 68),
"different words in one backing object need distinct keys"
);
assert_ne!(
FutexID::shared(object, 64),
FutexID::private(MmId::initial(DetTid::from_raw(10)), 64),
"private and shared namespaces must remain distinct"
);
}
}