[Kernel] Use VM_DONTEXPAND in mmap() for stability and isolation

This commit is contained in:
CyrIng
2025-05-28 07:01:03 +02:00
parent 989036f384
commit 6b176cd026
4 changed files with 90 additions and 22 deletions

View File

@@ -5346,7 +5346,8 @@ static int CoreFreqK_mmap(struct file *pfile, struct vm_area_struct *vma)
{ {
unsigned long reqSize = vma->vm_end - vma->vm_start; unsigned long reqSize = vma->vm_end - vma->vm_start;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0) #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0)
vm_flags_t vm_ro = VM_READ; vm_flags_t vm_ro = VM_READ | VM_DONTEXPAND;
vm_flags_t vm_rw = VM_READ | VM_WRITE | VM_DONTEXPAND;
#endif #endif
int rc = -EIO; int rc = -EIO;
UNUSED(pfile); UNUSED(pfile);
@@ -5363,7 +5364,7 @@ static int CoreFreqK_mmap(struct file *pfile, struct vm_area_struct *vma)
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0) #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0)
vm_flags_reset_once(vma, vm_ro); vm_flags_reset_once(vma, vm_ro);
#else #else
vma->vm_flags = VM_READ; vma->vm_flags = VM_READ | VM_DONTEXPAND;
#endif #endif
vma->vm_page_prot = PAGE_READONLY; vma->vm_page_prot = PAGE_READONLY;
@@ -5382,6 +5383,14 @@ static int CoreFreqK_mmap(struct file *pfile, struct vm_area_struct *vma)
goto EXIT_PAGE; goto EXIT_PAGE;
} }
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0) \
|| (defined(RHEL_MAJOR) && (RHEL_MAJOR >= 9) && (RHEL_MINOR >= 5))
vm_flags_reset_once(vma, vm_rw);
#else
vma->vm_flags = VM_READ | VM_WRITE | VM_DONTEXPAND;
#endif
vma->vm_page_prot = PAGE_SHARED;
rc = remap_pfn_range( vma, rc = remap_pfn_range( vma,
vma->vm_start, vma->vm_start,
virt_to_phys((void *) PUBLIC(RW(Proc))) >> PAGE_SHIFT, virt_to_phys((void *) PUBLIC(RW(Proc))) >> PAGE_SHIFT,
@@ -5405,7 +5414,7 @@ static int CoreFreqK_mmap(struct file *pfile, struct vm_area_struct *vma)
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0) #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0)
vm_flags_reset_once(vma, vm_ro); vm_flags_reset_once(vma, vm_ro);
#else #else
vma->vm_flags = VM_READ; vma->vm_flags = VM_READ | VM_DONTEXPAND;
#endif #endif
vma->vm_page_prot = PAGE_READONLY; vma->vm_page_prot = PAGE_READONLY;
@@ -5436,7 +5445,7 @@ static int CoreFreqK_mmap(struct file *pfile, struct vm_area_struct *vma)
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0) #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0)
vm_flags_reset_once(vma, vm_ro); vm_flags_reset_once(vma, vm_ro);
#else #else
vma->vm_flags = VM_READ; vma->vm_flags = VM_READ | VM_DONTEXPAND;
#endif #endif
vma->vm_page_prot = PAGE_READONLY; vma->vm_page_prot = PAGE_READONLY;
@@ -5463,6 +5472,14 @@ static int CoreFreqK_mmap(struct file *pfile, struct vm_area_struct *vma)
goto EXIT_PAGE; goto EXIT_PAGE;
} }
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0) \
|| (defined(RHEL_MAJOR) && (RHEL_MAJOR >= 9) && (RHEL_MINOR >= 5))
vm_flags_reset_once(vma, vm_rw);
#else
vma->vm_flags = VM_READ | VM_WRITE | VM_DONTEXPAND;
#endif
vma->vm_page_prot = PAGE_SHARED;
rc = remap_pfn_range( vma, rc = remap_pfn_range( vma,
vma->vm_start, vma->vm_start,
virt_to_phys((void *) PUBLIC(RW(Core, AT(cpu)))) >> PAGE_SHIFT, virt_to_phys((void *) PUBLIC(RW(Core, AT(cpu)))) >> PAGE_SHIFT,

View File

@@ -3703,7 +3703,8 @@ static int CoreFreqK_mmap(struct file *pfile, struct vm_area_struct *vma)
{ {
unsigned long reqSize = vma->vm_end - vma->vm_start; unsigned long reqSize = vma->vm_end - vma->vm_start;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0) #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0)
vm_flags_t vm_ro = VM_READ; vm_flags_t vm_ro = VM_READ | VM_DONTEXPAND;
vm_flags_t vm_rw = VM_READ | VM_WRITE | VM_DONTEXPAND;
#endif #endif
int rc = -EIO; int rc = -EIO;
UNUSED(pfile); UNUSED(pfile);
@@ -3720,9 +3721,9 @@ static int CoreFreqK_mmap(struct file *pfile, struct vm_area_struct *vma)
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0) #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0)
vm_flags_reset_once(vma, vm_ro); vm_flags_reset_once(vma, vm_ro);
#else #else
vma->vm_flags = VM_READ; vma->vm_flags = VM_READ | VM_DONTEXPAND;
#endif #endif
/* vma->vm_page_prot = PAGE_READONLY; TODO*/ vma->vm_page_prot = PAGE_READONLY;
rc = remap_pfn_range( vma, rc = remap_pfn_range( vma,
vma->vm_start, vma->vm_start,
@@ -3739,6 +3740,14 @@ static int CoreFreqK_mmap(struct file *pfile, struct vm_area_struct *vma)
goto EXIT_PAGE; goto EXIT_PAGE;
} }
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0) \
|| (defined(RHEL_MAJOR) && (RHEL_MAJOR >= 9) && (RHEL_MINOR >= 5))
vm_flags_reset_once(vma, vm_rw);
#else
vma->vm_flags = VM_READ | VM_WRITE | VM_DONTEXPAND;
#endif
vma->vm_page_prot = PAGE_SHARED;
rc = remap_pfn_range( vma, rc = remap_pfn_range( vma,
vma->vm_start, vma->vm_start,
virt_to_phys((void *) PUBLIC(RW(Proc))) >> PAGE_SHIFT, virt_to_phys((void *) PUBLIC(RW(Proc))) >> PAGE_SHIFT,
@@ -3762,9 +3771,9 @@ static int CoreFreqK_mmap(struct file *pfile, struct vm_area_struct *vma)
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0) #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0)
vm_flags_reset_once(vma, vm_ro); vm_flags_reset_once(vma, vm_ro);
#else #else
vma->vm_flags = VM_READ; vma->vm_flags = VM_READ | VM_DONTEXPAND;
#endif #endif
/* vma->vm_page_prot = PAGE_READONLY; TODO*/ vma->vm_page_prot = PAGE_READONLY;
rc = remap_pfn_range( vma, rc = remap_pfn_range( vma,
vma->vm_start, vma->vm_start,
@@ -3793,9 +3802,9 @@ static int CoreFreqK_mmap(struct file *pfile, struct vm_area_struct *vma)
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0) #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0)
vm_flags_reset_once(vma, vm_ro); vm_flags_reset_once(vma, vm_ro);
#else #else
vma->vm_flags = VM_READ; vma->vm_flags = VM_READ | VM_DONTEXPAND;
#endif #endif
/* vma->vm_page_prot = PAGE_READONLY; TODO*/ vma->vm_page_prot = PAGE_READONLY;
rc = remap_pfn_range( vma, rc = remap_pfn_range( vma,
vma->vm_start, vma->vm_start,
@@ -3820,6 +3829,14 @@ static int CoreFreqK_mmap(struct file *pfile, struct vm_area_struct *vma)
goto EXIT_PAGE; goto EXIT_PAGE;
} }
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0) \
|| (defined(RHEL_MAJOR) && (RHEL_MAJOR >= 9) && (RHEL_MINOR >= 5))
vm_flags_reset_once(vma, vm_rw);
#else
vma->vm_flags = VM_READ | VM_WRITE | VM_DONTEXPAND;
#endif
vma->vm_page_prot = PAGE_SHARED;
rc = remap_pfn_range( vma, rc = remap_pfn_range( vma,
vma->vm_start, vma->vm_start,
virt_to_phys((void *) PUBLIC(RW(Core, AT(cpu)))) >> PAGE_SHIFT, virt_to_phys((void *) PUBLIC(RW(Core, AT(cpu)))) >> PAGE_SHIFT,

View File

@@ -3690,7 +3690,8 @@ static int CoreFreqK_mmap(struct file *pfile, struct vm_area_struct *vma)
{ {
unsigned long reqSize = vma->vm_end - vma->vm_start; unsigned long reqSize = vma->vm_end - vma->vm_start;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0) #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0)
vm_flags_t vm_ro = VM_READ; vm_flags_t vm_ro = VM_READ | VM_DONTEXPAND;
vm_flags_t vm_rw = VM_READ | VM_WRITE | VM_DONTEXPAND;
#endif #endif
int rc = -EIO; int rc = -EIO;
UNUSED(pfile); UNUSED(pfile);
@@ -3707,9 +3708,9 @@ static int CoreFreqK_mmap(struct file *pfile, struct vm_area_struct *vma)
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0) #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0)
vm_flags_reset_once(vma, vm_ro); vm_flags_reset_once(vma, vm_ro);
#else #else
vma->vm_flags = VM_READ; vma->vm_flags = VM_READ | VM_DONTEXPAND;
#endif #endif
/* vma->vm_page_prot = PAGE_READONLY; TODO*/ vma->vm_page_prot = PAGE_READ;
rc = remap_pfn_range( vma, rc = remap_pfn_range( vma,
vma->vm_start, vma->vm_start,
@@ -3726,6 +3727,14 @@ static int CoreFreqK_mmap(struct file *pfile, struct vm_area_struct *vma)
goto EXIT_PAGE; goto EXIT_PAGE;
} }
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0) \
|| (defined(RHEL_MAJOR) && (RHEL_MAJOR >= 9) && (RHEL_MINOR >= 5))
vm_flags_reset_once(vma, vm_rw);
#else
vma->vm_flags = VM_READ | VM_WRITE | VM_DONTEXPAND;
#endif
vma->vm_page_prot = PAGE_SHARED;
rc = remap_pfn_range( vma, rc = remap_pfn_range( vma,
vma->vm_start, vma->vm_start,
virt_to_phys((void *) PUBLIC(RW(Proc))) >> PAGE_SHIFT, virt_to_phys((void *) PUBLIC(RW(Proc))) >> PAGE_SHIFT,
@@ -3749,9 +3758,9 @@ static int CoreFreqK_mmap(struct file *pfile, struct vm_area_struct *vma)
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0) #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0)
vm_flags_reset_once(vma, vm_ro); vm_flags_reset_once(vma, vm_ro);
#else #else
vma->vm_flags = VM_READ; vma->vm_flags = VM_READ | VM_DONTEXPAND;
#endif #endif
/* vma->vm_page_prot = PAGE_READONLY; TODO*/ vma->vm_page_prot = PAGE_READ;
rc = remap_pfn_range( vma, rc = remap_pfn_range( vma,
vma->vm_start, vma->vm_start,
@@ -3780,9 +3789,9 @@ static int CoreFreqK_mmap(struct file *pfile, struct vm_area_struct *vma)
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0) #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0)
vm_flags_reset_once(vma, vm_ro); vm_flags_reset_once(vma, vm_ro);
#else #else
vma->vm_flags = VM_READ; vma->vm_flags = VM_READ | VM_DONTEXPAND;
#endif #endif
/* vma->vm_page_prot = PAGE_READONLY; TODO*/ vma->vm_page_prot = PAGE_READ;
rc = remap_pfn_range( vma, rc = remap_pfn_range( vma,
vma->vm_start, vma->vm_start,
@@ -3807,6 +3816,14 @@ static int CoreFreqK_mmap(struct file *pfile, struct vm_area_struct *vma)
goto EXIT_PAGE; goto EXIT_PAGE;
} }
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0) \
|| (defined(RHEL_MAJOR) && (RHEL_MAJOR >= 9) && (RHEL_MINOR >= 5))
vm_flags_reset_once(vma, vm_rw);
#else
vma->vm_flags = VM_READ | VM_WRITE | VM_DONTEXPAND;
#endif
vma->vm_page_prot = PAGE_SHARED;
rc = remap_pfn_range( vma, rc = remap_pfn_range( vma,
vma->vm_start, vma->vm_start,
virt_to_phys((void *) PUBLIC(RW(Core, AT(cpu)))) >> PAGE_SHIFT, virt_to_phys((void *) PUBLIC(RW(Core, AT(cpu)))) >> PAGE_SHIFT,

View File

@@ -23931,7 +23931,8 @@ static int CoreFreqK_mmap(struct file *pfile, struct vm_area_struct *vma)
unsigned long reqSize = vma->vm_end - vma->vm_start; unsigned long reqSize = vma->vm_end - vma->vm_start;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0) \ #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0) \
|| (defined(RHEL_MAJOR) && (RHEL_MAJOR >= 9) && (RHEL_MINOR >= 5)) || (defined(RHEL_MAJOR) && (RHEL_MAJOR >= 9) && (RHEL_MINOR >= 5))
vm_flags_t vm_ro = VM_READ; vm_flags_t vm_ro = VM_READ | VM_DONTEXPAND;
vm_flags_t vm_rw = VM_READ | VM_WRITE | VM_DONTEXPAND;
#endif #endif
int rc = -EIO; int rc = -EIO;
UNUSED(pfile); UNUSED(pfile);
@@ -23949,7 +23950,7 @@ static int CoreFreqK_mmap(struct file *pfile, struct vm_area_struct *vma)
|| (defined(RHEL_MAJOR) && (RHEL_MAJOR >= 9) && (RHEL_MINOR >= 5)) || (defined(RHEL_MAJOR) && (RHEL_MAJOR >= 9) && (RHEL_MINOR >= 5))
vm_flags_reset_once(vma, vm_ro); vm_flags_reset_once(vma, vm_ro);
#else #else
vma->vm_flags = VM_READ; vma->vm_flags = VM_READ | VM_DONTEXPAND;
#endif #endif
vma->vm_page_prot = PAGE_READONLY; vma->vm_page_prot = PAGE_READONLY;
@@ -23968,6 +23969,14 @@ static int CoreFreqK_mmap(struct file *pfile, struct vm_area_struct *vma)
goto EXIT_PAGE; goto EXIT_PAGE;
} }
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0) \
|| (defined(RHEL_MAJOR) && (RHEL_MAJOR >= 9) && (RHEL_MINOR >= 5))
vm_flags_reset_once(vma, vm_rw);
#else
vma->vm_flags = VM_READ | VM_WRITE | VM_DONTEXPAND;
#endif
vma->vm_page_prot = PAGE_SHARED;
rc = remap_pfn_range( vma, rc = remap_pfn_range( vma,
vma->vm_start, vma->vm_start,
virt_to_phys((void *) PUBLIC(RW(Proc))) >> PAGE_SHIFT, virt_to_phys((void *) PUBLIC(RW(Proc))) >> PAGE_SHIFT,
@@ -23992,7 +24001,7 @@ static int CoreFreqK_mmap(struct file *pfile, struct vm_area_struct *vma)
|| (defined(RHEL_MAJOR) && (RHEL_MAJOR >= 9) && (RHEL_MINOR >= 5)) || (defined(RHEL_MAJOR) && (RHEL_MAJOR >= 9) && (RHEL_MINOR >= 5))
vm_flags_reset_once(vma, vm_ro); vm_flags_reset_once(vma, vm_ro);
#else #else
vma->vm_flags = VM_READ; vma->vm_flags = VM_READ | VM_DONTEXPAND;
#endif #endif
vma->vm_page_prot = PAGE_READONLY; vma->vm_page_prot = PAGE_READONLY;
@@ -24024,7 +24033,7 @@ static int CoreFreqK_mmap(struct file *pfile, struct vm_area_struct *vma)
|| (defined(RHEL_MAJOR) && (RHEL_MAJOR >= 9) && (RHEL_MINOR >= 5)) || (defined(RHEL_MAJOR) && (RHEL_MAJOR >= 9) && (RHEL_MINOR >= 5))
vm_flags_reset_once(vma, vm_ro); vm_flags_reset_once(vma, vm_ro);
#else #else
vma->vm_flags = VM_READ; vma->vm_flags = VM_READ | VM_DONTEXPAND;
#endif #endif
vma->vm_page_prot = PAGE_READONLY; vma->vm_page_prot = PAGE_READONLY;
@@ -24051,6 +24060,14 @@ static int CoreFreqK_mmap(struct file *pfile, struct vm_area_struct *vma)
goto EXIT_PAGE; goto EXIT_PAGE;
} }
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 0) \
|| (defined(RHEL_MAJOR) && (RHEL_MAJOR >= 9) && (RHEL_MINOR >= 5))
vm_flags_reset_once(vma, vm_rw);
#else
vma->vm_flags = VM_READ | VM_WRITE | VM_DONTEXPAND;
#endif
vma->vm_page_prot = PAGE_SHARED;
rc = remap_pfn_range( vma, rc = remap_pfn_range( vma,
vma->vm_start, vma->vm_start,
virt_to_phys((void *) PUBLIC(RW(Core, AT(cpu)))) >> PAGE_SHIFT, virt_to_phys((void *) PUBLIC(RW(Core, AT(cpu)))) >> PAGE_SHIFT,