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