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
cfg_if! {
if #[cfg(not(any(feature = "sim", feature = "hyper")))] {
pub use hw::*;
} else {
pub use sw::*;
}
}
#[cfg(not(any(feature = "sim", feature = "hyper")))]
mod hw {
use crate::arch::{self, Layout};
use crate::edmm::epc::{PageFlags, PageInfo, PageRange, PageType};
use crate::edmm::layout::LayoutTable;
use crate::edmm::perm;
use crate::edmm::trim;
use crate::elf::program::Type;
use crate::enclave::parse;
use crate::enclave::MmLayout;
use crate::feature::{SysFeatures, Version};
use core::convert::TryFrom;
use sgx_types::error::{SgxResult, SgxStatus};
use sgx_types::types::ProtectPerm;
pub fn apply_epc_pages(addr: usize, count: usize) -> SgxResult {
ensure!(addr != 0 && count != 0, SgxStatus::InvalidParameter);
if let Some(attr) = LayoutTable::new().check_dyn_range(addr, count, None) {
let pages = PageRange::new(
addr,
count,
PageInfo {
typ: PageType::Reg,
flags: PageFlags::R | PageFlags::W | PageFlags::PENDING,
},
)?;
if (attr.attr & arch::PAGE_DIR_GROW_DOWN) == 0 {
pages.accept_forward()
} else {
pages.accept_backward()
}
} else {
Err(SgxStatus::InvalidParameter)
}
}
pub fn trim_epc_pages(addr: usize, count: usize) -> SgxResult {
ensure!(addr != 0 && count != 0, SgxStatus::InvalidParameter);
LayoutTable::new()
.check_dyn_range(addr, count, None)
.ok_or(SgxStatus::InvalidParameter)?;
trim::trim_range(addr, count)?;
let pages = PageRange::new(
addr,
count,
PageInfo {
typ: PageType::Trim,
flags: PageFlags::MODIFIED,
},
)?;
pages.accept_forward()?;
trim::trim_range_commit(addr, count)?;
Ok(())
}
pub fn expand_stack_epc_pages(addr: usize, count: usize) -> SgxResult {
ensure!(addr != 0 && count != 0, SgxStatus::InvalidParameter);
LayoutTable::new()
.check_dyn_range(addr, count, None)
.ok_or(SgxStatus::InvalidParameter)?;
let pages = PageRange::new(
addr,
count,
PageInfo {
typ: PageType::Reg,
flags: PageFlags::R | PageFlags::W | PageFlags::PENDING,
},
)?;
pages.accept_forward()?;
Ok(())
}
#[inline]
pub fn accept_post_remove() -> SgxResult {
reentrant_accept_post_remove(arch::Global::get().layout_table(), 0)
}
fn reentrant_accept_post_remove(table: &[Layout], offset: usize) -> SgxResult {
let base = MmLayout::image_base();
unsafe {
for (i, layout) in table.iter().enumerate() {
if is_group_id!(layout.group.id) {
let mut step = 0_usize;
for _ in 0..layout.group.load_times {
step += layout.group.load_step as usize;
reentrant_accept_post_remove(
&table[i - layout.group.entry_count as usize..i],
step,
)?;
}
} else if (layout.entry.attributes & arch::PAGE_ATTR_POST_REMOVE) != 0 {
let addr = base + layout.entry.rva as usize + offset;
let count = layout.entry.page_count as usize;
let pages = PageRange::new(
addr,
count,
PageInfo {
typ: PageType::Trim,
flags: PageFlags::MODIFIED,
},
)?;
pages.accept_forward()?;
}
}
Ok(())
}
}
pub fn change_perm() -> SgxResult {
let elf = parse::new_elf()?;
let text_relo = parse::has_text_relo()?;
let base = MmLayout::image_base();
for phdr in elf.program_iter() {
let typ = phdr.get_type().unwrap_or(Type::Null);
if typ == Type::Load && text_relo && !phdr.flags().is_write() {
let mut perm = 0_u64;
let start = base + trim_to_page!(phdr.virtual_addr() as usize);
let end =
base + round_to_page!(phdr.virtual_addr() as usize + phdr.mem_size() as usize);
let count = (end - start) / arch::SE_PAGE_SIZE;
if phdr.flags().is_read() {
perm |= arch::SI_FLAG_R;
}
if phdr.flags().is_execute() {
perm |= arch::SI_FLAG_X;
}
modify_perm(start, count, perm as u8)?;
}
if typ == Type::GnuRelro {
let start = base + trim_to_page!(phdr.virtual_addr() as usize);
let end =
base + round_to_page!(phdr.virtual_addr() as usize + phdr.mem_size() as usize);
let count = (end - start) / arch::SE_PAGE_SIZE;
if count > 0 {
modify_perm(start, count, arch::SI_FLAG_R as u8)?;
}
}
}
let layout_table = arch::Global::get().layout_table();
if let Some(layout) = layout_table.iter().find(|layout| unsafe {
(layout.entry.id == arch::LAYOUT_ID_RSRV_MIN)
&& (layout.entry.si_flags == arch::SI_FLAGS_RWX)
&& (layout.entry.page_count > 0)
}) {
let start = base + unsafe { layout.entry.rva as usize };
let count = unsafe { layout.entry.page_count as usize };
modify_perm(start, count, (arch::SI_FLAG_R | arch::SI_FLAG_W) as u8)?;
}
Ok(())
}
fn modify_perm(addr: usize, count: usize, perm: u8) -> SgxResult {
let pages = PageRange::new(
addr,
count,
PageInfo {
typ: PageType::Reg,
flags: PageFlags::PR | PageFlags::from_bits_truncate(perm),
},
)?;
if SysFeatures::get().version() == Version::Sdk2_0 {
perm::modpr_ocall(
addr,
count,
ProtectPerm::try_from(perm).map_err(|_| SgxStatus::InvalidParameter)?,
)?;
}
pages.modify()
}
}
#[cfg(any(feature = "sim", feature = "hyper"))]
mod sw {
use sgx_types::error::SgxResult;
#[allow(clippy::unnecessary_wraps)]
#[inline]
pub fn apply_epc_pages(_addr: usize, _count: usize) -> SgxResult {
Ok(())
}
#[allow(clippy::unnecessary_wraps)]
#[inline]
pub fn trim_epc_pages(_addr: usize, _count: usize) -> SgxResult {
Ok(())
}
#[allow(clippy::unnecessary_wraps)]
#[inline]
pub fn expand_stack_epc_pages(_addr: usize, _count: usize) -> SgxResult {
Ok(())
}
}