base.c 85 KB

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