__uClibc_main.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 int main(int argc, char **argv, char **envp);
  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_start_main(int argc, char **argv, char **envp,
  141. void (*app_init)(void), void (*app_fini)(void), void (*rtld_fini)(void))
  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. /* If we are dynamically linked, then ldso already did this for us. */
  153. if (__environ==NULL) {
  154. /* Statically linked. */
  155. __environ = envp;
  156. }
  157. /* Pull stuff from the ELF header when possible */
  158. #ifdef __ARCH_HAS_MMU__
  159. aux_dat = (unsigned long*)envp;
  160. while (*aux_dat) {
  161. aux_dat++;
  162. }
  163. aux_dat++;
  164. while (*aux_dat) {
  165. Elf32_auxv_t *auxv_entry = (Elf32_auxv_t *) aux_dat;
  166. if (auxv_entry->a_type <= AT_EGID) {
  167. memcpy(&(auxvt[auxv_entry->a_type]), auxv_entry, sizeof(Elf32_auxv_t));
  168. }
  169. aux_dat += 2;
  170. }
  171. /* Make certain getpagesize() gives the correct answer */
  172. __pagesize = (auxvt[AT_PAGESZ].a_un.a_val)? auxvt[AT_PAGESZ].a_un.a_val : PAGE_SIZE;
  173. /* Prevent starting SUID binaries where the stdin. stdout, and
  174. * stderr file descriptors are not already opened. */
  175. if ((auxvt[AT_UID].a_un.a_val==-1 && __check_suid()) ||
  176. (auxvt[AT_UID].a_un.a_val != -1 &&
  177. (auxvt[AT_UID].a_un.a_val != auxvt[AT_EUID].a_un.a_val ||
  178. auxvt[AT_GID].a_un.a_val != auxvt[AT_EGID].a_un.a_val)))
  179. {
  180. __check_one_fd (STDIN_FILENO, O_RDONLY | O_NOFOLLOW);
  181. __check_one_fd (STDOUT_FILENO, O_RDWR | O_NOFOLLOW);
  182. __check_one_fd (STDERR_FILENO, O_RDWR | O_NOFOLLOW);
  183. }
  184. #endif
  185. __progname = *argv;
  186. #ifdef __UCLIBC_CTOR_DTOR__
  187. /* Arrange for the application's dtors to run before we exit. */
  188. __app_fini = app_fini;
  189. /* Run all the application's ctors now. */
  190. if (app_init!=NULL) {
  191. app_init();
  192. }
  193. #endif
  194. #ifdef __UCLIBC_HAS_SSP__
  195. __guard_setup ();
  196. #endif
  197. /* Note: It is possible that any initialization done above could
  198. * have resulted in errno being set nonzero, so set it to 0 before
  199. * we call main.
  200. */
  201. if (likely(__errno_location!=NULL))
  202. *(__errno_location()) = 0;
  203. /* Set h_errno to 0 as well */
  204. if (likely(__h_errno_location!=NULL))
  205. *(__h_errno_location()) = 0;
  206. /*
  207. * Finally, invoke application's main and then exit.
  208. */
  209. exit(main(argc, argv, envp));
  210. }
  211. /* __uClibc_main is the old main stub of the uClibc. This
  212. * function is called from crt0 (uClibc 0.9.15 and older) after
  213. * ALL shared libraries are initialized, and just before we call
  214. * the application's main() function.
  215. *
  216. * Attention: This stub does not call the .init/.fini sections of
  217. * the application. If you need this, please fix your uClibc port
  218. * so that __uClibc_start_main is called by your crt0.S with
  219. * _init and _fini properly set.
  220. */
  221. void __attribute__ ((__noreturn__))
  222. __uClibc_main(int argc, char **argv, char ** envp)
  223. {
  224. __uClibc_start_main(argc, argv, envp, NULL, NULL, NULL);
  225. }