Skip to content

Commit 165a15e

Browse files
committed
wayland: Seal memfd to prevent shrinking
I believe this should be possible wherever `memfd_create` is available. Sealing isn't required, but Wayland doesn't allow a client to shrink an shm pool, so there's no reason we should shrink the file. And if we mmap the file, this prevents a `SIGBUS` if the compositor (incorrectly) shrunk it. So we might as well do this.
1 parent 2cdbb48 commit 165a15e

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

src/wayland/buffer.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,22 @@ use super::State;
1616

1717
#[cfg(any(target_os = "linux", target_os = "freebsd"))]
1818
fn create_memfile() -> File {
19-
use nix::sys::memfd::{memfd_create, MemFdCreateFlag};
19+
use nix::{
20+
fcntl::{fcntl, FcntlArg, SealFlag},
21+
sys::memfd::{memfd_create, MemFdCreateFlag},
22+
};
2023

2124
let name = unsafe { CStr::from_bytes_with_nul_unchecked("softbuffer\0".as_bytes()) };
22-
let fd = memfd_create(name, MemFdCreateFlag::MFD_CLOEXEC)
23-
.expect("Failed to create memfd to store buffer.");
25+
let fd = memfd_create(
26+
name,
27+
MemFdCreateFlag::MFD_CLOEXEC | MemFdCreateFlag::MFD_ALLOW_SEALING,
28+
)
29+
.expect("Failed to create memfd to store buffer.");
30+
let _ = fcntl(
31+
fd,
32+
FcntlArg::F_ADD_SEALS(SealFlag::F_SEAL_SHRINK | SealFlag::F_SEAL_SEAL),
33+
)
34+
.expect("Failed to seal memfd.");
2435
unsafe { File::from_raw_fd(fd) }
2536
}
2637

0 commit comments

Comments
 (0)