Skip to contentSkip to conversation

memory corruption in X.25 facilities parsing

SECURITY Applied Dan Rosenberg 3 participants 5 messages Nov 2, 2010 – Nov 4, 2010

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

I put this together after a quick glance, so if someone knows this code better than I do (i.e. at all), feel free to comment or drop this patch if it's unnecessary.

A value of 0 will cause a memcpy() of ULONG_MAX size, destroying the kernel heap.

net/x25/x25_facilities.c +2 -2
@@ -134,14 +134,14 @@ int x25_parse_facilities(struct sk_buff 
 		case X25_FAC_CLASS_D:
 			switch (*p) {
 			case X25_FAC_CALLING_AE:
-				if (p[1] > X25_MAX_DTE_FACIL_LEN)
+				if (p[1] > X25_MAX_DTE_FACIL_LEN || p[1] == 0)
 					break;
 				dte_facs->calling_len = p[2];
 				memcpy(dte_facs->calling_ae, &p[3], p[1] - 1);
 				*vc_fac_mask |= X25_MASK_CALLING_AE;
 				break;
 			case X25_FAC_CALLED_AE:
-				if (p[1] > X25_MAX_DTE_FACIL_LEN)
+				if (p[1] > X25_MAX_DTE_FACIL_LEN || p[1] == 0)
 					break;
 				dte_facs->called_len = p[2];
 				memcpy(dte_facs->called_ae, &p[3], p[1] - 1);
#1 lore raw

There is an issue here, under select scenarios I can crash systems. However the patch doesn't resolve it fully, I think after breaking at that point the len and p pointers are messed up before it tries to parse the next facility.

Maybe it should return not break? It should reject/clear such calls. I'll start checking if the callers properly handle errors. Also should it be if (p[1] > X25_MAX_DTE_FACIL_LEN || p[1] <= 1), because it does the memcpy with p[1] -1

Quoted 31 lines · Dan Rosenberg

I put this together after a quick glance, so if someone knows this code better than I do (i.e. at all), feel free to comment or drop this patch if it's unnecessary.

A value of 0 will cause a memcpy() of ULONG_MAX size, destroying the kernel heap.

net/x25/x25_facilities.c   2010-11-02 10:36:02.827291324 -0400 renamed from net/x25/x25_facilities.c      2010-09-28 21:01:22.000000000 -0400 +0 -0
@@ -134,14 +134,14 @@ int x25_parse_facilities(struct sk_buff

               case X25_FAC_CLASS_D:
                       switch (*p) {
                       case X25_FAC_CALLING_AE:
-                               if (p[1] > X25_MAX_DTE_FACIL_LEN)
+                               if (p[1] > X25_MAX_DTE_FACIL_LEN || p[1] == 0) break; dte_facs->calling_len = p[2]; memcpy(dte_facs->calling_ae, &p[3], p[1] - 1); *vc_fac_mask |= X25_MASK_CALLING_AE; break;
                       case X25_FAC_CALLED_AE:
-                               if (p[1] > X25_MAX_DTE_FACIL_LEN)
+                               if (p[1] > X25_MAX_DTE_FACIL_LEN || p[1] == 0) break; dte_facs->called_len = p[2]; memcpy(dte_facs->called_ae, &p[3], p[1] - 1);

#2 lore raw
Quoted 35 lines · Andrew Hendry

There is an issue here, under select scenarios I can crash systems. However the patch doesn't resolve it fully, I think after breaking at that point the len and p pointers are messed up before it tries to parse the next facility.

Maybe it should return not break? It should reject/clear such calls. I'll start checking if the callers properly handle errors. Also should it be if (p[1] > X25_MAX_DTE_FACIL_LEN || p[1] <= 1), because it does the memcpy with p[1] -1

Quoted 23 lines · Dan Rosenberg

I put this together after a quick glance, so if someone knows this code better than I do (i.e. at all), feel free to comment or drop this patch if it's unnecessary.

A value of 0 will cause a memcpy() of ULONG_MAX size, destroying the kernel heap.

net/x25/x25_facilities.c 2010-11-02 10:36:02.827291324 -0400 renamed from net/x25/x25_facilities.c 2010-09-28 21:01:22.000000000 -0400 +1 -1
@@ -134,14 +134,14 @@ int x25_parse_facilities(struct sk_buff
               case X25_FAC_CLASS_D:
                       switch (*p) {
                       case X25_FAC_CALLING_AE:
-                               if (p[1] > X25_MAX_DTE_FACIL_LEN)
+                               if (p[1] > X25_MAX_DTE_FACIL_LEN || p[1] == 0)
                                       break;
                               dte_facs->calling_len = p[2];
                               memcpy(dte_facs->calling_ae, &p[3], p[1] - 1);
                               *vc_fac_mask |= X25_MASK_CALLING_AE;
                               break;
                       case X25_FAC_CALLED_AE:
Quoted 8 lines

net/x25/x25_facilities.c 2010-11-02 10:36:02.827291324 -0400 int x25_parse_facilities(struct sk_buff · line 147

Quoted 8 lines
net/x25/x25_facilities.c 2010-11-02 10:36:02.827291324 -0400 renamed from net/x25/x25_facilities.c 2010-09-28 21:01:22.000000000 -0400 +1 -1
-                               if (p[1] > X25_MAX_DTE_FACIL_LEN)
+                               if (p[1] > X25_MAX_DTE_FACIL_LEN || p[1] == 0)
                                       break;
                               dte_facs->called_len = p[2];
                               memcpy(dte_facs->called_ae, &p[3], p[1] - 1);

How does this look? It appears to fix it for the cases I could test.

net/x25/x25_facilities.c +4 -4
index 771bab0..3a8c4c4 100644
@@ -134,15 +134,15 @@ int x25_parse_facilities(struct sk_buff *skb, struct x25_facilities *facilities,
 		case X25_FAC_CLASS_D:
 			switch (*p) {
 			case X25_FAC_CALLING_AE:
-				if (p[1] > X25_MAX_DTE_FACIL_LEN)
-					break;
+				if (p[1] > X25_MAX_DTE_FACIL_LEN || p[1] <= 1)
+					return 0;
 				dte_facs->calling_len = p[2];
 				memcpy(dte_facs->calling_ae, &p[3], p[1] - 1);
 				*vc_fac_mask |= X25_MASK_CALLING_AE;
 				break;
 			case X25_FAC_CALLED_AE:
-				if (p[1] > X25_MAX_DTE_FACIL_LEN)
-					break;
+				if (p[1] > X25_MAX_DTE_FACIL_LEN || p[1] <= 1)
+					return 0;
 				dte_facs->called_len = p[2];
 				memcpy(dte_facs->called_ae, &p[3], p[1] - 1);
 				*vc_fac_mask |= X25_MASK_CALLED_AE;
net/x25/x25_in.c +2 -0
index 6317896..1d80e10 100644
@@ -119,6 +119,8 @@ static int x25_state1_machine(struct sock *sk, struct sk_buff *skb, int frametyp
 						&x25->vc_facil_mask);
 			if (len > 0)
 				skb_pull(skb, len);
+			else 
+				return -1;
 			/*
 			 *	Copy any Call User Data.
 			 */
#3 lore raw

Looks good to me. Thanks for the quick turnaround.

-Dan

Quoted 86 lines · Andrew Hendry
Quoted 35 lines · Andrew Hendry

There is an issue here, under select scenarios I can crash systems. However the patch doesn't resolve it fully, I think after breaking at that point the len and p pointers are messed up before it tries to parse the next facility.

Maybe it should return not break? It should reject/clear such calls. I'll start checking if the callers properly handle errors. Also should it be if (p[1] > X25_MAX_DTE_FACIL_LEN || p[1] <= 1), because it does the memcpy with p[1] -1

Quoted 23 lines · Dan Rosenberg

I put this together after a quick glance, so if someone knows this code better than I do (i.e. at all), feel free to comment or drop this patch if it's unnecessary.

A value of 0 will cause a memcpy() of ULONG_MAX size, destroying the kernel heap.

net/x25/x25_facilities.c 2010-11-02 10:36:02.827291324 -0400 renamed from net/x25/x25_facilities.c 2010-09-28 21:01:22.000000000 -0400 +1 -1
@@ -134,14 +134,14 @@ int x25_parse_facilities(struct sk_buff
               case X25_FAC_CLASS_D:
                       switch (*p) {
                       case X25_FAC_CALLING_AE:
-                               if (p[1] > X25_MAX_DTE_FACIL_LEN)
+                               if (p[1] > X25_MAX_DTE_FACIL_LEN || p[1] == 0)
                                       break;
                               dte_facs->calling_len = p[2];
                               memcpy(dte_facs->calling_ae, &p[3], p[1] - 1);
                               *vc_fac_mask |= X25_MASK_CALLING_AE;
                               break;
                       case X25_FAC_CALLED_AE:
Quoted 8 lines

net/x25/x25_facilities.c 2010-11-02 10:36:02.827291324 -0400 int x25_parse_facilities(struct sk_buff · line 147

Quoted 8 lines
net/x25/x25_facilities.c 2010-11-02 10:36:02.827291324 -0400 renamed from net/x25/x25_facilities.c 2010-09-28 21:01:22.000000000 -0400 +1 -1
-                               if (p[1] > X25_MAX_DTE_FACIL_LEN)
+                               if (p[1] > X25_MAX_DTE_FACIL_LEN || p[1] == 0)
                                       break;
                               dte_facs->called_len = p[2];
                               memcpy(dte_facs->called_ae, &p[3], p[1] - 1);

How does this look? It appears to fix it for the cases I could test.

net/x25/x25_facilities.c +4 -4
index 771bab0..3a8c4c4 100644
@@ -134,15 +134,15 @@ int x25_parse_facilities(struct sk_buff *skb, struct x25_facilities *facilities,
 		case X25_FAC_CLASS_D:
 			switch (*p) {
 			case X25_FAC_CALLING_AE:
-				if (p[1] > X25_MAX_DTE_FACIL_LEN)
-					break;
+				if (p[1] > X25_MAX_DTE_FACIL_LEN || p[1] <= 1)
+					return 0;
 				dte_facs->calling_len = p[2];
 				memcpy(dte_facs->calling_ae, &p[3], p[1] - 1);
 				*vc_fac_mask |= X25_MASK_CALLING_AE;
 				break;
 			case X25_FAC_CALLED_AE:
-				if (p[1] > X25_MAX_DTE_FACIL_LEN)
-					break;
+				if (p[1] > X25_MAX_DTE_FACIL_LEN || p[1] <= 1)
+					return 0;
 				dte_facs->called_len = p[2];
 				memcpy(dte_facs->called_ae, &p[3], p[1] - 1);
 				*vc_fac_mask |= X25_MASK_CALLED_AE;
net/x25/x25_in.c +2 -0
index 6317896..1d80e10 100644
@@ -119,6 +119,8 @@ static int x25_state1_machine(struct sock *sk, struct sk_buff *skb, int frametyp
 						&x25->vc_facil_mask);
 			if (len > 0)
 				skb_pull(skb, len);
+			else 
+				return -1;
 			/*
 			 *	Copy any Call User Data.
 			 */
maintainer Applied
#4 lore raw

From: Dan Rosenberg <[email protected]>
Date: Wed, 03 Nov 2010 19:44:31 -0400

Quoted 1 line

Looks good to me. Thanks for the quick turnaround.

Applied, thanks!