dropper.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Naive system call dropper built on seccomp_filter.
  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_SET_SECCOMP, 2, ...).
  11. *
  12. * When run, returns the specified errno for the specified
  13. * system call number against the given architecture.
  14. *
  15. */
  16. #include <errno.h>
  17. #include <linux/audit.h>
  18. #include <linux/filter.h>
  19. #include <linux/seccomp.h>
  20. #include <linux/unistd.h>
  21. #include <stdio.h>
  22. #include <stddef.h>
  23. #include <stdlib.h>
  24. #include <sys/prctl.h>
  25. #include <unistd.h>
  26. static int install_filter(int nr, int arch, int error)
  27. {
  28. struct sock_filter filter[] = {
  29. BPF_STMT(BPF_LD+BPF_W+BPF_ABS,
  30. (offsetof(struct seccomp_data, arch))),
  31. BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, arch, 0, 3),
  32. BPF_STMT(BPF_LD+BPF_W+BPF_ABS,
  33. (offsetof(struct seccomp_data, nr))),
  34. BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, nr, 0, 1),
  35. BPF_STMT(BPF_RET+BPF_K,
  36. SECCOMP_RET_ERRNO|(error & SECCOMP_RET_DATA)),
  37. BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_ALLOW),
  38. };
  39. struct sock_fprog prog = {
  40. .len = (unsigned short)(sizeof(filter)/sizeof(filter[0])),
  41. .filter = filter,
  42. };
  43. if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
  44. perror("prctl(NO_NEW_PRIVS)");
  45. return 1;
  46. }
  47. if (prctl(PR_SET_SECCOMP, 2, &prog)) {
  48. perror("prctl(PR_SET_SECCOMP)");
  49. return 1;
  50. }
  51. return 0;
  52. }
  53. int main(int argc, char **argv)
  54. {
  55. if (argc < 5) {
  56. fprintf(stderr, "Usage:\n"
  57. "dropper <syscall_nr> <arch> <errno> <prog> [<args>]\n"
  58. "Hint: AUDIT_ARCH_I386: 0x%X\n"
  59. " AUDIT_ARCH_X86_64: 0x%X\n"
  60. "\n", AUDIT_ARCH_I386, AUDIT_ARCH_X86_64);
  61. return 1;
  62. }
  63. if (install_filter(strtol(argv[1], NULL, 0), strtol(argv[2], NULL, 0),
  64. strtol(argv[3], NULL, 0)))
  65. return 1;
  66. execv(argv[4], &argv[4]);
  67. printf("Failed to execv\n");
  68. return 255;
  69. }