__uClibc_main.c 7.2 KB

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