base.c 84 KB

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