Paging Question

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
eryjus
Member
Member
Posts: 286
Joined: Fri Oct 21, 2011 9:47 pm
Libera.chat IRC: eryjus
Location: Tustin, CA USA

Paging Question

Post by eryjus »

HI,

I'm working on a 64-bit virtual memory manager at the moment. My GDT is located in a frame at physical memory 0x0000000000100000 and my initial paging tables are in 7 contiguous frames at 0x0000000000108000. I have no IDT at the moment, but assume that the GDT and IDT will be analogous. The pages for these frames are addresses are identity mapped for the kernel. I am successfully in long mode and the kernel is also mapped to higher memory. I am working on freeing the 32-bit boot pages I won't need anymore.

I know I will need to keep the mapping for my kernel page tables and GDT, but for user-mode page tables will these addresses be required in those page tables as well? Specifically: should I be marking the GDT page and the page tables pages as shared pages?
Adam

The name is fitting: Century Hobby OS -- At this rate, it's gonna take me that long!
Read about my mistakes and missteps with this iteration: Journal

"Sometimes things just don't make sense until you figure them out." -- Phil Stahlheber
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: Paging Question

Post by Brendan »

Hi,

eryjus wrote:I know I will need to keep the mapping for my kernel page tables and GDT, but for user-mode page tables will these addresses be required in those page tables as well? Specifically: should I be marking the GDT page and the page tables pages as shared pages?
You'd relocate the GDT (and create an IDT) in the kernel's higher memory; and typically none of the pages in kernel space are shared (for security reasons). All of the pages in that temporary identity mapping get removed (including the original GDT) and none of them become shared.


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.
User avatar
eryjus
Member
Member
Posts: 286
Joined: Fri Oct 21, 2011 9:47 pm
Libera.chat IRC: eryjus
Location: Tustin, CA USA

Re: Paging Question

Post by eryjus »

Brendan wrote:... and typically none of the pages in kernel space are shared (for security reasons).
Hmmm.... I thought the kernel code needed to be shared so that user space could make calls to the published kernel functions. I'll add it to my research list.

At any rate, Brendan, thank you for your reply.
Adam

The name is fitting: Century Hobby OS -- At this rate, it's gonna take me that long!
Read about my mistakes and missteps with this iteration: Journal

"Sometimes things just don't make sense until you figure them out." -- Phil Stahlheber
dansmahajan
Member
Member
Posts: 62
Joined: Mon Jan 07, 2013 10:38 am

Re: Paging Question

Post by dansmahajan »

eryjus wrote:
Brendan wrote:... and typically none of the pages in kernel space are shared (for security reasons).
Hmmm.... I thought the kernel code needed to be shared so that user space could make calls to the published kernel functions. I'll add it to my research list.

At any rate, Brendan, thank you for your reply.
I think what @Brendan might have meant is only GDT/IDT code is not to be linked/shared.
AFAIK kernel code has to be linked(read only) in every address space to make a system call.
If kernel code is not linked then we've to switch page directory twice after every system call,i guess i'd be costly operation.
User avatar
sortie
Member
Member
Posts: 931
Joined: Wed Mar 21, 2012 3:01 pm
Libera.chat IRC: sortie

Re: Paging Question

Post by sortie »

You want to take advantage of the memory protection offered by paging. You can mark pages as user-space pages. When the CPU is in user-mode, it will refuse to access any pages that isn't marked for user-space use. When a CPU exception/trap/interrupt occurs, it will switch to kernel-mode and access the IDT, GDT and TSS as needed to do the privilege transfer. This means that the IDT/GDT/TSS must be mapped somewhere, but it doesn't need to be readable by user-space, indeed it really shouldn't be (or bad security vulnerabilities happen). You will usually put them among the kernel data, where the kernel is mapped in every single address space identically.
User avatar
xenos
Member
Member
Posts: 1121
Joined: Thu Aug 11, 2005 11:00 pm
Libera.chat IRC: xenos1984
Location: Tartu, Estonia
Contact:

Re: Paging Question

Post by xenos »

sortie wrote:This means that the IDT/GDT/TSS must be mapped somewhere, but it doesn't need to be readable by user-space, indeed it really shouldn't be (or bad security vulnerabilities happen).
Just out of curiosity: What would be the security risk of having IDT and GDT user readable? In my kernel the GDT is static, it is once set up at system start and never modified, and it's contents are even in the source code. The same basically applies to the IDT, which is filled with the addresses of all interrupt stubs, and these are also known at system startup. So I see no information in the GDT and IDT that couldn't be also obtained from just looking at the source code / kernel binary / linker map.
Programmers' Hardware Database // GitHub user: xenos1984; OS project: NOS
User avatar
sortie
Member
Member
Posts: 931
Joined: Wed Mar 21, 2012 3:01 pm
Libera.chat IRC: sortie

Re: Paging Question

Post by sortie »

XenOS wrote:
sortie wrote:This means that the IDT/GDT/TSS must be mapped somewhere, but it doesn't need to be readable by user-space, indeed it really shouldn't be (or bad security vulnerabilities happen).
Just out of curiosity: What would be the security risk of having IDT and GDT user readable? In my kernel the GDT is static, it is once set up at system start and never modified, and it's contents are even in the source code. The same basically applies to the IDT, which is filled with the addresses of all interrupt stubs, and these are also known at system startup. So I see no information in the GDT and IDT that couldn't be also obtained from just looking at the source code / kernel binary / linker map.
Ah, I misstated. I meant writeable, where the attack is that the GDT is changed to make user-space kernel space, then do a syscall, and you are now the kernel executing user code. There are concerns with read access as well, but that's mostly just information leaks, if your kernel uses ALSR Internally. Otherwise it's harmless and public information given kernel sources. Still, there is no reason to make it user visible.
Post Reply