__uClibc_main.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * Manuel Novoa III Feb 2001
  3. * Erik Andersen 2002-2004
  4. *
  5. * __uClibc_main is the routine to be called by all the arch-specific
  6. * versions of crt1.S in uClibc.
  7. *
  8. * It is meant to handle any special initialization needed by the library
  9. * such as setting the global variable(s) __environ (environ) and
  10. * initializing the stdio package. Using weak symbols, the latter is
  11. * avoided in the static library case.
  12. */
  13. #define _ERRNO_H
  14. #include <features.h>
  15. #include <unistd.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <elf.h>
  19. #include <link.h>
  20. #include <bits/uClibc_page.h>
  21. #include <paths.h>
  22. #include <unistd.h>
  23. #include <asm/errno.h>
  24. #include <fcntl.h>
  25. #include <sys/stat.h>
  26. #include <sys/sysmacros.h>
  27. #ifdef __UCLIBC_HAS_SSP__
  28. extern void __guard_setup(void);
  29. #endif
  30. /*
  31. * Prototypes.
  32. */
  33. extern void *__libc_stack_end;
  34. extern void weak_function _stdio_init(void);
  35. extern int *weak_const_function __errno_location(void);
  36. extern int *weak_const_function __h_errno_location(void);
  37. #ifdef __UCLIBC_HAS_LOCALE__
  38. extern void weak_function _locale_init(void);
  39. #endif
  40. #ifdef __UCLIBC_HAS_THREADS__
  41. extern void weak_function __pthread_initialize_minimal(void);
  42. #endif
  43. /*
  44. * Declare the __environ global variable and create a weak alias environ.
  45. * Note: Apparently we must initialize __environ to ensure that the weak
  46. * environ symbol is also included.
  47. */
  48. char **__environ = 0;
  49. weak_alias(__environ, environ);
  50. size_t __pagesize = 0;
  51. const char *__progname = 0;
  52. #ifndef O_NOFOLLOW
  53. # define O_NOFOLLOW 0
  54. #endif
  55. extern int __libc_fcntl(int fd, int cmd, ...);
  56. extern int __libc_open(const char *file, int flags, ...);
  57. static void __check_one_fd(int fd, int mode)
  58. {
  59. /* Check if the specified fd is already open */
  60. if (unlikely(__libc_fcntl(fd, F_GETFD)==-1 && *(__errno_location())==EBADF))
  61. {
  62. /* The descriptor is probably not open, so try to use /dev/null */
  63. struct stat st;
  64. int nullfd = __libc_open(_PATH_DEVNULL, mode);
  65. /* /dev/null is major=1 minor=3. Make absolutely certain
  66. * that is in fact the device that we have opened and not
  67. * some other wierd file... */
  68. if ( (nullfd!=fd) || fstat(fd, &st) || !S_ISCHR(st.st_mode) ||
  69. (st.st_rdev != makedev(1, 3)))
  70. {
  71. /* Somebody is trying some trickery here... */
  72. while (1) {
  73. abort();
  74. }
  75. }
  76. }
  77. }
  78. static int __check_suid(void)
  79. {
  80. uid_t uid, euid;
  81. gid_t gid, egid;
  82. uid = getuid();
  83. euid = geteuid();
  84. gid = getgid();
  85. egid = getegid();
  86. if(uid == euid && gid == egid) {
  87. return 0;
  88. }
  89. return 1;
  90. }
  91. /* __uClibc_init completely initialize uClibc so it is ready to use.
  92. *
  93. * On ELF systems (with a dynamic loader) this function must be called
  94. * from the dynamic loader (see TIS and ELF Specification), so that
  95. * constructors of shared libraries (which depend on libc) can use all
  96. * the libc code without restriction. For this we link the shared
  97. * version of the uClibc with -init __uClibc_init so DT_INIT for
  98. * uClibc is the address of __uClibc_init
  99. *
  100. * In all other cases we call it from the main stub
  101. * __uClibc_main.
  102. */
  103. void __uClibc_init(void)
  104. {
  105. static int been_there_done_that = 0;
  106. if (been_there_done_that)
  107. return;
  108. been_there_done_that++;
  109. /* Setup an initial value. This may not be perfect, but is
  110. * better than malloc using __pagesize=0 for atexit, ctors, etc. */
  111. __pagesize = PAGE_SIZE;
  112. #ifdef __UCLIBC_HAS_THREADS__
  113. /* Before we start initializing uClibc we have to call
  114. * __pthread_initialize_minimal so we can use pthread_locks
  115. * whenever they are needed.
  116. */
  117. if (likely(__pthread_initialize_minimal!=NULL))
  118. __pthread_initialize_minimal();
  119. #endif
  120. #ifdef __UCLIBC_HAS_LOCALE__
  121. /* Initialize the global locale structure. */
  122. if (likely(_locale_init!=NULL))
  123. _locale_init();
  124. #endif
  125. /*
  126. * Initialize stdio here. In the static library case, this will
  127. * be bypassed if not needed because of the weak alias above.
  128. */
  129. if (likely(_stdio_init != NULL))
  130. _stdio_init();
  131. }
  132. #ifdef __UCLIBC_CTOR_DTOR__
  133. void attribute_hidden (*__app_fini)(void) = NULL;
  134. #endif
  135. void attribute_hidden (*__rtld_fini)(void) = NULL;
  136. /* __uClibc_main is the new main stub for uClibc. This function is
  137. * called from crt1 (version 0.9.28 or newer), after ALL shared libraries
  138. * are initialized, just before we call the application's main function.
  139. */
  140. void __attribute__ ((__noreturn__))
  141. __uClibc_main(int (*main)(int, char **, char **), int argc,
  142. char **argv, void (*app_init)(void), void (*app_fini)(void),
  143. void (*rtld_fini)(void), void *stack_end)
  144. {
  145. #ifdef __ARCH_HAS_MMU__
  146. unsigned long *aux_dat;
  147. ElfW(auxv_t) auxvt[AT_EGID + 1];
  148. #endif
  149. __libc_stack_end = stack_end;
  150. /* We need to initialize uClibc. If we are dynamically linked this
  151. * may have already been completed by the shared lib loader. We call
  152. * __uClibc_init() regardless, to be sure the right thing happens. */
  153. __uClibc_init();
  154. __rtld_fini = rtld_fini;
  155. /* The environment begins right after argv. */
  156. __environ = &argv[argc + 1];
  157. /* If the first thing after argv is the arguments
  158. * the the environment is empty. */
  159. if ((char *) __environ == *argv) {
  160. /* Make __environ point to the NULL at argv[argc] */
  161. __environ = &argv[argc];
  162. }
  163. /* Pull stuff from the ELF header when possible */
  164. #ifdef __ARCH_HAS_MMU__
  165. aux_dat = (unsigned long*)__environ;
  166. while (*aux_dat) {
  167. aux_dat++;
  168. }
  169. aux_dat++;
  170. while (*aux_dat) {
  171. ElfW(auxv_t) *auxv_entry = (ElfW(auxv_t) *) aux_dat;
  172. if (auxv_entry->a_type <= AT_EGID) {
  173. memcpy(&(auxvt[auxv_entry->a_type]), auxv_entry, sizeof(ElfW(auxv_t)));
  174. }
  175. aux_dat += 2;
  176. }
  177. /* Make certain getpagesize() gives the correct answer */
  178. __pagesize = (auxvt[AT_PAGESZ].a_un.a_val)? auxvt[AT_PAGESZ].a_un.a_val : PAGE_SIZE;
  179. /* Prevent starting SUID binaries where the stdin. stdout, and
  180. * stderr file descriptors are not already opened. */
  181. if ((auxvt[AT_UID].a_un.a_val==-1 && __check_suid()) ||
  182. (auxvt[AT_UID].a_un.a_val != -1 &&
  183. (auxvt[AT_UID].a_un.a_val != auxvt[AT_EUID].a_un.a_val ||
  184. auxvt[AT_GID].a_un.a_val != auxvt[AT_EGID].a_un.a_val)))
  185. {
  186. __check_one_fd (STDIN_FILENO, O_RDONLY | O_NOFOLLOW);
  187. __check_one_fd (STDOUT_FILENO, O_RDWR | O_NOFOLLOW);
  188. __check_one_fd (STDERR_FILENO, O_RDWR | O_NOFOLLOW);
  189. }
  190. #endif
  191. __progname = *argv;
  192. #ifdef __UCLIBC_CTOR_DTOR__
  193. /* Arrange for the application's dtors to run before we exit. */
  194. __app_fini = app_fini;
  195. /* Run all the application's ctors now. */
  196. if (app_init!=NULL) {
  197. app_init();
  198. }
  199. #endif
  200. #ifdef __UCLIBC_HAS_SSP__
  201. __guard_setup ();
  202. #endif
  203. /* Note: It is possible that any initialization done above could
  204. * have resulted in errno being set nonzero, so set it to 0 before
  205. * we call main.
  206. */
  207. if (likely(__errno_location!=NULL))
  208. *(__errno_location()) = 0;
  209. /* Set h_errno to 0 as well */
  210. if (likely(__h_errno_location!=NULL))
  211. *(__h_errno_location()) = 0;
  212. /*
  213. * Finally, invoke application's main and then exit.
  214. */
  215. exit(main(argc, argv, __environ));
  216. }