Skip to contentSkip to conversation

wifi: cisco: do not assign -1 to unsigned char

PATCH In review Jason A. Donenfeld 3 participants 4 messages Oct 24, 2022 – Nov 1, 2022 Networking drivers (wireless)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 wifi: cisco: do not assign -1 to unsigned char

With char becoming unsigned by default, and with char alone being ambiguous and based on architecture, we get a warning when assigning the unchecked output of hex_to_bin() to that unsigned char. Mark key as a u8, which matches the struct's type, and then check each call to hex_to_bin() before casting.

---
 drivers/net/wireless/cisco/airo.c | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)
drivers/net/wireless/cisco/airo.c +14 -4
index 10daef81c355..fb2c35bd73bb 100644
@@ -5232,7 +5232,7 @@ static int get_wep_tx_idx(struct airo_info *ai)
 	return -1;
 }
 
-static int set_wep_key(struct airo_info *ai, u16 index, const char *key,
+static int set_wep_key(struct airo_info *ai, u16 index, const u8 *key,
 		       u16 keylen, int perm, int lock)
 {
 	static const unsigned char macaddr[ETH_ALEN] = { 0x01, 0, 0, 0, 0, 0 };
@@ -5283,7 +5283,7 @@ static void proc_wepkey_on_close(struct inode *inode, struct file *file)
 	struct net_device *dev = pde_data(inode);
 	struct airo_info *ai = dev->ml_priv;
 	int i, rc;
-	char key[16];
+	u8 key[16];
 	u16 index = 0;
 	int j = 0;
 
@@ -5311,12 +5311,22 @@ static void proc_wepkey_on_close(struct inode *inode, struct file *file)
 	}
 
 	for (i = 0; i < 16*3 && data->wbuffer[i+j]; i++) {
+		int val;
+
+		if (i % 3 == 2)
+			continue;
+
+		val = hex_to_bin(data->wbuffer[i+j]);
+		if (val < 0) {
+			airo_print_err(ai->dev->name, "WebKey passed invalid key hex");
+			return;
+		}
 		switch(i%3) {
 		case 0:
-			key[i/3] = hex_to_bin(data->wbuffer[i+j])<<4;
+			key[i/3] = (u8)val << 4;
 			break;
 		case 1:
-			key[i/3] |= hex_to_bin(data->wbuffer[i+j]);
+			key[i/3] |= (u8)val;
 			break;
 		}
 	}
Signature · 1 line
2.38.1
#1 lore raw

From: Jason A. Donenfeld

Quoted 52 lines

Sent: 24 October 2022 17:29

With char becoming unsigned by default, and with char alone being ambiguous and based on architecture, we get a warning when assigning the unchecked output of hex_to_bin() to that unsigned char. Mark key as a u8, which matches the struct's type, and then check each call to hex_to_bin() before casting.

---
 drivers/net/wireless/cisco/airo.c | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)
drivers/net/wireless/cisco/airo.c +12 -2
index 10daef81c355..fb2c35bd73bb 100644
@@ -5232,7 +5232,7 @@ static int get_wep_tx_idx(struct airo_info *ai)
 	return -1;
 }
 
-static int set_wep_key(struct airo_info *ai, u16 index, const char *key,
+static int set_wep_key(struct airo_info *ai, u16 index, const u8 *key,
 		       u16 keylen, int perm, int lock)
 {
 	static const unsigned char macaddr[ETH_ALEN] = { 0x01, 0, 0, 0, 0, 0 };
@@ -5283,7 +5283,7 @@ static void proc_wepkey_on_close(struct inode *inode, struct file *file)
 	struct net_device *dev = pde_data(inode);
 	struct airo_info *ai = dev->ml_priv;
 	int i, rc;
-	char key[16];
+	u8 key[16];
 	u16 index = 0;
 	int j = 0;
 
@@ -5311,12 +5311,22 @@ static void proc_wepkey_on_close(struct inode *inode, struct file *file)
 	}
 
 	for (i = 0; i < 16*3 && data->wbuffer[i+j]; i++) {
+		int val;
+
+		if (i % 3 == 2)
+			continue;
+
+		val = hex_to_bin(data->wbuffer[i+j]);
+		if (val < 0) {
+			airo_print_err(ai->dev->name, "WebKey passed invalid key hex");
+			return;
+		}
 		switch(i%3) {

drivers/net/wireless/cisco/airo.c static void proc_wepkey_on_close(struct inode *inode, struct file *file) · line 5332

Quoted 10 lines
drivers/net/wireless/cisco/airo.c +2 -2
 		case 0:
-			key[i/3] = hex_to_bin(data->wbuffer[i+j])<<4;
+			key[i/3] = (u8)val << 4;
 			break;
 		case 1:
-			key[i/3] |= hex_to_bin(data->wbuffer[i+j]);
+			key[i/3] |= (u8)val;
 			break;
 		}
 	}

That is about the crappiest loop I've seen.
I was just going to point out that the (u8) casts aren't needed. Something like:

	for (i = 0, buf = data->wbuffer + j; i < 16; i++, buf += 3) {
		int val;
		if (!buf[0] || !buf[1])
			break;
		val = hex_to_bin(buf[0]) | hex_to_bin(buf[1]) << 8;
		if (val < 0) {
			airo_print_err(ai->dev->name, "WebKey passed invalid key hex");
			return;
		}
		key[i] = val;
		if (!buf[2])
			break;
	}

Although there should be a check for buf[2] being valid. Any I worry about exactly what happens if there aren't 16 full bytes.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)

#2 lore raw
Quoted 81 lines · David Laight

From: Jason A. Donenfeld

Quoted 52 lines

Sent: 24 October 2022 17:29

With char becoming unsigned by default, and with char alone being ambiguous and based on architecture, we get a warning when assigning the unchecked output of hex_to_bin() to that unsigned char. Mark key as a u8, which matches the struct's type, and then check each call to hex_to_bin() before casting.

---
 drivers/net/wireless/cisco/airo.c | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)
