* [PATCH RFC] exec: avoid possible undefined behavior in count()
@ 2013-01-06 5:29 Xi Wang
2013-01-07 21:44 ` Andrew Morton
0 siblings, 1 reply; 3+ messages in thread
From: Xi Wang @ 2013-01-06 5:29 UTC (permalink / raw)
To: linux-kernel; +Cc: Jason Baron, Andrew Morton, Al Viro, Xi Wang
The tricky problem is this check:
if (i++ >= max)
icc (mis)optimizes this check as:
if (++i > max)
The check now becomes a no-op since max is MAX_ARG_STRINGS (0x7FFFFFFF).
This is "allowed" by the C standard, assuming i++ never overflows,
because signed integer overflow is undefined behavior. This optimization
effectively reverts the previous commit 362e6663ef ("exec.c, compat.c:
fix count(), compat_count() bounds checking") that tries to fix the check.
This patch simply moves ++ after the check.
Signed-off-by: Xi Wang <xi.wang@gmail.com>
---
Not sure how many people are using Intel's icc to compiled the kernel.
Some projects like LinuxDNA did.
The kernel uses gcc's -fno-strict-overflow to disable this optimization.
icc probably doesn't recognize the option.
To illustrate the problem, try this simple program:
int count(int i, int max)
{
if (i++ >= max) {
__builtin_trap();
return -1;
}
return i;
}
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
int x = atoi(argv[1]);
int max = atoi(argv[2]);
printf("%d %d %d\n", x, max, count(x, max));
}
$ gcc -O2 t.c
$ ./a.out 2147483647 2147483647
Illegal instruction (core dumped)
$ icc -O2 t.c
$ ./a.out 2147483647 2147483647
2147483647 2147483647 -2147483648
There's no difference whether we add -fno-strict-overflow or not.
---
fs/exec.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/fs/exec.c b/fs/exec.c
index 18c45ca..20df02c 100644
--- a/fs/exec.c
+++ b/fs/exec.c
@@ -434,8 +434,9 @@ static int count(struct user_arg_ptr argv, int max)
if (IS_ERR(p))
return -EFAULT;
- if (i++ >= max)
+ if (i >= max)
return -E2BIG;
+ ++i;
if (fatal_signal_pending(current))
return -ERESTARTNOHAND;
--
1.7.10.4
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH RFC] exec: avoid possible undefined behavior in count()
2013-01-06 5:29 [PATCH RFC] exec: avoid possible undefined behavior in count() Xi Wang
@ 2013-01-07 21:44 ` Andrew Morton
2013-01-16 21:47 ` Xi Wang
0 siblings, 1 reply; 3+ messages in thread
From: Andrew Morton @ 2013-01-07 21:44 UTC (permalink / raw)
To: Xi Wang; +Cc: linux-kernel, Jason Baron, Al Viro
On Sun, 6 Jan 2013 00:29:05 -0500
Xi Wang <xi.wang@gmail.com> wrote:
> The tricky problem is this check:
>
> if (i++ >= max)
>
> icc (mis)optimizes this check as:
>
> if (++i > max)
>
> The check now becomes a no-op since max is MAX_ARG_STRINGS (0x7FFFFFFF).
>
> This is "allowed" by the C standard, assuming i++ never overflows,
> because signed integer overflow is undefined behavior. This optimization
> effectively reverts the previous commit 362e6663ef ("exec.c, compat.c:
> fix count(), compat_count() bounds checking") that tries to fix the check.
>
> This patch simply moves ++ after the check.
>
> ...
>
> --- a/fs/exec.c
> +++ b/fs/exec.c
> @@ -434,8 +434,9 @@ static int count(struct user_arg_ptr argv, int max)
> if (IS_ERR(p))
> return -EFAULT;
>
> - if (i++ >= max)
> + if (i >= max)
> return -E2BIG;
> + ++i;
>
> if (fatal_signal_pending(current))
> return -ERESTARTNOHAND;
I have no problem working around a compiler bug when the workaround is
so small and simple. For clarity and accuracy I renamed the patch to
"fs/exec.c: work around icc miscompilation".
However I'd also like to be able to add "this bug has been reported to
the icc developers and will be fixed in version X.Y"?
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH RFC] exec: avoid possible undefined behavior in count()
2013-01-07 21:44 ` Andrew Morton
@ 2013-01-16 21:47 ` Xi Wang
0 siblings, 0 replies; 3+ messages in thread
From: Xi Wang @ 2013-01-16 21:47 UTC (permalink / raw)
To: Andrew Morton; +Cc: linux-kernel, Jason Baron, Al Viro
On 1/7/13 4:44 PM, Andrew Morton wrote:
> I have no problem working around a compiler bug when the workaround is
> so small and simple. For clarity and accuracy I renamed the patch to
> "fs/exec.c: work around icc miscompilation".
Thanks!
> However I'd also like to be able to add "this bug has been reported to
> the icc developers and will be fixed in version X.Y"?
The icc developers have confirmed this bug and filed a defect ticket.
I'll let you know if there's any further update.
- xi
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2013-01-16 21:48 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-01-06 5:29 [PATCH RFC] exec: avoid possible undefined behavior in count() Xi Wang
2013-01-07 21:44 ` Andrew Morton
2013-01-16 21:47 ` Xi Wang
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox