seccomp_benchmark.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * Strictly speaking, this is not a test. But it can report during test
  3. * runs so relative performace can be measured.
  4. */
  5. #define _GNU_SOURCE
  6. #include <assert.h>
  7. #include <limits.h>
  8. #include <stdbool.h>
  9. #include <stddef.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <time.h>
  13. #include <unistd.h>
  14. #include <linux/filter.h>
  15. #include <linux/seccomp.h>
  16. #include <sys/param.h>
  17. #include <sys/prctl.h>
  18. #include <sys/syscall.h>
  19. #include <sys/types.h>
  20. #include "../kselftest.h"
  21. unsigned long long timing(clockid_t clk_id, unsigned long long samples)
  22. {
  23. struct timespec start, finish;
  24. unsigned long long i;
  25. pid_t pid, ret;
  26. pid = getpid();
  27. assert(clock_gettime(clk_id, &start) == 0);
  28. for (i = 0; i < samples; i++) {
  29. ret = syscall(__NR_getpid);
  30. assert(pid == ret);
  31. }
  32. assert(clock_gettime(clk_id, &finish) == 0);
  33. i = finish.tv_sec - start.tv_sec;
  34. i *= 1000000000ULL;
  35. i += finish.tv_nsec - start.tv_nsec;
  36. printf("%lu.%09lu - %lu.%09lu = %llu (%.1fs)\n",
  37. finish.tv_sec, finish.tv_nsec,
  38. start.tv_sec, start.tv_nsec,
  39. i, (double)i / 1000000000.0);
  40. return i;
  41. }
  42. unsigned long long calibrate(void)
  43. {
  44. struct timespec start, finish;
  45. unsigned long long i, samples, step = 9973;
  46. pid_t pid, ret;
  47. int seconds = 15;
  48. printf("Calibrating sample size for %d seconds worth of syscalls ...\n", seconds);
  49. samples = 0;
  50. pid = getpid();
  51. assert(clock_gettime(CLOCK_MONOTONIC, &start) == 0);
  52. do {
  53. for (i = 0; i < step; i++) {
  54. ret = syscall(__NR_getpid);
  55. assert(pid == ret);
  56. }
  57. assert(clock_gettime(CLOCK_MONOTONIC, &finish) == 0);
  58. samples += step;
  59. i = finish.tv_sec - start.tv_sec;
  60. i *= 1000000000ULL;
  61. i += finish.tv_nsec - start.tv_nsec;
  62. } while (i < 1000000000ULL);
  63. return samples * seconds;
  64. }
  65. bool approx(int i_one, int i_two)
  66. {
  67. double one = i_one, one_bump = one * 0.01;
  68. double two = i_two, two_bump = two * 0.01;
  69. one_bump = one + MAX(one_bump, 2.0);
  70. two_bump = two + MAX(two_bump, 2.0);
  71. /* Equal to, or within 1% or 2 digits */
  72. if (one == two ||
  73. (one > two && one <= two_bump) ||
  74. (two > one && two <= one_bump))
  75. return true;
  76. return false;
  77. }
  78. bool le(int i_one, int i_two)
  79. {
  80. if (i_one <= i_two)
  81. return true;
  82. return false;
  83. }
  84. long compare(const char *name_one, const char *name_eval, const char *name_two,
  85. unsigned long long one, bool (*eval)(int, int), unsigned long long two)
  86. {
  87. bool good;
  88. printf("\t%s %s %s (%lld %s %lld): ", name_one, name_eval, name_two,
  89. (long long)one, name_eval, (long long)two);
  90. if (one > INT_MAX) {
  91. printf("Miscalculation! Measurement went negative: %lld\n", (long long)one);
  92. return 1;
  93. }
  94. if (two > INT_MAX) {
  95. printf("Miscalculation! Measurement went negative: %lld\n", (long long)two);
  96. return 1;
  97. }
  98. good = eval(one, two);
  99. printf("%s\n", good ? "✔️" : "❌");
  100. return good ? 0 : 1;
  101. }
  102. int main(int argc, char *argv[])
  103. {
  104. struct sock_filter bitmap_filter[] = {
  105. BPF_STMT(BPF_LD|BPF_W|BPF_ABS, offsetof(struct seccomp_data, nr)),
  106. BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
  107. };
  108. struct sock_fprog bitmap_prog = {
  109. .len = (unsigned short)ARRAY_SIZE(bitmap_filter),
  110. .filter = bitmap_filter,
  111. };
  112. struct sock_filter filter[] = {
  113. BPF_STMT(BPF_LD|BPF_W|BPF_ABS, offsetof(struct seccomp_data, args[0])),
  114. BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW),
  115. };
  116. struct sock_fprog prog = {
  117. .len = (unsigned short)ARRAY_SIZE(filter),
  118. .filter = filter,
  119. };
  120. long ret, bits;
  121. unsigned long long samples, calc;
  122. unsigned long long native, filter1, filter2, bitmap1, bitmap2;
  123. unsigned long long entry, per_filter1, per_filter2;
  124. setbuf(stdout, NULL);
  125. printf("Running on:\n");
  126. system("uname -a");
  127. printf("Current BPF sysctl settings:\n");
  128. /* Avoid using "sysctl" which may not be installed. */
  129. system("grep -H . /proc/sys/net/core/bpf_jit_enable");
  130. system("grep -H . /proc/sys/net/core/bpf_jit_harden");
  131. if (argc > 1)
  132. samples = strtoull(argv[1], NULL, 0);
  133. else
  134. samples = calibrate();
  135. printf("Benchmarking %llu syscalls...\n", samples);
  136. /* Native call */
  137. native = timing(CLOCK_PROCESS_CPUTIME_ID, samples) / samples;
  138. printf("getpid native: %llu ns\n", native);
  139. ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
  140. assert(ret == 0);
  141. /* One filter resulting in a bitmap */
  142. ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &bitmap_prog);
  143. assert(ret == 0);
  144. bitmap1 = timing(CLOCK_PROCESS_CPUTIME_ID, samples) / samples;
  145. printf("getpid RET_ALLOW 1 filter (bitmap): %llu ns\n", bitmap1);
  146. /* Second filter resulting in a bitmap */
  147. ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &bitmap_prog);
  148. assert(ret == 0);
  149. bitmap2 = timing(CLOCK_PROCESS_CPUTIME_ID, samples) / samples;
  150. printf("getpid RET_ALLOW 2 filters (bitmap): %llu ns\n", bitmap2);
  151. /* Third filter, can no longer be converted to bitmap */
  152. ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog);
  153. assert(ret == 0);
  154. filter1 = timing(CLOCK_PROCESS_CPUTIME_ID, samples) / samples;
  155. printf("getpid RET_ALLOW 3 filters (full): %llu ns\n", filter1);
  156. /* Fourth filter, can not be converted to bitmap because of filter 3 */
  157. ret = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &bitmap_prog);
  158. assert(ret == 0);
  159. filter2 = timing(CLOCK_PROCESS_CPUTIME_ID, samples) / samples;
  160. printf("getpid RET_ALLOW 4 filters (full): %llu ns\n", filter2);
  161. /* Estimations */
  162. #define ESTIMATE(fmt, var, what) do { \
  163. var = (what); \
  164. printf("Estimated " fmt ": %llu ns\n", var); \
  165. if (var > INT_MAX) \
  166. goto more_samples; \
  167. } while (0)
  168. ESTIMATE("total seccomp overhead for 1 bitmapped filter", calc,
  169. bitmap1 - native);
  170. ESTIMATE("total seccomp overhead for 2 bitmapped filters", calc,
  171. bitmap2 - native);
  172. ESTIMATE("total seccomp overhead for 3 full filters", calc,
  173. filter1 - native);
  174. ESTIMATE("total seccomp overhead for 4 full filters", calc,
  175. filter2 - native);
  176. ESTIMATE("seccomp entry overhead", entry,
  177. bitmap1 - native - (bitmap2 - bitmap1));
  178. ESTIMATE("seccomp per-filter overhead (last 2 diff)", per_filter1,
  179. filter2 - filter1);
  180. ESTIMATE("seccomp per-filter overhead (filters / 4)", per_filter2,
  181. (filter2 - native - entry) / 4);
  182. printf("Expectations:\n");
  183. ret |= compare("native", "≤", "1 bitmap", native, le, bitmap1);
  184. bits = compare("native", "≤", "1 filter", native, le, filter1);
  185. if (bits)
  186. goto more_samples;
  187. ret |= compare("per-filter (last 2 diff)", "≈", "per-filter (filters / 4)",
  188. per_filter1, approx, per_filter2);
  189. bits = compare("1 bitmapped", "≈", "2 bitmapped",
  190. bitmap1 - native, approx, bitmap2 - native);
  191. if (bits) {
  192. printf("Skipping constant action bitmap expectations: they appear unsupported.\n");
  193. goto out;
  194. }
  195. ret |= compare("entry", "≈", "1 bitmapped", entry, approx, bitmap1 - native);
  196. ret |= compare("entry", "≈", "2 bitmapped", entry, approx, bitmap2 - native);
  197. ret |= compare("native + entry + (per filter * 4)", "≈", "4 filters total",
  198. entry + (per_filter1 * 4) + native, approx, filter2);
  199. if (ret == 0)
  200. goto out;
  201. more_samples:
  202. printf("Saw unexpected benchmark result. Try running again with more samples?\n");
  203. out:
  204. return 0;
  205. }