LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-12-2023, 12:10 AM   #1
erezt
LQ Newbie
 
Registered: Oct 2023
Posts: 6

Rep: Reputation: 0
1G writable page mapped by kernel


Hi,

I’m using arm64 machine and checking the memory mapping entries that are fed into the MMU TLB. For some reason I keep seeing a 1G writable page mapped to the end of the physical dram. I tried to intercept this mapping on all three supported page table levels, I.e. set_pgd, set_pmd and set_pte (although I clearly see from HW signals that it’s mapped on first level - pgd) but I couldn’t catch any mapping like that on those functions. (BTW, I disabled hugepages but I still see this page).
Anyone has a clue from where this 1G writable page mapping could come from? And why I don’t see it explicitly in the set_pgd function?

Thanks!
 
Old 10-14-2023, 12:00 PM   #2
sag47
Senior Member
 
Registered: Sep 2009
Location: Raleigh, NC
Distribution: Ubuntu, PopOS, Raspbian
Posts: 1,899
Blog Entries: 36

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
Can you expand on what you’re doing and why?
 
Old 10-22-2023, 06:29 AM   #3
erezt
LQ Newbie
 
Registered: Oct 2023
Posts: 6

Original Poster
Rep: Reputation: 0
I’m trying to check if there are any memory pages that are pointed by Linux kernel as both executable and writable. I didn’t see any page entry that is marked as both executable & writable, however I did see several occurrences where two different page table entries map the same physical page entry - one as executable and one as writable. I tried to sort these cases and found that most of them are 1GB page mapped at the end of the dram as writable while from Tim to time I see that the kernel allocates 4KB executable pages that are included in this 1GB region. I used all the defconfigs that should disallow writable/ executable conflicts but I couldn’t get rid of this specific one.
 
Old 10-22-2023, 12:12 PM   #4
sag47
Senior Member
 
Registered: Sep 2009
Location: Raleigh, NC
Distribution: Ubuntu, PopOS, Raspbian
Posts: 1,899
Blog Entries: 36

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
I still don’t understand the problem you are trying to solve. Instead of issues you’ve encountered with perceived solution, can you elaborate on the original problem that sent you down this course?
 
Old 10-22-2023, 06:52 PM   #5
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,246

Rep: Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323
Quote:
Originally Posted by erezt View Post
I’m trying to check if there are any memory pages that are pointed by Linux kernel as both executable and writable.
And why are you trying to check that?
 
Old 10-22-2023, 08:52 PM   #6
erezt
LQ Newbie
 
Registered: Oct 2023
Posts: 6

Original Poster
Rep: Reputation: 0
We have some system that should serve high security bar solutions. One of our targets is to prevent attackers from running malicious code on our system. For that, we built a dedicated hardware that is capable of validating the integrity of each executable memory page that is being loaded. Now, in order for this hardware to be efficient, we need to make sure that the (valid) SW that is running on our system will never map an executable memory page as writable, since this means that even if we validated the executable page integrity when it was loaded, it can still be modified later on. Hence, we’re trying to define such state of executable / writable pages as invalid state. I.e. we detect it as “attack” situation. I’m trying to check whether Linux is capable of fulfilling this requirement.
 
Old 10-23-2023, 07:40 AM   #7
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,246

Rep: Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323
You have not mentioned SELinux once. Can I assume you've already looked at it extensively, with a focus on what it can and cannot do with memory pages?

Last edited by dugan; 10-23-2023 at 07:49 AM.
 
Old 10-23-2023, 09:51 AM   #8
erezt
LQ Newbie
 
Registered: Oct 2023
Posts: 6

Original Poster
Rep: Reputation: 0
Yes we evaluated SE Linux. Our concern is more about kernel exploits that can enable attacker taking full control on the kernel on EL1. Hence, our solution is not based on the kernel but on dedicated HW.
But let me ask please - is my assumption really true? I.e. that Linux shouldn’t map the same physical regions as writable and executable at he same time? Or am I missing some basic fundamental mechanism of Linux kernel operation?
 
Old 10-23-2023, 09:52 AM   #9
EdGr
Senior Member
 
Registered: Dec 2010
Location: California, USA
Distribution: I run my own OS
Posts: 1,000

Rep: Reputation: 472Reputation: 472Reputation: 472Reputation: 472Reputation: 472
The kernel has a legitimate need to preclude page faults in "pinned" areas of memory.

Software like JITs has a legitimate need to write to executable pages.

I think your question can only be answered by studying the kernel source. You seem to have enough resources.
Ed
 
Old 10-25-2023, 12:50 PM   #10
erezt
LQ Newbie
 
Registered: Oct 2023
Posts: 6

Original Poster
Rep: Reputation: 0
Thanks Ed.
Let me please focus on one aspect that I’m not totally understand.
When Linux kernel load a new user space process, I assume it reads it from the disk and copy it to memory pages that are pointed by kernel page table entries and marked as writable non-executable. Now, it prepare those pages for user space execution, so I assume it marks the .text region as executable non-writable for user space in the user space page table entries. Does the kernel at this point also changes the kernel page table entries that point on these .text pages to be non-writable? Or does it keep it as writable for kernel?
Can someone point me to the code that deals with it?

Thanks!
 
Old 10-25-2023, 08:48 PM   #11
EdGr
Senior Member
 
Registered: Dec 2010
Location: California, USA
Distribution: I run my own OS
Posts: 1,000

Rep: Reputation: 472Reputation: 472Reputation: 472Reputation: 472Reputation: 472
Read the mmap () and mprotect () man pages. Then, look at /proc/$PID/maps for some process.

A lot of pages are mapped to files. This works by DMA'ing data into main memory.

The relevant kernel source is in the mm directory. Arm64-specific code is in arch/arm64/mm.
Ed
 
Old 11-22-2023, 10:08 AM   #12
Cayden
LQ Newbie
 
Registered: Nov 2023
Posts: 1

Rep: Reputation: 0
Interesting topic! The 1G writable page mapped by the kernel could significantly enhance performance for large-scale operations. It suggests an optimized memory management approach, allowing faster access to large data sets. However, I'm curious about the security implications and potential vulnerability to buffer overflows or similar exploits. Thoughts on apps to write essays https://essaypro.app/?

Last edited by Cayden; 12-04-2023 at 10:12 AM.
 
Old 11-23-2023, 05:22 AM   #13
erezt
LQ Newbie
 
Registered: Oct 2023
Posts: 6

Original Poster
Rep: Reputation: 0
Essentially, if you find some kernel exploit that can cause it writing into this page (which is possible since it’s defined as “writable”) - you could change executable pages of running processes since they are allocated from the same physical memory, so in case of I-Cache miss they will fetch the “tampered data” and execute it under their permissions.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Multiple config file not writable: Configuration file "home/$USER/.config/consolerc" not writable. Please contact your system administrator. andrewysk Linux - Newbie 8 05-16-2021 02:01 PM
memory mapped i/o and i/o mapped i/o shilpa rangappa General 1 12-14-2009 10:27 PM
Network device Memory mapped or Port mapped ?? Bignon Linux - Hardware 0 10-20-2009 08:36 AM
Kernel crashes while accessing the IO mapped memory rohshall Linux - Embedded & Single-board computer 0 12-10-2008 09:52 AM
RH & HP4050N PCL - page, pause, page, pause, page andguent Linux - Hardware 0 11-10-2003 08:35 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:01 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration