[PATCH] compat: fix possible out-of-bound accesses in compat_get_bitmap() and compat_put_bitmap()

From: Helge Deller
Date: Mon Jun 01 2015 - 14:44:52 EST


In functions compat_get_bitmap() and compat_put_bitmap() the variable
nr_compat_longs stores how many compat_ulong_t words should be copied in a loop.

The copy-loop itself is this:
if (nr_compat_longs-- > 0) {
if (__get_user(um, umask)) return -EFAULT;
} else {
um = 0;
}

Since nr_compat_longs gets unconditionally decremented in each loop, it's type
needs to be signed instead of unsigned to avoid possibly accessing userspace
memory behind the bitmap which shouldn't be accessed.

This bug seems to have been here for quite some time already, e.g. kernel
2.6.11 has the same coding. I didn't checked earlier versions.

Signed-off-by: Helge Deller <deller@xxxxxx>
To: Al Viro <viro@xxxxxxxxxxxxxxxxxx>
Cc: stable@xxxxxxxxxxxxxxx

diff --git a/kernel/compat.c b/kernel/compat.c
index 24f0061..bfbb312 100644
--- a/kernel/compat.c
+++ b/kernel/compat.c
@@ -893,7 +893,7 @@ long compat_get_bitmap(unsigned long *mask, const compat_ulong_t __user *umask,
int i, j;
unsigned long m;
compat_ulong_t um;
- unsigned long nr_compat_longs;
+ signed long nr_compat_longs;

/* align bitmap up to nearest compat_long_t boundary */
bitmap_size = ALIGN(bitmap_size, BITS_PER_COMPAT_LONG);
@@ -934,7 +934,7 @@ long compat_put_bitmap(compat_ulong_t __user *umask, unsigned long *mask,
int i, j;
unsigned long m;
compat_ulong_t um;
- unsigned long nr_compat_longs;
+ signed long nr_compat_longs;

/* align bitmap up to nearest compat_long_t boundary */
bitmap_size = ALIGN(bitmap_size, BITS_PER_COMPAT_LONG);
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/