preload.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Copyright (C) 2015 John Crispin <blogic@openwrt.org>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU Lesser General Public License version 2.1
  6. * as published by the Free Software Foundation
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #define _GNU_SOURCE
  14. #include <sys/types.h>
  15. #include <stdlib.h>
  16. #include <unistd.h>
  17. #include <string.h>
  18. #include <dlfcn.h>
  19. #include "log.h"
  20. #include "seccomp.h"
  21. #include "../preload.h"
  22. static main_t __main__;
  23. int debug;
  24. static int __preload_main__(int argc, char **argv, char **envp)
  25. {
  26. char *env_file = getenv("SECCOMP_FILE");
  27. char *env_debug = getenv("SECCOMP_DEBUG");
  28. if (!env_file || !env_file[0]) {
  29. ERROR("SECCOMP_FILE not specified\n");
  30. return -1;
  31. }
  32. if (env_debug)
  33. debug = atoi(env_debug);
  34. else
  35. debug = 0;
  36. if (install_syscall_filter(*argv, env_file))
  37. return -1;
  38. unsetenv("LD_PRELOAD");
  39. unsetenv("SECCOMP_DEBUG");
  40. unsetenv("SECCOMP_FILE");
  41. return (*__main__)(argc, argv, envp);
  42. }
  43. int __libc_start_main(main_t main,
  44. int argc,
  45. char **argv,
  46. ElfW(auxv_t) *auxvec,
  47. __typeof (main) init,
  48. void (*fini) (void),
  49. void (*rtld_fini) (void),
  50. void *stack_end)
  51. {
  52. start_main_t __start_main__;
  53. __start_main__ = dlsym(RTLD_NEXT, "__libc_start_main");
  54. if (!__start_main__) {
  55. INFO("failed to find __libc_start_main %s\n", dlerror());
  56. return -1;
  57. }
  58. __main__ = main;
  59. return (*__start_main__)(__preload_main__, argc, argv, auxvec,
  60. init, fini, rtld_fini, stack_end);
  61. }
  62. void __uClibc_main(main_t main,
  63. int argc,
  64. char **argv,
  65. void (*app_init)(void),
  66. void (*app_fini)(void),
  67. void (*rtld_fini)(void),
  68. void *stack_end attribute_unused)
  69. {
  70. uClibc_main __start_main__;
  71. __start_main__ = dlsym(RTLD_NEXT, "__uClibc_main");
  72. if (!__start_main__) {
  73. INFO("failed to find __uClibc_main %s\n", dlerror());
  74. return;
  75. }
  76. __main__ = main;
  77. return (*__start_main__)(__preload_main__, argc, argv,
  78. app_init, app_fini, rtld_fini, stack_end);
  79. }