Skip to content

Commit 455cd5a

Browse files
Dan Rosenbergtorvalds
authored andcommitted
kptr_restrict for hiding kernel pointers from unprivileged users
Add the %pK printk format specifier and the /proc/sys/kernel/kptr_restrict sysctl. The %pK format specifier is designed to hide exposed kernel pointers, specifically via /proc interfaces. Exposing these pointers provides an easy target for kernel write vulnerabilities, since they reveal the locations of writable structures containing easily triggerable function pointers. The behavior of %pK depends on the kptr_restrict sysctl. If kptr_restrict is set to 0, no deviation from the standard %p behavior occurs. If kptr_restrict is set to 1, the default, if the current user (intended to be a reader via seq_printf(), etc.) does not have CAP_SYSLOG (currently in the LSM tree), kernel pointers using %pK are printed as 0's. If kptr_restrict is set to 2, kernel pointers using %pK are printed as 0's regardless of privileges. Replacing with 0's was chosen over the default "(null)", which cannot be parsed by userland %p, which expects "(nil)". [akpm@linux-foundation.org: check for IRQ context when !kptr_restrict, save an indent level, s/WARN/WARN_ONCE/] [akpm@linux-foundation.org: coding-style fixup] [randy.dunlap@oracle.com: fix kernel/sysctl.c warning] Signed-off-by: Dan Rosenberg <drosenberg@vsecurity.com> Signed-off-by: Randy Dunlap <randy.dunlap@oracle.com> Cc: James Morris <jmorris@namei.org> Cc: Eric Dumazet <eric.dumazet@gmail.com> Cc: Thomas Graf <tgraf@infradead.org> Cc: Eugene Teo <eugeneteo@kernel.org> Cc: Kees Cook <kees.cook@canonical.com> Cc: Ingo Molnar <mingo@elte.hu> Cc: David S. Miller <davem@davemloft.net> Cc: Peter Zijlstra <a.p.zijlstra@chello.nl> Cc: Eric Paris <eparis@parisplace.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 351f8f8 commit 455cd5a

4 files changed

Lines changed: 47 additions & 0 deletions

File tree

‎Documentation/sysctl/kernel.txt‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ show up in /proc/sys/kernel:
3434
- hotplug
3535
- java-appletviewer [ binfmt_java, obsolete ]
3636
- java-interpreter [ binfmt_java, obsolete ]
37+
- kptr_restrict
3738
- kstack_depth_to_print [ X86 only ]
3839
- l2cr [ PPC only ]
3940
- modprobe ==> Documentation/debugging-modules.txt
@@ -261,6 +262,19 @@ This flag controls the L2 cache of G3 processor boards. If
261262

262263
==============================================================
263264

265+
kptr_restrict:
266+
267+
This toggle indicates whether restrictions are placed on
268+
exposing kernel addresses via /proc and other interfaces. When
269+
kptr_restrict is set to (0), there are no restrictions. When
270+
kptr_restrict is set to (1), the default, kernel pointers
271+
printed using the %pK format specifier will be replaced with 0's
272+
unless the user has CAP_SYSLOG. When kptr_restrict is set to
273+
(2), kernel pointers printed using %pK will be replaced with 0's
274+
regardless of privileges.
275+
276+
==============================================================
277+
264278
kstack_depth_to_print: (X86 only)
265279

266280
Controls the number of words to print when dumping the raw

‎include/linux/printk.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ extern bool printk_timed_ratelimit(unsigned long *caller_jiffies,
8383

8484
extern int printk_delay_msec;
8585
extern int dmesg_restrict;
86+
extern int kptr_restrict;
8687

8788
/*
8889
* Print a one-time message (analogous to WARN_ONCE() et al):

‎kernel/sysctl.c‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <linux/slab.h>
2525
#include <linux/sysctl.h>
2626
#include <linux/signal.h>
27+
#include <linux/printk.h>
2728
#include <linux/proc_fs.h>
2829
#include <linux/security.h>
2930
#include <linux/ctype.h>
@@ -710,6 +711,15 @@ static struct ctl_table kern_table[] = {
710711
.extra1 = &zero,
711712
.extra2 = &one,
712713
},
714+
{
715+
.procname = "kptr_restrict",
716+
.data = &kptr_restrict,
717+
.maxlen = sizeof(int),
718+
.mode = 0644,
719+
.proc_handler = proc_dointvec_minmax,
720+
.extra1 = &zero,
721+
.extra2 = &two,
722+
},
713723
#endif
714724
{
715725
.procname = "ngroups_max",

‎lib/vsprintf.c‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -936,6 +936,8 @@ char *uuid_string(char *buf, char *end, const u8 *addr,
936936
return string(buf, end, uuid, spec);
937937
}
938938

939+
int kptr_restrict = 1;
940+
939941
/*
940942
* Show a '%p' thing. A kernel extension is that the '%p' is followed
941943
* by an extra set of alphanumeric characters that are extended format
@@ -979,6 +981,7 @@ char *uuid_string(char *buf, char *end, const u8 *addr,
979981
* Implements a "recursive vsnprintf".
980982
* Do not use this feature without some mechanism to verify the
981983
* correctness of the format string and va_list arguments.
984+
* - 'K' For a kernel pointer that should be hidden from unprivileged users
982985
*
983986
* Note: The difference between 'S' and 'F' is that on ia64 and ppc64
984987
* function pointers are really function descriptors, which contain a
@@ -1035,6 +1038,25 @@ char *pointer(const char *fmt, char *buf, char *end, void *ptr,
10351038
return buf + vsnprintf(buf, end - buf,
10361039
((struct va_format *)ptr)->fmt,
10371040
*(((struct va_format *)ptr)->va));
1041+
case 'K':
1042+
/*
1043+
* %pK cannot be used in IRQ context because its test
1044+
* for CAP_SYSLOG would be meaningless.
1045+
*/
1046+
if (in_irq() || in_serving_softirq() || in_nmi()) {
1047+
if (spec.field_width == -1)
1048+
spec.field_width = 2 * sizeof(void *);
1049+
return string(buf, end, "pK-error", spec);
1050+
} else if ((kptr_restrict == 0) ||
1051+
(kptr_restrict == 1 &&
1052+
has_capability_noaudit(current, CAP_SYSLOG)))
1053+
break;
1054+
1055+
if (spec.field_width == -1) {
1056+
spec.field_width = 2 * sizeof(void *);
1057+
spec.flags |= ZEROPAD;
1058+
}
1059+
return number(buf, end, 0, spec);
10381060
}
10391061
spec.flags |= SMALL;
10401062
if (spec.field_width == -1) {

0 commit comments

Comments
 (0)