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
use crate::sealing;
use rustix::fs::{MemfdFlags, SealFlags};
use std::fs;
use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
#[derive(Clone, Debug)]
pub struct MemfdOptions {
allow_sealing: bool,
cloexec: bool,
hugetlb: Option<HugetlbSize>,
}
impl MemfdOptions {
pub const fn new() -> Self {
Self {
allow_sealing: false,
cloexec: true,
hugetlb: None,
}
}
pub const fn allow_sealing(mut self, value: bool) -> Self {
self.allow_sealing = value;
self
}
pub const fn close_on_exec(mut self, value: bool) -> Self {
self.cloexec = value;
self
}
pub const fn hugetlb(mut self, size: Option<HugetlbSize>) -> Self {
self.hugetlb = size;
self
}
fn bitflags(&self) -> MemfdFlags {
let mut bits = MemfdFlags::empty();
if self.allow_sealing {
bits |= MemfdFlags::ALLOW_SEALING;
}
if self.cloexec {
bits |= MemfdFlags::CLOEXEC;
}
if let Some(ref hugetlb) = self.hugetlb {
bits |= hugetlb.bitflags();
bits |= MemfdFlags::HUGETLB;
}
bits
}
pub fn create<T: AsRef<str>>(&self, name: T) -> Result<Memfd, crate::Error> {
let flags = self.bitflags();
let fd = rustix::fs::memfd_create(name.as_ref(), flags)
.map_err(Into::into)
.map_err(crate::Error::Create)?;
Ok(Memfd {
file: rustix::fd::FromFd::from_fd(fd.into()),
})
}
}
impl Default for MemfdOptions {
fn default() -> Self {
Self::new()
}
}
#[derive(Copy, Clone, Debug)]
pub enum HugetlbSize {
Huge64KB,
Huge512KB,
Huge1MB,
Huge2MB,
Huge8MB,
Huge16MB,
Huge256MB,
Huge1GB,
Huge2GB,
Huge16GB,
}
impl HugetlbSize {
const fn bitflags(self) -> MemfdFlags {
match self {
Self::Huge64KB => MemfdFlags::HUGE_64KB,
Self::Huge512KB => MemfdFlags::HUGE_512KB,
Self::Huge1MB => MemfdFlags::HUGE_1MB,
Self::Huge2MB => MemfdFlags::HUGE_2MB,
Self::Huge8MB => MemfdFlags::HUGE_8MB,
Self::Huge16MB => MemfdFlags::HUGE_16MB,
Self::Huge256MB => MemfdFlags::HUGE_256MB,
Self::Huge1GB => MemfdFlags::HUGE_1GB,
Self::Huge2GB => MemfdFlags::HUGE_2GB,
Self::Huge16GB => MemfdFlags::HUGE_16GB,
}
}
}
#[derive(Debug)]
pub struct Memfd {
file: fs::File,
}
impl Memfd {
pub fn try_from_fd<F>(fd: F) -> Result<Self, F>
where
F: AsRawFd + IntoRawFd,
{
if is_memfd(&fd) {
let file = unsafe { fs::File::from_raw_fd(fd.into_raw_fd()) };
Ok(Self { file })
} else {
Err(fd)
}
}
pub fn try_from_file(file: fs::File) -> Result<Self, fs::File> {
Self::try_from_fd(file)
}
pub const fn as_file(&self) -> &fs::File {
&self.file
}
pub fn into_file(self) -> fs::File {
self.file
}
pub fn seals(&self) -> Result<sealing::SealsHashSet, crate::Error> {
let flags = Self::file_get_seals(&self.file)?;
Ok(sealing::bitflags_to_seals(flags))
}
pub fn add_seal(&self, seal: sealing::FileSeal) -> Result<(), crate::Error> {
let flags = seal.bitflags();
self.add_seal_flags(flags)
}
pub fn add_seals<'a>(
&self,
seals: impl IntoIterator<Item = &'a sealing::FileSeal>,
) -> Result<(), crate::Error> {
let flags = sealing::seals_to_bitflags(seals);
self.add_seal_flags(flags)
}
fn add_seal_flags(&self, flags: rustix::fs::SealFlags) -> Result<(), crate::Error> {
rustix::fs::fcntl_add_seals(&self.file, flags)
.map_err(Into::into)
.map_err(crate::Error::AddSeals)?;
Ok(())
}
fn file_get_seals(fp: &fs::File) -> Result<SealFlags, crate::Error> {
let r = rustix::fs::fcntl_get_seals(fp)
.map_err(Into::into)
.map_err(crate::Error::GetSeals)?;
Ok(r)
}
}
impl FromRawFd for Memfd {
unsafe fn from_raw_fd(fd: RawFd) -> Self {
let file = fs::File::from_raw_fd(fd);
Self { file }
}
}
impl AsRawFd for Memfd {
fn as_raw_fd(&self) -> RawFd {
self.file.as_raw_fd()
}
}
impl IntoRawFd for Memfd {
fn into_raw_fd(self) -> RawFd {
self.into_file().into_raw_fd()
}
}
fn is_memfd<F: AsRawFd>(fd: &F) -> bool {
let fd = unsafe { rustix::fd::BorrowedFd::borrow_raw(fd.as_raw_fd()) };
rustix::fs::fcntl_get_seals(&fd).is_ok()
}