Re: [PATCH] 3c505: do not set pcb->data.raw beyond its size

Previous thread: Re: mdio-gpio: Add mdc pin direction initialization by David Miller on Tuesday, February 10, 2009 - 6:11 pm. (1 message)

Next thread: [PATCH] Update jhash.h with the new version of Jenkins' hash by Jozsef Kadlecsik on Wednesday, February 11, 2009 - 3:19 am. (14 messages)
From: Roel Kluin
Date: Wednesday, February 11, 2009 - 3:01 am

Better?

-------------------->8----------------8<-----------------------
With while (x++ < n) { ... } x can reach n+1.

Signed-off-by: Roel Kluin <roel.kluin@gmail.com>
---
 drivers/net/3c505.c        |    4 ++--
 drivers/net/irda/mcs7780.c |    2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/net/3c505.c b/drivers/net/3c505.c
index 6124605..fb74846 100644
--- a/drivers/net/3c505.c
+++ b/drivers/net/3c505.c
@@ -500,9 +500,9 @@ static bool receive_pcb(struct net_device *dev, pcb_struct * pcb)
 		pcb->data.raw[i++] = inb_command(dev->base_addr);
 		if (i > MAX_PCB_DATA)
 			INVALID_PCB_MSG(i);
-	} while ((stat & ASF_PCB_MASK) != ASF_PCB_END && j < 20000);
+	} while ((stat & ASF_PCB_MASK) != ASF_PCB_END && j <= 20000);
 	spin_unlock_irqrestore(&adapter->lock, flags);
-	if (j >= 20000) {
+	if (j > 20000) {
 		TIMEOUT_MSG(__LINE__);
 		return false;
 	}
diff --git a/drivers/net/irda/mcs7780.c b/drivers/net/irda/mcs7780.c
index 7eafdca..b4f4f19 100644
--- a/drivers/net/irda/mcs7780.c
+++ b/drivers/net/irda/mcs7780.c
@@ -585,7 +585,7 @@ static int mcs_speed_change(struct mcs_cb *mcs)
 		mcs_get_reg(mcs, MCS_RESV_REG, &rval);
 	} while(cnt++ < 100 && (rval & MCS_IRINTX));
 
-	if(cnt >= 100) {
+	if(cnt > 100) {
 		IRDA_ERROR("unable to change speed\n");
 		ret = -EIO;
 		goto error;
--

From: Jarek Poplawski
Date: Wednesday, February 11, 2009 - 6:33 am

i is also misused here and array can be overriden, so additional
break/return is needed.

Thanks,
--

From: Roel Kluin
Date: Wednesday, February 11, 2009 - 7:22 am

Thanks, is this how it should be?
-------------------->8----------------8<-----------------------

With while (x++ < n) { ... } x can reach n+1. As Jarek Poplawski pointed out, array
pcb->data.raw was not correctly used.

Signed-off-by: Roel Kluin <roel.kluin@gmail.com>
---
diff --git a/drivers/net/3c505.c b/drivers/net/3c505.c
index 6124605..4cf3050 100644
--- a/drivers/net/3c505.c
+++ b/drivers/net/3c505.c
@@ -497,12 +497,15 @@ static bool receive_pcb(struct net_device *dev, pcb_struct * pcb)
 	do {
 		j = 0;
 		while (((stat = get_status(dev->base_addr)) & ACRF) == 0 && j++ < 20000);
-		pcb->data.raw[i++] = inb_command(dev->base_addr);
-		if (i > MAX_PCB_DATA)
-			INVALID_PCB_MSG(i);
-	} while ((stat & ASF_PCB_MASK) != ASF_PCB_END && j < 20000);
+		pcb->data.raw[i] = inb_command(dev->base_addr);
+	} while (++i < MAX_PCB_DATA && (stat & ASF_PCB_MASK) != ASF_PCB_END && j <= 20000);
+
 	spin_unlock_irqrestore(&adapter->lock, flags);
-	if (j >= 20000) {
+	if (i >= MAX_PCB_DATA) {
+		INVALID_PCB_MSG(i);
+		return false;
+	}
+	if (j > 20000) {
 		TIMEOUT_MSG(__LINE__);
 		return false;
 	}
--

From: Jarek Poplawski
Date: Wednesday, February 11, 2009 - 10:14 am

It looks (almost) OK to me. :-) Except this > 80 line.

On the other hand, I wonder if it's not a good time to make it more
readable; I mean the first while (): length, ";", and maybe ++j
similarly to i now?

BTW, I hope you remember about irda.

--

From: Roel Kluin
Date: Wednesday, February 11, 2009 - 12:33 pm

Thanks again for reviewing, how's this? with irda this time.
-------------------->8----------------8<-----------------------

With while (x++ < n) { ... } x can reach n+1. As Jarek Poplawski pointed out, array
pcb->data.raw was not correctly used.

Signed-off-by: Roel Kluin <roel.kluin@gmail.com>
---
diff --git a/drivers/net/3c505.c b/drivers/net/3c505.c
index 6124605..4ade64a 100644
--- a/drivers/net/3c505.c
+++ b/drivers/net/3c505.c
@@ -493,15 +493,21 @@ static bool receive_pcb(struct net_device *dev, pcb_struct * pcb)
 	}
 	/* read the data */
 	spin_lock_irqsave(&adapter->lock, flags);
-	i = 0;
-	do {
-		j = 0;
-		while (((stat = get_status(dev->base_addr)) & ACRF) == 0 && j++ < 20000);
-		pcb->data.raw[i++] = inb_command(dev->base_addr);
-		if (i > MAX_PCB_DATA)
-			INVALID_PCB_MSG(i);
-	} while ((stat & ASF_PCB_MASK) != ASF_PCB_END && j < 20000);
+	for (i = 0; i < MAX_PCB_DATA; i++) {
+		for (j = 0; j < 20000; j++) {
+			stat = get_status(dev->base_addr);
+			if (stat & ACRF)
+				break;
+		}
+		pcb->data.raw[i] = inb_command(dev->base_addr);
+		if (stat & ASF_PCB_MASK == ASF_PCB_END || j >= 20000)
+			break;
+	}
 	spin_unlock_irqrestore(&adapter->lock, flags);
+	if (i >= MAX_PCB_DATA) {
+		INVALID_PCB_MSG(i);
+		return false;
+	}
 	if (j >= 20000) {
 		TIMEOUT_MSG(__LINE__);
 		return false;
diff --git a/drivers/net/irda/mcs7780.c b/drivers/net/irda/mcs7780.c
index 7eafdca..85e88da 100644
--- a/drivers/net/irda/mcs7780.c
+++ b/drivers/net/irda/mcs7780.c
@@ -585,7 +585,7 @@ static int mcs_speed_change(struct mcs_cb *mcs)
 		mcs_get_reg(mcs, MCS_RESV_REG, &rval);
 	} while(cnt++ < 100 && (rval & MCS_IRINTX));
 
