32d5ef7
From a8bc90052f18348718412cebf7b569da95bad264 Mon Sep 17 00:00:00 2001
32d5ef7
From: Ben Hutchings <ben@decadent.org.uk>
32d5ef7
Date: Sun, 1 Nov 2015 16:22:53 +0000
32d5ef7
Subject: [PATCH 2/2] ppp, slip: Validate VJ compression slot parameters
32d5ef7
 completely
32d5ef7
MIME-Version: 1.0
32d5ef7
Content-Type: text/plain; charset=UTF-8
32d5ef7
Content-Transfer-Encoding: 8bit
32d5ef7
32d5ef7
Currently slhc_init() treats out-of-range values of rslots and tslots
32d5ef7
as equivalent to 0, except that if tslots is too large it will
32d5ef7
dereference a null pointer (CVE-2015-7799).
32d5ef7
32d5ef7
Add a range-check at the top of the function and make it return an
32d5ef7
ERR_PTR() on error instead of NULL.  Change the callers accordingly.
32d5ef7
32d5ef7
Compile-tested only.
32d5ef7
32d5ef7
Reported-by: 郭永刚 <guoyonggang@360.cn>
32d5ef7
References: http://article.gmane.org/gmane.comp.security.oss.general/17908
32d5ef7
Signed-off-by: Ben Hutchings <ben@decadent.org.uk>
32d5ef7
---
32d5ef7
 drivers/isdn/i4l/isdn_ppp.c   | 10 ++++------
32d5ef7
 drivers/net/ppp/ppp_generic.c |  6 ++----
32d5ef7
 drivers/net/slip/slhc.c       | 12 ++++++++----
32d5ef7
 drivers/net/slip/slip.c       |  2 +-
32d5ef7
 4 files changed, 15 insertions(+), 15 deletions(-)
32d5ef7
32d5ef7
diff --git a/drivers/isdn/i4l/isdn_ppp.c b/drivers/isdn/i4l/isdn_ppp.c
32d5ef7
index 86f9abebcb72..9c1e8adaf4fc 100644
32d5ef7
--- a/drivers/isdn/i4l/isdn_ppp.c
32d5ef7
+++ b/drivers/isdn/i4l/isdn_ppp.c
32d5ef7
@@ -322,9 +322,9 @@ isdn_ppp_open(int min, struct file *file)
32d5ef7
 	 * VJ header compression init
32d5ef7
 	 */
32d5ef7
 	is->slcomp = slhc_init(16, 16);	/* not necessary for 2. link in bundle */
32d5ef7
-	if (!is->slcomp) {
32d5ef7
+	if (IS_ERR(is->slcomp)) {
32d5ef7
 		isdn_ppp_ccp_reset_free(is);
32d5ef7
-		return -ENOMEM;
32d5ef7
+		return PTR_ERR(is->slcomp);
32d5ef7
 	}
32d5ef7
 #endif
32d5ef7
 #ifdef CONFIG_IPPP_FILTER
32d5ef7
@@ -573,10 +573,8 @@ isdn_ppp_ioctl(int min, struct file *file, unsigned int cmd, unsigned long arg)
32d5ef7
 			is->maxcid = val;
32d5ef7
 #ifdef CONFIG_ISDN_PPP_VJ
32d5ef7
 			sltmp = slhc_init(16, val);
32d5ef7
-			if (!sltmp) {
32d5ef7
-				printk(KERN_ERR "ippp, can't realloc slhc struct\n");
32d5ef7
-				return -ENOMEM;
32d5ef7
-			}
32d5ef7
+			if (IS_ERR(sltmp))
32d5ef7
+				return PTR_ERR(sltmp);
32d5ef7
 			if (is->slcomp)
32d5ef7
 				slhc_free(is->slcomp);
32d5ef7
 			is->slcomp = sltmp;
32d5ef7
diff --git a/drivers/net/ppp/ppp_generic.c b/drivers/net/ppp/ppp_generic.c
32d5ef7
index ed00446759b2..9a863c6a6a33 100644
32d5ef7
--- a/drivers/net/ppp/ppp_generic.c
32d5ef7
+++ b/drivers/net/ppp/ppp_generic.c
32d5ef7
@@ -721,10 +721,8 @@ static long ppp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
32d5ef7
 			val &= 0xffff;
32d5ef7
 		}
