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