-	if(cnt >= 100) {
+	if (cnt > 100) {
 		IRDA_ERROR("unable to change speed\n");
 		ret = -EIO;
 		goto error;

--

From: Jarek Poplawski
Date: Wednesday, February 11, 2009 - 1:27 pm

Hmm... except one bug: "stat & ..." needs "()"...

Roel, please resend once more, but separated: something could go to
stable, but not necessarily together. And add more description,
especially to this 3c505 (here is more changes than 'while (x++ < n)').

Anyway, I hope David will like these "off by one" patches again,
especially after this, and sun3lance one. Good catches!

Cheers,
--

From: Jarek Poplawski
Date: Wednesday, February 11, 2009 - 1:58 pm

...


Here is no ++i after breaking the loop, so no need for [--i] later;
please, check this more.

--

From: Roel Kluin
Date: Wednesday, February 11, 2009 - 3:55 pm

Many thanks, Jarek,

Is this changelog ok?
------------------------------>8----------------8<------------------------------

Ensure that we do not set pcb->data.raw beyond its size, print an error message
and return false if we attempt to. A timout message was printed one too early.

Signed-off-by: Roel Kluin <roel.kluin@gmail.com>
---
diff --git a/drivers/net/3c505.c b/drivers/net/3c505.c
index 6124605..a8107f9 100644
--- a/drivers/net/3c505.c
+++ b/drivers/net/3c505.c
@@ -493,21 +493,27 @@ static bool receive_pcb(struct net_device *dev, pcb_struct * pcb)
 	}
 	/* read the data */
 	spin_lock_irqsave(&adapter->lock, flags);
-	i = 0;
-	do {
-		j = 0;
-		while (((stat = get_status(dev->base_addr)) & ACRF) == 0 && j++ < 20000);
-		pcb->data.raw[i++] = inb_command(dev->base_addr);
-		if (i > MAX_PCB_DATA)
-			INVALID_PCB_MSG(i);
-	} while ((stat & ASF_PCB_MASK) != ASF_PCB_END && j < 20000);
+	for (i = 0; i < MAX_PCB_DATA; i++) {
+		for (j = 0; j < 20000; j++) {
+			stat = get_status(dev->base_addr);
+			if (stat & ACRF)
+				break;
+		}
+		pcb->data.raw[i] = inb_command(dev->base_addr);
+		if ((stat & ASF_PCB_MASK) == ASF_PCB_END || j >= 20000)
+			break;
+	}
 	spin_unlock_irqrestore(&adapter->lock, flags);
+	if (i >= MAX_PCB_DATA) {
+		INVALID_PCB_MSG(i);
+		return false;
+	}
 	if (j >= 20000) {
 		TIMEOUT_MSG(__LINE__);
 		return false;
 	}
-	/* woops, the last "data" byte was really the length! */
-	total_length = pcb->data.raw[--i];
+	/* the last "data" byte was really the length! */
+	total_length = pcb->data.raw[i];
 
 	/* safety check total length vs data length */
 	if (total_length != (pcb->length + 2)) {
--

From: Jarek Poplawski
Date: Wednesday, February 11, 2009 - 11:37 pm

Yes, the changelog and patch look OK to me.

Thanks,
--

From: David Miller
Date: Thursday, February 12, 2009 - 5:52 pm

From: Jarek Poplawski <jarkao2@gmail.com>

Applied, thanks everyone.
--

From: Roel Kluin
Date: Wednesday, February 11, 2009 - 2:22 pm

Ok, Here's for irda:

------------------------------>8----------------8<------------------------------
If no prior break occurs, cnt reaches 101 after the loop, so we are still able
to change speed when cnt has become 100.

Signed-off-by: Roel Kluin <roel.kluin@gmail.com>

diff --git a/drivers/net/irda/mcs7780.c b/drivers/net/irda/mcs7780.c
index 7eafdca..85e88da 100644
--- a/drivers/net/irda/mcs7780.c
+++ b/drivers/net/irda/mcs7780.c
@@ -585,7 +585,7 @@ static int mcs_speed_change(struct mcs_cb *mcs)
 		mcs_get_reg(mcs, MCS_RESV_REG, &rval);
 	} while(cnt++ < 100 && (rval & MCS_IRINTX));
 
-	if(cnt >= 100) {
+	if (cnt > 100) {
 		IRDA_ERROR("unable to change speed\n");
 		ret = -EIO;
 		goto error;
--

From: David Miller
Date: Thursday, February 12, 2009 - 5:42 pm

From: Roel Kluin <roel.kluin@gmail.com>

Applied, thanks Roel.
--

Previous thread: Re: mdio-gpio: Add mdc pin direction initialization by David Miller on Tuesday, February 10, 2009 - 6:11 pm. (1 message)

Next thread: [PATCH] Update jhash.h with the new version of Jenkins' hash by Jozsef Kadlecsik on Wednesday, February 11, 2009 - 3:19 am. (14 messages)