Skip to content

Commit 5aaa7d0

Browse files
committed
Avoid truncating mmap2 offsets if not multiple of page size
1 parent 805f9b3 commit 5aaa7d0

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

lib/std/os/linux.zig

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,29 @@ pub fn umount2(special: [*]const u8, flags: u32) usize {
193193

194194
pub fn mmap(address: ?[*]u8, length: usize, prot: usize, flags: u32, fd: i32, offset: u64) usize {
195195
if (@hasDecl(@This(), "SYS_mmap2")) {
196-
return syscall6(SYS_mmap2, @ptrToInt(address), length, prot, flags, @bitCast(usize, isize(fd)), @truncate(usize, offset / MMAP2_UNIT));
196+
// Make sure the offset is also specified in multiples of page size
197+
if ((offset & (MMAP2_UNIT - 1)) != 0)
198+
return @bitCast(usize, isize(-EINVAL));
199+
200+
return syscall6(
201+
SYS_mmap2,
202+
@ptrToInt(address),
203+
length,
204+
prot,
205+
flags,
206+
@bitCast(usize, isize(fd)),
207+
@truncate(usize, offset / MMAP2_UNIT),
208+
);
197209
} else {
198-
return syscall6(SYS_mmap, @ptrToInt(address), length, prot, flags, @bitCast(usize, isize(fd)), offset);
210+
return syscall6(
211+
SYS_mmap,
212+
@ptrToInt(address),
213+
length,
214+
prot,
215+
flags,
216+
@bitCast(usize, isize(fd)),
217+
offset,
218+
);
199219
}
200220
}
201221

0 commit comments

Comments
 (0)