Skip to contentSkip to conversation

fix count(), compat_count() bounds checking

PATCH In review Jason Baron 3 participants 3 messages Sep 19, 2008

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

PATCH 1/1 fix count(), compat_count() bounds checking

hi,

With MAX_ARG_STRINGS set to 0x7FFFFFFF, and being passed to 'count()' and compat_count(), it would appear that the current max bounds check of fs/exec.c:394:

	if(++i > max)
		return -E2BIG;

would never trigger. Since 'i' is of type int, so values would wrap and the function would continue looping.

Simple fix seems to be chaning ++i to i++ and checking for '>='.

thanks,

-Jason

---

 fs/compat.c |    2 +-
 fs/exec.c   |    2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
fs/compat.c +1 -1
index 3d4d57a..2c68dd7 100644
@@ -1233,7 +1233,7 @@ static int compat_count(compat_uptr_t __user *argv, int max)
 			if (!p)
 				break;
 			argv++;
-			if(++i > max)
+			if (i++ >= max)
 				return -E2BIG;
 		}
 	}
fs/exec.c +1 -1
index 9bf0476..7766839 100644
@@ -389,7 +389,7 @@ static int count(char __user * __user * argv, int max)
 			if (!p)
 				break;
 			argv++;
-			if(++i > max)
+			if (i++ >= max)
 				return -E2BIG;
 			cond_resched();
 		}
Acked-by on v1 1/1
#1 lore raw
Quoted 9 lines · Jason Baron

hi,

With MAX_ARG_STRINGS set to 0x7FFFFFFF, and being passed to 'count()' and compat_count(), it would appear that the current max bounds check of fs/exec.c:394:

	if(++i > max)
		return -E2BIG;
Quoted 8 lines

would never trigger. Since 'i' is of type int, so values would wrap and the function would continue looping.

Simple fix seems to be chaning ++i to i++ and checking for '>='.

thanks,

-Jason

Right - looks sane enough, although in practise I'm not sure we'll ever care since I suspect we'll hit the target stack limit before this.

Ollie?

Quoted 1 line
Quoted 34 lines

---

 fs/compat.c |    2 +-
 fs/exec.c   |    2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
fs/compat.c +1 -1
index 3d4d57a..2c68dd7 100644
@@ -1233,7 +1233,7 @@ static int compat_count(compat_uptr_t __user *argv, int max)
 			if (!p)
 				break;
 			argv++;
-			if(++i > max)
+			if (i++ >= max)
 				return -E2BIG;
 		}
 	}
fs/exec.c +1 -1
index 9bf0476..7766839 100644
@@ -389,7 +389,7 @@ static int count(char __user * __user * argv, int max)
 			if (!p)
 				break;
 			argv++;
-			if(++i > max)
+			if (i++ >= max)
 				return -E2BIG;
 			cond_resched();
 		}
#2 lore raw
Quoted 9 lines · Peter Zijlstra
Quoted 2 lines · Jason Baron

Simple fix seems to be chaning ++i to i++ and checking for '>='.

Right - looks sane enough, although in practise I'm not sure we'll ever care since I suspect we'll hit the target stack limit before this.

Ollie?

Sounds reasonable to me. I'd also be ok with changing MAX_ARG_STRINGS to 0x7FFFFFFE to help prevent people from making this mistake in the future.

Ollie