Shadow stacks for userspace
Subsystems (44)
Re: [PATCH v7 33/41] x86/shstk: Introduce map_shadow_stack syscall
In reply to Edgecombe, Rick PQuoted 188 lines · Rick P
Quoted 54 lines · Deepak Gupta
Quoted 53 lines · Rick P
Quoted 52 lines · Deepak Gupta
Quoted 50 lines · Rick P
Quoted 49 lines · Deepak Gupta
Quoted 46 lines · Szabolcs Nagy
Quoted 1 line · Rick Edgecombe
Previously, a new
PROT_SHADOW_STACKwas attempted,...
Quoted 22 lines
So rather than repurpose two existing syscalls (mmap, madvise)
that don't
quite fit, just implement a newmap_shadow_stacksyscall to
allow
userspace to map and setup new shadow stacks in one step. While
ucontext
is the primary motivator, userspace may have other unforeseen
reasons to
setup it's own shadow stacks using the WRSS instruction. Towards
this
provide a flag so that stacks can be optionally setup securely
for the
common case of ucontext without enabling WRSS. Or potentially
have the
kernel set up the shadow stack in some new way....
Quoted 6 lines
The following example demonstrates how to create a new shadow
stack withmap_shadow_stack:
void *shstk =map_shadow_stack(addr, stack_size, SHADOW_STACK_SET_TOKEN);i think
mmap(addr, size, PROT_READ, MAP_ANON|MAP_SHADOW_STACK, -1, 0);could do the same with less disruption to users (new syscalls
are harder to deal with than new flags). it would do the guard page and initial token setup too (there is no flag for
it but could be squeezed in).Quoted 6 lines
Quoted 6 lines
Quoted 4 lines
Quoted 4 lines
Quoted 2 lines
Discussion on this topic in v6
https://lore.kernel.org/all/[email protected]/
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Again I know earlier CET patches had protection flag and somehow
due
to pushback
on mailing list,
it was adopted to go for special syscall because no one else
had shadow stack.Quoted 14 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Seeing a response from Szabolcs, I am assuming arm4 would also
want
to followQuoted 10 lines
Quoted 8 lines
Quoted 8 lines
Quoted 6 lines
using mmap to manufacture shadow stack. For reference RFC patches
for
risc-v shadow stack,
use a new protection flag =PROT_SHADOWSTACK.https://lore.kernel.org/lkml/[email protected]/
Quoted 101 lines
Quoted 83 lines
Quoted 68 lines
Quoted 25 lines
Quoted 18 lines
I know earlier discussion had been that we let this go and do a
re-
factor later as other
arch support trickle in. But as I thought more on this and I think it
may just be
messy from user mode point of view as well to have cognition of
two
different ways of
creating shadow stack. One would be special syscall (in current
libc)
and anothermmap
(whenever future re-factor happens)Quoted 2 lines
If it's not too late, it would be more wise to take
mmapapproach rather than specialsyscallapproach.There is sort of two things intermixed here when we talk about a
PROT_SHADOW_STACK.Quoted 15 lines
One is: what is the interface for specifying how the shadow stack
should be provisioned with data? Right now there are two ways supported, all zero or with an X86 shadow stack restore token at
the
end. Then there was already some conversation about a third type.
In
which case the question would be is using mmapMAP_flags the right
place for this? How many types of initialization will be needed in
the
end and what is the overlap between the architectures?First of all, arches can choose to have token at the bottom or not.
Token serve following purposes
- It allows one to put desired value in shadow stack pointer in
safe/secure manner.
Note: x86 doesn't provide any opcode encoding to value in SSP
register. So having
a token is kind of a necessity because x86 doesn't easily
allow
writing shadow stack.- A token at the bottom acts marker / barrier and can be useful
in
debugging- If (and a big *if*) we ever reach a point in future where
return
address is only pushed
on shadow stack (x86 should have motivation to do this
because
less uops on call/ret),
a token at the bottom (bottom means lower address) is
ensuring
sure shot way of getting
a fault when exhausted.Quoted 6 lines
Current RISCV zisslpcfi proposal doesn't define CPU based tokens because it's RISC.
It allows mechanisms using which software can define formatting of
token for itself.
Not sure of what ARM is doing.Ok, so riscv doesn't need to have the kernel write the token, but x86
does.Quoted 3 lines
Now coming to the point of all zero v/s shadow stack token. Why not always have token at the bottom?
Quoted 7 lines
With WRSS you can setup the shadow stack however you want. So the user
would then have to take care to erase the token if they didn't want it.
Not the end of the world, but kind of clunky if there is no reason for
it.Yes but kernel always assumes the user is going to use the token. It' upto the user
to decide whether they want to use the restore token or not. If they've WRSS capability
security posture is anyways diluted. An attacker who would be clever enough to
re-useRSTORSSPpresent in address space to restore using kernel prepared token, should
anyways can be clever enough to use WRSS as well.
Quoted 8 lines
Quoted 3 lines
It kind of makes shadow stack creation simpler for kernel to always place the token.
This point is irrespective of whether to use system call or mmap.Think about like CRIU restoring the shadow stack, or other special cases like that. Userspace can always overwrite the token, but this involves some amount of extra work (extra writes, earlier faulting in the page, etc). It is clunky and very negligibly worse.
Earlier faulting in the page because the kernel is writing the token at base?
Quoted 23 lines
Quoted 23 lines
Quoted 22 lines
Quoted 4 lines
Quoted 9 lines
In case of x86, Why need for two ways and why not always have a token
at the bottom.
The way x86 is going, user mode is responsible for establishing shadow stack and thus
whenever shadow stack is created then if x86 kernel implementation
always place a token
at the base/bottom.There was also some discussion recently of adding a token AND an end of
stack marker, as a potential solution for backtracing in ucontext stacks. In this case it could cause an ABI break to just start adding
the end of stack marker where the token was, and so would require a newmap_shadow_stackflag.
Quoted 8 lines
Quoted 5 lines
Was this discussed why restore token itself can't be used as marker for
end of stack (if we assume there is always going to be one at the bottom).
It's a unique value. An address pointing to itself.I thought the same thing at first, but it gets clobbered during the pivot and push.
aah I remember. It was changed from savessp/rstorssp pair to rstorssp/saveprevssp to follow the make before break model.
Quoted 88 lines
Quoted 75 lines
Quoted 49 lines
Quoted 41 lines
Now user mode can do following:--
- If it has access to WRSS, it can sure go ahead and create a
token
of its choosing and
overwrite kernel created token. and then do RSTORSSP on it's
own
created token.- If it doesn't have access to WRSS (and dont need to create
its
own token), it can do
RSTORSSP on this. As soon as it does, no other thread in
process
can restore to it.
Onfork, you get the same un-restorable token.So why not always have a token at the bottom.
This is my plan for riscv implementation as well (to have a token at
the bottom)Quoted 15 lines
The other thing is: should shadow stack memory creation be tightly
controlled? For example in x86 we limit this to anonymous memory,
etc.
Some reasons for this are x86 specific, but some are not. So if we
disallow most of the options why allow the interface to take them?
And
then you are in the position of carefully maintaining a list of not-
allowed options instead letting a list of allowed options sit there.Quoted 7 lines
I am new to linux kernel and thus may be not able to follow the argument of
limiting to anonymous memory.Why is limiting it to anonymous memory a problem. IIRC, ARM's
PROT_MTEis applicable
only to anonymous memory. I can probably find few more examples.Quoted 6 lines
Oh I see, they have a special arch VMA flag
VM_MTE_ALLOWEDthat only
gets set if all the rules are followed. ThenPROT_MTEcan only be set
on that to setVM_MTE. That is kind of nice because certain other special situations can choose to support it.That's because MTE is different. It allows to assign tags to existing virtual memory. So one need to know whether a memory can have tags assigned.
Quoted 14 lines
It does take another arch vma flag though. For x86 I guess I would need
to figure out how to squeezeVM_SHADOW_STACKinto other flags to have a
free flag to use the same method. It also only supportsmprotect()and
shadow stack would only want to supportmmap(). And you still have the
initialization stuff to plumb through. Yea, I think thePROT_MTEis a
good thing to consider, but it's not super obvious to me how similar
the logic would be for shadow stack.Quoted 3 lines
I dont think you need another VMA flag. Memory tagging allows adding tags
to existing virtual memory....need another VMA flag to use the existing mmap arch breakouts in the same way as
VM_MTE. Of course changing mmap makes other solutions possible.Quoted 4 lines
That's why having
mprotectmakes sense for MTE.
In shadow stack case, there is no requirement of changing a shadow stack
to regular memory or vice-versa.
Quoted 5 lines
uffd needs mprotect internals. You might take a look at it in regards to your VM_WRITE/mprotect blocking approach for riscv. I was imagining, even if mmap was the syscall,
mprotect()would not be blocked in the x86 case at least. Themprotect()blocking is a separate thing than the syscall, right?
Yes, mprotect blocking is a different thing.VM_XXX flags are not exposed to mprotect (or any memory mapping API). PROT_XXX flags are. On riscv, in my current plan if mprotect or mmap specifies PROT_WRITE (no PROT_READ),
It'll be mapped to VM_READ | VM_WRITE on vma flags (this is to make sure we don't break compat with existing user code which has been using only PROT_WRITE)
If PROT_SHADOWSTACK (new protection flag) is specified, it'll be mapped to VM_WRITE on the vma_flag.
Yes I am aware of uffd. I intend to handle it the same way I am handling the fork of riscv shadow stack.
core-mm write protect checks if VM_WRiTE is specified it'll convert PTE encodings to read-only.
uffd for regular memory should work as it is. In case if someone was monitoring shadow stack memory, following could occur
1) A write happens on shadow stack memory, a store page fault would occur.
2) A read happens, this would be allowed.
3) A shadow stack load / store happens, a store access fault would occur.
Case 1 and 3 are reported to the kernel and it can make sure the uffd monitor is reported about it.
Was there a specific concern here with respect to uffd and x86 shadow stack?
Quoted 193 lines
Quoted 42 lines
All that's needed to change is
mmap.mprotectshould fail. Syscall
approach gives that benefit by default because there is no protection flag
for shadow stack.I was giving example that any feature which gives new meaning to virtual memory
has been able to work with existing memory mapping APIs without the need of new
system call (including whether you're dealing with anonymous memory).Quoted 10 lines
The question I'm asking though is, not "can mmap code and rules be changed to enforce the required limitations?". I think it is yes. But
the question is "why is that plumbing better than a new syscall?". I
guess to get a better idea, the mmap solution would need to get POCed.
I had half done this at one point, but abandoned the approach.Quoted 11 lines
For your question about why limit it, the special x86 case is the Dirty=1,Write=0 PTE bit combination for shadow stacks. So for shadow
stack you could have some confusion about whether a PTE is actually dirty for writeback, etc. I wouldn't say it's known to be impossible to
doMAP_SHARED, but it has not been fully analyzed enough to know what
the changes would be. There were some solvable concrete issues that tipped the scale as well. It was also not expected to be a common usage, if at all.I am not sure how confusion of D=1,W=0 is not completely taken away by
syscall approach. It'll always be there. One can only do things to minimize
the chances.Quoted 6 lines
In case of syscall approach, syscall makes sure that
flags = MAP_ANONYMOUS | MAP_PRIVATE | MAP_ABOVE4GThis can be easily checked in arch specific landing function for mmap.
Right, this is why I listed two types of things in the mix here. The memory features supported, and what the syscall is. You asked why limit the memory features, so that is the explanation.
Quoted 15 lines
Additionally, If you always have the token at base, you don't need that ABI
between user and kernel.Quoted 7 lines
The non-x86, general reasons for it, are for a smaller benefit. It blocks a lot of ways shadow stack memory could be written to. Like say
you have a memory mapped writable file, and you also map it shadow stack. So it has better security properties depending on what your threat model is.Quoted 5 lines
I wouldn't say any architecture should allow such primitives. It kind of defeats
the purpose for shadow stack. Yes if some sort of secure memory is needed, there may
be new ISA extensions for that.Yea, seems reasonable to prevent this regardless of the extra x86 reasons, if that is what you are saying. It depends on people's threat models (as always in security).
Quoted 102 lines
Quoted 95 lines
Quoted 49 lines
Eventually syscall will also go ahead and use memory management code
to
perform mapping. So I didn't understand the reasoning here. The way
syscall
can limit it to anonymous memory, why mmap can't do the same if it
seesPROT_SHADOWSTACK.Quoted 12 lines
The only benefit I've heard is that it saves creating a new syscall,
but it also saves severalMAP_flags. That, and that the RFC for
riscv
did aPROT_SHADOW_STACKto start. So, yes, two people asked the same
question, but I'm still not seeing any benefits. Can you give the
pros
and cons please?Again the way syscall will limit it to anonymous memory, Why mmap can't do same?
There is precedence for it (likePROT_MTEis applicable only to anonymous memory)So if it can be done, then why introduce a new syscall?
Quoted 10 lines
BTW, in glibc
map_shadow_stackis called from arch code. So I think
userspace wise, for this to affect other architectures there would
need
to be some code that could do things generically, with somehow the
shadow stack pivot abstracted but the shadow stack allocation not.Agreed, yes it can be done in a way where it won't put tax on other
architectures.Quoted 11 lines
But what about fragmentation within x86. Will x86 always choose to
use system call
method map shadow stack. If future re-factor results in x86 also usemmapmethod.
Isn't it a mess for x86 glibc to figure out what to do; whether to
use system call
ormmap?Ok, so this is the downside I guess. What happens if we want to support
the other types of memory in the future and end up using mmap for this?
Then we have 15-20 lines of extra syscall wrapping code to maintain to
support legacy.For the mmap solution, we have the downside of using extra
MAP_flags,
and *some* amount of currently unknownvm_flagand address range logic,
plus mmap arch breakouts to add to core MM. Like I said earlier, you
would need to POC it out to see how bad that looks and get some core MM
feedback on the new type of MAP flag usage. But, syscalls being pretty
straightforward, it would probably be *some* amount of added complexity
_now_ to support something that might happen in the future. I'm not seeing either one as a landslide win.It's kind of an eternal software design philosophical question, isn't
it? How much work should you do to prepare for things that might be needed in the future? From what I've seen the balance in the kernel seems to be to try not to paint yourself in to an ABI corner, but otherwise let the kernel evolve naturally in response to real usages.
If anyone wants to correct this, please do. But otherwise I think the
new syscall is aligned with that.Quoted 6 lines
TBH, you are making me wonder if I'm missing something. It seems you
strongly don't prefer this approach, but I'm not hearing any huge potential negative impacts. And you also say it won't tax the riscv implementation. Is this just something just smells bad here? Or it would shrink the riscv series?Quoted 8 lines
No you're not missing anything. It's just wierdness of adding a system call
which enforces certainMAP_XXflags and pretty much mapping API. And difference between architectures on how they will create shadow stack. +
if x86 chooses to usemmapin future, then there is ugliness in user mode to
decide which method to choose.Ok, I think I will leave it given it's entirely in
arch/x86. It just got some special error codes in the other thread today too.Quoted 1 line
Quoted 6 lines
Quoted 4 lines
And yes you got it right, to some extent there is my own selfishness playing out
as well here to reduce riscv patches.Feel free to join the
map_shadow_stackparty. :)
I am warming up to it :-)
Mike Rapoport
maintainerRe: [PATCH v7 33/41] x86/shstk: Introduce map_shadow_stack syscall
In reply to Deepak GuptaHi,
Quoted 38 lines · Deepak Gupta
Quoted 16 lines · Szabolcs Nagy
Quoted 1 line · Rick Edgecombe
Previously, a new
PROT_SHADOW_STACKwas attempted,...
Quoted 8 lines
So rather than repurpose two existing syscalls (mmap, madvise) that don't quite fit, just implement a new
map_shadow_stacksyscall to allow userspace to map and setup new shadow stacks in one step. While ucontext is the primary motivator, userspace may have other unforeseen reasons to setup it's own shadow stacks using the WRSS instruction. Towards this provide a flag so that stacks can be optionally setup securely for the common case of ucontext without enabling WRSS. Or potentially have the kernel set up the shadow stack in some new way....
Quoted 3 lines
The following example demonstrates how to create a new shadow stack with
map_shadow_stack:
void *shstk =map_shadow_stack(addr, stack_size, SHADOW_STACK_SET_TOKEN);Quoted 8 lines
i think
mmap(addr, size, PROT_READ, MAP_ANON|MAP_SHADOW_STACK, -1, 0);could do the same with less disruption to users (new syscalls are harder to deal with than new flags). it would do the guard page and initial token setup too (there is no flag for it but could be squeezed in).
Discussion on this topic in v6
https://lore.kernel.org/all/[email protected]/Again I know earlier CET patches had protection flag and somehow due to pushback on mailing list, it was adopted to go for special syscall because no one else had shadow stack.
Seeing a response from Szabolcs, I am assuming arm4 would also want to follow using mmap to manufacture shadow stack. For reference RFC patches for risc-v shadow stack, use a new protection flag =
PROT_SHADOWSTACK.
https://lore.kernel.org/lkml/[email protected]/
Quoted 8 lines
I know earlier discussion had been that we let this go and do a re-factor later as other arch support trickle in. But as I thought more on this and I think it may just be messy from user mode point of view as well to have cognition of two different ways of creating shadow stack. One would be special syscall (in current libc) and another
mmap(whenever future re-factor happens)If it's not too late, it would be more wise to take
mmapapproach rather than specialsyscallapproach.
I disagree.
Having shadow stack flags for mmap() adds unnecessary complexity to the core-mm, while having a dedicated syscall hides all the details in the architecture specific code.
Another reason to use a dedicated system call allows for better extensibility if/when we'd need to update the way shadow stack VMA is created.
As for the userspace convenience, it is anyway required to add special code for creating the shadow stack and it wouldn't matter if that code would use mmap(NEW_FLAG) or map_shadow_stack().
Quoted 5 lines
Quoted 5 lines
most of the mmap features need not be available (EINVAL) when
MAP_SHADOW_STACKis specified.the main drawback is running out of mmap flags so extension is limited. (but the new syscall has limitations too).
Signature · 2 lines
Sincerely yours, Mike.
Re: [PATCH v7 33/41] x86/shstk: Introduce map_shadow_stack syscall
Quoted 48 lines · Mike Rapoport
Hi,
Quoted 44 lines · Deepak Gupta
Quoted 16 lines · Szabolcs Nagy
Quoted 1 line · Rick Edgecombe
Previously, a new
PROT_SHADOW_STACKwas attempted,...
Quoted 8 lines
So rather than repurpose two existing syscalls (mmap, madvise) that don't quite fit, just implement a new
map_shadow_stacksyscall to allow userspace to map and setup new shadow stacks in one step. While ucontext is the primary motivator, userspace may have other unforeseen reasons to setup it's own shadow stacks using the WRSS instruction. Towards this provide a flag so that stacks can be optionally setup securely for the common case of ucontext without enabling WRSS. Or potentially have the kernel set up the shadow stack in some new way....
Quoted 3 lines
The following example demonstrates how to create a new shadow stack with
map_shadow_stack:
void *shstk =map_shadow_stack(addr, stack_size, SHADOW_STACK_SET_TOKEN);Quoted 8 lines
i think
mmap(addr, size, PROT_READ, MAP_ANON|MAP_SHADOW_STACK, -1, 0);could do the same with less disruption to users (new syscalls are harder to deal with than new flags). it would do the guard page and initial token setup too (there is no flag for it but could be squeezed in).
Discussion on this topic in v6
https://lore.kernel.org/all/[email protected]/Again I know earlier CET patches had protection flag and somehow due to pushback on mailing list, it was adopted to go for special syscall because no one else had shadow stack.
Seeing a response from Szabolcs, I am assuming arm4 would also want to follow using mmap to manufacture shadow stack. For reference RFC patches for risc-v shadow stack, use a new protection flag =
PROT_SHADOWSTACK.
https://lore.kernel.org/lkml/[email protected]/I know earlier discussion had been that we let this go and do a re-factor later as other arch support trickle in. But as I thought more on this and I think it may just be messy from user mode point of view as well to have cognition of two different ways of creating shadow stack. One would be special syscall (in current libc) and another
mmap(whenever future re-factor happens)
Quoted 8 lines
Quoted 2 lines
If it's not too late, it would be more wise to take
mmapapproach rather than specialsyscallapproach.I disagree.
Having shadow stack flags for
mmap()adds unnecessary complexity to the core-mm, while having a dedicated syscall hides all the details in the architecture specific code.
Again reiterating it would've made sense if only x86 had a shadow stack. aarch64 announced support for guarded stack. risc-v spec is in development to support shadow stack.
So there will be shadow stack related flow in these arches.
Quoted 4 lines
Another reason to use a dedicated system call allows for better extensibility if/when we'd need to update the way shadow stack VMA is created.
I see two valid points here
- Shadow stack doesn't need conversion into different memory types
(which is usually the case for address ranges created by mmap)
So there is a static page permissions on shadow stack which is
not mutable.
- Future feature addition (if there is one needed) at the time of
shadow stack creation
It would avoid future tax on mmap
I'll think more about this.
Quoted 4 lines
As for the userspace convenience, it is anyway required to add special code for creating the shadow stack and it wouldn't matter if that code would use
mmap(NEW_FLAG)ormap_shadow_stack().
Yes *strictly* from userspace convenience, it doesn't matter which option.
Quoted 10 lines
Quoted 6 lines
Quoted 6 lines
most of the mmap features need not be available (EINVAL) when
MAP_SHADOW_STACKis specified.the main drawback is running out of mmap flags so extension is limited. (but the new syscall has limitations too).
--
Sincerely yours,
Mike.
Re: [PATCH v7 33/41] x86/shstk: Introduce map_shadow_stack syscall
Quoted 6 lines · Deepak Gupta
Quoted 3 lines · Mike Rapoport
As for the userspace convenience, it is anyway required to add special code for creating the shadow stack and it wouldn't matter if that code would use
mmap(NEW_FLAG)ormap_shadow_stack().Yes *strictly* from userspace convenience, it doesn't matter which option.
everybody seems to assume that the new syscall only matters for the code allocating the shadow stack.
there are tools like strace, seccomp,.. that need to learn about the new syscall and anything that's built on top of them as well as libc api interposers like address sanitizer need to learn about the related new libc apis (if there are any.. which will be another long debate on the userspace side, delaying the usability of shadow stacks even more). such tools already know about mmap and often can handle new flags without much change.
i agree that too much special logic in mmap is not ideal and using an mmap flag limits future extensions of both mmap and shadow map functionality. but i disagree that a new syscall is generally easy for userspace to deal with. in this case the cost seems acceptable to me, but it's not free at all.
Borislav Petkov
maintainerRe: [PATCH v7 33/41] x86/shstk: Introduce map_shadow_stack syscall
In reply to Rick EdgecombeQuoted 10 lines · Rick Edgecombe
When operating with shadow stacks enabled, the kernel will automatically allocate shadow stacks for new threads, however in some cases userspace will need additional shadow stacks. The main example of this is the ucontext family of functions, which require userspace allocating and pivoting to userspace managed stacks.
Unlike most other user memory permissions, shadow stacks need to be provisioned with special data in order to be useful. They need to be setup with a restore token so that userspace can pivot to them via the RSTORSSP instruction. But, the security design of shadow stack's is that they
"stacks"
Quoted 7 lines
should not be written to except in limited circumstances. This presents a problem for userspace, as to how userspace can provision this special data, without allowing for the shadow stack to be generally writable.
Previously, a new
PROT_SHADOW_STACKwas attempted, which could be mprotect()ed from RW permissions after the data was provisioned. This was found to not be secure enough, as other thread's could write to the
"threads"
Quoted 9 lines
shadow stack during the writable window.
The kernel can use a special instruction, WRUSS, to write directly to userspace shadow stacks. So the solution can be that memory can be mapped as shadow stack permissions from the beginning (never generally writable in userspace), and the kernel itself can write the restore token.
First, a new
madvise()flag was explored, which could operate on thePROT_SHADOW_STACKmemory. This had a couple downsides:
^ of
Quoted 10 lines
1. Extra checks were needed in
mprotect()to prevent writable memory from ever becomingPROT_SHADOW_STACK.
2. Extra checks/vma state were needed in the newmadvise()to prevent restore tokens being written into the middle of pre-used shadow stacks. It is ideal to prevent restore tokens being added at arbitrary locations, so the check was to make sure the shadow stack had never been written to.
3. It stood out from the rest of the madvise flags, as more of direct action than a hint at future desired behavior.
Quoted 5 lines
So rather than repurpose two existing syscalls (mmap, madvise) that don't quite fit, just implement a new
map_shadow_stacksyscall to allow userspace to map and setup new shadow stacks in one step. While ucontext is the primary motivator, userspace may have other unforeseen reasons to setup it's own shadow stacks using the WRSS instruction. Towards this
"its"
Quoted 7 lines
provide a flag so that stacks can be optionally setup securely for the common case of ucontext without enabling WRSS. Or potentially have the kernel set up the shadow stack in some new way.
The following example demonstrates how to create a new shadow stack with
map_shadow_stack:
void *shstk =map_shadow_stack(addr, stack_size, SHADOW_STACK_SET_TOKEN);
...
arch/x86/entry/syscalls/syscall_64.tbl line 375
Quoted 9 lines
arch/x86/entry/syscalls/syscall_64.tbl +1
-0@@ -372,6 +372,7 @@ 448 common process_mrelease sys_process_mrelease 449 common futex_waitv sys_futex_waitv 450 common set_mempolicy_home_node sys_set_mempolicy_home_node +451 64 map_shadow_stack sys_map_shadow_stack
Yeah, this'll need a manpage too, I presume. But later.
Quoted 6 lines
arch/x86/entry/syscalls/syscall_64.tbl +6
-0+SYSCALL_DEFINE3(map_shadow_stack, unsigned long, addr, unsigned long, size, unsigned int, flags) +{ + bool set_tok = flags & SHADOW_STACK_SET_TOKEN; + unsigned long aligned_size; + + if (!cpu_feature_enabled(X86_FEATURE_USER_SHSTK))
arch/x86/entry/syscalls/syscall_64.tbl
Quoted 10 lines
arch/x86/entry/syscalls/syscall_64.tbl +10
-0+ return -EOPNOTSUPP; + + if (flags & ~SHADOW_STACK_SET_TOKEN) + return -EINVAL; + + /* If there isn't space for a token */ + if (set_tok && size < 8) + return -EINVAL; + + if (addr && addr <= 0xFFFFFFFF)
< SZ_4G
arch/x86/entry/syscalls/syscall_64.tbl
Quoted 1 line
arch/x86/entry/syscalls/syscall_64.tbl +1
-0+ return -EINVAL;
Can we use distinct negative retvals in each case so that it is clear to userspace where it fails, *if* it fails?
Quoted 11 lines
arch/x86/entry/syscalls/syscall_64.tbl +11
-0+ /* + * An overflow would result in attempting to write the restore token + * to the wrong location. Not catastrophic, but just return the right + * error code and block it. + */ + aligned_size = PAGE_ALIGN(size); + if (aligned_size < size) + return -EOVERFLOW; + + return alloc_shstk(addr, aligned_size, size, set_tok); +}
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [PATCH v7 33/41] x86/shstk: Introduce map_shadow_stack syscall
On Fri, 2023-03-10 at 17:11 +0100, Borislav Petkov wrote:
[...]
Thanks on all the text edits.
Quoted 7 lines · Rick Edgecombe
Quoted 6 lines
arch/x86/entry/syscalls/syscall_64.tbl +0
-0@@ -372,6 +372,7 @@
Quoted 8 lines
Quoted 6 lines
arch/x86/entry/syscalls/syscall_64.tbl +0
-0448 common process_mrelease sys_process_mrelease
449 commonfutex_waitvsys_futex_waitv
450 commonset_mempolicy_home_nodesys_set_mempolicy_home_node
+451 64map_shadow_stacksys_map_shadow_stackYeah, this'll need a manpage too, I presume. But later.
I have one to submit.
[...]
Quoted 10 lines
arch/x86/entry/syscalls/syscall_64.tbl
Quoted 2 lines
arch/x86/entry/syscalls/syscall_64.tbl +2
-0+ + if (addr && addr <= 0xFFFFFFFF)< SZ_4Garch/x86/entry/syscalls/syscall_64.tbl
Quoted 1 line
arch/x86/entry/syscalls/syscall_64.tbl +1
-0+ return -EINVAL;Can we use distinct negative retvals in each case so that it is clear to
userspace where it fails, *if* it fails?
Good idea, I think maybe ERANGE.
Borislav Petkov
maintainerRe: [PATCH v7 33/41] x86/shstk: Introduce map_shadow_stack syscall
Quoted 5 lines · Rick P
Quoted 3 lines
Can we use distinct negative retvals in each case so that it is clear to
userspace where it fails, *if* it fails?Good idea, I think maybe ERANGE.
For those two, right?
/* If there isn't space for a token */
if (set_tok && size < 8)
return -EINVAL;
if (addr && addr <= 0xFFFFFFFF)
return -EINVAL;They are kinda range-checking of sorts. A wider range but still similar...
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [PATCH v7 33/41] x86/shstk: Introduce map_shadow_stack syscall
Quoted 17 lines · Borislav Petkov
Quoted 6 lines · Rick P
Quoted 4 lines
Can we use distinct negative retvals in each case so that it is clear
to
userspace where it fails, *if* it fails?Good idea, I think maybe ERANGE.
For those two, right?
/* If there isn't space for a token */ if (set_tok && size < 8) return -EINVAL; if (addr && addr <= 0xFFFFFFFF) return -EINVAL;
Quoted 2 lines
They are kinda range-checking of sorts. A wider range but still similar...
I was thinking ERANGE would be for the 4GB limit. This is the weird 32 bit limiting thing. So if someone hit it they could look up in the docs what is going on. The size-big-enough-for-a-token check could be ENOSPC? Then each reason could have a different error code.
Borislav Petkov
maintainerRe: [PATCH v7 33/41] x86/shstk: Introduce map_shadow_stack syscall
Quoted 4 lines · Rick P
I was thinking ERANGE would be for the 4GB limit. This is the weird 32 bit limiting thing. So if someone hit it they could look up in the docs what is going on. The size-big-enough-for-a-token check could be ENOSPC? Then each reason could have a different error code.
Yah, makes perfect sense to me.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquettePATCH v7 34/41 x86/shstk: Support WRSS for userspace
In reply to Rick EdgecombeFor the current shadow stack implementation, shadow stacks contents can't easily be provisioned with arbitrary data. This property helps apps protect themselves better, but also restricts any potential apps that may want to do exotic things at the expense of a little security.
The x86 shadow stack feature introduces a new instruction, WRSS, which can be enabled to write directly to shadow stack permissioned memory from userspace. Allow it to get enabled via the prctl interface.
Only enable the userspace WRSS instruction, which allows writes to userspace shadow stacks from userspace. Do not allow it to be enabled independently of shadow stack, as HW does not support using WRSS when shadow stack is disabled.
From a fault handler perspective, WRSS will behave very similar to WRUSS, which is treated like a user access from a #PF err code perspective.
---
v6:
- Make set_clr_bits_msrl() avoid side affects in 'msr'
v5:
- Switch to EOPNOTSUPP
- Move set_clr_bits_msrl() to patch where it is first used
- Commit log formatting
v3:
- Make wrss_control() static
- Fix verbiage in commit log (Kees)
v2:
- Add some commit log verbiage from (Dave Hansen)
v1:
- New patch.
--- arch/x86/include/asm/msr.h | 11 +++++++++++ arch/x86/include/uapi/asm/prctl.h | 1 + arch/x86/kernel/shstk.c | 32 ++++++++++++++++++++++++++++++- 3 files changed, 43 insertions(+), 1 deletion(-)
arch/x86/include/asm/msr.h +11 -0
@@ -310,6 +310,17 @@ void msrs_free(struct msr *msrs);int msr_set_bit (u32 msr,u8 bit);int msr_clear_bit (u32 msr,u8 bit); +/* Helper that can never get accidentally un-inlined. */ +#define set_clr_bits_msrl (msr, set, clear) do { \ + u64 __val, __new_val, __msr = msr; \ + \ + rdmsrl(__msr, __val); \ + __new_val = (__val & ~(clear)) | (set); \ + \ + if (__new_val != __val) \ + wrmsrl(__msr, __new_val); \ +} while (0) +#ifdef CONFIG_SMP int rdmsr_on_cpu (unsigned int cpu,u32 msr_no,u32 *l,u32 *h);int wrmsr_on_cpu (unsigned int cpu,u32 msr_no,u32 l,u32 h);
arch/x86/include/uapi/asm/prctl.h +1 -0
@@ -28,5 +28,6 @@/* ARCH_SHSTK_ features bits */ #define ARCH_SHSTK_SHSTK (1ULL << 0) +#define ARCH_SHSTK_WRSS (1ULL << 1)#endif /* _ASM_X86_PRCTL_H */
arch/x86/kernel/shstk.c +31 -1
@@ -363,6 +363,36 @@ void shstk_free(struct task_struct *tsk)unmap_shadow_stack (shstk->base, shstk->size); } +static int wrss_control (bool enable) +{ +if (!cpu_feature_enabled (X86_FEATURE_USER_SHSTK )) +return -EOPNOTSUPP ; + +/* +* Only enable wrss if shadow stack is enabled. If shadow stack is not +* enabled, wrss will already be disabled, so don't bother clearing it +* when disabling. +*/ +if (!features_enabled (ARCH_SHSTK_SHSTK )) +return -EPERM ; + +/* Already enabled/disabled? */ +if (features_enabled (ARCH_SHSTK_WRSS ) == enable) +return 0 ; + +fpregs_lock_and_load (); +if (enable) { +set_clr_bits_msrl (MSR_IA32_U_CET ,CET_WRSS_EN ,0 ); +features_set (ARCH_SHSTK_WRSS ); + }else { +set_clr_bits_msrl (MSR_IA32_U_CET ,0 ,CET_WRSS_EN ); +features_clr (ARCH_SHSTK_WRSS ); + } +fpregs_unlock (); + +return 0 ; +} +static int shstk_disable (void ) {if (!cpu_feature_enabled (X86_FEATURE_USER_SHSTK ))
@@ -379,7 +409,7 @@ static int shstk_disable(void)fpregs_unlock ();shstk_free (current); -features_clr (ARCH_SHSTK_SHSTK ); +features_clr (ARCH_SHSTK_SHSTK |ARCH_SHSTK_WRSS );return 0 ; }
Signature · 1 line
2.17.1
Borislav Petkov
maintainerRe: [PATCH v7 34/41] x86/shstk: Support WRSS for userspace
Quoted 7 lines · Rick Edgecombe
For the current shadow stack implementation, shadow stacks contents can't easily be provisioned with arbitrary data. This property helps apps protect themselves better, but also restricts any potential apps that may want to do exotic things at the expense of a little security.
The x86 shadow stack feature introduces a new instruction, WRSS, which can be enabled to write directly to shadow stack permissioned memory from
s/permissioned //
By now it is clear that shadow stack memory is a special thing anyway.
Quoted 9 lines
userspace. Allow it to get enabled via the prctl interface.
Only enable the userspace WRSS instruction, which allows writes to userspace shadow stacks from userspace. Do not allow it to be enabled independently of shadow stack, as HW does not support using WRSS when shadow stack is disabled.
From a fault handler perspective, WRSS will behave very similar to WRUSS, which is treated like a user access from a #PF err code perspective.
...
arch/x86/include/asm/msr.h void msrs_free(struct msr *msrs); · line 314
Quoted 10 lines
arch/x86/include/asm/msr.h +2
-0@@ -310,6 +310,17 @@ void msrs_free(struct msr *msrs);int msr_set_bit (u32 msr,u8 bit);int msr_clear_bit (u32 msr,u8 bit); +/* Helper that can never get accidentally un-inlined. */ +#define set_clr_bits_msrl (msr, set, clear) do { \
Uff, pls kill this thing.
Our MSR interfaces universe is already insane and arch/x86/lib/msr.c already has similar attempts to what you're doing here in addition to all the other gunk in msr.h.
I highly doubt this can't be done the usual way, lemme see...
Quoted 28 lines
arch/x86/include/asm/msr.h +9
-0+u64 __val, __new_val, __msr = msr; \ + \ +rdmsrl (__msr, __val); \ + __new_val = (__val & ~(clear)) | (set); \ + \ +if (__new_val != __val) \ +wrmsrl (__msr, __new_val); \ +} while (0) +#ifdef CONFIG_SMP int rdmsr_on_cpu (unsigned int cpu,u32 msr_no,u32 *l,u32 *h);int wrmsr_on_cpu (unsigned int cpu,u32 msr_no,u32 l,u32 h);arch/x86/include/uapi/asm/prctl.h +1
-0@@ -28,5 +28,6 @@/* ARCH_SHSTK_ features bits */ #define ARCH_SHSTK_SHSTK (1ULL << 0) +#define ARCH_SHSTK_WRSS (1ULL << 1)#endif /* _ASM_X86_PRCTL_H */ arch/x86/kernel/shstk.c +0
-0
arch/x86/kernel/shstk.c void shstk_free(struct task_struct *tsk) · line 372
Quoted 10 lines
arch/x86/kernel/shstk.c +7
-0@@ -363,6 +363,36 @@ void shstk_free(struct task_struct *tsk)unmap_shadow_stack (shstk->base, shstk->size); } +static int wrss_control (bool enable) +{ +if (!cpu_feature_enabled (X86_FEATURE_USER_SHSTK )) +return -EOPNOTSUPP ; + +/* +* Only enable wrss if shadow stack is enabled. If shadow stack is not
"WRSS". Insns in all caps pls.
arch/x86/kernel/shstk.c void shstk_free(struct task_struct *tsk) · line 373
Quoted 1 line
arch/x86/kernel/shstk.c +1
-0+* enabled, wrss will already be disabled, so don't bother clearing it
Ditto.
Quoted 8 lines
arch/x86/kernel/shstk.c +8
-0+* when disabling. +*/ +if (!features_enabled (ARCH_SHSTK_SHSTK )) +return -EPERM ; + +/* Already enabled/disabled? */ +if (features_enabled (ARCH_SHSTK_WRSS ) == enable) +return 0 ;
arch/x86/kernel/shstk.c void shstk_free(struct task_struct *tsk) · line 391
Quoted 10 lines
arch/x86/kernel/shstk.c +10
-0+ +fpregs_lock_and_load (); +if (enable) { +set_clr_bits_msrl (MSR_IA32_U_CET ,CET_WRSS_EN ,0 ); +features_set (ARCH_SHSTK_WRSS ); + }else { +set_clr_bits_msrl (MSR_IA32_U_CET ,0 ,CET_WRSS_EN ); +features_clr (ARCH_SHSTK_WRSS ); + } +fpregs_unlock ();
Yes, doing it the "usual" way is more readable because it is a common code pattern which one encounters all around arch/x86/.
Diff ontop:
arch/x86/kernel/shstk.c +13 -2
@@ -365,6 +365,8 @@ void shstk_free(struct task_struct *tsk)static int wrss_control (bool enable) { +u64 msrval; +if (!cpu_feature_enabled (X86_FEATURE_USER_SHSTK ))return -EOPNOTSUPP ;
@@ -381,13 +383,22 @@ static int wrss_control(bool enable)return 0 ;fpregs_lock_and_load (); +rdmsrl (MSR_IA32_U_CET , msrval); +if (enable) { -set_clr_bits_msrl (MSR_IA32_U_CET ,CET_WRSS_EN ,0 );features_set (ARCH_SHSTK_WRSS ); + msrval |=CET_WRSS_EN ; }else { -set_clr_bits_msrl (MSR_IA32_U_CET ,0 ,CET_WRSS_EN );features_clr (ARCH_SHSTK_WRSS ); +if (!(msrval &CET_WRSS_EN )) + goto unlock; + + msrval &= ~CET_WRSS_EN ; } + +wrmsrl (MSR_IA32_U_CET , msrval); + +unlock:fpregs_unlock ();return 0 ;
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [PATCH v7 34/41] x86/shstk: Support WRSS for userspace
On Fri, 2023-03-10 at 17:44 +0100, Borislav Petkov wrote:
Thanks on text edits.
Quoted 11 lines
Review
Quoted 3 lines
(quoted diff) +2
-0+/* Helper that can never get accidentally un-inlined. */ +#define set_clr_bits_msrl(msr, set, clear) do { \Uff, pls kill this thing.
Our MSR interfaces universe is already insane and
arch/x86/lib/msr.calready has similar attempts to what you're doing here in addition to all the other gunk inmsr.h.I highly doubt this can't be done the usual way, lemme see...
Seems reasonable.
PATCH v7 35/41 x86: Expose thread features in /proc/$PID/status
In reply to Rick EdgecombeApplications and loaders can have logic to decide whether to enable shadow stack. They usually don't report whether shadow stack has been enabled or not, so there is no way to verify whether an application actually is protected by shadow stack.
Add two lines in /proc/$PID/status to report enabled and locked features.
Since, this involves referring to arch specific defines in asm/prctl.h, implement an arch breakout to emit the feature lines.
---
v4:
- Remove "CET" references
v3:
- Move to /proc/pid/status (Kees)
v2:
- New patch
--- arch/x86/kernel/cpu/proc.c | 23 +++++++++++++++++++++++ fs/proc/array.c | 6 ++++++ include/linux/proc_fs.h | 2 ++ 3 files changed, 31 insertions(+)
arch/x86/kernel/cpu/proc.c +23 -0
@@ -4,6 +4,8 @@#include <linux/string.h> #include <linux/seq_file.h> #include <linux/cpufreq.h> +#include <asm/prctl.h> +#include <linux/proc_fs.h> #include "cpu.h"
@@ -175,3 +177,24 @@ const struct seq_operations cpuinfo_op = { .stop = c_stop, .show = show_cpuinfo, }; + +#ifdef CONFIG_X86_USER_SHADOW_STACK +static void dump_x86_features (struct seq_file *m,unsigned long features) +{ +if (features &ARCH_SHSTK_SHSTK ) +seq_puts (m,"shstk " ); +if (features &ARCH_SHSTK_WRSS ) +seq_puts (m,"wrss " ); +} + +void arch_proc_pid_thread_features (struct seq_file *m,struct task_struct *task) +{ +seq_puts (m,"x86_Thread_features:\t" ); +dump_x86_features (m, task->thread.features); +seq_putc (m,'\n' ); + +seq_puts (m,"x86_Thread_features_locked:\t" ); +dump_x86_features (m, task->thread.features_locked); +seq_putc (m,'\n' ); +} +#endif /* CONFIG_X86_USER_SHADOW_STACK */
fs/proc/array.c +6 -0
@@ -428,6 +428,11 @@ static inline void task_thp_status(struct seq_file *m, struct mm_struct *mm)seq_printf (m,"THP_enabled:\t%d\n" , thp_enabled); } +__weak void arch_proc_pid_thread_features (struct seq_file *m, +struct task_struct *task) +{ +} +int proc_pid_status (struct seq_file *m,struct pid_namespace *ns,struct pid *pid,struct task_struct *task) {
@@ -451,6 +456,7 @@ int proc_pid_status(struct seq_file *m, struct pid_namespace *ns,task_cpus_allowed (m, task);cpuset_task_status_allowed (m, task);task_context_switch_counts (m, task); +arch_proc_pid_thread_features (m, task);return 0 ; }
include/linux/proc_fs.h +2 -0
@@ -158,6 +158,8 @@ int proc_pid_arch_status(struct seq_file *m, struct pid_namespace *ns, struct pid *pid, struct task_struct *task);#endif /* CONFIG_PROC_PID_ARCH_STATUS */ +void arch_proc_pid_thread_features (struct seq_file *m,struct task_struct *task); +#else /* CONFIG_PROC_FS */ static inline void proc_root_init (void )
Signature · 1 line
2.17.1
PATCH v7 36/41 x86/shstk: Wire in shadow stack interface
In reply to Rick EdgecombeThe kernel now has the main shadow stack functionality to support applications. Wire in the WRSS and shadow stack enable/disable functions into the existing shadow stack API skeleton.
---
v4:
- Remove "CET" references
v2:
- Split from other patches
--- arch/x86/kernel/shstk.c | 8 ++++++++ 1 file changed, 8 insertions(+)
arch/x86/kernel/shstk.c +8 -0
@@ -464,9 +464,17 @@ long shstk_prctl(struct task_struct *task, int option, unsigned long features)return -EINVAL ;if (option ==ARCH_SHSTK_DISABLE ) { +if (features &ARCH_SHSTK_WRSS ) +return wrss_control (false); +if (features &ARCH_SHSTK_SHSTK ) +return shstk_disable ();return -EINVAL ; }/* Handle ARCH_SHSTK_ENABLE */ +if (features &ARCH_SHSTK_SHSTK ) +return shstk_setup (); +if (features &ARCH_SHSTK_WRSS ) +return wrss_control (true);return -EINVAL ; }
Signature · 1 line
2.17.1
PATCH v7 37/41 selftests/x86: Add shadow stack test
In reply to Rick EdgecombeAdd a simple selftest for exercising some shadow stack behavior:
- map_shadow_stack syscall and pivot
- Faulting in shadow stack memory
- Handling shadow stack violations
- GUP of shadow stack memory
- mprotect() of shadow stack memory
- Userfaultfd on shadow stack memory
- 32 bit segmentation
---
v7:
- Remove KHDR_INCLUDES and just add a copy of the defines (Boris)
v6:
- Tweak mprotect test
- Code style tweaks
v5:
- Update 32 bit signal test with new ABI and better asm
v4:
- Add test for 32 bit signal ABI blocking
v3:
- Change "+m" to "=m" in write_shstk() (Andrew Cooper)
- Fix userfaultfd test with transparent huge pages by doing a MADV_DONTNEED, since the token write faults in the while stack with huge pages.
--- tools/testing/selftests/x86/Makefile | 2 +- .../testing/selftests/x86/test_shadow_stack.c | 695 ++++++++++++++++++ 2 files changed, 696 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/x86/test_shadow_stack.c
tools/testing/selftests/x86/Makefile +1 -1
@@ -18,7 +18,7 @@ TARGETS_C_32BIT_ONLY := entry_from_vm86 test_syscall_vdso unwind_vdso \ test_FCMOV test_FCOMI test_FISTTP \ vdso_restorer TARGETS_C_64BIT_ONLY := fsgsbase sysret_rip syscall_numbering \ - corrupt_xstate_header amx + corrupt_xstate_header amx test_shadow_stack # Some selftests require 32bit support enabled also on 64bit systems TARGETS_C_32BIT_NEEDED := ldt_gdt ptrace_syscall
tools/testing/selftests/x86/test_shadow_stack.c new file +695 -0
695 lines not on this page · raw message
Signature · 1 line
2.17.1
PATCH v7 38/41 x86/fpu: Add helper for initing features
In reply to Rick EdgecombeIf an xfeature is saved in a buffer, the xfeature's bit will be set in xsave->header.xfeatures. The CPU may opt to not save the xfeature if it is in it's init state. In this case the xfeature buffer address cannot be retrieved with get_xsave_addr().
Future patches will need to handle the case of writing to an xfeature that may not be saved. So provide helpers to init an xfeature in an xsave buffer.
This could of course be done directly by reaching into the xsave buffer, however this would not be robust against future changes to optimize the xsave buffer by compacting it. In that case the xsave buffer would need to be re-arranged as well. So the logic properly belongs encapsulated in a helper where the logic can be unified.
---
v2:
- New patch
--- arch/x86/kernel/fpu/xstate.c | 58 +++++++++++++++++++++++++++++------- arch/x86/kernel/fpu/xstate.h | 6 ++++ 2 files changed, 53 insertions(+), 11 deletions(-)
arch/x86/kernel/fpu/xstate.c +47 -11
@@ -934,6 +934,24 @@ static void *__raw_xsave_addr(struct xregs_state *xsave, int xfeature_nr)return (void *)xsave +xfeature_get_offset (xcomp_bv, xfeature_nr); } +static int xsave_buffer_access_checks (int xfeature_nr) +{ +/* +* Do we even *have* xsave state? +*/ +if (!boot_cpu_has (X86_FEATURE_XSAVE )) +return 1 ; + +/* +* We should not ever be requesting features that we +* have not enabled. +*/ +if (WARN_ON_ONCE (!xfeature_enabled (xfeature_nr))) +return 1 ; + +return 0 ; +} +/* * Given the xsave area and a state inside, this function returns the * address of the state.
@@ -954,17 +972,7 @@ static void *__raw_xsave_addr(struct xregs_state *xsave, int xfeature_nr)*/ void *get_xsave_addr (struct xregs_state *xsave,int xfeature_nr) { -/* -* Do we even *have* xsave state? -*/ -if (!boot_cpu_has (X86_FEATURE_XSAVE )) -return NULL ; - -/* -* We should not ever be requesting features that we -* have not enabled. -*/ -if (WARN_ON_ONCE (!xfeature_enabled (xfeature_nr))) +if (xsave_buffer_access_checks (xfeature_nr))return NULL ;/*
@@ -984,6 +992,34 @@ void *get_xsave_addr(struct xregs_state *xsave, int xfeature_nr)return __raw_xsave_addr (xsave, xfeature_nr); } +/* +* Given the xsave area and a state inside, this function +* initializes an xfeature in the buffer. +* +* get_xsave_addr() will return NULL if the feature bit is +* not present in the header. This function will make it so +* the xfeature buffer address is ready to be retrieved by +* get_xsave_addr(). +* +* Inputs: +* xstate: the thread's storage area for all FPU data +* xfeature_nr: state which is defined in xsave.h (e.g. XFEATURE_FP, +* XFEATURE_SSE, etc...) +* Output: +* 1 if the feature cannot be inited, 0 on success +*/ +int init_xfeature (struct xregs_state *xsave,int xfeature_nr) +{ +if (xsave_buffer_access_checks (xfeature_nr)) +return 1 ; + +/* +* Mark the feature inited. +*/ + xsave->header.xfeatures |=BIT_ULL (xfeature_nr); +return 0 ; +} +#ifdef CONFIG_ARCH_HAS_PKEYS /*
arch/x86/kernel/fpu/xstate.h +6 -0
@@ -54,6 +54,12 @@ extern void fpu__init_cpu_xstate(void);extern void fpu__init_system_xstate (unsigned int legacy_size);extern void *get_xsave_addr (struct xregs_state *xsave,int xfeature_nr); +extern int init_xfeature (struct xregs_state *xsave,int xfeature_nr); + +static inline int xfeature_saved (struct xregs_state *xsave,int xfeature_nr) +{ +return xsave->header.xfeatures &BIT_ULL (xfeature_nr); +}static inline u64 xfeatures_mask_supervisor (void ) {
Signature · 1 line
2.17.1
Borislav Petkov
maintainerRe: [PATCH v7 38/41] x86/fpu: Add helper for initing features
Quoted 1 line · Rick Edgecombe
Subject: Re: [PATCH v7 38/41] x86/fpu: Add helper for initing features
"initializing"
Quoted 3 lines
If an xfeature is saved in a buffer, the xfeature's bit will be set in
xsave->header.xfeatures. The CPU may opt to not save the xfeature if it is in it's init state. In this case the xfeature buffer address cannot
"its"
Quoted 32 lines
be retrieved with
get_xsave_addr().Future patches will need to handle the case of writing to an xfeature that may not be saved. So provide helpers to init an xfeature in an xsave buffer.
This could of course be done directly by reaching into the xsave buffer, however this would not be robust against future changes to optimize the xsave buffer by compacting it. In that case the xsave buffer would need to be re-arranged as well. So the logic properly belongs encapsulated in a helper where the logic can be unified.
Tested-by Pengfei Xu <[email protected]>Tested-by John Allen <[email protected]>Tested-by Kees Cook <[email protected]>Reviewed-by Kees Cook <[email protected]>Signed-off-by Rick Edgecombe <[email protected]>---
v2:
- New patch--- arch/x86/kernel/fpu/xstate.c | 58 +++++++++++++++++++++++++++++------- arch/x86/kernel/fpu/xstate.h | 6 ++++ 2 files changed, 53 insertions(+), 11 deletions(-)arch/x86/kernel/fpu/xstate.c +0
-0
arch/x86/kernel/fpu/xstate.c static void *__raw_xsave_addr(struct xregs_state *xsave, int xfeature_nr) · line 937
Quoted 4 lines
arch/x86/kernel/fpu/xstate.c +1
-0@@ -934,6 +934,24 @@ static void *__raw_xsave_addr(struct xregs_state *xsave, int xfeature_nr)return (void *)xsave +xfeature_get_offset (xcomp_bv, xfeature_nr); } +static int xsave_buffer_access_checks (int xfeature_nr)
Function name needs a verb.
arch/x86/kernel/fpu/xstate.c static void *__raw_xsave_addr(struct xregs_state *xsave, int xfeature_nr) · line 941
Quoted 4 lines
arch/x86/kernel/fpu/xstate.c +4
-0+{ +/* +* Do we even *have* xsave state? +*/
That comment is superfluous.
arch/x86/kernel/fpu/xstate.c static void *__raw_xsave_addr(struct xregs_state *xsave, int xfeature_nr) · line 942
Quoted 1 line
arch/x86/kernel/fpu/xstate.c +1
-0+if (!boot_cpu_has (X86_FEATURE_XSAVE ))
check_for_deprecated_apis: WARNING: arch/x86/kernel/fpu/xstate.c:942: Do not use boot_cpu_has() - use cpu_feature_enabled() instead.
arch/x86/kernel/fpu/xstate.c static void *__raw_xsave_addr(struct xregs_state *xsave, int xfeature_nr) · line 946
Quoted 4 lines
arch/x86/kernel/fpu/xstate.c +4
-0+return 1 ; + +/* +* We should not ever be requesting features that we
Please use passive voice in your commit message: no "we" or "I", etc, and describe your changes in imperative mood.
Quoted 31 lines
arch/x86/kernel/fpu/xstate.c +9
-11+* have not enabled. +*/ +if (WARN_ON_ONCE (!xfeature_enabled (xfeature_nr))) +return 1 ; + +return 0 ; +} +/* * Given the xsave area and a state inside, this function returns the * address of the state. @@ -954,17 +972,7 @@ static void *__raw_xsave_addr(struct xregs_state *xsave, int xfeature_nr)*/ void *get_xsave_addr (struct xregs_state *xsave,int xfeature_nr) { -/* -* Do we even *have* xsave state? -*/ -if (!boot_cpu_has (X86_FEATURE_XSAVE )) -return NULL ; - -/* -* We should not ever be requesting features that we -* have not enabled. -*/ -if (WARN_ON_ONCE (!xfeature_enabled (xfeature_nr))) +if (xsave_buffer_access_checks (xfeature_nr))return NULL ;/*
arch/x86/kernel/fpu/xstate.c void *get_xsave_addr(struct xregs_state *xsave, int xfeature_nr) · line 997
Quoted 6 lines
arch/x86/kernel/fpu/xstate.c +3
-0@@ -984,6 +992,34 @@ void *get_xsave_addr(struct xregs_state *xsave, int xfeature_nr)return __raw_xsave_addr (xsave, xfeature_nr); } +/* +* Given the xsave area and a state inside, this function +* initializes an xfeature in the buffer.
s/this function initializes/initialize/
arch/x86/kernel/fpu/xstate.c void *get_xsave_addr(struct xregs_state *xsave, int xfeature_nr) · line 1002
Quoted 5 lines
arch/x86/kernel/fpu/xstate.c +5
-0+* +* get_xsave_addr() will return NULL if the feature bit is +* not present in the header. This function will make it so +* the xfeature buffer address is ready to be retrieved by +* get_xsave_addr().
So users of get_xsave_addr() would have to know that they would need to call init_xfeature()?
I think the better approach would be:
void *get_xsave_addr(struct xregs_state *xsave, int xfeature_nr, bool init)
and then that @init controls whether get_xsave_addr() should init the buffer.
And then you don't have to have a bunch of small functions here and there and know when to call what but get_xsave_addr() would simply DTRT.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [PATCH v7 38/41] x86/fpu: Add helper for initing features
Quoted 5 lines · Borislav Petkov
Quoted 2 lines · Rick Edgecombe
Subject: Re: [PATCH v7 38/41] x86/fpu: Add helper for initing features
"initializing"
Sure.
Quoted 9 lines
Quoted 7 lines
If an xfeature is saved in a buffer, the xfeature's bit will be set in
xsave->header.xfeatures. The CPU may opt to not save the xfeature if it
is in it's init state. In this case the xfeature buffer address cannot"its"
I clearly need to be better about it's and its.
Quoted 45 lines
Quoted 45 lines
be retrieved with
get_xsave_addr().Future patches will need to handle the case of writing to an xfeature
that may not be saved. So provide helpers to init an xfeature in an xsave buffer.This could of course be done directly by reaching into the xsave buffer,
however this would not be robust against future changes to optimize the
xsave buffer by compacting it. In that case the xsave buffer would need
to be re-arranged as well. So the logic properly belongs encapsulated
in a helper where the logic can be unified.Tested-by Pengfei Xu <[email protected]>Tested-by John Allen <[email protected]>Tested-by Kees Cook <[email protected]>Reviewed-by Kees Cook <[email protected]>Signed-off-by Rick Edgecombe <[email protected]>---
v2:
- New patch--- arch/x86/kernel/fpu/xstate.c | 58 +++++++++++++++++++++++++++++--- ---- arch/x86/kernel/fpu/xstate.h | 6 ++++ 2 files changed, 53 insertions(+), 11 deletions(-)arch/x86/kernel/fpu/xstate.c +0
-0@@ -934,6 +934,24 @@ static void *__raw_xsave_addr(struct xregs_state *xsave, int xfeature_nr) return (void *)xsave + xfeature_get_offset(xcomp_bv, xfeature_nr); }
Quoted 3 lines
arch/x86/kernel/fpu/xstate.c static void *__raw_xsave_addr(struct xregs_state *xsave, int xfeature_nr) · line 937
Quoted 1 line
arch/x86/kernel/fpu/xstate.c +1
-0+static int xsave_buffer_access_checks (int xfeature_nr)Function name needs a verb.
Right.
Quoted 13 lines
arch/x86/kernel/fpu/xstate.c static void *__raw_xsave_addr(struct xregs_state *xsave, int xfeature_nr) · line 941
Quoted 5 lines
arch/x86/kernel/fpu/xstate.c +4
-0+{ +/* +* Do we even *have* xsave state? +*/ That comment is superfluous.
arch/x86/kernel/fpu/xstate.c static void *__raw_xsave_addr(struct xregs_state *xsave, int xfeature_nr) · line 942
Quoted 1 line
arch/x86/kernel/fpu/xstate.c +1
-0+if (!boot_cpu_has (X86_FEATURE_XSAVE ))
check_for_deprecated_apis: WARNING:arch/x86/kernel/fpu/xstate.c:942:
Do not useboot_cpu_has()- usecpu_feature_enabled()instead.
Quoted 7 lines
arch/x86/kernel/fpu/xstate.c static void *__raw_xsave_addr(struct xregs_state *xsave, int xfeature_nr) · line 946
Quoted 4 lines
arch/x86/kernel/fpu/xstate.c +4
-0+return 1 ; + +/* +* We should not ever be requesting features that we Please use passive voice in your commit message: no "we" or "I", etc, and describe your changes in imperative mood.
These two are from the existing code. Basically they get extracted into a new function.
Quoted 38 lines
Quoted 38 lines
arch/x86/kernel/fpu/xstate.c +8
-0+* have not enabled. +*/ +if (WARN_ON_ONCE (!xfeature_enabled (xfeature_nr))) +return 1 ; + +return 0 ; +} +/* * Given the xsave area and a state inside, this function returns
the
* address of the state.arch/x86/kernel/fpu/xstate.c +1
-11@@ -954,17 +972,7 @@ static void *__raw_xsave_addr(structxregs_state *xsave, int xfeature_nr) */ void *get_xsave_addr (struct xregs_state *xsave,int xfeature_nr) { -/* -* Do we even *have* xsave state? -*/ -if (!boot_cpu_has (X86_FEATURE_XSAVE )) -return NULL ; - -/* -* We should not ever be requesting features that we -* have not enabled. -*/ -if (WARN_ON_ONCE (!xfeature_enabled (xfeature_nr))) +if (xsave_buffer_access_checks (xfeature_nr))return NULL ;arch/x86/kernel/fpu/xstate.c +0
-0/* @@ -984,6 +992,34 @@ void *get_xsave_addr(struct xregs_state *xsave, int xfeature_nr)return __raw_xsave_addr (xsave, xfeature_nr); }
Quoted 5 lines
arch/x86/kernel/fpu/xstate.c void *get_xsave_addr(struct xregs_state *xsave, int xfeature_nr) · line 997
Quoted 3 lines
arch/x86/kernel/fpu/xstate.c +3
-0+/* +* Given the xsave area and a state inside, this function +* initializes an xfeature in the buffer. s/this function initializes/initialize/
Sure.
Quoted 10 lines
arch/x86/kernel/fpu/xstate.c void *get_xsave_addr(struct xregs_state *xsave, int xfeature_nr) · line 1002
Quoted 6 lines
arch/x86/kernel/fpu/xstate.c +5
-0+* +* get_xsave_addr() will return NULL if the feature bit is +* not present in the header. This function will make it so +* the xfeature buffer address is ready to be retrieved by +* get_xsave_addr(). So users of
get_xsave_addr()would have to know that they would need to
callinit_xfeature()?
That is the situation today. FWIW both of these functions are limited to the FPU internals, so I would think it's not a too unreasonable assumption.
Quoted 6 lines
I think the better approach would be:
void *get_xsave_addr(struct
xregs_state*xsave, intxfeature_nr, bool init)
Quoted 6 lines
and then that @init controls whether
get_xsave_addr()should init the buffer.And then you don't have to have a bunch of small functions here and there and know when to call what but
get_xsave_addr()would simply DTRT.
It would have to actually copy the init state to the buffer from init_fpstate, because otherwise the caller couldn't know if get_xsave_addr() was returning valid data or some old data in the buffer. And I guess the init mean to initialize it only if it is in the init state, not to overwrite the current state with the init state.
I did it up, and it makes the caller code cleaner. But I'm not sure what to think of it. Is this not mixing two operations together? Today get_xsave_addr() pretty much just gets a buffer offset with some checks. Now it would compute the offset and also silently go off and changes the buffer.
I looked at this fpu code originally and thought I could add some useful abstractions, but this failed. I came away wondering if this was just an area with so many special cases and details, that abstractions just added confusion. I'm just bringing this up because the other option is to just do this in the regset code:xsave->header.xfeatures |= BIT_ULL(XFEATURE_CET_USER);
Let me know if you think it would be better to just open code it.
Quoted 3 lines
Thx.
Borislav Petkov
maintainerRe: [PATCH v7 38/41] x86/fpu: Add helper for initing features
Quoted 2 lines · Rick P
These two are from the existing code. Basically they get extracted into a new function.
I know but you can fix them while at it.
Quoted 5 lines
I did it up, and it makes the caller code cleaner. But I'm not sure what to think of it. Is this not mixing two operations together? Today
get_xsave_addr()pretty much just gets a buffer offset with some checks. Now it would compute the offset and also silently go off and changes the buffer.
Ok, so why don't you write the call site this way instead:
cetregs = get_xsave_addr(xsave, XFEATURE_CET_USER);
if (!cetregs) {
if (xfeature_saved(xsave, XFEATURE_CET_USER)) {
WARN("something's wrong with this buffer")
return ...;
}
/* Not saved, initialize it */
init_xfeature(xsave, XFEATURE_CET_USER));
}
cetregs = get_xsave_addr(xsave, XFEATURE_CET_USER);
if (!cetregs) {
WARN_ON("WTF")
return -ENODEV;
}Now it is clear what happens and it is a common code pattern of trying to get something and initializing it if it wasn't initialized yet, and then retrying...
Hmm?
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [PATCH v7 38/41] x86/fpu: Add helper for initing features
Quoted 6 lines · Borislav Petkov
Quoted 3 lines · Rick P
These two are from the existing code. Basically they get extracted into
a new function.I know but you can fix them while at it.
Ok.
Quoted 28 lines
Quoted 8 lines
I did it up, and it makes the caller code cleaner. But I'm not sure what to think of it. Is this not mixing two operations together? Today
get_xsave_addr()pretty much just gets a buffer offset with some checks. Now it would compute the offset and also silently go off and
changes the buffer.Ok, so why don't you write the call site this way instead:
cetregs = get_xsave_addr(xsave, XFEATURE_CET_USER); if (!cetregs) { if (xfeature_saved(xsave, XFEATURE_CET_USER)) { WARN("something's wrong with this buffer") return ...; } /* Not saved, initialize it */ init_xfeature(xsave, XFEATURE_CET_USER)); } cetregs = get_xsave_addr(xsave, XFEATURE_CET_USER); if (!cetregs) { WARN_ON("WTF") return -ENODEV; }
Quoted 7 lines
Now it is clear what happens and it is a common code pattern of trying
to get something and initializing it if it wasn't initialized yet, and
then retrying...Hmm?
This seems more clear. I'm sorry for the noise here though, because this has made me realize that the initing logic should never be hit. We used to support the full CET_U state in ptrace, but then dropped it to just the SSP and only allowed it when shadow stack is active. This means that CET_U will always have at least the CET_SHSTK_EN bit set and so not be in the init state. So this can probably just warn and bail if it sees an init state.
Unless the extra logic seems more robust? But it is always nice when the chance comes to drop a patch out of this thing...
Borislav Petkov
maintainerRe: [PATCH v7 38/41] x86/fpu: Add helper for initing features
Quoted 4 lines · Rick P
This seems more clear. I'm sorry for the noise here though, because this has made me realize that the initing logic should never be hit. We used to support the full
CET_Ustate in ptrace, but then dropped it to just the SSP and only allowed it when shadow stack is active.
Right, you do check that at function entry.
Quoted 3 lines
This means that
CET_Uwill always have at least theCET_SHSTK_ENbit set and so not be in the init state. So this can probably just warn and bail if it sees an init state.
I don't mind the additional checks as this is a security thing so sanity checks are good, especially if they're cheap.
And you don't need to reinit the buffer - just scream loudly when get_xsave_addr() returns NULL.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [PATCH v7 38/41] x86/fpu: Add helper for initing features
Quoted 11 lines · Borislav Petkov
Quoted 4 lines
This means that
CET_Uwill always have at least theCET_SHSTK_ENbit
set and so not be in the init state. So this can probably just warn and bail if it sees an init state.I don't mind the additional checks as this is a security thing so sanity checks are good, especially if they're cheap.
And you don't need to reinit the buffer - just scream loudly when
get_xsave_addr()
returns NULL.
Ok, will do this instead in "x86: Add PTRACE interface for shadow stack".
PATCH v7 39/41 x86: Add PTRACE interface for shadow stack
In reply to Rick EdgecombeFrom: Yu-cheng Yu <[email protected]>
Some applications (like GDB) would like to tweak shadow stack state via ptrace. This allows for existing functionality to continue to work for seized shadow stack applications. Provide an regset interface for manipulating the shadow stack pointer (SSP).
There is already ptrace functionality for accessing xstate, but this does not include supervisor xfeatures. So there is not a completely clear place for where to put the shadow stack state. Adding it to the user xfeatures regset would complicate that code, as it currently shares logic with signals which should not have supervisor features.
Don't add a general supervisor xfeature regset like the user one, because it is better to maintain flexibility for other supervisor xfeatures to define their own interface. For example, an xfeature may decide not to expose all of it's state to userspace, as is actually the case for shadow stack ptrace functionality. A lot of enum values remain to be used, so just put it in dedicated shadow stack regset.
The only downside to not having a generic supervisor xfeature regset, is that apps need to be enlightened of any new supervisor xfeature exposed this way (i.e. they can't try to have generic save/restore logic). But maybe that is a good thing, because they have to think through each new xfeature instead of encountering issues when new a new supervisor xfeature was added.
By adding a shadow stack regset, it also has the effect of including the shadow stack state in a core dump, which could be useful for debugging.
The shadow stack specific xstate includes the SSP, and the shadow stack and WRSS enablement status. Enabling shadow stack or wrss in the kernel involves more than just flipping the bit. The kernel is made aware that it has to do extra things when cloning or handling signals. That logic is triggered off of separate feature enablement state kept in the task struct. So the flipping on HW shadow stack enforcement without notifying the kernel to change its behavior would severely limit what an application could do without crashing, and the results would depend on kernel internal implementation details. There is also no known use for controlling this state via prtace today. So only expose the SSP, which is something that userspace already has indirect control over.
---
v5:
- Check shadow stack enablement status for tracee (rppt)
- Fix typo in comment
v4:
- Make shadow stack only. Reduce to only supporting SSP register, and remove CET references (peterz)
- Add comment to not use 0x203, because binutils already looks for it in coredumps. (Christina Schimpe)
v3:
- Drop dependence on thread.shstk.size, and use thread.features bits
- Drop 32 bit support
v2:
- Check alignment on ssp.
- Block IBT bits.
- Handle init states instead of returning error.
- Add verbose commit log justifying the design.
--- arch/x86/include/asm/fpu/regset.h | 7 +-- arch/x86/kernel/fpu/regset.c | 86 +++++++++++++++++++++++++++++++ arch/x86/kernel/ptrace.c | 12 +++++ include/uapi/linux/elf.h | 2 + 4 files changed, 104 insertions(+), 3 deletions(-)
arch/x86/include/asm/fpu/regset.h +4 -3
@@ -7,11 +7,12 @@#include <linux/regset.h> -extern user_regset_active_fn regset_fpregs_active, regset_xregset_fpregs_active; +extern user_regset_active_fn regset_fpregs_active, regset_xregset_fpregs_active, + ssp_active;extern user_regset_get2_fn fpregs_get, xfpregs_get, fpregs_soft_get, - xstateregs_get; + xstateregs_get, ssp_get;extern user_regset_set_fn fpregs_set, xfpregs_set, fpregs_soft_set, - xstateregs_set; + xstateregs_set, ssp_set;/* * xstateregs_active == regset_fpregs_active. Please refer to the comment
arch/x86/kernel/fpu/regset.c +86 -0
98 lines not on this page · raw message
arch/x86/kernel/ptrace.c +12 -0
@@ -58,6 +58,7 @@ enum x86_regset_64 {REGSET64_FP ,REGSET64_IOPERM ,REGSET64_XSTATE , + REGSET64_SSP, };#define REGSET_GENERAL \
@@ -1267,6 +1268,17 @@ static struct user_regset x86_64_regsets[] __ro_after_init = { .active = ioperm_active, .regset_get = ioperm_get }, +#ifdef CONFIG_X86_USER_SHADOW_STACK + [REGSET64_SSP] = { + .core_note_type = NT_X86_SHSTK, + .n = 1, + .size = sizeof(u64), + .align = sizeof(u64), + .active = ssp_active, + .regset_get = ssp_get, + .set = ssp_set + }, +#endif }; static const struct user_regset_view user_x86_64_view = {
include/uapi/linux/elf.h +2 -0
@@ -406,6 +406,8 @@ typedef struct elf64_shdr {#define NT_386_TLS 0x200/* i386 TLS slots (struct user_desc) */ #define NT_386_IOPERM 0x201/* x86 io permission bitmap (1=deny) */ #define NT_X86_XSTATE 0x202/* x86 extended state using xsave */ +/* Old binutils treats 0x203 as a CET state */ +#define NT_X86_SHSTK 0x204/* x86 SHSTK state */ #define NT_S390_HIGH_GPRS 0x300/* s390 upper register halves */ #define NT_S390_TIMER 0x301/* s390 timer register */ #define NT_S390_TODCMP 0x302/* s390 TOD clock comparator register */
Signature · 1 line
2.17.1
Borislav Petkov
maintainerRe: [PATCH v7 39/41] x86: Add PTRACE interface for shadow stack
Quoted 5 lines · Rick Edgecombe
The only downside to not having a generic supervisor xfeature regset, is that apps need to be enlightened of any new supervisor xfeature exposed this way (i.e. they can't try to have generic save/restore logic). But maybe that is a good thing, because they have to think through each new xfeature instead of encountering issues when new a new
Remove the first "new".
Quoted 7 lines
supervisor xfeature was added.
By adding a shadow stack regset, it also has the effect of including the shadow stack state in a core dump, which could be useful for debugging.
The shadow stack specific xstate includes the SSP, and the shadow stack and WRSS enablement status. Enabling shadow stack or wrss in the kernel
^^^^
"WRSS"
Quoted 8 lines
involves more than just flipping the bit. The kernel is made aware that it has to do extra things when cloning or handling signals. That logic is triggered off of separate feature enablement state kept in the task struct. So the flipping on HW shadow stack enforcement without notifying the kernel to change its behavior would severely limit what an application could do without crashing, and the results would depend on kernel internal implementation details. There is also no known use for controlling this state via prtace today. So only expose the SSP, which is something
Unknown word [prtace] in commit message.
Suggestions: ['ptrace'
Quoted 10 lines
that userspace already has indirect control over.
Tested-by Pengfei Xu <[email protected]>Tested-by John Allen <[email protected]>Tested-by Kees Cook <[email protected]>Reviewed-by Kees Cook <[email protected]>Co-developed-by Rick Edgecombe <[email protected]>Signed-off-by Rick Edgecombe <[email protected]>Signed-off-by Yu-cheng Yu <[email protected]>
I think your SOB should come last:
...
Pls check whole set.
Quoted 6 lines
arch/x86/kernel/fpu/regset.c +6
-0+#ifdef CONFIG_X86_USER_SHADOW_STACK +int ssp_active (struct task_struct *target,const struct user_regset *regset) +{ +if (target->thread.features &ARCH_SHSTK_SHSTK ) +return regset->n; +
arch/x86/kernel/fpu/regset.c int xstateregs_set(struct task_struct *target, const struct user_regset *regset, · line 193
Quoted 10 lines
arch/x86/kernel/fpu/regset.c +10
-0+return 0 ; +} + +int ssp_get (struct task_struct *target,const struct user_regset *regset, +struct membuf to) +{ +struct fpu *fpu = &target->thread.fpu; +struct cet_user_state *cetregs; + +if (!boot_cpu_has (X86_FEATURE_USER_SHSTK ))
check_for_deprecated_apis: WARNING: arch/x86/kernel/fpu/regset.c:193: Do not use boot_cpu_has() - use cpu_feature_enabled() instead.
Check your whole set pls.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteEdgecombe, Rick P
Tested-by Pengfei XuRe: [PATCH v7 39/41] x86: Add PTRACE interface for shadow stack
Quoted 1 line · Borislav Petkov
On Mon, Feb 27, 2023 at 02:29:55PM -0800, Rick Edgecombe wrote:
Quoted 9 lines
Quoted 7 lines
Tested-by John Allen <[email protected]>Tested-by Kees Cook <[email protected]>Reviewed-by Kees Cook <[email protected]>Co-developed-by Rick Edgecombe <[email protected]>Signed-off-by Rick Edgecombe <[email protected]>Signed-off-by Yu-cheng Yu <[email protected]>I think your SOB should come last:
Right on commit log typos, and yea this is screwed up. I think Dave re- ordered the SOB's already.
Quoted 31 lines
...
Signed-off-by Yu-cheng Yu <[email protected]>Co-developed-by Rick Edgecombe <[email protected]>Signed-off-by Rick Edgecombe <[email protected]>Pls check whole set.
Quoted 12 lines
(quoted diff) +2
-0+#ifdef CONFIG_X86_USER_SHADOW_STACK +int ssp_active(struct task_struct *target, const struct
user_regset*regset)arch/x86/kernel/fpu/regset.c +8
-0+{ +if (target->thread.features &ARCH_SHSTK_SHSTK ) +return regset->n; + +return 0 ; +} + +int ssp_get(struct task_struct *target, const struct user_regset*regset,
arch/x86/kernel/fpu/regset.c int xstateregs_set(struct task_struct *target, const struct user_regset *regset, · line 193
Quoted 6 lines
arch/x86/kernel/fpu/regset.c +6
-0+ struct membuf to) +{ +struct fpu *fpu = &target->thread.fpu; +struct cet_user_state *cetregs; + +if (!boot_cpu_has (X86_FEATURE_USER_SHSTK ))
check_for_deprecated_apis: WARNING:arch/x86/kernel/fpu/regset.c:193:
Do not useboot_cpu_has()- usecpu_feature_enabled()instead.
Quoted 1 line
Check your whole set pls.
Ok. I think the other case is in "x86/fpu: Add helper for initing features" where it was moved code.
PATCH v7 40/41 x86/shstk: Add ARCH_SHSTK_UNLOCK
In reply to Rick EdgecombeFrom: Mike Rapoport <[email protected]>
Userspace loaders may lock features before a CRIU restore operation has the chance to set them to whatever state is required by the process being restored. Allow a way for CRIU to unlock features. Add it as an arch_prctl() like the other shadow stack operations, but restrict it being called by the ptrace arch_pctl() interface.
---
v4:
- Add to docs that it is ptrace only.
- Remove "CET" references
v3:
- Depend on CONFIG_CHECKPOINT_RESTORE (Kees)
--- Documentation/x86/shstk.rst | 4 ++++ arch/x86/include/uapi/asm/prctl.h | 1 + arch/x86/kernel/process_64.c | 1 + arch/x86/kernel/shstk.c | 9 +++++++-- 4 files changed, 13 insertions(+), 2 deletions(-)
Documentation/x86/shstk.rst +4 -0
@@ -73,6 +73,10 @@ arch_prctl(ARCH_SHSTK_LOCK, unsigned long features) are ignored. The mask is ORed with the existing value. So any feature bits set here cannot be enabled or disabled afterwards. +arch_prctl(ARCH_SHSTK_UNLOCK, unsigned long features) + Unlock features. 'features' is a mask of all features to unlock. All + bits set are processed, unset bits are ignored. Only works via ptrace. + The return values are as follows. On success, return 0. On error, errno can be::
arch/x86/include/uapi/asm/prctl.h +1 -0
@@ -25,6 +25,7 @@#define ARCH_SHSTK_ENABLE 0x5001#define ARCH_SHSTK_DISABLE 0x5002#define ARCH_SHSTK_LOCK 0x5003 +#define ARCH_SHSTK_UNLOCK 0x5004/* ARCH_SHSTK_ features bits */ #define ARCH_SHSTK_SHSTK (1ULL << 0)
arch/x86/kernel/process_64.c +1 -0
@@ -835,6 +835,7 @@ long do_arch_prctl_64(struct task_struct *task, int option, unsigned long arg2)case ARCH_SHSTK_ENABLE :case ARCH_SHSTK_DISABLE :case ARCH_SHSTK_LOCK : +case ARCH_SHSTK_UNLOCK :return shstk_prctl (task, option, arg2);default : ret = -EINVAL ;
arch/x86/kernel/shstk.c +7 -2
@@ -451,9 +451,14 @@ long shstk_prctl(struct task_struct *task, int option, unsigned long features)return 0 ; } -/* Don't allow via ptrace */ -if (task != current) +/* Only allow via ptrace */ +if (task != current) { +if (option ==ARCH_SHSTK_UNLOCK &&IS_ENABLED (CONFIG_CHECKPOINT_RESTORE )) { + task->thread.features_locked &= ~features; +return 0 ; + }return -EINVAL ; + }/* Do not allow to change locked features */ if (features & task->thread.features_locked)
Signature · 1 line
2.17.1
Borislav Petkov
maintainerRe: [PATCH v7 40/41] x86/shstk: Add ARCH_SHSTK_UNLOCK
Quoted 8 lines · Rick Edgecombe
From: Mike Rapoport <[email protected]>
Userspace loaders may lock features before a CRIU restore operation has the chance to set them to whatever state is required by the process being restored. Allow a way for CRIU to unlock features. Add it as an
arch_prctl()like the other shadow stack operations, but restrict it being called by the ptracearch_pctl()interface.
Quoted 4 lines
Tested-by Pengfei Xu <[email protected]>Tested-by John Allen <[email protected]>Tested-by Kees Cook <[email protected]>
That tag is kinda implicit here. Unless he doesn't ACK his own patch. :-P
Quoted 4 lines
Reviewed-by Kees Cook <[email protected]>Signed-off-by Mike Rapoport <[email protected]>[Merged into recent API changes, added commit log and docs]Signed-off-by Rick Edgecombe <[email protected]>
...
arch/x86/kernel/shstk.c long shstk_prctl(struct task_struct *task, int option, unsigned long features) · line 455
Quoted 12 lines
arch/x86/kernel/shstk.c +2
-2@@ -451,9 +451,14 @@ long shstk_prctl(struct task_struct *task, int option, unsigned long features)return 0 ; } -/* Don't allow via ptrace */ -if (task != current) +/* Only allow via ptrace */ +if (task != current) {
Is that the only case? task != current means ptrace and there's no other way to do this from userspace?
Isn't there some flag which says that task is ptraced? I think we should check that one too...
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [PATCH v7 40/41] x86/shstk: Add ARCH_SHSTK_UNLOCK
Quoted 12 lines · Borislav Petkov
Quoted 11 lines · Rick Edgecombe
From: Mike Rapoport <[email protected]>
Userspace loaders may lock features before a CRIU restore operation has
the chance to set them to whatever state is required by the process being restored. Allow a way for CRIU to unlock features. Add it as anarch_prctl()like the other shadow stack operations, but restrict it being
called by the ptracearch_pctl()interface.
Quoted 7 lines
Quoted 4 lines
Tested-by Pengfei Xu <[email protected]>Tested-by John Allen <[email protected]>Tested-by Kees Cook <[email protected]>That tag is kinda implicit here. Unless he doesn't ACK his own patch. :-P
Uhh, right. This was me mindlessly adding his ack to all the patches in the series.
Quoted 17 lines
Quoted 5 lines
Reviewed-by Kees Cook <[email protected]>Signed-off-by Mike Rapoport <[email protected]>[Merged into recent API changes, added commit log and docs]Signed-off-by Rick Edgecombe <[email protected]>...
Quoted 9 lines
arch/x86/kernel/shstk.c +0
-0@@ -451,9 +451,14 @@ long shstk_prctl(struct task_struct *task, int option, unsigned long features)return 0 ; }
Quoted 8 lines
arch/x86/kernel/shstk.c long shstk_prctl(struct task_struct *task, int option, unsigned long features) · line 455
Quoted 4 lines
arch/x86/kernel/shstk.c +2
-2-/* Don't allow via ptrace */ -if (task != current) +/* Only allow via ptrace */ +if (task != current) {Is that the only case? task != current means ptrace and there's no other
way to do this from userspace?
Not that I could see...
Quoted 4 lines
Isn't there some flag which says that task is ptraced? I think we should
check that one too...
This is how the other arch_prctl()s handle it (if they do handle it, some don't). So I would think it would be nice to keep all the logic the same.
I guess the flag might work based on the assumption that if the task is being ptraced, the arch_prctl() couldn't be coming from anywhere else. Maybe it should get a nicely named helper that they could all use and whatever best logic could be commented.
Would this maybe be better as a future cleanup that did the change for them all?
Borislav Petkov
maintainerRe: [PATCH v7 40/41] x86/shstk: Add ARCH_SHSTK_UNLOCK
Quoted 11 lines · Rick P
This is how the other arch_prctl()s handle it (if they do handle it, some don't). So I would think it would be nice to keep all the logic the same.
I guess the flag might work based on the assumption that if the task is being ptraced, the
arch_prctl()couldn't be coming from anywhere else. Maybe it should get a nicely named helper that they could all use and whatever best logic could be commented.Would this maybe be better as a future cleanup that did the change for them all?
Yeah, I'm just being overly paranoid.
Because if there's another way to unlock that feature, then this whole "overhead" we're doing is for nothing.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquettePATCH v7 41/41 x86/shstk: Add ARCH_SHSTK_STATUS
In reply to Rick EdgecombeCRIU and GDB need to get the current shadow stack and WRSS enablement status. This information is already available via /proc/pid/status, but this is inconvenient for CRIU because it involves parsing the text output in an area of the code where this is difficult. Provide a status arch_prctl(), ARCH_SHSTK_STATUS for retrieving the status. Have arg2 be a userspace address, and make the new arch_prctl simply copy the features out to userspace.
---
v5:
- Fix typo in commit log
v4:
- New patch
--- Documentation/x86/shstk.rst | 6 ++++++ arch/x86/include/asm/shstk.h | 2 +- arch/x86/include/uapi/asm/prctl.h | 1 + arch/x86/kernel/process_64.c | 1 + arch/x86/kernel/shstk.c | 8 +++++++- 5 files changed, 16 insertions(+), 2 deletions(-)
Documentation/x86/shstk.rst +6 -0
@@ -77,6 +77,11 @@ arch_prctl(ARCH_SHSTK_UNLOCK, unsigned long features) Unlock features. 'features' is a mask of all features to unlock. All bits set are processed, unset bits are ignored. Only works via ptrace. +arch_prctl(ARCH_SHSTK_STATUS, unsigned long addr) + Copy the currently enabled features to the address passed in addr. The + features are described using the bits passed into the others in + 'features'. + The return values are as follows. On success, return 0. On error, errno can be::
@@ -84,6 +89,7 @@ be:: -ENOTSUPP if the feature is not supported by the hardware or kernel. -EINVAL arguments (non existing feature, etc) + -EFAULT if could not copy information back to userspace The feature's bits supported are::
arch/x86/include/asm/shstk.h +1 -1
@@ -14,7 +14,7 @@ struct thread_shstk {u64 size; }; -long shstk_prctl (struct task_struct *task,int option,unsigned long features); +long shstk_prctl (struct task_struct *task,int option,unsigned long arg2);void reset_thread_features (void ); int shstk_alloc_thread_stack(struct task_struct *p, unsigned long clone_flags, unsigned long stack_size,
arch/x86/include/uapi/asm/prctl.h +1 -0
@@ -26,6 +26,7 @@#define ARCH_SHSTK_DISABLE 0x5002#define ARCH_SHSTK_LOCK 0x5003#define ARCH_SHSTK_UNLOCK 0x5004 +#define ARCH_SHSTK_STATUS 0x5005/* ARCH_SHSTK_ features bits */ #define ARCH_SHSTK_SHSTK (1ULL << 0)
arch/x86/kernel/process_64.c +1 -0
@@ -836,6 +836,7 @@ long do_arch_prctl_64(struct task_struct *task, int option, unsigned long arg2)case ARCH_SHSTK_DISABLE :case ARCH_SHSTK_LOCK :case ARCH_SHSTK_UNLOCK : +case ARCH_SHSTK_STATUS :return shstk_prctl (task, option, arg2);default : ret = -EINVAL ;
arch/x86/kernel/shstk.c +7 -1
@@ -444,8 +444,14 @@ SYSCALL_DEFINE3(map_shadow_stack, unsigned long, addr, unsigned long, size, unsireturn alloc_shstk (addr, aligned_size, size, set_tok); } -long shstk_prctl (struct task_struct *task,int option,unsigned long features) +long shstk_prctl (struct task_struct *task,int option,unsigned long arg2) { +unsigned long features = arg2; + +if (option ==ARCH_SHSTK_STATUS ) { +return put_user (task->thread.features, (unsigned long __user *)arg2); + } +if (option ==ARCH_SHSTK_LOCK ) { task->thread.features_locked |= features;return 0 ;
Signature · 1 line
2.17.1