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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
use alloc::vec::Vec;
use crate::error::KernelError;
use failure::Backtrace;
use bit_field::BitField;
use bit_field::BitArray;
use core::fmt;
use core::convert::TryInto;
pub struct ProcessCapabilities {
pub syscall_mask: [u32; 256 / (8 * 4)],
pub irq_access_mask: [u8; 128],
pub ioports: Vec<u16>,
}
struct MaskPrinter<'a, T>(&'a [T]);
impl<'a, T: BitField> fmt::Debug for MaskPrinter<'a, T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_list()
.entries(self.0.iter().enumerate().flat_map(|(idx, v)| {
(0..T::BIT_LENGTH)
.filter(move |x| v.get_bit(*x))
.map(move |x| idx * T::BIT_LENGTH + x)
}))
.finish()
}
}
impl fmt::Debug for ProcessCapabilities {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ProcessCapabilities")
.field("syscall_mask", &MaskPrinter(&self.syscall_mask))
.field("irq_access_mask", &MaskPrinter(&self.irq_access_mask))
.field("ioports", &self.ioports)
.finish()
}
}
const KERNEL_FLAGS: u32 = 3;
const SYSCALL_MASK: u32 = 4;
const MAP_IO_OR_NORMAL_RANGE: u32 = 6;
const MAP_NORMAL_PAGE: u32 = 7;
const INTERRUPT_PAIR: u32 = 11;
const APPLICATION_TYPE: u32 = 13;
const KERNEL_RELEASE_VERSION: u32 = 14;
const HANDLE_TABLE_SIZE: u32 = 15;
const DEBUG_FLAGS: u32 = 16;
const IO_PORTS_ALLOWED: u32 = 10;
const MAX_SVC: usize = ::sunrise_libkern::nr::MaxSvc;
const KACS_NO_DUPLICATES: u32 = 0
| 1 << KERNEL_FLAGS
| 1 << APPLICATION_TYPE
| 1 << KERNEL_RELEASE_VERSION
| 1 << HANDLE_TABLE_SIZE
| 1 << DEBUG_FLAGS;
impl Default for ProcessCapabilities {
fn default() -> Self {
ProcessCapabilities {
syscall_mask: [0; 256 / (8 * 4)],
irq_access_mask: [0; 128],
ioports: Vec::new(),
}
}
}
impl ProcessCapabilities {
pub fn parse_kcaps(kacs: &[u8]) -> Result<ProcessCapabilities, KernelError> {
let mut capabilities = ProcessCapabilities {
syscall_mask: [0; 256 / (8 * 4)],
irq_access_mask: [0; 128],
ioports: Vec::new(),
};
let mut kac_iter = kacs.chunks(4);
let mut duplicate_kacs = 0;
let mut duplicate_svc = 0;
while let Some(kac) = kac_iter.next() {
let kac = u32::from_le_bytes(kac.try_into().expect("Unexpectted kac size"));
let kac_type = (!kac).trailing_zeros();
if duplicate_kacs.get_bit(kac_type as _) && KACS_NO_DUPLICATES.get_bit(kac_type as _) {
return Err(KernelError::InvalidCombination {
backtrace: Backtrace::new(),
});
}
duplicate_kacs.set_bit(kac_type as _, true);
match kac_type {
KERNEL_FLAGS => {
let lowest_allowed_prio = kac.get_bits(4..10);
let highest_allowed_prio = kac.get_bits(10..16);
let lowest_allowed_cpu = kac.get_bits(16..24);
let highest_allowed_cpu = kac.get_bits(24..32);
if lowest_allowed_prio > highest_allowed_prio {
return Err(KernelError::InvalidCombination {
backtrace: Backtrace::new(),
})
}
if lowest_allowed_cpu > highest_allowed_cpu {
return Err(KernelError::InvalidCombination {
backtrace: Backtrace::new(),
})
}
},
SYSCALL_MASK => {
let mask = kac.get_bits(5..29);
let index = kac.get_bits(29..32);
if duplicate_svc.get_bit(index as _) {
return Err(KernelError::InvalidCombination {
backtrace: Backtrace::new()
});
}
duplicate_svc.set_bit(index as _, true);
let index = index as usize * 24;
if let Some(highest_svc_in_mask) = 23usize.checked_sub(mask.leading_zeros() as usize - 8) {
if index + highest_svc_in_mask > MAX_SVC {
return Err(KernelError::ExceedingMaximum {
maximum: MAX_SVC as u64,
value: (index + highest_svc_in_mask) as u64,
backtrace: Backtrace::new()
});
}
}
capabilities.syscall_mask.set_bits(index..index + 24, mask);
}
MAP_IO_OR_NORMAL_RANGE => {
let _start_page = kac.get_bits(7..31);
let _is_ro = kac.get_bit(31);
if let Some(kac) = kac_iter.next() {
let kac = u32::from_le_bytes(kac.try_into().expect("Unexpectted kac size"));
if (!kac).trailing_zeros() == MAP_IO_OR_NORMAL_RANGE {
let _num_pages = kac.get_bits(7..31);
let _is_io = kac.get_bit(31);
continue;
}
}
return Err(KernelError::InvalidCombination {
backtrace: Backtrace::new()
});
},
MAP_NORMAL_PAGE => {
let _page = kac.get_bits(8..32);
},
INTERRUPT_PAIR => {
let irq0 = kac.get_bits(12..22) as usize;
let irq1 = kac.get_bits(22..32) as usize;
if irq0 != 0x3FF {
if irq0 > 0xFF {
return Err(KernelError::ExceedingMaximum {
maximum: 0xFF,
value: irq0 as u64,
backtrace: Backtrace::new(),
})
}
capabilities.irq_access_mask.set_bit(irq0, true);
}
if irq1 != 0x3FF {
if irq0 > 0xFF {
return Err(KernelError::ExceedingMaximum {
maximum: 0xFF,
value: irq1 as u64,
backtrace: Backtrace::new(),
})
}
capabilities.irq_access_mask.set_bit(irq1, true);
}
},
APPLICATION_TYPE => {
let _app_type = kac.get_bits(14..17);
if kac.get_bits(17..32) != 0 {
return Err(KernelError::ReservedValue {
backtrace: Backtrace::new()
})
}
},
KERNEL_RELEASE_VERSION => {
let _version = kac.get_bits(15..32);
}
HANDLE_TABLE_SIZE => {
let _handle_table_size = kac.get_bits(16..26);
if kac.get_bits(26..32) != 0 {
return Err(KernelError::ReservedValue {
backtrace: Backtrace::new()
})
}
}
DEBUG_FLAGS => {
let _can_be_debugged = kac.get_bit(17);
let _can_debug_others = kac.get_bit(18);
if kac.get_bits(19..32) != 0 {
return Err(KernelError::ReservedValue {
backtrace: Backtrace::new()
})
}
}
IO_PORTS_ALLOWED => {
let ioport = kac.get_bits(11..27) as u16;
if kac.get_bits(27..32) != 0 {
return Err(KernelError::ReservedValue {
backtrace: Backtrace::new()
})
}
capabilities.ioports.push(ioport);
}
_ => {
return Err(KernelError::InvalidKernelCaps {
kcap: kac,
backtrace: Backtrace::new(),
})
}
}
}
Ok(capabilities)
}
}