From: Sasha Levin <sasha.levin@oracle.com>
To: mingo@kernel.org, akpm@linux-foundation.org,
torvalds@linux-foundation.org
Cc: linux-kernel@vger.kernel.org, Sasha Levin <sasha.levin@oracle.com>
Subject: [RFC 1/2] compiler: use compiler to detect integer overflows
Date: Wed, 26 Nov 2014 09:00:18 -0500 [thread overview]
Message-ID: <1417010419-3827-1-git-send-email-sasha.levin@oracle.com> (raw)
We've used to detect integer overflows by causing an overflow and testing the
result. For example, to test for addition overflow we would:
if (a + b < a)
/* Overflow detected */
While it works, this is actually an undefined behaviour and we're not
guaranteed to have integers overflowing this way. GCC5 has introduced
built in macros (which existed in Clang/LLVM for a while) to test for
addition, subtraction and multiplication overflows.
Rather than keep relying on the current behaviour of GCC, let's take
it's olive branch and test for overflows by using the builtin
functions.
Changing existing code is simple and can be done using Coccinelle:
@@ expression X; expression Y; expression Z; constant C; @@
(
- X + Y < Y
+ check_add_overflow(X, Y)
|
- X - Y > X
+ check_sub_overflow(X, Y)
|
- X != 0 && Y > C / X
+ check_mul_overflow(X, Y, C)
)
Which also makes the code much more clearer, for example:
- if (addr + len < addr)
+ if (check_add_overflow(addr, len))
return -EFAULT;
Signed-off-by: Sasha Levin <sasha.levin@oracle.com>
---
The patch following this one is an example of how changes to existing
code will look like. It's just one patch out of about 40 which are very
simiar - so to avoid lots of useless mails I'll avoid sending them until
this patch looks ok.
include/linux/compiler-gcc5.h | 8 ++++++++
include/linux/compiler.h | 11 +++++++++++
2 files changed, 19 insertions(+)
diff --git a/include/linux/compiler-gcc5.h b/include/linux/compiler-gcc5.h
index c8c5659..9d39f66 100644
--- a/include/linux/compiler-gcc5.h
+++ b/include/linux/compiler-gcc5.h
@@ -63,3 +63,11 @@
#define __HAVE_BUILTIN_BSWAP64__
#define __HAVE_BUILTIN_BSWAP16__
#endif /* CONFIG_ARCH_USE_BUILTIN_BSWAP */
+
+__maybe_unused static unsigned int gcc_overflow_dummy;
+#define check_add_overflow(A, B) \
+ __builtin_add_overflow((A), (B), &gcc_overflow_dummy)
+#define check_sub_overflow(A, B) \
+ __builtin_sub_overflow((A), (B), &gcc_overflow_dummy)
+#define check_mul_overflow(A, B, C) \
+ __builtin_mul_overflow((A), (B), &gcc_overflow_dummy)
diff --git a/include/linux/compiler.h b/include/linux/compiler.h
index 934a834..7f15a18 100644
--- a/include/linux/compiler.h
+++ b/include/linux/compiler.h
@@ -388,4 +388,15 @@ void ftrace_likely_update(struct ftrace_branch_data *f, int val, int expect);
# define __kprobes
# define nokprobe_inline inline
#endif
+
+#ifndef check_add_overflow
+#define check_add_overflow(A, B) (((A) + (B)) < (A))
+#endif
+#ifndef check_sub_overflow
+#define check_sub_overflow(A, B) (((A) - (B)) > (A))
+#endif
+#ifndef check_mul_overflow
+#define check_mul_overflow(A, B, C) ((A) != 0 && (B) > (C) / (A))
+#endif
+
#endif /* __LINUX_COMPILER_H */
--
1.7.10.4
next reply other threads:[~2014-11-26 14:00 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-11-26 14:00 Sasha Levin [this message]
2014-11-26 14:00 ` [RFC 2/2] kvm: eventfd: detect integer overflow using check_*_overflow Sasha Levin
2014-11-26 17:50 ` Andrey Ryabinin
2014-11-26 17:55 ` Sasha Levin
2014-11-26 18:23 ` Linus Torvalds
2014-11-26 19:06 ` Sasha Levin
2014-11-26 19:12 ` Linus Torvalds
2014-11-26 19:27 ` Sasha Levin
2014-11-26 17:48 ` [RFC 1/2] compiler: use compiler to detect integer overflows Andrey Ryabinin
[not found] ` <CA+55aFzyDC=o_Beg+8hjW8+TQXYWCgQo_yfjgHsTz0LRTiomWA@mail.gmail.com>
2014-11-26 18:50 ` Sasha Levin
2014-11-26 18:58 ` Linus Torvalds
2014-11-27 20:42 ` Dan Carpenter
2014-12-05 9:54 ` Dan Carpenter
2014-12-05 18:50 ` Linus Torvalds
2014-12-05 19:39 ` Dan Carpenter
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1417010419-3827-1-git-send-email-sasha.levin@oracle.com \
--to=sasha.levin@oracle.com \
--cc=akpm@linux-foundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@kernel.org \
--cc=torvalds@linux-foundation.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®