exec.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148
  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/mm.h>
  27. #include <linux/stat.h>
  28. #include <linux/fcntl.h>
  29. #include <linux/swap.h>
  30. #include <linux/string.h>
  31. #include <linux/init.h>
  32. #include <linux/pagemap.h>
  33. #include <linux/perf_event.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/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/mount.h>
  45. #include <linux/security.h>
  46. #include <linux/syscalls.h>
  47. #include <linux/tsacct_kern.h>
  48. #include <linux/cn_proc.h>
  49. #include <linux/audit.h>
  50. #include <linux/tracehook.h>
  51. #include <linux/kmod.h>
  52. #include <linux/fsnotify.h>
  53. #include <linux/fs_struct.h>
  54. #include <linux/pipe_fs_i.h>
  55. #include <linux/oom.h>
  56. #include <asm/uaccess.h>
  57. #include <asm/mmu_context.h>
  58. #include <asm/tlb.h>
  59. #include "internal.h"
  60. int core_uses_pid;
  61. char core_pattern[CORENAME_MAX_SIZE] = "core";
  62. unsigned int core_pipe_limit;
  63. int suid_dumpable = 0;
  64. struct core_name {
  65. char *corename;
  66. int used, size;
  67. };
  68. static atomic_t call_count = ATOMIC_INIT(1);
  69. /* The maximal length of core_pattern is also specified in sysctl.c */
  70. static LIST_HEAD(formats);
  71. static DEFINE_RWLOCK(binfmt_lock);
  72. int __register_binfmt(struct linux_binfmt * fmt, int insert)
  73. {
  74. if (!fmt)
  75. return -EINVAL;
  76. write_lock(&binfmt_lock);
  77. insert ? list_add(&fmt->lh, &formats) :
  78. list_add_tail(&fmt->lh, &formats);
  79. write_unlock(&binfmt_lock);
  80. return 0;
  81. }
  82. EXPORT_SYMBOL(__register_binfmt);
  83. void unregister_binfmt(struct linux_binfmt * fmt)
  84. {
  85. write_lock(&binfmt_lock);
  86. list_del(&fmt->lh);
  87. write_unlock(&binfmt_lock);
  88. }
  89. EXPORT_SYMBOL(unregister_binfmt);
  90. static inline void put_binfmt(struct linux_binfmt * fmt)
  91. {
  92. module_put(fmt->module);
  93. }
  94. /*
  95. * Note that a shared library must be both readable and executable due to
  96. * security reasons.
  97. *
  98. * Also note that we take the address to load from from the file itself.
  99. */
  100. SYSCALL_DEFINE1(uselib, const char __user *, library)
  101. {
  102. struct file *file;
  103. char *tmp = getname(library);
  104. int error = PTR_ERR(tmp);
  105. static const struct open_flags uselib_flags = {
  106. .open_flag = O_LARGEFILE | O_RDONLY | __FMODE_EXEC,
  107. .acc_mode = MAY_READ | MAY_EXEC | MAY_OPEN,
  108. .intent = LOOKUP_OPEN
  109. };
  110. if (IS_ERR(tmp))
  111. goto out;
  112. file = do_filp_open(AT_FDCWD, tmp, &uselib_flags, LOOKUP_FOLLOW);
  113. putname(tmp);
  114. error = PTR_ERR(file);
  115. if (IS_ERR(file))
  116. goto out;
  117. error = -EINVAL;
  118. if (!S_ISREG(file->f_path.dentry->d_inode->i_mode))
  119. goto exit;
  120. error = -EACCES;
  121. if (file->f_path.mnt->mnt_flags & MNT_NOEXEC)
  122. goto exit;
  123. fsnotify_open(file);
  124. error = -ENOEXEC;
  125. if(file->f_op) {
  126. struct linux_binfmt * fmt;
  127. read_lock(&binfmt_lock);
  128. list_for_each_entry(fmt, &formats, lh) {
  129. if (!fmt->load_shlib)
  130. continue;
  131. if (!try_module_get(fmt->module))
  132. continue;
  133. read_unlock(&binfmt_lock);
  134. error = fmt->load_shlib(file);
  135. read_lock(&binfmt_lock);
  136. put_binfmt(fmt);
  137. if (error != -ENOEXEC)
  138. break;
  139. }
  140. read_unlock(&binfmt_lock);
  141. }
  142. exit:
  143. fput(file);
  144. out:
  145. return error;
  146. }
  147. #ifdef CONFIG_MMU
  148. void acct_arg_size(struct linux_binprm *bprm, unsigned long pages)
  149. {
  150. struct mm_struct *mm = current->mm;
  151. long diff = (long)(pages - bprm->vma_pages);
  152. if (!mm || !diff)
  153. return;
  154. bprm->vma_pages = pages;
  155. #ifdef SPLIT_RSS_COUNTING
  156. add_mm_counter(mm, MM_ANONPAGES, diff);
  157. #else
  158. spin_lock(&mm->page_table_lock);
  159. add_mm_counter(mm, MM_ANONPAGES, diff);
  160. spin_unlock(&mm->page_table_lock);
  161. #endif
  162. }
  163. struct page *get_arg_page(struct linux_binprm *bprm, unsigned long pos,
  164. int write)
  165. {
  166. struct page *page;
  167. int ret;
  168. #ifdef CONFIG_STACK_GROWSUP
  169. if (write) {
  170. ret = expand_stack_downwards(bprm->vma, pos);
  171. if (ret < 0)
  172. return NULL;
  173. }
  174. #endif
  175. ret = get_user_pages(current, bprm->mm, pos,
  176. 1, write, 1, &page, NULL);
  177. if (ret <= 0)
  178. return NULL;
  179. if (write) {
  180. unsigned long size = bprm->vma->vm_end - bprm->vma->vm_start;
  181. struct rlimit *rlim;
  182. acct_arg_size(bprm, size / PAGE_SIZE);
  183. /*
  184. * We've historically supported up to 32 pages (ARG_MAX)
  185. * of argument strings even with small stacks
  186. */
  187. if (size <= ARG_MAX)
  188. return page;
  189. /*
  190. * Limit to 1/4-th the stack size for the argv+env strings.
  191. * This ensures that:
  192. * - the remaining binfmt code will not run out of stack space,
  193. * - the program will have a reasonable amount of stack left
  194. * to work from.
  195. */
  196. rlim = current->signal->rlim;
  197. if (size > ACCESS_ONCE(rlim[RLIMIT_STACK].rlim_cur) / 4) {
  198. put_page(page);
  199. return NULL;
  200. }
  201. }
  202. return page;
  203. }
  204. static void put_arg_page(struct page *page)
  205. {
  206. put_page(page);
  207. }
  208. static void free_arg_page(struct linux_binprm *bprm, int i)
  209. {
  210. }
  211. static void free_arg_pages(struct linux_binprm *bprm)
  212. {
  213. }
  214. static void flush_arg_page(struct linux_binprm *bprm, unsigned long pos,
  215. struct page *page)
  216. {
  217. flush_cache_page(bprm->vma, pos, page_to_pfn(page));
  218. }
  219. static int __bprm_mm_init(struct linux_binprm *bprm)
  220. {
  221. int err;
  222. struct vm_area_struct *vma = NULL;
  223. struct mm_struct *mm = bprm->mm;
  224. bprm->vma = vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
  225. if (!vma)
  226. return -ENOMEM;
  227. down_write(&mm->mmap_sem);
  228. vma->vm_mm = mm;
  229. /*
  230. * Place the stack at the largest stack address the architecture
  231. * supports. Later, we'll move this to an appropriate place. We don't
  232. * use STACK_TOP because that can depend on attributes which aren't
  233. * configured yet.
  234. */
  235. BUG_ON(VM_STACK_FLAGS & VM_STACK_INCOMPLETE_SETUP);
  236. vma->vm_end = STACK_TOP_MAX;
  237. vma->vm_start = vma->vm_end - PAGE_SIZE;
  238. vma->vm_flags = VM_STACK_FLAGS | VM_STACK_INCOMPLETE_SETUP;
  239. vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
  240. INIT_LIST_HEAD(&vma->anon_vma_chain);
  241. err = security_file_mmap(NULL, 0, 0, 0, vma->vm_start, 1);
  242. if (err)
  243. goto err;
  244. err = insert_vm_struct(mm, vma);
  245. if (err)
  246. goto err;
  247. mm->stack_vm = mm->total_vm = 1;
  248. up_write(&mm->mmap_sem);
  249. bprm->p = vma->vm_end - sizeof(void *);
  250. return 0;
  251. err:
  252. up_write(&mm->mmap_sem);
  253. bprm->vma = NULL;
  254. kmem_cache_free(vm_area_cachep, vma);
  255. return err;
  256. }
  257. static bool valid_arg_len(struct linux_binprm *bprm, long len)
  258. {
  259. return len <= MAX_ARG_STRLEN;
  260. }
  261. #else
  262. void acct_arg_size(struct linux_binprm *bprm, unsigned long pages)
  263. {
  264. }
  265. struct page *get_arg_page(struct linux_binprm *bprm, unsigned long pos,
  266. int write)
  267. {
  268. struct page *page;
  269. page = bprm->page[pos / PAGE_SIZE];
  270. if (!page && write) {
  271. page = alloc_page(GFP_HIGHUSER|__GFP_ZERO);
  272. if (!page)
  273. return NULL;
  274. bprm->page[pos / PAGE_SIZE] = page;
  275. }
  276. return page;
  277. }
  278. static void put_arg_page(struct page *page)
  279. {
  280. }
  281. static void free_arg_page(struct linux_binprm *bprm, int i)
  282. {
  283. if (bprm->page[i]) {
  284. __free_page(bprm->page[i]);
  285. bprm->page[i] = NULL;
  286. }
  287. }
  288. static void free_arg_pages(struct linux_binprm *bprm)
  289. {
  290. int i;
  291. for (i = 0; i < MAX_ARG_PAGES; i++)
  292. free_arg_page(bprm, i);
  293. }
  294. static void flush_arg_page(struct linux_binprm *bprm, unsigned long pos,
  295. struct page *page)
  296. {
  297. }
  298. static int __bprm_mm_init(struct linux_binprm *bprm)
  299. {
  300. bprm->p = PAGE_SIZE * MAX_ARG_PAGES - sizeof(void *);
  301. return 0;
  302. }
  303. static bool valid_arg_len(struct linux_binprm *bprm, long len)
  304. {
  305. return len <= bprm->p;
  306. }
  307. #endif /* CONFIG_MMU */
  308. /*
  309. * Create a new mm_struct and populate it with a temporary stack
  310. * vm_area_struct. We don't have enough context at this point to set the stack
  311. * flags, permissions, and offset, so we use temporary values. We'll update
  312. * them later in setup_arg_pages().
  313. */
  314. int bprm_mm_init(struct linux_binprm *bprm)
  315. {
  316. int err;
  317. struct mm_struct *mm = NULL;
  318. bprm->mm = mm = mm_alloc();
  319. err = -ENOMEM;
  320. if (!mm)
  321. goto err;
  322. err = init_new_context(current, mm);
  323. if (err)
  324. goto err;
  325. err = __bprm_mm_init(bprm);
  326. if (err)
  327. goto err;
  328. return 0;
  329. err:
  330. if (mm) {
  331. bprm->mm = NULL;
  332. mmdrop(mm);
  333. }
  334. return err;
  335. }
  336. static const char __user *
  337. get_user_arg_ptr(const char __user * const __user *argv, int nr)
  338. {
  339. const char __user *ptr;
  340. if (get_user(ptr, argv + nr))
  341. return ERR_PTR(-EFAULT);
  342. return ptr;
  343. }
  344. /*
  345. * count() counts the number of strings in array ARGV.
  346. */
  347. static int count(const char __user * const __user * argv, int max)
  348. {
  349. int i = 0;
  350. if (argv != NULL) {
  351. for (;;) {
  352. const char __user *p = get_user_arg_ptr(argv, i);
  353. if (!p)
  354. break;
  355. if (IS_ERR(p))
  356. return -EFAULT;
  357. if (i++ >= max)
  358. return -E2BIG;
  359. if (fatal_signal_pending(current))
  360. return -ERESTARTNOHAND;
  361. cond_resched();
  362. }
  363. }
  364. return i;
  365. }
  366. /*
  367. * 'copy_strings()' copies argument/environment strings from the old
  368. * processes's memory to the new process's stack. The call to get_user_pages()
  369. * ensures the destination page is created and not swapped out.
  370. */
  371. static int copy_strings(int argc, const char __user *const __user *argv,
  372. struct linux_binprm *bprm)
  373. {
  374. struct page *kmapped_page = NULL;
  375. char *kaddr = NULL;
  376. unsigned long kpos = 0;
  377. int ret;
  378. while (argc-- > 0) {
  379. const char __user *str;
  380. int len;
  381. unsigned long pos;
  382. ret = -EFAULT;
  383. str = get_user_arg_ptr(argv, argc);
  384. if (IS_ERR(str))
  385. goto out;
  386. len = strnlen_user(str, MAX_ARG_STRLEN);
  387. if (!len)
  388. goto out;
  389. ret = -E2BIG;
  390. if (!valid_arg_len(bprm, len))
  391. goto out;
  392. /* We're going to work our way backwords. */
  393. pos = bprm->p;
  394. str += len;
  395. bprm->p -= len;
  396. while (len > 0) {
  397. int offset, bytes_to_copy;
  398. if (fatal_signal_pending(current)) {
  399. ret = -ERESTARTNOHAND;
  400. goto out;
  401. }
  402. cond_resched();
  403. offset = pos % PAGE_SIZE;
  404. if (offset == 0)
  405. offset = PAGE_SIZE;
  406. bytes_to_copy = offset;
  407. if (bytes_to_copy > len)
  408. bytes_to_copy = len;
  409. offset -= bytes_to_copy;
  410. pos -= bytes_to_copy;
  411. str -= bytes_to_copy;
  412. len -= bytes_to_copy;
  413. if (!kmapped_page || kpos != (pos & PAGE_MASK)) {
  414. struct page *page;
  415. page = get_arg_page(bprm, pos, 1);
  416. if (!page) {
  417. ret = -E2BIG;
  418. goto out;
  419. }
  420. if (kmapped_page) {
  421. flush_kernel_dcache_page(kmapped_page);
  422. kunmap(kmapped_page);
  423. put_arg_page(kmapped_page);
  424. }
  425. kmapped_page = page;
  426. kaddr = kmap(kmapped_page);
  427. kpos = pos & PAGE_MASK;
  428. flush_arg_page(bprm, kpos, kmapped_page);
  429. }
  430. if (copy_from_user(kaddr+offset, str, bytes_to_copy)) {
  431. ret = -EFAULT;
  432. goto out;
  433. }
  434. }
  435. }
  436. ret = 0;
  437. out:
  438. if (kmapped_page) {
  439. flush_kernel_dcache_page(kmapped_page);
  440. kunmap(kmapped_page);
  441. put_arg_page(kmapped_page);
  442. }
  443. return ret;
  444. }
  445. /*
  446. * Like copy_strings, but get argv and its values from kernel memory.
  447. */
  448. int copy_strings_kernel(int argc, const char *const *argv,
  449. struct linux_binprm *bprm)
  450. {
  451. int r;
  452. mm_segment_t oldfs = get_fs();
  453. set_fs(KERNEL_DS);
  454. r = copy_strings(argc, (const char __user *const __user *)argv, bprm);
  455. set_fs(oldfs);
  456. return r;
  457. }
  458. EXPORT_SYMBOL(copy_strings_kernel);
  459. #ifdef CONFIG_MMU
  460. /*
  461. * During bprm_mm_init(), we create a temporary stack at STACK_TOP_MAX. Once
  462. * the binfmt code determines where the new stack should reside, we shift it to
  463. * its final location. The process proceeds as follows:
  464. *
  465. * 1) Use shift to calculate the new vma endpoints.
  466. * 2) Extend vma to cover both the old and new ranges. This ensures the
  467. * arguments passed to subsequent functions are consistent.
  468. * 3) Move vma's page tables to the new range.
  469. * 4) Free up any cleared pgd range.
  470. * 5) Shrink the vma to cover only the new range.
  471. */
  472. static int shift_arg_pages(struct vm_area_struct *vma, unsigned long shift)
  473. {
  474. struct mm_struct *mm = vma->vm_mm;
  475. unsigned long old_start = vma->vm_start;
  476. unsigned long old_end = vma->vm_end;
  477. unsigned long length = old_end - old_start;
  478. unsigned long new_start = old_start - shift;
  479. unsigned long new_end = old_end - shift;
  480. struct mmu_gather *tlb;
  481. BUG_ON(new_start > new_end);
  482. /*
  483. * ensure there are no vmas between where we want to go
  484. * and where we are
  485. */
  486. if (vma != find_vma(mm, new_start))
  487. return -EFAULT;
  488. /*
  489. * cover the whole range: [new_start, old_end)
  490. */
  491. if (vma_adjust(vma, new_start, old_end, vma->vm_pgoff, NULL))
  492. return -ENOMEM;
  493. /*
  494. * move the page tables downwards, on failure we rely on
  495. * process cleanup to remove whatever mess we made.
  496. */
  497. if (length != move_page_tables(vma, old_start,
  498. vma, new_start, length))
  499. return -ENOMEM;
  500. lru_add_drain();
  501. tlb = tlb_gather_mmu(mm, 0);
  502. if (new_end > old_start) {
  503. /*
  504. * when the old and new regions overlap clear from new_end.
  505. */
  506. free_pgd_range(tlb, new_end, old_end, new_end,
  507. vma->vm_next ? vma->vm_next->vm_start : 0);
  508. } else {
  509. /*
  510. * otherwise, clean from old_start; this is done to not touch
  511. * the address space in [new_end, old_start) some architectures
  512. * have constraints on va-space that make this illegal (IA64) -
  513. * for the others its just a little faster.
  514. */
  515. free_pgd_range(tlb, old_start, old_end, new_end,
  516. vma->vm_next ? vma->vm_next->vm_start : 0);
  517. }
  518. tlb_finish_mmu(tlb, new_end, old_end);
  519. /*
  520. * Shrink the vma to just the new range. Always succeeds.
  521. */
  522. vma_adjust(vma, new_start, new_end, vma->vm_pgoff, NULL);
  523. return 0;
  524. }
  525. /*
  526. * Finalizes the stack vm_area_struct. The flags and permissions are updated,
  527. * the stack is optionally relocated, and some extra space is added.
  528. */
  529. int setup_arg_pages(struct linux_binprm *bprm,
  530. unsigned long stack_top,
  531. int executable_stack)
  532. {
  533. unsigned long ret;
  534. unsigned long stack_shift;
  535. struct mm_struct *mm = current->mm;
  536. struct vm_area_struct *vma = bprm->vma;
  537. struct vm_area_struct *prev = NULL;
  538. unsigned long vm_flags;
  539. unsigned long stack_base;
  540. unsigned long stack_size;
  541. unsigned long stack_expand;
  542. unsigned long rlim_stack;
  543. #ifdef CONFIG_STACK_GROWSUP
  544. /* Limit stack size to 1GB */
  545. stack_base = rlimit_max(RLIMIT_STACK);
  546. if (stack_base > (1 << 30))
  547. stack_base = 1 << 30;
  548. /* Make sure we didn't let the argument array grow too large. */
  549. if (vma->vm_end - vma->vm_start > stack_base)
  550. return -ENOMEM;
  551. stack_base = PAGE_ALIGN(stack_top - stack_base);
  552. stack_shift = vma->vm_start - stack_base;
  553. mm->arg_start = bprm->p - stack_shift;
  554. bprm->p = vma->vm_end - stack_shift;
  555. #else
  556. stack_top = arch_align_stack(stack_top);
  557. stack_top = PAGE_ALIGN(stack_top);
  558. if (unlikely(stack_top < mmap_min_addr) ||
  559. unlikely(vma->vm_end - vma->vm_start >= stack_top - mmap_min_addr))
  560. return -ENOMEM;
  561. stack_shift = vma->vm_end - stack_top;
  562. bprm->p -= stack_shift;
  563. mm->arg_start = bprm->p;
  564. #endif
  565. if (bprm->loader)
  566. bprm->loader -= stack_shift;
  567. bprm->exec -= stack_shift;
  568. down_write(&mm->mmap_sem);
  569. vm_flags = VM_STACK_FLAGS;
  570. /*
  571. * Adjust stack execute permissions; explicitly enable for
  572. * EXSTACK_ENABLE_X, disable for EXSTACK_DISABLE_X and leave alone
  573. * (arch default) otherwise.
  574. */
  575. if (unlikely(executable_stack == EXSTACK_ENABLE_X))
  576. vm_flags |= VM_EXEC;
  577. else if (executable_stack == EXSTACK_DISABLE_X)
  578. vm_flags &= ~VM_EXEC;
  579. vm_flags |= mm->def_flags;
  580. vm_flags |= VM_STACK_INCOMPLETE_SETUP;
  581. ret = mprotect_fixup(vma, &prev, vma->vm_start, vma->vm_end,
  582. vm_flags);
  583. if (ret)
  584. goto out_unlock;
  585. BUG_ON(prev != vma);
  586. /* Move stack pages down in memory. */
  587. if (stack_shift) {
  588. ret = shift_arg_pages(vma, stack_shift);
  589. if (ret)
  590. goto out_unlock;
  591. }
  592. /* mprotect_fixup is overkill to remove the temporary stack flags */
  593. vma->vm_flags &= ~VM_STACK_INCOMPLETE_SETUP;
  594. stack_expand = 131072UL; /* randomly 32*4k (or 2*64k) pages */
  595. stack_size = vma->vm_end - vma->vm_start;
  596. /*
  597. * Align this down to a page boundary as expand_stack
  598. * will align it up.
  599. */
  600. rlim_stack = rlimit(RLIMIT_STACK) & PAGE_MASK;
  601. #ifdef CONFIG_STACK_GROWSUP
  602. if (stack_size + stack_expand > rlim_stack)
  603. stack_base = vma->vm_start + rlim_stack;
  604. else
  605. stack_base = vma->vm_end + stack_expand;
  606. #else
  607. if (stack_size + stack_expand > rlim_stack)
  608. stack_base = vma->vm_end - rlim_stack;
  609. else
  610. stack_base = vma->vm_start - stack_expand;
  611. #endif
  612. current->mm->start_stack = bprm->p;
  613. ret = expand_stack(vma, stack_base);
  614. if (ret)
  615. ret = -EFAULT;
  616. out_unlock:
  617. up_write(&mm->mmap_sem);
  618. return ret;
  619. }
  620. EXPORT_SYMBOL(setup_arg_pages);
  621. #endif /* CONFIG_MMU */
  622. struct file *open_exec(const char *name)
  623. {
  624. struct file *file;
  625. int err;
  626. static const struct open_flags open_exec_flags = {
  627. .open_flag = O_LARGEFILE | O_RDONLY | __FMODE_EXEC,
  628. .acc_mode = MAY_EXEC | MAY_OPEN,
  629. .intent = LOOKUP_OPEN
  630. };
  631. file = do_filp_open(AT_FDCWD, name, &open_exec_flags, LOOKUP_FOLLOW);
  632. if (IS_ERR(file))
  633. goto out;
  634. err = -EACCES;
  635. if (!S_ISREG(file->f_path.dentry->d_inode->i_mode))
  636. goto exit;
  637. if (file->f_path.mnt->mnt_flags & MNT_NOEXEC)
  638. goto exit;
  639. fsnotify_open(file);
  640. err = deny_write_access(file);
  641. if (err)
  642. goto exit;
  643. out:
  644. return file;
  645. exit:
  646. fput(file);
  647. return ERR_PTR(err);
  648. }
  649. EXPORT_SYMBOL(open_exec);
  650. int kernel_read(struct file *file, loff_t offset,
  651. char *addr, unsigned long count)
  652. {
  653. mm_segment_t old_fs;
  654. loff_t pos = offset;
  655. int result;
  656. old_fs = get_fs();
  657. set_fs(get_ds());
  658. /* The cast to a user pointer is valid due to the set_fs() */
  659. result = vfs_read(file, (void __user *)addr, count, &pos);
  660. set_fs(old_fs);
  661. return result;
  662. }
  663. EXPORT_SYMBOL(kernel_read);
  664. static int exec_mmap(struct mm_struct *mm)
  665. {
  666. struct task_struct *tsk;
  667. struct mm_struct * old_mm, *active_mm;
  668. /* Notify parent that we're no longer interested in the old VM */
  669. tsk = current;
  670. old_mm = current->mm;
  671. sync_mm_rss(tsk, old_mm);
  672. mm_release(tsk, old_mm);
  673. if (old_mm) {
  674. /*
  675. * Make sure that if there is a core dump in progress
  676. * for the old mm, we get out and die instead of going
  677. * through with the exec. We must hold mmap_sem around
  678. * checking core_state and changing tsk->mm.
  679. */
  680. down_read(&old_mm->mmap_sem);
  681. if (unlikely(old_mm->core_state)) {
  682. up_read(&old_mm->mmap_sem);
  683. return -EINTR;
  684. }
  685. }
  686. task_lock(tsk);
  687. active_mm = tsk->active_mm;
  688. tsk->mm = mm;
  689. tsk->active_mm = mm;
  690. activate_mm(active_mm, mm);
  691. if (old_mm && tsk->signal->oom_score_adj == OOM_SCORE_ADJ_MIN) {
  692. atomic_dec(&old_mm->oom_disable_count);
  693. atomic_inc(&tsk->mm->oom_disable_count);
  694. }
  695. task_unlock(tsk);
  696. arch_pick_mmap_layout(mm);
  697. if (old_mm) {
  698. up_read(&old_mm->mmap_sem);
  699. BUG_ON(active_mm != old_mm);
  700. mm_update_next_owner(old_mm);
  701. mmput(old_mm);
  702. return 0;
  703. }
  704. mmdrop(active_mm);
  705. return 0;
  706. }
  707. /*
  708. * This function makes sure the current process has its own signal table,
  709. * so that flush_signal_handlers can later reset the handlers without
  710. * disturbing other processes. (Other processes might share the signal
  711. * table via the CLONE_SIGHAND option to clone().)
  712. */
  713. static int de_thread(struct task_struct *tsk)
  714. {
  715. struct signal_struct *sig = tsk->signal;
  716. struct sighand_struct *oldsighand = tsk->sighand;
  717. spinlock_t *lock = &oldsighand->siglock;
  718. if (thread_group_empty(tsk))
  719. goto no_thread_group;
  720. /*
  721. * Kill all other threads in the thread group.
  722. */
  723. spin_lock_irq(lock);
  724. if (signal_group_exit(sig)) {
  725. /*
  726. * Another group action in progress, just
  727. * return so that the signal is processed.
  728. */
  729. spin_unlock_irq(lock);
  730. return -EAGAIN;
  731. }
  732. sig->group_exit_task = tsk;
  733. sig->notify_count = zap_other_threads(tsk);
  734. if (!thread_group_leader(tsk))
  735. sig->notify_count--;
  736. while (sig->notify_count) {
  737. __set_current_state(TASK_UNINTERRUPTIBLE);
  738. spin_unlock_irq(lock);
  739. schedule();
  740. spin_lock_irq(lock);
  741. }
  742. spin_unlock_irq(lock);
  743. /*
  744. * At this point all other threads have exited, all we have to
  745. * do is to wait for the thread group leader to become inactive,
  746. * and to assume its PID:
  747. */
  748. if (!thread_group_leader(tsk)) {
  749. struct task_struct *leader = tsk->group_leader;
  750. sig->notify_count = -1; /* for exit_notify() */
  751. for (;;) {
  752. write_lock_irq(&tasklist_lock);
  753. if (likely(leader->exit_state))
  754. break;
  755. __set_current_state(TASK_UNINTERRUPTIBLE);
  756. write_unlock_irq(&tasklist_lock);
  757. schedule();
  758. }
  759. /*
  760. * The only record we have of the real-time age of a
  761. * process, regardless of execs it's done, is start_time.
  762. * All the past CPU time is accumulated in signal_struct
  763. * from sister threads now dead. But in this non-leader
  764. * exec, nothing survives from the original leader thread,
  765. * whose birth marks the true age of this process now.
  766. * When we take on its identity by switching to its PID, we
  767. * also take its birthdate (always earlier than our own).
  768. */
  769. tsk->start_time = leader->start_time;
  770. BUG_ON(!same_thread_group(leader, tsk));
  771. BUG_ON(has_group_leader_pid(tsk));
  772. /*
  773. * An exec() starts a new thread group with the
  774. * TGID of the previous thread group. Rehash the
  775. * two threads with a switched PID, and release
  776. * the former thread group leader:
  777. */
  778. /* Become a process group leader with the old leader's pid.
  779. * The old leader becomes a thread of the this thread group.
  780. * Note: The old leader also uses this pid until release_task
  781. * is called. Odd but simple and correct.
  782. */
  783. detach_pid(tsk, PIDTYPE_PID);
  784. tsk->pid = leader->pid;
  785. attach_pid(tsk, PIDTYPE_PID, task_pid(leader));
  786. transfer_pid(leader, tsk, PIDTYPE_PGID);
  787. transfer_pid(leader, tsk, PIDTYPE_SID);
  788. list_replace_rcu(&leader->tasks, &tsk->tasks);
  789. list_replace_init(&leader->sibling, &tsk->sibling);
  790. tsk->group_leader = tsk;
  791. leader->group_leader = tsk;
  792. tsk->exit_signal = SIGCHLD;
  793. BUG_ON(leader->exit_state != EXIT_ZOMBIE);
  794. leader->exit_state = EXIT_DEAD;
  795. write_unlock_irq(&tasklist_lock);
  796. release_task(leader);
  797. }
  798. sig->group_exit_task = NULL;
  799. sig->notify_count = 0;
  800. no_thread_group:
  801. if (current->mm)
  802. setmax_mm_hiwater_rss(&sig->maxrss, current->mm);
  803. exit_itimers(sig);
  804. flush_itimer_signals();
  805. if (atomic_read(&oldsighand->count) != 1) {
  806. struct sighand_struct *newsighand;
  807. /*
  808. * This ->sighand is shared with the CLONE_SIGHAND
  809. * but not CLONE_THREAD task, switch to the new one.
  810. */
  811. newsighand = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
  812. if (!newsighand)
  813. return -ENOMEM;
  814. atomic_set(&newsighand->count, 1);
  815. memcpy(newsighand->action, oldsighand->action,
  816. sizeof(newsighand->action));
  817. write_lock_irq(&tasklist_lock);
  818. spin_lock(&oldsighand->siglock);
  819. rcu_assign_pointer(tsk->sighand, newsighand);
  820. spin_unlock(&oldsighand->siglock);
  821. write_unlock_irq(&tasklist_lock);
  822. __cleanup_sighand(oldsighand);
  823. }
  824. BUG_ON(!thread_group_leader(tsk));
  825. return 0;
  826. }
  827. /*
  828. * These functions flushes out all traces of the currently running executable
  829. * so that a new one can be started
  830. */
  831. static void flush_old_files(struct files_struct * files)
  832. {
  833. long j = -1;
  834. struct fdtable *fdt;
  835. spin_lock(&files->file_lock);
  836. for (;;) {
  837. unsigned long set, i;
  838. j++;
  839. i = j * __NFDBITS;
  840. fdt = files_fdtable(files);
  841. if (i >= fdt->max_fds)
  842. break;
  843. set = fdt->close_on_exec->fds_bits[j];
  844. if (!set)
  845. continue;
  846. fdt->close_on_exec->fds_bits[j] = 0;
  847. spin_unlock(&files->file_lock);
  848. for ( ; set ; i++,set >>= 1) {
  849. if (set & 1) {
  850. sys_close(i);
  851. }
  852. }
  853. spin_lock(&files->file_lock);
  854. }
  855. spin_unlock(&files->file_lock);
  856. }
  857. char *get_task_comm(char *buf, struct task_struct *tsk)
  858. {
  859. /* buf must be at least sizeof(tsk->comm) in size */
  860. task_lock(tsk);
  861. strncpy(buf, tsk->comm, sizeof(tsk->comm));
  862. task_unlock(tsk);
  863. return buf;
  864. }
  865. void set_task_comm(struct task_struct *tsk, char *buf)
  866. {
  867. task_lock(tsk);
  868. /*
  869. * Threads may access current->comm without holding
  870. * the task lock, so write the string carefully.
  871. * Readers without a lock may see incomplete new
  872. * names but are safe from non-terminating string reads.
  873. */
  874. memset(tsk->comm, 0, TASK_COMM_LEN);
  875. wmb();
  876. strlcpy(tsk->comm, buf, sizeof(tsk->comm));
  877. task_unlock(tsk);
  878. perf_event_comm(tsk);
  879. }
  880. int flush_old_exec(struct linux_binprm * bprm)
  881. {
  882. int retval;
  883. /*
  884. * Make sure we have a private signal table and that
  885. * we are unassociated from the previous thread group.
  886. */
  887. retval = de_thread(current);
  888. if (retval)
  889. goto out;
  890. set_mm_exe_file(bprm->mm, bprm->file);
  891. /*
  892. * Release all of the old mmap stuff
  893. */
  894. acct_arg_size(bprm, 0);
  895. retval = exec_mmap(bprm->mm);
  896. if (retval)
  897. goto out;
  898. bprm->mm = NULL; /* We're using it now */
  899. current->flags &= ~(PF_RANDOMIZE | PF_KTHREAD);
  900. flush_thread();
  901. current->personality &= ~bprm->per_clear;
  902. return 0;
  903. out:
  904. return retval;
  905. }
  906. EXPORT_SYMBOL(flush_old_exec);
  907. void setup_new_exec(struct linux_binprm * bprm)
  908. {
  909. int i, ch;
  910. const char *name;
  911. char tcomm[sizeof(current->comm)];
  912. arch_pick_mmap_layout(current->mm);
  913. /* This is the point of no return */
  914. current->sas_ss_sp = current->sas_ss_size = 0;
  915. if (current_euid() == current_uid() && current_egid() == current_gid())
  916. set_dumpable(current->mm, 1);
  917. else
  918. set_dumpable(current->mm, suid_dumpable);
  919. name = bprm->filename;
  920. /* Copies the binary name from after last slash */
  921. for (i=0; (ch = *(name++)) != '\0';) {
  922. if (ch == '/')
  923. i = 0; /* overwrite what we wrote */
  924. else
  925. if (i < (sizeof(tcomm) - 1))
  926. tcomm[i++] = ch;
  927. }
  928. tcomm[i] = '\0';
  929. set_task_comm(current, tcomm);
  930. /* Set the new mm task size. We have to do that late because it may
  931. * depend on TIF_32BIT which is only updated in flush_thread() on
  932. * some architectures like powerpc
  933. */
  934. current->mm->task_size = TASK_SIZE;
  935. /* install the new credentials */
  936. if (bprm->cred->uid != current_euid() ||
  937. bprm->cred->gid != current_egid()) {
  938. current->pdeath_signal = 0;
  939. } else if (file_permission(bprm->file, MAY_READ) ||
  940. bprm->interp_flags & BINPRM_FLAGS_ENFORCE_NONDUMP) {
  941. set_dumpable(current->mm, suid_dumpable);
  942. }
  943. /*
  944. * Flush performance counters when crossing a
  945. * security domain:
  946. */
  947. if (!get_dumpable(current->mm))
  948. perf_event_exit_task(current);
  949. /* An exec changes our domain. We are no longer part of the thread
  950. group */
  951. current->self_exec_id++;
  952. flush_signal_handlers(current, 0);
  953. flush_old_files(current->files);
  954. }
  955. EXPORT_SYMBOL(setup_new_exec);
  956. /*
  957. * Prepare credentials and lock ->cred_guard_mutex.
  958. * install_exec_creds() commits the new creds and drops the lock.
  959. * Or, if exec fails before, free_bprm() should release ->cred and
  960. * and unlock.
  961. */
  962. int prepare_bprm_creds(struct linux_binprm *bprm)
  963. {
  964. if (mutex_lock_interruptible(&current->signal->cred_guard_mutex))
  965. return -ERESTARTNOINTR;
  966. bprm->cred = prepare_exec_creds();
  967. if (likely(bprm->cred))
  968. return 0;
  969. mutex_unlock(&current->signal->cred_guard_mutex);
  970. return -ENOMEM;
  971. }
  972. void free_bprm(struct linux_binprm *bprm)
  973. {
  974. free_arg_pages(bprm);
  975. if (bprm->cred) {
  976. mutex_unlock(&current->signal->cred_guard_mutex);
  977. abort_creds(bprm->cred);
  978. }
  979. kfree(bprm);
  980. }
  981. /*
  982. * install the new credentials for this executable
  983. */
  984. void install_exec_creds(struct linux_binprm *bprm)
  985. {
  986. security_bprm_committing_creds(bprm);
  987. commit_creds(bprm->cred);
  988. bprm->cred = NULL;
  989. /*
  990. * cred_guard_mutex must be held at least to this point to prevent
  991. * ptrace_attach() from altering our determination of the task's
  992. * credentials; any time after this it may be unlocked.
  993. */
  994. security_bprm_committed_creds(bprm);
  995. mutex_unlock(&current->signal->cred_guard_mutex);
  996. }
  997. EXPORT_SYMBOL(install_exec_creds);
  998. /*
  999. * determine how safe it is to execute the proposed program
  1000. * - the caller must hold ->cred_guard_mutex to protect against
  1001. * PTRACE_ATTACH
  1002. */
  1003. int check_unsafe_exec(struct linux_binprm *bprm)
  1004. {
  1005. struct task_struct *p = current, *t;
  1006. unsigned n_fs;
  1007. int res = 0;
  1008. bprm->unsafe = tracehook_unsafe_exec(p);
  1009. n_fs = 1;
  1010. spin_lock(&p->fs->lock);
  1011. rcu_read_lock();
  1012. for (t = next_thread(p); t != p; t = next_thread(t)) {
  1013. if (t->fs == p->fs)
  1014. n_fs++;
  1015. }
  1016. rcu_read_unlock();
  1017. if (p->fs->users > n_fs) {
  1018. bprm->unsafe |= LSM_UNSAFE_SHARE;
  1019. } else {
  1020. res = -EAGAIN;
  1021. if (!p->fs->in_exec) {
  1022. p->fs->in_exec = 1;
  1023. res = 1;
  1024. }
  1025. }
  1026. spin_unlock(&p->fs->lock);
  1027. return res;
  1028. }
  1029. /*
  1030. * Fill the binprm structure from the inode.
  1031. * Check permissions, then read the first 128 (BINPRM_BUF_SIZE) bytes
  1032. *
  1033. * This may be called multiple times for binary chains (scripts for example).
  1034. */
  1035. int prepare_binprm(struct linux_binprm *bprm)
  1036. {
  1037. umode_t mode;
  1038. struct inode * inode = bprm->file->f_path.dentry->d_inode;
  1039. int retval;
  1040. mode = inode->i_mode;
  1041. if (bprm->file->f_op == NULL)
  1042. return -EACCES;
  1043. /* clear any previous set[ug]id data from a previous binary */
  1044. bprm->cred->euid = current_euid();
  1045. bprm->cred->egid = current_egid();
  1046. if (!(bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID)) {
  1047. /* Set-uid? */
  1048. if (mode & S_ISUID) {
  1049. bprm->per_clear |= PER_CLEAR_ON_SETID;
  1050. bprm->cred->euid = inode->i_uid;
  1051. }
  1052. /* Set-gid? */
  1053. /*
  1054. * If setgid is set but no group execute bit then this
  1055. * is a candidate for mandatory locking, not a setgid
  1056. * executable.
  1057. */
  1058. if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
  1059. bprm->per_clear |= PER_CLEAR_ON_SETID;
  1060. bprm->cred->egid = inode->i_gid;
  1061. }
  1062. }
  1063. /* fill in binprm security blob */
  1064. retval = security_bprm_set_creds(bprm);
  1065. if (retval)
  1066. return retval;
  1067. bprm->cred_prepared = 1;
  1068. memset(bprm->buf, 0, BINPRM_BUF_SIZE);
  1069. return kernel_read(bprm->file, 0, bprm->buf, BINPRM_BUF_SIZE);
  1070. }
  1071. EXPORT_SYMBOL(prepare_binprm);
  1072. /*
  1073. * Arguments are '\0' separated strings found at the location bprm->p
  1074. * points to; chop off the first by relocating brpm->p to right after
  1075. * the first '\0' encountered.
  1076. */
  1077. int remove_arg_zero(struct linux_binprm *bprm)
  1078. {
  1079. int ret = 0;
  1080. unsigned long offset;
  1081. char *kaddr;
  1082. struct page *page;
  1083. if (!bprm->argc)
  1084. return 0;
  1085. do {
  1086. offset = bprm->p & ~PAGE_MASK;
  1087. page = get_arg_page(bprm, bprm->p, 0);
  1088. if (!page) {
  1089. ret = -EFAULT;
  1090. goto out;
  1091. }
  1092. kaddr = kmap_atomic(page, KM_USER0);
  1093. for (; offset < PAGE_SIZE && kaddr[offset];
  1094. offset++, bprm->p++)
  1095. ;
  1096. kunmap_atomic(kaddr, KM_USER0);
  1097. put_arg_page(page);
  1098. if (offset == PAGE_SIZE)
  1099. free_arg_page(bprm, (bprm->p >> PAGE_SHIFT) - 1);
  1100. } while (offset == PAGE_SIZE);
  1101. bprm->p++;
  1102. bprm->argc--;
  1103. ret = 0;
  1104. out:
  1105. return ret;
  1106. }
  1107. EXPORT_SYMBOL(remove_arg_zero);
  1108. /*
  1109. * cycle the list of binary formats handler, until one recognizes the image
  1110. */
  1111. int search_binary_handler(struct linux_binprm *bprm,struct pt_regs *regs)
  1112. {
  1113. unsigned int depth = bprm->recursion_depth;
  1114. int try,retval;
  1115. struct linux_binfmt *fmt;
  1116. retval = security_bprm_check(bprm);
  1117. if (retval)
  1118. return retval;
  1119. /* kernel module loader fixup */
  1120. /* so we don't try to load run modprobe in kernel space. */
  1121. set_fs(USER_DS);
  1122. retval = audit_bprm(bprm);
  1123. if (retval)
  1124. return retval;
  1125. retval = -ENOENT;
  1126. for (try=0; try<2; try++) {
  1127. read_lock(&binfmt_lock);
  1128. list_for_each_entry(fmt, &formats, lh) {
  1129. int (*fn)(struct linux_binprm *, struct pt_regs *) = fmt->load_binary;
  1130. if (!fn)
  1131. continue;
  1132. if (!try_module_get(fmt->module))
  1133. continue;
  1134. read_unlock(&binfmt_lock);
  1135. retval = fn(bprm, regs);
  1136. /*
  1137. * Restore the depth counter to its starting value
  1138. * in this call, so we don't have to rely on every
  1139. * load_binary function to restore it on return.
  1140. */
  1141. bprm->recursion_depth = depth;
  1142. if (retval >= 0) {
  1143. if (depth == 0)
  1144. tracehook_report_exec(fmt, bprm, regs);
  1145. put_binfmt(fmt);
  1146. allow_write_access(bprm->file);
  1147. if (bprm->file)
  1148. fput(bprm->file);
  1149. bprm->file = NULL;
  1150. current->did_exec = 1;
  1151. proc_exec_connector(current);
  1152. return retval;
  1153. }
  1154. read_lock(&binfmt_lock);
  1155. put_binfmt(fmt);
  1156. if (retval != -ENOEXEC || bprm->mm == NULL)
  1157. break;
  1158. if (!bprm->file) {
  1159. read_unlock(&binfmt_lock);
  1160. return retval;
  1161. }
  1162. }
  1163. read_unlock(&binfmt_lock);
  1164. if (retval != -ENOEXEC || bprm->mm == NULL) {
  1165. break;
  1166. #ifdef CONFIG_MODULES
  1167. } else {
  1168. #define printable(c) (((c)=='\t') || ((c)=='\n') || (0x20<=(c) && (c)<=0x7e))
  1169. if (printable(bprm->buf[0]) &&
  1170. printable(bprm->buf[1]) &&
  1171. printable(bprm->buf[2]) &&
  1172. printable(bprm->buf[3]))
  1173. break; /* -ENOEXEC */
  1174. request_module("binfmt-%04x", *(unsigned short *)(&bprm->buf[2]));
  1175. #endif
  1176. }
  1177. }
  1178. return retval;
  1179. }
  1180. EXPORT_SYMBOL(search_binary_handler);
  1181. /*
  1182. * sys_execve() executes a new program.
  1183. */
  1184. int do_execve(const char * filename,
  1185. const char __user *const __user *argv,
  1186. const char __user *const __user *envp,
  1187. struct pt_regs * regs)
  1188. {
  1189. struct linux_binprm *bprm;
  1190. struct file *file;
  1191. struct files_struct *displaced;
  1192. bool clear_in_exec;
  1193. int retval;
  1194. retval = unshare_files(&displaced);
  1195. if (retval)
  1196. goto out_ret;
  1197. retval = -ENOMEM;
  1198. bprm = kzalloc(sizeof(*bprm), GFP_KERNEL);
  1199. if (!bprm)
  1200. goto out_files;
  1201. retval = prepare_bprm_creds(bprm);
  1202. if (retval)
  1203. goto out_free;
  1204. retval = check_unsafe_exec(bprm);
  1205. if (retval < 0)
  1206. goto out_free;
  1207. clear_in_exec = retval;
  1208. current->in_execve = 1;
  1209. file = open_exec(filename);
  1210. retval = PTR_ERR(file);
  1211. if (IS_ERR(file))
  1212. goto out_unmark;
  1213. sched_exec();
  1214. bprm->file = file;
  1215. bprm->filename = filename;
  1216. bprm->interp = filename;
  1217. retval = bprm_mm_init(bprm);
  1218. if (retval)
  1219. goto out_file;
  1220. bprm->argc = count(argv, MAX_ARG_STRINGS);
  1221. if ((retval = bprm->argc) < 0)
  1222. goto out;
  1223. bprm->envc = count(envp, MAX_ARG_STRINGS);
  1224. if ((retval = bprm->envc) < 0)
  1225. goto out;
  1226. retval = prepare_binprm(bprm);
  1227. if (retval < 0)
  1228. goto out;
  1229. retval = copy_strings_kernel(1, &bprm->filename, bprm);
  1230. if (retval < 0)
  1231. goto out;
  1232. bprm->exec = bprm->p;
  1233. retval = copy_strings(bprm->envc, envp, bprm);
  1234. if (retval < 0)
  1235. goto out;
  1236. retval = copy_strings(bprm->argc, argv, bprm);
  1237. if (retval < 0)
  1238. goto out;
  1239. retval = search_binary_handler(bprm,regs);
  1240. if (retval < 0)
  1241. goto out;
  1242. /* execve succeeded */
  1243. current->fs->in_exec = 0;
  1244. current->in_execve = 0;
  1245. acct_update_integrals(current);
  1246. free_bprm(bprm);
  1247. if (displaced)
  1248. put_files_struct(displaced);
  1249. return retval;
  1250. out:
  1251. if (bprm->mm) {
  1252. acct_arg_size(bprm, 0);
  1253. mmput(bprm->mm);
  1254. }
  1255. out_file:
  1256. if (bprm->file) {
  1257. allow_write_access(bprm->file);
  1258. fput(bprm->file);
  1259. }
  1260. out_unmark:
  1261. if (clear_in_exec)
  1262. current->fs->in_exec = 0;
  1263. current->in_execve = 0;
  1264. out_free:
  1265. free_bprm(bprm);
  1266. out_files:
  1267. if (displaced)
  1268. reset_files_struct(displaced);
  1269. out_ret:
  1270. return retval;
  1271. }
  1272. void set_binfmt(struct linux_binfmt *new)
  1273. {
  1274. struct mm_struct *mm = current->mm;
  1275. if (mm->binfmt)
  1276. module_put(mm->binfmt->module);
  1277. mm->binfmt = new;
  1278. if (new)
  1279. __module_get(new->module);
  1280. }
  1281. EXPORT_SYMBOL(set_binfmt);
  1282. static int expand_corename(struct core_name *cn)
  1283. {
  1284. char *old_corename = cn->corename;
  1285. cn->size = CORENAME_MAX_SIZE * atomic_inc_return(&call_count);
  1286. cn->corename = krealloc(old_corename, cn->size, GFP_KERNEL);
  1287. if (!cn->corename) {
  1288. kfree(old_corename);
  1289. return -ENOMEM;
  1290. }
  1291. return 0;
  1292. }
  1293. static int cn_printf(struct core_name *cn, const char *fmt, ...)
  1294. {
  1295. char *cur;
  1296. int need;
  1297. int ret;
  1298. va_list arg;
  1299. va_start(arg, fmt);
  1300. need = vsnprintf(NULL, 0, fmt, arg);
  1301. va_end(arg);
  1302. if (likely(need < cn->size - cn->used - 1))
  1303. goto out_printf;
  1304. ret = expand_corename(cn);
  1305. if (ret)
  1306. goto expand_fail;
  1307. out_printf:
  1308. cur = cn->corename + cn->used;
  1309. va_start(arg, fmt);
  1310. vsnprintf(cur, need + 1, fmt, arg);
  1311. va_end(arg);
  1312. cn->used += need;
  1313. return 0;
  1314. expand_fail:
  1315. return ret;
  1316. }
  1317. /* format_corename will inspect the pattern parameter, and output a
  1318. * name into corename, which must have space for at least
  1319. * CORENAME_MAX_SIZE bytes plus one byte for the zero terminator.
  1320. */
  1321. static int format_corename(struct core_name *cn, long signr)
  1322. {
  1323. const struct cred *cred = current_cred();
  1324. const char *pat_ptr = core_pattern;
  1325. int ispipe = (*pat_ptr == '|');
  1326. int pid_in_pattern = 0;
  1327. int err = 0;
  1328. cn->size = CORENAME_MAX_SIZE * atomic_read(&call_count);
  1329. cn->corename = kmalloc(cn->size, GFP_KERNEL);
  1330. cn->used = 0;
  1331. if (!cn->corename)
  1332. return -ENOMEM;
  1333. /* Repeat as long as we have more pattern to process and more output
  1334. space */
  1335. while (*pat_ptr) {
  1336. if (*pat_ptr != '%') {
  1337. if (*pat_ptr == 0)
  1338. goto out;
  1339. err = cn_printf(cn, "%c", *pat_ptr++);
  1340. } else {
  1341. switch (*++pat_ptr) {
  1342. /* single % at the end, drop that */
  1343. case 0:
  1344. goto out;
  1345. /* Double percent, output one percent */
  1346. case '%':
  1347. err = cn_printf(cn, "%c", '%');
  1348. break;
  1349. /* pid */
  1350. case 'p':
  1351. pid_in_pattern = 1;
  1352. err = cn_printf(cn, "%d",
  1353. task_tgid_vnr(current));
  1354. break;
  1355. /* uid */
  1356. case 'u':
  1357. err = cn_printf(cn, "%d", cred->uid);
  1358. break;
  1359. /* gid */
  1360. case 'g':
  1361. err = cn_printf(cn, "%d", cred->gid);
  1362. break;
  1363. /* signal that caused the coredump */
  1364. case 's':
  1365. err = cn_printf(cn, "%ld", signr);
  1366. break;
  1367. /* UNIX time of coredump */
  1368. case 't': {
  1369. struct timeval tv;
  1370. do_gettimeofday(&tv);
  1371. err = cn_printf(cn, "%lu", tv.tv_sec);
  1372. break;
  1373. }
  1374. /* hostname */
  1375. case 'h':
  1376. down_read(&uts_sem);
  1377. err = cn_printf(cn, "%s",
  1378. utsname()->nodename);
  1379. up_read(&uts_sem);
  1380. break;
  1381. /* executable */
  1382. case 'e':
  1383. err = cn_printf(cn, "%s", current->comm);
  1384. break;
  1385. /* core limit size */
  1386. case 'c':
  1387. err = cn_printf(cn, "%lu",
  1388. rlimit(RLIMIT_CORE));
  1389. break;
  1390. default:
  1391. break;
  1392. }
  1393. ++pat_ptr;
  1394. }
  1395. if (err)
  1396. return err;
  1397. }
  1398. /* Backward compatibility with core_uses_pid:
  1399. *
  1400. * If core_pattern does not include a %p (as is the default)
  1401. * and core_uses_pid is set, then .%pid will be appended to
  1402. * the filename. Do not do this for piped commands. */
  1403. if (!ispipe && !pid_in_pattern && core_uses_pid) {
  1404. err = cn_printf(cn, ".%d", task_tgid_vnr(current));
  1405. if (err)
  1406. return err;
  1407. }
  1408. out:
  1409. return ispipe;
  1410. }
  1411. static int zap_process(struct task_struct *start, int exit_code)
  1412. {
  1413. struct task_struct *t;
  1414. int nr = 0;
  1415. start->signal->flags = SIGNAL_GROUP_EXIT;
  1416. start->signal->group_exit_code = exit_code;
  1417. start->signal->group_stop_count = 0;
  1418. t = start;
  1419. do {
  1420. if (t != current && t->mm) {
  1421. sigaddset(&t->pending.signal, SIGKILL);
  1422. signal_wake_up(t, 1);
  1423. nr++;
  1424. }
  1425. } while_each_thread(start, t);
  1426. return nr;
  1427. }
  1428. static inline int zap_threads(struct task_struct *tsk, struct mm_struct *mm,
  1429. struct core_state *core_state, int exit_code)
  1430. {
  1431. struct task_struct *g, *p;
  1432. unsigned long flags;
  1433. int nr = -EAGAIN;
  1434. spin_lock_irq(&tsk->sighand->siglock);
  1435. if (!signal_group_exit(tsk->signal)) {
  1436. mm->core_state = core_state;
  1437. nr = zap_process(tsk, exit_code);
  1438. }
  1439. spin_unlock_irq(&tsk->sighand->siglock);
  1440. if (unlikely(nr < 0))
  1441. return nr;
  1442. if (atomic_read(&mm->mm_users) == nr + 1)
  1443. goto done;
  1444. /*
  1445. * We should find and kill all tasks which use this mm, and we should
  1446. * count them correctly into ->nr_threads. We don't take tasklist
  1447. * lock, but this is safe wrt:
  1448. *
  1449. * fork:
  1450. * None of sub-threads can fork after zap_process(leader). All
  1451. * processes which were created before this point should be
  1452. * visible to zap_threads() because copy_process() adds the new
  1453. * process to the tail of init_task.tasks list, and lock/unlock
  1454. * of ->siglock provides a memory barrier.
  1455. *
  1456. * do_exit:
  1457. * The caller holds mm->mmap_sem. This means that the task which
  1458. * uses this mm can't pass exit_mm(), so it can't exit or clear
  1459. * its ->mm.
  1460. *
  1461. * de_thread:
  1462. * It does list_replace_rcu(&leader->tasks, &current->tasks),
  1463. * we must see either old or new leader, this does not matter.
  1464. * However, it can change p->sighand, so lock_task_sighand(p)
  1465. * must be used. Since p->mm != NULL and we hold ->mmap_sem
  1466. * it can't fail.
  1467. *
  1468. * Note also that "g" can be the old leader with ->mm == NULL
  1469. * and already unhashed and thus removed from ->thread_group.
  1470. * This is OK, __unhash_process()->list_del_rcu() does not
  1471. * clear the ->next pointer, we will find the new leader via
  1472. * next_thread().
  1473. */
  1474. rcu_read_lock();
  1475. for_each_process(g) {
  1476. if (g == tsk->group_leader)
  1477. continue;
  1478. if (g->flags & PF_KTHREAD)
  1479. continue;
  1480. p = g;
  1481. do {
  1482. if (p->mm) {
  1483. if (unlikely(p->mm == mm)) {
  1484. lock_task_sighand(p, &flags);
  1485. nr += zap_process(p, exit_code);
  1486. unlock_task_sighand(p, &flags);
  1487. }
  1488. break;
  1489. }
  1490. } while_each_thread(g, p);
  1491. }
  1492. rcu_read_unlock();
  1493. done:
  1494. atomic_set(&core_state->nr_threads, nr);
  1495. return nr;
  1496. }
  1497. static int coredump_wait(int exit_code, struct core_state *core_state)
  1498. {
  1499. struct task_struct *tsk = current;
  1500. struct mm_struct *mm = tsk->mm;
  1501. struct completion *vfork_done;
  1502. int core_waiters = -EBUSY;
  1503. init_completion(&core_state->startup);
  1504. core_state->dumper.task = tsk;
  1505. core_state->dumper.next = NULL;
  1506. down_write(&mm->mmap_sem);
  1507. if (!mm->core_state)
  1508. core_waiters = zap_threads(tsk, mm, core_state, exit_code);
  1509. up_write(&mm->mmap_sem);
  1510. if (unlikely(core_waiters < 0))
  1511. goto fail;
  1512. /*
  1513. * Make sure nobody is waiting for us to release the VM,
  1514. * otherwise we can deadlock when we wait on each other
  1515. */
  1516. vfork_done = tsk->vfork_done;
  1517. if (vfork_done) {
  1518. tsk->vfork_done = NULL;
  1519. complete(vfork_done);
  1520. }
  1521. if (core_waiters)
  1522. wait_for_completion(&core_state->startup);
  1523. fail:
  1524. return core_waiters;
  1525. }
  1526. static void coredump_finish(struct mm_struct *mm)
  1527. {
  1528. struct core_thread *curr, *next;
  1529. struct task_struct *task;
  1530. next = mm->core_state->dumper.next;
  1531. while ((curr = next) != NULL) {
  1532. next = curr->next;
  1533. task = curr->task;
  1534. /*
  1535. * see exit_mm(), curr->task must not see
  1536. * ->task == NULL before we read ->next.
  1537. */
  1538. smp_mb();
  1539. curr->task = NULL;
  1540. wake_up_process(task);
  1541. }
  1542. mm->core_state = NULL;
  1543. }
  1544. /*
  1545. * set_dumpable converts traditional three-value dumpable to two flags and
  1546. * stores them into mm->flags. It modifies lower two bits of mm->flags, but
  1547. * these bits are not changed atomically. So get_dumpable can observe the
  1548. * intermediate state. To avoid doing unexpected behavior, get get_dumpable
  1549. * return either old dumpable or new one by paying attention to the order of
  1550. * modifying the bits.
  1551. *
  1552. * dumpable | mm->flags (binary)
  1553. * old new | initial interim final
  1554. * ---------+-----------------------
  1555. * 0 1 | 00 01 01
  1556. * 0 2 | 00 10(*) 11
  1557. * 1 0 | 01 00 00
  1558. * 1 2 | 01 11 11
  1559. * 2 0 | 11 10(*) 00
  1560. * 2 1 | 11 11 01
  1561. *
  1562. * (*) get_dumpable regards interim value of 10 as 11.
  1563. */
  1564. void set_dumpable(struct mm_struct *mm, int value)
  1565. {
  1566. switch (value) {
  1567. case 0:
  1568. clear_bit(MMF_DUMPABLE, &mm->flags);
  1569. smp_wmb();
  1570. clear_bit(MMF_DUMP_SECURELY, &mm->flags);
  1571. break;
  1572. case 1:
  1573. set_bit(MMF_DUMPABLE, &mm->flags);
  1574. smp_wmb();
  1575. clear_bit(MMF_DUMP_SECURELY, &mm->flags);
  1576. break;
  1577. case 2:
  1578. set_bit(MMF_DUMP_SECURELY, &mm->flags);
  1579. smp_wmb();
  1580. set_bit(MMF_DUMPABLE, &mm->flags);
  1581. break;
  1582. }
  1583. }
  1584. static int __get_dumpable(unsigned long mm_flags)
  1585. {
  1586. int ret;
  1587. ret = mm_flags & MMF_DUMPABLE_MASK;
  1588. return (ret >= 2) ? 2 : ret;
  1589. }
  1590. int get_dumpable(struct mm_struct *mm)
  1591. {
  1592. return __get_dumpable(mm->flags);
  1593. }
  1594. static void wait_for_dump_helpers(struct file *file)
  1595. {
  1596. struct pipe_inode_info *pipe;
  1597. pipe = file->f_path.dentry->d_inode->i_pipe;
  1598. pipe_lock(pipe);
  1599. pipe->readers++;
  1600. pipe->writers--;
  1601. while ((pipe->readers > 1) && (!signal_pending(current))) {
  1602. wake_up_interruptible_sync(&pipe->wait);
  1603. kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
  1604. pipe_wait(pipe);
  1605. }
  1606. pipe->readers--;
  1607. pipe->writers++;
  1608. pipe_unlock(pipe);
  1609. }
  1610. /*
  1611. * umh_pipe_setup
  1612. * helper function to customize the process used
  1613. * to collect the core in userspace. Specifically
  1614. * it sets up a pipe and installs it as fd 0 (stdin)
  1615. * for the process. Returns 0 on success, or
  1616. * PTR_ERR on failure.
  1617. * Note that it also sets the core limit to 1. This
  1618. * is a special value that we use to trap recursive
  1619. * core dumps
  1620. */
  1621. static int umh_pipe_setup(struct subprocess_info *info)
  1622. {
  1623. struct file *rp, *wp;
  1624. struct fdtable *fdt;
  1625. struct coredump_params *cp = (struct coredump_params *)info->data;
  1626. struct files_struct *cf = current->files;
  1627. wp = create_write_pipe(0);
  1628. if (IS_ERR(wp))
  1629. return PTR_ERR(wp);
  1630. rp = create_read_pipe(wp, 0);
  1631. if (IS_ERR(rp)) {
  1632. free_write_pipe(wp);
  1633. return PTR_ERR(rp);
  1634. }
  1635. cp->file = wp;
  1636. sys_close(0);
  1637. fd_install(0, rp);
  1638. spin_lock(&cf->file_lock);
  1639. fdt = files_fdtable(cf);
  1640. FD_SET(0, fdt->open_fds);
  1641. FD_CLR(0, fdt->close_on_exec);
  1642. spin_unlock(&cf->file_lock);
  1643. /* and disallow core files too */
  1644. current->signal->rlim[RLIMIT_CORE] = (struct rlimit){1, 1};
  1645. return 0;
  1646. }
  1647. void do_coredump(long signr, int exit_code, struct pt_regs *regs)
  1648. {
  1649. struct core_state core_state;
  1650. struct core_name cn;
  1651. struct mm_struct *mm = current->mm;
  1652. struct linux_binfmt * binfmt;
  1653. const struct cred *old_cred;
  1654. struct cred *cred;
  1655. int retval = 0;
  1656. int flag = 0;
  1657. int ispipe;
  1658. static atomic_t core_dump_count = ATOMIC_INIT(0);
  1659. struct coredump_params cprm = {
  1660. .signr = signr,
  1661. .regs = regs,
  1662. .limit = rlimit(RLIMIT_CORE),
  1663. /*
  1664. * We must use the same mm->flags while dumping core to avoid
  1665. * inconsistency of bit flags, since this flag is not protected
  1666. * by any locks.
  1667. */
  1668. .mm_flags = mm->flags,
  1669. };
  1670. audit_core_dumps(signr);
  1671. binfmt = mm->binfmt;
  1672. if (!binfmt || !binfmt->core_dump)
  1673. goto fail;
  1674. if (!__get_dumpable(cprm.mm_flags))
  1675. goto fail;
  1676. cred = prepare_creds();
  1677. if (!cred)
  1678. goto fail;
  1679. /*
  1680. * We cannot trust fsuid as being the "true" uid of the
  1681. * process nor do we know its entire history. We only know it
  1682. * was tainted so we dump it as root in mode 2.
  1683. */
  1684. if (__get_dumpable(cprm.mm_flags) == 2) {
  1685. /* Setuid core dump mode */
  1686. flag = O_EXCL; /* Stop rewrite attacks */
  1687. cred->fsuid = 0; /* Dump root private */
  1688. }
  1689. retval = coredump_wait(exit_code, &core_state);
  1690. if (retval < 0)
  1691. goto fail_creds;
  1692. old_cred = override_creds(cred);
  1693. /*
  1694. * Clear any false indication of pending signals that might
  1695. * be seen by the filesystem code called to write the core file.
  1696. */
  1697. clear_thread_flag(TIF_SIGPENDING);
  1698. ispipe = format_corename(&cn, signr);
  1699. if (ispipe == -ENOMEM) {
  1700. printk(KERN_WARNING "format_corename failed\n");
  1701. printk(KERN_WARNING "Aborting core\n");
  1702. goto fail_corename;
  1703. }
  1704. if (ispipe) {
  1705. int dump_count;
  1706. char **helper_argv;
  1707. if (cprm.limit == 1) {
  1708. /*
  1709. * Normally core limits are irrelevant to pipes, since
  1710. * we're not writing to the file system, but we use
  1711. * cprm.limit of 1 here as a speacial value. Any
  1712. * non-1 limit gets set to RLIM_INFINITY below, but
  1713. * a limit of 0 skips the dump. This is a consistent
  1714. * way to catch recursive crashes. We can still crash
  1715. * if the core_pattern binary sets RLIM_CORE = !1
  1716. * but it runs as root, and can do lots of stupid things
  1717. * Note that we use task_tgid_vnr here to grab the pid
  1718. * of the process group leader. That way we get the
  1719. * right pid if a thread in a multi-threaded
  1720. * core_pattern process dies.
  1721. */
  1722. printk(KERN_WARNING
  1723. "Process %d(%s) has RLIMIT_CORE set to 1\n",
  1724. task_tgid_vnr(current), current->comm);
  1725. printk(KERN_WARNING "Aborting core\n");
  1726. goto fail_unlock;
  1727. }
  1728. cprm.limit = RLIM_INFINITY;
  1729. dump_count = atomic_inc_return(&core_dump_count);
  1730. if (core_pipe_limit && (core_pipe_limit < dump_count)) {
  1731. printk(KERN_WARNING "Pid %d(%s) over core_pipe_limit\n",
  1732. task_tgid_vnr(current), current->comm);
  1733. printk(KERN_WARNING "Skipping core dump\n");
  1734. goto fail_dropcount;
  1735. }
  1736. helper_argv = argv_split(GFP_KERNEL, cn.corename+1, NULL);
  1737. if (!helper_argv) {
  1738. printk(KERN_WARNING "%s failed to allocate memory\n",
  1739. __func__);
  1740. goto fail_dropcount;
  1741. }
  1742. retval = call_usermodehelper_fns(helper_argv[0], helper_argv,
  1743. NULL, UMH_WAIT_EXEC, umh_pipe_setup,
  1744. NULL, &cprm);
  1745. argv_free(helper_argv);
  1746. if (retval) {
  1747. printk(KERN_INFO "Core dump to %s pipe failed\n",
  1748. cn.corename);
  1749. goto close_fail;
  1750. }
  1751. } else {
  1752. struct inode *inode;
  1753. if (cprm.limit < binfmt->min_coredump)
  1754. goto fail_unlock;
  1755. cprm.file = filp_open(cn.corename,
  1756. O_CREAT | 2 | O_NOFOLLOW | O_LARGEFILE | flag,
  1757. 0600);
  1758. if (IS_ERR(cprm.file))
  1759. goto fail_unlock;
  1760. inode = cprm.file->f_path.dentry->d_inode;
  1761. if (inode->i_nlink > 1)
  1762. goto close_fail;
  1763. if (d_unhashed(cprm.file->f_path.dentry))
  1764. goto close_fail;
  1765. /*
  1766. * AK: actually i see no reason to not allow this for named
  1767. * pipes etc, but keep the previous behaviour for now.
  1768. */
  1769. if (!S_ISREG(inode->i_mode))
  1770. goto close_fail;
  1771. /*
  1772. * Dont allow local users get cute and trick others to coredump
  1773. * into their pre-created files.
  1774. */
  1775. if (inode->i_uid != current_fsuid())
  1776. goto close_fail;
  1777. if (!cprm.file->f_op || !cprm.file->f_op->write)
  1778. goto close_fail;
  1779. if (do_truncate(cprm.file->f_path.dentry, 0, 0, cprm.file))
  1780. goto close_fail;
  1781. }
  1782. retval = binfmt->core_dump(&cprm);
  1783. if (retval)
  1784. current->signal->group_exit_code |= 0x80;
  1785. if (ispipe && core_pipe_limit)
  1786. wait_for_dump_helpers(cprm.file);
  1787. close_fail:
  1788. if (cprm.file)
  1789. filp_close(cprm.file, NULL);
  1790. fail_dropcount:
  1791. if (ispipe)
  1792. atomic_dec(&core_dump_count);
  1793. fail_unlock:
  1794. kfree(cn.corename);
  1795. fail_corename:
  1796. coredump_finish(mm);
  1797. revert_creds(old_cred);
  1798. fail_creds:
  1799. put_cred(cred);
  1800. fail:
  1801. return;
  1802. }
  1803. /*
  1804. * Core dumping helper functions. These are the only things you should
  1805. * do on a core-file: use only these functions to write out all the
  1806. * necessary info.
  1807. */
  1808. int dump_write(struct file *file, const void *addr, int nr)
  1809. {
  1810. return access_ok(VERIFY_READ, addr, nr) && file->f_op->write(file, addr, nr, &file->f_pos) == nr;
  1811. }
  1812. EXPORT_SYMBOL(dump_write);
  1813. int dump_seek(struct file *file, loff_t off)
  1814. {
  1815. int ret = 1;
  1816. if (file->f_op->llseek && file->f_op->llseek != no_llseek) {
  1817. if (file->f_op->llseek(file, off, SEEK_CUR) < 0)
  1818. return 0;
  1819. } else {
  1820. char *buf = (char *)get_zeroed_page(GFP_KERNEL);
  1821. if (!buf)
  1822. return 0;
  1823. while (off > 0) {
  1824. unsigned long n = off;
  1825. if (n > PAGE_SIZE)
  1826. n = PAGE_SIZE;
  1827. if (!dump_write(file, buf, n)) {
  1828. ret = 0;
  1829. break;
  1830. }
  1831. off -= n;
  1832. }
  1833. free_page((unsigned long)buf);
  1834. }
  1835. return ret;
  1836. }
  1837. EXPORT_SYMBOL(dump_seek);