[PATCH 3/3] /dev/mem: Do not map unaccepted memory
Adrian Hunter
adrian.hunter at intel.com
Wed Sep 6 00:39:02 PDT 2023
Support for unaccepted memory was added recently, refer commit
dcdfdd40fa82 ("mm: Add support for unaccepted memory"), whereby
a virtual machine may need to accept memory before it can be used.
Do not map unaccepted memory because it can cause the guest to fail.
For /dev/mem, this means a read of unaccepted memory will return zeros,
a write to unaccepted memory will be ignored, but an mmap of unaccepted
memory will return an error.
Fixes: dcdfdd40fa82 ("mm: Add support for unaccepted memory")
Signed-off-by: Adrian Hunter <adrian.hunter at intel.com>
---
drivers/char/mem.c | 9 +++++++--
1 file changed, 7 insertions(+), 2 deletions(-)
diff --git a/drivers/char/mem.c b/drivers/char/mem.c
index 1052b0f2d4cf..1a7c5728783c 100644
--- a/drivers/char/mem.c
+++ b/drivers/char/mem.c
@@ -147,7 +147,8 @@ static ssize_t read_mem(struct file *file, char __user *buf,
goto failed;
err = -EFAULT;
- if (allowed == 2) {
+ if (allowed == 2 ||
+ range_contains_unaccepted_memory(p, p + sz)) {
/* Show zeros for restricted memory. */
remaining = clear_user(buf, sz);
} else {
@@ -226,7 +227,8 @@ static ssize_t write_mem(struct file *file, const char __user *buf,
return -EPERM;
/* Skip actual writing when a page is marked as restricted. */
- if (allowed == 1) {
+ if (allowed == 1 &&
+ !range_contains_unaccepted_memory(p, p + sz)) {
/*
* On ia64 if a page has been mapped somewhere as
* uncached, then it must also be accessed uncached
@@ -378,6 +380,9 @@ static int mmap_mem(struct file *file, struct vm_area_struct *vma)
&vma->vm_page_prot))
return -EINVAL;
+ if (range_contains_unaccepted_memory(offset, offset + size))
+ return -EINVAL;
+
vma->vm_page_prot = phys_mem_access_prot(file, vma->vm_pgoff,
size,
vma->vm_page_prot);
--
2.34.1
More information about the kexec
mailing list