Skip to contentSkip to conversation

drivers/xen/grant-table.c: Be sure of unsigned value never comparing with 0

PATCH Applied Chen Gang 2 participants 9 messages Aug 26, 2014 – Aug 27, 2014 Xen hypervisor interface

lore
Share & tools
lore

Chen Gang

author
#0 lore raw

PATCH 1/1 drivers/xen/grant-table.c: Be sure of unsigned value never comparing with 0

In grow_gnttab_list(), 'i' is 'unsigned int', and 'nr_glist_frames' may be 0 because 'nr_grant_frames' may be 0. So 'i' may never be less than 'nr_glist_frames' in failure processing, which cause infinite looping.

---
 drivers/xen/grant-table.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/xen/grant-table.c +2 -2
index c254ae0..be07645 100644
@@ -592,8 +592,8 @@ static int grow_gnttab_list(unsigned int more_frames)
 	return 0;
 
 grow_nomem:
-	for ( ; i >= nr_glist_frames; i--)
-		free_page((unsigned long) gnttab_list[i]);
+	while (i > nr_glist_frames)
+		free_page((unsigned long) gnttab_list[--i]);
 	return -ENOMEM;
 }
 
Signature · 1 line
1.9.3
#1 lore raw
Quoted 3 lines · Chen Gang

In grow_gnttab_list(), 'i' is 'unsigned int', and 'nr_glist_frames' may be 0 because 'nr_grant_frames' may be 0. So 'i' may never be less than 'nr_glist_frames' in failure processing, which cause infinite looping.

nr_grant_frames is at least 1. See gnttab_init().

drivers/xen/grant-table.c static int grow_gnttab_list(unsigned int more_frames) · line 596

Quoted 10 lines
drivers/xen/grant-table.c +2 -2
@@ -592,8 +592,8 @@ static int grow_gnttab_list(unsigned int more_frames)
 	return 0;
 
 grow_nomem:
-	for ( ; i >= nr_glist_frames; i--)
-		free_page((unsigned long) gnttab_list[i]);
+	while (i > nr_glist_frames)
+		free_page((unsigned long) gnttab_list[--i]);

while (i-- > nr_glist_frames)

    ...

Would have been better.

David

#8 lore raw
In reply to Chen Gang
Quoted 3 lines · Chen Gang

In grow_gnttab_list(), 'i' is 'unsigned int', and 'nr_glist_frames' may be 0 because 'nr_grant_frames' may be 0. So 'i' may never be less than 'nr_glist_frames' in failure processing, which cause infinite looping.

nr_grant_frames is at least 1. See gnttab_init().

drivers/xen/grant-table.c static int grow_gnttab_list(unsigned int more_frames) · line 596

Quoted 10 lines
drivers/xen/grant-table.c +2 -2
@@ -592,8 +592,8 @@ static int grow_gnttab_list(unsigned int more_frames)
 	return 0;
 
 grow_nomem:
-	for ( ; i >= nr_glist_frames; i--)
-		free_page((unsigned long) gnttab_list[i]);
+	while (i > nr_glist_frames)
+		free_page((unsigned long) gnttab_list[--i]);

while (i-- > nr_glist_frames)

    ...

Would have been better.

David

author
#2 lore raw
In reply to David Vrabel
Quoted 7 lines · David Vrabel
Quoted 3 lines · Chen Gang

In grow_gnttab_list(), 'i' is 'unsigned int', and 'nr_glist_frames' may be 0 because 'nr_grant_frames' may be 0. So 'i' may never be less than 'nr_glist_frames' in failure processing, which cause infinite looping.

nr_grant_frames is at least 1. See gnttab_init().

OK, thanks, that sounds reasonable to me, it is not a real wold bug, it is my fault. :-)

Quoted 12 lines

drivers/xen/grant-table.c static int grow_gnttab_list(unsigned int more_frames) · line 596

Quoted 10 lines
drivers/xen/grant-table.c +2 -2
@@ -592,8 +592,8 @@ static int grow_gnttab_list(unsigned int more_frames)
 	return 0;
 
 grow_nomem:
-	for ( ; i >= nr_glist_frames; i--)
-		free_page((unsigned long) gnttab_list[i]);
+	while (i > nr_glist_frames)
+		free_page((unsigned long) gnttab_list[--i]);

while (i-- > nr_glist_frames)

Quoted 4 lines
    ...

Would have been better.

OK, thanks, that sounds reasonable to me.

If necessary to send patch v2 (change comments and contents), please let me know, and I shall send.

Thanks.

Signature · 3 lines
Chen Gang

Open share and attitude like air water and life which God blessed
author
#7 lore raw
In reply to David Vrabel
Quoted 7 lines · David Vrabel
Quoted 3 lines · Chen Gang

In grow_gnttab_list(), 'i' is 'unsigned int', and 'nr_glist_frames' may be 0 because 'nr_grant_frames' may be 0. So 'i' may never be less than 'nr_glist_frames' in failure processing, which cause infinite looping.

nr_grant_frames is at least 1. See gnttab_init().

OK, thanks, that sounds reasonable to me, it is not a real wold bug, it is my fault. :-)

Quoted 12 lines

drivers/xen/grant-table.c static int grow_gnttab_list(unsigned int more_frames) · line 596

Quoted 10 lines
drivers/xen/grant-table.c +2 -2
@@ -592,8 +592,8 @@ static int grow_gnttab_list(unsigned int more_frames)
 	return 0;
 
 grow_nomem:
