OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 3:20 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 11 posts ] 
Author Message
 Post subject: About Memory/Task management...
PostPosted: Sat Nov 11, 2017 3:23 pm 
Offline

Joined: Wed Feb 08, 2017 7:51 pm
Posts: 22
Let's say I have a task A that has a specific block of allocated memory. How this task gets into memory and execution is not of consideration; let's just say it is there and somehow it gets processor time.

Let's say this task A has a memory leak that goes beyond the bounds of its allocation, and that this threatens to overwrite all of memory.

Can anyone list EVERY SINGLE POSSIBLE WAY that you can prevent task A from accessing memory outside of its allocation in a X86 32 bit execution environment?

Note that this is a LIST, if you have an idea you would consider to be risky/horrible/just bad, then I still want to hear it.

Also note that task A has full control of the processor at this moment, unless somehow it could be interrupted at the moment of accessing memory outside of its allocation.

I am sorry if this is a stupid question or a repeat, but I have looked a while on this form and I have not really found any question quite as comprehensive as this.


Top
 Profile  
 
 Post subject: Re: About Memory/Task management...
PostPosted: Sat Nov 11, 2017 3:45 pm 
Offline
Member
Member
User avatar

Joined: Sun Apr 05, 2015 3:15 pm
Posts: 31
A process should never be able to get full control of the CPU, if it does, then there's no point in protecting the memory pages.
Your process gets virtual memory assigned by your kernel, so the kernel controls what memory can and cannot be written to/read from. If your process allocates memory and there's no assigned virtual memory left for that process, it should request more virtual memory.

The kernel receives this request (probably using a syscall) and can for example check if the process isn't using too much resources yet.

_________________
osdev project, goal is to run wasm as userspace: https://github.com/kwast-os/kwast


Top
 Profile  
 
 Post subject: Re: About Memory/Task management...
PostPosted: Sat Nov 11, 2017 3:47 pm 
Offline
Member
Member

Joined: Tue Mar 04, 2014 5:27 am
Posts: 1108
interruption wrote:
Let's say I have a task A that has a specific block of allocated memory. How this task gets into memory and execution is not of consideration; let's just say it is there and somehow it gets processor time.

Let's say this task A has a memory leak that goes beyond the bounds of its allocation, and that this threatens to overwrite all of memory.

Can anyone list EVERY SINGLE POSSIBLE WAY that you can prevent task A from accessing memory outside of its allocation in a X86 32 bit execution environment?

Note that this is a LIST, if you have an idea you would consider to be risky/horrible/just bad, then I still want to hear it.

Also note that task A has full control of the processor at this moment, unless somehow it could be interrupted at the moment of accessing memory outside of its allocation.

I am sorry if this is a stupid question or a repeat, but I have looked a while on this form and I have not really found any question quite as comprehensive as this.


If you don't control what's in the task, then segmentation, page translation, interpretation/emulation of task's code (hardware-assisted virtualization is just a variation on the theme of memory protection with page translation).
If you do, you can just insert checks into the task.


Top
 Profile  
 
 Post subject: Re: About Memory/Task management...
PostPosted: Sat Nov 11, 2017 5:36 pm 
Offline

Joined: Wed Feb 08, 2017 7:51 pm
Posts: 22
Quote:
A process should never be able to get full control of the CPU, if it does, then there's no point in protecting the memory pages.


Sorry; what I meant really was that the process is currently running on the CPU, and the kernel is not currently running, so the process has control over the CPU unless the CPU prevents it from accessing memory, or the OS can somehow be called through an interrupt when the process accesses out of bounds memory.

Quote:
Your process gets virtual memory assigned by your kernel, so the kernel controls what memory can and cannot be written to/read from. If your process allocates memory and there's no assigned virtual memory left for that process, it should request more virtual memory.

The kernel receives this request (probably using a syscall) and can for example check if the process isn't using too much resources yet.


Isn't that basically describing paging? Actually, the entire purpose of this post was to see if there were any alternatives to paging because the pages take up a fair amount of memory on a system with memory constraints, and I don't really need all of the functionalities it promises. I just need to be able to prevent a task from overwriting kernel space.

