diff mbox

ipmi: fix unsigned long underflow

Message ID 1501381228-30958-1-git-send-email-minyard@acm.org
State New
Headers show

Commit Message

Corey Minyard July 30, 2017, 2:20 a.m. UTC
From: Corey Minyard <cminyard@mvista.com>


When I set the timeout to a specific value such as 500ms, the timeout
event will not happen in time due to the overflow in function
check_msg_timeout:
...
	ent->timeout -= timeout_period;
	if (ent->timeout > 0)
		return;
...

The type of timeout_period is long, but ent->timeout is unsigned long.
This patch makes the type consistent.

Reported-by: Weilong Chen <chenweilong@huawei.com>
Signed-off-by: Corey Minyard <cminyard@mvista.com>

---
I like to keep things consistent (though I obviously messed up here)
and keep variables that should be positive unsigned.

But you are right, there is a bug here and some inconsistency.
This patch changes timeout_period to be unsigned and fixes the
check.  Can you try this out?

 drivers/char/ipmi/ipmi_msghandler.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

-- 
2.7.4

Comments

Weilong Chen Aug. 7, 2017, 8:41 a.m. UTC | #1
Hi Minyard,

I test this patch, it works.

Thanks.

On 2017/7/30 10:20, minyard@acm.org wrote:
> From: Corey Minyard <cminyard@mvista.com>

>

> When I set the timeout to a specific value such as 500ms, the timeout

> event will not happen in time due to the overflow in function

> check_msg_timeout:

> ...

> 	ent->timeout -= timeout_period;

> 	if (ent->timeout > 0)

> 		return;

> ...

>

> The type of timeout_period is long, but ent->timeout is unsigned long.

> This patch makes the type consistent.

>

> Reported-by: Weilong Chen <chenweilong@huawei.com>

> Signed-off-by: Corey Minyard <cminyard@mvista.com>

> ---

> I like to keep things consistent (though I obviously messed up here)

> and keep variables that should be positive unsigned.

>

> But you are right, there is a bug here and some inconsistency.

> This patch changes timeout_period to be unsigned and fixes the

> check.  Can you try this out?

>

>  drivers/char/ipmi/ipmi_msghandler.c | 10 ++++++----

>  1 file changed, 6 insertions(+), 4 deletions(-)

>

> diff --git a/drivers/char/ipmi/ipmi_msghandler.c b/drivers/char/ipmi/ipmi_msghandler.c

> index 810b138..c82d9fd 100644

> --- a/drivers/char/ipmi/ipmi_msghandler.c

> +++ b/drivers/char/ipmi/ipmi_msghandler.c

> @@ -4030,7 +4030,8 @@ smi_from_recv_msg(ipmi_smi_t intf, struct ipmi_recv_msg *recv_msg,

>  }

>

>  static void check_msg_timeout(ipmi_smi_t intf, struct seq_table *ent,

> -			      struct list_head *timeouts, long timeout_period,

> +			      struct list_head *timeouts,

> +			      unsigned long timeout_period,

>  			      int slot, unsigned long *flags,

>  			      unsigned int *waiting_msgs)

