0229-bpf-fix-branch-pruning-logic.patch 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. From 4cdf7c9d4e0958e38635df638229bba6f562511a Mon Sep 17 00:00:00 2001
  2. From: Alexei Starovoitov <[email protected]>
  3. Date: Thu, 4 Jan 2018 08:01:24 -0600
  4. Subject: [PATCH 229/231] bpf: fix branch pruning logic
  5. MIME-Version: 1.0
  6. Content-Type: text/plain; charset=UTF-8
  7. Content-Transfer-Encoding: 8bit
  8. when the verifier detects that register contains a runtime constant
  9. and it's compared with another constant it will prune exploration
  10. of the branch that is guaranteed not to be taken at runtime.
  11. This is all correct, but malicious program may be constructed
  12. in such a way that it always has a constant comparison and
  13. the other branch is never taken under any conditions.
  14. In this case such path through the program will not be explored
  15. by the verifier. It won't be taken at run-time either, but since
  16. all instructions are JITed the malicious program may cause JITs
  17. to complain about using reserved fields, etc.
  18. To fix the issue we have to track the instructions explored by
  19. the verifier and sanitize instructions that are dead at run time
  20. with NOPs. We cannot reject such dead code, since llvm generates
  21. it for valid C code, since it doesn't do as much data flow
  22. analysis as the verifier does.
  23. Fixes: 17a5267067f3 ("bpf: verifier (add verifier core)")
  24. Signed-off-by: Alexei Starovoitov <[email protected]>
  25. Acked-by: Daniel Borkmann <[email protected]>
  26. Signed-off-by: Daniel Borkmann <[email protected]>
  27. (cherry picked from commit c131187db2d3fa2f8bf32fdf4e9a4ef805168467)
  28. CVE-2017-17862
  29. Signed-off-by: Seth Forshee <[email protected]>
  30. Signed-off-by: Andy Whitcroft <[email protected]>
  31. Signed-off-by: Kleber Sacilotto de Souza <[email protected]>
  32. (cherry picked from commit 2df70878d072d06f5bad0db3f2ee1ed47179dff8)
  33. Signed-off-by: Fabian Grünbichler <[email protected]>
  34. ---
  35. include/linux/bpf_verifier.h | 2 +-
  36. kernel/bpf/verifier.c | 27 +++++++++++++++++++++++++++
  37. 2 files changed, 28 insertions(+), 1 deletion(-)
  38. diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h
  39. index 8e5d31f6faef..effeaa64257d 100644
  40. --- a/include/linux/bpf_verifier.h
  41. +++ b/include/linux/bpf_verifier.h
  42. @@ -75,7 +75,7 @@ struct bpf_insn_aux_data {
  43. struct bpf_map *map_ptr; /* pointer for call insn into lookup_elem */
  44. };
  45. int ctx_field_size; /* the ctx field size for load insn, maybe 0 */
  46. - int converted_op_size; /* the valid value width after perceived conversion */
  47. + bool seen; /* this insn was processed by the verifier */
  48. };
  49. #define MAX_USED_MAPS 64 /* max number of maps accessed by one eBPF program */
  50. diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
  51. index 4ecb2e10c5e0..dab5ba668b97 100644
  52. --- a/kernel/bpf/verifier.c
  53. +++ b/kernel/bpf/verifier.c
  54. @@ -3152,6 +3152,7 @@ static int do_check(struct bpf_verifier_env *env)
  55. if (err)
  56. return err;
  57. + env->insn_aux_data[insn_idx].seen = true;
  58. if (class == BPF_ALU || class == BPF_ALU64) {
  59. err = check_alu_op(env, insn);
  60. if (err)
  61. @@ -3342,6 +3343,7 @@ static int do_check(struct bpf_verifier_env *env)
  62. return err;
  63. insn_idx++;
  64. + env->insn_aux_data[insn_idx].seen = true;
  65. } else {
  66. verbose("invalid BPF_LD mode\n");
  67. return -EINVAL;
  68. @@ -3523,6 +3525,7 @@ static int adjust_insn_aux_data(struct bpf_verifier_env *env, u32 prog_len,
  69. u32 off, u32 cnt)
  70. {
  71. struct bpf_insn_aux_data *new_data, *old_data = env->insn_aux_data;
  72. + int i;
  73. if (cnt == 1)
  74. return 0;
  75. @@ -3532,6 +3535,8 @@ static int adjust_insn_aux_data(struct bpf_verifier_env *env, u32 prog_len,
  76. memcpy(new_data, old_data, sizeof(struct bpf_insn_aux_data) * off);
  77. memcpy(new_data + off + cnt - 1, old_data + off,
  78. sizeof(struct bpf_insn_aux_data) * (prog_len - off - cnt + 1));
  79. + for (i = off; i < off + cnt - 1; i++)
  80. + new_data[i].seen = true;
  81. env->insn_aux_data = new_data;
  82. vfree(old_data);
  83. return 0;
  84. @@ -3550,6 +3555,25 @@ static struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 of
  85. return new_prog;
  86. }
  87. +/* The verifier does more data flow analysis than llvm and will not explore
  88. + * branches that are dead at run time. Malicious programs can have dead code
  89. + * too. Therefore replace all dead at-run-time code with nops.
  90. + */
  91. +static void sanitize_dead_code(struct bpf_verifier_env *env)
  92. +{
  93. + struct bpf_insn_aux_data *aux_data = env->insn_aux_data;
  94. + struct bpf_insn nop = BPF_MOV64_REG(BPF_REG_0, BPF_REG_0);
  95. + struct bpf_insn *insn = env->prog->insnsi;
  96. + const int insn_cnt = env->prog->len;
  97. + int i;
  98. +
  99. + for (i = 0; i < insn_cnt; i++) {
  100. + if (aux_data[i].seen)
  101. + continue;
  102. + memcpy(insn + i, &nop, sizeof(nop));
  103. + }
  104. +}
  105. +
  106. /* convert load instructions that access fields of 'struct __sk_buff'
  107. * into sequence of instructions that access fields of 'struct sk_buff'
  108. */
  109. @@ -3841,6 +3865,9 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr)
  110. while (pop_stack(env, NULL) >= 0);
  111. free_states(env);
  112. + if (ret == 0)
  113. + sanitize_dead_code(env);
  114. +
  115. if (ret == 0)
  116. /* program is valid, convert *(u32*)(ctx + off) accesses */
  117. ret = convert_ctx_accesses(env);
  118. --
  119. 2.14.2