Quote:
If you don't control what's in the task, then segmentation, page translation, interpretation/emulation of task's code (hardware-assisted virtualization is just a variation on the theme of memory protection with page translation).


Can you elaborate more on each system and roughly give like how much memory it uses and such? I have seen some of what you are saying and have an idea of it, but what I was asking more for was like a bulleted list of each process and perhaps a brief overview on what it entails. I know that it is somewhat vague and perhaps hard/tedious to map out.

Quote:
If you do, you can just insert checks into the task.


Lets just go with the initial premise that assuming an arbitrary task that can contain anything is in memory, and it is running at the moment.
I just want to prevent it from overwriting memory outside of its allocated space; if the task causes other problems such as processor/operating system exceptions, than I can deal with that. I just want it to stay in it's bounds.


Top
 Profile  
 
 Post subject: Re: About Memory/Task management...
PostPosted: Sat Nov 11, 2017 5:50 pm 
Offline
Member
Member
User avatar

Joined: Fri Feb 17, 2017 4:01 pm
Posts: 640
Location: Ukraine, Bachmut
the next/previous page after/before the last/first page in this block allocated for the process has an entry in the page table indicating its not present status, causing processor to generate a page fault exception on access to it. it's a basics of virtual memory protection. there are also attributes for preventing only writes, execute.

_________________
ANT - NT-like OS for x64 and arm64.
efify - UEFI for a couple of boards (mips and arm). suspended due to lost of all the target park boards (russians destroyed our town).


Top
 Profile  
 
 Post subject: Re: About Memory/Task management...
PostPosted: Sat Nov 11, 2017 6:03 pm 
Offline

Joined: Wed Feb 08, 2017 7:51 pm
Posts: 22
So, in other words, paging is the only way that you can control an arbitrary process and make sure it obeys the bounds of memory?


Top
 Profile  
 
 Post subject: Re: About Memory/Task management...
PostPosted: Sat Nov 11, 2017 6:16 pm 
Offline
Member
Member
User avatar

Joined: Tue Mar 06, 2007 11:17 am
Posts: 1225
I posted here a full 32-bit page table/directory for the 4GB address space:
viewtopic.php?t=32513

I think that this sample paging table might clear the initial confusion associated on how to use paging since it displays how to build it more clearly and shows the whole range of identity-mapped memory.


You could start your kernel by making it contain static paging tables that are identity mapped. You could create paging tables dynamically for each new process from there. You could use several schemes, for example reserve some lower memory to map the kernel globally in other processes as read-only, indirect write or privileged write, or you could have a fully dynamic address space, but locating and reallocating things scattered around for the kernel and system modules would be too hard for a start.

In fact you could start a kernel with the most difficult structures (PCI address space, page tables, I/O maps, etc.) as static tables that you could later free dynamically and replace/modify as needed.

_________________
Live PC 1: Image Live PC 2: Image

YouTube:
http://youtube.com/@AltComp126/streams
http://youtube.com/@proyectos/streams

http://master.dl.sourceforge.net/projec ... 7z?viasf=1


Top
 Profile  
 
 Post subject: Re: About Memory/Task management...
PostPosted: Sat Nov 11, 2017 11:08 pm 
Offline
Member
Member

Joined: Mon Jan 03, 2011 6:58 pm
Posts: 283
This is either a homework question, in which case say so, or you are just trying to figure out what to use/research, in which case use paging. And ignore ~, he has a habit of spewing misinformation.

- Amy


Top
 Profile  
 
 Post subject: Re: About Memory/Task management...
PostPosted: Sun Nov 12, 2017 3:19 am 
Offline
Member
Member
User avatar

Joined: Sat Jan 15, 2005 12:00 am
Posts: 8561
Location: At his keyboard!
Hi,

interruption wrote:
Isn't that basically describing paging? Actually, the entire purpose of this post was to see if there were any alternatives to paging because the pages take up a fair amount of memory on a system with memory constraints, and I don't really need all of the functionalities it promises. I just need to be able to prevent a task from overwriting kernel space.


