exec.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788
  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/fdtable.h>
  26. #include <linux/mman.h>
  27. #include <linux/a.out.h>
  28. #include <linux/stat.h>
  29. #include <linux/fcntl.h>
  30. #include <linux/smp_lock.h>
  31. #include <linux/string.h>
  32. #include <linux/init.h>
  33. #include <linux/pagemap.h>
  34. #include <linux/highmem.h>
  35. #include <linux/spinlock.h>
  36. #include <linux/key.h>
  37. #include <linux/personality.h>
  38. #include <linux/binfmts.h>
  39. #include <linux/swap.h>
  40. #include <linux/utsname.h>
  41. #include <linux/pid_namespace.h>
  42. #include <linux/module.h>
  43. #include <linux/namei.h>
  44. #include <linux/proc_fs.h>
  45. #include <linux/ptrace.h>
  46. #include <linux/mount.h>
  47. #include <linux/security.h>
  48. #include <linux/syscalls.h>
  49. #include <linux/rmap.h>
  50. #include <linux/tsacct_kern.h>
  51. #include <linux/cn_proc.h>
  52. #include <linux/audit.h>
  53. #include <asm/uaccess.h>
  54. #include <asm/mmu_context.h>
  55. #include <asm/tlb.h>
  56. #ifdef CONFIG_KMOD
  57. #include <linux/kmod.h>
  58. #endif
  59. int core_uses_pid;
  60. char core_pattern[CORENAME_MAX_SIZE] = "core";
  61. int suid_dumpable = 0;
  62. /* The maximal length of core_pattern is also specified in sysctl.c */
  63. static LIST_HEAD(formats);
  64. static DEFINE_RWLOCK(binfmt_lock);
  65. int register_binfmt(struct linux_binfmt * fmt)
  66. {
  67. if (!fmt)
  68. return -EINVAL;
  69. write_lock(&binfmt_lock);
  70. list_add(&fmt->lh, &formats);
  71. write_unlock(&binfmt_lock);
  72. return 0;
  73. }
  74. EXPORT_SYMBOL(register_binfmt);
  75. void unregister_binfmt(struct linux_binfmt * fmt)
  76. {
  77. write_lock(&binfmt_lock);
  78. list_del(&fmt->lh);
  79. write_unlock(&binfmt_lock);
  80. }
  81. EXPORT_SYMBOL(unregister_binfmt);
  82. static inline void put_binfmt(struct linux_binfmt * fmt)
  83. {
  84. module_put(fmt->module);
  85. }
  86. /*
  87. * Note that a shared library must be both readable and executable due to
  88. * security reasons.
  89. *
  90. * Also note that we take the address to load from from the file itself.
  91. */
  92. asmlinkage long sys_uselib(const char __user * library)
  93. {
  94. struct file * file;
  95. struct nameidata nd;
  96. int error;
  97. error = __user_path_lookup_open(library, LOOKUP_FOLLOW, &nd, FMODE_READ|FMODE_EXEC);
  98. if (error)
  99. goto out;
  100. error = -EINVAL;
  101. if (!S_ISREG(nd.path.dentry->d_inode->i_mode))
  102. goto exit;
  103. error = vfs_permission(&nd, MAY_READ | MAY_EXEC);
  104. if (error)
  105. goto exit;
  106. file = nameidata_to_filp(&nd, O_RDONLY|O_LARGEFILE);
  107. error = PTR_ERR(file);
  108. if (IS_ERR(file))
  109. goto out;
  110. error = -ENOEXEC;
  111. if(file->f_op) {
  112. struct linux_binfmt * fmt;
  113. read_lock(&binfmt_lock);
  114. list_for_each_entry(fmt, &formats, lh) {
  115. if (!fmt->load_shlib)
  116. continue;
  117. if (!try_module_get(fmt->module))
  118. continue;
  119. read_unlock(&binfmt_lock);
  120. error = fmt->load_shlib(file);
  121. read_lock(&binfmt_lock);
  122. put_binfmt(fmt);
  123. if (error != -ENOEXEC)
  124. break;
  125. }
  126. read_unlock(&binfmt_lock);
  127. }
  128. fput(file);
  129. out:
  130. return error;
  131. exit:
  132. release_open_intent(&nd);
  133. path_put(&nd.path);
  134. goto out;
  135. }
  136. #ifdef CONFIG_MMU
  137. static struct page *get_arg_page(struct linux_binprm *bprm, unsigned long pos,
  138. int write)
  139. {
  140. struct page *page;
  141. int ret;
  142. #ifdef CONFIG_STACK_GROWSUP
  143. if (write) {
  144. ret = expand_stack_downwards(bprm->vma, pos);
  145. if (ret < 0)
  146. return NULL;
  147. }
  148. #endif
  149. ret = get_user_pages(current, bprm->mm, pos,
  150. 1, write, 1, &page, NULL);
  151. if (ret <= 0)
  152. return NULL;
  153. if (write) {
  154. unsigned long size = bprm->vma->vm_end - bprm->vma->vm_start;
  155. struct rlimit *rlim;
  156. /*
  157. * We've historically supported up to 32 pages (ARG_MAX)
  158. * of argument strings even with small stacks
  159. */
  160. if (size <= ARG_MAX)
  161. return page;
  162. /*
  163. * Limit to 1/4-th the stack size for the argv+env strings.
  164. * This ensures that:
  165. * - the remaining binfmt code will not run out of stack space,
  166. * - the program will have a reasonable amount of stack left
  167. * to work from.
  168. */
  169. rlim = current->signal->rlim;
  170. if (size > rlim[RLIMIT_STACK].rlim_cur / 4) {
  171. put_page(page);
  172. return NULL;
  173. }
  174. }
  175. return page;
  176. }
  177. static void put_arg_page(struct page *page)
  178. {
  179. put_page(page);
  180. }
  181. static void free_arg_page(struct linux_binprm *bprm, int i)
  182. {
  183. }
  184. static void free_arg_pages(struct linux_binprm *bprm)
  185. {
  186. }
  187. static void flush_arg_page(struct linux_binprm *bprm, unsigned long pos,
  188. struct page *page)
  189. {
  190. flush_cache_page(bprm->vma, pos, page_to_pfn(page));
  191. }
  192. static int __bprm_mm_init(struct linux_binprm *bprm)
  193. {
  194. int err = -ENOMEM;
  195. struct vm_area_struct *vma = NULL;
  196. struct mm_struct *mm = bprm->mm;
  197. bprm->vma = vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
  198. if (!vma)
  199. goto err;
  200. down_write(&mm->mmap_sem);
  201. vma->vm_mm = mm;
  202. /*
  203. * Place the stack at the largest stack address the architecture
  204. * supports. Later, we'll move this to an appropriate place. We don't
  205. * use STACK_TOP because that can depend on attributes which aren't
  206. * configured yet.
  207. */
  208. vma->vm_end = STACK_TOP_MAX;
  209. vma->vm_start = vma->vm_end - PAGE_SIZE;
  210. vma->vm_flags = VM_STACK_FLAGS;
  211. vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
  212. err = insert_vm_struct(mm, vma);
  213. if (err) {
  214. up_write(&mm->mmap_sem);
  215. goto err;
  216. }
  217. mm->stack_vm = mm->total_vm = 1;
  218. up_write(&mm->mmap_sem);
  219. bprm->p = vma->vm_end - sizeof(void *);
  220. return 0;
  221. err:
  222. if (vma) {
  223. bprm->vma = NULL;
  224. kmem_cache_free(vm_area_cachep, vma);
  225. }
  226. return err;
  227. }
  228. static bool valid_arg_len(struct linux_binprm *bprm, long len)
  229. {
  230. return len <= MAX_ARG_STRLEN;
  231. }
  232. #else
  233. static struct page *get_arg_page(struct linux_binprm *bprm, unsigned long pos,
  234. int write)
  235. {
  236. struct page *page;
  237. page = bprm->page[pos / PAGE_SIZE];
  238. if (!page && write) {
  239. page = alloc_page(GFP_HIGHUSER|__GFP_ZERO);
  240. if (!page)
  241. return NULL;
  242. bprm->page[pos / PAGE_SIZE] = page;
  243. }
  244. return page;
  245. }
  246. static void put_arg_page(struct page *page)
  247. {
  248. }
  249. static void free_arg_page(struct linux_binprm *bprm, int i)
  250. {
  251. if (bprm->page[i]) {
  252. __free_page(bprm->page[i]);
  253. bprm->page[i] = NULL;
  254. }
  255. }
  256. static void free_arg_pages(struct linux_binprm *bprm)
  257. {
  258. int i;
  259. for (i = 0; i < MAX_ARG_PAGES; i++)
  260. free_arg_page(bprm, i);
  261. }
  262. static void flush_arg_page(struct linux_binprm *bprm, unsigned long pos,
  263. struct page *page)
  264. {
  265. }
  266. static int __bprm_mm_init(struct linux_binprm *bprm)
  267. {
  268. bprm->p = PAGE_SIZE * MAX_ARG_PAGES - sizeof(void *);
  269. return 0;
  270. }
  271. static bool valid_arg_len(struct linux_binprm *bprm, long len)
  272. {
  273. return len <= bprm->p;
  274. }
  275. #endif /* CONFIG_MMU */
  276. /*
  277. * Create a new mm_struct and populate it with a temporary stack
  278. * vm_area_struct. We don't have enough context at this point to set the stack
  279. * flags, permissions, and offset, so we use temporary values. We'll update
  280. * them later in setup_arg_pages().
  281. */
  282. int bprm_mm_init(struct linux_binprm *bprm)
  283. {
  284. int err;
  285. struct mm_struct *mm = NULL;
  286. bprm->mm = mm = mm_alloc();
  287. err = -ENOMEM;
  288. if (!mm)
  289. goto err;
  290. err = init_new_context(current, mm);
  291. if (err)
  292. goto err;
  293. err = __bprm_mm_init(bprm);
  294. if (err)
  295. goto err;
  296. return 0;
  297. err:
  298. if (mm) {
  299. bprm->mm = NULL;
  300. mmdrop(mm);
  301. }
  302. return err;
  303. }
  304. /*
  305. * count() counts the number of strings in array ARGV.
  306. */
  307. static int count(char __user * __user * argv, int max)
  308. {
  309. int i = 0;
  310. if (argv != NULL) {
  311. for (;;) {
  312. char __user * p;
  313. if (get_user(p, argv))
  314. return -EFAULT;
  315. if (!p)
  316. break;
  317. argv++;
  318. if(++i > max)
  319. return -E2BIG;
  320. cond_resched();
  321. }
  322. }
  323. return i;
  324. }
  325. /*
  326. * 'copy_strings()' copies argument/environment strings from the old
  327. * processes's memory to the new process's stack. The call to get_user_pages()
  328. * ensures the destination page is created and not swapped out.
  329. */
  330. static int copy_strings(int argc, char __user * __user * argv,
  331. struct linux_binprm *bprm)
  332. {
  333. struct page *kmapped_page = NULL;
  334. char *kaddr = NULL;
  335. unsigned long kpos = 0;
  336. int ret;
  337. while (argc-- > 0) {
  338. char __user *str;
  339. int len;
  340. unsigned long pos;
  341. if (get_user(str, argv+argc) ||
  342. !(len = strnlen_user(str, MAX_ARG_STRLEN))) {
  343. ret = -EFAULT;
  344. goto out;
  345. }
  346. if (!valid_arg_len(bprm, len)) {
  347. ret = -E2BIG;
  348. goto out;
  349. }
  350. /* We're going to work our way backwords. */
  351. pos = bprm->p;
  352. str += len;
  353. bprm->p -= len;
  354. while (len > 0) {
  355. int offset, bytes_to_copy;
  356. offset = pos % PAGE_SIZE;
  357. if (offset == 0)
  358. offset = PAGE_SIZE;
  359. bytes_to_copy = offset;
  360. if (bytes_to_copy > len)
  361. bytes_to_copy = len;
  362. offset -= bytes_to_copy;
  363. pos -= bytes_to_copy;
  364. str -= bytes_to_copy;
  365. len -= bytes_to_copy;
  366. if (!kmapped_page || kpos != (pos & PAGE_MASK)) {
  367. struct page *page;
  368. page = get_arg_page(bprm, pos, 1);
  369. if (!page) {
  370. ret = -E2BIG;
  371. goto out;
  372. }
  373. if (kmapped_page) {
  374. flush_kernel_dcache_page(kmapped_page);
  375. kunmap(kmapped_page);
  376. put_arg_page(kmapped_page);
  377. }
  378. kmapped_page = page;
  379. kaddr = kmap(kmapped_page);
  380. kpos = pos & PAGE_MASK;
  381. flush_arg_page(bprm, kpos, kmapped_page);
  382. }
  383. if (copy_from_user(kaddr+offset, str, bytes_to_copy)) {
  384. ret = -EFAULT;
  385. goto out;
  386. }
  387. }
  388. }
  389. ret = 0;
  390. out:
  391. if (kmapped_page) {
  392. flush_kernel_dcache_page(kmapped_page);
  393. kunmap(kmapped_page);
  394. put_arg_page(kmapped_page);
  395. }
  396. return ret;
  397. }
  398. /*
  399. * Like copy_strings, but get argv and its values from kernel memory.
  400. */
  401. int copy_strings_kernel(int argc,char ** argv, struct linux_binprm *bprm)
  402. {
  403. int r;
  404. mm_segment_t oldfs = get_fs();
  405. set_fs(KERNEL_DS);
  406. r = copy_strings(argc, (char __user * __user *)argv, bprm);
  407. set_fs(oldfs);
  408. return r;
  409. }
  410. EXPORT_SYMBOL(copy_strings_kernel);
  411. #ifdef CONFIG_MMU
  412. /*
  413. * During bprm_mm_init(), we create a temporary stack at STACK_TOP_MAX. Once
  414. * the binfmt code determines where the new stack should reside, we shift it to
  415. * its final location. The process proceeds as follows:
  416. *
  417. * 1) Use shift to calculate the new vma endpoints.
  418. * 2) Extend vma to cover both the old and new ranges. This ensures the
  419. * arguments passed to subsequent functions are consistent.
  420. * 3) Move vma's page tables to the new range.
  421. * 4) Free up any cleared pgd range.
  422. * 5) Shrink the vma to cover only the new range.
  423. */
  424. static int shift_arg_pages(struct vm_area_struct *vma, unsigned long shift)
  425. {
  426. struct mm_struct *mm = vma->vm_mm;
  427. unsigned long old_start = vma->vm_start;
  428. unsigned long old_end = vma->vm_end;
  429. unsigned long length = old_end - old_start;
  430. unsigned long new_start = old_start - shift;
  431. unsigned long new_end = old_end - shift;
  432. struct mmu_gather *tlb;
  433. BUG_ON(new_start > new_end);
  434. /*
  435. * ensure there are no vmas between where we want to go
  436. * and where we are
  437. */
  438. if (vma != find_vma(mm, new_start))
  439. return -EFAULT;
  440. /*
  441. * cover the whole range: [new_start, old_end)
  442. */
  443. vma_adjust(vma, new_start, old_end, vma->vm_pgoff, NULL);
  444. /*
  445. * move the page tables downwards, on failure we rely on
  446. * process cleanup to remove whatever mess we made.
  447. */
  448. if (length != move_page_tables(vma, old_start,
  449. vma, new_start, length))
  450. return -ENOMEM;
  451. lru_add_drain();
  452. tlb = tlb_gather_mmu(mm, 0);
  453. if (new_end > old_start) {
  454. /*
  455. * when the old and new regions overlap clear from new_end.
  456. */
  457. free_pgd_range(&tlb, new_end, old_end, new_end,
  458. vma->vm_next ? vma->vm_next->vm_start : 0);
  459. } else {
  460. /*
  461. * otherwise, clean from old_start; this is done to not touch
  462. * the address space in [new_end, old_start) some architectures
  463. * have constraints on va-space that make this illegal (IA64) -
  464. * for the others its just a little faster.
  465. */
  466. free_pgd_range(&tlb, old_start, old_end, new_end,
  467. vma->vm_next ? vma->vm_next->vm_start : 0);
  468. }
  469. tlb_finish_mmu(tlb, new_end, old_end);
  470. /*
  471. * shrink the vma to just the new range.
  472. */
  473. vma_adjust(vma, new_start, new_end, vma->vm_pgoff, NULL);
  474. return 0;
  475. }
  476. #define EXTRA_STACK_VM_PAGES 20 /* random */
  477. /*
  478. * Finalizes the stack vm_area_struct. The flags and permissions are updated,
  479. * the stack is optionally relocated, and some extra space is added.
  480. */
  481. int setup_arg_pages(struct linux_binprm *bprm,
  482. unsigned long stack_top,
  483. int executable_stack)
  484. {
  485. unsigned long ret;
  486. unsigned long stack_shift;
  487. struct mm_struct *mm = current->mm;
  488. struct vm_area_struct *vma = bprm->vma;
  489. struct vm_area_struct *prev = NULL;
  490. unsigned long vm_flags;
  491. unsigned long stack_base;
  492. #ifdef CONFIG_STACK_GROWSUP
  493. /* Limit stack size to 1GB */
  494. stack_base = current->signal->rlim[RLIMIT_STACK].rlim_max;
  495. if (stack_base > (1 << 30))
  496. stack_base = 1 << 30;
  497. /* Make sure we didn't let the argument array grow too large. */
  498. if (vma->vm_end - vma->vm_start > stack_base)
  499. return -ENOMEM;
  500. stack_base = PAGE_ALIGN(stack_top - stack_base);
  501. stack_shift = vma->vm_start - stack_base;
  502. mm->arg_start = bprm->p - stack_shift;
  503. bprm->p = vma->vm_end - stack_shift;
  504. #else
  505. stack_top = arch_align_stack(stack_top);
  506. stack_top = PAGE_ALIGN(stack_top);
  507. stack_shift = vma->vm_end - stack_top;
  508. bprm->p -= stack_shift;
  509. mm->arg_start = bprm->p;
  510. #endif
  511. if (bprm->loader)
  512. bprm->loader -= stack_shift;
  513. bprm->exec -= stack_shift;
  514. down_write(&mm->mmap_sem);
  515. vm_flags = vma->vm_flags;
  516. /*
  517. * Adjust stack execute permissions; explicitly enable for
  518. * EXSTACK_ENABLE_X, disable for EXSTACK_DISABLE_X and leave alone
  519. * (arch default) otherwise.
  520. */
  521. if (unlikely(executable_stack == EXSTACK_ENABLE_X))
  522. vm_flags |= VM_EXEC;
  523. else if (executable_stack == EXSTACK_DISABLE_X)
  524. vm_flags &= ~VM_EXEC;
  525. vm_flags |= mm->def_flags;
  526. ret = mprotect_fixup(vma, &prev, vma->vm_start, vma->vm_end,
  527. vm_flags);
  528. if (ret)
  529. goto out_unlock;
  530. BUG_ON(prev != vma);
  531. /* Move stack pages down in memory. */
  532. if (stack_shift) {
  533. ret = shift_arg_pages(vma, stack_shift);
  534. if (ret) {
  535. up_write(&mm->mmap_sem);
  536. return ret;
  537. }
  538. }
  539. #ifdef CONFIG_STACK_GROWSUP
  540. stack_base = vma->vm_end + EXTRA_STACK_VM_PAGES * PAGE_SIZE;
  541. #else
  542. stack_base = vma->vm_start - EXTRA_STACK_VM_PAGES * PAGE_SIZE;
  543. #endif
  544. ret = expand_stack(vma, stack_base);
  545. if (ret)
  546. ret = -EFAULT;
  547. out_unlock:
  548. up_write(&mm->mmap_sem);
  549. return 0;
  550. }
  551. EXPORT_SYMBOL(setup_arg_pages);
  552. #endif /* CONFIG_MMU */
  553. struct file *open_exec(const char *name)
  554. {
  555. struct nameidata nd;
  556. int err;
  557. struct file *file;
  558. err = path_lookup_open(AT_FDCWD, name, LOOKUP_FOLLOW, &nd, FMODE_READ|FMODE_EXEC);
  559. file = ERR_PTR(err);
  560. if (!err) {
  561. struct inode *inode = nd.path.dentry->d_inode;
  562. file = ERR_PTR(-EACCES);
  563. if (S_ISREG(inode->i_mode)) {
  564. int err = vfs_permission(&nd, MAY_EXEC);
  565. file = ERR_PTR(err);
  566. if (!err) {
  567. file = nameidata_to_filp(&nd,
  568. O_RDONLY|O_LARGEFILE);
  569. if (!IS_ERR(file)) {
  570. err = deny_write_access(file);
  571. if (err) {
  572. fput(file);
  573. file = ERR_PTR(err);
  574. }
  575. }
  576. out:
  577. return file;
  578. }
  579. }
  580. release_open_intent(&nd);
  581. path_put(&nd.path);
  582. }
  583. goto out;
  584. }
  585. EXPORT_SYMBOL(open_exec);
  586. int kernel_read(struct file *file, unsigned long offset,
  587. char *addr, unsigned long count)
  588. {
  589. mm_segment_t old_fs;
  590. loff_t pos = offset;
  591. int result;
  592. old_fs = get_fs();
  593. set_fs(get_ds());
  594. /* The cast to a user pointer is valid due to the set_fs() */
  595. result = vfs_read(file, (void __user *)addr, count, &pos);
  596. set_fs(old_fs);
  597. return result;
  598. }
  599. EXPORT_SYMBOL(kernel_read);
  600. static int exec_mmap(struct mm_struct *mm)
  601. {
  602. struct task_struct *tsk;
  603. struct mm_struct * old_mm, *active_mm;
  604. /* Notify parent that we're no longer interested in the old VM */
  605. tsk = current;
  606. old_mm = current->mm;
  607. mm_release(tsk, old_mm);
  608. if (old_mm) {
  609. /*
  610. * Make sure that if there is a core dump in progress
  611. * for the old mm, we get out and die instead of going
  612. * through with the exec. We must hold mmap_sem around
  613. * checking core_waiters and changing tsk->mm. The
  614. * core-inducing thread will increment core_waiters for
  615. * each thread whose ->mm == old_mm.
  616. */
  617. down_read(&old_mm->mmap_sem);
  618. if (unlikely(old_mm->core_waiters)) {
  619. up_read(&old_mm->mmap_sem);
  620. return -EINTR;
  621. }
  622. }
  623. task_lock(tsk);
  624. active_mm = tsk->active_mm;
  625. tsk->mm = mm;
  626. tsk->active_mm = mm;
  627. activate_mm(active_mm, mm);
  628. task_unlock(tsk);
  629. mm_update_next_owner(old_mm);
  630. arch_pick_mmap_layout(mm);
  631. if (old_mm) {
  632. up_read(&old_mm->mmap_sem);
  633. BUG_ON(active_mm != old_mm);
  634. mmput(old_mm);
  635. return 0;
  636. }
  637. mmdrop(active_mm);
  638. return 0;
  639. }
  640. /*
  641. * This function makes sure the current process has its own signal table,
  642. * so that flush_signal_handlers can later reset the handlers without
  643. * disturbing other processes. (Other processes might share the signal
  644. * table via the CLONE_SIGHAND option to clone().)
  645. */
  646. static int de_thread(struct task_struct *tsk)
  647. {
  648. struct signal_struct *sig = tsk->signal;
  649. struct sighand_struct *oldsighand = tsk->sighand;
  650. spinlock_t *lock = &oldsighand->siglock;
  651. struct task_struct *leader = NULL;
  652. int count;
  653. if (thread_group_empty(tsk))
  654. goto no_thread_group;
  655. /*
  656. * Kill all other threads in the thread group.
  657. */
  658. spin_lock_irq(lock);
  659. if (signal_group_exit(sig)) {
  660. /*
  661. * Another group action in progress, just
  662. * return so that the signal is processed.
  663. */
  664. spin_unlock_irq(lock);
  665. return -EAGAIN;
  666. }
  667. sig->group_exit_task = tsk;
  668. zap_other_threads(tsk);
  669. /* Account for the thread group leader hanging around: */
  670. count = thread_group_leader(tsk) ? 1 : 2;
  671. sig->notify_count = count;
  672. while (atomic_read(&sig->count) > count) {
  673. __set_current_state(TASK_UNINTERRUPTIBLE);
  674. spin_unlock_irq(lock);
  675. schedule();
  676. spin_lock_irq(lock);
  677. }
  678. spin_unlock_irq(lock);
  679. /*
  680. * At this point all other threads have exited, all we have to
  681. * do is to wait for the thread group leader to become inactive,
  682. * and to assume its PID:
  683. */
  684. if (!thread_group_leader(tsk)) {
  685. leader = tsk->group_leader;
  686. sig->notify_count = -1; /* for exit_notify() */
  687. for (;;) {
  688. write_lock_irq(&tasklist_lock);
  689. if (likely(leader->exit_state))
  690. break;
  691. __set_current_state(TASK_UNINTERRUPTIBLE);
  692. write_unlock_irq(&tasklist_lock);
  693. schedule();
  694. }
  695. if (unlikely(task_child_reaper(tsk) == leader))
  696. task_active_pid_ns(tsk)->child_reaper = tsk;
  697. /*
  698. * The only record we have of the real-time age of a
  699. * process, regardless of execs it's done, is start_time.
  700. * All the past CPU time is accumulated in signal_struct
  701. * from sister threads now dead. But in this non-leader
  702. * exec, nothing survives from the original leader thread,
  703. * whose birth marks the true age of this process now.
  704. * When we take on its identity by switching to its PID, we
  705. * also take its birthdate (always earlier than our own).
  706. */
  707. tsk->start_time = leader->start_time;
  708. BUG_ON(!same_thread_group(leader, tsk));
  709. BUG_ON(has_group_leader_pid(tsk));
  710. /*
  711. * An exec() starts a new thread group with the
  712. * TGID of the previous thread group. Rehash the
  713. * two threads with a switched PID, and release
  714. * the former thread group leader:
  715. */
  716. /* Become a process group leader with the old leader's pid.
  717. * The old leader becomes a thread of the this thread group.
  718. * Note: The old leader also uses this pid until release_task
  719. * is called. Odd but simple and correct.
  720. */
  721. detach_pid(tsk, PIDTYPE_PID);
  722. tsk->pid = leader->pid;
  723. attach_pid(tsk, PIDTYPE_PID, task_pid(leader));
  724. transfer_pid(leader, tsk, PIDTYPE_PGID);
  725. transfer_pid(leader, tsk, PIDTYPE_SID);
  726. list_replace_rcu(&leader->tasks, &tsk->tasks);
  727. tsk->group_leader = tsk;
  728. leader->group_leader = tsk;
  729. tsk->exit_signal = SIGCHLD;
  730. BUG_ON(leader->exit_state != EXIT_ZOMBIE);
  731. leader->exit_state = EXIT_DEAD;
  732. write_unlock_irq(&tasklist_lock);
  733. }
  734. sig->group_exit_task = NULL;
  735. sig->notify_count = 0;
  736. no_thread_group:
  737. exit_itimers(sig);
  738. flush_itimer_signals();
  739. if (leader)
  740. release_task(leader);
  741. if (atomic_read(&oldsighand->count) != 1) {
  742. struct sighand_struct *newsighand;
  743. /*
  744. * This ->sighand is shared with the CLONE_SIGHAND
  745. * but not CLONE_THREAD task, switch to the new one.
  746. */
  747. newsighand = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
  748. if (!newsighand)
  749. return -ENOMEM;
  750. atomic_set(&newsighand->count, 1);
  751. memcpy(newsighand->action, oldsighand->action,
  752. sizeof(newsighand->action));
  753. write_lock_irq(&tasklist_lock);
  754. spin_lock(&oldsighand->siglock);
  755. rcu_assign_pointer(tsk->sighand, newsighand);
  756. spin_unlock(&oldsighand->siglock);
  757. write_unlock_irq(&tasklist_lock);
  758. __cleanup_sighand(oldsighand);
  759. }
  760. BUG_ON(!thread_group_leader(tsk));
  761. return 0;
  762. }
  763. /*
  764. * These functions flushes out all traces of the currently running executable
  765. * so that a new one can be started
  766. */
  767. static void flush_old_files(struct files_struct * files)
  768. {
  769. long j = -1;
  770. struct fdtable *fdt;
  771. spin_lock(&files->file_lock);
  772. for (;;) {
  773. unsigned long set, i;
  774. j++;
  775. i = j * __NFDBITS;
  776. fdt = files_fdtable(files);
  777. if (i >= fdt->max_fds)
  778. break;
  779. set = fdt->close_on_exec->fds_bits[j];
  780. if (!set)
  781. continue;
  782. fdt->close_on_exec->fds_bits[j] = 0;
  783. spin_unlock(&files->file_lock);
  784. for ( ; set ; i++,set >>= 1) {
  785. if (set & 1) {
  786. sys_close(i);
  787. }
  788. }
  789. spin_lock(&files->file_lock);
  790. }
  791. spin_unlock(&files->file_lock);
  792. }
  793. char *get_task_comm(char *buf, struct task_struct *tsk)
  794. {
  795. /* buf must be at least sizeof(tsk->comm) in size */
  796. task_lock(tsk);
  797. strncpy(buf, tsk->comm, sizeof(tsk->comm));
  798. task_unlock(tsk);
  799. return buf;
  800. }
  801. void set_task_comm(struct task_struct *tsk, char *buf)
  802. {
  803. task_lock(tsk);
  804. strlcpy(tsk->comm, buf, sizeof(tsk->comm));
  805. task_unlock(tsk);
  806. }
  807. int flush_old_exec(struct linux_binprm * bprm)
  808. {
  809. char * name;
  810. int i, ch, retval;
  811. char tcomm[sizeof(current->comm)];
  812. /*
  813. * Make sure we have a private signal table and that
  814. * we are unassociated from the previous thread group.
  815. */
  816. retval = de_thread(current);
  817. if (retval)
  818. goto out;
  819. set_mm_exe_file(bprm->mm, bprm->file);
  820. /*
  821. * Release all of the old mmap stuff
  822. */
  823. retval = exec_mmap(bprm->mm);
  824. if (retval)
  825. goto out;
  826. bprm->mm = NULL; /* We're using it now */
  827. /* This is the point of no return */
  828. current->sas_ss_sp = current->sas_ss_size = 0;
  829. if (current->euid == current->uid && current->egid == current->gid)
  830. set_dumpable(current->mm, 1);
  831. else
  832. set_dumpable(current->mm, suid_dumpable);
  833. name = bprm->filename;
  834. /* Copies the binary name from after last slash */
  835. for (i=0; (ch = *(name++)) != '\0';) {
  836. if (ch == '/')
  837. i = 0; /* overwrite what we wrote */
  838. else
  839. if (i < (sizeof(tcomm) - 1))
  840. tcomm[i++] = ch;
  841. }
  842. tcomm[i] = '\0';
  843. set_task_comm(current, tcomm);
  844. current->flags &= ~PF_RANDOMIZE;
  845. flush_thread();
  846. /* Set the new mm task size. We have to do that late because it may
  847. * depend on TIF_32BIT which is only updated in flush_thread() on
  848. * some architectures like powerpc
  849. */
  850. current->mm->task_size = TASK_SIZE;
  851. if (bprm->e_uid != current->euid || bprm->e_gid != current->egid) {
  852. suid_keys(current);
  853. set_dumpable(current->mm, suid_dumpable);
  854. current->pdeath_signal = 0;
  855. } else if (file_permission(bprm->file, MAY_READ) ||
  856. (bprm->interp_flags & BINPRM_FLAGS_ENFORCE_NONDUMP)) {
  857. suid_keys(current);
  858. set_dumpable(current->mm, suid_dumpable);
  859. }
  860. /* An exec changes our domain. We are no longer part of the thread
  861. group */
  862. current->self_exec_id++;
  863. flush_signal_handlers(current, 0);
  864. flush_old_files(current->files);
  865. return 0;
  866. out:
  867. return retval;
  868. }
  869. EXPORT_SYMBOL(flush_old_exec);
  870. /*
  871. * Fill the binprm structure from the inode.
  872. * Check permissions, then read the first 128 (BINPRM_BUF_SIZE) bytes
  873. */
  874. int prepare_binprm(struct linux_binprm *bprm)
  875. {
  876. int mode;
  877. struct inode * inode = bprm->file->f_path.dentry->d_inode;
  878. int retval;
  879. mode = inode->i_mode;
  880. if (bprm->file->f_op == NULL)
  881. return -EACCES;
  882. bprm->e_uid = current->euid;
  883. bprm->e_gid = current->egid;
  884. if(!(bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID)) {
  885. /* Set-uid? */
  886. if (mode & S_ISUID) {
  887. current->personality &= ~PER_CLEAR_ON_SETID;
  888. bprm->e_uid = inode->i_uid;
  889. }
  890. /* Set-gid? */
  891. /*
  892. * If setgid is set but no group execute bit then this
  893. * is a candidate for mandatory locking, not a setgid
  894. * executable.
  895. */
  896. if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
  897. current->personality &= ~PER_CLEAR_ON_SETID;
  898. bprm->e_gid = inode->i_gid;
  899. }
  900. }
  901. /* fill in binprm security blob */
  902. retval = security_bprm_set(bprm);
  903. if (retval)
  904. return retval;
  905. memset(bprm->buf,0,BINPRM_BUF_SIZE);
  906. return kernel_read(bprm->file,0,bprm->buf,BINPRM_BUF_SIZE);
  907. }
  908. EXPORT_SYMBOL(prepare_binprm);
  909. static int unsafe_exec(struct task_struct *p)
  910. {
  911. int unsafe = 0;
  912. if (p->ptrace & PT_PTRACED) {
  913. if (p->ptrace & PT_PTRACE_CAP)
  914. unsafe |= LSM_UNSAFE_PTRACE_CAP;
  915. else
  916. unsafe |= LSM_UNSAFE_PTRACE;
  917. }
  918. if (atomic_read(&p->fs->count) > 1 ||
  919. atomic_read(&p->files->count) > 1 ||
  920. atomic_read(&p->sighand->count) > 1)
  921. unsafe |= LSM_UNSAFE_SHARE;
  922. return unsafe;
  923. }
  924. void compute_creds(struct linux_binprm *bprm)
  925. {
  926. int unsafe;
  927. if (bprm->e_uid != current->uid) {
  928. suid_keys(current);
  929. current->pdeath_signal = 0;
  930. }
  931. exec_keys(current);
  932. task_lock(current);
  933. unsafe = unsafe_exec(current);
  934. security_bprm_apply_creds(bprm, unsafe);
  935. task_unlock(current);
  936. security_bprm_post_apply_creds(bprm);
  937. }
  938. EXPORT_SYMBOL(compute_creds);
  939. /*
  940. * Arguments are '\0' separated strings found at the location bprm->p
  941. * points to; chop off the first by relocating brpm->p to right after
  942. * the first '\0' encountered.
  943. */
  944. int remove_arg_zero(struct linux_binprm *bprm)
  945. {
  946. int ret = 0;
  947. unsigned long offset;
  948. char *kaddr;
  949. struct page *page;
  950. if (!bprm->argc)
  951. return 0;
  952. do {
  953. offset = bprm->p & ~PAGE_MASK;
  954. page = get_arg_page(bprm, bprm->p, 0);
  955. if (!page) {
  956. ret = -EFAULT;
  957. goto out;
  958. }
  959. kaddr = kmap_atomic(page, KM_USER0);
  960. for (; offset < PAGE_SIZE && kaddr[offset];
  961. offset++, bprm->p++)
  962. ;
  963. kunmap_atomic(kaddr, KM_USER0);
  964. put_arg_page(page);
  965. if (offset == PAGE_SIZE)
  966. free_arg_page(bprm, (bprm->p >> PAGE_SHIFT) - 1);
  967. } while (offset == PAGE_SIZE);
  968. bprm->p++;
  969. bprm->argc--;
  970. ret = 0;
  971. out:
  972. return ret;
  973. }
  974. EXPORT_SYMBOL(remove_arg_zero);
  975. /*
  976. * cycle the list of binary formats handler, until one recognizes the image
  977. */
  978. int search_binary_handler(struct linux_binprm *bprm,struct pt_regs *regs)
  979. {
  980. int try,retval;
  981. struct linux_binfmt *fmt;
  982. #if defined(__alpha__) && defined(CONFIG_ARCH_SUPPORTS_AOUT)
  983. /* handle /sbin/loader.. */
  984. {
  985. struct exec * eh = (struct exec *) bprm->buf;
  986. if (!bprm->loader && eh->fh.f_magic == 0x183 &&
  987. (eh->fh.f_flags & 0x3000) == 0x3000)
  988. {
  989. struct file * file;
  990. unsigned long loader;
  991. allow_write_access(bprm->file);
  992. fput(bprm->file);
  993. bprm->file = NULL;
  994. loader = bprm->vma->vm_end - sizeof(void *);
  995. file = open_exec("/sbin/loader");
  996. retval = PTR_ERR(file);
  997. if (IS_ERR(file))
  998. return retval;
  999. /* Remember if the application is TASO. */
  1000. bprm->sh_bang = eh->ah.entry < 0x100000000UL;
  1001. bprm->file = file;
  1002. bprm->loader = loader;
  1003. retval = prepare_binprm(bprm);
  1004. if (retval<0)
  1005. return retval;
  1006. /* should call search_binary_handler recursively here,
  1007. but it does not matter */
  1008. }
  1009. }
  1010. #endif
  1011. retval = security_bprm_check(bprm);
  1012. if (retval)
  1013. return retval;
  1014. /* kernel module loader fixup */
  1015. /* so we don't try to load run modprobe in kernel space. */
  1016. set_fs(USER_DS);
  1017. retval = audit_bprm(bprm);
  1018. if (retval)
  1019. return retval;
  1020. retval = -ENOENT;
  1021. for (try=0; try<2; try++) {
  1022. read_lock(&binfmt_lock);
  1023. list_for_each_entry(fmt, &formats, lh) {
  1024. int (*fn)(struct linux_binprm *, struct pt_regs *) = fmt->load_binary;
  1025. if (!fn)
  1026. continue;
  1027. if (!try_module_get(fmt->module))
  1028. continue;
  1029. read_unlock(&binfmt_lock);
  1030. retval = fn(bprm, regs);
  1031. if (retval >= 0) {
  1032. put_binfmt(fmt);
  1033. allow_write_access(bprm->file);
  1034. if (bprm->file)
  1035. fput(bprm->file);
  1036. bprm->file = NULL;
  1037. current->did_exec = 1;
  1038. proc_exec_connector(current);
  1039. return retval;
  1040. }
  1041. read_lock(&binfmt_lock);
  1042. put_binfmt(fmt);
  1043. if (retval != -ENOEXEC || bprm->mm == NULL)
  1044. break;
  1045. if (!bprm->file) {
  1046. read_unlock(&binfmt_lock);
  1047. return retval;
  1048. }
  1049. }
  1050. read_unlock(&binfmt_lock);
  1051. if (retval != -ENOEXEC || bprm->mm == NULL) {
  1052. break;
  1053. #ifdef CONFIG_KMOD
  1054. }else{
  1055. #define printable(c) (((c)=='\t') || ((c)=='\n') || (0x20<=(c) && (c)<=0x7e))
  1056. if (printable(bprm->buf[0]) &&
  1057. printable(bprm->buf[1]) &&
  1058. printable(bprm->buf[2]) &&
  1059. printable(bprm->buf[3]))
  1060. break; /* -ENOEXEC */
  1061. request_module("binfmt-%04x", *(unsigned short *)(&bprm->buf[2]));
  1062. #endif
  1063. }
  1064. }
  1065. return retval;
  1066. }
  1067. EXPORT_SYMBOL(search_binary_handler);
  1068. void free_bprm(struct linux_binprm *bprm)
  1069. {
  1070. free_arg_pages(bprm);
  1071. kfree(bprm);
  1072. }
  1073. /*
  1074. * sys_execve() executes a new program.
  1075. */
  1076. int do_execve(char * filename,
  1077. char __user *__user *argv,
  1078. char __user *__user *envp,
  1079. struct pt_regs * regs)
  1080. {
  1081. struct linux_binprm *bprm;
  1082. struct file *file;
  1083. struct files_struct *displaced;
  1084. int retval;
  1085. retval = unshare_files(&displaced);
  1086. if (retval)
  1087. goto out_ret;
  1088. retval = -ENOMEM;
  1089. bprm = kzalloc(sizeof(*bprm), GFP_KERNEL);
  1090. if (!bprm)
  1091. goto out_files;
  1092. file = open_exec(filename);
  1093. retval = PTR_ERR(file);
  1094. if (IS_ERR(file))
  1095. goto out_kfree;
  1096. sched_exec();
  1097. bprm->file = file;
  1098. bprm->filename = filename;
  1099. bprm->interp = filename;
  1100. retval = bprm_mm_init(bprm);
  1101. if (retval)
  1102. goto out_file;
  1103. bprm->argc = count(argv, MAX_ARG_STRINGS);
  1104. if ((retval = bprm->argc) < 0)
  1105. goto out_mm;
  1106. bprm->envc = count(envp, MAX_ARG_STRINGS);
  1107. if ((retval = bprm->envc) < 0)
  1108. goto out_mm;
  1109. retval = security_bprm_alloc(bprm);
  1110. if (retval)
  1111. goto out;
  1112. retval = prepare_binprm(bprm);
  1113. if (retval < 0)
  1114. goto out;
  1115. retval = copy_strings_kernel(1, &bprm->filename, bprm);
  1116. if (retval < 0)
  1117. goto out;
  1118. bprm->exec = bprm->p;
  1119. retval = copy_strings(bprm->envc, envp, bprm);
  1120. if (retval < 0)
  1121. goto out;
  1122. retval = copy_strings(bprm->argc, argv, bprm);
  1123. if (retval < 0)
  1124. goto out;
  1125. retval = search_binary_handler(bprm,regs);
  1126. if (retval >= 0) {
  1127. /* execve success */
  1128. security_bprm_free(bprm);
  1129. acct_update_integrals(current);
  1130. free_bprm(bprm);
  1131. if (displaced)
  1132. put_files_struct(displaced);
  1133. return retval;
  1134. }
  1135. out:
  1136. if (bprm->security)
  1137. security_bprm_free(bprm);
  1138. out_mm:
  1139. if (bprm->mm)
  1140. mmput (bprm->mm);
  1141. out_file:
  1142. if (bprm->file) {
  1143. allow_write_access(bprm->file);
  1144. fput(bprm->file);
  1145. }
  1146. out_kfree:
  1147. free_bprm(bprm);
  1148. out_files:
  1149. if (displaced)
  1150. reset_files_struct(displaced);
  1151. out_ret:
  1152. return retval;
  1153. }
  1154. int set_binfmt(struct linux_binfmt *new)
  1155. {
  1156. struct linux_binfmt *old = current->binfmt;
  1157. if (new) {
  1158. if (!try_module_get(new->module))
  1159. return -1;
  1160. }
  1161. current->binfmt = new;
  1162. if (old)
  1163. module_put(old->module);
  1164. return 0;
  1165. }
  1166. EXPORT_SYMBOL(set_binfmt);
  1167. /* format_corename will inspect the pattern parameter, and output a
  1168. * name into corename, which must have space for at least
  1169. * CORENAME_MAX_SIZE bytes plus one byte for the zero terminator.
  1170. */
  1171. static int format_corename(char *corename, const char *pattern, long signr)
  1172. {
  1173. const char *pat_ptr = pattern;
  1174. char *out_ptr = corename;
  1175. char *const out_end = corename + CORENAME_MAX_SIZE;
  1176. int rc;
  1177. int pid_in_pattern = 0;
  1178. int ispipe = 0;
  1179. if (*pattern == '|')
  1180. ispipe = 1;
  1181. /* Repeat as long as we have more pattern to process and more output
  1182. space */
  1183. while (*pat_ptr) {
  1184. if (*pat_ptr != '%') {
  1185. if (out_ptr == out_end)
  1186. goto out;
  1187. *out_ptr++ = *pat_ptr++;
  1188. } else {
  1189. switch (*++pat_ptr) {
  1190. case 0:
  1191. goto out;
  1192. /* Double percent, output one percent */
  1193. case '%':
  1194. if (out_ptr == out_end)
  1195. goto out;
  1196. *out_ptr++ = '%';
  1197. break;
  1198. /* pid */
  1199. case 'p':
  1200. pid_in_pattern = 1;
  1201. rc = snprintf(out_ptr, out_end - out_ptr,
  1202. "%d", task_tgid_vnr(current));
  1203. if (rc > out_end - out_ptr)
  1204. goto out;
  1205. out_ptr += rc;
  1206. break;
  1207. /* uid */
  1208. case 'u':
  1209. rc = snprintf(out_ptr, out_end - out_ptr,
  1210. "%d", current->uid);
  1211. if (rc > out_end - out_ptr)
  1212. goto out;
  1213. out_ptr += rc;
  1214. break;
  1215. /* gid */
  1216. case 'g':
  1217. rc = snprintf(out_ptr, out_end - out_ptr,
  1218. "%d", current->gid);
  1219. if (rc > out_end - out_ptr)
  1220. goto out;
  1221. out_ptr += rc;
  1222. break;
  1223. /* signal that caused the coredump */
  1224. case 's':
  1225. rc = snprintf(out_ptr, out_end - out_ptr,
  1226. "%ld", signr);
  1227. if (rc > out_end - out_ptr)
  1228. goto out;
  1229. out_ptr += rc;
  1230. break;
  1231. /* UNIX time of coredump */
  1232. case 't': {
  1233. struct timeval tv;
  1234. do_gettimeofday(&tv);
  1235. rc = snprintf(out_ptr, out_end - out_ptr,
  1236. "%lu", tv.tv_sec);
  1237. if (rc > out_end - out_ptr)
  1238. goto out;
  1239. out_ptr += rc;
  1240. break;
  1241. }
  1242. /* hostname */
  1243. case 'h':
  1244. down_read(&uts_sem);
  1245. rc = snprintf(out_ptr, out_end - out_ptr,
  1246. "%s", utsname()->nodename);
  1247. up_read(&uts_sem);
  1248. if (rc > out_end - out_ptr)
  1249. goto out;
  1250. out_ptr += rc;
  1251. break;
  1252. /* executable */
  1253. case 'e':
  1254. rc = snprintf(out_ptr, out_end - out_ptr,
  1255. "%s", current->comm);
  1256. if (rc > out_end - out_ptr)
  1257. goto out;
  1258. out_ptr += rc;
  1259. break;
  1260. /* core limit size */
  1261. case 'c':
  1262. rc = snprintf(out_ptr, out_end - out_ptr,
  1263. "%lu", current->signal->rlim[RLIMIT_CORE].rlim_cur);
  1264. if (rc > out_end - out_ptr)
  1265. goto out;
  1266. out_ptr += rc;
  1267. break;
  1268. default:
  1269. break;
  1270. }
  1271. ++pat_ptr;
  1272. }
  1273. }
  1274. /* Backward compatibility with core_uses_pid:
  1275. *
  1276. * If core_pattern does not include a %p (as is the default)
  1277. * and core_uses_pid is set, then .%pid will be appended to
  1278. * the filename. Do not do this for piped commands. */
  1279. if (!ispipe && !pid_in_pattern
  1280. && (core_uses_pid || atomic_read(&current->mm->mm_users) != 1)) {
  1281. rc = snprintf(out_ptr, out_end - out_ptr,
  1282. ".%d", task_tgid_vnr(current));
  1283. if (rc > out_end - out_ptr)
  1284. goto out;
  1285. out_ptr += rc;
  1286. }
  1287. out:
  1288. *out_ptr = 0;
  1289. return ispipe;
  1290. }
  1291. static void zap_process(struct task_struct *start)
  1292. {
  1293. struct task_struct *t;
  1294. start->signal->flags = SIGNAL_GROUP_EXIT;
  1295. start->signal->group_stop_count = 0;
  1296. t = start;
  1297. do {
  1298. if (t != current && t->mm) {
  1299. t->mm->core_waiters++;
  1300. sigaddset(&t->pending.signal, SIGKILL);
  1301. signal_wake_up(t, 1);
  1302. }
  1303. } while ((t = next_thread(t)) != start);
  1304. }
  1305. static inline int zap_threads(struct task_struct *tsk, struct mm_struct *mm,
  1306. int exit_code)
  1307. {
  1308. struct task_struct *g, *p;
  1309. unsigned long flags;
  1310. int err = -EAGAIN;
  1311. spin_lock_irq(&tsk->sighand->siglock);
  1312. if (!signal_group_exit(tsk->signal)) {
  1313. tsk->signal->group_exit_code = exit_code;
  1314. zap_process(tsk);
  1315. err = 0;
  1316. }
  1317. spin_unlock_irq(&tsk->sighand->siglock);
  1318. if (err)
  1319. return err;
  1320. if (atomic_read(&mm->mm_users) == mm->core_waiters + 1)
  1321. goto done;
  1322. rcu_read_lock();
  1323. for_each_process(g) {
  1324. if (g == tsk->group_leader)
  1325. continue;
  1326. p = g;
  1327. do {
  1328. if (p->mm) {
  1329. if (p->mm == mm) {
  1330. /*
  1331. * p->sighand can't disappear, but
  1332. * may be changed by de_thread()
  1333. */
  1334. lock_task_sighand(p, &flags);
  1335. zap_process(p);
  1336. unlock_task_sighand(p, &flags);
  1337. }
  1338. break;
  1339. }
  1340. } while ((p = next_thread(p)) != g);
  1341. }
  1342. rcu_read_unlock();
  1343. done:
  1344. return mm->core_waiters;
  1345. }
  1346. static int coredump_wait(int exit_code)
  1347. {
  1348. struct task_struct *tsk = current;
  1349. struct mm_struct *mm = tsk->mm;
  1350. struct completion startup_done;
  1351. struct completion *vfork_done;
  1352. int core_waiters;
  1353. init_completion(&mm->core_done);
  1354. init_completion(&startup_done);
  1355. mm->core_startup_done = &startup_done;
  1356. core_waiters = zap_threads(tsk, mm, exit_code);
  1357. up_write(&mm->mmap_sem);
  1358. if (unlikely(core_waiters < 0))
  1359. goto fail;
  1360. /*
  1361. * Make sure nobody is waiting for us to release the VM,
  1362. * otherwise we can deadlock when we wait on each other
  1363. */
  1364. vfork_done = tsk->vfork_done;
  1365. if (vfork_done) {
  1366. tsk->vfork_done = NULL;
  1367. complete(vfork_done);
  1368. }
  1369. if (core_waiters)
  1370. wait_for_completion(&startup_done);
  1371. fail:
  1372. BUG_ON(mm->core_waiters);
  1373. return core_waiters;
  1374. }
  1375. /*
  1376. * set_dumpable converts traditional three-value dumpable to two flags and
  1377. * stores them into mm->flags. It modifies lower two bits of mm->flags, but
  1378. * these bits are not changed atomically. So get_dumpable can observe the
  1379. * intermediate state. To avoid doing unexpected behavior, get get_dumpable
  1380. * return either old dumpable or new one by paying attention to the order of
  1381. * modifying the bits.
  1382. *
  1383. * dumpable | mm->flags (binary)
  1384. * old new | initial interim final
  1385. * ---------+-----------------------
  1386. * 0 1 | 00 01 01
  1387. * 0 2 | 00 10(*) 11
  1388. * 1 0 | 01 00 00
  1389. * 1 2 | 01 11 11
  1390. * 2 0 | 11 10(*) 00
  1391. * 2 1 | 11 11 01
  1392. *
  1393. * (*) get_dumpable regards interim value of 10 as 11.
  1394. */
  1395. void set_dumpable(struct mm_struct *mm, int value)
  1396. {
  1397. switch (value) {
  1398. case 0:
  1399. clear_bit(MMF_DUMPABLE, &mm->flags);
  1400. smp_wmb();
  1401. clear_bit(MMF_DUMP_SECURELY, &mm->flags);
  1402. break;
  1403. case 1:
  1404. set_bit(MMF_DUMPABLE, &mm->flags);
  1405. smp_wmb();
  1406. clear_bit(MMF_DUMP_SECURELY, &mm->flags);
  1407. break;
  1408. case 2:
  1409. set_bit(MMF_DUMP_SECURELY, &mm->flags);
  1410. smp_wmb();
  1411. set_bit(MMF_DUMPABLE, &mm->flags);
  1412. break;
  1413. }
  1414. }
  1415. int get_dumpable(struct mm_struct *mm)
  1416. {
  1417. int ret;
  1418. ret = mm->flags & 0x3;
  1419. return (ret >= 2) ? 2 : ret;
  1420. }
  1421. int do_coredump(long signr, int exit_code, struct pt_regs * regs)
  1422. {
  1423. char corename[CORENAME_MAX_SIZE + 1];
  1424. struct mm_struct *mm = current->mm;
  1425. struct linux_binfmt * binfmt;
  1426. struct inode * inode;
  1427. struct file * file;
  1428. int retval = 0;
  1429. int fsuid = current->fsuid;
  1430. int flag = 0;
  1431. int ispipe = 0;
  1432. unsigned long core_limit = current->signal->rlim[RLIMIT_CORE].rlim_cur;
  1433. char **helper_argv = NULL;
  1434. int helper_argc = 0;
  1435. char *delimit;
  1436. audit_core_dumps(signr);
  1437. binfmt = current->binfmt;
  1438. if (!binfmt || !binfmt->core_dump)
  1439. goto fail;
  1440. down_write(&mm->mmap_sem);
  1441. /*
  1442. * If another thread got here first, or we are not dumpable, bail out.
  1443. */
  1444. if (mm->core_waiters || !get_dumpable(mm)) {
  1445. up_write(&mm->mmap_sem);
  1446. goto fail;
  1447. }
  1448. /*
  1449. * We cannot trust fsuid as being the "true" uid of the
  1450. * process nor do we know its entire history. We only know it
  1451. * was tainted so we dump it as root in mode 2.
  1452. */
  1453. if (get_dumpable(mm) == 2) { /* Setuid core dump mode */
  1454. flag = O_EXCL; /* Stop rewrite attacks */
  1455. current->fsuid = 0; /* Dump root private */
  1456. }
  1457. retval = coredump_wait(exit_code);
  1458. if (retval < 0)
  1459. goto fail;
  1460. /*
  1461. * Clear any false indication of pending signals that might
  1462. * be seen by the filesystem code called to write the core file.
  1463. */
  1464. clear_thread_flag(TIF_SIGPENDING);
  1465. /*
  1466. * lock_kernel() because format_corename() is controlled by sysctl, which
  1467. * uses lock_kernel()
  1468. */
  1469. lock_kernel();
  1470. ispipe = format_corename(corename, core_pattern, signr);
  1471. unlock_kernel();
  1472. /*
  1473. * Don't bother to check the RLIMIT_CORE value if core_pattern points
  1474. * to a pipe. Since we're not writing directly to the filesystem
  1475. * RLIMIT_CORE doesn't really apply, as no actual core file will be
  1476. * created unless the pipe reader choses to write out the core file
  1477. * at which point file size limits and permissions will be imposed
  1478. * as it does with any other process
  1479. */
  1480. if ((!ispipe) && (core_limit < binfmt->min_coredump))
  1481. goto fail_unlock;
  1482. if (ispipe) {
  1483. helper_argv = argv_split(GFP_KERNEL, corename+1, &helper_argc);
  1484. /* Terminate the string before the first option */
  1485. delimit = strchr(corename, ' ');
  1486. if (delimit)
  1487. *delimit = '\0';
  1488. delimit = strrchr(helper_argv[0], '/');
  1489. if (delimit)
  1490. delimit++;
  1491. else
  1492. delimit = helper_argv[0];
  1493. if (!strcmp(delimit, current->comm)) {
  1494. printk(KERN_NOTICE "Recursive core dump detected, "
  1495. "aborting\n");
  1496. goto fail_unlock;
  1497. }
  1498. core_limit = RLIM_INFINITY;
  1499. /* SIGPIPE can happen, but it's just never processed */
  1500. if (call_usermodehelper_pipe(corename+1, helper_argv, NULL,
  1501. &file)) {
  1502. printk(KERN_INFO "Core dump to %s pipe failed\n",
  1503. corename);
  1504. goto fail_unlock;
  1505. }
  1506. } else
  1507. file = filp_open(corename,
  1508. O_CREAT | 2 | O_NOFOLLOW | O_LARGEFILE | flag,
  1509. 0600);
  1510. if (IS_ERR(file))
  1511. goto fail_unlock;
  1512. inode = file->f_path.dentry->d_inode;
  1513. if (inode->i_nlink > 1)
  1514. goto close_fail; /* multiple links - don't dump */
  1515. if (!ispipe && d_unhashed(file->f_path.dentry))
  1516. goto close_fail;
  1517. /* AK: actually i see no reason to not allow this for named pipes etc.,
  1518. but keep the previous behaviour for now. */
  1519. if (!ispipe && !S_ISREG(inode->i_mode))
  1520. goto close_fail;
  1521. /*
  1522. * Dont allow local users get cute and trick others to coredump
  1523. * into their pre-created files:
  1524. */
  1525. if (inode->i_uid != current->fsuid)
  1526. goto close_fail;
  1527. if (!file->f_op)
  1528. goto close_fail;
  1529. if (!file->f_op->write)
  1530. goto close_fail;
  1531. if (!ispipe && do_truncate(file->f_path.dentry, 0, 0, file) != 0)
  1532. goto close_fail;
  1533. retval = binfmt->core_dump(signr, regs, file, core_limit);
  1534. if (retval)
  1535. current->signal->group_exit_code |= 0x80;
  1536. close_fail:
  1537. filp_close(file, NULL);
  1538. fail_unlock:
  1539. if (helper_argv)
  1540. argv_free(helper_argv);
  1541. current->fsuid = fsuid;
  1542. complete_all(&mm->core_done);
  1543. fail:
  1544. return retval;
  1545. }