32d5ef7
 		vj = slhc_init(val2+1, val+1);
32d5ef7
-		if (!vj) {
32d5ef7
-			netdev_err(ppp->dev,
32d5ef7
-				   "PPP: no memory (VJ compressor)\n");
32d5ef7
-			err = -ENOMEM;
32d5ef7
+		if (IS_ERR(vj)) {
32d5ef7
+			err = PTR_ERR(vj);
32d5ef7
 			break;
32d5ef7
 		}
32d5ef7
 		ppp_lock(ppp);
32d5ef7
diff --git a/drivers/net/slip/slhc.c b/drivers/net/slip/slhc.c
32d5ef7
index 079f7adfcde5..27ed25252aac 100644
32d5ef7
--- a/drivers/net/slip/slhc.c
32d5ef7
+++ b/drivers/net/slip/slhc.c
32d5ef7
@@ -84,8 +84,9 @@ static long decode(unsigned char **cpp);
32d5ef7
 static unsigned char * put16(unsigned char *cp, unsigned short x);
32d5ef7
 static unsigned short pull16(unsigned char **cpp);
32d5ef7
 
32d5ef7
-/* Initialize compression data structure
32d5ef7
+/* Allocate compression data structure
32d5ef7
  *	slots must be in range 0 to 255 (zero meaning no compression)
32d5ef7
+ * Returns pointer to structure or ERR_PTR() on error.
32d5ef7
  */
32d5ef7
 struct slcompress *
32d5ef7
 slhc_init(int rslots, int tslots)
32d5ef7
@@ -94,11 +95,14 @@ slhc_init(int rslots, int tslots)
32d5ef7
 	register struct cstate *ts;
32d5ef7
 	struct slcompress *comp;
32d5ef7
 
32d5ef7
+	if (rslots < 0 || rslots > 255 || tslots < 0 || tslots > 255)
32d5ef7
+		return ERR_PTR(-EINVAL);
32d5ef7
+
32d5ef7
 	comp = kzalloc(sizeof(struct slcompress), GFP_KERNEL);
32d5ef7
 	if (! comp)
32d5ef7
 		goto out_fail;
32d5ef7
 
32d5ef7
-	if ( rslots > 0  &&  rslots < 256 ) {
32d5ef7
+	if (rslots > 0) {
32d5ef7
 		size_t rsize = rslots * sizeof(struct cstate);
32d5ef7
 		comp->rstate = kzalloc(rsize, GFP_KERNEL);
32d5ef7
 		if (! comp->rstate)
32d5ef7
@@ -106,7 +110,7 @@ slhc_init(int rslots, int tslots)
32d5ef7
 		comp->rslot_limit = rslots - 1;
32d5ef7
 	}
32d5ef7
 
32d5ef7
-	if ( tslots > 0  &&  tslots < 256 ) {
32d5ef7
+	if (tslots > 0) {
32d5ef7
 		size_t tsize = tslots * sizeof(struct cstate);
32d5ef7
 		comp->tstate = kzalloc(tsize, GFP_KERNEL);
32d5ef7
 		if (! comp->tstate)
32d5ef7
@@ -141,7 +145,7 @@ out_free2:
32d5ef7
 out_free:
32d5ef7
 	kfree(comp);
32d5ef7
 out_fail:
32d5ef7
-	return NULL;
32d5ef7
+	return ERR_PTR(-ENOMEM);
32d5ef7
 }
32d5ef7
 
32d5ef7
 
32d5ef7
diff --git a/drivers/net/slip/slip.c b/drivers/net/slip/slip.c
32d5ef7
index 05387b1e2e95..a17d86a57734 100644
32d5ef7
--- a/drivers/net/slip/slip.c
32d5ef7
+++ b/drivers/net/slip/slip.c
32d5ef7
@@ -164,7 +164,7 @@ static int sl_alloc_bufs(struct slip *sl, int mtu)
32d5ef7
 	if (cbuff == NULL)
32d5ef7
 		goto err_exit;
32d5ef7
 	slcomp = slhc_init(16, 16);
32d5ef7
-	if (slcomp == NULL)
32d5ef7
+	if (IS_ERR(slcomp))
32d5ef7
 		goto err_exit;
32d5ef7
 #endif
32d5ef7
 	spin_lock_bh(&sl->lock);
32d5ef7
-- 
32d5ef7
2.4.3
32d5ef7