Skip to content

Commit 9d1f8be

Browse files
dhowellsJames Morris
authored andcommitted
bpf: Restrict bpf when kernel lockdown is in confidentiality mode
bpf_read() and bpf_read_str() could potentially be abused to (eg) allow private keys in kernel memory to be leaked. Disable them if the kernel has been locked down in confidentiality mode. Suggested-by: Alexei Starovoitov <[email protected]> Signed-off-by: Matthew Garrett <[email protected]> Reviewed-by: Kees Cook <[email protected]> cc: [email protected] cc: Chun-Yi Lee <[email protected]> cc: Alexei Starovoitov <[email protected]> Cc: Daniel Borkmann <[email protected]> Signed-off-by: James Morris <[email protected]>
1 parent a94549d commit 9d1f8be

File tree

3 files changed

+12
-0
lines changed

3 files changed

+12
-0
lines changed

include/linux/security.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ enum lockdown_reason {
118118
LOCKDOWN_INTEGRITY_MAX,
119119
LOCKDOWN_KCORE,
120120
LOCKDOWN_KPROBES,
121+
LOCKDOWN_BPF_READ,
121122
LOCKDOWN_CONFIDENTIALITY_MAX,
122123
};
123124

kernel/trace/bpf_trace.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,13 @@ BPF_CALL_3(bpf_probe_read, void *, dst, u32, size, const void *, unsafe_ptr)
139139
{
140140
int ret;
141141

142+
ret = security_locked_down(LOCKDOWN_BPF_READ);
143+
if (ret < 0)
144+
goto out;
145+
142146
ret = probe_kernel_read(dst, unsafe_ptr, size);
143147
if (unlikely(ret < 0))
148+
out:
144149
memset(dst, 0, size);
145150

146151
return ret;
@@ -566,6 +571,10 @@ BPF_CALL_3(bpf_probe_read_str, void *, dst, u32, size,
566571
{
567572
int ret;
568573

574+
ret = security_locked_down(LOCKDOWN_BPF_READ);
575+
if (ret < 0)
576+
goto out;
577+
569578
/*
570579
* The strncpy_from_unsafe() call will likely not fill the entire
571580
* buffer, but that's okay in this circumstance as we're probing
@@ -577,6 +586,7 @@ BPF_CALL_3(bpf_probe_read_str, void *, dst, u32, size,
577586
*/
578587
ret = strncpy_from_unsafe(dst, unsafe_ptr, size);
579588
if (unlikely(ret < 0))
589+
out:
580590
memset(dst, 0, size);
581591

582592
return ret;

security/lockdown/lockdown.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ static char *lockdown_reasons[LOCKDOWN_CONFIDENTIALITY_MAX+1] = {
3333
[LOCKDOWN_INTEGRITY_MAX] = "integrity",
3434
[LOCKDOWN_KCORE] = "/proc/kcore access",
3535
[LOCKDOWN_KPROBES] = "use of kprobes",
36+
[LOCKDOWN_BPF_READ] = "use of bpf to read kernel RAM",
3637
[LOCKDOWN_CONFIDENTIALITY_MAX] = "confidentiality",
3738
};
3839

0 commit comments

Comments
 (0)