2018-01-19 01:50:24 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
|
|
|
/*
|
|
|
|
* PowerPC Memory Protection Keys management
|
|
|
|
*
|
|
|
|
* Copyright 2017, Ram Pai, IBM Corporation.
|
|
|
|
*/
|
|
|
|
|
2018-01-19 01:50:29 +00:00
|
|
|
#include <asm/mman.h>
|
2018-10-22 14:54:20 +00:00
|
|
|
#include <asm/mmu_context.h>
|
2019-04-18 06:51:24 +00:00
|
|
|
#include <asm/mmu.h>
|
2018-01-19 01:50:44 +00:00
|
|
|
#include <asm/setup.h>
|
2018-01-19 01:50:24 +00:00
|
|
|
#include <linux/pkeys.h>
|
2020-07-09 03:29:36 +00:00
|
|
|
#include <linux/of_fdt.h>
|
|
|
|
|
2020-07-09 03:29:33 +00:00
|
|
|
int num_pkey; /* Max number of pkeys supported */
|
2020-07-09 03:29:29 +00:00
|
|
|
/*
|
|
|
|
* Keys marked in the reservation list cannot be allocated by userspace
|
|
|
|
*/
|
2020-07-09 03:29:34 +00:00
|
|
|
u32 reserved_allocation_mask __ro_after_init;
|
|
|
|
|
|
|
|
/* Bits set for the initially allocated keys */
|
|
|
|
static u32 initial_allocation_mask __ro_after_init;
|
|
|
|
|
2020-07-09 03:29:29 +00:00
|
|
|
/*
|
|
|
|
* Even if we allocate keys with sys_pkey_alloc(), we need to make sure
|
|
|
|
* other thread still find the access denied using the same keys.
|
|
|
|
*/
|
|
|
|
static u64 default_amr = ~0x0UL;
|
|
|
|
static u64 default_iamr = 0x5555555555555555UL;
|
2020-07-09 03:29:42 +00:00
|
|
|
u64 default_uamor __ro_after_init;
|
2020-07-09 03:29:29 +00:00
|
|
|
/*
|
|
|
|
* Key used to implement PROT_EXEC mmap. Denies READ/WRITE
|
|
|
|
* We pick key 2 because 0 is special key and 1 is reserved as per ISA.
|
|
|
|
*/
|
2018-10-22 14:54:20 +00:00
|
|
|
static int execute_only_key = 2;
|
2020-07-09 03:29:38 +00:00
|
|
|
static bool pkey_execute_disable_supported;
|
2018-01-19 01:50:24 +00:00
|
|
|
|
2020-07-09 03:29:29 +00:00
|
|
|
|
2018-01-19 01:50:27 +00:00
|
|
|
#define AMR_BITS_PER_PKEY 2
|
2018-01-19 01:50:29 +00:00
|
|
|
#define AMR_RD_BIT 0x1UL
|
|
|
|
#define AMR_WR_BIT 0x2UL
|
|
|
|
#define IAMR_EX_BIT 0x1UL
|
2020-07-09 03:29:29 +00:00
|
|
|
#define PKEY_REG_BITS (sizeof(u64) * 8)
|
2018-01-19 01:50:27 +00:00
|
|
|
#define pkeyshift(pkey) (PKEY_REG_BITS - ((pkey+1) * AMR_BITS_PER_PKEY))
|
|
|
|
|
2020-07-09 03:29:36 +00:00
|
|
|
static int __init dt_scan_storage_keys(unsigned long node,
|
|
|
|
const char *uname, int depth,
|
|
|
|
void *data)
|
|
|
|
{
|
|
|
|
const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
|
|
|
|
const __be32 *prop;
|
|
|
|
int *pkeys_total = (int *) data;
|
|
|
|
|
|
|
|
/* We are scanning "cpu" nodes only */
|
|
|
|
if (type == NULL || strcmp(type, "cpu") != 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
prop = of_get_flat_dt_prop(node, "ibm,processor-storage-keys", NULL);
|
|
|
|
if (!prop)
|
|
|
|
return 0;
|
|
|
|
*pkeys_total = be32_to_cpu(prop[0]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-07-09 03:29:29 +00:00
|
|
|
static int scan_pkey_feature(void)
|
2018-01-19 01:50:44 +00:00
|
|
|
{
|
2020-07-09 03:29:36 +00:00
|
|
|
int ret;
|
2020-07-09 03:29:29 +00:00
|
|
|
int pkeys_total = 0;
|
2018-01-19 01:50:44 +00:00
|
|
|
|
2020-07-09 03:29:29 +00:00
|
|
|
/*
|
|
|
|
* Pkey is not supported with Radix translation.
|
|
|
|
*/
|
2020-07-09 03:29:36 +00:00
|
|
|
if (early_radix_enabled())
|
2020-07-09 03:29:29 +00:00
|
|
|
return 0;
|
2018-01-19 01:50:44 +00:00
|
|
|
|
2020-07-09 03:29:36 +00:00
|
|
|
ret = of_scan_flat_dt(dt_scan_storage_keys, &pkeys_total);
|
|
|
|
if (ret == 0) {
|
2020-07-09 03:29:29 +00:00
|
|
|
/*
|
|
|
|
* Let's assume 32 pkeys on P8/P9 bare metal, if its not defined by device
|
|
|
|
* tree. We make this exception since some version of skiboot forgot to
|
|
|
|
* expose this property on power8/9.
|
|
|
|
*/
|
|
|
|
if (!firmware_has_feature(FW_FEATURE_LPAR)) {
|
|
|
|
unsigned long pvr = mfspr(SPRN_PVR);
|
|
|
|
|
|
|
|
if (PVR_VER(pvr) == PVR_POWER8 || PVR_VER(pvr) == PVR_POWER8E ||
|
|
|
|
PVR_VER(pvr) == PVR_POWER8NVL || PVR_VER(pvr) == PVR_POWER9)
|
|
|
|
pkeys_total = 32;
|
|
|
|
}
|
|
|
|
}
|
2018-01-19 01:50:44 +00:00
|
|
|
|
|
|
|
/*
|
2020-07-09 03:29:29 +00:00
|
|
|
* Adjust the upper limit, based on the number of bits supported by
|
|
|
|
* arch-neutral code.
|
2018-01-19 01:50:44 +00:00
|
|
|
*/
|
2020-07-09 03:29:29 +00:00
|
|
|
pkeys_total = min_t(int, pkeys_total,
|
|
|
|
((ARCH_VM_PKEY_FLAGS >> VM_PKEY_SHIFT) + 1));
|
|
|
|
return pkeys_total;
|
2018-01-19 01:50:44 +00:00
|
|
|
}
|
|
|
|
|
2020-07-09 03:29:36 +00:00
|
|
|
void __init pkey_early_init_devtree(void)
|
2018-01-19 01:50:24 +00:00
|
|
|
{
|
2020-07-09 03:29:33 +00:00
|
|
|
int pkeys_total, i;
|
2018-01-19 01:50:25 +00:00
|
|
|
|
2018-01-19 01:50:30 +00:00
|
|
|
/*
|
|
|
|
* 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));
|
|
|
|
|
2018-01-19 01:50:33 +00:00
|
|
|
/*
|
|
|
|
* pkey_to_vmflag_bits() assumes that the pkey bits are contiguous
|
|
|
|
* in the vmaflag. Make sure that is really the case.
|
|
|
|
*/
|
|
|
|
BUILD_BUG_ON(__builtin_clzl(ARCH_VM_PKEY_FLAGS >> VM_PKEY_SHIFT) +
|
|
|
|
__builtin_popcountl(ARCH_VM_PKEY_FLAGS >> VM_PKEY_SHIFT)
|
|
|
|
!= (sizeof(u64) * BITS_PER_BYTE));
|
|
|
|
|
2020-08-10 10:26:23 +00:00
|
|
|
/*
|
|
|
|
* Only P7 and above supports SPRN_AMR update with MSR[PR] = 1
|
|
|
|
*/
|
|
|
|
if (!early_cpu_has_feature(CPU_FTR_ARCH_206))
|
|
|
|
return;
|
|
|
|
|
2018-01-19 01:50:44 +00:00
|
|
|
/* scan the device tree for pkey feature */
|
2020-07-09 03:29:29 +00:00
|
|
|
pkeys_total = scan_pkey_feature();
|
2020-07-09 03:29:42 +00:00
|
|
|
if (!pkeys_total)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
/* Allow all keys to be modified by default */
|
|
|
|
default_uamor = ~0x0UL;
|
2018-01-19 01:50:44 +00:00
|
|
|
|
2020-07-09 03:29:36 +00:00
|
|
|
cur_cpu_spec->mmu_features |= MMU_FTR_PKEY;
|
|
|
|
|
2018-01-19 01:50:24 +00:00
|
|
|
/*
|
2018-01-19 01:50:44 +00:00
|
|
|
* The device tree cannot be relied to indicate support for
|
|
|
|
* execute_disable support. Instead we use a PVR check.
|
2018-01-19 01:50:24 +00:00
|
|
|
*/
|
2018-01-19 01:50:44 +00:00
|
|
|
if (pvr_version_is(PVR_POWER7) || pvr_version_is(PVR_POWER7p))
|
|
|
|
pkey_execute_disable_supported = false;
|
|
|
|
else
|
|
|
|
pkey_execute_disable_supported = true;
|
2018-01-19 01:50:25 +00:00
|
|
|
|
|
|
|
#ifdef CONFIG_PPC_4K_PAGES
|
|
|
|
/*
|
|
|
|
* The OS can manage only 8 pkeys due to its inability to represent them
|
2020-07-09 03:29:29 +00:00
|
|
|
* in the Linux 4K PTE. Mark all other keys reserved.
|
2018-01-19 01:50:25 +00:00
|
|
|
*/
|
2020-07-09 03:29:33 +00:00
|
|
|
num_pkey = min(8, pkeys_total);
|
2018-01-19 01:50:25 +00:00
|
|
|
#else
|
2020-07-09 03:29:33 +00:00
|
|
|
num_pkey = pkeys_total;
|
2018-01-19 01:50:25 +00:00
|
|
|
#endif
|
powerpc/pkeys: Give all threads control of their key permissions
Currently in a multithreaded application, a key allocated by one
thread is not usable by other threads. By "not usable" we mean that
other threads are unable to change the access permissions for that
key for themselves.
When a new key is allocated in one thread, the corresponding UAMOR
bits for that thread get enabled, however the UAMOR bits for that key
for all other threads remain disabled.
Other threads have no way to set permissions on the key, and the
current default permissions are that read/write is enabled for all
keys, which means the key has no effect for other threads. Although
that may be the desired behaviour in some circumstances, having all
threads able to control their permissions for the key is more
flexible.
The current behaviour also differs from the x86 behaviour, which is
problematic for users.
To fix this, enable the UAMOR bits for all keys, at process
creation (in start_thread(), ie exec time). Since the contents of
UAMOR are inherited at fork, all threads are capable of modifying the
permissions on any key.
This is technically an ABI break on powerpc, but pkey support is fairly
new on powerpc and not widely used, and this brings us into
line with x86.
Fixes: cf43d3b26452 ("powerpc: Enable pkey subsystem")
Cc: stable@vger.kernel.org # v4.16+
Tested-by: Florian Weimer <fweimer@redhat.com>
Signed-off-by: Ram Pai <linuxram@us.ibm.com>
[mpe: Reword some of the changelog]
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
2018-07-17 13:51:02 +00:00
|
|
|
|
2020-07-09 03:29:38 +00:00
|
|
|
if (unlikely(num_pkey <= execute_only_key) || !pkey_execute_disable_supported) {
|
2018-07-17 13:51:07 +00:00
|
|
|
/*
|
|
|
|
* Insufficient number of keys to support
|
|
|
|
* execute only key. Mark it unavailable.
|
|
|
|
*/
|
|
|
|
execute_only_key = -1;
|
2020-07-09 03:29:29 +00:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Mark the execute_only_pkey as not available for
|
|
|
|
* user allocation via pkey_alloc.
|
|
|
|
*/
|
|
|
|
reserved_allocation_mask |= (0x1 << execute_only_key);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Deny READ/WRITE for execute_only_key.
|
|
|
|
* Allow execute in IAMR.
|
|
|
|
*/
|
|
|
|
default_amr |= (0x3ul << pkeyshift(execute_only_key));
|
|
|
|
default_iamr &= ~(0x1ul << pkeyshift(execute_only_key));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Clear the uamor bits for this key.
|
|
|
|
*/
|
|
|
|
default_uamor &= ~(0x3ul << pkeyshift(execute_only_key));
|
2018-07-17 13:51:07 +00:00
|
|
|
}
|
|
|
|
|
2020-07-09 03:29:29 +00:00
|
|
|
/*
|
|
|
|
* Allow access for only key 0. And prevent any other modification.
|
|
|
|
*/
|
|
|
|
default_amr &= ~(0x3ul << pkeyshift(0));
|
|
|
|
default_iamr &= ~(0x1ul << pkeyshift(0));
|
|
|
|
default_uamor &= ~(0x3ul << pkeyshift(0));
|
|
|
|
/*
|
|
|
|
* key 0 is special in that we want to consider it an allocated
|
|
|
|
* key which is preallocated. We don't allow changing AMR bits
|
|
|
|
* w.r.t key 0. But one can pkey_free(key0)
|
|
|
|
*/
|
|
|
|
initial_allocation_mask |= (0x1 << 0);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* key 1 is recommended not to be used. PowerISA(3.0) page 1015,
|
|
|
|
* programming note.
|
|
|
|
*/
|
|
|
|
reserved_allocation_mask |= (0x1 << 1);
|
2020-07-09 03:29:30 +00:00
|
|
|
default_uamor &= ~(0x3ul << pkeyshift(1));
|
2020-07-09 03:29:29 +00:00
|
|
|
|
|
|
|
/*
|
2020-07-09 03:29:33 +00:00
|
|
|
* Prevent the usage of OS reserved keys. Update UAMOR
|
2020-07-09 03:29:35 +00:00
|
|
|
* for those keys. Also mark the rest of the bits in the
|
|
|
|
* 32 bit mask as reserved.
|
2020-07-09 03:29:29 +00:00
|
|
|
*/
|
2020-07-09 03:29:35 +00:00
|
|
|
for (i = num_pkey; i < 32 ; i++) {
|
2020-07-09 03:29:29 +00:00
|
|
|
reserved_allocation_mask |= (0x1 << i);
|
|
|
|
default_uamor &= ~(0x3ul << pkeyshift(i));
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Prevent the allocation of reserved keys too.
|
|
|
|
*/
|
|
|
|
initial_allocation_mask |= reserved_allocation_mask;
|
|
|
|
|
2020-07-09 03:29:40 +00:00
|
|
|
pr_info("Enabling pkeys with max key count %d\n", num_pkey);
|
2020-07-09 03:29:42 +00:00
|
|
|
out:
|
|
|
|
/*
|
|
|
|
* Setup uamor on boot cpu
|
|
|
|
*/
|
|
|
|
mtspr(SPRN_UAMOR, default_uamor);
|
|
|
|
|
2020-07-09 03:29:36 +00:00
|
|
|
return;
|
2018-01-19 01:50:24 +00:00
|
|
|
}
|
|
|
|
|
2018-01-19 01:50:25 +00:00
|
|
|
void pkey_mm_init(struct mm_struct *mm)
|
|
|
|
{
|
2020-07-09 03:29:39 +00:00
|
|
|
if (!mmu_has_feature(MMU_FTR_PKEY))
|
2018-01-19 01:50:25 +00:00
|
|
|
return;
|
|
|
|
mm_pkey_allocation_map(mm) = initial_allocation_mask;
|
2018-07-17 13:51:07 +00:00
|
|
|
mm->context.execute_only_pkey = execute_only_key;
|
2018-01-19 01:50:25 +00:00
|
|
|
}
|
2018-01-19 01:50:26 +00:00
|
|
|
|
|
|
|
static inline u64 read_amr(void)
|
|
|
|
{
|
|
|
|
return mfspr(SPRN_AMR);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void write_amr(u64 value)
|
|
|
|
{
|
|
|
|
mtspr(SPRN_AMR, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline u64 read_iamr(void)
|
|
|
|
{
|
|
|
|
if (!likely(pkey_execute_disable_supported))
|
|
|
|
return 0x0UL;
|
|
|
|
|
|
|
|
return mfspr(SPRN_IAMR);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void write_iamr(u64 value)
|
|
|
|
{
|
|
|
|
if (!likely(pkey_execute_disable_supported))
|
|
|
|
return;
|
|
|
|
|
|
|
|
mtspr(SPRN_IAMR, value);
|
|
|
|
}
|
|
|
|
|
2018-01-19 01:50:27 +00:00
|
|
|
static inline void init_amr(int pkey, u8 init_bits)
|
|
|
|
{
|
|
|
|
u64 new_amr_bits = (((u64)init_bits & 0x3UL) << pkeyshift(pkey));
|
|
|
|
u64 old_amr = read_amr() & ~((u64)(0x3ul) << pkeyshift(pkey));
|
|
|
|
|
|
|
|
write_amr(old_amr | new_amr_bits);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void init_iamr(int pkey, u8 init_bits)
|
|
|
|
{
|
|
|
|
u64 new_iamr_bits = (((u64)init_bits & 0x1UL) << pkeyshift(pkey));
|
|
|
|
u64 old_iamr = read_iamr() & ~((u64)(0x1ul) << pkeyshift(pkey));
|
|
|
|
|
|
|
|
write_iamr(old_iamr | new_iamr_bits);
|
|
|
|
}
|
|
|
|
|
2018-01-19 01:50:29 +00:00
|
|
|
/*
|
|
|
|
* Set the access rights in AMR IAMR and UAMOR registers for @pkey to that
|
|
|
|
* specified in @init_val.
|
|
|
|
*/
|
|
|
|
int __arch_set_user_pkey_access(struct task_struct *tsk, int pkey,
|
|
|
|
unsigned long init_val)
|
|
|
|
{
|
|
|
|
u64 new_amr_bits = 0x0ul;
|
2018-01-19 01:50:30 +00:00
|
|
|
u64 new_iamr_bits = 0x0ul;
|
2020-07-09 03:29:46 +00:00
|
|
|
u64 pkey_bits, uamor_pkey_bits;
|
2018-01-19 01:50:29 +00:00
|
|
|
|
2020-07-09 03:29:46 +00:00
|
|
|
/*
|
|
|
|
* Check whether the key is disabled by UAMOR.
|
|
|
|
*/
|
|
|
|
pkey_bits = 0x3ul << pkeyshift(pkey);
|
|
|
|
uamor_pkey_bits = (default_uamor & pkey_bits);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Both the bits in UAMOR corresponding to the key should be set
|
|
|
|
*/
|
|
|
|
if (uamor_pkey_bits != pkey_bits)
|
2018-01-19 01:50:29 +00:00
|
|
|
return -EINVAL;
|
|
|
|
|
2018-01-19 01:50:30 +00:00
|
|
|
if (init_val & PKEY_DISABLE_EXECUTE) {
|
|
|
|
if (!pkey_execute_disable_supported)
|
|
|
|
return -EINVAL;
|
|
|
|
new_iamr_bits |= IAMR_EX_BIT;
|
|
|
|
}
|
|
|
|
init_iamr(pkey, new_iamr_bits);
|
|
|
|
|
2018-01-19 01:50:29 +00:00
|
|
|
/* Set the bits we need in AMR: */
|
|
|
|
if (init_val & PKEY_DISABLE_ACCESS)
|
|
|
|
new_amr_bits |= AMR_RD_BIT | AMR_WR_BIT;
|
|
|
|
else if (init_val & PKEY_DISABLE_WRITE)
|
|
|
|
new_amr_bits |= AMR_WR_BIT;
|
|
|
|
|
|
|
|
init_amr(pkey, new_amr_bits);
|
|
|
|
return 0;
|
|
|
|
}
|
2018-01-19 01:50:31 +00:00
|
|
|
|
|
|
|
void thread_pkey_regs_save(struct thread_struct *thread)
|
|
|
|
{
|
2020-07-09 03:29:39 +00:00
|
|
|
if (!mmu_has_feature(MMU_FTR_PKEY))
|
2018-01-19 01:50:31 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* TODO: Skip saving registers if @thread hasn't used any keys yet.
|
|
|
|
*/
|
|
|
|
thread->amr = read_amr();
|
|
|
|
thread->iamr = read_iamr();
|
|
|
|
}
|
|
|
|
|
|
|
|
void thread_pkey_regs_restore(struct thread_struct *new_thread,
|
|
|
|
struct thread_struct *old_thread)
|
|
|
|
{
|
2020-07-09 03:29:39 +00:00
|
|
|
if (!mmu_has_feature(MMU_FTR_PKEY))
|
2018-01-19 01:50:31 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (old_thread->amr != new_thread->amr)
|
|
|
|
write_amr(new_thread->amr);
|
|
|
|
if (old_thread->iamr != new_thread->iamr)
|
|
|
|
write_iamr(new_thread->iamr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void thread_pkey_regs_init(struct thread_struct *thread)
|
|
|
|
{
|
2020-07-09 03:29:39 +00:00
|
|
|
if (!mmu_has_feature(MMU_FTR_PKEY))
|
2018-01-19 01:50:31 +00:00
|
|
|
return;
|
|
|
|
|
2020-07-09 03:29:29 +00:00
|
|
|
thread->amr = default_amr;
|
|
|
|
thread->iamr = default_iamr;
|
powerpc/pkeys: Give all threads control of their key permissions
Currently in a multithreaded application, a key allocated by one
thread is not usable by other threads. By "not usable" we mean that
other threads are unable to change the access permissions for that
key for themselves.
When a new key is allocated in one thread, the corresponding UAMOR
bits for that thread get enabled, however the UAMOR bits for that key
for all other threads remain disabled.
Other threads have no way to set permissions on the key, and the
current default permissions are that read/write is enabled for all
keys, which means the key has no effect for other threads. Although
that may be the desired behaviour in some circumstances, having all
threads able to control their permissions for the key is more
flexible.
The current behaviour also differs from the x86 behaviour, which is
problematic for users.
To fix this, enable the UAMOR bits for all keys, at process
creation (in start_thread(), ie exec time). Since the contents of
UAMOR are inherited at fork, all threads are capable of modifying the
permissions on any key.
This is technically an ABI break on powerpc, but pkey support is fairly
new on powerpc and not widely used, and this brings us into
line with x86.
Fixes: cf43d3b26452 ("powerpc: Enable pkey subsystem")
Cc: stable@vger.kernel.org # v4.16+
Tested-by: Florian Weimer <fweimer@redhat.com>
Signed-off-by: Ram Pai <linuxram@us.ibm.com>
[mpe: Reword some of the changelog]
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
2018-07-17 13:51:02 +00:00
|
|
|
|
2020-07-09 03:29:29 +00:00
|
|
|
write_amr(default_amr);
|
|
|
|
write_iamr(default_iamr);
|
2018-01-19 01:50:31 +00:00
|
|
|
}
|
2018-01-19 01:50:32 +00:00
|
|
|
|
2020-07-09 03:29:38 +00:00
|
|
|
int execute_only_pkey(struct mm_struct *mm)
|
2018-01-19 01:50:32 +00:00
|
|
|
{
|
2018-07-17 13:51:07 +00:00
|
|
|
return mm->context.execute_only_pkey;
|
2018-01-19 01:50:32 +00:00
|
|
|
}
|
2018-01-19 01:50:34 +00:00
|
|
|
|
|
|
|
static inline bool vma_is_pkey_exec_only(struct vm_area_struct *vma)
|
|
|
|
{
|
|
|
|
/* Do this check first since the vm_flags should be hot */
|
2020-04-10 21:33:09 +00:00
|
|
|
if ((vma->vm_flags & VM_ACCESS_FLAGS) != VM_EXEC)
|
2018-01-19 01:50:34 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return (vma_pkey(vma) == vma->vm_mm->context.execute_only_pkey);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This should only be called for *plain* mprotect calls.
|
|
|
|
*/
|
|
|
|
int __arch_override_mprotect_pkey(struct vm_area_struct *vma, int prot,
|
|
|
|
int pkey)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* If the currently associated pkey is execute-only, but the requested
|
2018-05-04 20:01:51 +00:00
|
|
|
* protection is not execute-only, move it back to the default pkey.
|
2018-01-19 01:50:34 +00:00
|
|
|
*/
|
2018-05-04 20:01:51 +00:00
|
|
|
if (vma_is_pkey_exec_only(vma) && (prot != PROT_EXEC))
|
2018-01-19 01:50:34 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The requested protection is execute-only. Hence let's use an
|
|
|
|
* execute-only pkey.
|
|
|
|
*/
|
|
|
|
if (prot == PROT_EXEC) {
|
|
|
|
pkey = execute_only_pkey(vma->vm_mm);
|
|
|
|
if (pkey > 0)
|
|
|
|
return pkey;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Nothing to override. */
|
|
|
|
return vma_pkey(vma);
|
|
|
|
}
|
2018-01-19 01:50:37 +00:00
|
|
|
|
|
|
|
static bool pkey_access_permitted(int pkey, bool write, bool execute)
|
|
|
|
{
|
|
|
|
int pkey_shift;
|
|
|
|
u64 amr;
|
|
|
|
|
|
|
|
pkey_shift = pkeyshift(pkey);
|
powerpc/book3s64/pkeys: Fix pkey_access_permitted() for execute disable pkey
Even if the IAMR value denies execute access, the current code returns
true from pkey_access_permitted() for an execute permission check, if
the AMR read pkey bit is cleared.
This results in repeated page fault loop with a test like below:
#define _GNU_SOURCE
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <inttypes.h>
#include <assert.h>
#include <malloc.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/mman.h>
#ifdef SYS_pkey_mprotect
#undef SYS_pkey_mprotect
#endif
#ifdef SYS_pkey_alloc
#undef SYS_pkey_alloc
#endif
#ifdef SYS_pkey_free
#undef SYS_pkey_free
#endif
#undef PKEY_DISABLE_EXECUTE
#define PKEY_DISABLE_EXECUTE 0x4
#define SYS_pkey_mprotect 386
#define SYS_pkey_alloc 384
#define SYS_pkey_free 385
#define PPC_INST_NOP 0x60000000
#define PPC_INST_BLR 0x4e800020
#define PROT_RWX (PROT_READ | PROT_WRITE | PROT_EXEC)
static int sys_pkey_mprotect(void *addr, size_t len, int prot, int pkey)
{
return syscall(SYS_pkey_mprotect, addr, len, prot, pkey);
}
static int sys_pkey_alloc(unsigned long flags, unsigned long access_rights)
{
return syscall(SYS_pkey_alloc, flags, access_rights);
}
static int sys_pkey_free(int pkey)
{
return syscall(SYS_pkey_free, pkey);
}
static void do_execute(void *region)
{
/* jump to region */
asm volatile(
"mtctr %0;"
"bctrl"
: : "r"(region) : "ctr", "lr");
}
static void do_protect(void *region)
{
size_t pgsize;
int i, pkey;
pgsize = getpagesize();
pkey = sys_pkey_alloc(0, PKEY_DISABLE_EXECUTE);
assert (pkey > 0);
/* perform mprotect */
assert(!sys_pkey_mprotect(region, pgsize, PROT_RWX, pkey));
do_execute(region);
/* free pkey */
assert(!sys_pkey_free(pkey));
}
int main(int argc, char **argv)
{
size_t pgsize, numinsns;
unsigned int *region;
int i;
/* allocate memory region to protect */
pgsize = getpagesize();
region = memalign(pgsize, pgsize);
assert(region != NULL);
assert(!mprotect(region, pgsize, PROT_RWX));
/* fill page with NOPs with a BLR at the end */
numinsns = pgsize / sizeof(region[0]);
for (i = 0; i < numinsns - 1; i++)
region[i] = PPC_INST_NOP;
region[i] = PPC_INST_BLR;
do_protect(region);
return EXIT_SUCCESS;
}
The fix is to only check the IAMR for an execute check, the AMR value
is not relevant.
Fixes: f2407ef3ba22 ("powerpc: helper to validate key-access permissions of a pte")
Cc: stable@vger.kernel.org # v4.16+
Reported-by: Sandipan Das <sandipan@linux.ibm.com>
Signed-off-by: Aneesh Kumar K.V <aneesh.kumar@linux.ibm.com>
[mpe: Add detail to change log, tweak wording & formatting]
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
Link: https://lore.kernel.org/r/20200712132047.1038594-1-aneesh.kumar@linux.ibm.com
2020-07-12 13:20:47 +00:00
|
|
|
if (execute)
|
|
|
|
return !(read_iamr() & (IAMR_EX_BIT << pkey_shift));
|
|
|
|
|
|
|
|
amr = read_amr();
|
|
|
|
if (write)
|
|
|
|
return !(amr & (AMR_WR_BIT << pkey_shift));
|
2018-01-19 01:50:37 +00:00
|
|
|
|
powerpc/book3s64/pkeys: Fix pkey_access_permitted() for execute disable pkey
Even if the IAMR value denies execute access, the current code returns
true from pkey_access_permitted() for an execute permission check, if
the AMR read pkey bit is cleared.
This results in repeated page fault loop with a test like below:
#define _GNU_SOURCE
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <inttypes.h>
#include <assert.h>
#include <malloc.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/mman.h>
#ifdef SYS_pkey_mprotect
#undef SYS_pkey_mprotect
#endif
#ifdef SYS_pkey_alloc
#undef SYS_pkey_alloc
#endif
#ifdef SYS_pkey_free
#undef SYS_pkey_free
#endif
#undef PKEY_DISABLE_EXECUTE
#define PKEY_DISABLE_EXECUTE 0x4
#define SYS_pkey_mprotect 386
#define SYS_pkey_alloc 384
#define SYS_pkey_free 385
#define PPC_INST_NOP 0x60000000
#define PPC_INST_BLR 0x4e800020
#define PROT_RWX (PROT_READ | PROT_WRITE | PROT_EXEC)
static int sys_pkey_mprotect(void *addr, size_t len, int prot, int pkey)
{
return syscall(SYS_pkey_mprotect, addr, len, prot, pkey);
}
static int sys_pkey_alloc(unsigned long flags, unsigned long access_rights)
{
return syscall(SYS_pkey_alloc, flags, access_rights);
}
static int sys_pkey_free(int pkey)
{
return syscall(SYS_pkey_free, pkey);
}
static void do_execute(void *region)
{
/* jump to region */
asm volatile(
"mtctr %0;"
"bctrl"
: : "r"(region) : "ctr", "lr");
}
static void do_protect(void *region)
{
size_t pgsize;
int i, pkey;
pgsize = getpagesize();
pkey = sys_pkey_alloc(0, PKEY_DISABLE_EXECUTE);
assert (pkey > 0);
/* perform mprotect */
assert(!sys_pkey_mprotect(region, pgsize, PROT_RWX, pkey));
do_execute(region);
/* free pkey */
assert(!sys_pkey_free(pkey));
}
int main(int argc, char **argv)
{
size_t pgsize, numinsns;
unsigned int *region;
int i;
/* allocate memory region to protect */
pgsize = getpagesize();
region = memalign(pgsize, pgsize);
assert(region != NULL);
assert(!mprotect(region, pgsize, PROT_RWX));
/* fill page with NOPs with a BLR at the end */
numinsns = pgsize / sizeof(region[0]);
for (i = 0; i < numinsns - 1; i++)
region[i] = PPC_INST_NOP;
region[i] = PPC_INST_BLR;
do_protect(region);
return EXIT_SUCCESS;
}
The fix is to only check the IAMR for an execute check, the AMR value
is not relevant.
Fixes: f2407ef3ba22 ("powerpc: helper to validate key-access permissions of a pte")
Cc: stable@vger.kernel.org # v4.16+
Reported-by: Sandipan Das <sandipan@linux.ibm.com>
Signed-off-by: Aneesh Kumar K.V <aneesh.kumar@linux.ibm.com>
[mpe: Add detail to change log, tweak wording & formatting]
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
Link: https://lore.kernel.org/r/20200712132047.1038594-1-aneesh.kumar@linux.ibm.com
2020-07-12 13:20:47 +00:00
|
|
|
return !(amr & (AMR_RD_BIT << pkey_shift));
|
2018-01-19 01:50:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool arch_pte_access_permitted(u64 pte, bool write, bool execute)
|
|
|
|
{
|
2020-07-09 03:29:39 +00:00
|
|
|
if (!mmu_has_feature(MMU_FTR_PKEY))
|
2018-01-19 01:50:37 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return pkey_access_permitted(pte_to_pkey_bits(pte), write, execute);
|
|
|
|
}
|
2018-01-19 01:50:39 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We only want to enforce protection keys on the current thread because we
|
|
|
|
* effectively have no access to AMR/IAMR for other threads or any way to tell
|
|
|
|
* which AMR/IAMR in a threaded process we could use.
|
|
|
|
*
|
|
|
|
* So do not enforce things if the VMA is not from the current mm, or if we are
|
|
|
|
* in a kernel thread.
|
|
|
|
*/
|
|
|
|
bool arch_vma_access_permitted(struct vm_area_struct *vma, bool write,
|
|
|
|
bool execute, bool foreign)
|
|
|
|
{
|
2020-07-09 03:29:39 +00:00
|
|
|
if (!mmu_has_feature(MMU_FTR_PKEY))
|
2018-01-19 01:50:39 +00:00
|
|
|
return true;
|
|
|
|
/*
|
|
|
|
* Do not enforce our key-permissions on a foreign vma.
|
|
|
|
*/
|
|
|
|
if (foreign || vma_is_foreign(vma))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return pkey_access_permitted(vma_pkey(vma), write, execute);
|
|
|
|
}
|
2018-12-20 20:03:30 +00:00
|
|
|
|
|
|
|
void arch_dup_pkeys(struct mm_struct *oldmm, struct mm_struct *mm)
|
|
|
|
{
|
2020-07-09 03:29:39 +00:00
|
|
|
if (!mmu_has_feature(MMU_FTR_PKEY))
|
2018-12-20 20:03:30 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
/* Duplicate the oldmm pkey state in mm: */
|
|
|
|
mm_pkey_allocation_map(mm) = mm_pkey_allocation_map(oldmm);
|
|
|
|
mm->context.execute_only_pkey = oldmm->context.execute_only_pkey;
|
|
|
|
}
|