Why is INVLPG a privileged instrucion (x86)?

Discussions on more advanced topics such as monolithic vs micro-kernels, transactional memory models, and paging vs segmentation should go here. Use this forum to expand and improve the wiki!
Post Reply
jlledom
Posts: 7
Joined: Sat Jul 25, 2015 4:33 am

Why is INVLPG a privileged instrucion (x86)?

Post by jlledom »

Hello!

Not sure whether this is the proper place to ask this, but I've got a doubt about the x86 architecture.

My doubt is, why is INVLPG a privileged instruction? What could happen if a user program was able to execute it? It would be able to invalidate only the TLB entries of its own pages, isn't it?

Thanks!
alexfru
Member
Member
Posts: 1111
Joined: Tue Mar 04, 2014 5:27 am

Re: Why is INVLPG a privileged instrucion (x86)?

Post by alexfru »

This instruction does not distinguish between user and kernel pages. So, a misbehaving app would cause unnecessary memory reads (to refill the TLB) and introduce delays, which may be especially bad in the kernel and device drivers. And if some page tables that are needed to refill the TLB happen to be swapped out, this will incur additional disk reads and further increase delays. I'm not sure if there are other side effects.
Octocontrabass
Member
Member
Posts: 5517
Joined: Mon Mar 25, 2013 7:01 pm

Re: Why is INVLPG a privileged instrucion (x86)?

Post by Octocontrabass »

User mode programs can't modify page tables, so there's no reason to let them use INVLPG.
jlledom
Posts: 7
Joined: Sat Jul 25, 2015 4:33 am

Re: Why is INVLPG a privileged instrucion (x86)?

Post by jlledom »

alexfru wrote:This instruction does not distinguish between user and kernel pages. So, a misbehaving app would cause unnecessary memory reads (to refill the TLB) and introduce delays, which may be especially bad in the kernel and device drivers. And if some page tables that are needed to refill the TLB happen to be swapped out, this will incur additional disk reads and further increase delays. I'm not sure if there are other side effects.
Oh yes, I understand now. Thank you!
rdos
Member
Member
Posts: 3280
Joined: Wed Oct 01, 2008 1:55 pm

Re: Why is INVLPG a privileged instrucion (x86)?

Post by rdos »

Octocontrabass wrote:User mode programs can't modify page tables, so there's no reason to let them use INVLPG.
That's only a convention and not something that is enforced by the processor. It's perfectly possibly to map the page tables so user mode programs can access and modify them.
rdos
Member
Member
Posts: 3280
Joined: Wed Oct 01, 2008 1:55 pm

Re: Why is INVLPG a privileged instrucion (x86)?

Post by rdos »

alexfru wrote:This instruction does not distinguish between user and kernel pages. So, a misbehaving app would cause unnecessary memory reads (to refill the TLB) and introduce delays, which may be especially bad in the kernel and device drivers. And if some page tables that are needed to refill the TLB happen to be swapped out, this will incur additional disk reads and further increase delays. I'm not sure if there are other side effects.
I'd say that's a minor issue. It will only invalidate the cache, not the page tables themselves. Which entries are in the TLB is not really deterministic for the OS anyway. Any task switch might completely flush the TLB by changing CR3, and those are not really deterministic for a typical OS either. I'd say an OS that relies on certain TLB entries being cached is severely broken.

Still, I see no legitimate use for invlpg for a user program, but I cannot see how it can do much if any harm by using it either.
simeonz
Member
Member
Posts: 360
Joined: Fri Aug 19, 2016 10:28 pm

Re: Why is INVLPG a privileged instrucion (x86)?

Post by simeonz »

I will speculate that by using INVLPG and measuring the execution times of specific system calls with rdtsc (to detect TLB misses), unprivileged attacker will be able to discover important kernel locations. A following attack vector on a buggy driver (say, stack smashing) will be precisely targeted, counteracting kernel ASLR and the entropy of the dynamic allocator. An ordinary user-space "mov" should also be able to roughly sieve the address space, since it will cause conflict TLB misses eventually. INVLPG can be used as a second step to refine the search.

I am not deep into security, but it is conventional to assume some arbitrary compromise to begin with - trojan, exploit, counterfeit payload, etc. The goal is to limit the security ramifications. Some kind of mitigation is usually present, like ASLR, safe unlinking, W^X. The hardware must, as in this case, protect the effectiveness of the mitigation from heuristics.
Post Reply