[Devel] [PATCH RHEL7 COMMIT] ms/lib/idr.c: fix out-of-bounds pointer dereference

Konstantin Khorenko khorenko at virtuozzo.com
Thu Sep 3 08:27:31 PDT 2015


The commit is pushed to "branch-rh7-3.10.0-229.7.2-ovz" and will appear at https://src.openvz.org/scm/ovz/vzkernel.git
after rh7-3.10.0-229.7.2.vz7.6.6
------>
commit fd0dd7792b2302e1d811fee88208faebbdb8ffe2
Author: Andrey Ryabinin <aryabinin at odin.com>
Date:   Thu Sep 3 19:27:31 2015 +0400

    ms/lib/idr.c: fix out-of-bounds pointer dereference
    
    https://jira.sw.ru/browse/PSBM-26429
    
    From: Andrey Ryabinin <a.ryabinin at samsung.com>
    
    commit 93b7aca35dd7bf0c3ba7ea0542b556bcfdb28e76 upstream.
    
    I'm working on address sanitizer project for kernel.  Recently we
    started experiments with stack instrumentation, to detect out-of-bounds
    read/write bugs on stack.
    
    Just after booting I've hit out-of-bounds read on stack in idr_for_each
    (and in __idr_remove_all as well):
    
    	struct idr_layer **paa = &pa[0];
    
    	while (id >= 0 && id <= max) {
    		...
    		while (n < fls(id)) {
    			n += IDR_BITS;
    			p = *--paa; <--- here we are reading pa[-1] value.
    		}
    	}
    
    Despite the fact that after this dereference we are exiting out of loop
    and never use p, such behaviour is undefined and should be avoided.
    
    Fix this by moving pointer derference to the beggining of the loop,
    right before we will use it.
    
    Signed-off-by: Andrey Ryabinin <a.ryabinin at samsung.com>
    Reviewed-by: Lai Jiangshan <laijs at cn.fujitsu.com>
    Cc: Tejun Heo <tj at kernel.org>
    Cc: Alexey Preobrazhensky <preobr at google.com>
    Cc: Dmitry Vyukov <dvyukov at google.com>
    Cc: Konstantin Khlebnikov <koct9i at gmail.com>
    Signed-off-by: Andrew Morton <akpm at linux-foundation.org>
    Signed-off-by: Linus Torvalds <torvalds at linux-foundation.org>
    Signed-off-by: Andrey Ryabinin <aryabinin at odin.com>
    
    Signed-off-by: Andrey Ryabinin <aryabinin at odin.com>
---
 lib/idr.c | 25 ++++++++++++++-----------
 1 file changed, 14 insertions(+), 11 deletions(-)

diff --git a/lib/idr.c b/lib/idr.c
index 674c30b..0859b3a 100644
--- a/lib/idr.c
+++ b/lib/idr.c
@@ -616,26 +616,27 @@ void __idr_remove_all(struct idr *idp)
 	struct idr_layer **paa = &pa[0];
 
 	n = idp->layers * IDR_BITS;
-	p = idp->top;
+	*paa = idp->top;
 	rcu_assign_pointer(idp->top, NULL);
 	max = idr_max(idp->layers);
 
 	id = 0;
 	while (id >= 0 && id <= max) {
+		p = *paa;
 		while (n > IDR_BITS && p) {
 			n -= IDR_BITS;
-			*paa++ = p;
 			p = p->ary[(id >> n) & IDR_MASK];
+			*++paa = p;
 		}
 
 		bt_mask = id;
 		id += 1 << n;
 		/* Get the highest bit that the above add changed from 0->1. */
 		while (n < fls(id ^ bt_mask)) {
-			if (p)
-				free_layer(idp, p);
+			if (*paa)
+				free_layer(idp, *paa);
 			n += IDR_BITS;
-			p = *--paa;
+			--paa;
 		}
 	}
 	idp->layers = 0;
@@ -719,15 +720,16 @@ int idr_for_each(struct idr *idp,
 	struct idr_layer **paa = &pa[0];
 
 	n = idp->layers * IDR_BITS;
-	p = rcu_dereference_raw(idp->top);
+	*paa = rcu_dereference_raw(idp->top);
 	max = idr_max(idp->layers);
 
 	id = 0;
 	while (id >= 0 && id <= max) {
+		p = *paa;
 		while (n > 0 && p) {
 			n -= IDR_BITS;
-			*paa++ = p;
 			p = rcu_dereference_raw(p->ary[(id >> n) & IDR_MASK]);
+			*++paa = p;
 		}
 
 		if (p) {
@@ -739,7 +741,7 @@ int idr_for_each(struct idr *idp,
 		id += 1 << n;
 		while (n < fls(id)) {
 			n += IDR_BITS;
-			p = *--paa;
+			--paa;
 		}
 	}
 
@@ -767,17 +769,18 @@ void *idr_get_next(struct idr *idp, int *nextidp)
 	int n, max;
 
 	/* find first ent */
-	p = rcu_dereference_raw(idp->top);
+	p = *paa = rcu_dereference_raw(idp->top);
 	if (!p)
 		return NULL;
 	n = (p->layer + 1) * IDR_BITS;
 	max = idr_max(p->layer + 1);
 
 	while (id >= 0 && id <= max) {
+		p = *paa;
 		while (n > 0 && p) {
 			n -= IDR_BITS;
-			*paa++ = p;
 			p = rcu_dereference_raw(p->ary[(id >> n) & IDR_MASK]);
+			*++paa = p;
 		}
 
 		if (p) {
@@ -795,7 +798,7 @@ void *idr_get_next(struct idr *idp, int *nextidp)
 		id = round_up(id + 1, 1 << n);
 		while (n < fls(id)) {
 			n += IDR_BITS;
-			p = *--paa;
+			--paa;
 		}
 	}
 	return NULL;



More information about the Devel mailing list