exec.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539
  1. /*
  2. * linux/fs/exec.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. */
  6. /*
  7. * #!-checking implemented by tytso.
  8. */
  9. /*
  10. * Demand-loading implemented 01.12.91 - no need to read anything but
  11. * the header into memory. The inode of the executable is put into
  12. * "current->executable", and page faults do the actual loading. Clean.
  13. *
  14. * Once more I can proudly say that linux stood up to being changed: it
  15. * was less than 2 hours work to get demand-loading completely implemented.
  16. *
  17. * Demand loading changed July 1993 by Eric Youngdale. Use mmap instead,
  18. * current->executable is only used by the procfs. This allows a dispatch
  19. * table to check for several different types of binary formats. We keep
  20. * trying until we recognize the file or we run out of supported binary
  21. * formats.
  22. */
  23. #include <linux/config.h>
  24. #include <linux/slab.h>
  25. #include <linux/file.h>
  26. #include <linux/mman.h>
  27. #include <linux/a.out.h>
  28. #include <linux/stat.h>
  29. #include <linux/fcntl.h>
  30. #include <linux/smp_lock.h>
  31. #include <linux/init.h>
  32. #include <linux/pagemap.h>
  33. #include <linux/highmem.h>
  34. #include <linux/spinlock.h>
  35. #include <linux/key.h>
  36. #include <linux/personality.h>
  37. #include <linux/binfmts.h>
  38. #include <linux/swap.h>
  39. #include <linux/utsname.h>
  40. #include <linux/module.h>
  41. #include <linux/namei.h>
  42. #include <linux/proc_fs.h>
  43. #include <linux/ptrace.h>
  44. #include <linux/mount.h>
  45. #include <linux/security.h>
  46. #include <linux/syscalls.h>
  47. #include <linux/rmap.h>
  48. #include <linux/acct.h>
  49. #include <linux/cn_proc.h>
  50. #include <linux/audit.h>
  51. #include <asm/uaccess.h>
  52. #include <asm/mmu_context.h>
  53. #ifdef CONFIG_KMOD
  54. #include <linux/kmod.h>
  55. #endif
  56. int core_uses_pid;
  57. char core_pattern[65] = "core";
  58. int suid_dumpable = 0;
  59. EXPORT_SYMBOL(suid_dumpable);
  60. /* The maximal length of core_pattern is also specified in sysctl.c */
  61. static struct linux_binfmt *formats;
  62. static DEFINE_RWLOCK(binfmt_lock);
  63. int register_binfmt(struct linux_binfmt * fmt)
  64. {
  65. struct linux_binfmt ** tmp = &formats;
  66. if (!fmt)
  67. return -EINVAL;
  68. if (fmt->next)
  69. return -EBUSY;
  70. write_lock(&binfmt_lock);
  71. while (*tmp) {
  72. if (fmt == *tmp) {
  73. write_unlock(&binfmt_lock);
  74. return -EBUSY;
  75. }
  76. tmp = &(*tmp)->next;
  77. }
  78. fmt->next = formats;
  79. formats = fmt;
  80. write_unlock(&binfmt_lock);
  81. return 0;
  82. }
  83. EXPORT_SYMBOL(register_binfmt);
  84. int unregister_binfmt(struct linux_binfmt * fmt)
  85. {
  86. struct linux_binfmt ** tmp = &formats;
  87. write_lock(&binfmt_lock);
  88. while (*tmp) {
  89. if (fmt == *tmp) {
  90. *tmp = fmt->next;
  91. write_unlock(&binfmt_lock);
  92. return 0;
  93. }
  94. tmp = &(*tmp)->next;
  95. }
  96. write_unlock(&binfmt_lock);
  97. return -EINVAL;
  98. }
  99. EXPORT_SYMBOL(unregister_binfmt);
  100. static inline void put_binfmt(struct linux_binfmt * fmt)
  101. {
  102. module_put(fmt->module);
  103. }
  104. /*
  105. * Note that a shared library must be both readable and executable due to
  106. * security reasons.
  107. *
  108. * Also note that we take the address to load from from the file itself.
  109. */
  110. asmlinkage long sys_uselib(const char __user * library)
  111. {
  112. struct file * file;
  113. struct nameidata nd;
  114. int error;
  115. error = __user_path_lookup_open(library, LOOKUP_FOLLOW, &nd, FMODE_READ|FMODE_EXEC);
  116. if (error)
  117. goto out;
  118. error = -EINVAL;
  119. if (!S_ISREG(nd.dentry->d_inode->i_mode))
  120. goto exit;
  121. error = vfs_permission(&nd, MAY_READ | MAY_EXEC);
  122. if (error)
  123. goto exit;
  124. file = nameidata_to_filp(&nd, O_RDONLY);
  125. error = PTR_ERR(file);
  126. if (IS_ERR(file))
  127. goto out;
  128. error = -ENOEXEC;
  129. if(file->f_op) {
  130. struct linux_binfmt * fmt;
  131. read_lock(&binfmt_lock);
  132. for (fmt = formats ; fmt ; fmt = fmt->next) {
  133. if (!fmt->load_shlib)
  134. continue;
  135. if (!try_module_get(fmt->module))
  136. continue;
  137. read_unlock(&binfmt_lock);
  138. error = fmt->load_shlib(file);
  139. read_lock(&binfmt_lock);
  140. put_binfmt(fmt);
  141. if (error != -ENOEXEC)
  142. break;
  143. }
  144. read_unlock(&binfmt_lock);
  145. }
  146. fput(file);
  147. out:
  148. return error;
  149. exit:
  150. release_open_intent(&nd);
  151. path_release(&nd);
  152. goto out;
  153. }
  154. /*
  155. * count() counts the number of strings in array ARGV.
  156. */
  157. static int count(char __user * __user * argv, int max)
  158. {
  159. int i = 0;
  160. if (argv != NULL) {
  161. for (;;) {
  162. char __user * p;
  163. if (get_user(p, argv))
  164. return -EFAULT;
  165. if (!p)
  166. break;
  167. argv++;
  168. if(++i > max)
  169. return -E2BIG;
  170. cond_resched();
  171. }
  172. }
  173. return i;
  174. }
  175. /*
  176. * 'copy_strings()' copies argument/environment strings from user
  177. * memory to free pages in kernel mem. These are in a format ready
  178. * to be put directly into the top of new user memory.
  179. */
  180. static int copy_strings(int argc, char __user * __user * argv,
  181. struct linux_binprm *bprm)
  182. {
  183. struct page *kmapped_page = NULL;
  184. char *kaddr = NULL;
  185. int ret;
  186. while (argc-- > 0) {
  187. char __user *str;
  188. int len;
  189. unsigned long pos;
  190. if (get_user(str, argv+argc) ||
  191. !(len = strnlen_user(str, bprm->p))) {
  192. ret = -EFAULT;
  193. goto out;
  194. }
  195. if (bprm->p < len) {
  196. ret = -E2BIG;
  197. goto out;
  198. }
  199. bprm->p -= len;
  200. /* XXX: add architecture specific overflow check here. */
  201. pos = bprm->p;
  202. while (len > 0) {
  203. int i, new, err;
  204. int offset, bytes_to_copy;
  205. struct page *page;
  206. offset = pos % PAGE_SIZE;
  207. i = pos/PAGE_SIZE;
  208. page = bprm->page[i];
  209. new = 0;
  210. if (!page) {
  211. page = alloc_page(GFP_HIGHUSER);
  212. bprm->page[i] = page;
  213. if (!page) {
  214. ret = -ENOMEM;
  215. goto out;
  216. }
  217. new = 1;
  218. }
  219. if (page != kmapped_page) {
  220. if (kmapped_page)
  221. kunmap(kmapped_page);
  222. kmapped_page = page;
  223. kaddr = kmap(kmapped_page);
  224. }
  225. if (new && offset)
  226. memset(kaddr, 0, offset);
  227. bytes_to_copy = PAGE_SIZE - offset;
  228. if (bytes_to_copy > len) {
  229. bytes_to_copy = len;
  230. if (new)
  231. memset(kaddr+offset+len, 0,
  232. PAGE_SIZE-offset-len);
  233. }
  234. err = copy_from_user(kaddr+offset, str, bytes_to_copy);
  235. if (err) {
  236. ret = -EFAULT;
  237. goto out;
  238. }
  239. pos += bytes_to_copy;
  240. str += bytes_to_copy;
  241. len -= bytes_to_copy;
  242. }
  243. }
  244. ret = 0;
  245. out:
  246. if (kmapped_page)
  247. kunmap(kmapped_page);
  248. return ret;
  249. }
  250. /*
  251. * Like copy_strings, but get argv and its values from kernel memory.
  252. */
  253. int copy_strings_kernel(int argc,char ** argv, struct linux_binprm *bprm)
  254. {
  255. int r;
  256. mm_segment_t oldfs = get_fs();
  257. set_fs(KERNEL_DS);
  258. r = copy_strings(argc, (char __user * __user *)argv, bprm);
  259. set_fs(oldfs);
  260. return r;
  261. }
  262. EXPORT_SYMBOL(copy_strings_kernel);
  263. #ifdef CONFIG_MMU
  264. /*
  265. * This routine is used to map in a page into an address space: needed by
  266. * execve() for the initial stack and environment pages.
  267. *
  268. * vma->vm_mm->mmap_sem is held for writing.
  269. */
  270. void install_arg_page(struct vm_area_struct *vma,
  271. struct page *page, unsigned long address)
  272. {
  273. struct mm_struct *mm = vma->vm_mm;
  274. pte_t * pte;
  275. spinlock_t *ptl;
  276. if (unlikely(anon_vma_prepare(vma)))
  277. goto out;
  278. flush_dcache_page(page);
  279. pte = get_locked_pte(mm, address, &ptl);
  280. if (!pte)
  281. goto out;
  282. if (!pte_none(*pte)) {
  283. pte_unmap_unlock(pte, ptl);
  284. goto out;
  285. }
  286. inc_mm_counter(mm, anon_rss);
  287. lru_cache_add_active(page);
  288. set_pte_at(mm, address, pte, pte_mkdirty(pte_mkwrite(mk_pte(
  289. page, vma->vm_page_prot))));
  290. page_add_new_anon_rmap(page, vma, address);
  291. pte_unmap_unlock(pte, ptl);
  292. /* no need for flush_tlb */
  293. return;
  294. out:
  295. __free_page(page);
  296. force_sig(SIGKILL, current);
  297. }
  298. #define EXTRA_STACK_VM_PAGES 20 /* random */
  299. int setup_arg_pages(struct linux_binprm *bprm,
  300. unsigned long stack_top,
  301. int executable_stack)
  302. {
  303. unsigned long stack_base;
  304. struct vm_area_struct *mpnt;
  305. struct mm_struct *mm = current->mm;
  306. int i, ret;
  307. long arg_size;
  308. #ifdef CONFIG_STACK_GROWSUP
  309. /* Move the argument and environment strings to the bottom of the
  310. * stack space.
  311. */
  312. int offset, j;
  313. char *to, *from;
  314. /* Start by shifting all the pages down */
  315. i = 0;
  316. for (j = 0; j < MAX_ARG_PAGES; j++) {
  317. struct page *page = bprm->page[j];
  318. if (!page)
  319. continue;
  320. bprm->page[i++] = page;
  321. }
  322. /* Now move them within their pages */
  323. offset = bprm->p % PAGE_SIZE;
  324. to = kmap(bprm->page[0]);
  325. for (j = 1; j < i; j++) {
  326. memmove(to, to + offset, PAGE_SIZE - offset);
  327. from = kmap(bprm->page[j]);
  328. memcpy(to + PAGE_SIZE - offset, from, offset);
  329. kunmap(bprm->page[j - 1]);
  330. to = from;
  331. }
  332. memmove(to, to + offset, PAGE_SIZE - offset);
  333. kunmap(bprm->page[j - 1]);
  334. /* Limit stack size to 1GB */
  335. stack_base = current->signal->rlim[RLIMIT_STACK].rlim_max;
  336. if (stack_base > (1 << 30))
  337. stack_base = 1 << 30;
  338. stack_base = PAGE_ALIGN(stack_top - stack_base);
  339. /* Adjust bprm->p to point to the end of the strings. */
  340. bprm->p = stack_base + PAGE_SIZE * i - offset;
  341. mm->arg_start = stack_base;
  342. arg_size = i << PAGE_SHIFT;
  343. /* zero pages that were copied above */
  344. while (i < MAX_ARG_PAGES)
  345. bprm->page[i++] = NULL;
  346. #else
  347. stack_base = arch_align_stack(stack_top - MAX_ARG_PAGES*PAGE_SIZE);
  348. stack_base = PAGE_ALIGN(stack_base);
  349. bprm->p += stack_base;
  350. mm->arg_start = bprm->p;
  351. arg_size = stack_top - (PAGE_MASK & (unsigned long) mm->arg_start);
  352. #endif
  353. arg_size += EXTRA_STACK_VM_PAGES * PAGE_SIZE;
  354. if (bprm->loader)
  355. bprm->loader += stack_base;
  356. bprm->exec += stack_base;
  357. mpnt = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
  358. if (!mpnt)
  359. return -ENOMEM;
  360. memset(mpnt, 0, sizeof(*mpnt));
  361. down_write(&mm->mmap_sem);
  362. {
  363. mpnt->vm_mm = mm;
  364. #ifdef CONFIG_STACK_GROWSUP
  365. mpnt->vm_start = stack_base;
  366. mpnt->vm_end = stack_base + arg_size;
  367. #else
  368. mpnt->vm_end = stack_top;
  369. mpnt->vm_start = mpnt->vm_end - arg_size;
  370. #endif
  371. /* Adjust stack execute permissions; explicitly enable
  372. * for EXSTACK_ENABLE_X, disable for EXSTACK_DISABLE_X
  373. * and leave alone (arch default) otherwise. */
  374. if (unlikely(executable_stack == EXSTACK_ENABLE_X))
  375. mpnt->vm_flags = VM_STACK_FLAGS | VM_EXEC;
  376. else if (executable_stack == EXSTACK_DISABLE_X)
  377. mpnt->vm_flags = VM_STACK_FLAGS & ~VM_EXEC;
  378. else
  379. mpnt->vm_flags = VM_STACK_FLAGS;
  380. mpnt->vm_flags |= mm->def_flags;
  381. mpnt->vm_page_prot = protection_map[mpnt->vm_flags & 0x7];
  382. if ((ret = insert_vm_struct(mm, mpnt))) {
  383. up_write(&mm->mmap_sem);
  384. kmem_cache_free(vm_area_cachep, mpnt);
  385. return ret;
  386. }
  387. mm->stack_vm = mm->total_vm = vma_pages(mpnt);
  388. }
  389. for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
  390. struct page *page = bprm->page[i];
  391. if (page) {
  392. bprm->page[i] = NULL;
  393. install_arg_page(mpnt, page, stack_base);
  394. }
  395. stack_base += PAGE_SIZE;
  396. }
  397. up_write(&mm->mmap_sem);
  398. return 0;
  399. }
  400. EXPORT_SYMBOL(setup_arg_pages);
  401. #define free_arg_pages(bprm) do { } while (0)
  402. #else
  403. static inline void free_arg_pages(struct linux_binprm *bprm)
  404. {
  405. int i;
  406. for (i = 0; i < MAX_ARG_PAGES; i++) {
  407. if (bprm->page[i])
  408. __free_page(bprm->page[i]);
  409. bprm->page[i] = NULL;
  410. }
  411. }
  412. #endif /* CONFIG_MMU */
  413. struct file *open_exec(const char *name)
  414. {
  415. struct nameidata nd;
  416. int err;
  417. struct file *file;
  418. err = path_lookup_open(AT_FDCWD, name, LOOKUP_FOLLOW, &nd, FMODE_READ|FMODE_EXEC);
  419. file = ERR_PTR(err);
  420. if (!err) {
  421. struct inode *inode = nd.dentry->d_inode;
  422. file = ERR_PTR(-EACCES);
  423. if (!(nd.mnt->mnt_flags & MNT_NOEXEC) &&
  424. S_ISREG(inode->i_mode)) {
  425. int err = vfs_permission(&nd, MAY_EXEC);
  426. if (!err && !(inode->i_mode & 0111))
  427. err = -EACCES;
  428. file = ERR_PTR(err);
  429. if (!err) {
  430. file = nameidata_to_filp(&nd, O_RDONLY);
  431. if (!IS_ERR(file)) {
  432. err = deny_write_access(file);
  433. if (err) {
  434. fput(file);
  435. file = ERR_PTR(err);
  436. }
  437. }
  438. out:
  439. return file;
  440. }
  441. }
  442. release_open_intent(&nd);
  443. path_release(&nd);
  444. }
  445. goto out;
  446. }
  447. EXPORT_SYMBOL(open_exec);
  448. int kernel_read(struct file *file, unsigned long offset,
  449. char *addr, unsigned long count)
  450. {
  451. mm_segment_t old_fs;
  452. loff_t pos = offset;
  453. int result;
  454. old_fs = get_fs();
  455. set_fs(get_ds());
  456. /* The cast to a user pointer is valid due to the set_fs() */
  457. result = vfs_read(file, (void __user *)addr, count, &pos);
  458. set_fs(old_fs);
  459. return result;
  460. }
  461. EXPORT_SYMBOL(kernel_read);
  462. static int exec_mmap(struct mm_struct *mm)
  463. {
  464. struct task_struct *tsk;
  465. struct mm_struct * old_mm, *active_mm;
  466. /* Notify parent that we're no longer interested in the old VM */
  467. tsk = current;
  468. old_mm = current->mm;
  469. mm_release(tsk, old_mm);
  470. if (old_mm) {
  471. /*
  472. * Make sure that if there is a core dump in progress
  473. * for the old mm, we get out and die instead of going
  474. * through with the exec. We must hold mmap_sem around
  475. * checking core_waiters and changing tsk->mm. The
  476. * core-inducing thread will increment core_waiters for
  477. * each thread whose ->mm == old_mm.
  478. */
  479. down_read(&old_mm->mmap_sem);
  480. if (unlikely(old_mm->core_waiters)) {
  481. up_read(&old_mm->mmap_sem);
  482. return -EINTR;
  483. }
  484. }
  485. task_lock(tsk);
  486. active_mm = tsk->active_mm;
  487. tsk->mm = mm;
  488. tsk->active_mm = mm;
  489. activate_mm(active_mm, mm);
  490. task_unlock(tsk);
  491. arch_pick_mmap_layout(mm);
  492. if (old_mm) {
  493. up_read(&old_mm->mmap_sem);
  494. BUG_ON(active_mm != old_mm);
  495. mmput(old_mm);
  496. return 0;
  497. }
  498. mmdrop(active_mm);
  499. return 0;
  500. }
  501. /*
  502. * This function makes sure the current process has its own signal table,
  503. * so that flush_signal_handlers can later reset the handlers without
  504. * disturbing other processes. (Other processes might share the signal
  505. * table via the CLONE_SIGHAND option to clone().)
  506. */
  507. static int de_thread(struct task_struct *tsk)
  508. {
  509. struct signal_struct *sig = tsk->signal;
  510. struct sighand_struct *newsighand, *oldsighand = tsk->sighand;
  511. spinlock_t *lock = &oldsighand->siglock;
  512. struct task_struct *leader = NULL;
  513. int count;
  514. /*
  515. * If we don't share sighandlers, then we aren't sharing anything
  516. * and we can just re-use it all.
  517. */
  518. if (atomic_read(&oldsighand->count) <= 1) {
  519. BUG_ON(atomic_read(&sig->count) != 1);
  520. exit_itimers(sig);
  521. return 0;
  522. }
  523. newsighand = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
  524. if (!newsighand)
  525. return -ENOMEM;
  526. if (thread_group_empty(current))
  527. goto no_thread_group;
  528. /*
  529. * Kill all other threads in the thread group.
  530. * We must hold tasklist_lock to call zap_other_threads.
  531. */
  532. read_lock(&tasklist_lock);
  533. spin_lock_irq(lock);
  534. if (sig->flags & SIGNAL_GROUP_EXIT) {
  535. /*
  536. * Another group action in progress, just
  537. * return so that the signal is processed.
  538. */
  539. spin_unlock_irq(lock);
  540. read_unlock(&tasklist_lock);
  541. kmem_cache_free(sighand_cachep, newsighand);
  542. return -EAGAIN;
  543. }
  544. /*
  545. * child_reaper ignores SIGKILL, change it now.
  546. * Reparenting needs write_lock on tasklist_lock,
  547. * so it is safe to do it under read_lock.
  548. */
  549. if (unlikely(current->group_leader == child_reaper))
  550. child_reaper = current;
  551. zap_other_threads(current);
  552. read_unlock(&tasklist_lock);
  553. /*
  554. * Account for the thread group leader hanging around:
  555. */
  556. count = 1;
  557. if (!thread_group_leader(current)) {
  558. count = 2;
  559. /*
  560. * The SIGALRM timer survives the exec, but needs to point
  561. * at us as the new group leader now. We have a race with
  562. * a timer firing now getting the old leader, so we need to
  563. * synchronize with any firing (by calling del_timer_sync)
  564. * before we can safely let the old group leader die.
  565. */
  566. sig->tsk = current;
  567. spin_unlock_irq(lock);
  568. if (hrtimer_cancel(&sig->real_timer))
  569. hrtimer_restart(&sig->real_timer);
  570. spin_lock_irq(lock);
  571. }
  572. while (atomic_read(&sig->count) > count) {
  573. sig->group_exit_task = current;
  574. sig->notify_count = count;
  575. __set_current_state(TASK_UNINTERRUPTIBLE);
  576. spin_unlock_irq(lock);
  577. schedule();
  578. spin_lock_irq(lock);
  579. }
  580. sig->group_exit_task = NULL;
  581. sig->notify_count = 0;
  582. spin_unlock_irq(lock);
  583. /*
  584. * At this point all other threads have exited, all we have to
  585. * do is to wait for the thread group leader to become inactive,
  586. * and to assume its PID:
  587. */
  588. if (!thread_group_leader(current)) {
  589. struct dentry *proc_dentry1, *proc_dentry2;
  590. /*
  591. * Wait for the thread group leader to be a zombie.
  592. * It should already be zombie at this point, most
  593. * of the time.
  594. */
  595. leader = current->group_leader;
  596. while (leader->exit_state != EXIT_ZOMBIE)
  597. yield();
  598. /*
  599. * The only record we have of the real-time age of a
  600. * process, regardless of execs it's done, is start_time.
  601. * All the past CPU time is accumulated in signal_struct
  602. * from sister threads now dead. But in this non-leader
  603. * exec, nothing survives from the original leader thread,
  604. * whose birth marks the true age of this process now.
  605. * When we take on its identity by switching to its PID, we
  606. * also take its birthdate (always earlier than our own).
  607. */
  608. current->start_time = leader->start_time;
  609. spin_lock(&leader->proc_lock);
  610. spin_lock(&current->proc_lock);
  611. proc_dentry1 = proc_pid_unhash(current);
  612. proc_dentry2 = proc_pid_unhash(leader);
  613. write_lock_irq(&tasklist_lock);
  614. BUG_ON(leader->tgid != current->tgid);
  615. BUG_ON(current->pid == current->tgid);
  616. /*
  617. * An exec() starts a new thread group with the
  618. * TGID of the previous thread group. Rehash the
  619. * two threads with a switched PID, and release
  620. * the former thread group leader:
  621. */
  622. /* Become a process group leader with the old leader's pid.
  623. * Note: The old leader also uses thispid until release_task
  624. * is called. Odd but simple and correct.
  625. */
  626. detach_pid(current, PIDTYPE_PID);
  627. current->pid = leader->pid;
  628. attach_pid(current, PIDTYPE_PID, current->pid);
  629. attach_pid(current, PIDTYPE_PGID, current->signal->pgrp);
  630. attach_pid(current, PIDTYPE_SID, current->signal->session);
  631. list_add_tail_rcu(&current->tasks, &init_task.tasks);
  632. current->group_leader = current;
  633. leader->group_leader = current;
  634. /* Reduce leader to a thread */
  635. detach_pid(leader, PIDTYPE_PGID);
  636. detach_pid(leader, PIDTYPE_SID);
  637. list_del_init(&leader->tasks);
  638. current->exit_signal = SIGCHLD;
  639. BUG_ON(leader->exit_state != EXIT_ZOMBIE);
  640. leader->exit_state = EXIT_DEAD;
  641. write_unlock_irq(&tasklist_lock);
  642. spin_unlock(&leader->proc_lock);
  643. spin_unlock(&current->proc_lock);
  644. proc_pid_flush(proc_dentry1);
  645. proc_pid_flush(proc_dentry2);
  646. }
  647. /*
  648. * There may be one thread left which is just exiting,
  649. * but it's safe to stop telling the group to kill themselves.
  650. */
  651. sig->flags = 0;
  652. no_thread_group:
  653. exit_itimers(sig);
  654. if (leader)
  655. release_task(leader);
  656. BUG_ON(atomic_read(&sig->count) != 1);
  657. if (atomic_read(&oldsighand->count) == 1) {
  658. /*
  659. * Now that we nuked the rest of the thread group,
  660. * it turns out we are not sharing sighand any more either.
  661. * So we can just keep it.
  662. */
  663. kmem_cache_free(sighand_cachep, newsighand);
  664. } else {
  665. /*
  666. * Move our state over to newsighand and switch it in.
  667. */
  668. atomic_set(&newsighand->count, 1);
  669. memcpy(newsighand->action, oldsighand->action,
  670. sizeof(newsighand->action));
  671. write_lock_irq(&tasklist_lock);
  672. spin_lock(&oldsighand->siglock);
  673. spin_lock(&newsighand->siglock);
  674. rcu_assign_pointer(current->sighand, newsighand);
  675. recalc_sigpending();
  676. spin_unlock(&newsighand->siglock);
  677. spin_unlock(&oldsighand->siglock);
  678. write_unlock_irq(&tasklist_lock);
  679. if (atomic_dec_and_test(&oldsighand->count))
  680. kmem_cache_free(sighand_cachep, oldsighand);
  681. }
  682. BUG_ON(!thread_group_leader(current));
  683. return 0;
  684. }
  685. /*
  686. * These functions flushes out all traces of the currently running executable
  687. * so that a new one can be started
  688. */
  689. static void flush_old_files(struct files_struct * files)
  690. {
  691. long j = -1;
  692. struct fdtable *fdt;
  693. spin_lock(&files->file_lock);
  694. for (;;) {
  695. unsigned long set, i;
  696. j++;
  697. i = j * __NFDBITS;
  698. fdt = files_fdtable(files);
  699. if (i >= fdt->max_fds || i >= fdt->max_fdset)
  700. break;
  701. set = fdt->close_on_exec->fds_bits[j];
  702. if (!set)
  703. continue;
  704. fdt->close_on_exec->fds_bits[j] = 0;
  705. spin_unlock(&files->file_lock);
  706. for ( ; set ; i++,set >>= 1) {
  707. if (set & 1) {
  708. sys_close(i);
  709. }
  710. }
  711. spin_lock(&files->file_lock);
  712. }
  713. spin_unlock(&files->file_lock);
  714. }
  715. void get_task_comm(char *buf, struct task_struct *tsk)
  716. {
  717. /* buf must be at least sizeof(tsk->comm) in size */
  718. task_lock(tsk);
  719. strncpy(buf, tsk->comm, sizeof(tsk->comm));
  720. task_unlock(tsk);
  721. }
  722. void set_task_comm(struct task_struct *tsk, char *buf)
  723. {
  724. task_lock(tsk);
  725. strlcpy(tsk->comm, buf, sizeof(tsk->comm));
  726. task_unlock(tsk);
  727. }
  728. int flush_old_exec(struct linux_binprm * bprm)
  729. {
  730. char * name;
  731. int i, ch, retval;
  732. struct files_struct *files;
  733. char tcomm[sizeof(current->comm)];
  734. /*
  735. * Make sure we have a private signal table and that
  736. * we are unassociated from the previous thread group.
  737. */
  738. retval = de_thread(current);
  739. if (retval)
  740. goto out;
  741. /*
  742. * Make sure we have private file handles. Ask the
  743. * fork helper to do the work for us and the exit
  744. * helper to do the cleanup of the old one.
  745. */
  746. files = current->files; /* refcounted so safe to hold */
  747. retval = unshare_files();
  748. if (retval)
  749. goto out;
  750. /*
  751. * Release all of the old mmap stuff
  752. */
  753. retval = exec_mmap(bprm->mm);
  754. if (retval)
  755. goto mmap_failed;
  756. bprm->mm = NULL; /* We're using it now */
  757. /* This is the point of no return */
  758. steal_locks(files);
  759. put_files_struct(files);
  760. current->sas_ss_sp = current->sas_ss_size = 0;
  761. if (current->euid == current->uid && current->egid == current->gid)
  762. current->mm->dumpable = 1;
  763. else
  764. current->mm->dumpable = suid_dumpable;
  765. name = bprm->filename;
  766. /* Copies the binary name from after last slash */
  767. for (i=0; (ch = *(name++)) != '\0';) {
  768. if (ch == '/')
  769. i = 0; /* overwrite what we wrote */
  770. else
  771. if (i < (sizeof(tcomm) - 1))
  772. tcomm[i++] = ch;
  773. }
  774. tcomm[i] = '\0';
  775. set_task_comm(current, tcomm);
  776. current->flags &= ~PF_RANDOMIZE;
  777. flush_thread();
  778. /* Set the new mm task size. We have to do that late because it may
  779. * depend on TIF_32BIT which is only updated in flush_thread() on
  780. * some architectures like powerpc
  781. */
  782. current->mm->task_size = TASK_SIZE;
  783. if (bprm->e_uid != current->euid || bprm->e_gid != current->egid ||
  784. file_permission(bprm->file, MAY_READ) ||
  785. (bprm->interp_flags & BINPRM_FLAGS_ENFORCE_NONDUMP)) {
  786. suid_keys(current);
  787. current->mm->dumpable = suid_dumpable;
  788. }
  789. /* An exec changes our domain. We are no longer part of the thread
  790. group */
  791. current->self_exec_id++;
  792. flush_signal_handlers(current, 0);
  793. flush_old_files(current->files);
  794. return 0;
  795. mmap_failed:
  796. put_files_struct(current->files);
  797. current->files = files;
  798. out:
  799. return retval;
  800. }
  801. EXPORT_SYMBOL(flush_old_exec);
  802. /*
  803. * Fill the binprm structure from the inode.
  804. * Check permissions, then read the first 128 (BINPRM_BUF_SIZE) bytes
  805. */
  806. int prepare_binprm(struct linux_binprm *bprm)
  807. {
  808. int mode;
  809. struct inode * inode = bprm->file->f_dentry->d_inode;
  810. int retval;
  811. mode = inode->i_mode;
  812. /*
  813. * Check execute perms again - if the caller has CAP_DAC_OVERRIDE,
  814. * generic_permission lets a non-executable through
  815. */
  816. if (!(mode & 0111)) /* with at least _one_ execute bit set */
  817. return -EACCES;
  818. if (bprm->file->f_op == NULL)
  819. return -EACCES;
  820. bprm->e_uid = current->euid;
  821. bprm->e_gid = current->egid;
  822. if(!(bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID)) {
  823. /* Set-uid? */
  824. if (mode & S_ISUID) {
  825. current->personality &= ~PER_CLEAR_ON_SETID;
  826. bprm->e_uid = inode->i_uid;
  827. }
  828. /* Set-gid? */
  829. /*
  830. * If setgid is set but no group execute bit then this
  831. * is a candidate for mandatory locking, not a setgid
  832. * executable.
  833. */
  834. if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
  835. current->personality &= ~PER_CLEAR_ON_SETID;
  836. bprm->e_gid = inode->i_gid;
  837. }
  838. }
  839. /* fill in binprm security blob */
  840. retval = security_bprm_set(bprm);
  841. if (retval)
  842. return retval;
  843. memset(bprm->buf,0,BINPRM_BUF_SIZE);
  844. return kernel_read(bprm->file,0,bprm->buf,BINPRM_BUF_SIZE);
  845. }
  846. EXPORT_SYMBOL(prepare_binprm);
  847. static int unsafe_exec(struct task_struct *p)
  848. {
  849. int unsafe = 0;
  850. if (p->ptrace & PT_PTRACED) {
  851. if (p->ptrace & PT_PTRACE_CAP)
  852. unsafe |= LSM_UNSAFE_PTRACE_CAP;
  853. else
  854. unsafe |= LSM_UNSAFE_PTRACE;
  855. }
  856. if (atomic_read(&p->fs->count) > 1 ||
  857. atomic_read(&p->files->count) > 1 ||
  858. atomic_read(&p->sighand->count) > 1)
  859. unsafe |= LSM_UNSAFE_SHARE;
  860. return unsafe;
  861. }
  862. void compute_creds(struct linux_binprm *bprm)
  863. {
  864. int unsafe;
  865. if (bprm->e_uid != current->uid)
  866. suid_keys(current);
  867. exec_keys(current);
  868. task_lock(current);
  869. unsafe = unsafe_exec(current);
  870. security_bprm_apply_creds(bprm, unsafe);
  871. task_unlock(current);
  872. security_bprm_post_apply_creds(bprm);
  873. }
  874. EXPORT_SYMBOL(compute_creds);
  875. void remove_arg_zero(struct linux_binprm *bprm)
  876. {
  877. if (bprm->argc) {
  878. unsigned long offset;
  879. char * kaddr;
  880. struct page *page;
  881. offset = bprm->p % PAGE_SIZE;
  882. goto inside;
  883. while (bprm->p++, *(kaddr+offset++)) {
  884. if (offset != PAGE_SIZE)
  885. continue;
  886. offset = 0;
  887. kunmap_atomic(kaddr, KM_USER0);
  888. inside:
  889. page = bprm->page[bprm->p/PAGE_SIZE];
  890. kaddr = kmap_atomic(page, KM_USER0);
  891. }
  892. kunmap_atomic(kaddr, KM_USER0);
  893. bprm->argc--;
  894. }
  895. }
  896. EXPORT_SYMBOL(remove_arg_zero);
  897. /*
  898. * cycle the list of binary formats handler, until one recognizes the image
  899. */
  900. int search_binary_handler(struct linux_binprm *bprm,struct pt_regs *regs)
  901. {
  902. int try,retval;
  903. struct linux_binfmt *fmt;
  904. #ifdef __alpha__
  905. /* handle /sbin/loader.. */
  906. {
  907. struct exec * eh = (struct exec *) bprm->buf;
  908. if (!bprm->loader && eh->fh.f_magic == 0x183 &&
  909. (eh->fh.f_flags & 0x3000) == 0x3000)
  910. {
  911. struct file * file;
  912. unsigned long loader;
  913. allow_write_access(bprm->file);
  914. fput(bprm->file);
  915. bprm->file = NULL;
  916. loader = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
  917. file = open_exec("/sbin/loader");
  918. retval = PTR_ERR(file);
  919. if (IS_ERR(file))
  920. return retval;
  921. /* Remember if the application is TASO. */
  922. bprm->sh_bang = eh->ah.entry < 0x100000000UL;
  923. bprm->file = file;
  924. bprm->loader = loader;
  925. retval = prepare_binprm(bprm);
  926. if (retval<0)
  927. return retval;
  928. /* should call search_binary_handler recursively here,
  929. but it does not matter */
  930. }
  931. }
  932. #endif
  933. retval = security_bprm_check(bprm);
  934. if (retval)
  935. return retval;
  936. /* kernel module loader fixup */
  937. /* so we don't try to load run modprobe in kernel space. */
  938. set_fs(USER_DS);
  939. retval = audit_bprm(bprm);
  940. if (retval)
  941. return retval;
  942. retval = -ENOENT;
  943. for (try=0; try<2; try++) {
  944. read_lock(&binfmt_lock);
  945. for (fmt = formats ; fmt ; fmt = fmt->next) {
  946. int (*fn)(struct linux_binprm *, struct pt_regs *) = fmt->load_binary;
  947. if (!fn)
  948. continue;
  949. if (!try_module_get(fmt->module))
  950. continue;
  951. read_unlock(&binfmt_lock);
  952. retval = fn(bprm, regs);
  953. if (retval >= 0) {
  954. put_binfmt(fmt);
  955. allow_write_access(bprm->file);
  956. if (bprm->file)
  957. fput(bprm->file);
  958. bprm->file = NULL;
  959. current->did_exec = 1;
  960. proc_exec_connector(current);
  961. return retval;
  962. }
  963. read_lock(&binfmt_lock);
  964. put_binfmt(fmt);
  965. if (retval != -ENOEXEC || bprm->mm == NULL)
  966. break;
  967. if (!bprm->file) {
  968. read_unlock(&binfmt_lock);
  969. return retval;
  970. }
  971. }
  972. read_unlock(&binfmt_lock);
  973. if (retval != -ENOEXEC || bprm->mm == NULL) {
  974. break;
  975. #ifdef CONFIG_KMOD
  976. }else{
  977. #define printable(c) (((c)=='\t') || ((c)=='\n') || (0x20<=(c) && (c)<=0x7e))
  978. if (printable(bprm->buf[0]) &&
  979. printable(bprm->buf[1]) &&
  980. printable(bprm->buf[2]) &&
  981. printable(bprm->buf[3]))
  982. break; /* -ENOEXEC */
  983. request_module("binfmt-%04x", *(unsigned short *)(&bprm->buf[2]));
  984. #endif
  985. }
  986. }
  987. return retval;
  988. }
  989. EXPORT_SYMBOL(search_binary_handler);
  990. /*
  991. * sys_execve() executes a new program.
  992. */
  993. int do_execve(char * filename,
  994. char __user *__user *argv,
  995. char __user *__user *envp,
  996. struct pt_regs * regs)
  997. {
  998. struct linux_binprm *bprm;
  999. struct file *file;
  1000. int retval;
  1001. int i;
  1002. retval = -ENOMEM;
  1003. bprm = kzalloc(sizeof(*bprm), GFP_KERNEL);
  1004. if (!bprm)
  1005. goto out_ret;
  1006. file = open_exec(filename);
  1007. retval = PTR_ERR(file);
  1008. if (IS_ERR(file))
  1009. goto out_kfree;
  1010. sched_exec();
  1011. bprm->p = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
  1012. bprm->file = file;
  1013. bprm->filename = filename;
  1014. bprm->interp = filename;
  1015. bprm->mm = mm_alloc();
  1016. retval = -ENOMEM;
  1017. if (!bprm->mm)
  1018. goto out_file;
  1019. retval = init_new_context(current, bprm->mm);
  1020. if (retval < 0)
  1021. goto out_mm;
  1022. bprm->argc = count(argv, bprm->p / sizeof(void *));
  1023. if ((retval = bprm->argc) < 0)
  1024. goto out_mm;
  1025. bprm->envc = count(envp, bprm->p / sizeof(void *));
  1026. if ((retval = bprm->envc) < 0)
  1027. goto out_mm;
  1028. retval = security_bprm_alloc(bprm);
  1029. if (retval)
  1030. goto out;
  1031. retval = prepare_binprm(bprm);
  1032. if (retval < 0)
  1033. goto out;
  1034. retval = copy_strings_kernel(1, &bprm->filename, bprm);
  1035. if (retval < 0)
  1036. goto out;
  1037. bprm->exec = bprm->p;
  1038. retval = copy_strings(bprm->envc, envp, bprm);
  1039. if (retval < 0)
  1040. goto out;
  1041. retval = copy_strings(bprm->argc, argv, bprm);
  1042. if (retval < 0)
  1043. goto out;
  1044. retval = search_binary_handler(bprm,regs);
  1045. if (retval >= 0) {
  1046. free_arg_pages(bprm);
  1047. /* execve success */
  1048. security_bprm_free(bprm);
  1049. acct_update_integrals(current);
  1050. kfree(bprm);
  1051. return retval;
  1052. }
  1053. out:
  1054. /* Something went wrong, return the inode and free the argument pages*/
  1055. for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
  1056. struct page * page = bprm->page[i];
  1057. if (page)
  1058. __free_page(page);
  1059. }
  1060. if (bprm->security)
  1061. security_bprm_free(bprm);
  1062. out_mm:
  1063. if (bprm->mm)
  1064. mmdrop(bprm->mm);
  1065. out_file:
  1066. if (bprm->file) {
  1067. allow_write_access(bprm->file);
  1068. fput(bprm->file);
  1069. }
  1070. out_kfree:
  1071. kfree(bprm);
  1072. out_ret:
  1073. return retval;
  1074. }
  1075. int set_binfmt(struct linux_binfmt *new)
  1076. {
  1077. struct linux_binfmt *old = current->binfmt;
  1078. if (new) {
  1079. if (!try_module_get(new->module))
  1080. return -1;
  1081. }
  1082. current->binfmt = new;
  1083. if (old)
  1084. module_put(old->module);
  1085. return 0;
  1086. }
  1087. EXPORT_SYMBOL(set_binfmt);
  1088. #define CORENAME_MAX_SIZE 64
  1089. /* format_corename will inspect the pattern parameter, and output a
  1090. * name into corename, which must have space for at least
  1091. * CORENAME_MAX_SIZE bytes plus one byte for the zero terminator.
  1092. */
  1093. static void format_corename(char *corename, const char *pattern, long signr)
  1094. {
  1095. const char *pat_ptr = pattern;
  1096. char *out_ptr = corename;
  1097. char *const out_end = corename + CORENAME_MAX_SIZE;
  1098. int rc;
  1099. int pid_in_pattern = 0;
  1100. /* Repeat as long as we have more pattern to process and more output
  1101. space */
  1102. while (*pat_ptr) {
  1103. if (*pat_ptr != '%') {
  1104. if (out_ptr == out_end)
  1105. goto out;
  1106. *out_ptr++ = *pat_ptr++;
  1107. } else {
  1108. switch (*++pat_ptr) {
  1109. case 0:
  1110. goto out;
  1111. /* Double percent, output one percent */
  1112. case '%':
  1113. if (out_ptr == out_end)
  1114. goto out;
  1115. *out_ptr++ = '%';
  1116. break;
  1117. /* pid */
  1118. case 'p':
  1119. pid_in_pattern = 1;
  1120. rc = snprintf(out_ptr, out_end - out_ptr,
  1121. "%d", current->tgid);
  1122. if (rc > out_end - out_ptr)
  1123. goto out;
  1124. out_ptr += rc;
  1125. break;
  1126. /* uid */
  1127. case 'u':
  1128. rc = snprintf(out_ptr, out_end - out_ptr,
  1129. "%d", current->uid);
  1130. if (rc > out_end - out_ptr)
  1131. goto out;
  1132. out_ptr += rc;
  1133. break;
  1134. /* gid */
  1135. case 'g':
  1136. rc = snprintf(out_ptr, out_end - out_ptr,
  1137. "%d", current->gid);
  1138. if (rc > out_end - out_ptr)
  1139. goto out;
  1140. out_ptr += rc;
  1141. break;
  1142. /* signal that caused the coredump */
  1143. case 's':
  1144. rc = snprintf(out_ptr, out_end - out_ptr,
  1145. "%ld", signr);
  1146. if (rc > out_end - out_ptr)
  1147. goto out;
  1148. out_ptr += rc;
  1149. break;
  1150. /* UNIX time of coredump */
  1151. case 't': {
  1152. struct timeval tv;
  1153. do_gettimeofday(&tv);
  1154. rc = snprintf(out_ptr, out_end - out_ptr,
  1155. "%lu", tv.tv_sec);
  1156. if (rc > out_end - out_ptr)
  1157. goto out;
  1158. out_ptr += rc;
  1159. break;
  1160. }
  1161. /* hostname */
  1162. case 'h':
  1163. down_read(&uts_sem);
  1164. rc = snprintf(out_ptr, out_end - out_ptr,
  1165. "%s", system_utsname.nodename);
  1166. up_read(&uts_sem);
  1167. if (rc > out_end - out_ptr)
  1168. goto out;
  1169. out_ptr += rc;
  1170. break;
  1171. /* executable */
  1172. case 'e':
  1173. rc = snprintf(out_ptr, out_end - out_ptr,
  1174. "%s", current->comm);
  1175. if (rc > out_end - out_ptr)
  1176. goto out;
  1177. out_ptr += rc;
  1178. break;
  1179. default:
  1180. break;
  1181. }
  1182. ++pat_ptr;
  1183. }
  1184. }
  1185. /* Backward compatibility with core_uses_pid:
  1186. *
  1187. * If core_pattern does not include a %p (as is the default)
  1188. * and core_uses_pid is set, then .%pid will be appended to
  1189. * the filename */
  1190. if (!pid_in_pattern
  1191. && (core_uses_pid || atomic_read(&current->mm->mm_users) != 1)) {
  1192. rc = snprintf(out_ptr, out_end - out_ptr,
  1193. ".%d", current->tgid);
  1194. if (rc > out_end - out_ptr)
  1195. goto out;
  1196. out_ptr += rc;
  1197. }
  1198. out:
  1199. *out_ptr = 0;
  1200. }
  1201. static void zap_threads (struct mm_struct *mm)
  1202. {
  1203. struct task_struct *g, *p;
  1204. struct task_struct *tsk = current;
  1205. struct completion *vfork_done = tsk->vfork_done;
  1206. int traced = 0;
  1207. /*
  1208. * Make sure nobody is waiting for us to release the VM,
  1209. * otherwise we can deadlock when we wait on each other
  1210. */
  1211. if (vfork_done) {
  1212. tsk->vfork_done = NULL;
  1213. complete(vfork_done);
  1214. }
  1215. read_lock(&tasklist_lock);
  1216. do_each_thread(g,p)
  1217. if (mm == p->mm && p != tsk) {
  1218. force_sig_specific(SIGKILL, p);
  1219. mm->core_waiters++;
  1220. if (unlikely(p->ptrace) &&
  1221. unlikely(p->parent->mm == mm))
  1222. traced = 1;
  1223. }
  1224. while_each_thread(g,p);
  1225. read_unlock(&tasklist_lock);
  1226. if (unlikely(traced)) {
  1227. /*
  1228. * We are zapping a thread and the thread it ptraces.
  1229. * If the tracee went into a ptrace stop for exit tracing,
  1230. * we could deadlock since the tracer is waiting for this
  1231. * coredump to finish. Detach them so they can both die.
  1232. */
  1233. write_lock_irq(&tasklist_lock);
  1234. do_each_thread(g,p) {
  1235. if (mm == p->mm && p != tsk &&
  1236. p->ptrace && p->parent->mm == mm) {
  1237. __ptrace_detach(p, 0);
  1238. }
  1239. } while_each_thread(g,p);
  1240. write_unlock_irq(&tasklist_lock);
  1241. }
  1242. }
  1243. static void coredump_wait(struct mm_struct *mm)
  1244. {
  1245. DECLARE_COMPLETION(startup_done);
  1246. int core_waiters;
  1247. mm->core_startup_done = &startup_done;
  1248. zap_threads(mm);
  1249. core_waiters = mm->core_waiters;
  1250. up_write(&mm->mmap_sem);
  1251. if (core_waiters)
  1252. wait_for_completion(&startup_done);
  1253. BUG_ON(mm->core_waiters);
  1254. }
  1255. int do_coredump(long signr, int exit_code, struct pt_regs * regs)
  1256. {
  1257. char corename[CORENAME_MAX_SIZE + 1];
  1258. struct mm_struct *mm = current->mm;
  1259. struct linux_binfmt * binfmt;
  1260. struct inode * inode;
  1261. struct file * file;
  1262. int retval = 0;
  1263. int fsuid = current->fsuid;
  1264. int flag = 0;
  1265. binfmt = current->binfmt;
  1266. if (!binfmt || !binfmt->core_dump)
  1267. goto fail;
  1268. down_write(&mm->mmap_sem);
  1269. if (!mm->dumpable) {
  1270. up_write(&mm->mmap_sem);
  1271. goto fail;
  1272. }
  1273. /*
  1274. * We cannot trust fsuid as being the "true" uid of the
  1275. * process nor do we know its entire history. We only know it
  1276. * was tainted so we dump it as root in mode 2.
  1277. */
  1278. if (mm->dumpable == 2) { /* Setuid core dump mode */
  1279. flag = O_EXCL; /* Stop rewrite attacks */
  1280. current->fsuid = 0; /* Dump root private */
  1281. }
  1282. mm->dumpable = 0;
  1283. retval = -EAGAIN;
  1284. spin_lock_irq(&current->sighand->siglock);
  1285. if (!(current->signal->flags & SIGNAL_GROUP_EXIT)) {
  1286. current->signal->flags = SIGNAL_GROUP_EXIT;
  1287. current->signal->group_exit_code = exit_code;
  1288. current->signal->group_stop_count = 0;
  1289. retval = 0;
  1290. }
  1291. spin_unlock_irq(&current->sighand->siglock);
  1292. if (retval) {
  1293. up_write(&mm->mmap_sem);
  1294. goto fail;
  1295. }
  1296. init_completion(&mm->core_done);
  1297. coredump_wait(mm);
  1298. /*
  1299. * Clear any false indication of pending signals that might
  1300. * be seen by the filesystem code called to write the core file.
  1301. */
  1302. clear_thread_flag(TIF_SIGPENDING);
  1303. if (current->signal->rlim[RLIMIT_CORE].rlim_cur < binfmt->min_coredump)
  1304. goto fail_unlock;
  1305. /*
  1306. * lock_kernel() because format_corename() is controlled by sysctl, which
  1307. * uses lock_kernel()
  1308. */
  1309. lock_kernel();
  1310. format_corename(corename, core_pattern, signr);
  1311. unlock_kernel();
  1312. file = filp_open(corename, O_CREAT | 2 | O_NOFOLLOW | O_LARGEFILE | flag, 0600);
  1313. if (IS_ERR(file))
  1314. goto fail_unlock;
  1315. inode = file->f_dentry->d_inode;
  1316. if (inode->i_nlink > 1)
  1317. goto close_fail; /* multiple links - don't dump */
  1318. if (d_unhashed(file->f_dentry))
  1319. goto close_fail;
  1320. if (!S_ISREG(inode->i_mode))
  1321. goto close_fail;
  1322. if (!file->f_op)
  1323. goto close_fail;
  1324. if (!file->f_op->write)
  1325. goto close_fail;
  1326. if (do_truncate(file->f_dentry, 0, 0, file) != 0)
  1327. goto close_fail;
  1328. retval = binfmt->core_dump(signr, regs, file);
  1329. if (retval)
  1330. current->signal->group_exit_code |= 0x80;
  1331. close_fail:
  1332. filp_close(file, NULL);
  1333. fail_unlock:
  1334. current->fsuid = fsuid;
  1335. complete_all(&mm->core_done);
  1336. fail:
  1337. return retval;
  1338. }