exec.c 35 KB

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