nfp_bpf_verifier.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Copyright (C) 2016 Netronome Systems, Inc.
  3. *
  4. * This software is dual licensed under the GNU General License Version 2,
  5. * June 1991 as shown in the file COPYING in the top-level directory of this
  6. * source tree or the BSD 2-Clause License provided below. You have the
  7. * option to license this software under the complete terms of either license.
  8. *
  9. * The BSD 2-Clause License:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * 1. Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * 2. Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. */
  33. #define pr_fmt(fmt) "NFP net bpf: " fmt
  34. #include <linux/bpf.h>
  35. #include <linux/bpf_verifier.h>
  36. #include <linux/kernel.h>
  37. #include <linux/pkt_cls.h>
  38. #include "nfp_bpf.h"
  39. /* Analyzer/verifier definitions */
  40. struct nfp_bpf_analyzer_priv {
  41. struct nfp_prog *prog;
  42. struct nfp_insn_meta *meta;
  43. };
  44. static struct nfp_insn_meta *
  45. nfp_bpf_goto_meta(struct nfp_prog *nfp_prog, struct nfp_insn_meta *meta,
  46. unsigned int insn_idx, unsigned int n_insns)
  47. {
  48. unsigned int forward, backward, i;
  49. backward = meta->n - insn_idx;
  50. forward = insn_idx - meta->n;
  51. if (min(forward, backward) > n_insns - insn_idx - 1) {
  52. backward = n_insns - insn_idx - 1;
  53. meta = nfp_prog_last_meta(nfp_prog);
  54. }
  55. if (min(forward, backward) > insn_idx && backward > insn_idx) {
  56. forward = insn_idx;
  57. meta = nfp_prog_first_meta(nfp_prog);
  58. }
  59. if (forward < backward)
  60. for (i = 0; i < forward; i++)
  61. meta = nfp_meta_next(meta);
  62. else
  63. for (i = 0; i < backward; i++)
  64. meta = nfp_meta_prev(meta);
  65. return meta;
  66. }
  67. static int
  68. nfp_bpf_check_exit(struct nfp_prog *nfp_prog,
  69. const struct bpf_verifier_env *env)
  70. {
  71. const struct bpf_reg_state *reg0 = &env->cur_state.regs[0];
  72. if (nfp_prog->act == NN_ACT_XDP)
  73. return 0;
  74. if (reg0->type != CONST_IMM) {
  75. pr_info("unsupported exit state: %d, imm: %llx\n",
  76. reg0->type, reg0->imm);
  77. return -EINVAL;
  78. }
  79. if (nfp_prog->act != NN_ACT_DIRECT &&
  80. reg0->imm != 0 && (reg0->imm & ~0U) != ~0U) {
  81. pr_info("unsupported exit state: %d, imm: %llx\n",
  82. reg0->type, reg0->imm);
  83. return -EINVAL;
  84. }
  85. if (nfp_prog->act == NN_ACT_DIRECT && reg0->imm <= TC_ACT_REDIRECT &&
  86. reg0->imm != TC_ACT_SHOT && reg0->imm != TC_ACT_STOLEN &&
  87. reg0->imm != TC_ACT_QUEUED) {
  88. pr_info("unsupported exit state: %d, imm: %llx\n",
  89. reg0->type, reg0->imm);
  90. return -EINVAL;
  91. }
  92. return 0;
  93. }
  94. static int
  95. nfp_bpf_check_ctx_ptr(struct nfp_prog *nfp_prog,
  96. const struct bpf_verifier_env *env, u8 reg)
  97. {
  98. if (env->cur_state.regs[reg].type != PTR_TO_CTX)
  99. return -EINVAL;
  100. return 0;
  101. }
  102. static int
  103. nfp_verify_insn(struct bpf_verifier_env *env, int insn_idx, int prev_insn_idx)
  104. {
  105. struct nfp_bpf_analyzer_priv *priv = env->analyzer_priv;
  106. struct nfp_insn_meta *meta = priv->meta;
  107. meta = nfp_bpf_goto_meta(priv->prog, meta, insn_idx, env->prog->len);
  108. priv->meta = meta;
  109. if (meta->insn.src_reg == BPF_REG_10 ||
  110. meta->insn.dst_reg == BPF_REG_10) {
  111. pr_err("stack not yet supported\n");
  112. return -EINVAL;
  113. }
  114. if (meta->insn.src_reg >= MAX_BPF_REG ||
  115. meta->insn.dst_reg >= MAX_BPF_REG) {
  116. pr_err("program uses extended registers - jit hardening?\n");
  117. return -EINVAL;
  118. }
  119. if (meta->insn.code == (BPF_JMP | BPF_EXIT))
  120. return nfp_bpf_check_exit(priv->prog, env);
  121. if ((meta->insn.code & ~BPF_SIZE_MASK) == (BPF_LDX | BPF_MEM))
  122. return nfp_bpf_check_ctx_ptr(priv->prog, env,
  123. meta->insn.src_reg);
  124. if ((meta->insn.code & ~BPF_SIZE_MASK) == (BPF_STX | BPF_MEM))
  125. return nfp_bpf_check_ctx_ptr(priv->prog, env,
  126. meta->insn.dst_reg);
  127. return 0;
  128. }
  129. static const struct bpf_ext_analyzer_ops nfp_bpf_analyzer_ops = {
  130. .insn_hook = nfp_verify_insn,
  131. };
  132. int nfp_prog_verify(struct nfp_prog *nfp_prog, struct bpf_prog *prog)
  133. {
  134. struct nfp_bpf_analyzer_priv *priv;
  135. int ret;
  136. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  137. if (!priv)
  138. return -ENOMEM;
  139. priv->prog = nfp_prog;
  140. priv->meta = nfp_prog_first_meta(nfp_prog);
  141. ret = bpf_analyzer(prog, &nfp_bpf_analyzer_ops, priv);
  142. kfree(priv);
  143. return ret;
  144. }