| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * Intel Memory Protection Keys management |
|---|
| 3 | 4 | * Copyright (c) 2015, Intel Corporation. |
|---|
| 4 | | - * |
|---|
| 5 | | - * This program is free software; you can redistribute it and/or modify it |
|---|
| 6 | | - * under the terms and conditions of the GNU General Public License, |
|---|
| 7 | | - * version 2, as published by the Free Software Foundation. |
|---|
| 8 | | - * |
|---|
| 9 | | - * This program is distributed in the hope it will be useful, but WITHOUT |
|---|
| 10 | | - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|---|
| 11 | | - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
|---|
| 12 | | - * more details. |
|---|
| 13 | 5 | */ |
|---|
| 14 | 6 | #include <linux/debugfs.h> /* debugfs_create_u32() */ |
|---|
| 15 | 7 | #include <linux/mm_types.h> /* mm_struct, vma, etc... */ |
|---|
| .. | .. |
|---|
| 18 | 10 | |
|---|
| 19 | 11 | #include <asm/cpufeature.h> /* boot_cpu_has, ... */ |
|---|
| 20 | 12 | #include <asm/mmu_context.h> /* vma_pkey() */ |
|---|
| 13 | +#include <asm/fpu/internal.h> /* init_fpstate */ |
|---|
| 21 | 14 | |
|---|
| 22 | 15 | int __execute_only_pkey(struct mm_struct *mm) |
|---|
| 23 | 16 | { |
|---|
| .. | .. |
|---|
| 39 | 32 | * dance to set PKRU if we do not need to. Check it |
|---|
| 40 | 33 | * first and assume that if the execute-only pkey is |
|---|
| 41 | 34 | * write-disabled that we do not have to set it |
|---|
| 42 | | - * ourselves. We need preempt off so that nobody |
|---|
| 43 | | - * can make fpregs inactive. |
|---|
| 35 | + * ourselves. |
|---|
| 44 | 36 | */ |
|---|
| 45 | | - preempt_disable(); |
|---|
| 46 | 37 | if (!need_to_set_mm_pkey && |
|---|
| 47 | | - current->thread.fpu.initialized && |
|---|
| 48 | 38 | !__pkru_allows_read(read_pkru(), execute_only_pkey)) { |
|---|
| 49 | | - preempt_enable(); |
|---|
| 50 | 39 | return execute_only_pkey; |
|---|
| 51 | 40 | } |
|---|
| 52 | | - preempt_enable(); |
|---|
| 53 | 41 | |
|---|
| 54 | 42 | /* |
|---|
| 55 | 43 | * Set up PKRU so that it denies access for everything |
|---|
| .. | .. |
|---|
| 75 | 63 | static inline bool vma_is_pkey_exec_only(struct vm_area_struct *vma) |
|---|
| 76 | 64 | { |
|---|
| 77 | 65 | /* Do this check first since the vm_flags should be hot */ |
|---|
| 78 | | - if ((vma->vm_flags & (VM_READ | VM_WRITE | VM_EXEC)) != VM_EXEC) |
|---|
| 66 | + if ((vma->vm_flags & VM_ACCESS_FLAGS) != VM_EXEC) |
|---|
| 79 | 67 | return false; |
|---|
| 80 | 68 | if (vma_pkey(vma) != vma->vm_mm->context.execute_only_pkey) |
|---|
| 81 | 69 | return false; |
|---|
| .. | .. |
|---|
| 147 | 135 | { |
|---|
| 148 | 136 | u32 init_pkru_value_snapshot = READ_ONCE(init_pkru_value); |
|---|
| 149 | 137 | /* |
|---|
| 150 | | - * Any write to PKRU takes it out of the XSAVE 'init |
|---|
| 151 | | - * state' which increases context switch cost. Avoid |
|---|
| 152 | | - * writing 0 when PKRU was already 0. |
|---|
| 153 | | - */ |
|---|
| 154 | | - if (!init_pkru_value_snapshot && !read_pkru()) |
|---|
| 155 | | - return; |
|---|
| 156 | | - /* |
|---|
| 157 | 138 | * Override the PKRU state that came from 'init_fpstate' |
|---|
| 158 | 139 | * with the baseline from the process. |
|---|
| 159 | 140 | */ |
|---|
| .. | .. |
|---|
| 173 | 154 | static ssize_t init_pkru_write_file(struct file *file, |
|---|
| 174 | 155 | const char __user *user_buf, size_t count, loff_t *ppos) |
|---|
| 175 | 156 | { |
|---|
| 157 | + struct pkru_state *pk; |
|---|
| 176 | 158 | char buf[32]; |
|---|
| 177 | 159 | ssize_t len; |
|---|
| 178 | 160 | u32 new_init_pkru; |
|---|
| .. | .. |
|---|
| 195 | 177 | return -EINVAL; |
|---|
| 196 | 178 | |
|---|
| 197 | 179 | WRITE_ONCE(init_pkru_value, new_init_pkru); |
|---|
| 180 | + pk = get_xsave_addr(&init_fpstate.xsave, XFEATURE_PKRU); |
|---|
| 181 | + if (!pk) |
|---|
| 182 | + return -EINVAL; |
|---|
| 183 | + pk->pkru = new_init_pkru; |
|---|
| 198 | 184 | return count; |
|---|
| 199 | 185 | } |
|---|
| 200 | 186 | |
|---|