Merge pull request #62 from rust-windowing/seal-shrink

wayland: Seal memfd to prevent shrinking
This commit is contained in:
Jeremy Soller 2023-01-06 09:01:55 -07:00 committed by GitHub
commit ee3e6e8870
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -16,11 +16,22 @@ use super::State;
#[cfg(any(target_os = "linux", target_os = "freebsd"))] #[cfg(any(target_os = "linux", target_os = "freebsd"))]
fn create_memfile() -> File { fn create_memfile() -> File {
use nix::sys::memfd::{memfd_create, MemFdCreateFlag}; use nix::{
fcntl::{fcntl, FcntlArg, SealFlag},
sys::memfd::{memfd_create, MemFdCreateFlag},
};
let name = unsafe { CStr::from_bytes_with_nul_unchecked("softbuffer\0".as_bytes()) }; let name = unsafe { CStr::from_bytes_with_nul_unchecked("softbuffer\0".as_bytes()) };
let fd = memfd_create(name, MemFdCreateFlag::MFD_CLOEXEC) let fd = memfd_create(
.expect("Failed to create memfd to store buffer."); name,
MemFdCreateFlag::MFD_CLOEXEC | MemFdCreateFlag::MFD_ALLOW_SEALING,
)
.expect("Failed to create memfd to store buffer.");
let _ = fcntl(
fd,
FcntlArg::F_ADD_SEALS(SealFlag::F_SEAL_SHRINK | SealFlag::F_SEAL_SEAL),
)
.expect("Failed to seal memfd.");
unsafe { File::from_raw_fd(fd) } unsafe { File::from_raw_fd(fd) }
} }