base.c 79 KB

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