bpf-helper.c 2.4 KB

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