base.c 83 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520
  1. /*
  2. * linux/fs/proc/base.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. *
  6. * proc base directory handling functions
  7. *
  8. * 1999, Al Viro. Rewritten. Now it covers the whole per-process part.
  9. * Instead of using magical inumbers to determine the kind of object
  10. * we allocate and fill in-core inodes upon lookup. They don't even
  11. * go into icache. We cache the reference to task_struct upon lookup too.
  12. * Eventually it should become a filesystem in its own. We don't use the
  13. * rest of procfs anymore.
  14. *
  15. *
  16. * Changelog:
  17. * 17-Jan-2005
  18. * Allan Bezerra
  19. * Bruna Moreira <bruna.moreira@indt.org.br>
  20. * Edjard Mota <edjard.mota@indt.org.br>
  21. * Ilias Biris <ilias.biris@indt.org.br>
  22. * Mauricio Lin <mauricio.lin@indt.org.br>
  23. *
  24. * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
  25. *
  26. * A new process specific entry (smaps) included in /proc. It shows the
  27. * size of rss for each memory area. The maps entry lacks information
  28. * about physical memory size (rss) for each mapped file, i.e.,
  29. * rss information for executables and library files.
  30. * This additional information is useful for any tools that need to know
  31. * about physical memory consumption for a process specific library.
  32. *
  33. * Changelog:
  34. * 21-Feb-2005
  35. * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
  36. * Pud inclusion in the page table walking.
  37. *
  38. * ChangeLog:
  39. * 10-Mar-2005
  40. * 10LE Instituto Nokia de Tecnologia - INdT:
  41. * A better way to walks through the page table as suggested by Hugh Dickins.
  42. *
  43. * Simo Piiroinen <simo.piiroinen@nokia.com>:
  44. * Smaps information related to shared, private, clean and dirty pages.
  45. *
  46. * Paul Mundt <paul.mundt@nokia.com>:
  47. * Overall revision about smaps.
  48. */
  49. #include <asm/uaccess.h>
  50. #include <linux/errno.h>
  51. #include <linux/time.h>
  52. #include <linux/proc_fs.h>
  53. #include <linux/stat.h>
  54. #include <linux/task_io_accounting_ops.h>
  55. #include <linux/init.h>
  56. #include <linux/capability.h>
  57. #include <linux/file.h>
  58. #include <linux/fdtable.h>
  59. #include <linux/string.h>
  60. #include <linux/seq_file.h>
  61. #include <linux/namei.h>
  62. #include <linux/mnt_namespace.h>
  63. #include <linux/mm.h>
  64. #include <linux/swap.h>
  65. #include <linux/rcupdate.h>
  66. #include <linux/kallsyms.h>
  67. #include <linux/stacktrace.h>
  68. #include <linux/resource.h>
  69. #include <linux/module.h>
  70. #include <linux/mount.h>
  71. #include <linux/security.h>
  72. #include <linux/ptrace.h>
  73. #include <linux/tracehook.h>
  74. #include <linux/printk.h>
  75. #include <linux/cgroup.h>
  76. #include <linux/cpuset.h>
  77. #include <linux/audit.h>
  78. #include <linux/poll.h>
  79. #include <linux/nsproxy.h>
  80. #include <linux/oom.h>
  81. #include <linux/elf.h>
  82. #include <linux/pid_namespace.h>
  83. #include <linux/user_namespace.h>
  84. #include <linux/fs_struct.h>
  85. #include <linux/slab.h>
  86. #include <linux/flex_array.h>
  87. #include <linux/posix-timers.h>
  88. #ifdef CONFIG_HARDWALL
  89. #include <asm/hardwall.h>
  90. #endif
  91. #include <trace/events/oom.h>
  92. #include "internal.h"
  93. #include "fd.h"
  94. /* NOTE:
  95. * Implementing inode permission operations in /proc is almost
  96. * certainly an error. Permission checks need to happen during
  97. * each system call not at open time. The reason is that most of
  98. * what we wish to check for permissions in /proc varies at runtime.
  99. *
  100. * The classic example of a problem is opening file descriptors
  101. * in /proc for a task before it execs a suid executable.
  102. */
  103. struct pid_entry {
  104. const char *name;
  105. int len;
  106. umode_t mode;
  107. const struct inode_operations *iop;
  108. const struct file_operations *fop;
  109. union proc_op op;
  110. };
  111. #define NOD(NAME, MODE, IOP, FOP, OP) { \
  112. .name = (NAME), \
  113. .len = sizeof(NAME) - 1, \
  114. .mode = MODE, \
  115. .iop = IOP, \
  116. .fop = FOP, \
  117. .op = OP, \
  118. }
  119. #define DIR(NAME, MODE, iops, fops) \
  120. NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
  121. #define LNK(NAME, get_link) \
  122. NOD(NAME, (S_IFLNK|S_IRWXUGO), \
  123. &proc_pid_link_inode_operations, NULL, \
  124. { .proc_get_link = get_link } )
  125. #define REG(NAME, MODE, fops) \
  126. NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
  127. #define ONE(NAME, MODE, show) \
  128. NOD(NAME, (S_IFREG|(MODE)), \
  129. NULL, &proc_single_file_operations, \
  130. { .proc_show = show } )
  131. /*
  132. * Count the number of hardlinks for the pid_entry table, excluding the .
  133. * and .. links.
  134. */
  135. static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
  136. unsigned int n)
  137. {
  138. unsigned int i;
  139. unsigned int count;
  140. count = 0;
  141. for (i = 0; i < n; ++i) {
  142. if (S_ISDIR(entries[i].mode))
  143. ++count;
  144. }
  145. return count;
  146. }
  147. static int get_task_root(struct task_struct *task, struct path *root)
  148. {
  149. int result = -ENOENT;
  150. task_lock(task);
  151. if (task->fs) {
  152. get_fs_root(task->fs, root);
  153. result = 0;
  154. }
  155. task_unlock(task);
  156. return result;
  157. }
  158. static int proc_cwd_link(struct dentry *dentry, struct path *path)
  159. {
  160. struct task_struct *task = get_proc_task(d_inode(dentry));
  161. int result = -ENOENT;
  162. if (task) {
  163. task_lock(task);
  164. if (task->fs) {
  165. get_fs_pwd(task->fs, path);
  166. result = 0;
  167. }
  168. task_unlock(task);
  169. put_task_struct(task);
  170. }
  171. return result;
  172. }
  173. static int proc_root_link(struct dentry *dentry, struct path *path)
  174. {
  175. struct task_struct *task = get_proc_task(d_inode(dentry));
  176. int result = -ENOENT;
  177. if (task) {
  178. result = get_task_root(task, path);
  179. put_task_struct(task);
  180. }
  181. return result;
  182. }
  183. static ssize_t proc_pid_cmdline_read(struct file *file, char __user *buf,
  184. size_t _count, loff_t *pos)
  185. {
  186. struct task_struct *tsk;
  187. struct mm_struct *mm;
  188. char *page;
  189. unsigned long count = _count;
  190. unsigned long arg_start, arg_end, env_start, env_end;
  191. unsigned long len1, len2, len;
  192. unsigned long p;
  193. char c;
  194. ssize_t rv;
  195. BUG_ON(*pos < 0);
  196. tsk = get_proc_task(file_inode(file));
  197. if (!tsk)
  198. return -ESRCH;
  199. mm = get_task_mm(tsk);
  200. put_task_struct(tsk);
  201. if (!mm)
  202. return 0;
  203. /* Check if process spawned far enough to have cmdline. */
  204. if (!mm->env_end) {
  205. rv = 0;
  206. goto out_mmput;
  207. }
  208. page = (char *)__get_free_page(GFP_TEMPORARY);
  209. if (!page) {
  210. rv = -ENOMEM;
  211. goto out_mmput;
  212. }
  213. down_read(&mm->mmap_sem);
  214. arg_start = mm->arg_start;
  215. arg_end = mm->arg_end;
  216. env_start = mm->env_start;
  217. env_end = mm->env_end;
  218. up_read(&mm->mmap_sem);
  219. BUG_ON(arg_start > arg_end);
  220. BUG_ON(env_start > env_end);
  221. len1 = arg_end - arg_start;
  222. len2 = env_end - env_start;
  223. /* Empty ARGV. */
  224. if (len1 == 0) {
  225. rv = 0;
  226. goto out_free_page;
  227. }
  228. /*
  229. * Inherently racy -- command line shares address space
  230. * with code and data.
  231. */
  232. rv = access_remote_vm(mm, arg_end - 1, &c, 1, 0);
  233. if (rv <= 0)
  234. goto out_free_page;
  235. rv = 0;
  236. if (c == '\0') {
  237. /* Command line (set of strings) occupies whole ARGV. */
  238. if (len1 <= *pos)
  239. goto out_free_page;
  240. p = arg_start + *pos;
  241. len = len1 - *pos;
  242. while (count > 0 && len > 0) {
  243. unsigned int _count;
  244. int nr_read;
  245. _count = min3(count, len, PAGE_SIZE);
  246. nr_read = access_remote_vm(mm, p, page, _count, 0);
  247. if (nr_read < 0)
  248. rv = nr_read;
  249. if (nr_read <= 0)
  250. goto out_free_page;
  251. if (copy_to_user(buf, page, nr_read)) {
  252. rv = -EFAULT;
  253. goto out_free_page;
  254. }
  255. p += nr_read;
  256. len -= nr_read;
  257. buf += nr_read;
  258. count -= nr_read;
  259. rv += nr_read;
  260. }
  261. } else {
  262. /*
  263. * Command line (1 string) occupies ARGV and maybe
  264. * extends into ENVP.
  265. */
  266. if (len1 + len2 <= *pos)
  267. goto skip_argv_envp;
  268. if (len1 <= *pos)
  269. goto skip_argv;
  270. p = arg_start + *pos;
  271. len = len1 - *pos;
  272. while (count > 0 && len > 0) {
  273. unsigned int _count, l;
  274. int nr_read;
  275. bool final;
  276. _count = min3(count, len, PAGE_SIZE);
  277. nr_read = access_remote_vm(mm, p, page, _count, 0);
  278. if (nr_read < 0)
  279. rv = nr_read;
  280. if (nr_read <= 0)
  281. goto out_free_page;
  282. /*
  283. * Command line can be shorter than whole ARGV
  284. * even if last "marker" byte says it is not.
  285. */
  286. final = false;
  287. l = strnlen(page, nr_read);
  288. if (l < nr_read) {
  289. nr_read = l;
  290. final = true;
  291. }
  292. if (copy_to_user(buf, page, nr_read)) {
  293. rv = -EFAULT;
  294. goto out_free_page;
  295. }
  296. p += nr_read;
  297. len -= nr_read;
  298. buf += nr_read;
  299. count -= nr_read;
  300. rv += nr_read;
  301. if (final)
  302. goto out_free_page;
  303. }
  304. skip_argv:
  305. /*
  306. * Command line (1 string) occupies ARGV and
  307. * extends into ENVP.
  308. */
  309. if (len1 <= *pos) {
  310. p = env_start + *pos - len1;
  311. len = len1 + len2 - *pos;
  312. } else {
  313. p = env_start;
  314. len = len2;
  315. }
  316. while (count > 0 && len > 0) {
  317. unsigned int _count, l;
  318. int nr_read;
  319. bool final;
  320. _count = min3(count, len, PAGE_SIZE);
  321. nr_read = access_remote_vm(mm, p, page, _count, 0);
  322. if (nr_read < 0)
  323. rv = nr_read;
  324. if (nr_read <= 0)
  325. goto out_free_page;
  326. /* Find EOS. */
  327. final = false;
  328. l = strnlen(page, nr_read);
  329. if (l < nr_read) {
  330. nr_read = l;
  331. final = true;
  332. }
  333. if (copy_to_user(buf, page, nr_read)) {
  334. rv = -EFAULT;
  335. goto out_free_page;
  336. }
  337. p += nr_read;
  338. len -= nr_read;
  339. buf += nr_read;
  340. count -= nr_read;
  341. rv += nr_read;
  342. if (final)
  343. goto out_free_page;
  344. }
  345. skip_argv_envp:
  346. ;
  347. }
  348. out_free_page:
  349. free_page((unsigned long)page);
  350. out_mmput:
  351. mmput(mm);
  352. if (rv > 0)
  353. *pos += rv;
  354. return rv;
  355. }
  356. static const struct file_operations proc_pid_cmdline_ops = {
  357. .read = proc_pid_cmdline_read,
  358. .llseek = generic_file_llseek,
  359. };
  360. static int proc_pid_auxv(struct seq_file *m, struct pid_namespace *ns,
  361. struct pid *pid, struct task_struct *task)
  362. {
  363. struct mm_struct *mm = mm_access(task, PTRACE_MODE_READ_FSCREDS);
  364. if (mm && !IS_ERR(mm)) {
  365. unsigned int nwords = 0;
  366. do {
  367. nwords += 2;
  368. } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
  369. seq_write(m, mm->saved_auxv, nwords * sizeof(mm->saved_auxv[0]));
  370. mmput(mm);
  371. return 0;
  372. } else
  373. return PTR_ERR(mm);
  374. }
  375. #ifdef CONFIG_KALLSYMS
  376. /*
  377. * Provides a wchan file via kallsyms in a proper one-value-per-file format.
  378. * Returns the resolved symbol. If that fails, simply return the address.
  379. */
  380. static int proc_pid_wchan(struct seq_file *m, struct pid_namespace *ns,
  381. struct pid *pid, struct task_struct *task)
  382. {
  383. unsigned long wchan;
  384. char symname[KSYM_NAME_LEN];
  385. wchan = get_wchan(task);
  386. if (wchan && ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)
  387. && !lookup_symbol_name(wchan, symname))
  388. seq_printf(m, "%s", symname);
  389. else
  390. seq_putc(m, '0');
  391. return 0;
  392. }
  393. #endif /* CONFIG_KALLSYMS */
  394. static int lock_trace(struct task_struct *task)
  395. {
  396. int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
  397. if (err)
  398. return err;
  399. if (!ptrace_may_access(task, PTRACE_MODE_ATTACH_FSCREDS)) {
  400. mutex_unlock(&task->signal->cred_guard_mutex);
  401. return -EPERM;
  402. }
  403. return 0;
  404. }
  405. static void unlock_trace(struct task_struct *task)
  406. {
  407. mutex_unlock(&task->signal->cred_guard_mutex);
  408. }
  409. #ifdef CONFIG_STACKTRACE
  410. #define MAX_STACK_TRACE_DEPTH 64
  411. static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
  412. struct pid *pid, struct task_struct *task)
  413. {
  414. struct stack_trace trace;
  415. unsigned long *entries;
  416. int err;
  417. int i;
  418. entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
  419. if (!entries)
  420. return -ENOMEM;
  421. trace.nr_entries = 0;
  422. trace.max_entries = MAX_STACK_TRACE_DEPTH;
  423. trace.entries = entries;
  424. trace.skip = 0;
  425. err = lock_trace(task);
  426. if (!err) {
  427. save_stack_trace_tsk(task, &trace);
  428. for (i = 0; i < trace.nr_entries; i++) {
  429. seq_printf(m, "[<%pK>] %pS\n",
  430. (void *)entries[i], (void *)entries[i]);
  431. }
  432. unlock_trace(task);
  433. }
  434. kfree(entries);
  435. return err;
  436. }
  437. #endif
  438. #ifdef CONFIG_SCHED_INFO
  439. /*
  440. * Provides /proc/PID/schedstat
  441. */
  442. static int proc_pid_schedstat(struct seq_file *m, struct pid_namespace *ns,
  443. struct pid *pid, struct task_struct *task)
  444. {
  445. if (unlikely(!sched_info_on()))
  446. seq_printf(m, "0 0 0\n");
  447. else
  448. seq_printf(m, "%llu %llu %lu\n",
  449. (unsigned long long)task->se.sum_exec_runtime,
  450. (unsigned long long)task->sched_info.run_delay,
  451. task->sched_info.pcount);
  452. return 0;
  453. }
  454. #endif
  455. #ifdef CONFIG_LATENCYTOP
  456. static int lstats_show_proc(struct seq_file *m, void *v)
  457. {
  458. int i;
  459. struct inode *inode = m->private;
  460. struct task_struct *task = get_proc_task(inode);
  461. if (!task)
  462. return -ESRCH;
  463. seq_puts(m, "Latency Top version : v0.1\n");
  464. for (i = 0; i < 32; i++) {
  465. struct latency_record *lr = &task->latency_record[i];
  466. if (lr->backtrace[0]) {
  467. int q;
  468. seq_printf(m, "%i %li %li",
  469. lr->count, lr->time, lr->max);
  470. for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
  471. unsigned long bt = lr->backtrace[q];
  472. if (!bt)
  473. break;
  474. if (bt == ULONG_MAX)
  475. break;
  476. seq_printf(m, " %ps", (void *)bt);
  477. }
  478. seq_putc(m, '\n');
  479. }
  480. }
  481. put_task_struct(task);
  482. return 0;
  483. }
  484. static int lstats_open(struct inode *inode, struct file *file)
  485. {
  486. return single_open(file, lstats_show_proc, inode);
  487. }
  488. static ssize_t lstats_write(struct file *file, const char __user *buf,
  489. size_t count, loff_t *offs)
  490. {
  491. struct task_struct *task = get_proc_task(file_inode(file));
  492. if (!task)
  493. return -ESRCH;
  494. clear_all_latency_tracing(task);
  495. put_task_struct(task);
  496. return count;
  497. }
  498. static const struct file_operations proc_lstats_operations = {
  499. .open = lstats_open,
  500. .read = seq_read,
  501. .write = lstats_write,
  502. .llseek = seq_lseek,
  503. .release = single_release,
  504. };
  505. #endif
  506. static int proc_oom_score(struct seq_file *m, struct pid_namespace *ns,
  507. struct pid *pid, struct task_struct *task)
  508. {
  509. unsigned long totalpages = totalram_pages + total_swap_pages;
  510. unsigned long points = 0;
  511. read_lock(&tasklist_lock);
  512. if (pid_alive(task))
  513. points = oom_badness(task, NULL, NULL, totalpages) *
  514. 1000 / totalpages;
  515. read_unlock(&tasklist_lock);
  516. seq_printf(m, "%lu\n", points);
  517. return 0;
  518. }
  519. struct limit_names {
  520. const char *name;
  521. const char *unit;
  522. };
  523. static const struct limit_names lnames[RLIM_NLIMITS] = {
  524. [RLIMIT_CPU] = {"Max cpu time", "seconds"},
  525. [RLIMIT_FSIZE] = {"Max file size", "bytes"},
  526. [RLIMIT_DATA] = {"Max data size", "bytes"},
  527. [RLIMIT_STACK] = {"Max stack size", "bytes"},
  528. [RLIMIT_CORE] = {"Max core file size", "bytes"},
  529. [RLIMIT_RSS] = {"Max resident set", "bytes"},
  530. [RLIMIT_NPROC] = {"Max processes", "processes"},
  531. [RLIMIT_NOFILE] = {"Max open files", "files"},
  532. [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
  533. [RLIMIT_AS] = {"Max address space", "bytes"},
  534. [RLIMIT_LOCKS] = {"Max file locks", "locks"},
  535. [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
  536. [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
  537. [RLIMIT_NICE] = {"Max nice priority", NULL},
  538. [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
  539. [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
  540. };
  541. /* Display limits for a process */
  542. static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns,
  543. struct pid *pid, struct task_struct *task)
  544. {
  545. unsigned int i;
  546. unsigned long flags;
  547. struct rlimit rlim[RLIM_NLIMITS];
  548. if (!lock_task_sighand(task, &flags))
  549. return 0;
  550. memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
  551. unlock_task_sighand(task, &flags);
  552. /*
  553. * print the file header
  554. */
  555. seq_printf(m, "%-25s %-20s %-20s %-10s\n",
  556. "Limit", "Soft Limit", "Hard Limit", "Units");
  557. for (i = 0; i < RLIM_NLIMITS; i++) {
  558. if (rlim[i].rlim_cur == RLIM_INFINITY)
  559. seq_printf(m, "%-25s %-20s ",
  560. lnames[i].name, "unlimited");
  561. else
  562. seq_printf(m, "%-25s %-20lu ",
  563. lnames[i].name, rlim[i].rlim_cur);
  564. if (rlim[i].rlim_max == RLIM_INFINITY)
  565. seq_printf(m, "%-20s ", "unlimited");
  566. else
  567. seq_printf(m, "%-20lu ", rlim[i].rlim_max);
  568. if (lnames[i].unit)
  569. seq_printf(m, "%-10s\n", lnames[i].unit);
  570. else
  571. seq_putc(m, '\n');
  572. }
  573. return 0;
  574. }
  575. #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
  576. static int proc_pid_syscall(struct seq_file *m, struct pid_namespace *ns,
  577. struct pid *pid, struct task_struct *task)
  578. {
  579. long nr;
  580. unsigned long args[6], sp, pc;
  581. int res;
  582. res = lock_trace(task);
  583. if (res)
  584. return res;
  585. if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
  586. seq_puts(m, "running\n");
  587. else if (nr < 0)
  588. seq_printf(m, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
  589. else
  590. seq_printf(m,
  591. "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
  592. nr,
  593. args[0], args[1], args[2], args[3], args[4], args[5],
  594. sp, pc);
  595. unlock_trace(task);
  596. return 0;
  597. }
  598. #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
  599. /************************************************************************/
  600. /* Here the fs part begins */
  601. /************************************************************************/
  602. /* permission checks */
  603. static int proc_fd_access_allowed(struct inode *inode)
  604. {
  605. struct task_struct *task;
  606. int allowed = 0;
  607. /* Allow access to a task's file descriptors if it is us or we
  608. * may use ptrace attach to the process and find out that
  609. * information.
  610. */
  611. task = get_proc_task(inode);
  612. if (task) {
  613. allowed = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
  614. put_task_struct(task);
  615. }
  616. return allowed;
  617. }
  618. int proc_setattr(struct dentry *dentry, struct iattr *attr)
  619. {
  620. int error;
  621. struct inode *inode = d_inode(dentry);
  622. if (attr->ia_valid & ATTR_MODE)
  623. return -EPERM;
  624. error = inode_change_ok(inode, attr);
  625. if (error)
  626. return error;
  627. setattr_copy(inode, attr);
  628. mark_inode_dirty(inode);
  629. return 0;
  630. }
  631. /*
  632. * May current process learn task's sched/cmdline info (for hide_pid_min=1)
  633. * or euid/egid (for hide_pid_min=2)?
  634. */
  635. static bool has_pid_permissions(struct pid_namespace *pid,
  636. struct task_struct *task,
  637. int hide_pid_min)
  638. {
  639. if (pid->hide_pid < hide_pid_min)
  640. return true;
  641. if (in_group_p(pid->pid_gid))
  642. return true;
  643. return ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
  644. }
  645. static int proc_pid_permission(struct inode *inode, int mask)
  646. {
  647. struct pid_namespace *pid = inode->i_sb->s_fs_info;
  648. struct task_struct *task;
  649. bool has_perms;
  650. task = get_proc_task(inode);
  651. if (!task)
  652. return -ESRCH;
  653. has_perms = has_pid_permissions(pid, task, 1);
  654. put_task_struct(task);
  655. if (!has_perms) {
  656. if (pid->hide_pid == 2) {
  657. /*
  658. * Let's make getdents(), stat(), and open()
  659. * consistent with each other. If a process
  660. * may not stat() a file, it shouldn't be seen
  661. * in procfs at all.
  662. */
  663. return -ENOENT;
  664. }
  665. return -EPERM;
  666. }
  667. return generic_permission(inode, mask);
  668. }
  669. static const struct inode_operations proc_def_inode_operations = {
  670. .setattr = proc_setattr,
  671. };
  672. static int proc_single_show(struct seq_file *m, void *v)
  673. {
  674. struct inode *inode = m->private;
  675. struct pid_namespace *ns;
  676. struct pid *pid;
  677. struct task_struct *task;
  678. int ret;
  679. ns = inode->i_sb->s_fs_info;
  680. pid = proc_pid(inode);
  681. task = get_pid_task(pid, PIDTYPE_PID);
  682. if (!task)
  683. return -ESRCH;
  684. ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
  685. put_task_struct(task);
  686. return ret;
  687. }
  688. static int proc_single_open(struct inode *inode, struct file *filp)
  689. {
  690. return single_open(filp, proc_single_show, inode);
  691. }
  692. static const struct file_operations proc_single_file_operations = {
  693. .open = proc_single_open,
  694. .read = seq_read,
  695. .llseek = seq_lseek,
  696. .release = single_release,
  697. };
  698. struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode)
  699. {
  700. struct task_struct *task = get_proc_task(inode);
  701. struct mm_struct *mm = ERR_PTR(-ESRCH);
  702. if (task) {
  703. mm = mm_access(task, mode | PTRACE_MODE_FSCREDS);
  704. put_task_struct(task);
  705. if (!IS_ERR_OR_NULL(mm)) {
  706. /* ensure this mm_struct can't be freed */
  707. atomic_inc(&mm->mm_count);
  708. /* but do not pin its memory */
  709. mmput(mm);
  710. }
  711. }
  712. return mm;
  713. }
  714. static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
  715. {
  716. struct mm_struct *mm = proc_mem_open(inode, mode);
  717. if (IS_ERR(mm))
  718. return PTR_ERR(mm);
  719. file->private_data = mm;
  720. return 0;
  721. }
  722. static int mem_open(struct inode *inode, struct file *file)
  723. {
  724. int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
  725. /* OK to pass negative loff_t, we can catch out-of-range */
  726. file->f_mode |= FMODE_UNSIGNED_OFFSET;
  727. return ret;
  728. }
  729. static ssize_t mem_rw(struct file *file, char __user *buf,
  730. size_t count, loff_t *ppos, int write)
  731. {
  732. struct mm_struct *mm = file->private_data;
  733. unsigned long addr = *ppos;
  734. ssize_t copied;
  735. char *page;
  736. if (!mm)
  737. return 0;
  738. page = (char *)__get_free_page(GFP_TEMPORARY);
  739. if (!page)
  740. return -ENOMEM;
  741. copied = 0;
  742. if (!atomic_inc_not_zero(&mm->mm_users))
  743. goto free;
  744. while (count > 0) {
  745. int this_len = min_t(int, count, PAGE_SIZE);
  746. if (write && copy_from_user(page, buf, this_len)) {
  747. copied = -EFAULT;
  748. break;
  749. }
  750. this_len = access_remote_vm(mm, addr, page, this_len, write);
  751. if (!this_len) {
  752. if (!copied)
  753. copied = -EIO;
  754. break;
  755. }
  756. if (!write && copy_to_user(buf, page, this_len)) {
  757. copied = -EFAULT;
  758. break;
  759. }
  760. buf += this_len;
  761. addr += this_len;
  762. copied += this_len;
  763. count -= this_len;
  764. }
  765. *ppos = addr;
  766. mmput(mm);
  767. free:
  768. free_page((unsigned long) page);
  769. return copied;
  770. }
  771. static ssize_t mem_read(struct file *file, char __user *buf,
  772. size_t count, loff_t *ppos)
  773. {
  774. return mem_rw(file, buf, count, ppos, 0);
  775. }
  776. static ssize_t mem_write(struct file *file, const char __user *buf,
  777. size_t count, loff_t *ppos)
  778. {
  779. return mem_rw(file, (char __user*)buf, count, ppos, 1);
  780. }
  781. loff_t mem_lseek(struct file *file, loff_t offset, int orig)
  782. {
  783. switch (orig) {
  784. case 0:
  785. file->f_pos = offset;
  786. break;
  787. case 1:
  788. file->f_pos += offset;
  789. break;
  790. default:
  791. return -EINVAL;
  792. }
  793. force_successful_syscall_return();
  794. return file->f_pos;
  795. }
  796. static int mem_release(struct inode *inode, struct file *file)
  797. {
  798. struct mm_struct *mm = file->private_data;
  799. if (mm)
  800. mmdrop(mm);
  801. return 0;
  802. }
  803. static const struct file_operations proc_mem_operations = {
  804. .llseek = mem_lseek,
  805. .read = mem_read,
  806. .write = mem_write,
  807. .open = mem_open,
  808. .release = mem_release,
  809. };
  810. static int environ_open(struct inode *inode, struct file *file)
  811. {
  812. return __mem_open(inode, file, PTRACE_MODE_READ);
  813. }
  814. static ssize_t environ_read(struct file *file, char __user *buf,
  815. size_t count, loff_t *ppos)
  816. {
  817. char *page;
  818. unsigned long src = *ppos;
  819. int ret = 0;
  820. struct mm_struct *mm = file->private_data;
  821. unsigned long env_start, env_end;
  822. /* Ensure the process spawned far enough to have an environment. */
  823. if (!mm || !mm->env_end)
  824. return 0;
  825. page = (char *)__get_free_page(GFP_TEMPORARY);
  826. if (!page)
  827. return -ENOMEM;
  828. ret = 0;
  829. if (!atomic_inc_not_zero(&mm->mm_users))
  830. goto free;
  831. down_read(&mm->mmap_sem);
  832. env_start = mm->env_start;
  833. env_end = mm->env_end;
  834. up_read(&mm->mmap_sem);
  835. while (count > 0) {
  836. size_t this_len, max_len;
  837. int retval;
  838. if (src >= (env_end - env_start))
  839. break;
  840. this_len = env_end - (env_start + src);
  841. max_len = min_t(size_t, PAGE_SIZE, count);
  842. this_len = min(max_len, this_len);
  843. retval = access_remote_vm(mm, (env_start + src),
  844. page, this_len, 0);
  845. if (retval <= 0) {
  846. ret = retval;
  847. break;
  848. }
  849. if (copy_to_user(buf, page, retval)) {
  850. ret = -EFAULT;
  851. break;
  852. }
  853. ret += retval;
  854. src += retval;
  855. buf += retval;
  856. count -= retval;
  857. }
  858. *ppos = src;
  859. mmput(mm);
  860. free:
  861. free_page((unsigned long) page);
  862. return ret;
  863. }
  864. static const struct file_operations proc_environ_operations = {
  865. .open = environ_open,
  866. .read = environ_read,
  867. .llseek = generic_file_llseek,
  868. .release = mem_release,
  869. };
  870. static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
  871. loff_t *ppos)
  872. {
  873. struct task_struct *task = get_proc_task(file_inode(file));
  874. char buffer[PROC_NUMBUF];
  875. int oom_adj = OOM_ADJUST_MIN;
  876. size_t len;
  877. unsigned long flags;
  878. if (!task)
  879. return -ESRCH;
  880. if (lock_task_sighand(task, &flags)) {
  881. if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
  882. oom_adj = OOM_ADJUST_MAX;
  883. else
  884. oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
  885. OOM_SCORE_ADJ_MAX;
  886. unlock_task_sighand(task, &flags);
  887. }
  888. put_task_struct(task);
  889. len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
  890. return simple_read_from_buffer(buf, count, ppos, buffer, len);
  891. }
  892. /*
  893. * /proc/pid/oom_adj exists solely for backwards compatibility with previous
  894. * kernels. The effective policy is defined by oom_score_adj, which has a
  895. * different scale: oom_adj grew exponentially and oom_score_adj grows linearly.
  896. * Values written to oom_adj are simply mapped linearly to oom_score_adj.
  897. * Processes that become oom disabled via oom_adj will still be oom disabled
  898. * with this implementation.
  899. *
  900. * oom_adj cannot be removed since existing userspace binaries use it.
  901. */
  902. static ssize_t oom_adj_write(struct file *file, const char __user *buf,
  903. size_t count, loff_t *ppos)
  904. {
  905. struct task_struct *task;
  906. char buffer[PROC_NUMBUF];
  907. int oom_adj;
  908. unsigned long flags;
  909. int err;
  910. memset(buffer, 0, sizeof(buffer));
  911. if (count > sizeof(buffer) - 1)
  912. count = sizeof(buffer) - 1;
  913. if (copy_from_user(buffer, buf, count)) {
  914. err = -EFAULT;
  915. goto out;
  916. }
  917. err = kstrtoint(strstrip(buffer), 0, &oom_adj);
  918. if (err)
  919. goto out;
  920. if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
  921. oom_adj != OOM_DISABLE) {
  922. err = -EINVAL;
  923. goto out;
  924. }
  925. task = get_proc_task(file_inode(file));
  926. if (!task) {
  927. err = -ESRCH;
  928. goto out;
  929. }
  930. task_lock(task);
  931. if (!task->mm) {
  932. err = -EINVAL;
  933. goto err_task_lock;
  934. }
  935. if (!lock_task_sighand(task, &flags)) {
  936. err = -ESRCH;
  937. goto err_task_lock;
  938. }
  939. /*
  940. * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
  941. * value is always attainable.
  942. */
  943. if (oom_adj == OOM_ADJUST_MAX)
  944. oom_adj = OOM_SCORE_ADJ_MAX;
  945. else
  946. oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
  947. if (oom_adj < task->signal->oom_score_adj &&
  948. !capable(CAP_SYS_RESOURCE)) {
  949. err = -EACCES;
  950. goto err_sighand;
  951. }
  952. /*
  953. * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
  954. * /proc/pid/oom_score_adj instead.
  955. */
  956. pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
  957. current->comm, task_pid_nr(current), task_pid_nr(task),
  958. task_pid_nr(task));
  959. task->signal->oom_score_adj = oom_adj;
  960. trace_oom_score_adj_update(task);
  961. err_sighand:
  962. unlock_task_sighand(task, &flags);
  963. err_task_lock:
  964. task_unlock(task);
  965. put_task_struct(task);
  966. out:
  967. return err < 0 ? err : count;
  968. }
  969. static const struct file_operations proc_oom_adj_operations = {
  970. .read = oom_adj_read,
  971. .write = oom_adj_write,
  972. .llseek = generic_file_llseek,
  973. };
  974. static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
  975. size_t count, loff_t *ppos)
  976. {
  977. struct task_struct *task = get_proc_task(file_inode(file));
  978. char buffer[PROC_NUMBUF];
  979. short oom_score_adj = OOM_SCORE_ADJ_MIN;
  980. unsigned long flags;
  981. size_t len;
  982. if (!task)
  983. return -ESRCH;
  984. if (lock_task_sighand(task, &flags)) {
  985. oom_score_adj = task->signal->oom_score_adj;
  986. unlock_task_sighand(task, &flags);
  987. }
  988. put_task_struct(task);
  989. len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
  990. return simple_read_from_buffer(buf, count, ppos, buffer, len);
  991. }
  992. static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
  993. size_t count, loff_t *ppos)
  994. {
  995. struct task_struct *task;
  996. char buffer[PROC_NUMBUF];
  997. unsigned long flags;
  998. int oom_score_adj;
  999. int err;
  1000. memset(buffer, 0, sizeof(buffer));
  1001. if (count > sizeof(buffer) - 1)
  1002. count = sizeof(buffer) - 1;
  1003. if (copy_from_user(buffer, buf, count)) {
  1004. err = -EFAULT;
  1005. goto out;
  1006. }
  1007. err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
  1008. if (err)
  1009. goto out;
  1010. if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
  1011. oom_score_adj > OOM_SCORE_ADJ_MAX) {
  1012. err = -EINVAL;
  1013. goto out;
  1014. }
  1015. task = get_proc_task(file_inode(file));
  1016. if (!task) {
  1017. err = -ESRCH;
  1018. goto out;
  1019. }
  1020. task_lock(task);
  1021. if (!task->mm) {
  1022. err = -EINVAL;
  1023. goto err_task_lock;
  1024. }
  1025. if (!lock_task_sighand(task, &flags)) {
  1026. err = -ESRCH;
  1027. goto err_task_lock;
  1028. }
  1029. if ((short)oom_score_adj < task->signal->oom_score_adj_min &&
  1030. !capable(CAP_SYS_RESOURCE)) {
  1031. err = -EACCES;
  1032. goto err_sighand;
  1033. }
  1034. task->signal->oom_score_adj = (short)oom_score_adj;
  1035. if (has_capability_noaudit(current, CAP_SYS_RESOURCE))
  1036. task->signal->oom_score_adj_min = (short)oom_score_adj;
  1037. trace_oom_score_adj_update(task);
  1038. err_sighand:
  1039. unlock_task_sighand(task, &flags);
  1040. err_task_lock:
  1041. task_unlock(task);
  1042. put_task_struct(task);
  1043. out:
  1044. return err < 0 ? err : count;
  1045. }
  1046. static const struct file_operations proc_oom_score_adj_operations = {
  1047. .read = oom_score_adj_read,
  1048. .write = oom_score_adj_write,
  1049. .llseek = default_llseek,
  1050. };
  1051. #ifdef CONFIG_AUDITSYSCALL
  1052. #define TMPBUFLEN 21
  1053. static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
  1054. size_t count, loff_t *ppos)
  1055. {
  1056. struct inode * inode = file_inode(file);
  1057. struct task_struct *task = get_proc_task(inode);
  1058. ssize_t length;
  1059. char tmpbuf[TMPBUFLEN];
  1060. if (!task)
  1061. return -ESRCH;
  1062. length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
  1063. from_kuid(file->f_cred->user_ns,
  1064. audit_get_loginuid(task)));
  1065. put_task_struct(task);
  1066. return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
  1067. }
  1068. static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
  1069. size_t count, loff_t *ppos)
  1070. {
  1071. struct inode * inode = file_inode(file);
  1072. uid_t loginuid;
  1073. kuid_t kloginuid;
  1074. int rv;
  1075. rcu_read_lock();
  1076. if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
  1077. rcu_read_unlock();
  1078. return -EPERM;
  1079. }
  1080. rcu_read_unlock();
  1081. if (*ppos != 0) {
  1082. /* No partial writes. */
  1083. return -EINVAL;
  1084. }
  1085. rv = kstrtou32_from_user(buf, count, 10, &loginuid);
  1086. if (rv < 0)
  1087. return rv;
  1088. /* is userspace tring to explicitly UNSET the loginuid? */
  1089. if (loginuid == AUDIT_UID_UNSET) {
  1090. kloginuid = INVALID_UID;
  1091. } else {
  1092. kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
  1093. if (!uid_valid(kloginuid))
  1094. return -EINVAL;
  1095. }
  1096. rv = audit_set_loginuid(kloginuid);
  1097. if (rv < 0)
  1098. return rv;
  1099. return count;
  1100. }
  1101. static const struct file_operations proc_loginuid_operations = {
  1102. .read = proc_loginuid_read,
  1103. .write = proc_loginuid_write,
  1104. .llseek = generic_file_llseek,
  1105. };
  1106. static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
  1107. size_t count, loff_t *ppos)
  1108. {
  1109. struct inode * inode = file_inode(file);
  1110. struct task_struct *task = get_proc_task(inode);
  1111. ssize_t length;
  1112. char tmpbuf[TMPBUFLEN];
  1113. if (!task)
  1114. return -ESRCH;
  1115. length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
  1116. audit_get_sessionid(task));
  1117. put_task_struct(task);
  1118. return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
  1119. }
  1120. static const struct file_operations proc_sessionid_operations = {
  1121. .read = proc_sessionid_read,
  1122. .llseek = generic_file_llseek,
  1123. };
  1124. #endif
  1125. #ifdef CONFIG_FAULT_INJECTION
  1126. static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
  1127. size_t count, loff_t *ppos)
  1128. {
  1129. struct task_struct *task = get_proc_task(file_inode(file));
  1130. char buffer[PROC_NUMBUF];
  1131. size_t len;
  1132. int make_it_fail;
  1133. if (!task)
  1134. return -ESRCH;
  1135. make_it_fail = task->make_it_fail;
  1136. put_task_struct(task);
  1137. len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
  1138. return simple_read_from_buffer(buf, count, ppos, buffer, len);
  1139. }
  1140. static ssize_t proc_fault_inject_write(struct file * file,
  1141. const char __user * buf, size_t count, loff_t *ppos)
  1142. {
  1143. struct task_struct *task;
  1144. char buffer[PROC_NUMBUF];
  1145. int make_it_fail;
  1146. int rv;
  1147. if (!capable(CAP_SYS_RESOURCE))
  1148. return -EPERM;
  1149. memset(buffer, 0, sizeof(buffer));
  1150. if (count > sizeof(buffer) - 1)
  1151. count = sizeof(buffer) - 1;
  1152. if (copy_from_user(buffer, buf, count))
  1153. return -EFAULT;
  1154. rv = kstrtoint(strstrip(buffer), 0, &make_it_fail);
  1155. if (rv < 0)
  1156. return rv;
  1157. if (make_it_fail < 0 || make_it_fail > 1)
  1158. return -EINVAL;
  1159. task = get_proc_task(file_inode(file));
  1160. if (!task)
  1161. return -ESRCH;
  1162. task->make_it_fail = make_it_fail;
  1163. put_task_struct(task);
  1164. return count;
  1165. }
  1166. static const struct file_operations proc_fault_inject_operations = {
  1167. .read = proc_fault_inject_read,
  1168. .write = proc_fault_inject_write,
  1169. .llseek = generic_file_llseek,
  1170. };
  1171. #endif
  1172. #ifdef CONFIG_SCHED_DEBUG
  1173. /*
  1174. * Print out various scheduling related per-task fields:
  1175. */
  1176. static int sched_show(struct seq_file *m, void *v)
  1177. {
  1178. struct inode *inode = m->private;
  1179. struct task_struct *p;
  1180. p = get_proc_task(inode);
  1181. if (!p)
  1182. return -ESRCH;
  1183. proc_sched_show_task(p, m);
  1184. put_task_struct(p);
  1185. return 0;
  1186. }
  1187. static ssize_t
  1188. sched_write(struct file *file, const char __user *buf,
  1189. size_t count, loff_t *offset)
  1190. {
  1191. struct inode *inode = file_inode(file);
  1192. struct task_struct *p;
  1193. p = get_proc_task(inode);
  1194. if (!p)
  1195. return -ESRCH;
  1196. proc_sched_set_task(p);
  1197. put_task_struct(p);
  1198. return count;
  1199. }
  1200. static int sched_open(struct inode *inode, struct file *filp)
  1201. {
  1202. return single_open(filp, sched_show, inode);
  1203. }
  1204. static const struct file_operations proc_pid_sched_operations = {
  1205. .open = sched_open,
  1206. .read = seq_read,
  1207. .write = sched_write,
  1208. .llseek = seq_lseek,
  1209. .release = single_release,
  1210. };
  1211. #endif
  1212. #ifdef CONFIG_SCHED_AUTOGROUP
  1213. /*
  1214. * Print out autogroup related information:
  1215. */
  1216. static int sched_autogroup_show(struct seq_file *m, void *v)
  1217. {
  1218. struct inode *inode = m->private;
  1219. struct task_struct *p;
  1220. p = get_proc_task(inode);
  1221. if (!p)
  1222. return -ESRCH;
  1223. proc_sched_autogroup_show_task(p, m);
  1224. put_task_struct(p);
  1225. return 0;
  1226. }
  1227. static ssize_t
  1228. sched_autogroup_write(struct file *file, const char __user *buf,
  1229. size_t count, loff_t *offset)
  1230. {
  1231. struct inode *inode = file_inode(file);
  1232. struct task_struct *p;
  1233. char buffer[PROC_NUMBUF];
  1234. int nice;
  1235. int err;
  1236. memset(buffer, 0, sizeof(buffer));
  1237. if (count > sizeof(buffer) - 1)
  1238. count = sizeof(buffer) - 1;
  1239. if (copy_from_user(buffer, buf, count))
  1240. return -EFAULT;
  1241. err = kstrtoint(strstrip(buffer), 0, &nice);
  1242. if (err < 0)
  1243. return err;
  1244. p = get_proc_task(inode);
  1245. if (!p)
  1246. return -ESRCH;
  1247. err = proc_sched_autogroup_set_nice(p, nice);
  1248. if (err)
  1249. count = err;
  1250. put_task_struct(p);
  1251. return count;
  1252. }
  1253. static int sched_autogroup_open(struct inode *inode, struct file *filp)
  1254. {
  1255. int ret;
  1256. ret = single_open(filp, sched_autogroup_show, NULL);
  1257. if (!ret) {
  1258. struct seq_file *m = filp->private_data;
  1259. m->private = inode;
  1260. }
  1261. return ret;
  1262. }
  1263. static const struct file_operations proc_pid_sched_autogroup_operations = {
  1264. .open = sched_autogroup_open,
  1265. .read = seq_read,
  1266. .write = sched_autogroup_write,
  1267. .llseek = seq_lseek,
  1268. .release = single_release,
  1269. };
  1270. #endif /* CONFIG_SCHED_AUTOGROUP */
  1271. static ssize_t comm_write(struct file *file, const char __user *buf,
  1272. size_t count, loff_t *offset)
  1273. {
  1274. struct inode *inode = file_inode(file);
  1275. struct task_struct *p;
  1276. char buffer[TASK_COMM_LEN];
  1277. const size_t maxlen = sizeof(buffer) - 1;
  1278. memset(buffer, 0, sizeof(buffer));
  1279. if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
  1280. return -EFAULT;
  1281. p = get_proc_task(inode);
  1282. if (!p)
  1283. return -ESRCH;
  1284. if (same_thread_group(current, p))
  1285. set_task_comm(p, buffer);
  1286. else
  1287. count = -EINVAL;
  1288. put_task_struct(p);
  1289. return count;
  1290. }
  1291. static int comm_show(struct seq_file *m, void *v)
  1292. {
  1293. struct inode *inode = m->private;
  1294. struct task_struct *p;
  1295. p = get_proc_task(inode);
  1296. if (!p)
  1297. return -ESRCH;
  1298. task_lock(p);
  1299. seq_printf(m, "%s\n", p->comm);
  1300. task_unlock(p);
  1301. put_task_struct(p);
  1302. return 0;
  1303. }
  1304. static int comm_open(struct inode *inode, struct file *filp)
  1305. {
  1306. return single_open(filp, comm_show, inode);
  1307. }
  1308. static const struct file_operations proc_pid_set_comm_operations = {
  1309. .open = comm_open,
  1310. .read = seq_read,
  1311. .write = comm_write,
  1312. .llseek = seq_lseek,
  1313. .release = single_release,
  1314. };
  1315. static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
  1316. {
  1317. struct task_struct *task;
  1318. struct mm_struct *mm;
  1319. struct file *exe_file;
  1320. task = get_proc_task(d_inode(dentry));
  1321. if (!task)
  1322. return -ENOENT;
  1323. mm = get_task_mm(task);
  1324. put_task_struct(task);
  1325. if (!mm)
  1326. return -ENOENT;
  1327. exe_file = get_mm_exe_file(mm);
  1328. mmput(mm);
  1329. if (exe_file) {
  1330. *exe_path = exe_file->f_path;
  1331. path_get(&exe_file->f_path);
  1332. fput(exe_file);
  1333. return 0;
  1334. } else
  1335. return -ENOENT;
  1336. }
  1337. static const char *proc_pid_get_link(struct dentry *dentry,
  1338. struct inode *inode,
  1339. struct delayed_call *done)
  1340. {
  1341. struct path path;
  1342. int error = -EACCES;
  1343. if (!dentry)
  1344. return ERR_PTR(-ECHILD);
  1345. /* Are we allowed to snoop on the tasks file descriptors? */
  1346. if (!proc_fd_access_allowed(inode))
  1347. goto out;
  1348. error = PROC_I(inode)->op.proc_get_link(dentry, &path);
  1349. if (error)
  1350. goto out;
  1351. nd_jump_link(&path);
  1352. return NULL;
  1353. out:
  1354. return ERR_PTR(error);
  1355. }
  1356. static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
  1357. {
  1358. char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
  1359. char *pathname;
  1360. int len;
  1361. if (!tmp)
  1362. return -ENOMEM;
  1363. pathname = d_path(path, tmp, PAGE_SIZE);
  1364. len = PTR_ERR(pathname);
  1365. if (IS_ERR(pathname))
  1366. goto out;
  1367. len = tmp + PAGE_SIZE - 1 - pathname;
  1368. if (len > buflen)
  1369. len = buflen;
  1370. if (copy_to_user(buffer, pathname, len))
  1371. len = -EFAULT;
  1372. out:
  1373. free_page((unsigned long)tmp);
  1374. return len;
  1375. }
  1376. static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
  1377. {
  1378. int error = -EACCES;
  1379. struct inode *inode = d_inode(dentry);
  1380. struct path path;
  1381. /* Are we allowed to snoop on the tasks file descriptors? */
  1382. if (!proc_fd_access_allowed(inode))
  1383. goto out;
  1384. error = PROC_I(inode)->op.proc_get_link(dentry, &path);
  1385. if (error)
  1386. goto out;
  1387. error = do_proc_readlink(&path, buffer, buflen);
  1388. path_put(&path);
  1389. out:
  1390. return error;
  1391. }
  1392. const struct inode_operations proc_pid_link_inode_operations = {
  1393. .readlink = proc_pid_readlink,
  1394. .get_link = proc_pid_get_link,
  1395. .setattr = proc_setattr,
  1396. };
  1397. /* building an inode */
  1398. struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
  1399. {
  1400. struct inode * inode;
  1401. struct proc_inode *ei;
  1402. const struct cred *cred;
  1403. /* We need a new inode */
  1404. inode = new_inode(sb);
  1405. if (!inode)
  1406. goto out;
  1407. /* Common stuff */
  1408. ei = PROC_I(inode);
  1409. inode->i_ino = get_next_ino();
  1410. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  1411. inode->i_op = &proc_def_inode_operations;
  1412. /*
  1413. * grab the reference to task.
  1414. */
  1415. ei->pid = get_task_pid(task, PIDTYPE_PID);
  1416. if (!ei->pid)
  1417. goto out_unlock;
  1418. if (task_dumpable(task)) {
  1419. rcu_read_lock();
  1420. cred = __task_cred(task);
  1421. inode->i_uid = cred->euid;
  1422. inode->i_gid = cred->egid;
  1423. rcu_read_unlock();
  1424. }
  1425. security_task_to_inode(task, inode);
  1426. out:
  1427. return inode;
  1428. out_unlock:
  1429. iput(inode);
  1430. return NULL;
  1431. }
  1432. int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
  1433. {
  1434. struct inode *inode = d_inode(dentry);
  1435. struct task_struct *task;
  1436. const struct cred *cred;
  1437. struct pid_namespace *pid = dentry->d_sb->s_fs_info;
  1438. generic_fillattr(inode, stat);
  1439. rcu_read_lock();
  1440. stat->uid = GLOBAL_ROOT_UID;
  1441. stat->gid = GLOBAL_ROOT_GID;
  1442. task = pid_task(proc_pid(inode), PIDTYPE_PID);
  1443. if (task) {
  1444. if (!has_pid_permissions(pid, task, 2)) {
  1445. rcu_read_unlock();
  1446. /*
  1447. * This doesn't prevent learning whether PID exists,
  1448. * it only makes getattr() consistent with readdir().
  1449. */
  1450. return -ENOENT;
  1451. }
  1452. if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
  1453. task_dumpable(task)) {
  1454. cred = __task_cred(task);
  1455. stat->uid = cred->euid;
  1456. stat->gid = cred->egid;
  1457. }
  1458. }
  1459. rcu_read_unlock();
  1460. return 0;
  1461. }
  1462. /* dentry stuff */
  1463. /*
  1464. * Exceptional case: normally we are not allowed to unhash a busy
  1465. * directory. In this case, however, we can do it - no aliasing problems
  1466. * due to the way we treat inodes.
  1467. *
  1468. * Rewrite the inode's ownerships here because the owning task may have
  1469. * performed a setuid(), etc.
  1470. *
  1471. * Before the /proc/pid/status file was created the only way to read
  1472. * the effective uid of a /process was to stat /proc/pid. Reading
  1473. * /proc/pid/status is slow enough that procps and other packages
  1474. * kept stating /proc/pid. To keep the rules in /proc simple I have
  1475. * made this apply to all per process world readable and executable
  1476. * directories.
  1477. */
  1478. int pid_revalidate(struct dentry *dentry, unsigned int flags)
  1479. {
  1480. struct inode *inode;
  1481. struct task_struct *task;
  1482. const struct cred *cred;
  1483. if (flags & LOOKUP_RCU)
  1484. return -ECHILD;
  1485. inode = d_inode(dentry);
  1486. task = get_proc_task(inode);
  1487. if (task) {
  1488. if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
  1489. task_dumpable(task)) {
  1490. rcu_read_lock();
  1491. cred = __task_cred(task);
  1492. inode->i_uid = cred->euid;
  1493. inode->i_gid = cred->egid;
  1494. rcu_read_unlock();
  1495. } else {
  1496. inode->i_uid = GLOBAL_ROOT_UID;
  1497. inode->i_gid = GLOBAL_ROOT_GID;
  1498. }
  1499. inode->i_mode &= ~(S_ISUID | S_ISGID);
  1500. security_task_to_inode(task, inode);
  1501. put_task_struct(task);
  1502. return 1;
  1503. }
  1504. return 0;
  1505. }
  1506. static inline bool proc_inode_is_dead(struct inode *inode)
  1507. {
  1508. return !proc_pid(inode)->tasks[PIDTYPE_PID].first;
  1509. }
  1510. int pid_delete_dentry(const struct dentry *dentry)
  1511. {
  1512. /* Is the task we represent dead?
  1513. * If so, then don't put the dentry on the lru list,
  1514. * kill it immediately.
  1515. */
  1516. return proc_inode_is_dead(d_inode(dentry));
  1517. }
  1518. const struct dentry_operations pid_dentry_operations =
  1519. {
  1520. .d_revalidate = pid_revalidate,
  1521. .d_delete = pid_delete_dentry,
  1522. };
  1523. /* Lookups */
  1524. /*
  1525. * Fill a directory entry.
  1526. *
  1527. * If possible create the dcache entry and derive our inode number and
  1528. * file type from dcache entry.
  1529. *
  1530. * Since all of the proc inode numbers are dynamically generated, the inode
  1531. * numbers do not exist until the inode is cache. This means creating the
  1532. * the dcache entry in readdir is necessary to keep the inode numbers
  1533. * reported by readdir in sync with the inode numbers reported
  1534. * by stat.
  1535. */
  1536. bool proc_fill_cache(struct file *file, struct dir_context *ctx,
  1537. const char *name, int len,
  1538. instantiate_t instantiate, struct task_struct *task, const void *ptr)
  1539. {
  1540. struct dentry *child, *dir = file->f_path.dentry;
  1541. struct qstr qname = QSTR_INIT(name, len);
  1542. struct inode *inode;
  1543. unsigned type;
  1544. ino_t ino;
  1545. child = d_hash_and_lookup(dir, &qname);
  1546. if (!child) {
  1547. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
  1548. child = d_alloc_parallel(dir, &qname, &wq);
  1549. if (IS_ERR(child))
  1550. goto end_instantiate;
  1551. if (d_in_lookup(child)) {
  1552. int err = instantiate(d_inode(dir), child, task, ptr);
  1553. d_lookup_done(child);
  1554. if (err < 0) {
  1555. dput(child);
  1556. goto end_instantiate;
  1557. }
  1558. }
  1559. }
  1560. inode = d_inode(child);
  1561. ino = inode->i_ino;
  1562. type = inode->i_mode >> 12;
  1563. dput(child);
  1564. return dir_emit(ctx, name, len, ino, type);
  1565. end_instantiate:
  1566. return dir_emit(ctx, name, len, 1, DT_UNKNOWN);
  1567. }
  1568. /*
  1569. * dname_to_vma_addr - maps a dentry name into two unsigned longs
  1570. * which represent vma start and end addresses.
  1571. */
  1572. static int dname_to_vma_addr(struct dentry *dentry,
  1573. unsigned long *start, unsigned long *end)
  1574. {
  1575. if (sscanf(dentry->d_name.name, "%lx-%lx", start, end) != 2)
  1576. return -EINVAL;
  1577. return 0;
  1578. }
  1579. static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
  1580. {
  1581. unsigned long vm_start, vm_end;
  1582. bool exact_vma_exists = false;
  1583. struct mm_struct *mm = NULL;
  1584. struct task_struct *task;
  1585. const struct cred *cred;
  1586. struct inode *inode;
  1587. int status = 0;
  1588. if (flags & LOOKUP_RCU)
  1589. return -ECHILD;
  1590. inode = d_inode(dentry);
  1591. task = get_proc_task(inode);
  1592. if (!task)
  1593. goto out_notask;
  1594. mm = mm_access(task, PTRACE_MODE_READ_FSCREDS);
  1595. if (IS_ERR_OR_NULL(mm))
  1596. goto out;
  1597. if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
  1598. down_read(&mm->mmap_sem);
  1599. exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
  1600. up_read(&mm->mmap_sem);
  1601. }
  1602. mmput(mm);
  1603. if (exact_vma_exists) {
  1604. if (task_dumpable(task)) {
  1605. rcu_read_lock();
  1606. cred = __task_cred(task);
  1607. inode->i_uid = cred->euid;
  1608. inode->i_gid = cred->egid;
  1609. rcu_read_unlock();
  1610. } else {
  1611. inode->i_uid = GLOBAL_ROOT_UID;
  1612. inode->i_gid = GLOBAL_ROOT_GID;
  1613. }
  1614. security_task_to_inode(task, inode);
  1615. status = 1;
  1616. }
  1617. out:
  1618. put_task_struct(task);
  1619. out_notask:
  1620. return status;
  1621. }
  1622. static const struct dentry_operations tid_map_files_dentry_operations = {
  1623. .d_revalidate = map_files_d_revalidate,
  1624. .d_delete = pid_delete_dentry,
  1625. };
  1626. static int map_files_get_link(struct dentry *dentry, struct path *path)
  1627. {
  1628. unsigned long vm_start, vm_end;
  1629. struct vm_area_struct *vma;
  1630. struct task_struct *task;
  1631. struct mm_struct *mm;
  1632. int rc;
  1633. rc = -ENOENT;
  1634. task = get_proc_task(d_inode(dentry));
  1635. if (!task)
  1636. goto out;
  1637. mm = get_task_mm(task);
  1638. put_task_struct(task);
  1639. if (!mm)
  1640. goto out;
  1641. rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
  1642. if (rc)
  1643. goto out_mmput;
  1644. rc = -ENOENT;
  1645. down_read(&mm->mmap_sem);
  1646. vma = find_exact_vma(mm, vm_start, vm_end);
  1647. if (vma && vma->vm_file) {
  1648. *path = vma->vm_file->f_path;
  1649. path_get(path);
  1650. rc = 0;
  1651. }
  1652. up_read(&mm->mmap_sem);
  1653. out_mmput:
  1654. mmput(mm);
  1655. out:
  1656. return rc;
  1657. }
  1658. struct map_files_info {
  1659. fmode_t mode;
  1660. unsigned long len;
  1661. unsigned char name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
  1662. };
  1663. /*
  1664. * Only allow CAP_SYS_ADMIN to follow the links, due to concerns about how the
  1665. * symlinks may be used to bypass permissions on ancestor directories in the
  1666. * path to the file in question.
  1667. */
  1668. static const char *
  1669. proc_map_files_get_link(struct dentry *dentry,
  1670. struct inode *inode,
  1671. struct delayed_call *done)
  1672. {
  1673. if (!capable(CAP_SYS_ADMIN))
  1674. return ERR_PTR(-EPERM);
  1675. return proc_pid_get_link(dentry, inode, done);
  1676. }
  1677. /*
  1678. * Identical to proc_pid_link_inode_operations except for get_link()
  1679. */
  1680. static const struct inode_operations proc_map_files_link_inode_operations = {
  1681. .readlink = proc_pid_readlink,
  1682. .get_link = proc_map_files_get_link,
  1683. .setattr = proc_setattr,
  1684. };
  1685. static int
  1686. proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
  1687. struct task_struct *task, const void *ptr)
  1688. {
  1689. fmode_t mode = (fmode_t)(unsigned long)ptr;
  1690. struct proc_inode *ei;
  1691. struct inode *inode;
  1692. inode = proc_pid_make_inode(dir->i_sb, task);
  1693. if (!inode)
  1694. return -ENOENT;
  1695. ei = PROC_I(inode);
  1696. ei->op.proc_get_link = map_files_get_link;
  1697. inode->i_op = &proc_map_files_link_inode_operations;
  1698. inode->i_size = 64;
  1699. inode->i_mode = S_IFLNK;
  1700. if (mode & FMODE_READ)
  1701. inode->i_mode |= S_IRUSR;
  1702. if (mode & FMODE_WRITE)
  1703. inode->i_mode |= S_IWUSR;
  1704. d_set_d_op(dentry, &tid_map_files_dentry_operations);
  1705. d_add(dentry, inode);
  1706. return 0;
  1707. }
  1708. static struct dentry *proc_map_files_lookup(struct inode *dir,
  1709. struct dentry *dentry, unsigned int flags)
  1710. {
  1711. unsigned long vm_start, vm_end;
  1712. struct vm_area_struct *vma;
  1713. struct task_struct *task;
  1714. int result;
  1715. struct mm_struct *mm;
  1716. result = -ENOENT;
  1717. task = get_proc_task(dir);
  1718. if (!task)
  1719. goto out;
  1720. result = -EACCES;
  1721. if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
  1722. goto out_put_task;
  1723. result = -ENOENT;
  1724. if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
  1725. goto out_put_task;
  1726. mm = get_task_mm(task);
  1727. if (!mm)
  1728. goto out_put_task;
  1729. down_read(&mm->mmap_sem);
  1730. vma = find_exact_vma(mm, vm_start, vm_end);
  1731. if (!vma)
  1732. goto out_no_vma;
  1733. if (vma->vm_file)
  1734. result = proc_map_files_instantiate(dir, dentry, task,
  1735. (void *)(unsigned long)vma->vm_file->f_mode);
  1736. out_no_vma:
  1737. up_read(&mm->mmap_sem);
  1738. mmput(mm);
  1739. out_put_task:
  1740. put_task_struct(task);
  1741. out:
  1742. return ERR_PTR(result);
  1743. }
  1744. static const struct inode_operations proc_map_files_inode_operations = {
  1745. .lookup = proc_map_files_lookup,
  1746. .permission = proc_fd_permission,
  1747. .setattr = proc_setattr,
  1748. };
  1749. static int
  1750. proc_map_files_readdir(struct file *file, struct dir_context *ctx)
  1751. {
  1752. struct vm_area_struct *vma;
  1753. struct task_struct *task;
  1754. struct mm_struct *mm;
  1755. unsigned long nr_files, pos, i;
  1756. struct flex_array *fa = NULL;
  1757. struct map_files_info info;
  1758. struct map_files_info *p;
  1759. int ret;
  1760. ret = -ENOENT;
  1761. task = get_proc_task(file_inode(file));
  1762. if (!task)
  1763. goto out;
  1764. ret = -EACCES;
  1765. if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
  1766. goto out_put_task;
  1767. ret = 0;
  1768. if (!dir_emit_dots(file, ctx))
  1769. goto out_put_task;
  1770. mm = get_task_mm(task);
  1771. if (!mm)
  1772. goto out_put_task;
  1773. down_read(&mm->mmap_sem);
  1774. nr_files = 0;
  1775. /*
  1776. * We need two passes here:
  1777. *
  1778. * 1) Collect vmas of mapped files with mmap_sem taken
  1779. * 2) Release mmap_sem and instantiate entries
  1780. *
  1781. * otherwise we get lockdep complained, since filldir()
  1782. * routine might require mmap_sem taken in might_fault().
  1783. */
  1784. for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
  1785. if (vma->vm_file && ++pos > ctx->pos)
  1786. nr_files++;
  1787. }
  1788. if (nr_files) {
  1789. fa = flex_array_alloc(sizeof(info), nr_files,
  1790. GFP_KERNEL);
  1791. if (!fa || flex_array_prealloc(fa, 0, nr_files,
  1792. GFP_KERNEL)) {
  1793. ret = -ENOMEM;
  1794. if (fa)
  1795. flex_array_free(fa);
  1796. up_read(&mm->mmap_sem);
  1797. mmput(mm);
  1798. goto out_put_task;
  1799. }
  1800. for (i = 0, vma = mm->mmap, pos = 2; vma;
  1801. vma = vma->vm_next) {
  1802. if (!vma->vm_file)
  1803. continue;
  1804. if (++pos <= ctx->pos)
  1805. continue;
  1806. info.mode = vma->vm_file->f_mode;
  1807. info.len = snprintf(info.name,
  1808. sizeof(info.name), "%lx-%lx",
  1809. vma->vm_start, vma->vm_end);
  1810. if (flex_array_put(fa, i++, &info, GFP_KERNEL))
  1811. BUG();
  1812. }
  1813. }
  1814. up_read(&mm->mmap_sem);
  1815. for (i = 0; i < nr_files; i++) {
  1816. p = flex_array_get(fa, i);
  1817. if (!proc_fill_cache(file, ctx,
  1818. p->name, p->len,
  1819. proc_map_files_instantiate,
  1820. task,
  1821. (void *)(unsigned long)p->mode))
  1822. break;
  1823. ctx->pos++;
  1824. }
  1825. if (fa)
  1826. flex_array_free(fa);
  1827. mmput(mm);
  1828. out_put_task:
  1829. put_task_struct(task);
  1830. out:
  1831. return ret;
  1832. }
  1833. static const struct file_operations proc_map_files_operations = {
  1834. .read = generic_read_dir,
  1835. .iterate_shared = proc_map_files_readdir,
  1836. .llseek = generic_file_llseek,
  1837. };
  1838. #ifdef CONFIG_CHECKPOINT_RESTORE
  1839. struct timers_private {
  1840. struct pid *pid;
  1841. struct task_struct *task;
  1842. struct sighand_struct *sighand;
  1843. struct pid_namespace *ns;
  1844. unsigned long flags;
  1845. };
  1846. static void *timers_start(struct seq_file *m, loff_t *pos)
  1847. {
  1848. struct timers_private *tp = m->private;
  1849. tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
  1850. if (!tp->task)
  1851. return ERR_PTR(-ESRCH);
  1852. tp->sighand = lock_task_sighand(tp->task, &tp->flags);
  1853. if (!tp->sighand)
  1854. return ERR_PTR(-ESRCH);
  1855. return seq_list_start(&tp->task->signal->posix_timers, *pos);
  1856. }
  1857. static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
  1858. {
  1859. struct timers_private *tp = m->private;
  1860. return seq_list_next(v, &tp->task->signal->posix_timers, pos);
  1861. }
  1862. static void timers_stop(struct seq_file *m, void *v)
  1863. {
  1864. struct timers_private *tp = m->private;
  1865. if (tp->sighand) {
  1866. unlock_task_sighand(tp->task, &tp->flags);
  1867. tp->sighand = NULL;
  1868. }
  1869. if (tp->task) {
  1870. put_task_struct(tp->task);
  1871. tp->task = NULL;
  1872. }
  1873. }
  1874. static int show_timer(struct seq_file *m, void *v)
  1875. {
  1876. struct k_itimer *timer;
  1877. struct timers_private *tp = m->private;
  1878. int notify;
  1879. static const char * const nstr[] = {
  1880. [SIGEV_SIGNAL] = "signal",
  1881. [SIGEV_NONE] = "none",
  1882. [SIGEV_THREAD] = "thread",
  1883. };
  1884. timer = list_entry((struct list_head *)v, struct k_itimer, list);
  1885. notify = timer->it_sigev_notify;
  1886. seq_printf(m, "ID: %d\n", timer->it_id);
  1887. seq_printf(m, "signal: %d/%p\n",
  1888. timer->sigq->info.si_signo,
  1889. timer->sigq->info.si_value.sival_ptr);
  1890. seq_printf(m, "notify: %s/%s.%d\n",
  1891. nstr[notify & ~SIGEV_THREAD_ID],
  1892. (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
  1893. pid_nr_ns(timer->it_pid, tp->ns));
  1894. seq_printf(m, "ClockID: %d\n", timer->it_clock);
  1895. return 0;
  1896. }
  1897. static const struct seq_operations proc_timers_seq_ops = {
  1898. .start = timers_start,
  1899. .next = timers_next,
  1900. .stop = timers_stop,
  1901. .show = show_timer,
  1902. };
  1903. static int proc_timers_open(struct inode *inode, struct file *file)
  1904. {
  1905. struct timers_private *tp;
  1906. tp = __seq_open_private(file, &proc_timers_seq_ops,
  1907. sizeof(struct timers_private));
  1908. if (!tp)
  1909. return -ENOMEM;
  1910. tp->pid = proc_pid(inode);
  1911. tp->ns = inode->i_sb->s_fs_info;
  1912. return 0;
  1913. }
  1914. static const struct file_operations proc_timers_operations = {
  1915. .open = proc_timers_open,
  1916. .read = seq_read,
  1917. .llseek = seq_lseek,
  1918. .release = seq_release_private,
  1919. };
  1920. #endif
  1921. static ssize_t timerslack_ns_write(struct file *file, const char __user *buf,
  1922. size_t count, loff_t *offset)
  1923. {
  1924. struct inode *inode = file_inode(file);
  1925. struct task_struct *p;
  1926. u64 slack_ns;
  1927. int err;
  1928. err = kstrtoull_from_user(buf, count, 10, &slack_ns);
  1929. if (err < 0)
  1930. return err;
  1931. p = get_proc_task(inode);
  1932. if (!p)
  1933. return -ESRCH;
  1934. if (ptrace_may_access(p, PTRACE_MODE_ATTACH_FSCREDS)) {
  1935. task_lock(p);
  1936. if (slack_ns == 0)
  1937. p->timer_slack_ns = p->default_timer_slack_ns;
  1938. else
  1939. p->timer_slack_ns = slack_ns;
  1940. task_unlock(p);
  1941. } else
  1942. count = -EPERM;
  1943. put_task_struct(p);
  1944. return count;
  1945. }
  1946. static int timerslack_ns_show(struct seq_file *m, void *v)
  1947. {
  1948. struct inode *inode = m->private;
  1949. struct task_struct *p;
  1950. int err = 0;
  1951. p = get_proc_task(inode);
  1952. if (!p)
  1953. return -ESRCH;
  1954. if (ptrace_may_access(p, PTRACE_MODE_ATTACH_FSCREDS)) {
  1955. task_lock(p);
  1956. seq_printf(m, "%llu\n", p->timer_slack_ns);
  1957. task_unlock(p);
  1958. } else
  1959. err = -EPERM;
  1960. put_task_struct(p);
  1961. return err;
  1962. }
  1963. static int timerslack_ns_open(struct inode *inode, struct file *filp)
  1964. {
  1965. return single_open(filp, timerslack_ns_show, inode);
  1966. }
  1967. static const struct file_operations proc_pid_set_timerslack_ns_operations = {
  1968. .open = timerslack_ns_open,
  1969. .read = seq_read,
  1970. .write = timerslack_ns_write,
  1971. .llseek = seq_lseek,
  1972. .release = single_release,
  1973. };
  1974. static int proc_pident_instantiate(struct inode *dir,
  1975. struct dentry *dentry, struct task_struct *task, const void *ptr)
  1976. {
  1977. const struct pid_entry *p = ptr;
  1978. struct inode *inode;
  1979. struct proc_inode *ei;
  1980. inode = proc_pid_make_inode(dir->i_sb, task);
  1981. if (!inode)
  1982. goto out;
  1983. ei = PROC_I(inode);
  1984. inode->i_mode = p->mode;
  1985. if (S_ISDIR(inode->i_mode))
  1986. set_nlink(inode, 2); /* Use getattr to fix if necessary */
  1987. if (p->iop)
  1988. inode->i_op = p->iop;
  1989. if (p->fop)
  1990. inode->i_fop = p->fop;
  1991. ei->op = p->op;
  1992. d_set_d_op(dentry, &pid_dentry_operations);
  1993. d_add(dentry, inode);
  1994. /* Close the race of the process dying before we return the dentry */
  1995. if (pid_revalidate(dentry, 0))
  1996. return 0;
  1997. out:
  1998. return -ENOENT;
  1999. }
  2000. static struct dentry *proc_pident_lookup(struct inode *dir,
  2001. struct dentry *dentry,
  2002. const struct pid_entry *ents,
  2003. unsigned int nents)
  2004. {
  2005. int error;
  2006. struct task_struct *task = get_proc_task(dir);
  2007. const struct pid_entry *p, *last;
  2008. error = -ENOENT;
  2009. if (!task)
  2010. goto out_no_task;
  2011. /*
  2012. * Yes, it does not scale. And it should not. Don't add
  2013. * new entries into /proc/<tgid>/ without very good reasons.
  2014. */
  2015. last = &ents[nents - 1];
  2016. for (p = ents; p <= last; p++) {
  2017. if (p->len != dentry->d_name.len)
  2018. continue;
  2019. if (!memcmp(dentry->d_name.name, p->name, p->len))
  2020. break;
  2021. }
  2022. if (p > last)
  2023. goto out;
  2024. error = proc_pident_instantiate(dir, dentry, task, p);
  2025. out:
  2026. put_task_struct(task);
  2027. out_no_task:
  2028. return ERR_PTR(error);
  2029. }
  2030. static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
  2031. const struct pid_entry *ents, unsigned int nents)
  2032. {
  2033. struct task_struct *task = get_proc_task(file_inode(file));
  2034. const struct pid_entry *p;
  2035. if (!task)
  2036. return -ENOENT;
  2037. if (!dir_emit_dots(file, ctx))
  2038. goto out;
  2039. if (ctx->pos >= nents + 2)
  2040. goto out;
  2041. for (p = ents + (ctx->pos - 2); p <= ents + nents - 1; p++) {
  2042. if (!proc_fill_cache(file, ctx, p->name, p->len,
  2043. proc_pident_instantiate, task, p))
  2044. break;
  2045. ctx->pos++;
  2046. }
  2047. out:
  2048. put_task_struct(task);
  2049. return 0;
  2050. }
  2051. #ifdef CONFIG_SECURITY
  2052. static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
  2053. size_t count, loff_t *ppos)
  2054. {
  2055. struct inode * inode = file_inode(file);
  2056. char *p = NULL;
  2057. ssize_t length;
  2058. struct task_struct *task = get_proc_task(inode);
  2059. if (!task)
  2060. return -ESRCH;
  2061. length = security_getprocattr(task,
  2062. (char*)file->f_path.dentry->d_name.name,
  2063. &p);
  2064. put_task_struct(task);
  2065. if (length > 0)
  2066. length = simple_read_from_buffer(buf, count, ppos, p, length);
  2067. kfree(p);
  2068. return length;
  2069. }
  2070. static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
  2071. size_t count, loff_t *ppos)
  2072. {
  2073. struct inode * inode = file_inode(file);
  2074. void *page;
  2075. ssize_t length;
  2076. struct task_struct *task = get_proc_task(inode);
  2077. length = -ESRCH;
  2078. if (!task)
  2079. goto out_no_task;
  2080. if (count > PAGE_SIZE)
  2081. count = PAGE_SIZE;
  2082. /* No partial writes. */
  2083. length = -EINVAL;
  2084. if (*ppos != 0)
  2085. goto out;
  2086. page = memdup_user(buf, count);
  2087. if (IS_ERR(page)) {
  2088. length = PTR_ERR(page);
  2089. goto out;
  2090. }
  2091. /* Guard against adverse ptrace interaction */
  2092. length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
  2093. if (length < 0)
  2094. goto out_free;
  2095. length = security_setprocattr(task,
  2096. (char*)file->f_path.dentry->d_name.name,
  2097. page, count);
  2098. mutex_unlock(&task->signal->cred_guard_mutex);
  2099. out_free:
  2100. kfree(page);
  2101. out:
  2102. put_task_struct(task);
  2103. out_no_task:
  2104. return length;
  2105. }
  2106. static const struct file_operations proc_pid_attr_operations = {
  2107. .read = proc_pid_attr_read,
  2108. .write = proc_pid_attr_write,
  2109. .llseek = generic_file_llseek,
  2110. };
  2111. static const struct pid_entry attr_dir_stuff[] = {
  2112. REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
  2113. REG("prev", S_IRUGO, proc_pid_attr_operations),
  2114. REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
  2115. REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
  2116. REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
  2117. REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
  2118. };
  2119. static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
  2120. {
  2121. return proc_pident_readdir(file, ctx,
  2122. attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
  2123. }
  2124. static const struct file_operations proc_attr_dir_operations = {
  2125. .read = generic_read_dir,
  2126. .iterate_shared = proc_attr_dir_readdir,
  2127. .llseek = generic_file_llseek,
  2128. };
  2129. static struct dentry *proc_attr_dir_lookup(struct inode *dir,
  2130. struct dentry *dentry, unsigned int flags)
  2131. {
  2132. return proc_pident_lookup(dir, dentry,
  2133. attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
  2134. }
  2135. static const struct inode_operations proc_attr_dir_inode_operations = {
  2136. .lookup = proc_attr_dir_lookup,
  2137. .getattr = pid_getattr,
  2138. .setattr = proc_setattr,
  2139. };
  2140. #endif
  2141. #ifdef CONFIG_ELF_CORE
  2142. static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
  2143. size_t count, loff_t *ppos)
  2144. {
  2145. struct task_struct *task = get_proc_task(file_inode(file));
  2146. struct mm_struct *mm;
  2147. char buffer[PROC_NUMBUF];
  2148. size_t len;
  2149. int ret;
  2150. if (!task)
  2151. return -ESRCH;
  2152. ret = 0;
  2153. mm = get_task_mm(task);
  2154. if (mm) {
  2155. len = snprintf(buffer, sizeof(buffer), "%08lx\n",
  2156. ((mm->flags & MMF_DUMP_FILTER_MASK) >>
  2157. MMF_DUMP_FILTER_SHIFT));
  2158. mmput(mm);
  2159. ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
  2160. }
  2161. put_task_struct(task);
  2162. return ret;
  2163. }
  2164. static ssize_t proc_coredump_filter_write(struct file *file,
  2165. const char __user *buf,
  2166. size_t count,
  2167. loff_t *ppos)
  2168. {
  2169. struct task_struct *task;
  2170. struct mm_struct *mm;
  2171. unsigned int val;
  2172. int ret;
  2173. int i;
  2174. unsigned long mask;
  2175. ret = kstrtouint_from_user(buf, count, 0, &val);
  2176. if (ret < 0)
  2177. return ret;
  2178. ret = -ESRCH;
  2179. task = get_proc_task(file_inode(file));
  2180. if (!task)
  2181. goto out_no_task;
  2182. mm = get_task_mm(task);
  2183. if (!mm)
  2184. goto out_no_mm;
  2185. ret = 0;
  2186. for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
  2187. if (val & mask)
  2188. set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
  2189. else
  2190. clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
  2191. }
  2192. mmput(mm);
  2193. out_no_mm:
  2194. put_task_struct(task);
  2195. out_no_task:
  2196. if (ret < 0)
  2197. return ret;
  2198. return count;
  2199. }
  2200. static const struct file_operations proc_coredump_filter_operations = {
  2201. .read = proc_coredump_filter_read,
  2202. .write = proc_coredump_filter_write,
  2203. .llseek = generic_file_llseek,
  2204. };
  2205. #endif
  2206. #ifdef CONFIG_TASK_IO_ACCOUNTING
  2207. static int do_io_accounting(struct task_struct *task, struct seq_file *m, int whole)
  2208. {
  2209. struct task_io_accounting acct = task->ioac;
  2210. unsigned long flags;
  2211. int result;
  2212. result = mutex_lock_killable(&task->signal->cred_guard_mutex);
  2213. if (result)
  2214. return result;
  2215. if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
  2216. result = -EACCES;
  2217. goto out_unlock;
  2218. }
  2219. if (whole && lock_task_sighand(task, &flags)) {
  2220. struct task_struct *t = task;
  2221. task_io_accounting_add(&acct, &task->signal->ioac);
  2222. while_each_thread(task, t)
  2223. task_io_accounting_add(&acct, &t->ioac);
  2224. unlock_task_sighand(task, &flags);
  2225. }
  2226. seq_printf(m,
  2227. "rchar: %llu\n"
  2228. "wchar: %llu\n"
  2229. "syscr: %llu\n"
  2230. "syscw: %llu\n"
  2231. "read_bytes: %llu\n"
  2232. "write_bytes: %llu\n"
  2233. "cancelled_write_bytes: %llu\n",
  2234. (unsigned long long)acct.rchar,
  2235. (unsigned long long)acct.wchar,
  2236. (unsigned long long)acct.syscr,
  2237. (unsigned long long)acct.syscw,
  2238. (unsigned long long)acct.read_bytes,
  2239. (unsigned long long)acct.write_bytes,
  2240. (unsigned long long)acct.cancelled_write_bytes);
  2241. result = 0;
  2242. out_unlock:
  2243. mutex_unlock(&task->signal->cred_guard_mutex);
  2244. return result;
  2245. }
  2246. static int proc_tid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
  2247. struct pid *pid, struct task_struct *task)
  2248. {
  2249. return do_io_accounting(task, m, 0);
  2250. }
  2251. static int proc_tgid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
  2252. struct pid *pid, struct task_struct *task)
  2253. {
  2254. return do_io_accounting(task, m, 1);
  2255. }
  2256. #endif /* CONFIG_TASK_IO_ACCOUNTING */
  2257. #ifdef CONFIG_USER_NS
  2258. static int proc_id_map_open(struct inode *inode, struct file *file,
  2259. const struct seq_operations *seq_ops)
  2260. {
  2261. struct user_namespace *ns = NULL;
  2262. struct task_struct *task;
  2263. struct seq_file *seq;
  2264. int ret = -EINVAL;
  2265. task = get_proc_task(inode);
  2266. if (task) {
  2267. rcu_read_lock();
  2268. ns = get_user_ns(task_cred_xxx(task, user_ns));
  2269. rcu_read_unlock();
  2270. put_task_struct(task);
  2271. }
  2272. if (!ns)
  2273. goto err;
  2274. ret = seq_open(file, seq_ops);
  2275. if (ret)
  2276. goto err_put_ns;
  2277. seq = file->private_data;
  2278. seq->private = ns;
  2279. return 0;
  2280. err_put_ns:
  2281. put_user_ns(ns);
  2282. err:
  2283. return ret;
  2284. }
  2285. static int proc_id_map_release(struct inode *inode, struct file *file)
  2286. {
  2287. struct seq_file *seq = file->private_data;
  2288. struct user_namespace *ns = seq->private;
  2289. put_user_ns(ns);
  2290. return seq_release(inode, file);
  2291. }
  2292. static int proc_uid_map_open(struct inode *inode, struct file *file)
  2293. {
  2294. return proc_id_map_open(inode, file, &proc_uid_seq_operations);
  2295. }
  2296. static int proc_gid_map_open(struct inode *inode, struct file *file)
  2297. {
  2298. return proc_id_map_open(inode, file, &proc_gid_seq_operations);
  2299. }
  2300. static int proc_projid_map_open(struct inode *inode, struct file *file)
  2301. {
  2302. return proc_id_map_open(inode, file, &proc_projid_seq_operations);
  2303. }
  2304. static const struct file_operations proc_uid_map_operations = {
  2305. .open = proc_uid_map_open,
  2306. .write = proc_uid_map_write,
  2307. .read = seq_read,
  2308. .llseek = seq_lseek,
  2309. .release = proc_id_map_release,
  2310. };
  2311. static const struct file_operations proc_gid_map_operations = {
  2312. .open = proc_gid_map_open,
  2313. .write = proc_gid_map_write,
  2314. .read = seq_read,
  2315. .llseek = seq_lseek,
  2316. .release = proc_id_map_release,
  2317. };
  2318. static const struct file_operations proc_projid_map_operations = {
  2319. .open = proc_projid_map_open,
  2320. .write = proc_projid_map_write,
  2321. .read = seq_read,
  2322. .llseek = seq_lseek,
  2323. .release = proc_id_map_release,
  2324. };
  2325. static int proc_setgroups_open(struct inode *inode, struct file *file)
  2326. {
  2327. struct user_namespace *ns = NULL;
  2328. struct task_struct *task;
  2329. int ret;
  2330. ret = -ESRCH;
  2331. task = get_proc_task(inode);
  2332. if (task) {
  2333. rcu_read_lock();
  2334. ns = get_user_ns(task_cred_xxx(task, user_ns));
  2335. rcu_read_unlock();
  2336. put_task_struct(task);
  2337. }
  2338. if (!ns)
  2339. goto err;
  2340. if (file->f_mode & FMODE_WRITE) {
  2341. ret = -EACCES;
  2342. if (!ns_capable(ns, CAP_SYS_ADMIN))
  2343. goto err_put_ns;
  2344. }
  2345. ret = single_open(file, &proc_setgroups_show, ns);
  2346. if (ret)
  2347. goto err_put_ns;
  2348. return 0;
  2349. err_put_ns:
  2350. put_user_ns(ns);
  2351. err:
  2352. return ret;
  2353. }
  2354. static int proc_setgroups_release(struct inode *inode, struct file *file)
  2355. {
  2356. struct seq_file *seq = file->private_data;
  2357. struct user_namespace *ns = seq->private;
  2358. int ret = single_release(inode, file);
  2359. put_user_ns(ns);
  2360. return ret;
  2361. }
  2362. static const struct file_operations proc_setgroups_operations = {
  2363. .open = proc_setgroups_open,
  2364. .write = proc_setgroups_write,
  2365. .read = seq_read,
  2366. .llseek = seq_lseek,
  2367. .release = proc_setgroups_release,
  2368. };
  2369. #endif /* CONFIG_USER_NS */
  2370. static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
  2371. struct pid *pid, struct task_struct *task)
  2372. {
  2373. int err = lock_trace(task);
  2374. if (!err) {
  2375. seq_printf(m, "%08x\n", task->personality);
  2376. unlock_trace(task);
  2377. }
  2378. return err;
  2379. }
  2380. /*
  2381. * Thread groups
  2382. */
  2383. static const struct file_operations proc_task_operations;
  2384. static const struct inode_operations proc_task_inode_operations;
  2385. static const struct pid_entry tgid_base_stuff[] = {
  2386. DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
  2387. DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
  2388. DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
  2389. DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
  2390. DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
  2391. #ifdef CONFIG_NET
  2392. DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
  2393. #endif
  2394. REG("environ", S_IRUSR, proc_environ_operations),
  2395. ONE("auxv", S_IRUSR, proc_pid_auxv),
  2396. ONE("status", S_IRUGO, proc_pid_status),
  2397. ONE("personality", S_IRUSR, proc_pid_personality),
  2398. ONE("limits", S_IRUGO, proc_pid_limits),
  2399. #ifdef CONFIG_SCHED_DEBUG
  2400. REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
  2401. #endif
  2402. #ifdef CONFIG_SCHED_AUTOGROUP
  2403. REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
  2404. #endif
  2405. REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
  2406. #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
  2407. ONE("syscall", S_IRUSR, proc_pid_syscall),
  2408. #endif
  2409. REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
  2410. ONE("stat", S_IRUGO, proc_tgid_stat),
  2411. ONE("statm", S_IRUGO, proc_pid_statm),
  2412. REG("maps", S_IRUGO, proc_pid_maps_operations),
  2413. #ifdef CONFIG_NUMA
  2414. REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
  2415. #endif
  2416. REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
  2417. LNK("cwd", proc_cwd_link),
  2418. LNK("root", proc_root_link),
  2419. LNK("exe", proc_exe_link),
  2420. REG("mounts", S_IRUGO, proc_mounts_operations),
  2421. REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
  2422. REG("mountstats", S_IRUSR, proc_mountstats_operations),
  2423. #ifdef CONFIG_PROC_PAGE_MONITOR
  2424. REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
  2425. REG("smaps", S_IRUGO, proc_pid_smaps_operations),
  2426. REG("pagemap", S_IRUSR, proc_pagemap_operations),
  2427. #endif
  2428. #ifdef CONFIG_SECURITY
  2429. DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
  2430. #endif
  2431. #ifdef CONFIG_KALLSYMS
  2432. ONE("wchan", S_IRUGO, proc_pid_wchan),
  2433. #endif
  2434. #ifdef CONFIG_STACKTRACE
  2435. ONE("stack", S_IRUSR, proc_pid_stack),
  2436. #endif
  2437. #ifdef CONFIG_SCHED_INFO
  2438. ONE("schedstat", S_IRUGO, proc_pid_schedstat),
  2439. #endif
  2440. #ifdef CONFIG_LATENCYTOP
  2441. REG("latency", S_IRUGO, proc_lstats_operations),
  2442. #endif
  2443. #ifdef CONFIG_PROC_PID_CPUSET
  2444. ONE("cpuset", S_IRUGO, proc_cpuset_show),
  2445. #endif
  2446. #ifdef CONFIG_CGROUPS
  2447. ONE("cgroup", S_IRUGO, proc_cgroup_show),
  2448. #endif
  2449. ONE("oom_score", S_IRUGO, proc_oom_score),
  2450. REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
  2451. REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
  2452. #ifdef CONFIG_AUDITSYSCALL
  2453. REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
  2454. REG("sessionid", S_IRUGO, proc_sessionid_operations),
  2455. #endif
  2456. #ifdef CONFIG_FAULT_INJECTION
  2457. REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
  2458. #endif
  2459. #ifdef CONFIG_ELF_CORE
  2460. REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
  2461. #endif
  2462. #ifdef CONFIG_TASK_IO_ACCOUNTING
  2463. ONE("io", S_IRUSR, proc_tgid_io_accounting),
  2464. #endif
  2465. #ifdef CONFIG_HARDWALL
  2466. ONE("hardwall", S_IRUGO, proc_pid_hardwall),
  2467. #endif
  2468. #ifdef CONFIG_USER_NS
  2469. REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
  2470. REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
  2471. REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
  2472. REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
  2473. #endif
  2474. #ifdef CONFIG_CHECKPOINT_RESTORE
  2475. REG("timers", S_IRUGO, proc_timers_operations),
  2476. #endif
  2477. REG("timerslack_ns", S_IRUGO|S_IWUGO, proc_pid_set_timerslack_ns_operations),
  2478. };
  2479. static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
  2480. {
  2481. return proc_pident_readdir(file, ctx,
  2482. tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
  2483. }
  2484. static const struct file_operations proc_tgid_base_operations = {
  2485. .read = generic_read_dir,
  2486. .iterate_shared = proc_tgid_base_readdir,
  2487. .llseek = generic_file_llseek,
  2488. };
  2489. static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
  2490. {
  2491. return proc_pident_lookup(dir, dentry,
  2492. tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
  2493. }
  2494. static const struct inode_operations proc_tgid_base_inode_operations = {
  2495. .lookup = proc_tgid_base_lookup,
  2496. .getattr = pid_getattr,
  2497. .setattr = proc_setattr,
  2498. .permission = proc_pid_permission,
  2499. };
  2500. static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
  2501. {
  2502. struct dentry *dentry, *leader, *dir;
  2503. char buf[PROC_NUMBUF];
  2504. struct qstr name;
  2505. name.name = buf;
  2506. name.len = snprintf(buf, sizeof(buf), "%d", pid);
  2507. /* no ->d_hash() rejects on procfs */
  2508. dentry = d_hash_and_lookup(mnt->mnt_root, &name);
  2509. if (dentry) {
  2510. d_invalidate(dentry);
  2511. dput(dentry);
  2512. }
  2513. if (pid == tgid)
  2514. return;
  2515. name.name = buf;
  2516. name.len = snprintf(buf, sizeof(buf), "%d", tgid);
  2517. leader = d_hash_and_lookup(mnt->mnt_root, &name);
  2518. if (!leader)
  2519. goto out;
  2520. name.name = "task";
  2521. name.len = strlen(name.name);
  2522. dir = d_hash_and_lookup(leader, &name);
  2523. if (!dir)
  2524. goto out_put_leader;
  2525. name.name = buf;
  2526. name.len = snprintf(buf, sizeof(buf), "%d", pid);
  2527. dentry = d_hash_and_lookup(dir, &name);
  2528. if (dentry) {
  2529. d_invalidate(dentry);
  2530. dput(dentry);
  2531. }
  2532. dput(dir);
  2533. out_put_leader:
  2534. dput(leader);
  2535. out:
  2536. return;
  2537. }
  2538. /**
  2539. * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
  2540. * @task: task that should be flushed.
  2541. *
  2542. * When flushing dentries from proc, one needs to flush them from global
  2543. * proc (proc_mnt) and from all the namespaces' procs this task was seen
  2544. * in. This call is supposed to do all of this job.
  2545. *
  2546. * Looks in the dcache for
  2547. * /proc/@pid
  2548. * /proc/@tgid/task/@pid
  2549. * if either directory is present flushes it and all of it'ts children
  2550. * from the dcache.
  2551. *
  2552. * It is safe and reasonable to cache /proc entries for a task until
  2553. * that task exits. After that they just clog up the dcache with
  2554. * useless entries, possibly causing useful dcache entries to be
  2555. * flushed instead. This routine is proved to flush those useless
  2556. * dcache entries at process exit time.
  2557. *
  2558. * NOTE: This routine is just an optimization so it does not guarantee
  2559. * that no dcache entries will exist at process exit time it
  2560. * just makes it very unlikely that any will persist.
  2561. */
  2562. void proc_flush_task(struct task_struct *task)
  2563. {
  2564. int i;
  2565. struct pid *pid, *tgid;
  2566. struct upid *upid;
  2567. pid = task_pid(task);
  2568. tgid = task_tgid(task);
  2569. for (i = 0; i <= pid->level; i++) {
  2570. upid = &pid->numbers[i];
  2571. proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
  2572. tgid->numbers[i].nr);
  2573. }
  2574. }
  2575. static int proc_pid_instantiate(struct inode *dir,
  2576. struct dentry * dentry,
  2577. struct task_struct *task, const void *ptr)
  2578. {
  2579. struct inode *inode;
  2580. inode = proc_pid_make_inode(dir->i_sb, task);
  2581. if (!inode)
  2582. goto out;
  2583. inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
  2584. inode->i_op = &proc_tgid_base_inode_operations;
  2585. inode->i_fop = &proc_tgid_base_operations;
  2586. inode->i_flags|=S_IMMUTABLE;
  2587. set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
  2588. ARRAY_SIZE(tgid_base_stuff)));
  2589. d_set_d_op(dentry, &pid_dentry_operations);
  2590. d_add(dentry, inode);
  2591. /* Close the race of the process dying before we return the dentry */
  2592. if (pid_revalidate(dentry, 0))
  2593. return 0;
  2594. out:
  2595. return -ENOENT;
  2596. }
  2597. struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
  2598. {
  2599. int result = -ENOENT;
  2600. struct task_struct *task;
  2601. unsigned tgid;
  2602. struct pid_namespace *ns;
  2603. tgid = name_to_int(&dentry->d_name);
  2604. if (tgid == ~0U)
  2605. goto out;
  2606. ns = dentry->d_sb->s_fs_info;
  2607. rcu_read_lock();
  2608. task = find_task_by_pid_ns(tgid, ns);
  2609. if (task)
  2610. get_task_struct(task);
  2611. rcu_read_unlock();
  2612. if (!task)
  2613. goto out;
  2614. result = proc_pid_instantiate(dir, dentry, task, NULL);
  2615. put_task_struct(task);
  2616. out:
  2617. return ERR_PTR(result);
  2618. }
  2619. /*
  2620. * Find the first task with tgid >= tgid
  2621. *
  2622. */
  2623. struct tgid_iter {
  2624. unsigned int tgid;
  2625. struct task_struct *task;
  2626. };
  2627. static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
  2628. {
  2629. struct pid *pid;
  2630. if (iter.task)
  2631. put_task_struct(iter.task);
  2632. rcu_read_lock();
  2633. retry:
  2634. iter.task = NULL;
  2635. pid = find_ge_pid(iter.tgid, ns);
  2636. if (pid) {
  2637. iter.tgid = pid_nr_ns(pid, ns);
  2638. iter.task = pid_task(pid, PIDTYPE_PID);
  2639. /* What we to know is if the pid we have find is the
  2640. * pid of a thread_group_leader. Testing for task
  2641. * being a thread_group_leader is the obvious thing
  2642. * todo but there is a window when it fails, due to
  2643. * the pid transfer logic in de_thread.
  2644. *
  2645. * So we perform the straight forward test of seeing
  2646. * if the pid we have found is the pid of a thread
  2647. * group leader, and don't worry if the task we have
  2648. * found doesn't happen to be a thread group leader.
  2649. * As we don't care in the case of readdir.
  2650. */
  2651. if (!iter.task || !has_group_leader_pid(iter.task)) {
  2652. iter.tgid += 1;
  2653. goto retry;
  2654. }
  2655. get_task_struct(iter.task);
  2656. }
  2657. rcu_read_unlock();
  2658. return iter;
  2659. }
  2660. #define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2)
  2661. /* for the /proc/ directory itself, after non-process stuff has been done */
  2662. int proc_pid_readdir(struct file *file, struct dir_context *ctx)
  2663. {
  2664. struct tgid_iter iter;
  2665. struct pid_namespace *ns = file_inode(file)->i_sb->s_fs_info;
  2666. loff_t pos = ctx->pos;
  2667. if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
  2668. return 0;
  2669. if (pos == TGID_OFFSET - 2) {
  2670. struct inode *inode = d_inode(ns->proc_self);
  2671. if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
  2672. return 0;
  2673. ctx->pos = pos = pos + 1;
  2674. }
  2675. if (pos == TGID_OFFSET - 1) {
  2676. struct inode *inode = d_inode(ns->proc_thread_self);
  2677. if (!dir_emit(ctx, "thread-self", 11, inode->i_ino, DT_LNK))
  2678. return 0;
  2679. ctx->pos = pos = pos + 1;
  2680. }
  2681. iter.tgid = pos - TGID_OFFSET;
  2682. iter.task = NULL;
  2683. for (iter = next_tgid(ns, iter);
  2684. iter.task;
  2685. iter.tgid += 1, iter = next_tgid(ns, iter)) {
  2686. char name[PROC_NUMBUF];
  2687. int len;
  2688. if (!has_pid_permissions(ns, iter.task, 2))
  2689. continue;
  2690. len = snprintf(name, sizeof(name), "%d", iter.tgid);
  2691. ctx->pos = iter.tgid + TGID_OFFSET;
  2692. if (!proc_fill_cache(file, ctx, name, len,
  2693. proc_pid_instantiate, iter.task, NULL)) {
  2694. put_task_struct(iter.task);
  2695. return 0;
  2696. }
  2697. }
  2698. ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
  2699. return 0;
  2700. }
  2701. /*
  2702. * proc_tid_comm_permission is a special permission function exclusively
  2703. * used for the node /proc/<pid>/task/<tid>/comm.
  2704. * It bypasses generic permission checks in the case where a task of the same
  2705. * task group attempts to access the node.
  2706. * The rationale behind this is that glibc and bionic access this node for
  2707. * cross thread naming (pthread_set/getname_np(!self)). However, if
  2708. * PR_SET_DUMPABLE gets set to 0 this node among others becomes uid=0 gid=0,
  2709. * which locks out the cross thread naming implementation.
  2710. * This function makes sure that the node is always accessible for members of
  2711. * same thread group.
  2712. */
  2713. static int proc_tid_comm_permission(struct inode *inode, int mask)
  2714. {
  2715. bool is_same_tgroup;
  2716. struct task_struct *task;
  2717. task = get_proc_task(inode);
  2718. if (!task)
  2719. return -ESRCH;
  2720. is_same_tgroup = same_thread_group(current, task);
  2721. put_task_struct(task);
  2722. if (likely(is_same_tgroup && !(mask & MAY_EXEC))) {
  2723. /* This file (/proc/<pid>/task/<tid>/comm) can always be
  2724. * read or written by the members of the corresponding
  2725. * thread group.
  2726. */
  2727. return 0;
  2728. }
  2729. return generic_permission(inode, mask);
  2730. }
  2731. static const struct inode_operations proc_tid_comm_inode_operations = {
  2732. .permission = proc_tid_comm_permission,
  2733. };
  2734. /*
  2735. * Tasks
  2736. */
  2737. static const struct pid_entry tid_base_stuff[] = {
  2738. DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
  2739. DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
  2740. DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
  2741. #ifdef CONFIG_NET
  2742. DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
  2743. #endif
  2744. REG("environ", S_IRUSR, proc_environ_operations),
  2745. ONE("auxv", S_IRUSR, proc_pid_auxv),
  2746. ONE("status", S_IRUGO, proc_pid_status),
  2747. ONE("personality", S_IRUSR, proc_pid_personality),
  2748. ONE("limits", S_IRUGO, proc_pid_limits),
  2749. #ifdef CONFIG_SCHED_DEBUG
  2750. REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
  2751. #endif
  2752. NOD("comm", S_IFREG|S_IRUGO|S_IWUSR,
  2753. &proc_tid_comm_inode_operations,
  2754. &proc_pid_set_comm_operations, {}),
  2755. #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
  2756. ONE("syscall", S_IRUSR, proc_pid_syscall),
  2757. #endif
  2758. REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
  2759. ONE("stat", S_IRUGO, proc_tid_stat),
  2760. ONE("statm", S_IRUGO, proc_pid_statm),
  2761. REG("maps", S_IRUGO, proc_tid_maps_operations),
  2762. #ifdef CONFIG_PROC_CHILDREN
  2763. REG("children", S_IRUGO, proc_tid_children_operations),
  2764. #endif
  2765. #ifdef CONFIG_NUMA
  2766. REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations),
  2767. #endif
  2768. REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
  2769. LNK("cwd", proc_cwd_link),
  2770. LNK("root", proc_root_link),
  2771. LNK("exe", proc_exe_link),
  2772. REG("mounts", S_IRUGO, proc_mounts_operations),
  2773. REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
  2774. #ifdef CONFIG_PROC_PAGE_MONITOR
  2775. REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
  2776. REG("smaps", S_IRUGO, proc_tid_smaps_operations),
  2777. REG("pagemap", S_IRUSR, proc_pagemap_operations),
  2778. #endif
  2779. #ifdef CONFIG_SECURITY
  2780. DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
  2781. #endif
  2782. #ifdef CONFIG_KALLSYMS
  2783. ONE("wchan", S_IRUGO, proc_pid_wchan),
  2784. #endif
  2785. #ifdef CONFIG_STACKTRACE
  2786. ONE("stack", S_IRUSR, proc_pid_stack),
  2787. #endif
  2788. #ifdef CONFIG_SCHED_INFO
  2789. ONE("schedstat", S_IRUGO, proc_pid_schedstat),
  2790. #endif
  2791. #ifdef CONFIG_LATENCYTOP
  2792. REG("latency", S_IRUGO, proc_lstats_operations),
  2793. #endif
  2794. #ifdef CONFIG_PROC_PID_CPUSET
  2795. ONE("cpuset", S_IRUGO, proc_cpuset_show),
  2796. #endif
  2797. #ifdef CONFIG_CGROUPS
  2798. ONE("cgroup", S_IRUGO, proc_cgroup_show),
  2799. #endif
  2800. ONE("oom_score", S_IRUGO, proc_oom_score),
  2801. REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
  2802. REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
  2803. #ifdef CONFIG_AUDITSYSCALL
  2804. REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
  2805. REG("sessionid", S_IRUGO, proc_sessionid_operations),
  2806. #endif
  2807. #ifdef CONFIG_FAULT_INJECTION
  2808. REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
  2809. #endif
  2810. #ifdef CONFIG_TASK_IO_ACCOUNTING
  2811. ONE("io", S_IRUSR, proc_tid_io_accounting),
  2812. #endif
  2813. #ifdef CONFIG_HARDWALL
  2814. ONE("hardwall", S_IRUGO, proc_pid_hardwall),
  2815. #endif
  2816. #ifdef CONFIG_USER_NS
  2817. REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
  2818. REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
  2819. REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
  2820. REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
  2821. #endif
  2822. };
  2823. static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
  2824. {
  2825. return proc_pident_readdir(file, ctx,
  2826. tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
  2827. }
  2828. static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
  2829. {
  2830. return proc_pident_lookup(dir, dentry,
  2831. tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
  2832. }
  2833. static const struct file_operations proc_tid_base_operations = {
  2834. .read = generic_read_dir,
  2835. .iterate_shared = proc_tid_base_readdir,
  2836. .llseek = generic_file_llseek,
  2837. };
  2838. static const struct inode_operations proc_tid_base_inode_operations = {
  2839. .lookup = proc_tid_base_lookup,
  2840. .getattr = pid_getattr,
  2841. .setattr = proc_setattr,
  2842. };
  2843. static int proc_task_instantiate(struct inode *dir,
  2844. struct dentry *dentry, struct task_struct *task, const void *ptr)
  2845. {
  2846. struct inode *inode;
  2847. inode = proc_pid_make_inode(dir->i_sb, task);
  2848. if (!inode)
  2849. goto out;
  2850. inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
  2851. inode->i_op = &proc_tid_base_inode_operations;
  2852. inode->i_fop = &proc_tid_base_operations;
  2853. inode->i_flags|=S_IMMUTABLE;
  2854. set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
  2855. ARRAY_SIZE(tid_base_stuff)));
  2856. d_set_d_op(dentry, &pid_dentry_operations);
  2857. d_add(dentry, inode);
  2858. /* Close the race of the process dying before we return the dentry */
  2859. if (pid_revalidate(dentry, 0))
  2860. return 0;
  2861. out:
  2862. return -ENOENT;
  2863. }
  2864. static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
  2865. {
  2866. int result = -ENOENT;
  2867. struct task_struct *task;
  2868. struct task_struct *leader = get_proc_task(dir);
  2869. unsigned tid;
  2870. struct pid_namespace *ns;
  2871. if (!leader)
  2872. goto out_no_task;
  2873. tid = name_to_int(&dentry->d_name);
  2874. if (tid == ~0U)
  2875. goto out;
  2876. ns = dentry->d_sb->s_fs_info;
  2877. rcu_read_lock();
  2878. task = find_task_by_pid_ns(tid, ns);
  2879. if (task)
  2880. get_task_struct(task);
  2881. rcu_read_unlock();
  2882. if (!task)
  2883. goto out;
  2884. if (!same_thread_group(leader, task))
  2885. goto out_drop_task;
  2886. result = proc_task_instantiate(dir, dentry, task, NULL);
  2887. out_drop_task:
  2888. put_task_struct(task);
  2889. out:
  2890. put_task_struct(leader);
  2891. out_no_task:
  2892. return ERR_PTR(result);
  2893. }
  2894. /*
  2895. * Find the first tid of a thread group to return to user space.
  2896. *
  2897. * Usually this is just the thread group leader, but if the users
  2898. * buffer was too small or there was a seek into the middle of the
  2899. * directory we have more work todo.
  2900. *
  2901. * In the case of a short read we start with find_task_by_pid.
  2902. *
  2903. * In the case of a seek we start with the leader and walk nr
  2904. * threads past it.
  2905. */
  2906. static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos,
  2907. struct pid_namespace *ns)
  2908. {
  2909. struct task_struct *pos, *task;
  2910. unsigned long nr = f_pos;
  2911. if (nr != f_pos) /* 32bit overflow? */
  2912. return NULL;
  2913. rcu_read_lock();
  2914. task = pid_task(pid, PIDTYPE_PID);
  2915. if (!task)
  2916. goto fail;
  2917. /* Attempt to start with the tid of a thread */
  2918. if (tid && nr) {
  2919. pos = find_task_by_pid_ns(tid, ns);
  2920. if (pos && same_thread_group(pos, task))
  2921. goto found;
  2922. }
  2923. /* If nr exceeds the number of threads there is nothing todo */
  2924. if (nr >= get_nr_threads(task))
  2925. goto fail;
  2926. /* If we haven't found our starting place yet start
  2927. * with the leader and walk nr threads forward.
  2928. */
  2929. pos = task = task->group_leader;
  2930. do {
  2931. if (!nr--)
  2932. goto found;
  2933. } while_each_thread(task, pos);
  2934. fail:
  2935. pos = NULL;
  2936. goto out;
  2937. found:
  2938. get_task_struct(pos);
  2939. out:
  2940. rcu_read_unlock();
  2941. return pos;
  2942. }
  2943. /*
  2944. * Find the next thread in the thread list.
  2945. * Return NULL if there is an error or no next thread.
  2946. *
  2947. * The reference to the input task_struct is released.
  2948. */
  2949. static struct task_struct *next_tid(struct task_struct *start)
  2950. {
  2951. struct task_struct *pos = NULL;
  2952. rcu_read_lock();
  2953. if (pid_alive(start)) {
  2954. pos = next_thread(start);
  2955. if (thread_group_leader(pos))
  2956. pos = NULL;
  2957. else
  2958. get_task_struct(pos);
  2959. }
  2960. rcu_read_unlock();
  2961. put_task_struct(start);
  2962. return pos;
  2963. }
  2964. /* for the /proc/TGID/task/ directories */
  2965. static int proc_task_readdir(struct file *file, struct dir_context *ctx)
  2966. {
  2967. struct inode *inode = file_inode(file);
  2968. struct task_struct *task;
  2969. struct pid_namespace *ns;
  2970. int tid;
  2971. if (proc_inode_is_dead(inode))
  2972. return -ENOENT;
  2973. if (!dir_emit_dots(file, ctx))
  2974. return 0;
  2975. /* f_version caches the tgid value that the last readdir call couldn't
  2976. * return. lseek aka telldir automagically resets f_version to 0.
  2977. */
  2978. ns = inode->i_sb->s_fs_info;
  2979. tid = (int)file->f_version;
  2980. file->f_version = 0;
  2981. for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns);
  2982. task;
  2983. task = next_tid(task), ctx->pos++) {
  2984. char name[PROC_NUMBUF];
  2985. int len;
  2986. tid = task_pid_nr_ns(task, ns);
  2987. len = snprintf(name, sizeof(name), "%d", tid);
  2988. if (!proc_fill_cache(file, ctx, name, len,
  2989. proc_task_instantiate, task, NULL)) {
  2990. /* returning this tgid failed, save it as the first
  2991. * pid for the next readir call */
  2992. file->f_version = (u64)tid;
  2993. put_task_struct(task);
  2994. break;
  2995. }
  2996. }
  2997. return 0;
  2998. }
  2999. static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
  3000. {
  3001. struct inode *inode = d_inode(dentry);
  3002. struct task_struct *p = get_proc_task(inode);
  3003. generic_fillattr(inode, stat);
  3004. if (p) {
  3005. stat->nlink += get_nr_threads(p);
  3006. put_task_struct(p);
  3007. }
  3008. return 0;
  3009. }
  3010. static const struct inode_operations proc_task_inode_operations = {
  3011. .lookup = proc_task_lookup,
  3012. .getattr = proc_task_getattr,
  3013. .setattr = proc_setattr,
  3014. .permission = proc_pid_permission,
  3015. };
  3016. static const struct file_operations proc_task_operations = {
  3017. .read = generic_read_dir,
  3018. .iterate_shared = proc_task_readdir,
  3019. .llseek = generic_file_llseek,
  3020. };