__uClibc_main.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. #include <ssp-internal.h>
  29. unsigned long __guard = 0UL;
  30. #endif
  31. /*
  32. * Prototypes.
  33. */
  34. extern void *__libc_stack_end;
  35. extern void weak_function _stdio_init(void);
  36. extern int *weak_const_function __errno_location(void);
  37. extern int *weak_const_function __h_errno_location(void);
  38. #ifdef __UCLIBC_HAS_LOCALE__
  39. extern void weak_function _locale_init(void);
  40. #endif
  41. #ifdef __UCLIBC_HAS_THREADS__
  42. extern void weak_function __pthread_initialize_minimal(void);
  43. #endif
  44. /*
  45. * Declare the __environ global variable and create a weak alias environ.
  46. * Note: Apparently we must initialize __environ to ensure that the weak
  47. * environ symbol is also included.
  48. */
  49. char **__environ = 0;
  50. weak_alias(__environ, environ);
  51. size_t __pagesize = 0;
  52. const char *__progname = 0;
  53. #ifndef O_NOFOLLOW
  54. # define O_NOFOLLOW 0
  55. #endif
  56. extern int __libc_fcntl(int fd, int cmd, ...);
  57. extern int __libc_open(const char *file, int flags, ...);
  58. static void __check_one_fd(int fd, int mode)
  59. {
  60. /* Check if the specified fd is already open */
  61. if (unlikely(__libc_fcntl(fd, F_GETFD)==-1 && *(__errno_location())==EBADF))
  62. {
  63. /* The descriptor is probably not open, so try to use /dev/null */
  64. struct stat st;
  65. int nullfd = __libc_open(_PATH_DEVNULL, mode);
  66. /* /dev/null is major=1 minor=3. Make absolutely certain
  67. * that is in fact the device that we have opened and not
  68. * some other wierd file... */
  69. if ( (nullfd!=fd) || fstat(fd, &st) || !S_ISCHR(st.st_mode) ||
  70. (st.st_rdev != makedev(1, 3)))
  71. {
  72. /* Somebody is trying some trickery here... */
  73. while (1) {
  74. abort();
  75. }
  76. }
  77. }
  78. }
  79. static int __check_suid(void)
  80. {
  81. uid_t uid, euid;
  82. gid_t gid, egid;
  83. uid = getuid();
  84. euid = geteuid();
  85. gid = getgid();
  86. egid = getegid();
  87. if(uid == euid && gid == egid) {
  88. return 0;
  89. }
  90. return 1;
  91. }
  92. #ifdef __UCLIBC_HAS_SSP__
  93. static __always_inline void __guard_setup(void)
  94. {
  95. if (__guard != 0UL)
  96. return;
  97. #ifndef __SSP_QUICK_CANARY__
  98. size_t size;
  99. # ifdef __SSP_USE_ERANDOM__
  100. {
  101. int mib[3];
  102. /* Random is another depth in Linux, hence an array of 3. */
  103. mib[0] = CTL_KERN;
  104. mib[1] = KERN_RANDOM;
  105. mib[2] = RANDOM_ERANDOM;
  106. size = sizeof(unsigned long);
  107. if (SYSCTL(mib, 3, &__guard, &size, NULL, 0) != (-1))
  108. if (__guard != 0UL)
  109. return;
  110. }
  111. # endif /* ifdef __SSP_USE_ERANDOM__ */
  112. {
  113. int fd;
  114. # ifdef __SSP_USE_ERANDOM__
  115. /*
  116. * Attempt to open kernel pseudo random device if one exists before
  117. * opening urandom to avoid system entropy depletion.
  118. */
  119. if ((fd = OPEN("/dev/erandom", O_RDONLY)) == (-1))
  120. # endif
  121. fd = OPEN("/dev/urandom", O_RDONLY);
  122. if (fd != (-1)) {
  123. size = READ(fd, (char *) &__guard, sizeof(__guard));
  124. CLOSE(fd);
  125. if (size == sizeof(__guard))
  126. return;
  127. }
  128. }
  129. #endif /* ifndef __SSP_QUICK_CANARY__ */
  130. /* Start with the "terminator canary". */
  131. __guard = 0xFF0A0D00UL;
  132. /* Everything failed? Or we are using a weakened model of the
  133. * terminator canary */
  134. {
  135. struct timeval tv;
  136. GETTIMEOFDAY(&tv, NULL);
  137. __guard ^= tv.tv_usec ^ tv.tv_sec;
  138. }
  139. }
  140. #endif /* __UCLIBC_HAS_SSP__ */
  141. /* __uClibc_init completely initialize uClibc so it is ready to use.
  142. *
  143. * On ELF systems (with a dynamic loader) this function must be called
  144. * from the dynamic loader (see TIS and ELF Specification), so that
  145. * constructors of shared libraries (which depend on libc) can use all
  146. * the libc code without restriction. For this we link the shared
  147. * version of the uClibc with -init __uClibc_init so DT_INIT for
  148. * uClibc is the address of __uClibc_init
  149. *
  150. * In all other cases we call it from the main stub
  151. * __uClibc_main.
  152. */
  153. void __uClibc_init(void)
  154. {
  155. static int been_there_done_that = 0;
  156. if (been_there_done_that)
  157. return;
  158. been_there_done_that++;
  159. /* Setup an initial value. This may not be perfect, but is
  160. * better than malloc using __pagesize=0 for atexit, ctors, etc. */
  161. __pagesize = PAGE_SIZE;
  162. #ifdef __UCLIBC_HAS_THREADS__
  163. /* Before we start initializing uClibc we have to call
  164. * __pthread_initialize_minimal so we can use pthread_locks
  165. * whenever they are needed.
  166. */
  167. if (likely(__pthread_initialize_minimal!=NULL))
  168. __pthread_initialize_minimal();
  169. #endif
  170. #ifdef __UCLIBC_HAS_SSP__
  171. __guard_setup ();
  172. #endif
  173. #ifdef __UCLIBC_HAS_LOCALE__
  174. /* Initialize the global locale structure. */
  175. if (likely(_locale_init!=NULL))
  176. _locale_init();
  177. #endif
  178. /*
  179. * Initialize stdio here. In the static library case, this will
  180. * be bypassed if not needed because of the weak alias above.
  181. */
  182. if (likely(_stdio_init != NULL))
  183. _stdio_init();
  184. }
  185. #ifdef __UCLIBC_CTOR_DTOR__
  186. void attribute_hidden (*__app_fini)(void) = NULL;
  187. #endif
  188. void attribute_hidden (*__rtld_fini)(void) = NULL;
  189. /* __uClibc_main is the new main stub for uClibc. This function is
  190. * called from crt1 (version 0.9.28 or newer), after ALL shared libraries
  191. * are initialized, just before we call the application's main function.
  192. */
  193. void __attribute__ ((__noreturn__))
  194. __uClibc_main(int (*main)(int, char **, char **), int argc,
  195. char **argv, void (*app_init)(void), void (*app_fini)(void),
  196. void (*rtld_fini)(void), void *stack_end)
  197. {
  198. #ifdef __ARCH_HAS_MMU__
  199. unsigned long *aux_dat;
  200. ElfW(auxv_t) auxvt[AT_EGID + 1];
  201. #endif
  202. __libc_stack_end = stack_end;
  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. __rtld_fini = rtld_fini;
  208. /* The environment begins right after argv. */
  209. __environ = &argv[argc + 1];
  210. /* If the first thing after argv is the arguments
  211. * the the environment is empty. */
  212. if ((char *) __environ == *argv) {
  213. /* Make __environ point to the NULL at argv[argc] */
  214. __environ = &argv[argc];
  215. }
  216. /* Pull stuff from the ELF header when possible */
  217. #ifdef __ARCH_HAS_MMU__
  218. aux_dat = (unsigned long*)__environ;
  219. while (*aux_dat) {
  220. aux_dat++;
  221. }
  222. aux_dat++;
  223. while (*aux_dat) {
  224. ElfW(auxv_t) *auxv_entry = (ElfW(auxv_t) *) aux_dat;
  225. if (auxv_entry->a_type <= AT_EGID) {
  226. memcpy(&(auxvt[auxv_entry->a_type]), auxv_entry, sizeof(ElfW(auxv_t)));
  227. }
  228. aux_dat += 2;
  229. }
  230. /* Make certain getpagesize() gives the correct answer */
  231. __pagesize = (auxvt[AT_PAGESZ].a_un.a_val)? auxvt[AT_PAGESZ].a_un.a_val : PAGE_SIZE;
  232. /* Prevent starting SUID binaries where the stdin. stdout, and
  233. * stderr file descriptors are not already opened. */
  234. if ((auxvt[AT_UID].a_un.a_val==-1 && __check_suid()) ||
  235. (auxvt[AT_UID].a_un.a_val != -1 &&
  236. (auxvt[AT_UID].a_un.a_val != auxvt[AT_EUID].a_un.a_val ||
  237. auxvt[AT_GID].a_un.a_val != auxvt[AT_EGID].a_un.a_val)))
  238. {
  239. __check_one_fd (STDIN_FILENO, O_RDONLY | O_NOFOLLOW);
  240. __check_one_fd (STDOUT_FILENO, O_RDWR | O_NOFOLLOW);
  241. __check_one_fd (STDERR_FILENO, O_RDWR | O_NOFOLLOW);
  242. }
  243. #endif
  244. __progname = *argv;
  245. #ifdef __UCLIBC_CTOR_DTOR__
  246. /* Arrange for the application's dtors to run before we exit. */
  247. __app_fini = app_fini;
  248. /* Run all the application's ctors now. */
  249. if (app_init!=NULL) {
  250. app_init();
  251. }
  252. #endif
  253. /* Note: It is possible that any initialization done above could
  254. * have resulted in errno being set nonzero, so set it to 0 before
  255. * we call main.
  256. */
  257. if (likely(__errno_location!=NULL))
  258. *(__errno_location()) = 0;
  259. /* Set h_errno to 0 as well */
  260. if (likely(__h_errno_location!=NULL))
  261. *(__h_errno_location()) = 0;
  262. /*
  263. * Finally, invoke application's main and then exit.
  264. */
  265. exit(main(argc, argv, __environ));
  266. }