drivers/net/wireless/cisco/airo.c +12 -2
index 10daef81c355..fb2c35bd73bb 100644
@@ -5232,7 +5232,7 @@ static int get_wep_tx_idx(struct airo_info *ai)
 	return -1;
 }
 
-static int set_wep_key(struct airo_info *ai, u16 index, const char *key,
+static int set_wep_key(struct airo_info *ai, u16 index, const u8 *key,
 		       u16 keylen, int perm, int lock)
 {
 	static const unsigned char macaddr[ETH_ALEN] = { 0x01, 0, 0, 0, 0, 0 };
@@ -5283,7 +5283,7 @@ static void proc_wepkey_on_close(struct inode *inode, struct file *file)
 	struct net_device *dev = pde_data(inode);
 	struct airo_info *ai = dev->ml_priv;
 	int i, rc;
-	char key[16];
+	u8 key[16];
 	u16 index = 0;
 	int j = 0;
 
@@ -5311,12 +5311,22 @@ static void proc_wepkey_on_close(struct inode *inode, struct file *file)
 	}
 
 	for (i = 0; i < 16*3 && data->wbuffer[i+j]; i++) {
+		int val;
+
+		if (i % 3 == 2)
+			continue;
+
+		val = hex_to_bin(data->wbuffer[i+j]);
+		if (val < 0) {
+			airo_print_err(ai->dev->name, "WebKey passed invalid key hex");
+			return;
+		}
 		switch(i%3) {

drivers/net/wireless/cisco/airo.c static void proc_wepkey_on_close(struct inode *inode, struct file *file) · line 5332

Quoted 10 lines
drivers/net/wireless/cisco/airo.c +2 -2
 		case 0:
-			key[i/3] = hex_to_bin(data->wbuffer[i+j])<<4;
+			key[i/3] = (u8)val << 4;
 			break;
 		case 1:
-			key[i/3] |= hex_to_bin(data->wbuffer[i+j]);
+			key[i/3] |= (u8)val;
 			break;
 		}
 	}

That is about the crappiest loop I've seen.
I was just going to point out that the (u8) casts aren't needed. Something like:

	for (i = 0, buf = data->wbuffer + j; i < 16; i++, buf += 3) {
		int val;
		if (!buf[0] || !buf[1])
			break;
		val = hex_to_bin(buf[0]) | hex_to_bin(buf[1]) << 8;
		if (val < 0) {
			airo_print_err(ai->dev->name, "WebKey passed invalid key hex");
			return;
		}
		key[i] = val;
		if (!buf[2])
			break;
	}
Quoted 2 lines

Although there should be a check for buf[2] being valid. Any I worry about exactly what happens if there aren't 16 full bytes.

buf[2] isn't checked. Presumably it's a space or something. Your <<8 also isn't right; this is a hex char. Anyway, I think I'd rather minimize this delta and leave this patch as-is.

Jason

#3 lore raw

Re: wifi: airo: do not assign -1 to unsigned char

In reply to Jason A. Donenfeld
Quoted 9 lines · Jason A. Donenfeld

With char becoming unsigned by default, and with char alone being ambiguous and based on architecture, we get a warning when assigning the unchecked output of hex_to_bin() to that unsigned char. Mark key as a u8, which matches the struct's type, and then check each call to hex_to_bin() before casting.

Patch applied to wireless.git, thanks.

e6cb8769452e wifi: airo: do not assign -1 to unsigned char

Signature · 3 lines
https://patchwork.kernel.org/project/linux-wireless/patch/[email protected]/

https://wireless.wiki.kernel.org/en/developers/documentation/submittingpatches