Skip to content

Commit 640ab21

Browse files
KAGA-KOKOkonradwilk
authored andcommitted
prctl: Add speculation control prctls
Add two new prctls to control aspects of speculation related vulnerabilites and their mitigations to provide finer grained control over performance impacting mitigations. PR_GET_SPECULATION_CTRL returns the state of the speculation misfeature which is selected with arg2 of prctl(2). The return value uses bit 0-2 with the following meaning: Bit Define Description 0 PR_SPEC_PRCTL Mitigation can be controlled per task by PR_SET_SPECULATION_CTRL 1 PR_SPEC_ENABLE The speculation feature is enabled, mitigation is disabled 2 PR_SPEC_DISABLE The speculation feature is disabled, mitigation is enabled If all bits are 0 the CPU is not affected by the speculation misfeature. If PR_SPEC_PRCTL is set, then the per task control of the mitigation is available. If not set, prctl(PR_SET_SPECULATION_CTRL) for the speculation misfeature will fail. PR_SET_SPECULATION_CTRL allows to control the speculation misfeature, which is selected by arg2 of prctl(2) per task. arg3 is used to hand in the control value, i.e. either PR_SPEC_ENABLE or PR_SPEC_DISABLE. The common return values are: EINVAL prctl is not implemented by the architecture or the unused prctl() arguments are not 0 ENODEV arg2 is selecting a not supported speculation misfeature PR_SET_SPECULATION_CTRL has these additional return values: ERANGE arg3 is incorrect, i.e. it's not either PR_SPEC_ENABLE or PR_SPEC_DISABLE ENXIO prctl control of the selected speculation misfeature is disabled The first supported controlable speculation misfeature is PR_SPEC_STORE_BYPASS. Add the define so this can be shared between architectures. Based on an initial patch from Tim Chen and mostly rewritten. Signed-off-by: Thomas Gleixner <[email protected]> Reviewed-by: Ingo Molnar <[email protected]> Reviewed-by: Konrad Rzeszutek Wilk <[email protected]> (cherry picked from commit b617cfc) Orabug: 28034177 CVE: CVE-2018-3639 Signed-off-by: Konrad Rzeszutek Wilk <[email protected]> Tested-by: Mihai Carabas <[email protected]> Reviewed-by: Mihai Carabas <[email protected]> Reviewed-by: John Haxby <[email protected]>
1 parent 06696d2 commit 640ab21

File tree

5 files changed

+125
-0
lines changed

5 files changed

+125
-0
lines changed

Documentation/userspace-api/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ place where this information is gathered.
1919
no_new_privs
2020
seccomp_filter
2121
unshare
22+
spec_ctrl
2223

2324
.. only:: subproject and html
2425

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
===================
2+
Speculation Control
3+
===================
4+
5+
Quite some CPUs have speculation related misfeatures which are in fact
6+
vulnerabilites causing data leaks in various forms even accross privilege
7+
domains.
8+
9+
The kernel provides mitigation for such vulnerabilities in various
10+
forms. Some of these mitigations are compile time configurable and some on
11+
the kernel command line.
12+
13+
There is also a class of mitigations which are very expensive, but they can
14+
be restricted to a certain set of processes or tasks in controlled
15+
environments. The mechanism to control these mitigations is via
16+
:manpage:`prctl(2)`.
17+
18+
There are two prctl options which are related to this:
19+
20+
* PR_GET_SPECULATION_CTRL
21+
22+
* PR_SET_SPECULATION_CTRL
23+
24+
PR_GET_SPECULATION_CTRL
25+
-----------------------
26+
27+
PR_GET_SPECULATION_CTRL returns the state of the speculation misfeature
28+
which is selected with arg2 of prctl(2). The return value uses bits 0-2 with
29+
the following meaning:
30+
31+
==== ================ ===================================================
32+
Bit Define Description
33+
==== ================ ===================================================
34+
0 PR_SPEC_PRCTL Mitigation can be controlled per task by
35+
PR_SET_SPECULATION_CTRL
36+
1 PR_SPEC_ENABLE The speculation feature is enabled, mitigation is
37+
disabled
38+
2 PR_SPEC_DISABLE The speculation feature is disabled, mitigation is
39+
enabled
40+
==== ================ ===================================================
41+
42+
If all bits are 0 the CPU is not affected by the speculation misfeature.
43+
44+
If PR_SPEC_PRCTL is set, then the per task control of the mitigation is
45+
available. If not set, prctl(PR_SET_SPECULATION_CTRL) for the speculation
46+
misfeature will fail.
47+
48+
PR_SET_SPECULATION_CTRL
49+
-----------------------
50+
PR_SET_SPECULATION_CTRL allows to control the speculation misfeature, which
51+
is selected by arg2 of :manpage:`prctl(2)` per task. arg3 is used to hand
52+
in the control value, i.e. either PR_SPEC_ENABLE or PR_SPEC_DISABLE.
53+
54+
Common error codes
55+
------------------
56+
======= =================================================================
57+
Value Meaning
58+
======= =================================================================
59+
EINVAL The prctl is not implemented by the architecture or unused
60+
prctl(2) arguments are not 0
61+
62+
ENODEV arg2 is selecting a not supported speculation misfeature
63+
======= =================================================================
64+
65+
PR_SET_SPECULATION_CTRL error codes
66+
-----------------------------------
67+
======= =================================================================
68+
Value Meaning
69+
======= =================================================================
70+
0 Success
71+
72+
ERANGE arg3 is incorrect, i.e. it's neither PR_SPEC_ENABLE nor
73+
PR_SPEC_DISABLE
74+
75+
ENXIO Control of the selected speculation misfeature is not possible.
76+
See PR_GET_SPECULATION_CTRL.
77+
======= =================================================================
78+
79+
Speculation misfeature controls
80+
-------------------------------
81+
- PR_SPEC_STORE_BYPASS: Speculative Store Bypass
82+
83+
Invocations:
84+
* prctl(PR_GET_SPECULATION_CTRL, PR_SPEC_STORE_BYPASS, 0, 0, 0);
85+
* prctl(PR_SET_SPECULATION_CTRL, PR_SPEC_STORE_BYPASS, PR_SPEC_ENABLE, 0, 0);
86+
* prctl(PR_SET_SPECULATION_CTRL, PR_SPEC_STORE_BYPASS, PR_SPEC_DISABLE, 0, 0);

