exec.c 36 KB

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