exec.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560
  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 task_struct *parent;
  589. struct dentry *proc_dentry1, *proc_dentry2;
  590. unsigned long ptrace;
  591. /*
  592. * Wait for the thread group leader to be a zombie.
  593. * It should already be zombie at this point, most
  594. * of the time.
  595. */
  596. leader = current->group_leader;
  597. while (leader->exit_state != EXIT_ZOMBIE)
  598. yield();
  599. /*
  600. * The only record we have of the real-time age of a
  601. * process, regardless of execs it's done, is start_time.
  602. * All the past CPU time is accumulated in signal_struct
  603. * from sister threads now dead. But in this non-leader
  604. * exec, nothing survives from the original leader thread,
  605. * whose birth marks the true age of this process now.
  606. * When we take on its identity by switching to its PID, we
  607. * also take its birthdate (always earlier than our own).
  608. */
  609. current->start_time = leader->start_time;
  610. spin_lock(&leader->proc_lock);
  611. spin_lock(&current->proc_lock);
  612. proc_dentry1 = proc_pid_unhash(current);
  613. proc_dentry2 = proc_pid_unhash(leader);
  614. write_lock_irq(&tasklist_lock);
  615. BUG_ON(leader->tgid != current->tgid);
  616. BUG_ON(current->pid == current->tgid);
  617. /*
  618. * An exec() starts a new thread group with the
  619. * TGID of the previous thread group. Rehash the
  620. * two threads with a switched PID, and release
  621. * the former thread group leader:
  622. */
  623. ptrace = leader->ptrace;
  624. parent = leader->parent;
  625. if (unlikely(ptrace) && unlikely(parent == current)) {
  626. /*
  627. * Joker was ptracing his own group leader,
  628. * and now he wants to be his own parent!
  629. * We can't have that.
  630. */
  631. ptrace = 0;
  632. }
  633. ptrace_unlink(current);
  634. ptrace_unlink(leader);
  635. remove_parent(current);
  636. remove_parent(leader);
  637. /* Become a process group leader with the old leader's pid.
  638. * Note: The old leader also uses thispid until release_task
  639. * is called. Odd but simple and correct.
  640. */
  641. detach_pid(current, PIDTYPE_PID);
  642. current->pid = leader->pid;
  643. attach_pid(current, PIDTYPE_PID, current->pid);
  644. attach_pid(current, PIDTYPE_PGID, current->signal->pgrp);
  645. attach_pid(current, PIDTYPE_SID, current->signal->session);
  646. list_add_tail(&current->tasks, &init_task.tasks);
  647. current->parent = current->real_parent = leader->real_parent;
  648. leader->parent = leader->real_parent = child_reaper;
  649. current->group_leader = current;
  650. leader->group_leader = current;
  651. /* Reduce leader to a thread */
  652. detach_pid(leader, PIDTYPE_PGID);
  653. detach_pid(leader, PIDTYPE_SID);
  654. list_del_init(&leader->tasks);
  655. add_parent(current);
  656. add_parent(leader);
  657. if (ptrace) {
  658. current->ptrace = ptrace;
  659. __ptrace_link(current, parent);
  660. }
  661. current->exit_signal = SIGCHLD;
  662. BUG_ON(leader->exit_state != EXIT_ZOMBIE);
  663. leader->exit_state = EXIT_DEAD;
  664. write_unlock_irq(&tasklist_lock);
  665. spin_unlock(&leader->proc_lock);
  666. spin_unlock(&current->proc_lock);
  667. proc_pid_flush(proc_dentry1);
  668. proc_pid_flush(proc_dentry2);
  669. }
  670. /*
  671. * There may be one thread left which is just exiting,
  672. * but it's safe to stop telling the group to kill themselves.
  673. */
  674. sig->flags = 0;
  675. no_thread_group:
  676. exit_itimers(sig);
  677. if (leader)
  678. release_task(leader);
  679. BUG_ON(atomic_read(&sig->count) != 1);
  680. if (atomic_read(&oldsighand->count) == 1) {
  681. /*
  682. * Now that we nuked the rest of the thread group,
  683. * it turns out we are not sharing sighand any more either.
  684. * So we can just keep it.
  685. */
  686. kmem_cache_free(sighand_cachep, newsighand);
  687. } else {
  688. /*
  689. * Move our state over to newsighand and switch it in.
  690. */
  691. atomic_set(&newsighand->count, 1);
  692. memcpy(newsighand->action, oldsighand->action,
  693. sizeof(newsighand->action));
  694. write_lock_irq(&tasklist_lock);
  695. spin_lock(&oldsighand->siglock);
  696. spin_lock(&newsighand->siglock);
  697. rcu_assign_pointer(current->sighand, newsighand);
  698. recalc_sigpending();
  699. spin_unlock(&newsighand->siglock);
  700. spin_unlock(&oldsighand->siglock);
  701. write_unlock_irq(&tasklist_lock);
  702. if (atomic_dec_and_test(&oldsighand->count))
  703. kmem_cache_free(sighand_cachep, oldsighand);
  704. }
  705. BUG_ON(!thread_group_leader(current));
  706. return 0;
  707. }
  708. /*
  709. * These functions flushes out all traces of the currently running executable
  710. * so that a new one can be started
  711. */
  712. static void flush_old_files(struct files_struct * files)
  713. {
  714. long j = -1;
  715. struct fdtable *fdt;
  716. spin_lock(&files->file_lock);
  717. for (;;) {
  718. unsigned long set, i;
  719. j++;
  720. i = j * __NFDBITS;
  721. fdt = files_fdtable(files);
  722. if (i >= fdt->max_fds || i >= fdt->max_fdset)
  723. break;
  724. set = fdt->close_on_exec->fds_bits[j];
  725. if (!set)
  726. continue;
  727. fdt->close_on_exec->fds_bits[j] = 0;
  728. spin_unlock(&files->file_lock);
  729. for ( ; set ; i++,set >>= 1) {
  730. if (set & 1) {
  731. sys_close(i);
  732. }
  733. }
  734. spin_lock(&files->file_lock);
  735. }
  736. spin_unlock(&files->file_lock);
  737. }
  738. void get_task_comm(char *buf, struct task_struct *tsk)
  739. {
  740. /* buf must be at least sizeof(tsk->comm) in size */
  741. task_lock(tsk);
  742. strncpy(buf, tsk->comm, sizeof(tsk->comm));
  743. task_unlock(tsk);
  744. }
  745. void set_task_comm(struct task_struct *tsk, char *buf)
  746. {
  747. task_lock(tsk);
  748. strlcpy(tsk->comm, buf, sizeof(tsk->comm));
  749. task_unlock(tsk);
  750. }
  751. int flush_old_exec(struct linux_binprm * bprm)
  752. {
  753. char * name;
  754. int i, ch, retval;
  755. struct files_struct *files;
  756. char tcomm[sizeof(current->comm)];
  757. /*
  758. * Make sure we have a private signal table and that
  759. * we are unassociated from the previous thread group.
  760. */
  761. retval = de_thread(current);
  762. if (retval)
  763. goto out;
  764. /*
  765. * Make sure we have private file handles. Ask the
  766. * fork helper to do the work for us and the exit
  767. * helper to do the cleanup of the old one.
  768. */
  769. files = current->files; /* refcounted so safe to hold */
  770. retval = unshare_files();
  771. if (retval)
  772. goto out;
  773. /*
  774. * Release all of the old mmap stuff
  775. */
  776. retval = exec_mmap(bprm->mm);
  777. if (retval)
  778. goto mmap_failed;
  779. bprm->mm = NULL; /* We're using it now */
  780. /* This is the point of no return */
  781. steal_locks(files);
  782. put_files_struct(files);
  783. current->sas_ss_sp = current->sas_ss_size = 0;
  784. if (current->euid == current->uid && current->egid == current->gid)
  785. current->mm->dumpable = 1;
  786. else
  787. current->mm->dumpable = suid_dumpable;
  788. name = bprm->filename;
  789. /* Copies the binary name from after last slash */
  790. for (i=0; (ch = *(name++)) != '\0';) {
  791. if (ch == '/')
  792. i = 0; /* overwrite what we wrote */
  793. else
  794. if (i < (sizeof(tcomm) - 1))
  795. tcomm[i++] = ch;
  796. }
  797. tcomm[i] = '\0';
  798. set_task_comm(current, tcomm);
  799. current->flags &= ~PF_RANDOMIZE;
  800. flush_thread();
  801. /* Set the new mm task size. We have to do that late because it may
  802. * depend on TIF_32BIT which is only updated in flush_thread() on
  803. * some architectures like powerpc
  804. */
  805. current->mm->task_size = TASK_SIZE;
  806. if (bprm->e_uid != current->euid || bprm->e_gid != current->egid ||
  807. file_permission(bprm->file, MAY_READ) ||
  808. (bprm->interp_flags & BINPRM_FLAGS_ENFORCE_NONDUMP)) {
  809. suid_keys(current);
  810. current->mm->dumpable = suid_dumpable;
  811. }
  812. /* An exec changes our domain. We are no longer part of the thread
  813. group */
  814. current->self_exec_id++;
  815. flush_signal_handlers(current, 0);
  816. flush_old_files(current->files);
  817. return 0;
  818. mmap_failed:
  819. put_files_struct(current->files);
  820. current->files = files;
  821. out:
  822. return retval;
  823. }
  824. EXPORT_SYMBOL(flush_old_exec);
  825. /*
  826. * Fill the binprm structure from the inode.
  827. * Check permissions, then read the first 128 (BINPRM_BUF_SIZE) bytes
  828. */
  829. int prepare_binprm(struct linux_binprm *bprm)
  830. {
  831. int mode;
  832. struct inode * inode = bprm->file->f_dentry->d_inode;
  833. int retval;
  834. mode = inode->i_mode;
  835. /*
  836. * Check execute perms again - if the caller has CAP_DAC_OVERRIDE,
  837. * generic_permission lets a non-executable through
  838. */
  839. if (!(mode & 0111)) /* with at least _one_ execute bit set */
  840. return -EACCES;
  841. if (bprm->file->f_op == NULL)
  842. return -EACCES;
  843. bprm->e_uid = current->euid;
  844. bprm->e_gid = current->egid;
  845. if(!(bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID)) {
  846. /* Set-uid? */
  847. if (mode & S_ISUID) {
  848. current->personality &= ~PER_CLEAR_ON_SETID;
  849. bprm->e_uid = inode->i_uid;
  850. }
  851. /* Set-gid? */
  852. /*
  853. * If setgid is set but no group execute bit then this
  854. * is a candidate for mandatory locking, not a setgid
  855. * executable.
  856. */
  857. if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
  858. current->personality &= ~PER_CLEAR_ON_SETID;
  859. bprm->e_gid = inode->i_gid;
  860. }
  861. }
  862. /* fill in binprm security blob */
  863. retval = security_bprm_set(bprm);
  864. if (retval)
  865. return retval;
  866. memset(bprm->buf,0,BINPRM_BUF_SIZE);
  867. return kernel_read(bprm->file,0,bprm->buf,BINPRM_BUF_SIZE);
  868. }
  869. EXPORT_SYMBOL(prepare_binprm);
  870. static int unsafe_exec(struct task_struct *p)
  871. {
  872. int unsafe = 0;
  873. if (p->ptrace & PT_PTRACED) {
  874. if (p->ptrace & PT_PTRACE_CAP)
  875. unsafe |= LSM_UNSAFE_PTRACE_CAP;
  876. else
  877. unsafe |= LSM_UNSAFE_PTRACE;
  878. }
  879. if (atomic_read(&p->fs->count) > 1 ||
  880. atomic_read(&p->files->count) > 1 ||
  881. atomic_read(&p->sighand->count) > 1)
  882. unsafe |= LSM_UNSAFE_SHARE;
  883. return unsafe;
  884. }
  885. void compute_creds(struct linux_binprm *bprm)
  886. {
  887. int unsafe;
  888. if (bprm->e_uid != current->uid)
  889. suid_keys(current);
  890. exec_keys(current);
  891. task_lock(current);
  892. unsafe = unsafe_exec(current);
  893. security_bprm_apply_creds(bprm, unsafe);
  894. task_unlock(current);
  895. security_bprm_post_apply_creds(bprm);
  896. }
  897. EXPORT_SYMBOL(compute_creds);
  898. void remove_arg_zero(struct linux_binprm *bprm)
  899. {
  900. if (bprm->argc) {
  901. unsigned long offset;
  902. char * kaddr;
  903. struct page *page;
  904. offset = bprm->p % PAGE_SIZE;
  905. goto inside;
  906. while (bprm->p++, *(kaddr+offset++)) {
  907. if (offset != PAGE_SIZE)
  908. continue;
  909. offset = 0;
  910. kunmap_atomic(kaddr, KM_USER0);
  911. inside:
  912. page = bprm->page[bprm->p/PAGE_SIZE];
  913. kaddr = kmap_atomic(page, KM_USER0);
  914. }
  915. kunmap_atomic(kaddr, KM_USER0);
  916. bprm->argc--;
  917. }
  918. }
  919. EXPORT_SYMBOL(remove_arg_zero);
  920. /*
  921. * cycle the list of binary formats handler, until one recognizes the image
  922. */
  923. int search_binary_handler(struct linux_binprm *bprm,struct pt_regs *regs)
  924. {
  925. int try,retval;
  926. struct linux_binfmt *fmt;
  927. #ifdef __alpha__
  928. /* handle /sbin/loader.. */
  929. {
  930. struct exec * eh = (struct exec *) bprm->buf;
  931. if (!bprm->loader && eh->fh.f_magic == 0x183 &&
  932. (eh->fh.f_flags & 0x3000) == 0x3000)
  933. {
  934. struct file * file;
  935. unsigned long loader;
  936. allow_write_access(bprm->file);
  937. fput(bprm->file);
  938. bprm->file = NULL;
  939. loader = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
  940. file = open_exec("/sbin/loader");
  941. retval = PTR_ERR(file);
  942. if (IS_ERR(file))
  943. return retval;
  944. /* Remember if the application is TASO. */
  945. bprm->sh_bang = eh->ah.entry < 0x100000000UL;
  946. bprm->file = file;
  947. bprm->loader = loader;
  948. retval = prepare_binprm(bprm);
  949. if (retval<0)
  950. return retval;
  951. /* should call search_binary_handler recursively here,
  952. but it does not matter */
  953. }
  954. }
  955. #endif
  956. retval = security_bprm_check(bprm);
  957. if (retval)
  958. return retval;
  959. /* kernel module loader fixup */
  960. /* so we don't try to load run modprobe in kernel space. */
  961. set_fs(USER_DS);
  962. retval = -ENOENT;
  963. for (try=0; try<2; try++) {
  964. read_lock(&binfmt_lock);
  965. for (fmt = formats ; fmt ; fmt = fmt->next) {
  966. int (*fn)(struct linux_binprm *, struct pt_regs *) = fmt->load_binary;
  967. if (!fn)
  968. continue;
  969. if (!try_module_get(fmt->module))
  970. continue;
  971. read_unlock(&binfmt_lock);
  972. retval = fn(bprm, regs);
  973. if (retval >= 0) {
  974. put_binfmt(fmt);
  975. allow_write_access(bprm->file);
  976. if (bprm->file)
  977. fput(bprm->file);
  978. bprm->file = NULL;
  979. current->did_exec = 1;
  980. proc_exec_connector(current);
  981. return retval;
  982. }
  983. read_lock(&binfmt_lock);
  984. put_binfmt(fmt);
  985. if (retval != -ENOEXEC || bprm->mm == NULL)
  986. break;
  987. if (!bprm->file) {
  988. read_unlock(&binfmt_lock);
  989. return retval;
  990. }
  991. }
  992. read_unlock(&binfmt_lock);
  993. if (retval != -ENOEXEC || bprm->mm == NULL) {
  994. break;
  995. #ifdef CONFIG_KMOD
  996. }else{
  997. #define printable(c) (((c)=='\t') || ((c)=='\n') || (0x20<=(c) && (c)<=0x7e))
  998. if (printable(bprm->buf[0]) &&
  999. printable(bprm->buf[1]) &&
  1000. printable(bprm->buf[2]) &&
  1001. printable(bprm->buf[3]))
  1002. break; /* -ENOEXEC */
  1003. request_module("binfmt-%04x", *(unsigned short *)(&bprm->buf[2]));
  1004. #endif
  1005. }
  1006. }
  1007. return retval;
  1008. }
  1009. EXPORT_SYMBOL(search_binary_handler);
  1010. /*
  1011. * sys_execve() executes a new program.
  1012. */
  1013. int do_execve(char * filename,
  1014. char __user *__user *argv,
  1015. char __user *__user *envp,
  1016. struct pt_regs * regs)
  1017. {
  1018. struct linux_binprm *bprm;
  1019. struct file *file;
  1020. int retval;
  1021. int i;
  1022. retval = -ENOMEM;
  1023. bprm = kzalloc(sizeof(*bprm), GFP_KERNEL);
  1024. if (!bprm)
  1025. goto out_ret;
  1026. file = open_exec(filename);
  1027. retval = PTR_ERR(file);
  1028. if (IS_ERR(file))
  1029. goto out_kfree;
  1030. sched_exec();
  1031. bprm->p = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
  1032. bprm->file = file;
  1033. bprm->filename = filename;
  1034. bprm->interp = filename;
  1035. bprm->mm = mm_alloc();
  1036. retval = -ENOMEM;
  1037. if (!bprm->mm)
  1038. goto out_file;
  1039. retval = init_new_context(current, bprm->mm);
  1040. if (retval < 0)
  1041. goto out_mm;
  1042. bprm->argc = count(argv, bprm->p / sizeof(void *));
  1043. if ((retval = bprm->argc) < 0)
  1044. goto out_mm;
  1045. bprm->envc = count(envp, bprm->p / sizeof(void *));
  1046. if ((retval = bprm->envc) < 0)
  1047. goto out_mm;
  1048. retval = security_bprm_alloc(bprm);
  1049. if (retval)
  1050. goto out;
  1051. retval = prepare_binprm(bprm);
  1052. if (retval < 0)
  1053. goto out;
  1054. retval = copy_strings_kernel(1, &bprm->filename, bprm);
  1055. if (retval < 0)
  1056. goto out;
  1057. bprm->exec = bprm->p;
  1058. retval = copy_strings(bprm->envc, envp, bprm);
  1059. if (retval < 0)
  1060. goto out;
  1061. retval = copy_strings(bprm->argc, argv, bprm);
  1062. if (retval < 0)
  1063. goto out;
  1064. retval = search_binary_handler(bprm,regs);
  1065. if (retval >= 0) {
  1066. free_arg_pages(bprm);
  1067. /* execve success */
  1068. security_bprm_free(bprm);
  1069. acct_update_integrals(current);
  1070. kfree(bprm);
  1071. return retval;
  1072. }
  1073. out:
  1074. /* Something went wrong, return the inode and free the argument pages*/
  1075. for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
  1076. struct page * page = bprm->page[i];
  1077. if (page)
  1078. __free_page(page);
  1079. }
  1080. if (bprm->security)
  1081. security_bprm_free(bprm);
  1082. out_mm:
  1083. if (bprm->mm)
  1084. mmdrop(bprm->mm);
  1085. out_file:
  1086. if (bprm->file) {
  1087. allow_write_access(bprm->file);
  1088. fput(bprm->file);
  1089. }
  1090. out_kfree:
  1091. kfree(bprm);
  1092. out_ret:
  1093. return retval;
  1094. }
  1095. int set_binfmt(struct linux_binfmt *new)
  1096. {
  1097. struct linux_binfmt *old = current->binfmt;
  1098. if (new) {
  1099. if (!try_module_get(new->module))
  1100. return -1;
  1101. }
  1102. current->binfmt = new;
  1103. if (old)
  1104. module_put(old->module);
  1105. return 0;
  1106. }
  1107. EXPORT_SYMBOL(set_binfmt);
  1108. #define CORENAME_MAX_SIZE 64
  1109. /* format_corename will inspect the pattern parameter, and output a
  1110. * name into corename, which must have space for at least
  1111. * CORENAME_MAX_SIZE bytes plus one byte for the zero terminator.
  1112. */
  1113. static void format_corename(char *corename, const char *pattern, long signr)
  1114. {
  1115. const char *pat_ptr = pattern;
  1116. char *out_ptr = corename;
  1117. char *const out_end = corename + CORENAME_MAX_SIZE;
  1118. int rc;
  1119. int pid_in_pattern = 0;
  1120. /* Repeat as long as we have more pattern to process and more output
  1121. space */
  1122. while (*pat_ptr) {
  1123. if (*pat_ptr != '%') {
  1124. if (out_ptr == out_end)
  1125. goto out;
  1126. *out_ptr++ = *pat_ptr++;
  1127. } else {
  1128. switch (*++pat_ptr) {
  1129. case 0:
  1130. goto out;
  1131. /* Double percent, output one percent */
  1132. case '%':
  1133. if (out_ptr == out_end)
  1134. goto out;
  1135. *out_ptr++ = '%';
  1136. break;
  1137. /* pid */
  1138. case 'p':
  1139. pid_in_pattern = 1;
  1140. rc = snprintf(out_ptr, out_end - out_ptr,
  1141. "%d", current->tgid);
  1142. if (rc > out_end - out_ptr)
  1143. goto out;
  1144. out_ptr += rc;
  1145. break;
  1146. /* uid */
  1147. case 'u':
  1148. rc = snprintf(out_ptr, out_end - out_ptr,
  1149. "%d", current->uid);
  1150. if (rc > out_end - out_ptr)
  1151. goto out;
  1152. out_ptr += rc;
  1153. break;
  1154. /* gid */
  1155. case 'g':
  1156. rc = snprintf(out_ptr, out_end - out_ptr,
  1157. "%d", current->gid);
  1158. if (rc > out_end - out_ptr)
  1159. goto out;
  1160. out_ptr += rc;
  1161. break;
  1162. /* signal that caused the coredump */
  1163. case 's':
  1164. rc = snprintf(out_ptr, out_end - out_ptr,
  1165. "%ld", signr);
  1166. if (rc > out_end - out_ptr)
  1167. goto out;
  1168. out_ptr += rc;
  1169. break;
  1170. /* UNIX time of coredump */
  1171. case 't': {
  1172. struct timeval tv;
  1173. do_gettimeofday(&tv);
  1174. rc = snprintf(out_ptr, out_end - out_ptr,
  1175. "%lu", tv.tv_sec);
  1176. if (rc > out_end - out_ptr)
  1177. goto out;
  1178. out_ptr += rc;
  1179. break;
  1180. }
  1181. /* hostname */
  1182. case 'h':
  1183. down_read(&uts_sem);
  1184. rc = snprintf(out_ptr, out_end - out_ptr,
  1185. "%s", system_utsname.nodename);
  1186. up_read(&uts_sem);
  1187. if (rc > out_end - out_ptr)
  1188. goto out;
  1189. out_ptr += rc;
  1190. break;
  1191. /* executable */
  1192. case 'e':
  1193. rc = snprintf(out_ptr, out_end - out_ptr,
  1194. "%s", current->comm);
  1195. if (rc > out_end - out_ptr)
  1196. goto out;
  1197. out_ptr += rc;
  1198. break;
  1199. default:
  1200. break;
  1201. }
  1202. ++pat_ptr;
  1203. }
  1204. }
  1205. /* Backward compatibility with core_uses_pid:
  1206. *
  1207. * If core_pattern does not include a %p (as is the default)
  1208. * and core_uses_pid is set, then .%pid will be appended to
  1209. * the filename */
  1210. if (!pid_in_pattern
  1211. && (core_uses_pid || atomic_read(&current->mm->mm_users) != 1)) {
  1212. rc = snprintf(out_ptr, out_end - out_ptr,
  1213. ".%d", current->tgid);
  1214. if (rc > out_end - out_ptr)
  1215. goto out;
  1216. out_ptr += rc;
  1217. }
  1218. out:
  1219. *out_ptr = 0;
  1220. }
  1221. static void zap_threads (struct mm_struct *mm)
  1222. {
  1223. struct task_struct *g, *p;
  1224. struct task_struct *tsk = current;
  1225. struct completion *vfork_done = tsk->vfork_done;
  1226. int traced = 0;
  1227. /*
  1228. * Make sure nobody is waiting for us to release the VM,
  1229. * otherwise we can deadlock when we wait on each other
  1230. */
  1231. if (vfork_done) {
  1232. tsk->vfork_done = NULL;
  1233. complete(vfork_done);
  1234. }
  1235. read_lock(&tasklist_lock);
  1236. do_each_thread(g,p)
  1237. if (mm == p->mm && p != tsk) {
  1238. force_sig_specific(SIGKILL, p);
  1239. mm->core_waiters++;
  1240. if (unlikely(p->ptrace) &&
  1241. unlikely(p->parent->mm == mm))
  1242. traced = 1;
  1243. }
  1244. while_each_thread(g,p);
  1245. read_unlock(&tasklist_lock);
  1246. if (unlikely(traced)) {
  1247. /*
  1248. * We are zapping a thread and the thread it ptraces.
  1249. * If the tracee went into a ptrace stop for exit tracing,
  1250. * we could deadlock since the tracer is waiting for this
  1251. * coredump to finish. Detach them so they can both die.
  1252. */
  1253. write_lock_irq(&tasklist_lock);
  1254. do_each_thread(g,p) {
  1255. if (mm == p->mm && p != tsk &&
  1256. p->ptrace && p->parent->mm == mm) {
  1257. __ptrace_detach(p, 0);
  1258. }
  1259. } while_each_thread(g,p);
  1260. write_unlock_irq(&tasklist_lock);
  1261. }
  1262. }
  1263. static void coredump_wait(struct mm_struct *mm)
  1264. {
  1265. DECLARE_COMPLETION(startup_done);
  1266. int core_waiters;
  1267. mm->core_startup_done = &startup_done;
  1268. zap_threads(mm);
  1269. core_waiters = mm->core_waiters;
  1270. up_write(&mm->mmap_sem);
  1271. if (core_waiters)
  1272. wait_for_completion(&startup_done);
  1273. BUG_ON(mm->core_waiters);
  1274. }
  1275. int do_coredump(long signr, int exit_code, struct pt_regs * regs)
  1276. {
  1277. char corename[CORENAME_MAX_SIZE + 1];
  1278. struct mm_struct *mm = current->mm;
  1279. struct linux_binfmt * binfmt;
  1280. struct inode * inode;
  1281. struct file * file;
  1282. int retval = 0;
  1283. int fsuid = current->fsuid;
  1284. int flag = 0;
  1285. binfmt = current->binfmt;
  1286. if (!binfmt || !binfmt->core_dump)
  1287. goto fail;
  1288. down_write(&mm->mmap_sem);
  1289. if (!mm->dumpable) {
  1290. up_write(&mm->mmap_sem);
  1291. goto fail;
  1292. }
  1293. /*
  1294. * We cannot trust fsuid as being the "true" uid of the
  1295. * process nor do we know its entire history. We only know it
  1296. * was tainted so we dump it as root in mode 2.
  1297. */
  1298. if (mm->dumpable == 2) { /* Setuid core dump mode */
  1299. flag = O_EXCL; /* Stop rewrite attacks */
  1300. current->fsuid = 0; /* Dump root private */
  1301. }
  1302. mm->dumpable = 0;
  1303. retval = -EAGAIN;
  1304. spin_lock_irq(&current->sighand->siglock);
  1305. if (!(current->signal->flags & SIGNAL_GROUP_EXIT)) {
  1306. current->signal->flags = SIGNAL_GROUP_EXIT;
  1307. current->signal->group_exit_code = exit_code;
  1308. current->signal->group_stop_count = 0;
  1309. retval = 0;
  1310. }
  1311. spin_unlock_irq(&current->sighand->siglock);
  1312. if (retval) {
  1313. up_write(&mm->mmap_sem);
  1314. goto fail;
  1315. }
  1316. init_completion(&mm->core_done);
  1317. coredump_wait(mm);
  1318. /*
  1319. * Clear any false indication of pending signals that might
  1320. * be seen by the filesystem code called to write the core file.
  1321. */
  1322. clear_thread_flag(TIF_SIGPENDING);
  1323. if (current->signal->rlim[RLIMIT_CORE].rlim_cur < binfmt->min_coredump)
  1324. goto fail_unlock;
  1325. /*
  1326. * lock_kernel() because format_corename() is controlled by sysctl, which
  1327. * uses lock_kernel()
  1328. */
  1329. lock_kernel();
  1330. format_corename(corename, core_pattern, signr);
  1331. unlock_kernel();
  1332. file = filp_open(corename, O_CREAT | 2 | O_NOFOLLOW | O_LARGEFILE | flag, 0600);
  1333. if (IS_ERR(file))
  1334. goto fail_unlock;
  1335. inode = file->f_dentry->d_inode;
  1336. if (inode->i_nlink > 1)
  1337. goto close_fail; /* multiple links - don't dump */
  1338. if (d_unhashed(file->f_dentry))
  1339. goto close_fail;
  1340. if (!S_ISREG(inode->i_mode))
  1341. goto close_fail;
  1342. if (!file->f_op)
  1343. goto close_fail;
  1344. if (!file->f_op->write)
  1345. goto close_fail;
  1346. if (do_truncate(file->f_dentry, 0, 0, file) != 0)
  1347. goto close_fail;
  1348. retval = binfmt->core_dump(signr, regs, file);
  1349. if (retval)
  1350. current->signal->group_exit_code |= 0x80;
  1351. close_fail:
  1352. filp_close(file, NULL);
  1353. fail_unlock:
  1354. current->fsuid = fsuid;
  1355. complete_all(&mm->core_done);
  1356. fail:
  1357. return retval;
  1358. }