include/linux/nospec.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,9 @@ static inline unsigned long array_index_mask_nospec(unsigned long index,
5555
\
5656
(typeof(_i)) (_i & _mask); \
5757
})
58+
59+
/* Speculation control prctl */
60+
int arch_prctl_spec_ctrl_get(unsigned long which);
61+
int arch_prctl_spec_ctrl_set(unsigned long which, unsigned long ctrl);
62+
5863
#endif /* _LINUX_NOSPEC_H */

include/uapi/linux/prctl.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,4 +207,15 @@ struct prctl_mm_map {
207207
# define PR_SVE_VL_LEN_MASK 0xffff
208208
# define PR_SVE_VL_INHERIT (1 << 17) /* inherit across exec */
209209

210+
/* Per task speculation control */
211+
#define PR_GET_SPECULATION_CTRL 52
212+
#define PR_SET_SPECULATION_CTRL 53
213+
/* Speculation control variants */
214+
# define PR_SPEC_STORE_BYPASS 0
215+
/* Return and control values for PR_SET/GET_SPECULATION_CTRL */
216+
# define PR_SPEC_NOT_AFFECTED 0
217+
# define PR_SPEC_PRCTL (1UL << 0)
218+
# define PR_SPEC_ENABLE (1UL << 1)
219+
# define PR_SPEC_DISABLE (1UL << 2)
220+
210221
#endif /* _LINUX_PRCTL_H */

kernel/sys.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@
6161
#include <linux/uidgid.h>
6262
#include <linux/cred.h>
6363

64+
#include <linux/nospec.h>
65+
6466
#include <linux/kmsg_dump.h>
6567
/* Move somewhere else to avoid recompiling? */
6668
#include <generated/utsrelease.h>
@@ -2190,6 +2192,16 @@ static int propagate_has_child_subreaper(struct task_struct *p, void *data)
21902192
return 1;
21912193
}
21922194

2195+
int __weak arch_prctl_spec_ctrl_get(unsigned long which)
2196+
{
2197+
return -EINVAL;
2198+
}
2199+
2200+
int __weak arch_prctl_spec_ctrl_set(unsigned long which, unsigned long ctrl)
2201+
{
2202+
return -EINVAL;
2203+
}
2204+
21932205
SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
21942206
unsigned long, arg4, unsigned long, arg5)
21952207
{
@@ -2398,6 +2410,16 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
23982410
case PR_SVE_GET_VL:
23992411
error = SVE_GET_VL();
24002412
break;
2413+
case PR_GET_SPECULATION_CTRL:
2414+
if (arg3 || arg4 || arg5)
2415+
return -EINVAL;
2416+
error = arch_prctl_spec_ctrl_get(arg2);
2417+
break;
2418+
case PR_SET_SPECULATION_CTRL:
2419+
if (arg4 || arg5)
2420+
return -EINVAL;
2421+
error = arch_prctl_spec_ctrl_set(arg2, arg3);
2422+
break;
24012423
default:
24022424
error = -EINVAL;
24032425
break;

0 commit comments

Comments
 (0)