^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Intel Memory Protection Keys management
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (c) 2015, Intel Corporation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/debugfs.h> /* debugfs_create_u32() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/mm_types.h> /* mm_struct, vma, etc... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/pkeys.h> /* PKEY_* */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <uapi/asm-generic/mman-common.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <asm/cpufeature.h> /* boot_cpu_has, ... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <asm/mmu_context.h> /* vma_pkey() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <asm/fpu/internal.h> /* init_fpstate */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) int __execute_only_pkey(struct mm_struct *mm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) bool need_to_set_mm_pkey = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) int execute_only_pkey = mm->context.execute_only_pkey;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) /* Do we need to assign a pkey for mm's execute-only maps? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) if (execute_only_pkey == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) /* Go allocate one to use, which might fail */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) execute_only_pkey = mm_pkey_alloc(mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) if (execute_only_pkey < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) need_to_set_mm_pkey = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * We do not want to go through the relatively costly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * dance to set PKRU if we do not need to. Check it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * first and assume that if the execute-only pkey is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * write-disabled that we do not have to set it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * ourselves.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) if (!need_to_set_mm_pkey &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) !__pkru_allows_read(read_pkru(), execute_only_pkey)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) return execute_only_pkey;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) * Set up PKRU so that it denies access for everything
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) * other than execution.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) ret = arch_set_user_pkey_access(current, execute_only_pkey,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) PKEY_DISABLE_ACCESS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * If the PKRU-set operation failed somehow, just return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) * 0 and effectively disable execute-only support.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) mm_set_pkey_free(mm, execute_only_pkey);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) /* We got one, store it and use it from here on out */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) if (need_to_set_mm_pkey)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) mm->context.execute_only_pkey = execute_only_pkey;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) return execute_only_pkey;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) static inline bool vma_is_pkey_exec_only(struct vm_area_struct *vma)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) /* Do this check first since the vm_flags should be hot */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) if ((vma->vm_flags & VM_ACCESS_FLAGS) != VM_EXEC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) if (vma_pkey(vma) != vma->vm_mm->context.execute_only_pkey)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) * This is only called for *plain* mprotect calls.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) int __arch_override_mprotect_pkey(struct vm_area_struct *vma, int prot, int pkey)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) * Is this an mprotect_pkey() call? If so, never
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) * override the value that came from the user.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) if (pkey != -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) return pkey;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) * The mapping is execute-only. Go try to get the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) * execute-only protection key. If we fail to do that,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) * fall through as if we do not have execute-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) * support in this mm.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) if (prot == PROT_EXEC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) pkey = execute_only_pkey(vma->vm_mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) if (pkey > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) return pkey;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) } else if (vma_is_pkey_exec_only(vma)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) * Protections are *not* PROT_EXEC, but the mapping
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) * is using the exec-only pkey. This mapping was
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) * PROT_EXEC and will no longer be. Move back to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) * the default pkey.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) return ARCH_DEFAULT_PKEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) * This is a vanilla, non-pkey mprotect (or we failed to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) * setup execute-only), inherit the pkey from the VMA we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) * are working on.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) return vma_pkey(vma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) #define PKRU_AD_KEY(pkey) (PKRU_AD_BIT << ((pkey) * PKRU_BITS_PER_PKEY))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) * Make the default PKRU value (at execve() time) as restrictive
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) * as possible. This ensures that any threads clone()'d early
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) * in the process's lifetime will not accidentally get access
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) * to data which is pkey-protected later on.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) u32 init_pkru_value = PKRU_AD_KEY( 1) | PKRU_AD_KEY( 2) | PKRU_AD_KEY( 3) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) PKRU_AD_KEY( 4) | PKRU_AD_KEY( 5) | PKRU_AD_KEY( 6) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) PKRU_AD_KEY( 7) | PKRU_AD_KEY( 8) | PKRU_AD_KEY( 9) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) PKRU_AD_KEY(10) | PKRU_AD_KEY(11) | PKRU_AD_KEY(12) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) PKRU_AD_KEY(13) | PKRU_AD_KEY(14) | PKRU_AD_KEY(15);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) * Called from the FPU code when creating a fresh set of FPU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) * registers. This is called from a very specific context where
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) * we know the FPU regstiers are safe for use and we can use PKRU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) * directly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) void copy_init_pkru_to_fpregs(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) u32 init_pkru_value_snapshot = READ_ONCE(init_pkru_value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) * Override the PKRU state that came from 'init_fpstate'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) * with the baseline from the process.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) write_pkru(init_pkru_value_snapshot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) static ssize_t init_pkru_read_file(struct file *file, char __user *user_buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) size_t count, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) char buf[32];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) unsigned int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) len = sprintf(buf, "0x%x\n", init_pkru_value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) return simple_read_from_buffer(user_buf, count, ppos, buf, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) static ssize_t init_pkru_write_file(struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) const char __user *user_buf, size_t count, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) struct pkru_state *pk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) char buf[32];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) ssize_t len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) u32 new_init_pkru;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) len = min(count, sizeof(buf) - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) if (copy_from_user(buf, user_buf, len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) /* Make the buffer a valid string that we can not overrun */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) buf[len] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) if (kstrtouint(buf, 0, &new_init_pkru))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) * Don't allow insane settings that will blow the system
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) * up immediately if someone attempts to disable access
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) * or writes to pkey 0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) if (new_init_pkru & (PKRU_AD_BIT|PKRU_WD_BIT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) WRITE_ONCE(init_pkru_value, new_init_pkru);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) pk = get_xsave_addr(&init_fpstate.xsave, XFEATURE_PKRU);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) if (!pk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) pk->pkru = new_init_pkru;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) static const struct file_operations fops_init_pkru = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) .read = init_pkru_read_file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) .write = init_pkru_write_file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) .llseek = default_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) static int __init create_init_pkru_value(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) debugfs_create_file("init_pkru", S_IRUSR | S_IWUSR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) arch_debugfs_dir, NULL, &fops_init_pkru);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) late_initcall(create_init_pkru_value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) static __init int setup_init_pkru(char *opt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) u32 new_init_pkru;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) if (kstrtouint(opt, 0, &new_init_pkru))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) WRITE_ONCE(init_pkru_value, new_init_pkru);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) __setup("init_pkru=", setup_init_pkru);