__uClibc_main.c 8.3 KB

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