vsprintf: check real user/group id for %pK
[pandora-kernel.git] / lib / vsprintf.c
index d7222a9..ae02e42 100644 (file)
 #include <linux/kallsyms.h>
 #include <linux/uaccess.h>
 #include <linux/ioport.h>
+#include <linux/cred.h>
 #include <net/addrconf.h>
 
 #include <asm/page.h>          /* for PAGE_SIZE */
 #include <asm/div64.h>
 #include <asm/sections.h>      /* for dereference_function_descriptor() */
 
-static unsigned int simple_guess_base(const char *cp)
-{
-       if (cp[0] == '0') {
-               if (_tolower(cp[1]) == 'x' && isxdigit(cp[2]))
-                       return 16;
-               else
-                       return 8;
-       } else {
-               return 10;
-       }
-}
+#include "kstrtox.h"
 
 /**
  * simple_strtoull - convert a string to an unsigned long long
@@ -51,23 +42,14 @@ static unsigned int simple_guess_base(const char *cp)
  */
 unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
 {
-       unsigned long long result = 0;
-
-       if (!base)
-               base = simple_guess_base(cp);
+       unsigned long long result;
+       unsigned int rv;
 
-       if (base == 16 && cp[0] == '0' && _tolower(cp[1]) == 'x')
-               cp += 2;
+       cp = _parse_integer_fixup_radix(cp, &base);
+       rv = _parse_integer(cp, base, &result);
+       /* FIXME */
+       cp += (rv & ~KSTRTOX_OVERFLOW);
 
-       while (isxdigit(*cp)) {
-               unsigned int value;
-
-               value = isdigit(*cp) ? *cp - '0' : _tolower(*cp) - 'a' + 10;
-               if (value >= base)
-                       break;
-               result = result * base + value;
-               cp++;
-       }
        if (endp)
                *endp = (char *)cp;
 
@@ -566,7 +548,7 @@ char *mac_address_string(char *buf, char *end, u8 *addr,
        }
 
        for (i = 0; i < 6; i++) {
-               p = pack_hex_byte(p, addr[i]);
+               p = hex_byte_pack(p, addr[i]);
                if (fmt[0] == 'M' && i != 5)
                        *p++ = separator;
        }
@@ -686,13 +668,13 @@ char *ip6_compressed_string(char *p, const char *addr)
                lo = word & 0xff;
                if (hi) {
                        if (hi > 0x0f)
-                               p = pack_hex_byte(p, hi);
+                               p = hex_byte_pack(p, hi);
                        else
                                *p++ = hex_asc_lo(hi);
-                       p = pack_hex_byte(p, lo);
+                       p = hex_byte_pack(p, lo);
                }
                else if (lo > 0x0f)
-                       p = pack_hex_byte(p, lo);
+                       p = hex_byte_pack(p, lo);
                else
                        *p++ = hex_asc_lo(lo);
                needcolon = true;
@@ -714,8 +696,8 @@ char *ip6_string(char *p, const char *addr, const char *fmt)
        int i;
 
        for (i = 0; i < 8; i++) {
-               p = pack_hex_byte(p, *addr++);
-               p = pack_hex_byte(p, *addr++);
+               p = hex_byte_pack(p, *addr++);
+               p = hex_byte_pack(p, *addr++);
                if (fmt[0] == 'I' && i != 7)
                        *p++ = ':';
        }
@@ -773,7 +755,7 @@ char *uuid_string(char *buf, char *end, const u8 *addr,
        }
 
        for (i = 0; i < 16; i++) {
-               p = pack_hex_byte(p, addr[index[i]]);
+               p = hex_byte_pack(p, addr[index[i]]);
                switch (i) {
                case 3:
                case 5:
@@ -905,15 +887,41 @@ char *pointer(const char *fmt, char *buf, char *end, void *ptr,
                 * %pK cannot be used in IRQ context because its test
                 * for CAP_SYSLOG would be meaningless.
                 */
-               if (in_irq() || in_serving_softirq() || in_nmi()) {
+               if (kptr_restrict && (in_irq() || in_serving_softirq() ||
+                                     in_nmi())) {
                        if (spec.field_width == -1)
                                spec.field_width = 2 * sizeof(void *);
                        return string(buf, end, "pK-error", spec);
                }
-               if (!((kptr_restrict == 0) ||
-                     (kptr_restrict == 1 &&
-                      has_capability_noaudit(current, CAP_SYSLOG))))
+
+               switch (kptr_restrict) {
+               case 0:
+                       /* Always print %pK values */
+                       break;
+               case 1: {
+                       /*
+                        * Only print the real pointer value if the current
+                        * process has CAP_SYSLOG and is running with the
+                        * same credentials it started with. This is because
+                        * access to files is checked at open() time, but %pK
+                        * checks permission at read() time. We don't want to
+                        * leak pointer values if a binary opens a file using
+                        * %pK and then elevates privileges before reading it.
+                        */
+                       const struct cred *cred = current_cred();
+
+                       if (!has_capability_noaudit(current, CAP_SYSLOG) ||
+                           cred->euid != cred->uid ||
+                           cred->egid != cred->gid)
+                               ptr = NULL;
+                       break;
+               }
+               case 2:
+               default:
+                       /* Always print 0's for %pK */
                        ptr = NULL;
+                       break;
+               }
                break;
        }
        spec.flags |= SMALL;