From 0466e87e598af0efb50fd0e7ac62744d21b76656 Mon Sep 17 00:00:00 2001
From: 4lDO2 <4lDO2@protonmail.com>
Date: Tue, 4 Jul 2023 15:57:30 +0200
Subject: [PATCH] Error if scheme fmap returns unaligned address.

---
 src/scheme/user.rs | 24 +++++++++++++++---------
 1 file changed, 15 insertions(+), 9 deletions(-)

diff --git a/src/scheme/user.rs b/src/scheme/user.rs
index 20128d0e..f354cf43 100644
--- a/src/scheme/user.rs
+++ b/src/scheme/user.rs
@@ -511,6 +511,7 @@ impl UserInner {
                 return Err(Error::new(EIO));
             }
         };
+
         let file_ref = GrantFileRef {
             description: desc,
             base_offset: map.offset,
@@ -520,16 +521,21 @@ impl UserInner {
         let mut src_write_guard;
 
         let src = match base_page_opt {
-            Some(base_addr) => Some(BorrowedFmapSource {
-                src_base: Page::containing_address(VirtualAddress::new(base_addr)),
-                mode: if map.flags.contains(MapFlags::MAP_SHARED) {
-                    src_read_guard = src_address_space.read();
-                    MmapMode::Shared(&src_read_guard.table.utable)
-                } else {
-                    src_write_guard = src_address_space.write();
-                    MmapMode::Cow(&mut src_write_guard.table.utable)
-                },
+            Some(base_addr) => Some({
+                if base_addr % PAGE_SIZE != 0 {
+                    return Err(Error::new(EINVAL));
+                }
+                BorrowedFmapSource {
+                    src_base: Page::containing_address(VirtualAddress::new(base_addr)),
+                    mode: if map.flags.contains(MapFlags::MAP_SHARED) {
+                        src_read_guard = src_address_space.read();
+                        MmapMode::Shared(&src_read_guard.table.utable)
+                    } else {
+                        src_write_guard = src_address_space.write();
+                        MmapMode::Cow(&mut src_write_guard.table.utable)
+                    },
 
+                }
             }),
             None => None,
         };
-- 
GitLab