This is very wrong.

Paging costs a tiny bit of memory (about 0.2%) for page tables, etc; which is relatively insignificant. However, it also allows you to do various tricks (allocation on demand, copy on write, memory mapped files, etc) that can save you a lot of memory; and it allows you to do other tricks (swap space, etc) that allow you to use more memory than you actually have.

If a computer has 4 GiB of RAM and is running processes that all use 512 MiB each; an OS that doesn't use paging probably won't be able to handle more than 7 processes; an OS that uses paging will probably handle 15 processes without using swap space; and with 40 GiB of swap space a process that uses paging will probably handle 30 processes before the user notices any performance difference (and will probably handle 150 processes before it becomes too slow to be usable).

interruption wrote:
So, in other words, paging is the only way that you can control an arbitrary process and make sure it obeys the bounds of memory?


For 80x86 protected mode (and not long mode which doesn't support segmentation); there's paging, segmentation and software protection. These aren't mutually exclusive, so (ignoring "no protection") there's 7 possible permutations (paging only, segmentation only, software only, paging+segmentation, paging+software, segmentation+software, paging+segmentation+software).

Note: "software" is some combination of special language/tools and/or special run-time (e.g. JIT interpreter).

"Paging only" gives the best compromise between complexity, performance, protection and usefulness; so almost nobody uses the other possibilities. Note: for debugging (where you don't care about complexity, performance or protection) "software" can be extremely powerful, and because of this there are some tools (e.g. valgrind) that are capable of providing "paging+software" on top of an OS that is intended as "paging only".


Cheers,

Brendan

_________________
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.


Top
 Profile  
 
 Post subject: Re: About Memory/Task management...
PostPosted: Sun Nov 12, 2017 3:26 am 
Offline
Member
Member

Joined: Tue Mar 04, 2014 5:27 am
Posts: 1108
interruption wrote:
Quote:
If you don't control what's in the task, then segmentation, page translation, interpretation/emulation of task's code (hardware-assisted virtualization is just a variation on the theme of memory protection with page translation).


Can you elaborate more on each system and roughly give like how much memory it uses and such? I have seen some of what you are saying and have an idea of it, but what I was asking more for was like a bulleted list of each process and perhaps a brief overview on what it entails. I know that it is somewhat vague and perhaps hard/tedious to map out.


See Intel® 64 and IA-32 Architectures Software Developer’s Manual.
Specifically, volume 3, System Programming Guide:
Chapter 2 — System Architecture Overview
Chapter 3 — Protected-Mode Memory Management
Chapter 4 — Paging
Chapter 5 — Protection

That's as elaborate as it gets. I don't want to restate it. You'll still need to take a deep dive to make use of this functionality, whether for protection you're asking about or for memory management in general. For yet another overview (if you find chapter 2 insufficient), look up our wiki, Wikipedia or just google stuff up.


Top
 Profile  
 
 Post subject: Re: About Memory/Task management...
PostPosted: Mon Nov 13, 2017 8:17 pm 
Offline
Member
Member

Joined: Sat Feb 27, 2010 8:55 pm
Posts: 147
interruption wrote:
Isn't that basically describing paging? Actually, the entire purpose of this post was to see if there were any alternatives to paging because the pages take up a fair amount of memory on a system with memory constraints...


I think you're thinking paging requires just over 4MB of RAM; it does not. Assuming 4KB page sizes and PAE disabled, CR3 points to one 4K table, and each of the 1024 entries there point to another 4K table, each of which point to the actual page. If you're using <= 4MB of contiguous, aligned virtual memory, your "upper" table (pointed to by CR3) will have 1 present entry and 1023 not present entries, and your "lower" page table (pointed to by the one present entry in the "upper" table) will point to the actual physical RAM you're using. Thus, you need as little as 8KB for your page tables, and only an additional 4KB for every additional contiguous aligned 4MB of virtual memory you need.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 24 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group