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