exec.c 35 KB

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