aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorDan Rosenberg <drosenberg@vsecurity.com>2011-01-12 19:59:41 -0500
committerLinus Torvalds <torvalds@linux-foundation.org>2011-01-13 11:03:08 -0500
commit455cd5ab305c90ffc422dd2e0fb634730942b257 (patch)
treec20e6c3f8e58967991ce9002abe03d31897b171c /lib
parent351f8f8e6499ae4fff40f5e3a8fe16d9e1903646 (diff)
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>
Diffstat (limited to 'lib')
-rw-r--r--lib/vsprintf.c22
1 files changed, 22 insertions, 0 deletions
diff --git a/lib/vsprintf.c b/lib/vsprintf.c
index c150d3dafff..6ff38524ec1 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -936,6 +936,8 @@ char *uuid_string(char *buf, char *end, const u8 *addr,
936 return string(buf, end, uuid, spec); 936 return string(buf, end, uuid, spec);
937} 937}
938 938
939int kptr_restrict = 1;
940
939/* 941/*
940 * Show a '%p' thing. A kernel extension is that the '%p' is followed 942 * Show a '%p' thing. A kernel extension is that the '%p' is followed
941 * by an extra set of alphanumeric characters that are extended format 943 * 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,
979 * Implements a "recursive vsnprintf". 981 * Implements a "recursive vsnprintf".
980 * Do not use this feature without some mechanism to verify the 982 * Do not use this feature without some mechanism to verify the
981 * correctness of the format string and va_list arguments. 983 * correctness of the format string and va_list arguments.
984 * - 'K' For a kernel pointer that should be hidden from unprivileged users
982 * 985 *
983 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64 986 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
984 * function pointers are really function descriptors, which contain a 987 * function pointers are really function descriptors, which contain a
@@ -1035,6 +1038,25 @@ char *pointer(const char *fmt, char *buf, char *end, void *ptr,
1035 return buf + vsnprintf(buf, end - buf, 1038 return buf + vsnprintf(buf, end - buf,
1036 ((struct va_format *)ptr)->fmt, 1039 ((struct va_format *)ptr)->fmt,
1037 *(((struct va_format *)ptr)->va)); 1040 *(((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);
1038 } 1060 }
1039 spec.flags |= SMALL; 1061 spec.flags |= SMALL;
1040 if (spec.field_width == -1) { 1062 if (spec.field_width == -1) {