__uClibc_main.c 7.8 KB

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