bpf-helper.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Seccomp BPF helper functions
  3. *
  4. * Copyright (c) 2012 The Chromium OS Authors <chromium-os-dev@chromium.org>
  5. * Author: Will Drewry <wad@chromium.org>
  6. *
  7. * The code may be used by anyone for any purpose,
  8. * and can serve as a starting point for developing
  9. * applications using prctl(PR_ATTACH_SECCOMP_FILTER).
  10. */
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include "bpf-helper.h"
  14. int bpf_resolve_jumps(struct bpf_labels *labels,
  15. struct sock_filter *filter, size_t count)
  16. {
  17. struct sock_filter *begin = filter;
  18. __u8 insn = count - 1;
  19. if (count < 1)
  20. return -1;
  21. /*
  22. * Walk it once, backwards, to build the label table and do fixups.
  23. * Since backward jumps are disallowed by BPF, this is easy.
  24. */
  25. filter += insn;
  26. for (; filter >= begin; --insn, --filter) {
  27. if (filter->code != (BPF_JMP+BPF_JA))
  28. continue;
  29. switch ((filter->jt<<8)|filter->jf) {
  30. case (JUMP_JT<<8)|JUMP_JF:
  31. if (labels->labels[filter->k].location == 0xffffffff) {
  32. fprintf(stderr, "Unresolved label: '%s'\n",
  33. labels->labels[filter->k].label);
  34. return 1;
  35. }
  36. filter->k = labels->labels[filter->k].location -
  37. (insn + 1);
  38. filter->jt = 0;
  39. filter->jf = 0;
  40. continue;
  41. case (LABEL_JT<<8)|LABEL_JF:
  42. if (labels->labels[filter->k].location != 0xffffffff) {
  43. fprintf(stderr, "Duplicate label use: '%s'\n",
  44. labels->labels[filter->k].label);
  45. return 1;
  46. }
  47. labels->labels[filter->k].location = insn;
  48. filter->k = 0; /* fall through */
  49. filter->jt = 0;
  50. filter->jf = 0;
  51. continue;
  52. }
  53. }
  54. return 0;
  55. }
  56. /* Simple lookup table for labels. */
  57. __u32 seccomp_bpf_label(struct bpf_labels *labels, const char *label)
  58. {
  59. struct __bpf_label *begin = labels->labels, *end;
  60. int id;
  61. if (labels->count == 0) {
  62. begin->label = label;
  63. begin->location = 0xffffffff;
  64. labels->count++;
  65. return 0;
  66. }
  67. end = begin + labels->count;
  68. for (id = 0; begin < end; ++begin, ++id) {
  69. if (!strcmp(label, begin->label))
  70. return id;
  71. }
  72. begin->label = label;
  73. begin->location = 0xffffffff;
  74. labels->count++;
  75. return id;
  76. }
  77. void seccomp_bpf_print(struct sock_filter *filter, size_t count)
  78. {
  79. struct sock_filter *end = filter + count;
  80. for ( ; filter < end; ++filter)
  81. printf("{ code=%u,jt=%u,jf=%u,k=%u },\n",
  82. filter->code, filter->jt, filter->jf, filter->k);
  83. }