0226-UBUNTU-SAUCE-bpf-reject-out-of-bounds-stack-pointer-.patch 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. From 3c5659ffcc9d2497045dda465a35720f78314e87 Mon Sep 17 00:00:00 2001
  2. From: Jann Horn <[email protected]>
  3. Date: Thu, 4 Jan 2018 08:01:21 -0600
  4. Subject: [PATCH 226/242] UBUNTU: SAUCE: bpf: reject out-of-bounds stack
  5. pointer calculation
  6. MIME-Version: 1.0
  7. Content-Type: text/plain; charset=UTF-8
  8. Content-Transfer-Encoding: 8bit
  9. Reject programs that compute wildly out-of-bounds stack pointers.
  10. Otherwise, pointers can be computed with an offset that doesn't fit into an
  11. `int`, causing security issues in the stack memory access check (as well as
  12. signed integer overflow during offset addition).
  13. This is a fix specifically for the v4.9 stable tree because the mainline
  14. code looks very different at this point.
  15. Fixes: 7bca0a9702edf ("bpf: enhance verifier to understand stack pointer arithmetic")
  16. Signed-off-by: Jann Horn <[email protected]>
  17. Acked-by: Daniel Borkmann <[email protected]>
  18. CVE-2017-17863
  19. Link: https://www.spinics.net/lists/stable/msg206985.html
  20. Signed-off-by: Seth Forshee <[email protected]>
  21. Signed-off-by: Andy Whitcroft <[email protected]>
  22. Signed-off-by: Kleber Sacilotto de Souza <[email protected]>
  23. (cherry picked from commit 1c26ffd0e9b24d512824cabc6687a14d4777d0f3)
  24. Signed-off-by: Fabian Grünbichler <[email protected]>
  25. ---
  26. kernel/bpf/verifier.c | 22 ++++++++++++++++++++--
  27. 1 file changed, 20 insertions(+), 2 deletions(-)
  28. diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
  29. index 3940019b9740..4321625fe32a 100644
  30. --- a/kernel/bpf/verifier.c
  31. +++ b/kernel/bpf/verifier.c
  32. @@ -2122,10 +2122,28 @@ static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn)
  33. ((BPF_SRC(insn->code) == BPF_X &&
  34. regs[insn->src_reg].type == CONST_IMM) ||
  35. BPF_SRC(insn->code) == BPF_K)) {
  36. - if (BPF_SRC(insn->code) == BPF_X)
  37. + if (BPF_SRC(insn->code) == BPF_X) {
  38. + /* check in case the register contains a big
  39. + * 64-bit value
  40. + */
  41. + if (regs[insn->src_reg].imm < -MAX_BPF_STACK ||
  42. + regs[insn->src_reg].imm > MAX_BPF_STACK) {
  43. + verbose("R%d value too big in R%d pointer arithmetic\n",
  44. + insn->src_reg, insn->dst_reg);
  45. + return -EACCES;
  46. + }
  47. dst_reg->imm += regs[insn->src_reg].imm;
  48. - else
  49. + } else {
  50. + /* safe against overflow: addition of 32-bit
  51. + * numbers in 64-bit representation
  52. + */
  53. dst_reg->imm += insn->imm;
  54. + }
  55. + if (dst_reg->imm > 0 || dst_reg->imm < -MAX_BPF_STACK) {
  56. + verbose("R%d out-of-bounds pointer arithmetic\n",
  57. + insn->dst_reg);
  58. + return -EACCES;
  59. + }
  60. return 0;
  61. } else if (opcode == BPF_ADD &&
  62. BPF_CLASS(insn->code) == BPF_ALU64 &&
  63. --
  64. 2.14.2