exec.c 35 KB

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