-	for ( ; i >= nr_glist_frames; i--)
-		free_page((unsigned long) gnttab_list[i]);
+	while (i > nr_glist_frames)
+		free_page((unsigned long) gnttab_list[--i]);

while (i-- > nr_glist_frames)

Quoted 4 lines
    ...

Would have been better.

OK, thanks, that sounds reasonable to me.

If necessary to send patch v2 (change comments and contents), please let me know, and I shall send.

Thanks.

Signature · 3 lines
Chen Gang

Open share and attitude like air water and life which God blessed
Applied
#3 lore raw
In reply to Chen Gang
Quoted 26 lines · Chen Gang
Quoted 7 lines · David Vrabel
Quoted 3 lines · Chen Gang

In grow_gnttab_list(), 'i' is 'unsigned int', and 'nr_glist_frames' may be 0 because 'nr_grant_frames' may be 0. So 'i' may never be less than 'nr_glist_frames' in failure processing, which cause infinite looping.

nr_grant_frames is at least 1. See gnttab_init().

OK, thanks, that sounds reasonable to me, it is not a real wold bug, it is my fault. :-)

Quoted 14 lines

drivers/xen/grant-table.c static int grow_gnttab_list(unsigned int more_frames) · line 596

Quoted 10 lines
drivers/xen/grant-table.c +2 -2
@@ -592,8 +592,8 @@ static int grow_gnttab_list(unsigned int more_frames)
 	return 0;
 
 grow_nomem:
-	for ( ; i >= nr_glist_frames; i--)
-		free_page((unsigned long) gnttab_list[i]);
+	while (i > nr_glist_frames)
+		free_page((unsigned long) gnttab_list[--i]);

while (i-- > nr_glist_frames)

    ...
Quoted 7 lines
Quoted 2 lines

Would have been better.

OK, thanks, that sounds reasonable to me.

If necessary to send patch v2 (change comments and contents), please let me know, and I shall send.

Applied to devel/for-linus-3.18 with this description:

xen/grant-table: refactor error cleanup in grow_gnttab_list()

The cleanup loop in grow_gnttab_list() is safe from the underflow of the unsigned 'i' since nr_glist_frames is >= 1, but refactor it anyway.

Thanks.

David

Applied
#6 lore raw
In reply to Chen Gang
Quoted 26 lines · Chen Gang
Quoted 7 lines · David Vrabel
Quoted 3 lines · Chen Gang

In grow_gnttab_list(), 'i' is 'unsigned int', and 'nr_glist_frames' may be 0 because 'nr_grant_frames' may be 0. So 'i' may never be less than 'nr_glist_frames' in failure processing, which cause infinite looping.

nr_grant_frames is at least 1. See gnttab_init().

OK, thanks, that sounds reasonable to me, it is not a real wold bug, it is my fault. :-)

Quoted 14 lines

drivers/xen/grant-table.c static int grow_gnttab_list(unsigned int more_frames) · line 596

Quoted 10 lines
drivers/xen/grant-table.c +2 -2
@@ -592,8 +592,8 @@ static int grow_gnttab_list(unsigned int more_frames)
 	return 0;
 
 grow_nomem:
-	for ( ; i >= nr_glist_frames; i--)
-		free_page((unsigned long) gnttab_list[i]);
+	while (i > nr_glist_frames)
+		free_page((unsigned long) gnttab_list[--i]);

while (i-- > nr_glist_frames)

    ...
Quoted 7 lines
Quoted 2 lines

Would have been better.

OK, thanks, that sounds reasonable to me.

If necessary to send patch v2 (change comments and contents), please let me know, and I shall send.

Applied to devel/for-linus-3.18 with this description:

xen/grant-table: refactor error cleanup in grow_gnttab_list()

The cleanup loop in grow_gnttab_list() is safe from the underflow of the unsigned 'i' since nr_glist_frames is >= 1, but refactor it anyway.

Thanks.

David

author
#4 lore raw
In reply to David Vrabel
Quoted 9 lines · David Vrabel

Applied to devel/for-linus-3.18 with this description:

xen/grant-table: refactor error cleanup in grow_gnttab_list()

The cleanup loop in grow_gnttab_list() is safe from the underflow of the unsigned 'i' since nr_glist_frames is >= 1, but refactor it anyway.

OK, thank you very much for your encouragement! And next, I shall try to provide another patches for Xen (hope I can finish within this month). And I should be more careful about the patches which I shall send.

Also I want to consult: "Is there a common test for Xen"? If there is, please let me know, and I should try to perform the common test before send any patches.

Welcome any additional ideas, suggestions, and completions.

Thanks.

Signature · 3 lines
Chen Gang

Open, share, and attitude like air, water, and life which God blessed
author
#5 lore raw
In reply to David Vrabel
Quoted 9 lines · David Vrabel

Applied to devel/for-linus-3.18 with this description:

xen/grant-table: refactor error cleanup in grow_gnttab_list()

The cleanup loop in grow_gnttab_list() is safe from the underflow of the unsigned 'i' since nr_glist_frames is >= 1, but refactor it anyway.

OK, thank you very much for your encouragement! And next, I shall try to provide another patches for Xen (hope I can finish within this month). And I should be more careful about the patches which I shall send.

Also I want to consult: "Is there a common test for Xen"? If there is, please let me know, and I should try to perform the common test before send any patches.

Welcome any additional ideas, suggestions, and completions.

Thanks.

Signature · 3 lines
Chen Gang

Open, share, and attitude like air, water, and life which God blessed