Page 1 of 1
IRET with change of privilege level
Posted: Mon Jul 28, 2008 6:21 am
by sbot
I try to integrate a privilege level change with the setjmp.asm and longjmp.c context switch given in the OSD example codes. An IRET switching to another privilege level expects two more registers on the stack, resulting in the following expected stack layout:
+--------+
| ss |
+ -------+
| esp |
+ -------+
| eflags |
+ -------+
| cs |
+ -------+
| eip |
+ -------+
To obtain this layout I integrated a few lines at the top of longjmp.c (see below). But this results in a GPF. Is my approach too naive? Is there something missing?
Thanks for your help!
sbot
void longjmp(jmp_buf buf, int ret_val)
{
unsigned *esp;
if(ret_val == 0)
ret_val++;
buf->eax = ret_val;
esp = (unsigned *)buf->esp;
// --- My code starts ---
esp--;
*esp = 0x28; // --- this is my user segment
esp--;
*esp = buf->esp; // --- stack to use after IRET
// --- My code ends ---
esp--;
*esp = buf->eflags;
esp--;
__asm__ __volatile__(
"mov %%cs,%0\n"
: "=m"(*esp));
esp--;
*esp = buf->eip;
buf->esp = (unsigned)esp;
__asm__ __volatile__(
"movl %0,%%esp\n"
"popa\n"
"movl -20(%%esp),%%esp\n"
"iret\n"
:
: "m"(buf));
}
Re: IRET with change of privilege level
Posted: Mon Jul 28, 2008 6:24 am
by JamesM
"movl -20(%%esp),%%esp\n"
This will need changing as you've added two extra arguments to the stack. -28 should do it.
Re: IRET with change of privilege level
Posted: Mon Jul 28, 2008 3:12 pm
by sbot
I am not sure. There is no comment about this -20, but my understanding is that it moves the esp pointer (which is currently pointing to buf of type jmp_buf) to buf->esp. The context is the jmp_buf:
unsigned edi, esi, ebp, esp, ebx, edx, ecx, eax;
unsigned eip, eflags;
after popa we are "at beginning of the 2nd line" - the 8 registers are popped and the esp goes forward.
Now, before IRET, we switch zu buf->esp, the one "we created just above". There are 4 elements between the pointer and the esp, to get there we have to skip this 4 and the esp itself (to get "in front of it" ). Each element has 4 bytes, so we have to go back 4 * 5 byte = 20 byte.
In other words, the -20 has nothing to do with size of our current stack (which is buf->esp, not buf!).
Has anybody tried to use the context switch with privilege level change?
Re: IRET with change of privilege level
Posted: Tue Jul 29, 2008 1:26 am
by AJ
Hi,
JamesM knows what he is on about. Have you even tried his suggestion, or have you just dismissed it offhand? It does sound right that if you add 2 parameters to your stack, you then need to add an extra 8 bytes to end up at the same stack location. Remember that at the start of your routine you have set esp to buf->esp.
Personal Opinion: Do ISR-type stuff in an assembly ISR wrapper rather than inline assembly (even if you are using standard functions). *ducksandruns*.
Cheers,
Adam
Re: IRET with change of privilege level
Posted: Tue Jul 29, 2008 2:52 am
by sbot
Dear Adam,
thanks for reply. Yes, I tried James' suggestion. But it does not work. I believe that the reason is that IRET does not use the stack the -20 is applied to. As I already tried to point out, I think that this -20 just puts the other stack to esp (buf to buf->esp). The buf->esp that is used by IRET is readjusted by buf->esp = esp. And here, I think, is where my code is going wrong. By the way, a register dump shows that the uSS points to the new code segment (cs) and the uSP points to the eip. If I make the buf->esp point to the position right after the position where I put the esp, everything looks fine in the dump. But that can't be correct (and it does not work) because the code now would ignore eflags, cs and eip.
To work correctly, it is not necessary to change the code itself, but only to change the user code segment to super user privileges.
I am really curious if anyone actually tried to use the osd code to switch to user mode.
Thanks and best regards,
--sbot
Re: IRET with change of privilege level
Posted: Tue Jul 29, 2008 4:06 am
by JamesM
Hi,
Another thing -
*esp = 0x28; // --- this is my user segment
the 0x28 should be 0x2b - RPL of 3.
Re: IRET with change of privilege level
Posted: Tue Jul 29, 2008 2:22 pm
by sbot
YES! I forgot add the privilege level!
Thank you, James!