Skip to content

Commit e474781

Browse files
hoshinolinaslp
authored andcommitted
x11bridge: Fix shmem read race & stale shm files
Without the patched libxshmfence, a failed ptrace replacement due to e.g. `strace glxgears` would leak the shm file. Fix this and the slightly racy shm data copy in one go, by moving it into replace_futex_storage() and removing the file before checking for errors. Signed-off-by: Asahi Lina <[email protected]>
1 parent f8b610e commit e474781

1 file changed

Lines changed: 15 additions & 6 deletions

File tree

crates/muvm/src/x11bridge/bin/muvm-x11bridge.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,8 +1094,18 @@ impl Client {
10941094
Ok(tids)
10951095
}
10961096

1097-
fn replace_futex_storage(my_fd: RawFd, pid: Pid, shmem_path: &str) -> Result<()> {
1097+
fn replace_futex_storage(
1098+
my_fd: RawFd,
1099+
pid: Pid,
1100+
shmem_path: &str,
1101+
shmem_file: &mut File,
1102+
) -> Result<()> {
10981103
let traced = Self::ptrace_all_threads(pid)?;
1104+
1105+
let mut data = [0; 4];
1106+
read(my_fd, &mut data)?;
1107+
shmem_file.write_all(&data)?;
1108+
10991109
// TODO: match st_dev too to avoid false positives
11001110
let my_ino = fstat(my_fd)?.st_ino;
11011111
let mut fds_to_replace = Vec::new();
@@ -1171,15 +1181,14 @@ impl Client {
11711181
} else {
11721182
let (fd, shmem_path) = mkstemp(SHM_TEMPLATE)?;
11731183
let mut shmem_file = unsafe { File::from_raw_fd(fd) };
1174-
let mut data = [0; 4];
1175-
read(memfd.as_raw_fd(), &mut data)?;
1176-
shmem_file.write_all(&data)?;
1177-
Self::replace_futex_storage(
1184+
let ret = Self::replace_futex_storage(
11781185
memfd.as_raw_fd(),
11791186
Pid::from_raw(pid),
11801187
shmem_path.as_os_str().to_str().unwrap(),
1181-
)?;
1188+
&mut shmem_file,
1189+
);
11821190
remove_file(&shmem_path)?;
1191+
ret?;
11831192
shmem_file
11841193
};
11851194

0 commit comments

Comments
 (0)