>  {

> @@ -4043,8 +4044,8 @@ static void check_msg_timeout(ipmi_smi_t intf, struct seq_table *ent,

>  	if (!ent->inuse)

>  		return;

>

> -	ent->timeout -= timeout_period;

> -	if (ent->timeout > 0) {

> +	if (timeout_period < ent->timeout) {

> +		ent->timeout -= timeout_period;

>  		(*waiting_msgs)++;

>  		return;

>  	}

> @@ -4110,7 +4111,8 @@ static void check_msg_timeout(ipmi_smi_t intf, struct seq_table *ent,

>  	}

>  }

>

> -static unsigned int ipmi_timeout_handler(ipmi_smi_t intf, long timeout_period)

> +static unsigned int ipmi_timeout_handler(ipmi_smi_t intf,

> +					 unsigned long timeout_period)

>  {

>  	struct list_head     timeouts;

>  	struct ipmi_recv_msg *msg, *msg2;

>
Corey Minyard Aug. 7, 2017, 12:24 p.m. UTC | #2
On 08/07/2017 03:41 AM, Weilong Chen wrote:
> Hi Minyard,

>

> I test this patch, it works.

>

> Thanks.

>


Thanks, I added a Tested-by: for you and it's queued for the next release.
If it's urgent I can send it in now.

I also added this for the stable kernels 3.16 and later.

-corey

> On 2017/7/30 10:20, minyard@acm.org wrote:

>> From: Corey Minyard <cminyard@mvista.com>

>>

>> When I set the timeout to a specific value such as 500ms, the timeout

>> event will not happen in time due to the overflow in function

>> check_msg_timeout:

>> ...

>>     ent->timeout -= timeout_period;

>>     if (ent->timeout > 0)

>>         return;

>> ...

>>

>> The type of timeout_period is long, but ent->timeout is unsigned long.

>> This patch makes the type consistent.

>>

>> Reported-by: Weilong Chen <chenweilong@huawei.com>

>> Signed-off-by: Corey Minyard <cminyard@mvista.com>

>> ---

>> I like to keep things consistent (though I obviously messed up here)

>> and keep variables that should be positive unsigned.

>>

>> But you are right, there is a bug here and some inconsistency.

>> This patch changes timeout_period to be unsigned and fixes the

>> check.  Can you try this out?

>>

>>  drivers/char/ipmi/ipmi_msghandler.c | 10 ++++++----

>>  1 file changed, 6 insertions(+), 4 deletions(-)

>>

>> diff --git a/drivers/char/ipmi/ipmi_msghandler.c 

>> b/drivers/char/ipmi/ipmi_msghandler.c

>> index 810b138..c82d9fd 100644

>> --- a/drivers/char/ipmi/ipmi_msghandler.c

>> +++ b/drivers/char/ipmi/ipmi_msghandler.c

>> @@ -4030,7 +4030,8 @@ smi_from_recv_msg(ipmi_smi_t intf, struct 

>> ipmi_recv_msg *recv_msg,

>>  }

>>

>>  static void check_msg_timeout(ipmi_smi_t intf, struct seq_table *ent,

>> -                  struct list_head *timeouts, long timeout_period,

>> +                  struct list_head *timeouts,

>> +                  unsigned long timeout_period,

>>                    int slot, unsigned long *flags,

>>                    unsigned int *waiting_msgs)

>>  {

>> @@ -4043,8 +4044,8 @@ static void check_msg_timeout(ipmi_smi_t intf, 

>> struct seq_table *ent,

>>      if (!ent->inuse)

>>          return;

>>

>> -    ent->timeout -= timeout_period;

>> -    if (ent->timeout > 0) {

>> +    if (timeout_period < ent->timeout) {

>> +        ent->timeout -= timeout_period;

>>          (*waiting_msgs)++;

>>          return;

>>      }

>> @@ -4110,7 +4111,8 @@ static void check_msg_timeout(ipmi_smi_t intf, 

>> struct seq_table *ent,

>>      }

>>  }

>>

>> -static unsigned int ipmi_timeout_handler(ipmi_smi_t intf, long 

>> timeout_period)

>> +static unsigned int ipmi_timeout_handler(ipmi_smi_t intf,

>> +                     unsigned long timeout_period)

>>  {

>>      struct list_head     timeouts;

>>      struct ipmi_recv_msg *msg, *msg2;

>>

>
diff mbox

Patch

diff --git a/drivers/char/ipmi/ipmi_msghandler.c b/drivers/char/ipmi/ipmi_msghandler.c
index 810b138..c82d9fd 100644
--- a/drivers/char/ipmi/ipmi_msghandler.c
+++ b/drivers/char/ipmi/ipmi_msghandler.c
@@ -4030,7 +4030,8 @@  smi_from_recv_msg(ipmi_smi_t intf, struct ipmi_recv_msg *recv_msg,
 }
 
 static void check_msg_timeout(ipmi_smi_t intf, struct seq_table *ent,
-			      struct list_head *timeouts, long timeout_period,
+			      struct list_head *timeouts,
+			      unsigned long timeout_period,
 			      int slot, unsigned long *flags,
 			      unsigned int *waiting_msgs)
 {
@@ -4043,8 +4044,8 @@  static void check_msg_timeout(ipmi_smi_t intf, struct seq_table *ent,
 	if (!ent->inuse)
 		return;
 
-	ent->timeout -= timeout_period;
-	if (ent->timeout > 0) {
+	if (timeout_period < ent->timeout) {
+		ent->timeout -= timeout_period;
 		(*waiting_msgs)++;
 		return;
 	}
@@ -4110,7 +4111,8 @@  static void check_msg_timeout(ipmi_smi_t intf, struct seq_table *ent,
 	}
 }
 
-static unsigned int ipmi_timeout_handler(ipmi_smi_t intf, long timeout_period)
+static unsigned int ipmi_timeout_handler(ipmi_smi_t intf,
+					 unsigned long timeout_period)
 {
 	struct list_head     timeouts;
 	struct ipmi_recv_msg *msg, *msg2;