base.c 84 KB

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