preload.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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/ptrace.h>
  15. #include <sys/types.h>
  16. #include <signal.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <unistd.h>
  20. #include <string.h>
  21. #include <dlfcn.h>
  22. #include "../preload.h"
  23. #define ERROR(fmt, ...) do { \
  24. fprintf(stderr,"perload-jail: "fmt, ## __VA_ARGS__); \
  25. } while (0)
  26. static main_t __main__;
  27. static int __preload_main__(int argc, char **argv, char **envp)
  28. {
  29. unsetenv("LD_PRELOAD");
  30. kill(getpid(), SIGSTOP);
  31. return (*__main__)(argc, argv, envp);
  32. }
  33. int __libc_start_main(main_t main,
  34. int argc,
  35. char **argv,
  36. ElfW(auxv_t) *auxvec,
  37. __typeof (main) init,
  38. void (*fini) (void),
  39. void (*rtld_fini) (void),
  40. void *stack_end)
  41. {
  42. start_main_t __start_main__;
  43. __start_main__ = dlsym(RTLD_NEXT, "__libc_start_main");
  44. if (!__start_main__) {
  45. ERROR("failed to find __libc_start_main %s\n", dlerror());
  46. return -1;
  47. }
  48. __main__ = main;
  49. return (*__start_main__)(__preload_main__, argc, argv, auxvec,
  50. init, fini, rtld_fini, stack_end);
  51. }
  52. void __uClibc_main(main_t main,
  53. int argc,
  54. char **argv,
  55. void (*app_init)(void),
  56. void (*app_fini)(void),
  57. void (*rtld_fini)(void),
  58. void *stack_end attribute_unused)
  59. {
  60. uClibc_main __start_main__;
  61. __start_main__ = dlsym(RTLD_NEXT, "__uClibc_main");
  62. if (!__start_main__) {
  63. ERROR("failed to find __uClibc_main %s\n", dlerror());
  64. return;
  65. }
  66. __main__ = main;
  67. return (*__start_main__)(__preload_main__, argc, argv,
  68. app_init, app_fini, rtld_fini, stack_end);
  69. }