__uClibc_main.c 7.3 KB

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