pkeys: Reserve PKEY_DISABLE_READ
Subsystems (8)
Would it be possible to reserve a bit for PKEY_DISABLE_READ?
I think the POWER implementation can disable read access at the hardware level, but not write access, and that cannot be expressed with the current PKEY_DISABLE_ACCESS and PKEY_DISABLE_WRITE bits.
My upcoming POWER implementation of pkey_set and pkey_get in glibc would benefit from this.
Thanks,
Florian
Quoted 5 lines · Florian Weimer
Would it be possible to reserve a bit for
PKEY_DISABLE_READ?I think the POWER implementation can disable read access at the hardware level, but not write access, and that cannot be expressed with the current
PKEY_DISABLE_ACCESSandPKEY_DISABLE_WRITEbits.
Do you just mean in the syscall interfaces? What would we need to do on x86 if we see the bit? Would we just say it's invalid on x86, or would we make sure that PKEY_DISABLE_ACCESS==PKEY_DISABLE_READ?
* Dave Hansen:
Quoted 10 lines · Florian Weimer
Quoted 5 lines
Would it be possible to reserve a bit for
PKEY_DISABLE_READ?I think the POWER implementation can disable read access at the hardware level, but not write access, and that cannot be expressed with the current
PKEY_DISABLE_ACCESSandPKEY_DISABLE_WRITEbits.Do you just mean in the syscall interfaces? What would we need to do on x86 if we see the bit? Would we just say it's invalid on x86, or would we make sure that PKEY_DISABLE_ACCESS==PKEY_DISABLE_READ?
Ideally, PKEY_DISABLE_READ | PKEY_DISABLE_WRITE and PKEY_DISABLE_READ | PKEY_DISABLE_ACCESS would be treated as PKEY_DISABLE_ACCESS both, and a line PKEY_DISABLE_READ would result in an EINVAL failure.
Thanks,
Florian
Quoted 3 lines · Florian Weimer
Ideally,
PKEY_DISABLE_READ|PKEY_DISABLE_WRITEandPKEY_DISABLE_READ|PKEY_DISABLE_ACCESSwould be treated asPKEY_DISABLE_ACCESSboth, and a linePKEY_DISABLE_READwould result in an EINVAL failure.
Sounds reasonable to me.
I don't see any urgency to do this right now. It could easily go in alongside the ppc patches when those get merged. The only thing I'd suggest is that we make it something slightly higher than 0x4. It'll make the code easier to deal with in the kernel if we have the ABI and the hardware mirror each other, and if we pick 0x4 in the ABI for PKEY_DISABLE_READ, it might get messy if the harware choose 0x4 for PKEY_DISABLE_EXECUTE or something.
So, let's make it 0x80 or something on x86 at least.
Also, I'll be happy to review and ack the patch to do this, but I'd expect the ppc guys (hi Ram!) to actually put it together.
* Dave Hansen:
Quoted 9 lines · Florian Weimer
Quoted 3 lines
Ideally,
PKEY_DISABLE_READ|PKEY_DISABLE_WRITEandPKEY_DISABLE_READ|PKEY_DISABLE_ACCESSwould be treated asPKEY_DISABLE_ACCESSboth, and a linePKEY_DISABLE_READwould result in an EINVAL failure.Sounds reasonable to me.
I don't see any urgency to do this right now. It could easily go in alongside the ppc patches when those get merged.
POWER support has already been merged, so we need to do something here now, before I can complete the userspace side.
Quoted 7 lines
The only thing I'd suggest is that we make it something slightly higher than
0x4. It'll make the code easier to deal with in the kernel if we have the ABI and the hardware mirror each other, and if we pick0x4in the ABI forPKEY_DISABLE_READ, it might get messy if the harware choose0x4forPKEY_DISABLE_EXECUTEor something.So, let's make it
0x80or something on x86 at least.
I don't have a problem with that if that's what it takes.
Quoted 2 lines
Also, I'll be happy to review and ack the patch to do this, but I'd expect the ppc guys (hi Ram!) to actually put it together.
Ram, do you want to write a patch?
I'll promise I finish the glibc support for this. 8-)
Thanks,
Florian
Quoted 21 lines · Florian Weimer
* Dave Hansen:
Quoted 9 lines · Florian Weimer
Quoted 3 lines
Ideally,
PKEY_DISABLE_READ|PKEY_DISABLE_WRITEandPKEY_DISABLE_READ|PKEY_DISABLE_ACCESSwould be treated asPKEY_DISABLE_ACCESSboth, and a linePKEY_DISABLE_READwould result in an EINVAL failure.Sounds reasonable to me.
I don't see any urgency to do this right now. It could easily go in alongside the ppc patches when those get merged.
POWER support has already been merged, so we need to do something here now, before I can complete the userspace side.
Quoted 6 lines
The only thing I'd suggest is that we make it something slightly higher than
0x4. It'll make the code easier to deal with in the kernel if we have the ABI and the hardware mirror each other, and if we pick0x4in the ABI forPKEY_DISABLE_READ, it might get messy if the harware choose0x4forPKEY_DISABLE_EXECUTEor something.
Quoted 8 lines
Quoted 1 line
So, let's make it
0x80or something on x86 at least.I don't have a problem with that if that's what it takes.
Quoted 2 lines
Also, I'll be happy to review and ack the patch to do this, but I'd expect the ppc guys (hi Ram!) to actually put it together.
Ram, do you want to write a patch?
Florian,
I can. But I am struggling to understand the requirement. Why is this needed? Are we proposing a enhancement to the sys_pkey_alloc(), to be able to allocate keys that are initialied to disable-read only?
RP
Quoted 5 lines
I'll promise I finish the glibc support for this. 8-)
Thanks,
Florian
Signature · 1 line
Ram Pai
* Ram Pai:
Quoted 6 lines
Florian,
I can. But I am struggling to understand the requirement. Why is this needed? Are we proposing a enhancement to the
sys_pkey_alloc(), to be able to allocate keys that are initialied to disable-read only?
Yes, I think that would be a natural consequence.
However, my immediate need comes from the fact that the AMR register can contain a flag combination that is not possible to represent with the existing PKEY_DISABLE_WRITE and PKEY_DISABLE_ACCESS flags. User code could write to AMR directly, so I cannot rule out that certain flag combinations exist there.
So I came up with this:
intpkey_get (int key)
{
if (key < 0 || key > PKEY_MAX)
{
__set_errno (EINVAL);
return -1;
} unsigned int index = pkey_index (key);
unsigned long int amr = pkey_read ();
unsigned int bits = (amr >> index) & 3;
/* Translate from AMR values. PKEY_AMR_READ standing alone is not
currently representable. */
if (bits & PKEY_AMR_READ)
return PKEY_DISABLE_ACCESS;
else if (bits == PKEY_AMR_WRITE)
return PKEY_DISABLE_WRITE; return 0;
}
And this is not ideal. I would prefer something like this instead:
switch (bits)
{
case PKEY_AMR_READ | PKEY_AMR_WRITE:
return PKEY_DISABLE_ACCESS;
case PKEY_AMR_READ:
return PKEY_DISABLE_READ;
case PKEY_AMR_WRITE:
return PKEY_DISABLE_WRITE;
case 0:
return 0;
}By the way, is the AMR register 64-bit or 32-bit on 32-bit POWER?
Thanks,
Florian
Quoted 27 lines · Florian Weimer
* Ram Pai:
Quoted 6 lines
Florian,
I can. But I am struggling to understand the requirement. Why is this needed? Are we proposing a enhancement to the
sys_pkey_alloc(), to be able to allocate keys that are initialied to disable-read only?Yes, I think that would be a natural consequence.
However, my immediate need comes from the fact that the AMR register can contain a flag combination that is not possible to represent with the existing
PKEY_DISABLE_WRITEandPKEY_DISABLE_ACCESSflags. User code could write to AMR directly, so I cannot rule out that certain flag combinations exist there.So I came up with this:
int
pkey_get(int key)
{
if (key < 0 || key >PKEY_MAX){ __set_errno (EINVAL); return -1; }
Quoted 7 lines
unsigned int index =
pkey_index(key);
unsigned long int amr =pkey_read();
unsigned int bits = (amr >> index) & 3;/* Translate from AMR values.
PKEY_AMR_READstanding alone is not
currently representable. */
if (bits &PKEY_AMR_READ)
this should be
if (bits & (PKEY_AMR_READ|PKEY_AMR_WRITE))
Quoted 10 lines
return PKEY_DISABLE_ACCESS;else if (bits ==
PKEY_AMR_WRITE)return PKEY_DISABLE_WRITE;return 0;
}And this is not ideal. I would prefer something like this instead:
Quoted 11 lines
switch (bits)
{
case PKEY_AMR_READ | PKEY_AMR_WRITE:
return PKEY_DISABLE_ACCESS;
case PKEY_AMR_READ:
return PKEY_DISABLE_READ;
case PKEY_AMR_WRITE:
return PKEY_DISABLE_WRITE;
case 0:
return 0;
}yes.
and on x86 it will be something like:
switch (bits)
{
case PKEY_PKRU_ACCESS :
return PKEY_DISABLE_ACCESS;
case PKEY_AMR_WRITE:
return PKEY_DISABLE_WRITE;
case 0:
return 0;
}But for this to work, why do you need to enhance the sys_pkey_alloc() interface? Not that I am against it. Trying to understand if the enhancement is really needed.
Quoted 2 lines
By the way, is the AMR register 64-bit or 32-bit on 32-bit POWER?
It is 64-bit.
RP
* Ram Pai:
Quoted 32 lines · Florian Weimer
Quoted 31 lines
* Ram Pai:
Quoted 6 lines
Florian,
I can. But I am struggling to understand the requirement. Why is this needed? Are we proposing a enhancement to the
sys_pkey_alloc(), to be able to allocate keys that are initialied to disable-read only?Yes, I think that would be a natural consequence.
However, my immediate need comes from the fact that the AMR register can contain a flag combination that is not possible to represent with the existing
PKEY_DISABLE_WRITEandPKEY_DISABLE_ACCESSflags. User code could write to AMR directly, so I cannot rule out that certain flag combinations exist there.So I came up with this:
int
pkey_get(int key)
{
if (key < 0 || key >PKEY_MAX){ __set_errno (EINVAL); return -1; }unsigned int index =
pkey_index(key);
unsigned long int amr =pkey_read();
unsigned int bits = (amr >> index) & 3;
Quoted 6 lines
Quoted 3 lines
/* Translate from AMR values.
PKEY_AMR_READstanding alone is not
currently representable. */
if (bits &PKEY_AMR_READ)this should be
if (bits & (PKEY_AMR_READ|PKEY_AMR_WRITE))
This would return zero for PKEY_AMR_READ alone.
Quoted 7 lines
Quoted 7 lines
return PKEY_DISABLE_ACCESS;else if (bits ==
PKEY_AMR_WRITE)return PKEY_DISABLE_WRITE;return 0;
}
It's hard to tell whether PKEY_DISABLE_ACCESS is better in this case. Which is why I want PKEY_DISABLE_READ.
Quoted 16 lines
Quoted 13 lines
And this is not ideal. I would prefer something like this instead:
switch (bits) { case PKEY_AMR_READ | PKEY_AMR_WRITE: return PKEY_DISABLE_ACCESS; case PKEY_AMR_READ: return PKEY_DISABLE_READ; case PKEY_AMR_WRITE: return PKEY_DISABLE_WRITE; case 0: return 0; }yes.
and on x86 it will be something like:
Quoted 9 lines
switch (bits)
{
case PKEY_PKRU_ACCESS :
return PKEY_DISABLE_ACCESS;
case PKEY_AMR_WRITE:
return PKEY_DISABLE_WRITE;
case 0:
return 0;
}x86 returns the PKRU bits directly, including the nonsensical case (PKEY_DISABLE_ACCESS | PKEY_DISABLE_WRITE).
Quoted 3 lines
But for this to work, why do you need to enhance the
sys_pkey_alloc()interface? Not that I am against it. Trying to understand if the enhancement is really needed.
sys_pkey_alloc performs an implicit pkey_set for the newly allocated key (that is, it updates the PKRU/AMR register). It makes sense to match the behavior of the userspace implementation.
Thanks,
Florian
Quoted 83 lines · Florian Weimer
* Ram Pai:
Quoted 32 lines · Florian Weimer
Quoted 31 lines
* Ram Pai:
Quoted 6 lines
Florian,
I can. But I am struggling to understand the requirement. Why is this needed? Are we proposing a enhancement to the
sys_pkey_alloc(), to be able to allocate keys that are initialied to disable-read only?Yes, I think that would be a natural consequence.
However, my immediate need comes from the fact that the AMR register can contain a flag combination that is not possible to represent with the existing
PKEY_DISABLE_WRITEandPKEY_DISABLE_ACCESSflags. User code could write to AMR directly, so I cannot rule out that certain flag combinations exist there.So I came up with this:
int
pkey_get(int key)
{
if (key < 0 || key >PKEY_MAX){ __set_errno (EINVAL); return -1; }unsigned int index =
pkey_index(key);
unsigned long int amr =pkey_read();
unsigned int bits = (amr >> index) & 3;Quoted 6 lines
Quoted 3 lines
/* Translate from AMR values.
PKEY_AMR_READstanding alone is not
currently representable. */
if (bits &PKEY_AMR_READ)this should be
if (bits & (PKEY_AMR_READ|PKEY_AMR_WRITE))This would return zero for
PKEY_AMR_READalone.Quoted 7 lines
Quoted 7 lines
return PKEY_DISABLE_ACCESS;else if (bits ==
PKEY_AMR_WRITE)return PKEY_DISABLE_WRITE;return 0;
}It's hard to tell whether
PKEY_DISABLE_ACCESSis better in this case. Which is why I wantPKEY_DISABLE_READ.Quoted 16 lines
Quoted 13 lines
And this is not ideal. I would prefer something like this instead:
switch (bits) { case PKEY_AMR_READ | PKEY_AMR_WRITE: return PKEY_DISABLE_ACCESS; case PKEY_AMR_READ: return PKEY_DISABLE_READ; case PKEY_AMR_WRITE: return PKEY_DISABLE_WRITE; case 0: return 0; }yes.
and on x86 it will be something like:Quoted 9 lines
switch (bits) { case PKEY_PKRU_ACCESS : return PKEY_DISABLE_ACCESS; case PKEY_AMR_WRITE: return PKEY_DISABLE_WRITE; case 0: return 0; }x86 returns the PKRU bits directly, including the nonsensical case (
PKEY_DISABLE_ACCESS|PKEY_DISABLE_WRITE).
Quoted 7 lines
Quoted 3 lines
But for this to work, why do you need to enhance the
sys_pkey_alloc()interface? Not that I am against it. Trying to understand if the enhancement is really needed.
sys_pkey_allocperforms an implicitpkey_setfor the newly allocated key (that is, it updates the PKRU/AMR register). It makes sense to match the behavior of the userspace implementation.
Here is a untested patch. Does this meet your needs?
It defines the new flags. Each architecture will than define the set of flags it supports through PKEY_ACCESS_MASK.
arch/powerpc/include/asm/pkeys.h +0 -5
@@ -21,11 +21,6 @@#define ARCH_VM_PKEY_FLAGS (VM_PKEY_BIT0 | VM_PKEY_BIT1 | VM_PKEY_BIT2 | \ VM_PKEY_BIT3 | VM_PKEY_BIT4) -/* Override any generic PKEY permission defines */ -#define PKEY_DISABLE_EXECUTE 0x4 -#define PKEY_ACCESS_MASK (PKEY_DISABLE_ACCESS | \ - PKEY_DISABLE_WRITE | \ - PKEY_DISABLE_EXECUTE)static inline u64 pkey_to_vmflag_bits (u16 pkey) {
arch/powerpc/include/uapi/asm/mman.h +1 -1
@@ -31,9 +31,9 @@#define MAP_HUGETLB 0x40000/* create a huge page mapping */ /* Override any generic PKEY permission defines */ -#define PKEY_DISABLE_EXECUTE 0x4#undef PKEY_ACCESS_MASK#define PKEY_ACCESS_MASK (PKEY_DISABLE_ACCESS |\ PKEY_DISABLE_WRITE |\ + PKEY_DISABLE_READ |\ PKEY_DISABLE_EXECUTE)#endif /* _UAPI_ASM_POWERPC_MMAN_H */
arch/powerpc/mm/pkeys.c +2 -8
@@ -62,14 +62,6 @@ int pkey_initialize(void)int os_reserved, i;/* -* We define PKEY_DISABLE_EXECUTE in addition to the arch-neutral -* generic defines for PKEY_DISABLE_ACCESS and PKEY_DISABLE_WRITE. -* Ensure that the bits a distinct. -*/ -BUILD_BUG_ON (PKEY_DISABLE_EXECUTE & - (PKEY_DISABLE_ACCESS |PKEY_DISABLE_WRITE )); - -/* * pkey_to_vmflag_bits() assumes that the pkey bits are contiguous * in the vmaflag. Make sure that is really the case. */
@@ -259,6 +251,8 @@ int __arch_set_user_pkey_access(struct task_struct *tsk, int pkey, new_amr_bits |=AMR_RD_BIT |AMR_WR_BIT ; else if (init_val & PKEY_DISABLE_WRITE) new_amr_bits |= AMR_WR_BIT; + else if (init_val & PKEY_DISABLE_READ) + new_amr_bits |= AMR_RD_BIT;init_amr (pkey ,new_amr_bits );return 0 ;
arch/x86/include/uapi/asm/mman.h +5 -0
@@ -24,6 +24,11 @@ ((key) & 0x2 ? VM_PKEY_BIT1 : 0) | \ ((key) & 0x4 ? VM_PKEY_BIT2 : 0) | \ ((key) & 0x8 ? VM_PKEY_BIT3 : 0)) + +/* Override any generic PKEY permission defines */ +#undef PKEY_ACCESS_MASK +#define PKEY_ACCESS_MASK (PKEY_DISABLE_ACCESS |\ + PKEY_DISABLE_WRITE)#endif #include <asm-generic/mman.h>
include/uapi/asm-generic/mman-common.h +4 -3
@@ -71,7 +71,8 @@#define PKEY_DISABLE_ACCESS 0x1#define PKEY_DISABLE_WRITE 0x2 -#define PKEY_ACCESS_MASK (PKEY_DISABLE_ACCESS |\ - PKEY_DISABLE_WRITE) - +#define PKEY_DISABLE_EXECUTE 0x4 +#define PKEY_DISABLE_READ 0x8 +#define PKEY_ACCESS_MASK 0x0/* arch can override and define its own +mask bits */ #endif /* __ASM_GENERIC_MMAN_COMMON_H */
* Ram Pai:
Quoted 5 lines
arch/x86/include/uapi/asm/mman.h +0
-0
arch/x86/include/uapi/asm/mman.h line 32
Quoted 9 lines
arch/x86/include/uapi/asm/mman.h +5
-0@@ -24,6 +24,11 @@ ((key) & 0x2 ? VM_PKEY_BIT1 : 0) | \ ((key) & 0x4 ? VM_PKEY_BIT2 : 0) | \ ((key) & 0x8 ? VM_PKEY_BIT3 : 0)) + +/* Override any generic PKEY permission defines */ +#undef PKEY_ACCESS_MASK +#define PKEY_ACCESS_MASK (PKEY_DISABLE_ACCESS |\ + PKEY_DISABLE_WRITE)#endif
I would have expected something that translates PKEY_DISABLE_WRITE | PKEY_DISABLE_READ into PKEY_DISABLE_ACCESS, and also accepts PKEY_DISABLE_ACCESS | PKEY_DISABLE_READ, for consistency with POWER.
(My understanding is that PKEY_DISABLE_ACCESS does not disable all access, but produces execute-only memory.)
Quoted 6 lines
include/uapi/asm-generic/mman-common.h +0
-0@@ -71,7 +71,8 @@
include/uapi/asm-generic/mman-common.h line 78
Quoted 10 lines
include/uapi/asm-generic/mman-common.h +4
-3#define PKEY_DISABLE_ACCESS 0x1#define PKEY_DISABLE_WRITE 0x2 -#define PKEY_ACCESS_MASK (PKEY_DISABLE_ACCESS |\ - PKEY_DISABLE_WRITE) - +#define PKEY_DISABLE_EXECUTE 0x4 +#define PKEY_DISABLE_READ 0x8 +#define PKEY_ACCESS_MASK 0x0/* arch can override and define its own +mask bits */ #endif /* __ASM_GENERIC_MMAN_COMMON_H */
I think Dave requested a value for PKEY_DISABLE_READ which is further away from the existing bits.
Thanks,
Florian
Quoted 6 lines · Florian Weimer
I would have expected something that translates
PKEY_DISABLE_WRITE|PKEY_DISABLE_READintoPKEY_DISABLE_ACCESS, and also acceptsPKEY_DISABLE_ACCESS|PKEY_DISABLE_READ, for consistency with POWER.(My understanding is that
PKEY_DISABLE_ACCESSdoes not disable all access, but produces execute-only memory.)
Correct, it disables all data access, but not execution.
* Dave Hansen:
Quoted 9 lines · Florian Weimer
Quoted 6 lines
I would have expected something that translates
PKEY_DISABLE_WRITE|PKEY_DISABLE_READintoPKEY_DISABLE_ACCESS, and also acceptsPKEY_DISABLE_ACCESS|PKEY_DISABLE_READ, for consistency with POWER.(My understanding is that
PKEY_DISABLE_ACCESSdoes not disable all access, but produces execute-only memory.)Correct, it disables all data access, but not execution.
So I would expect something like this (completely untested, I did not even compile this):
arch/powerpc/include/asm/pkeys.h +5 -0
@@ -199,6 +199,11 @@ static inline bool arch_pkeys_enabled(void)return !static_branch_likely (&pkey_disabled); } +static inline bool arch_pkey_access_rights_valid (unsigned long rights) +{ +return (rights & ~(unsigned long )PKEY_ACCESS_MASK ) ==0 ; +} +extern void pkey_mm_init (struct mm_struct *mm);extern bool arch_supports_pkeys (int cap);extern unsigned int arch_usable_pkeys (void );
arch/x86/include/asm/pkeys.h +11 -0
@@ -14,6 +14,17 @@ static inline bool arch_pkeys_enabled(void)return boot_cpu_has (X86_FEATURE_OSPKE ); } +static inline bool arch_pkey_access_rights_valid (unsigned long rights) +{ +if (rights & ~(unsigned long )PKEY_ACCESS_MASK ) +return false; +if (rights &PKEY_DISABLE_READ ) { +/* x86 can only disable read access along with write access. */ +return rights & (PKEY_DISABLE_WRITE |PKEY_DISABLE_ACCESS ); + } +return true; +} +/* * Try to dedicate one of the protection keys to be used as an * execute-only protection key.
arch/x86/kernel/fpu/xstate.c +7 -1
@@ -928,7 +928,13 @@ int arch_set_user_pkey_access(struct task_struct *tsk, int pkey,return -EINVAL ;/* Set the bits we need in PKRU: */ -if (init_val &PKEY_DISABLE_ACCESS ) +if (init_val & (PKEY_DISABLE_ACCESS |PKEY_DISABLE_READ )) +/* +* arch_pkey_access_rights_valid checked that +* PKEY_DISABLE_READ is actually representable on x86 +* (that is, it comes with PKEY_DISABLE_ACCESS or +* PKEY_DISABLE_WRITE). +*/ new_pkru_bits |=PKRU_AD_BIT ;if (init_val &PKEY_DISABLE_WRITE ) new_pkru_bits |=PKRU_WD_BIT ;
include/linux/pkeys.h +5 -0
@@ -48,6 +48,11 @@ static inline void copy_init_pkru_to_fpregs(void) { } +static inline bool arch_pkey_access_rights_valid (unsigned long rights) +{ +return false; +} +#endif /* ! CONFIG_ARCH_HAS_PKEYS */ #endif /* _LINUX_PKEYS_H */
mm/mprotect.c +1 -1
@@ -597,7 +597,7 @@ SYSCALL_DEFINE2(pkey_alloc, unsigned long, flags, unsigned long, init_val)if (flags)return -EINVAL ;/* check for unsupported init values */ -if (init_val & ~PKEY_ACCESS_MASK ) +if (!arch_pkey_access_rights_valid (init_val))return -EINVAL ;down_write (¤t->mm->mmap_sem);
Thanks,
Florian
Quoted 7 lines · Florian Weimer
* Dave Hansen:
Quoted 5 lines · Florian Weimer
Quoted 4 lines
I would have expected something that translates
PKEY_DISABLE_WRITE|PKEY_DISABLE_READintoPKEY_DISABLE_ACCESS, and also acceptsPKEY_DISABLE_ACCESS|PKEY_DISABLE_READ, for consistency with POWER.
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
(My understanding is that
PKEY_DISABLE_ACCESSdoes not disable all access, but produces execute-only memory.)Correct, it disables all data access, but not execution.
So I would expect something like this (completely untested, I did not even compile this):
Ok. I re-read through the entire email thread to understand the problem and the proposed solution. Let me summarize it below. Lets see if we are on the same plate.
So the problem is as follows:
Currently the kernel supports 'disable-write' and 'disable-access'.
On x86, cpu supports 'disable-write' and 'disable-access'. This matches with what the kernel supports. All good.
However on power, cpu supports 'disable-read' too. Since userspace can program the cpu directly, userspace has the ability to set 'disable-read' too. This can lead to inconsistency between the kernel and the userspace.
We want the kernel to match userspace on all architectures.
Proposed Solution:
Enhance the kernel to understand 'disable-read', and facilitate architectures that understand 'disable-read' to allow it.
Also explicitly define the semantics of disable-access as 'disable-read and disable-write'
Did I get this right? Assuming I did, the implementation has to do the following --
On power, sys_pkey_alloc() should succeed if the init_val is PKEY_DISABLE_READ, PKEY_DISABLE_WRITE, PKEY_DISABLE_ACCESS or any combination of the three.
On x86, sys_pkey_alloc() should succeed if the init_val is PKEY_DISABLE_WRITE or PKEY_DISABLE_ACCESS or PKEY_DISABLE_READ or any combination of the three, except PKEY_DISABLE_READ specified all by itself.
On all other arches, none of the flags are supported.
Are we on the same plate?
RP
Quoted 90 lines
arch/powerpc/include/asm/pkeys.h +5
-0@@ -199,6 +199,11 @@ static inline bool arch_pkeys_enabled(void)return !static_branch_likely (&pkey_disabled); } +static inline bool arch_pkey_access_rights_valid (unsigned long rights) +{ +return (rights & ~(unsigned long )PKEY_ACCESS_MASK ) ==0 ; +} +extern void pkey_mm_init (struct mm_struct *mm);extern bool arch_supports_pkeys (int cap);extern unsigned int arch_usable_pkeys (void );arch/x86/include/asm/pkeys.h +11
-0@@ -14,6 +14,17 @@ static inline bool arch_pkeys_enabled(void)return boot_cpu_has (X86_FEATURE_OSPKE ); } +static inline bool arch_pkey_access_rights_valid (unsigned long rights) +{ +if (rights & ~(unsigned long )PKEY_ACCESS_MASK ) +return false; +if (rights &PKEY_DISABLE_READ ) { +/* x86 can only disable read access along with write access. */ +return rights & (PKEY_DISABLE_WRITE |PKEY_DISABLE_ACCESS ); + } +return true; +} +/* * Try to dedicate one of the protection keys to be used as an * execute-only protection key. arch/x86/kernel/fpu/xstate.c +7
-1@@ -928,7 +928,13 @@ int arch_set_user_pkey_access(struct task_struct *tsk, int pkey,return -EINVAL ;/* Set the bits we need in PKRU: */ -if (init_val &PKEY_DISABLE_ACCESS ) +if (init_val & (PKEY_DISABLE_ACCESS |PKEY_DISABLE_READ )) +/* +* arch_pkey_access_rights_valid checked that +* PKEY_DISABLE_READ is actually representable on x86 +* (that is, it comes with PKEY_DISABLE_ACCESS or +* PKEY_DISABLE_WRITE). +*/ new_pkru_bits |=PKRU_AD_BIT ;if (init_val &PKEY_DISABLE_WRITE ) new_pkru_bits |=PKRU_WD_BIT ;include/linux/pkeys.h +5
-0@@ -48,6 +48,11 @@ static inline void copy_init_pkru_to_fpregs(void) { } +static inline bool arch_pkey_access_rights_valid (unsigned long rights) +{ +return false; +} +#endif /* ! CONFIG_ARCH_HAS_PKEYS */ #endif /* _LINUX_PKEYS_H */ mm/mprotect.c +1
-1@@ -597,7 +597,7 @@ SYSCALL_DEFINE2(pkey_alloc, unsigned long, flags, unsigned long, init_val)if (flags)return -EINVAL ;/* check for unsupported init values */ -if (init_val & ~PKEY_ACCESS_MASK ) +if (!arch_pkey_access_rights_valid (init_val))return -EINVAL ;down_write (¤t->mm->mmap_sem);Thanks,
Florian
Signature · 1 line
Ram Pai
* Ram Pai:
Quoted 7 lines
So the problem is as follows:
Currently the kernel supports 'disable-write' and 'disable-access'.
On x86, cpu supports 'disable-write' and 'disable-access'. This matches with what the kernel supports. All good.
Quoted 6 lines
However on power, cpu supports 'disable-read' too. Since userspace can program the cpu directly, userspace has the ability to set 'disable-read' too. This can lead to inconsistency between the kernel and the userspace.
We want the kernel to match userspace on all architectures.
Correct.
Quoted 8 lines
Proposed Solution:
Enhance the kernel to understand 'disable-read', and facilitate architectures that understand 'disable-read' to allow it.
Also explicitly define the semantics of disable-access as 'disable-read and disable-write'
Quoted 6 lines
Did I get this right? Assuming I did, the implementation has to do the following --
On power,
sys_pkey_alloc()should succeed if theinit_valisPKEY_DISABLE_READ,PKEY_DISABLE_WRITE,PKEY_DISABLE_ACCESSor any combination of the three.
Agreed.
Quoted 4 lines
On x86,
sys_pkey_alloc()should succeed if theinit_valisPKEY_DISABLE_WRITEorPKEY_DISABLE_ACCESSorPKEY_DISABLE_READor any combination of the three, exceptPKEY_DISABLE_READspecified all by itself.
Again agreed. That's a clever way of phrasing it actually.
Quoted 4 lines
On all other arches, none of the flags are supported.
Are we on the same plate?
I think so, thanks.
Florian
Quoted 42 lines · Florian Weimer
* Ram Pai:
Quoted 7 lines
So the problem is as follows:
Currently the kernel supports 'disable-write' and 'disable-access'.
On x86, cpu supports 'disable-write' and 'disable-access'. This matches with what the kernel supports. All good.
Quoted 6 lines
However on power, cpu supports 'disable-read' too. Since userspace can program the cpu directly, userspace has the ability to set 'disable-read' too. This can lead to inconsistency between the kernel and the userspace.
We want the kernel to match userspace on all architectures.
Correct.
Quoted 8 lines
Proposed Solution:
Enhance the kernel to understand 'disable-read', and facilitate architectures that understand 'disable-read' to allow it.
Also explicitly define the semantics of disable-access as 'disable-read and disable-write'
Quoted 6 lines
Did I get this right? Assuming I did, the implementation has to do the following --
On power,
sys_pkey_alloc()should succeed if theinit_valisPKEY_DISABLE_READ,PKEY_DISABLE_WRITE,PKEY_DISABLE_ACCESSor any combination of the three.Agreed.
Quoted 4 lines
On x86,
sys_pkey_alloc()should succeed if theinit_valisPKEY_DISABLE_WRITEorPKEY_DISABLE_ACCESSorPKEY_DISABLE_READor any combination of the three, exceptPKEY_DISABLE_READspecified all by itself.Again agreed. That's a clever way of phrasing it actually.
Quoted 8 lines
Quoted 4 lines
On all other arches, none of the flags are supported.
Are we on the same plate?
I think so, thanks.
Florian
Ok. here is a patch, compiled but not tested. See if this meets the specifications.
-----------------------------------------------------------------------------------
commit 3dc06e73f3795921265d5d1d935e428deab01616
Author: Ram Pai <[email protected]> Date: Tue Dec 4 00:04:11 2018 -0500
pkeys: add support of PKEY_DISABLE_READ
Kernel supports 'disable-write' and 'disable-access'.
x86 cpu supports 'disable-write' and 'disable-access'. This matches with the kernel support.
However POWER cpu supports 'disable-read' too. Since userspace can program the cpu directly, userspace has the ability to set 'disable-read' too. This can lead to inconsistency between the kernel and the userspace.
Make kernel match userspace on all architectures.
Enhance the kernel to understand 'disable-read', and facilitate architectures that understand 'disable-read' to allow it.
Define the semantics of disable-access as 'disable-read and disable-write'
arch/powerpc/include/asm/pkeys.h +10 -4
@@ -19,11 +19,7 @@#define ARCH_VM_PKEY_FLAGS (VM_PKEY_BIT0 | VM_PKEY_BIT1 | VM_PKEY_BIT2 | \ VM_PKEY_BIT3 | VM_PKEY_BIT4) -/* Override any generic PKEY permission defines */ #define PKEY_DISABLE_EXECUTE 0x4 -#define PKEY_ACCESS_MASK (PKEY_DISABLE_ACCESS | \ - PKEY_DISABLE_WRITE | \ - PKEY_DISABLE_EXECUTE)static inline u64 pkey_to_vmflag_bits (u16 pkey) {
@@ -199,6 +195,16 @@ static inline bool arch_pkeys_enabled(void)return !static_branch_likely (&pkey_disabled); } +extern bool __arch_pkey_access_rights_valid (unsigned long rights); + +static inline bool arch_pkey_access_rights_valid (unsigned long rights) +{ +if (static_branch_likely (&pkey_disabled)) +return false; + +return __arch_pkey_access_rights_valid (rights); +} +extern void pkey_mm_init (struct mm_struct *mm);extern bool arch_supports_pkeys (int cap);extern unsigned int arch_usable_pkeys (void );
arch/powerpc/include/uapi/asm/mman.h +0 -6
@@ -30,10 +30,4 @@#define MAP_STACK 0x20000/* give out an address that is best suited for process/thread stacks */ #define MAP_HUGETLB 0x40000/* create a huge page mapping */ -/* Override any generic PKEY permission defines */ -#define PKEY_DISABLE_EXECUTE 0x4 -#undef PKEY_ACCESS_MASK -#define PKEY_ACCESS_MASK (PKEY_DISABLE_ACCESS |\ - PKEY_DISABLE_WRITE |\ - PKEY_DISABLE_EXECUTE)#endif /* _UAPI_ASM_POWERPC_MMAN_H */
arch/powerpc/mm/pkeys.c +10 -1
@@ -67,7 +67,7 @@ int pkey_initialize(void)* Ensure that the bits a distinct. */ BUILD_BUG_ON (PKEY_DISABLE_EXECUTE & - (PKEY_DISABLE_ACCESS |PKEY_DISABLE_WRITE )); + (PKEY_DISABLE_READ |PKEY_DISABLE_WRITE |PKEY_DISABLE_ACCESS ));/* * pkey_to_vmflag_bits() assumes that the pkey bits are contiguous
@@ -259,11 +259,20 @@ int __arch_set_user_pkey_access(struct task_struct *tsk, int pkey, new_amr_bits |=AMR_RD_BIT |AMR_WR_BIT ; else if (init_val & PKEY_DISABLE_WRITE) new_amr_bits |= AMR_WR_BIT; + else if (init_val & PKEY_DISABLE_READ) + new_amr_bits |= AMR_RD_BIT;init_amr (pkey ,new_amr_bits );return 0 ; } +bool __arch_pkey_access_rights_valid (unsigned long rights) +{ +unsigned long mask =PKEY_DISABLE_READ |PKEY_DISABLE_WRITE |\ +PKEY_DISABLE_ACCESS ; +return (rights & mask); +} +void thread_pkey_regs_save (struct thread_struct *thread) {if (static_branch_likely (&pkey_disabled))
arch/x86/include/asm/pkeys.h +9 -0
@@ -14,6 +14,15 @@ static inline bool arch_pkeys_enabled(void)return boot_cpu_has (X86_FEATURE_OSPKE ); } +extern bool __arch_pkey_access_rights_valid (unsigned long rights); +static inline bool arch_pkey_access_rights_valid (unsigned long rights) +{ +if (!boot_cpu_has (X86_FEATURE_OSPKE )) +return false; + +return __arch_pkey_access_rights_valid (rights); +} +/* * Try to dedicate one of the protection keys to be used as an * execute-only protection key.
arch/x86/mm/pkeys.c +11 -0
@@ -72,6 +72,17 @@ int __execute_only_pkey(struct mm_struct *mm)return execute_only_pkey; } +bool __arch_pkey_access_rights_valid (unsigned long rights) +{ +unsigned long mask =PKEY_DISABLE_READ |PKEY_DISABLE_WRITE |\ +PKEY_DISABLE_ACCESS ; +if (!(rights & mask)) +return false; + +/* return failure if only PKEY_DISABLE_READ is specified */ +return ((rights & mask) !=PKEY_DISABLE_READ ); +} +static inline bool vma_is_pkey_exec_only (struct vm_area_struct *vma) {/* Do this check first since the vm_flags should be hot */
include/linux/pkeys.h +5 -0
@@ -48,6 +48,11 @@ static inline void copy_init_pkru_to_fpregs(void) { } +static inline bool arch_pkey_access_rights_valid (unsigned long rights) +{ +return false; +} +#endif /* ! CONFIG_ARCH_HAS_PKEYS */ #endif /* _LINUX_PKEYS_H */
include/uapi/asm-generic/mman-common.h +1 -3
@@ -71,7 +71,5 @@#define PKEY_DISABLE_ACCESS 0x1#define PKEY_DISABLE_WRITE 0x2 -#define PKEY_ACCESS_MASK (PKEY_DISABLE_ACCESS |\ - PKEY_DISABLE_WRITE) - +#define PKEY_DISABLE_READ 0x10#endif /* __ASM_GENERIC_MMAN_COMMON_H */
mm/mprotect.c +1 -1
@@ -597,7 +597,7 @@ static int do_mprotect_pkey(unsigned long start, size_t len,if (flags)return -EINVAL ;/* check for unsupported init values */ -if (init_val & ~PKEY_ACCESS_MASK ) +if (!arch_pkey_access_rights_valid (init_val))return -EINVAL ;down_write (¤t->mm->mmap_sem);
-----------------------------------------------------------------------------
* Ram Pai:
Quoted 10 lines
Ok. here is a patch, compiled but not tested. See if this meets the specifications.
-----------------------------------------------------------------------------------
commit
3dc06e73f3795921265d5d1d935e428deab01616Author: Ram Pai <[email protected]> Date: Tue Dec 4 00:04:11 2018 -0500pkeys: add support of
PKEY_DISABLE_READ
Thanks. In the x86 code, the translation of PKEY_DISABLE_READ | PKEY_DISABLE_WRITE to PKEY_DISABLE_ACCESS appears to be missing. I believe the existing code produces PKEY_DISABLE_WRITE, which is wrong.
Rest looks okay to me (again not tested).
Thanks,
Florian
Quoted 11 lines · Florian Weimer
* Ram Pai:
Quoted 9 lines
Ok. here is a patch, compiled but not tested. See if this meets the specifications.
-----------------------------------------------------------------------------------
commit
3dc06e73f3795921265d5d1d935e428deab01616Author: Ram Pai <[email protected]> Date: Tue Dec 4 00:04:11 2018 -0500
Quoted 5 lines
Quoted 1 line
pkeys: add support of
PKEY_DISABLE_READThanks. In the x86 code, the translation of
PKEY_DISABLE_READ|PKEY_DISABLE_WRITEtoPKEY_DISABLE_ACCESSappears to be missing. I believe the existing code producesPKEY_DISABLE_WRITE, which is wrong.
ah. yes. good point.
Quoted 2 lines
Rest looks okay to me (again not tested).
thanks,
RP
Andy Lutomirski
maintainerQuoted 52 lines · Ram Pai
Quoted 9 lines · Florian Weimer
* Dave Hansen:
Quoted 5 lines
Quoted 5 lines · Florian Weimer
I would have expected something that translates
PKEY_DISABLE_WRITE|PKEY_DISABLE_READintoPKEY_DISABLE_ACCESS, and also acceptsPKEY_DISABLE_ACCESS|PKEY_DISABLE_READ, for consistency with POWER.Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
(My understanding is that
PKEY_DISABLE_ACCESSdoes not disable all access, but produces execute-only memory.)Correct, it disables all data access, but not execution.
So I would expect something like this (completely untested, I did not even compile this):
Ok. I re-read through the entire email thread to understand the problem and the proposed solution. Let me summarize it below. Lets see if we are on the same plate.
So the problem is as follows:
Currently the kernel supports 'disable-write' and 'disable-access'.
On x86, cpu supports 'disable-write' and 'disable-access'. This matches with what the kernel supports. All good.
However on power, cpu supports 'disable-read' too. Since userspace can program the cpu directly, userspace has the ability to set 'disable-read' too. This can lead to inconsistency between the kernel and the userspace.
We want the kernel to match userspace on all architectures.
Proposed Solution:
Enhance the kernel to understand 'disable-read', and facilitate architectures that understand 'disable-read' to allow it.
Also explicitly define the semantics of disable-access as 'disable-read and disable-write'
Did I get this right? Assuming I did, the implementation has to do the following --
On power,
sys_pkey_alloc()should succeed if theinit_valisPKEY_DISABLE_READ,PKEY_DISABLE_WRITE,PKEY_DISABLE_ACCESSor any combination of the three.
Quoted 6 lines
On x86,
sys_pkey_alloc()should succeed if theinit_valisPKEY_DISABLE_WRITEorPKEY_DISABLE_ACCESSorPKEY_DISABLE_READor any combination of the three, exceptPKEY_DISABLE_READ
specified all by itself.On all other arches, none of the flags are supported.
I don’t really love having a situation where you can use different flag combinations to refer to the same mode.
Also, we should document the effect these flags have on execute permission.
Quoted 57 lines · Andy Lutomirski
Quoted 57 lines · Ram Pai
Quoted 9 lines · Florian Weimer
* Dave Hansen:
Quoted 5 lines
Quoted 5 lines · Florian Weimer
I would have expected something that translates
PKEY_DISABLE_WRITE|PKEY_DISABLE_READintoPKEY_DISABLE_ACCESS, and also acceptsPKEY_DISABLE_ACCESS|PKEY_DISABLE_READ, for consistency with POWER.Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
(My understanding is that
PKEY_DISABLE_ACCESSdoes not disable all access, but produces execute-only memory.)Correct, it disables all data access, but not execution.
So I would expect something like this (completely untested, I did not even compile this):
Ok. I re-read through the entire email thread to understand the problem and the proposed solution. Let me summarize it below. Lets see if we are on the same plate.
So the problem is as follows:
Currently the kernel supports 'disable-write' and 'disable-access'.
On x86, cpu supports 'disable-write' and 'disable-access'. This matches with what the kernel supports. All good.
However on power, cpu supports 'disable-read' too. Since userspace can program the cpu directly, userspace has the ability to set 'disable-read' too. This can lead to inconsistency between the kernel and the userspace.
We want the kernel to match userspace on all architectures.
Proposed Solution:
Enhance the kernel to understand 'disable-read', and facilitate architectures that understand 'disable-read' to allow it.
Also explicitly define the semantics of disable-access as 'disable-read and disable-write'
Did I get this right? Assuming I did, the implementation has to do the following --
On power,
sys_pkey_alloc()should succeed if theinit_valisPKEY_DISABLE_READ,PKEY_DISABLE_WRITE,PKEY_DISABLE_ACCESSor any combination of the three.On x86,
sys_pkey_alloc()should succeed if theinit_valisPKEY_DISABLE_WRITEorPKEY_DISABLE_ACCESSorPKEY_DISABLE_READor any combination of the three, exceptPKEY_DISABLE_READ
specified all by itself.
Quoted 4 lines
Quoted 1 line
On all other arches, none of the flags are supported.
I don’t really love having a situation where you can use different flag combinations to refer to the same mode.
true. But it is a side-effect of x86 cpu implicitly defining 'disable-access' as a combination of 'disable-read' and 'disable_write'. In other words, if you disable-access on a pte on x86, you are automatically disabling read and disabling write on that page. The software/kernel just happens to explicitly capture that implicit behavior.
Quoted 2 lines
Also, we should document the effect these flags have on execute permission.
Actually none of the above flags, interact with execute permission. They operate independently; both on x86 and on POWER. But yes, this statement needs to be documented somewhere.
RP
Quoted 7 lines · Dave Hansen
Quoted 3 lines · Florian Weimer
Ideally,
PKEY_DISABLE_READ|PKEY_DISABLE_WRITEandPKEY_DISABLE_READ|PKEY_DISABLE_ACCESSwould be treated asPKEY_DISABLE_ACCESSboth, and a linePKEY_DISABLE_READwould result in an EINVAL failure.Sounds reasonable to me.
Quoted 7 lines
I don't see any urgency to do this right now. It could easily go in alongside the ppc patches when those get merged. The only thing I'd suggest is that we make it something slightly higher than
0x4. It'll make the code easier to deal with in the kernel if we have the ABI and the hardware mirror each other, and if we pick0x4in the ABI forPKEY_DISABLE_READ, it might get messy if the harware choose0x4forPKEY_DISABLE_EXECUTEor something.
The hardware bits have to be decoupled from the software bits. Otherwise we will get too constrainted and will conflict with the bit configuration of some hardware. Powerpc implementation can deal with 0x4 or any other value.
Quoted 5 lines
So, let's make it
0x80or something on x86 at least.Also, I'll be happy to review and ack the patch to do this, but I'd expect the ppc guys (hi Ram!) to actually put it together.
Hi Dave! :) So what is needed? Support a new flag PKEY_DISABLE_READ, and make it return error for all architectures?
Or are we enhancing the symantics of pkey_alloc() to allocate keys with just disable-read permissions.? And if so, will x86 be able to support that semantics?
Signature · 1 line
Ram Pai
Quoted 4 lines · Ram Pai
Quoted 2 lines
Also, I'll be happy to review and ack the patch to do this, but I'd expect the ppc guys (hi Ram!) to actually put it together.
Hi Dave! :) So what is needed? Support a new flag
PKEY_DISABLE_READ, and make it return error for all architectures?
Florian proposed some semantics further up in the thread. Basically if someone asks for PKEY_DISABLE_READ, allow it to be accepted as long as its compatible with the other flags.
* Ram Pai:
Quoted 2 lines
Hi Dave! :) So what is needed? Support a new flag
PKEY_DISABLE_READ, and make it return error for all architectures?
PKEY_DISABLE_READ | PKEY_DISABLE_WRITE should be equivalent to PKEY_DISABLE_ACCESS. PKEY_DISABLE_READ without any other flag on x86 should return EINVAL (as for other invalid access rights specified for pkey_alloc).
Quoted 3 lines
Or are we enhancing the symantics of
pkey_alloc()to allocate keys with just disable-read permissions.? And if so, will x86 be able to support that semantics?
I think x86 cannot do this, but POWER can, but it's currently not possible to express this via pkey_alloc. That could be fixed, too.
Thanks,
Florian
Quoted 5 lines · Florian Weimer
Would it be possible to reserve a bit for
PKEY_DISABLE_READ?I think the POWER implementation can disable read access at the hardware level, but not write access, and that cannot be expressed with the current
PKEY_DISABLE_ACCESSandPKEY_DISABLE_WRITEbits.
POWER hardware can disable-read and can **also disable-write** at the hardware level. It can disable-execute aswell at the
hardware level. For example if the key bits for a given key in the AMR
register is
0b01 it is read-disable
0b10 it is write-disable
To support access-disable, we make the key value 0b11.
So in case if you want to know if the key is read-disable 'bitwise-and' it against 0x1. i.e (x & 0x1)
RP
* Ram Pai:
Quoted 14 lines · Florian Weimer
Quoted 5 lines
Would it be possible to reserve a bit for
PKEY_DISABLE_READ?I think the POWER implementation can disable read access at the hardware level, but not write access, and that cannot be expressed with the current
PKEY_DISABLE_ACCESSandPKEY_DISABLE_WRITEbits.POWER hardware can disable-read and can **also disable-write** at the hardware level. It can disable-execute aswell at the
hardware level. For example if the key bits for a given key in the AMR
register is
0b01 it is read-disable
0b10 it is write-disable
Quoted 4 lines
To support access-disable, we make the key value 0b11.
So in case if you want to know if the key is read-disable 'bitwise-and' it against
0x1. i.e (x &0x1)
Not sure if we covered that alreay, but my problem is that I cannot translate a 0b01 mask to a PKEY_DISABLE_* flag combination with the current flags. 0b10 and 0b11 are fine.
POWER also loses the distinction between PKEY_DISABLE_ACCESS and PKEY_DISABLE_ACCESS | PKEY_DISABLE_WRITE, but that's fine. This breaks the current glibc test case, but I have a patch for that. Arguably, the test is wrong or at least overly strict in what it accepts.
Thanks,
Florian