Skip to contentSkip to conversation

staging: rtl8712: memory corruption in wpa_set_encryption()

PATCH In review Dan Carpenter 2 participants 2 messages Jan 30, 2016 – Jan 31, 2016

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

PATCH 1/1 staging: rtl8712: memory corruption in wpa_set_encryption()

->KeyMaterial is declared as a 16 byte array, but we only ever allocate either 5 or 13 bytes of it. The problem is that we memset() all 16 bytes to zero so we're memsetting past the end of the allocated memory.

I fixed this in slightly lazy way, by just allocating 16 bytes. This works but there is a lot more cleanup you could do to this code if you wanted. Which is why this code is in staging.

drivers/staging/rtl8712/rtl871x_ioctl_linux.c +1 -4
index edfc680..db2e31bc 100644
@@ -398,12 +398,9 @@ static int wpa_set_encryption(struct net_device *dev, struct ieee_param *param,
 			wep_key_idx = 0;
 		if (wep_key_len > 0) {
 			wep_key_len = wep_key_len <= 5 ? 5 : 13;
-			pwep = kmalloc((u32)(wep_key_len +
-				FIELD_OFFSET(struct NDIS_802_11_WEP,
-				KeyMaterial)), GFP_ATOMIC);
+			pwep = kzalloc(sizeof(*pwep), GFP_ATOMIC);
 			if (pwep = NULL)
 				return -ENOMEM;
-			memset(pwep, 0, sizeof(struct NDIS_802_11_WEP));
 			pwep->KeyLength = wep_key_len;
 			pwep->Length = wep_key_len +
 				 FIELD_OFFSET(struct NDIS_802_11_WEP,
#1 lore raw
Quoted 7 lines · Dan Carpenter

->KeyMaterial is declared as a 16 byte array, but we only ever allocate either 5 or 13 bytes of it. The problem is that we memset() all 16 bytes to zero so we're memsetting past the end of the allocated memory.

I fixed this in slightly lazy way, by just allocating 16 bytes. This works but there is a lot more cleanup you could do to this code if you wanted. Which is why this code is in staging.

Better in every way than a crazy variable alloc if you ask me.

Quoted 8 lines
drivers/staging/rtl8712/rtl871x_ioctl_linux.c +0 -0
index edfc680..db2e31bc 100644

drivers/staging/rtl8712/rtl871x_ioctl_linux.c static int wpa_set_encryption(struct net_device *dev, struct ieee_param *param, · line 406

Quoted 10 lines
drivers/staging/rtl8712/rtl871x_ioctl_linux.c +1 -4
@@ -398,12 +398,9 @@ static int wpa_set_encryption(struct net_device *dev, struct ieee_param *param,
 			wep_key_idx = 0;
 		if (wep_key_len > 0) {
 			wep_key_len = wep_key_len <= 5 ? 5 : 13;
-			pwep = kmalloc((u32)(wep_key_len +
-				FIELD_OFFSET(struct NDIS_802_11_WEP,
-				KeyMaterial)), GFP_ATOMIC);
+			pwep = kzalloc(sizeof(*pwep), GFP_ATOMIC);
 			if (pwep = NULL)
 				return -ENOMEM;
-			memset(pwep, 0, sizeof(struct NDIS_802_11_WEP));

Should there be a newline after the "if" statement?

Quoted 3 lines
drivers/staging/rtl8712/rtl871x_ioctl_linux.c +0 -0
 			pwep->KeyLength = wep_key_len;
 			pwep->Length = wep_key_len +
 				 FIELD_OFFSET(struct NDIS_802_11_WEP,