Skip to content

Commit 7ecf282

Browse files
committed
feat: fall back to userfaultfd syscall on permission errors
1 parent aac58f5 commit 7ecf282

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
### Unreleased
2+
3+
- Fallback to `userfaultfd` syscall when opening `/dev/userfaultfd` fails due to permissions
4+
15
### 0.9.0
26

37
- Add support for `UFFDIO_CONTINUE` and `UFFDIO_REGISTER_MODE_MINOR` under the new `linux5_13` feature.

src/builder.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,17 +147,23 @@ impl UffdBuilder {
147147
// fall back to calling the system call.
148148
fn open_file_descriptor(&self, flags: i32) -> Result<Uffd> {
149149
// If `/dev/userfaultfd` exists we'll try to get the file descriptor from it. If the file
150-
// doesn't exist we will fall back to calling the system call. This means, that if the
151-
// device exists but the calling process does not have access rights to it, this will fail,
152-
// i.e. we will not fall back to calling the system call.
150+
// doesn't exist or we don't have the permissions to open it, we will fall back to calling
151+
// the system call. This means, that if the device exists but the calling process does not
152+
// have access rights to it, this will fail, i.e. we will not fall back to calling the
153+
// system call.
153154
match OpenOptions::new()
154155
.read(true)
155156
.write(true)
156157
.open(UFFD_DEVICE_PATH)
157158
{
158159
Ok(mut file) => self.uffd_from_dev(&mut file, flags),
159-
Err(err) if err.kind() == ErrorKind::NotFound => self.uffd_from_syscall(flags),
160-
Err(err) => Err(Error::OpenDevUserfaultfd(err)),
160+
Err(err) => {
161+
if let ErrorKind::NotFound | ErrorKind::PermissionDenied = err.kind() {
162+
self.uffd_from_syscall(flags)
163+
} else {
164+
Err(Error::OpenDevUserfaultfd(err))
165+
}
166+
}
161167
}
162168
}
163169

0 commit comments

Comments
 (0)