base.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437
  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/config.h>
  51. #include <linux/errno.h>
  52. #include <linux/time.h>
  53. #include <linux/proc_fs.h>
  54. #include <linux/stat.h>
  55. #include <linux/init.h>
  56. #include <linux/capability.h>
  57. #include <linux/file.h>
  58. #include <linux/string.h>
  59. #include <linux/seq_file.h>
  60. #include <linux/namei.h>
  61. #include <linux/namespace.h>
  62. #include <linux/mm.h>
  63. #include <linux/smp_lock.h>
  64. #include <linux/rcupdate.h>
  65. #include <linux/kallsyms.h>
  66. #include <linux/mount.h>
  67. #include <linux/security.h>
  68. #include <linux/ptrace.h>
  69. #include <linux/seccomp.h>
  70. #include <linux/cpuset.h>
  71. #include <linux/audit.h>
  72. #include <linux/poll.h>
  73. #include "internal.h"
  74. /* NOTE:
  75. * Implementing inode permission operations in /proc is almost
  76. * certainly an error. Permission checks need to happen during
  77. * each system call not at open time. The reason is that most of
  78. * what we wish to check for permissions in /proc varies at runtime.
  79. *
  80. * The classic example of a problem is opening file descriptors
  81. * in /proc for a task before it execs a suid executable.
  82. */
  83. /*
  84. * For hysterical raisins we keep the same inumbers as in the old procfs.
  85. * Feel free to change the macro below - just keep the range distinct from
  86. * inumbers of the rest of procfs (currently those are in 0x0000--0xffff).
  87. * As soon as we'll get a separate superblock we will be able to forget
  88. * about magical ranges too.
  89. */
  90. #define fake_ino(pid,ino) (((pid)<<16)|(ino))
  91. enum pid_directory_inos {
  92. PROC_TGID_INO = 2,
  93. PROC_TGID_TASK,
  94. PROC_TGID_STATUS,
  95. PROC_TGID_MEM,
  96. #ifdef CONFIG_SECCOMP
  97. PROC_TGID_SECCOMP,
  98. #endif
  99. PROC_TGID_CWD,
  100. PROC_TGID_ROOT,
  101. PROC_TGID_EXE,
  102. PROC_TGID_FD,
  103. PROC_TGID_ENVIRON,
  104. PROC_TGID_AUXV,
  105. PROC_TGID_CMDLINE,
  106. PROC_TGID_STAT,
  107. PROC_TGID_STATM,
  108. PROC_TGID_MAPS,
  109. PROC_TGID_NUMA_MAPS,
  110. PROC_TGID_MOUNTS,
  111. PROC_TGID_MOUNTSTATS,
  112. PROC_TGID_WCHAN,
  113. #ifdef CONFIG_MMU
  114. PROC_TGID_SMAPS,
  115. #endif
  116. #ifdef CONFIG_SCHEDSTATS
  117. PROC_TGID_SCHEDSTAT,
  118. #endif
  119. #ifdef CONFIG_CPUSETS
  120. PROC_TGID_CPUSET,
  121. #endif
  122. #ifdef CONFIG_SECURITY
  123. PROC_TGID_ATTR,
  124. PROC_TGID_ATTR_CURRENT,
  125. PROC_TGID_ATTR_PREV,
  126. PROC_TGID_ATTR_EXEC,
  127. PROC_TGID_ATTR_FSCREATE,
  128. PROC_TGID_ATTR_KEYCREATE,
  129. #endif
  130. #ifdef CONFIG_AUDITSYSCALL
  131. PROC_TGID_LOGINUID,
  132. #endif
  133. PROC_TGID_OOM_SCORE,
  134. PROC_TGID_OOM_ADJUST,
  135. PROC_TID_INO,
  136. PROC_TID_STATUS,
  137. PROC_TID_MEM,
  138. #ifdef CONFIG_SECCOMP
  139. PROC_TID_SECCOMP,
  140. #endif
  141. PROC_TID_CWD,
  142. PROC_TID_ROOT,
  143. PROC_TID_EXE,
  144. PROC_TID_FD,
  145. PROC_TID_ENVIRON,
  146. PROC_TID_AUXV,
  147. PROC_TID_CMDLINE,
  148. PROC_TID_STAT,
  149. PROC_TID_STATM,
  150. PROC_TID_MAPS,
  151. PROC_TID_NUMA_MAPS,
  152. PROC_TID_MOUNTS,
  153. PROC_TID_MOUNTSTATS,
  154. PROC_TID_WCHAN,
  155. #ifdef CONFIG_MMU
  156. PROC_TID_SMAPS,
  157. #endif
  158. #ifdef CONFIG_SCHEDSTATS
  159. PROC_TID_SCHEDSTAT,
  160. #endif
  161. #ifdef CONFIG_CPUSETS
  162. PROC_TID_CPUSET,
  163. #endif
  164. #ifdef CONFIG_SECURITY
  165. PROC_TID_ATTR,
  166. PROC_TID_ATTR_CURRENT,
  167. PROC_TID_ATTR_PREV,
  168. PROC_TID_ATTR_EXEC,
  169. PROC_TID_ATTR_FSCREATE,
  170. PROC_TID_ATTR_KEYCREATE,
  171. #endif
  172. #ifdef CONFIG_AUDITSYSCALL
  173. PROC_TID_LOGINUID,
  174. #endif
  175. PROC_TID_OOM_SCORE,
  176. PROC_TID_OOM_ADJUST,
  177. /* Add new entries before this */
  178. PROC_TID_FD_DIR = 0x8000, /* 0x8000-0xffff */
  179. };
  180. /* Worst case buffer size needed for holding an integer. */
  181. #define PROC_NUMBUF 10
  182. struct pid_entry {
  183. int type;
  184. int len;
  185. char *name;
  186. mode_t mode;
  187. };
  188. #define E(type,name,mode) {(type),sizeof(name)-1,(name),(mode)}
  189. static struct pid_entry tgid_base_stuff[] = {
  190. E(PROC_TGID_TASK, "task", S_IFDIR|S_IRUGO|S_IXUGO),
  191. E(PROC_TGID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR),
  192. E(PROC_TGID_ENVIRON, "environ", S_IFREG|S_IRUSR),
  193. E(PROC_TGID_AUXV, "auxv", S_IFREG|S_IRUSR),
  194. E(PROC_TGID_STATUS, "status", S_IFREG|S_IRUGO),
  195. E(PROC_TGID_CMDLINE, "cmdline", S_IFREG|S_IRUGO),
  196. E(PROC_TGID_STAT, "stat", S_IFREG|S_IRUGO),
  197. E(PROC_TGID_STATM, "statm", S_IFREG|S_IRUGO),
  198. E(PROC_TGID_MAPS, "maps", S_IFREG|S_IRUGO),
  199. #ifdef CONFIG_NUMA
  200. E(PROC_TGID_NUMA_MAPS, "numa_maps", S_IFREG|S_IRUGO),
  201. #endif
  202. E(PROC_TGID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR),
  203. #ifdef CONFIG_SECCOMP
  204. E(PROC_TGID_SECCOMP, "seccomp", S_IFREG|S_IRUSR|S_IWUSR),
  205. #endif
  206. E(PROC_TGID_CWD, "cwd", S_IFLNK|S_IRWXUGO),
  207. E(PROC_TGID_ROOT, "root", S_IFLNK|S_IRWXUGO),
  208. E(PROC_TGID_EXE, "exe", S_IFLNK|S_IRWXUGO),
  209. E(PROC_TGID_MOUNTS, "mounts", S_IFREG|S_IRUGO),
  210. E(PROC_TGID_MOUNTSTATS, "mountstats", S_IFREG|S_IRUSR),
  211. #ifdef CONFIG_MMU
  212. E(PROC_TGID_SMAPS, "smaps", S_IFREG|S_IRUGO),
  213. #endif
  214. #ifdef CONFIG_SECURITY
  215. E(PROC_TGID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO),
  216. #endif
  217. #ifdef CONFIG_KALLSYMS
  218. E(PROC_TGID_WCHAN, "wchan", S_IFREG|S_IRUGO),
  219. #endif
  220. #ifdef CONFIG_SCHEDSTATS
  221. E(PROC_TGID_SCHEDSTAT, "schedstat", S_IFREG|S_IRUGO),
  222. #endif
  223. #ifdef CONFIG_CPUSETS
  224. E(PROC_TGID_CPUSET, "cpuset", S_IFREG|S_IRUGO),
  225. #endif
  226. E(PROC_TGID_OOM_SCORE, "oom_score",S_IFREG|S_IRUGO),
  227. E(PROC_TGID_OOM_ADJUST,"oom_adj", S_IFREG|S_IRUGO|S_IWUSR),
  228. #ifdef CONFIG_AUDITSYSCALL
  229. E(PROC_TGID_LOGINUID, "loginuid", S_IFREG|S_IWUSR|S_IRUGO),
  230. #endif
  231. {0,0,NULL,0}
  232. };
  233. static struct pid_entry tid_base_stuff[] = {
  234. E(PROC_TID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR),
  235. E(PROC_TID_ENVIRON, "environ", S_IFREG|S_IRUSR),
  236. E(PROC_TID_AUXV, "auxv", S_IFREG|S_IRUSR),
  237. E(PROC_TID_STATUS, "status", S_IFREG|S_IRUGO),
  238. E(PROC_TID_CMDLINE, "cmdline", S_IFREG|S_IRUGO),
  239. E(PROC_TID_STAT, "stat", S_IFREG|S_IRUGO),
  240. E(PROC_TID_STATM, "statm", S_IFREG|S_IRUGO),
  241. E(PROC_TID_MAPS, "maps", S_IFREG|S_IRUGO),
  242. #ifdef CONFIG_NUMA
  243. E(PROC_TID_NUMA_MAPS, "numa_maps", S_IFREG|S_IRUGO),
  244. #endif
  245. E(PROC_TID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR),
  246. #ifdef CONFIG_SECCOMP
  247. E(PROC_TID_SECCOMP, "seccomp", S_IFREG|S_IRUSR|S_IWUSR),
  248. #endif
  249. E(PROC_TID_CWD, "cwd", S_IFLNK|S_IRWXUGO),
  250. E(PROC_TID_ROOT, "root", S_IFLNK|S_IRWXUGO),
  251. E(PROC_TID_EXE, "exe", S_IFLNK|S_IRWXUGO),
  252. E(PROC_TID_MOUNTS, "mounts", S_IFREG|S_IRUGO),
  253. #ifdef CONFIG_MMU
  254. E(PROC_TID_SMAPS, "smaps", S_IFREG|S_IRUGO),
  255. #endif
  256. #ifdef CONFIG_SECURITY
  257. E(PROC_TID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO),
  258. #endif
  259. #ifdef CONFIG_KALLSYMS
  260. E(PROC_TID_WCHAN, "wchan", S_IFREG|S_IRUGO),
  261. #endif
  262. #ifdef CONFIG_SCHEDSTATS
  263. E(PROC_TID_SCHEDSTAT, "schedstat",S_IFREG|S_IRUGO),
  264. #endif
  265. #ifdef CONFIG_CPUSETS
  266. E(PROC_TID_CPUSET, "cpuset", S_IFREG|S_IRUGO),
  267. #endif
  268. E(PROC_TID_OOM_SCORE, "oom_score",S_IFREG|S_IRUGO),
  269. E(PROC_TID_OOM_ADJUST, "oom_adj", S_IFREG|S_IRUGO|S_IWUSR),
  270. #ifdef CONFIG_AUDITSYSCALL
  271. E(PROC_TID_LOGINUID, "loginuid", S_IFREG|S_IWUSR|S_IRUGO),
  272. #endif
  273. {0,0,NULL,0}
  274. };
  275. #ifdef CONFIG_SECURITY
  276. static struct pid_entry tgid_attr_stuff[] = {
  277. E(PROC_TGID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO),
  278. E(PROC_TGID_ATTR_PREV, "prev", S_IFREG|S_IRUGO),
  279. E(PROC_TGID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO),
  280. E(PROC_TGID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
  281. E(PROC_TGID_ATTR_KEYCREATE, "keycreate", S_IFREG|S_IRUGO|S_IWUGO),
  282. {0,0,NULL,0}
  283. };
  284. static struct pid_entry tid_attr_stuff[] = {
  285. E(PROC_TID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO),
  286. E(PROC_TID_ATTR_PREV, "prev", S_IFREG|S_IRUGO),
  287. E(PROC_TID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO),
  288. E(PROC_TID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
  289. E(PROC_TID_ATTR_KEYCREATE, "keycreate", S_IFREG|S_IRUGO|S_IWUGO),
  290. {0,0,NULL,0}
  291. };
  292. #endif
  293. #undef E
  294. static int proc_fd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
  295. {
  296. struct task_struct *task = get_proc_task(inode);
  297. struct files_struct *files = NULL;
  298. struct file *file;
  299. int fd = proc_fd(inode);
  300. if (task) {
  301. files = get_files_struct(task);
  302. put_task_struct(task);
  303. }
  304. if (files) {
  305. /*
  306. * We are not taking a ref to the file structure, so we must
  307. * hold ->file_lock.
  308. */
  309. spin_lock(&files->file_lock);
  310. file = fcheck_files(files, fd);
  311. if (file) {
  312. *mnt = mntget(file->f_vfsmnt);
  313. *dentry = dget(file->f_dentry);
  314. spin_unlock(&files->file_lock);
  315. put_files_struct(files);
  316. return 0;
  317. }
  318. spin_unlock(&files->file_lock);
  319. put_files_struct(files);
  320. }
  321. return -ENOENT;
  322. }
  323. static struct fs_struct *get_fs_struct(struct task_struct *task)
  324. {
  325. struct fs_struct *fs;
  326. task_lock(task);
  327. fs = task->fs;
  328. if(fs)
  329. atomic_inc(&fs->count);
  330. task_unlock(task);
  331. return fs;
  332. }
  333. static int get_nr_threads(struct task_struct *tsk)
  334. {
  335. /* Must be called with the rcu_read_lock held */
  336. unsigned long flags;
  337. int count = 0;
  338. if (lock_task_sighand(tsk, &flags)) {
  339. count = atomic_read(&tsk->signal->count);
  340. unlock_task_sighand(tsk, &flags);
  341. }
  342. return count;
  343. }
  344. static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
  345. {
  346. struct task_struct *task = get_proc_task(inode);
  347. struct fs_struct *fs = NULL;
  348. int result = -ENOENT;
  349. if (task) {
  350. fs = get_fs_struct(task);
  351. put_task_struct(task);
  352. }
  353. if (fs) {
  354. read_lock(&fs->lock);
  355. *mnt = mntget(fs->pwdmnt);
  356. *dentry = dget(fs->pwd);
  357. read_unlock(&fs->lock);
  358. result = 0;
  359. put_fs_struct(fs);
  360. }
  361. return result;
  362. }
  363. static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
  364. {
  365. struct task_struct *task = get_proc_task(inode);
  366. struct fs_struct *fs = NULL;
  367. int result = -ENOENT;
  368. if (task) {
  369. fs = get_fs_struct(task);
  370. put_task_struct(task);
  371. }
  372. if (fs) {
  373. read_lock(&fs->lock);
  374. *mnt = mntget(fs->rootmnt);
  375. *dentry = dget(fs->root);
  376. read_unlock(&fs->lock);
  377. result = 0;
  378. put_fs_struct(fs);
  379. }
  380. return result;
  381. }
  382. #define MAY_PTRACE(task) \
  383. (task == current || \
  384. (task->parent == current && \
  385. (task->ptrace & PT_PTRACED) && \
  386. (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
  387. security_ptrace(current,task) == 0))
  388. static int proc_pid_environ(struct task_struct *task, char * buffer)
  389. {
  390. int res = 0;
  391. struct mm_struct *mm = get_task_mm(task);
  392. if (mm) {
  393. unsigned int len = mm->env_end - mm->env_start;
  394. if (len > PAGE_SIZE)
  395. len = PAGE_SIZE;
  396. res = access_process_vm(task, mm->env_start, buffer, len, 0);
  397. if (!ptrace_may_attach(task))
  398. res = -ESRCH;
  399. mmput(mm);
  400. }
  401. return res;
  402. }
  403. static int proc_pid_cmdline(struct task_struct *task, char * buffer)
  404. {
  405. int res = 0;
  406. unsigned int len;
  407. struct mm_struct *mm = get_task_mm(task);
  408. if (!mm)
  409. goto out;
  410. if (!mm->arg_end)
  411. goto out_mm; /* Shh! No looking before we're done */
  412. len = mm->arg_end - mm->arg_start;
  413. if (len > PAGE_SIZE)
  414. len = PAGE_SIZE;
  415. res = access_process_vm(task, mm->arg_start, buffer, len, 0);
  416. // If the nul at the end of args has been overwritten, then
  417. // assume application is using setproctitle(3).
  418. if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
  419. len = strnlen(buffer, res);
  420. if (len < res) {
  421. res = len;
  422. } else {
  423. len = mm->env_end - mm->env_start;
  424. if (len > PAGE_SIZE - res)
  425. len = PAGE_SIZE - res;
  426. res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
  427. res = strnlen(buffer, res);
  428. }
  429. }
  430. out_mm:
  431. mmput(mm);
  432. out:
  433. return res;
  434. }
  435. static int proc_pid_auxv(struct task_struct *task, char *buffer)
  436. {
  437. int res = 0;
  438. struct mm_struct *mm = get_task_mm(task);
  439. if (mm) {
  440. unsigned int nwords = 0;
  441. do
  442. nwords += 2;
  443. while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
  444. res = nwords * sizeof(mm->saved_auxv[0]);
  445. if (res > PAGE_SIZE)
  446. res = PAGE_SIZE;
  447. memcpy(buffer, mm->saved_auxv, res);
  448. mmput(mm);
  449. }
  450. return res;
  451. }
  452. #ifdef CONFIG_KALLSYMS
  453. /*
  454. * Provides a wchan file via kallsyms in a proper one-value-per-file format.
  455. * Returns the resolved symbol. If that fails, simply return the address.
  456. */
  457. static int proc_pid_wchan(struct task_struct *task, char *buffer)
  458. {
  459. char *modname;
  460. const char *sym_name;
  461. unsigned long wchan, size, offset;
  462. char namebuf[KSYM_NAME_LEN+1];
  463. wchan = get_wchan(task);
  464. sym_name = kallsyms_lookup(wchan, &size, &offset, &modname, namebuf);
  465. if (sym_name)
  466. return sprintf(buffer, "%s", sym_name);
  467. return sprintf(buffer, "%lu", wchan);
  468. }
  469. #endif /* CONFIG_KALLSYMS */
  470. #ifdef CONFIG_SCHEDSTATS
  471. /*
  472. * Provides /proc/PID/schedstat
  473. */
  474. static int proc_pid_schedstat(struct task_struct *task, char *buffer)
  475. {
  476. return sprintf(buffer, "%lu %lu %lu\n",
  477. task->sched_info.cpu_time,
  478. task->sched_info.run_delay,
  479. task->sched_info.pcnt);
  480. }
  481. #endif
  482. /* The badness from the OOM killer */
  483. unsigned long badness(struct task_struct *p, unsigned long uptime);
  484. static int proc_oom_score(struct task_struct *task, char *buffer)
  485. {
  486. unsigned long points;
  487. struct timespec uptime;
  488. do_posix_clock_monotonic_gettime(&uptime);
  489. points = badness(task, uptime.tv_sec);
  490. return sprintf(buffer, "%lu\n", points);
  491. }
  492. /************************************************************************/
  493. /* Here the fs part begins */
  494. /************************************************************************/
  495. /* permission checks */
  496. /* If the process being read is separated by chroot from the reading process,
  497. * don't let the reader access the threads.
  498. */
  499. static int proc_check_chroot(struct dentry *de, struct vfsmount *mnt)
  500. {
  501. struct dentry *base;
  502. struct vfsmount *our_vfsmnt;
  503. int res = 0;
  504. read_lock(&current->fs->lock);
  505. our_vfsmnt = mntget(current->fs->rootmnt);
  506. base = dget(current->fs->root);
  507. read_unlock(&current->fs->lock);
  508. spin_lock(&vfsmount_lock);
  509. while (mnt != our_vfsmnt) {
  510. if (mnt == mnt->mnt_parent)
  511. goto out;
  512. de = mnt->mnt_mountpoint;
  513. mnt = mnt->mnt_parent;
  514. }
  515. if (!is_subdir(de, base))
  516. goto out;
  517. spin_unlock(&vfsmount_lock);
  518. exit:
  519. dput(base);
  520. mntput(our_vfsmnt);
  521. return res;
  522. out:
  523. spin_unlock(&vfsmount_lock);
  524. res = -EACCES;
  525. goto exit;
  526. }
  527. extern struct seq_operations mounts_op;
  528. struct proc_mounts {
  529. struct seq_file m;
  530. int event;
  531. };
  532. static int mounts_open(struct inode *inode, struct file *file)
  533. {
  534. struct task_struct *task = get_proc_task(inode);
  535. struct namespace *namespace = NULL;
  536. struct proc_mounts *p;
  537. int ret = -EINVAL;
  538. if (task) {
  539. task_lock(task);
  540. namespace = task->namespace;
  541. if (namespace)
  542. get_namespace(namespace);
  543. task_unlock(task);
  544. put_task_struct(task);
  545. }
  546. if (namespace) {
  547. ret = -ENOMEM;
  548. p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
  549. if (p) {
  550. file->private_data = &p->m;
  551. ret = seq_open(file, &mounts_op);
  552. if (!ret) {
  553. p->m.private = namespace;
  554. p->event = namespace->event;
  555. return 0;
  556. }
  557. kfree(p);
  558. }
  559. put_namespace(namespace);
  560. }
  561. return ret;
  562. }
  563. static int mounts_release(struct inode *inode, struct file *file)
  564. {
  565. struct seq_file *m = file->private_data;
  566. struct namespace *namespace = m->private;
  567. put_namespace(namespace);
  568. return seq_release(inode, file);
  569. }
  570. static unsigned mounts_poll(struct file *file, poll_table *wait)
  571. {
  572. struct proc_mounts *p = file->private_data;
  573. struct namespace *ns = p->m.private;
  574. unsigned res = 0;
  575. poll_wait(file, &ns->poll, wait);
  576. spin_lock(&vfsmount_lock);
  577. if (p->event != ns->event) {
  578. p->event = ns->event;
  579. res = POLLERR;
  580. }
  581. spin_unlock(&vfsmount_lock);
  582. return res;
  583. }
  584. static struct file_operations proc_mounts_operations = {
  585. .open = mounts_open,
  586. .read = seq_read,
  587. .llseek = seq_lseek,
  588. .release = mounts_release,
  589. .poll = mounts_poll,
  590. };
  591. extern struct seq_operations mountstats_op;
  592. static int mountstats_open(struct inode *inode, struct file *file)
  593. {
  594. int ret = seq_open(file, &mountstats_op);
  595. if (!ret) {
  596. struct seq_file *m = file->private_data;
  597. struct namespace *namespace = NULL;
  598. struct task_struct *task = get_proc_task(inode);
  599. if (task) {
  600. task_lock(task);
  601. namespace = task->namespace;
  602. if (namespace)
  603. get_namespace(namespace);
  604. task_unlock(task);
  605. put_task_struct(task);
  606. }
  607. if (namespace)
  608. m->private = namespace;
  609. else {
  610. seq_release(inode, file);
  611. ret = -EINVAL;
  612. }
  613. }
  614. return ret;
  615. }
  616. static struct file_operations proc_mountstats_operations = {
  617. .open = mountstats_open,
  618. .read = seq_read,
  619. .llseek = seq_lseek,
  620. .release = mounts_release,
  621. };
  622. #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
  623. static ssize_t proc_info_read(struct file * file, char __user * buf,
  624. size_t count, loff_t *ppos)
  625. {
  626. struct inode * inode = file->f_dentry->d_inode;
  627. unsigned long page;
  628. ssize_t length;
  629. struct task_struct *task = get_proc_task(inode);
  630. length = -ESRCH;
  631. if (!task)
  632. goto out_no_task;
  633. if (count > PROC_BLOCK_SIZE)
  634. count = PROC_BLOCK_SIZE;
  635. length = -ENOMEM;
  636. if (!(page = __get_free_page(GFP_KERNEL)))
  637. goto out;
  638. length = PROC_I(inode)->op.proc_read(task, (char*)page);
  639. if (length >= 0)
  640. length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
  641. free_page(page);
  642. out:
  643. put_task_struct(task);
  644. out_no_task:
  645. return length;
  646. }
  647. static struct file_operations proc_info_file_operations = {
  648. .read = proc_info_read,
  649. };
  650. static int mem_open(struct inode* inode, struct file* file)
  651. {
  652. file->private_data = (void*)((long)current->self_exec_id);
  653. return 0;
  654. }
  655. static ssize_t mem_read(struct file * file, char __user * buf,
  656. size_t count, loff_t *ppos)
  657. {
  658. struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
  659. char *page;
  660. unsigned long src = *ppos;
  661. int ret = -ESRCH;
  662. struct mm_struct *mm;
  663. if (!task)
  664. goto out_no_task;
  665. if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
  666. goto out;
  667. ret = -ENOMEM;
  668. page = (char *)__get_free_page(GFP_USER);
  669. if (!page)
  670. goto out;
  671. ret = 0;
  672. mm = get_task_mm(task);
  673. if (!mm)
  674. goto out_free;
  675. ret = -EIO;
  676. if (file->private_data != (void*)((long)current->self_exec_id))
  677. goto out_put;
  678. ret = 0;
  679. while (count > 0) {
  680. int this_len, retval;
  681. this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
  682. retval = access_process_vm(task, src, page, this_len, 0);
  683. if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
  684. if (!ret)
  685. ret = -EIO;
  686. break;
  687. }
  688. if (copy_to_user(buf, page, retval)) {
  689. ret = -EFAULT;
  690. break;
  691. }
  692. ret += retval;
  693. src += retval;
  694. buf += retval;
  695. count -= retval;
  696. }
  697. *ppos = src;
  698. out_put:
  699. mmput(mm);
  700. out_free:
  701. free_page((unsigned long) page);
  702. out:
  703. put_task_struct(task);
  704. out_no_task:
  705. return ret;
  706. }
  707. #define mem_write NULL
  708. #ifndef mem_write
  709. /* This is a security hazard */
  710. static ssize_t mem_write(struct file * file, const char * buf,
  711. size_t count, loff_t *ppos)
  712. {
  713. int copied = 0;
  714. char *page;
  715. struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
  716. unsigned long dst = *ppos;
  717. copied = -ESRCH;
  718. if (!task)
  719. goto out_no_task;
  720. if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
  721. goto out;
  722. copied = -ENOMEM;
  723. page = (char *)__get_free_page(GFP_USER);
  724. if (!page)
  725. goto out;
  726. while (count > 0) {
  727. int this_len, retval;
  728. this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
  729. if (copy_from_user(page, buf, this_len)) {
  730. copied = -EFAULT;
  731. break;
  732. }
  733. retval = access_process_vm(task, dst, page, this_len, 1);
  734. if (!retval) {
  735. if (!copied)
  736. copied = -EIO;
  737. break;
  738. }
  739. copied += retval;
  740. buf += retval;
  741. dst += retval;
  742. count -= retval;
  743. }
  744. *ppos = dst;
  745. free_page((unsigned long) page);
  746. out:
  747. put_task_struct(task);
  748. out_no_task:
  749. return copied;
  750. }
  751. #endif
  752. static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
  753. {
  754. switch (orig) {
  755. case 0:
  756. file->f_pos = offset;
  757. break;
  758. case 1:
  759. file->f_pos += offset;
  760. break;
  761. default:
  762. return -EINVAL;
  763. }
  764. force_successful_syscall_return();
  765. return file->f_pos;
  766. }
  767. static struct file_operations proc_mem_operations = {
  768. .llseek = mem_lseek,
  769. .read = mem_read,
  770. .write = mem_write,
  771. .open = mem_open,
  772. };
  773. static ssize_t oom_adjust_read(struct file *file, char __user *buf,
  774. size_t count, loff_t *ppos)
  775. {
  776. struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
  777. char buffer[PROC_NUMBUF];
  778. size_t len;
  779. int oom_adjust;
  780. loff_t __ppos = *ppos;
  781. if (!task)
  782. return -ESRCH;
  783. oom_adjust = task->oomkilladj;
  784. put_task_struct(task);
  785. len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
  786. if (__ppos >= len)
  787. return 0;
  788. if (count > len-__ppos)
  789. count = len-__ppos;
  790. if (copy_to_user(buf, buffer + __ppos, count))
  791. return -EFAULT;
  792. *ppos = __ppos + count;
  793. return count;
  794. }
  795. static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
  796. size_t count, loff_t *ppos)
  797. {
  798. struct task_struct *task;
  799. char buffer[PROC_NUMBUF], *end;
  800. int oom_adjust;
  801. if (!capable(CAP_SYS_RESOURCE))
  802. return -EPERM;
  803. memset(buffer, 0, sizeof(buffer));
  804. if (count > sizeof(buffer) - 1)
  805. count = sizeof(buffer) - 1;
  806. if (copy_from_user(buffer, buf, count))
  807. return -EFAULT;
  808. oom_adjust = simple_strtol(buffer, &end, 0);
  809. if ((oom_adjust < -16 || oom_adjust > 15) && oom_adjust != OOM_DISABLE)
  810. return -EINVAL;
  811. if (*end == '\n')
  812. end++;
  813. task = get_proc_task(file->f_dentry->d_inode);
  814. if (!task)
  815. return -ESRCH;
  816. task->oomkilladj = oom_adjust;
  817. put_task_struct(task);
  818. if (end - buffer == 0)
  819. return -EIO;
  820. return end - buffer;
  821. }
  822. static struct file_operations proc_oom_adjust_operations = {
  823. .read = oom_adjust_read,
  824. .write = oom_adjust_write,
  825. };
  826. #ifdef CONFIG_AUDITSYSCALL
  827. #define TMPBUFLEN 21
  828. static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
  829. size_t count, loff_t *ppos)
  830. {
  831. struct inode * inode = file->f_dentry->d_inode;
  832. struct task_struct *task = get_proc_task(inode);
  833. ssize_t length;
  834. char tmpbuf[TMPBUFLEN];
  835. if (!task)
  836. return -ESRCH;
  837. length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
  838. audit_get_loginuid(task->audit_context));
  839. put_task_struct(task);
  840. return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
  841. }
  842. static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
  843. size_t count, loff_t *ppos)
  844. {
  845. struct inode * inode = file->f_dentry->d_inode;
  846. char *page, *tmp;
  847. ssize_t length;
  848. uid_t loginuid;
  849. if (!capable(CAP_AUDIT_CONTROL))
  850. return -EPERM;
  851. if (current != proc_tref(inode)->task)
  852. return -EPERM;
  853. if (count >= PAGE_SIZE)
  854. count = PAGE_SIZE - 1;
  855. if (*ppos != 0) {
  856. /* No partial writes. */
  857. return -EINVAL;
  858. }
  859. page = (char*)__get_free_page(GFP_USER);
  860. if (!page)
  861. return -ENOMEM;
  862. length = -EFAULT;
  863. if (copy_from_user(page, buf, count))
  864. goto out_free_page;
  865. page[count] = '\0';
  866. loginuid = simple_strtoul(page, &tmp, 10);
  867. if (tmp == page) {
  868. length = -EINVAL;
  869. goto out_free_page;
  870. }
  871. length = audit_set_loginuid(current, loginuid);
  872. if (likely(length == 0))
  873. length = count;
  874. out_free_page:
  875. free_page((unsigned long) page);
  876. return length;
  877. }
  878. static struct file_operations proc_loginuid_operations = {
  879. .read = proc_loginuid_read,
  880. .write = proc_loginuid_write,
  881. };
  882. #endif
  883. #ifdef CONFIG_SECCOMP
  884. static ssize_t seccomp_read(struct file *file, char __user *buf,
  885. size_t count, loff_t *ppos)
  886. {
  887. struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
  888. char __buf[20];
  889. loff_t __ppos = *ppos;
  890. size_t len;
  891. if (!tsk)
  892. return -ESRCH;
  893. /* no need to print the trailing zero, so use only len */
  894. len = sprintf(__buf, "%u\n", tsk->seccomp.mode);
  895. put_task_struct(tsk);
  896. if (__ppos >= len)
  897. return 0;
  898. if (count > len - __ppos)
  899. count = len - __ppos;
  900. if (copy_to_user(buf, __buf + __ppos, count))
  901. return -EFAULT;
  902. *ppos = __ppos + count;
  903. return count;
  904. }
  905. static ssize_t seccomp_write(struct file *file, const char __user *buf,
  906. size_t count, loff_t *ppos)
  907. {
  908. struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
  909. char __buf[20], *end;
  910. unsigned int seccomp_mode;
  911. ssize_t result;
  912. result = -ESRCH;
  913. if (!tsk)
  914. goto out_no_task;
  915. /* can set it only once to be even more secure */
  916. result = -EPERM;
  917. if (unlikely(tsk->seccomp.mode))
  918. goto out;
  919. result = -EFAULT;
  920. memset(__buf, 0, sizeof(__buf));
  921. count = min(count, sizeof(__buf) - 1);
  922. if (copy_from_user(__buf, buf, count))
  923. goto out;
  924. seccomp_mode = simple_strtoul(__buf, &end, 0);
  925. if (*end == '\n')
  926. end++;
  927. result = -EINVAL;
  928. if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) {
  929. tsk->seccomp.mode = seccomp_mode;
  930. set_tsk_thread_flag(tsk, TIF_SECCOMP);
  931. } else
  932. goto out;
  933. result = -EIO;
  934. if (unlikely(!(end - __buf)))
  935. goto out;
  936. result = end - __buf;
  937. out:
  938. put_task_struct(tsk);
  939. out_no_task:
  940. return result;
  941. }
  942. static struct file_operations proc_seccomp_operations = {
  943. .read = seccomp_read,
  944. .write = seccomp_write,
  945. };
  946. #endif /* CONFIG_SECCOMP */
  947. static int proc_check_dentry_visible(struct inode *inode,
  948. struct dentry *dentry, struct vfsmount *mnt)
  949. {
  950. /* Verify that the current process can already see the
  951. * file pointed at by the file descriptor.
  952. * This prevents /proc from being an accidental information leak.
  953. *
  954. * This prevents access to files that are not visible do to
  955. * being on the otherside of a chroot, in a different
  956. * namespace, or are simply process local (like pipes).
  957. */
  958. struct task_struct *task;
  959. struct files_struct *task_files, *files;
  960. int error = -EACCES;
  961. /* See if the the two tasks share a commone set of
  962. * file descriptors. If so everything is visible.
  963. */
  964. task = get_proc_task(inode);
  965. if (!task)
  966. goto out;
  967. files = get_files_struct(current);
  968. task_files = get_files_struct(task);
  969. if (files && task_files && (files == task_files))
  970. error = 0;
  971. if (task_files)
  972. put_files_struct(task_files);
  973. if (files)
  974. put_files_struct(files);
  975. put_task_struct(task);
  976. if (!error)
  977. goto out;
  978. /* If the two tasks don't share a common set of file
  979. * descriptors see if the destination dentry is already
  980. * visible in the current tasks filesystem namespace.
  981. */
  982. error = proc_check_chroot(dentry, mnt);
  983. out:
  984. return error;
  985. }
  986. static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
  987. {
  988. struct inode *inode = dentry->d_inode;
  989. int error = -EACCES;
  990. /* We don't need a base pointer in the /proc filesystem */
  991. path_release(nd);
  992. if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
  993. goto out;
  994. error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
  995. nd->last_type = LAST_BIND;
  996. if (error)
  997. goto out;
  998. /* Only return files this task can already see */
  999. error = proc_check_dentry_visible(inode, nd->dentry, nd->mnt);
  1000. if (error)
  1001. path_release(nd);
  1002. out:
  1003. return ERR_PTR(error);
  1004. }
  1005. static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
  1006. char __user *buffer, int buflen)
  1007. {
  1008. struct inode * inode;
  1009. char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
  1010. int len;
  1011. if (!tmp)
  1012. return -ENOMEM;
  1013. inode = dentry->d_inode;
  1014. path = d_path(dentry, mnt, tmp, PAGE_SIZE);
  1015. len = PTR_ERR(path);
  1016. if (IS_ERR(path))
  1017. goto out;
  1018. len = tmp + PAGE_SIZE - 1 - path;
  1019. if (len > buflen)
  1020. len = buflen;
  1021. if (copy_to_user(buffer, path, len))
  1022. len = -EFAULT;
  1023. out:
  1024. free_page((unsigned long)tmp);
  1025. return len;
  1026. }
  1027. static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
  1028. {
  1029. int error = -EACCES;
  1030. struct inode *inode = dentry->d_inode;
  1031. struct dentry *de;
  1032. struct vfsmount *mnt = NULL;
  1033. if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
  1034. goto out;
  1035. error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
  1036. if (error)
  1037. goto out;
  1038. /* Only return files this task can already see */
  1039. error = proc_check_dentry_visible(inode, de, mnt);
  1040. if (error)
  1041. goto out_put;
  1042. error = do_proc_readlink(de, mnt, buffer, buflen);
  1043. out_put:
  1044. dput(de);
  1045. mntput(mnt);
  1046. out:
  1047. return error;
  1048. }
  1049. static struct inode_operations proc_pid_link_inode_operations = {
  1050. .readlink = proc_pid_readlink,
  1051. .follow_link = proc_pid_follow_link
  1052. };
  1053. static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
  1054. {
  1055. struct dentry *dentry = filp->f_dentry;
  1056. struct inode *inode = dentry->d_inode;
  1057. struct task_struct *p = get_proc_task(inode);
  1058. unsigned int fd, tid, ino;
  1059. int retval;
  1060. char buf[PROC_NUMBUF];
  1061. struct files_struct * files;
  1062. struct fdtable *fdt;
  1063. retval = -ENOENT;
  1064. if (!p)
  1065. goto out_no_task;
  1066. retval = 0;
  1067. tid = p->pid;
  1068. fd = filp->f_pos;
  1069. switch (fd) {
  1070. case 0:
  1071. if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
  1072. goto out;
  1073. filp->f_pos++;
  1074. case 1:
  1075. ino = parent_ino(dentry);
  1076. if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
  1077. goto out;
  1078. filp->f_pos++;
  1079. default:
  1080. files = get_files_struct(p);
  1081. if (!files)
  1082. goto out;
  1083. rcu_read_lock();
  1084. fdt = files_fdtable(files);
  1085. for (fd = filp->f_pos-2;
  1086. fd < fdt->max_fds;
  1087. fd++, filp->f_pos++) {
  1088. unsigned int i,j;
  1089. if (!fcheck_files(files, fd))
  1090. continue;
  1091. rcu_read_unlock();
  1092. j = PROC_NUMBUF;
  1093. i = fd;
  1094. do {
  1095. j--;
  1096. buf[j] = '0' + (i % 10);
  1097. i /= 10;
  1098. } while (i);
  1099. ino = fake_ino(tid, PROC_TID_FD_DIR + fd);
  1100. if (filldir(dirent, buf+j, PROC_NUMBUF-j, fd+2, ino, DT_LNK) < 0) {
  1101. rcu_read_lock();
  1102. break;
  1103. }
  1104. rcu_read_lock();
  1105. }
  1106. rcu_read_unlock();
  1107. put_files_struct(files);
  1108. }
  1109. out:
  1110. put_task_struct(p);
  1111. out_no_task:
  1112. return retval;
  1113. }
  1114. static int proc_pident_readdir(struct file *filp,
  1115. void *dirent, filldir_t filldir,
  1116. struct pid_entry *ents, unsigned int nents)
  1117. {
  1118. int i;
  1119. int pid;
  1120. struct dentry *dentry = filp->f_dentry;
  1121. struct inode *inode = dentry->d_inode;
  1122. struct task_struct *task = get_proc_task(inode);
  1123. struct pid_entry *p;
  1124. ino_t ino;
  1125. int ret;
  1126. ret = -ENOENT;
  1127. if (!task)
  1128. goto out;
  1129. ret = 0;
  1130. pid = task->pid;
  1131. put_task_struct(task);
  1132. i = filp->f_pos;
  1133. switch (i) {
  1134. case 0:
  1135. ino = inode->i_ino;
  1136. if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
  1137. goto out;
  1138. i++;
  1139. filp->f_pos++;
  1140. /* fall through */
  1141. case 1:
  1142. ino = parent_ino(dentry);
  1143. if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
  1144. goto out;
  1145. i++;
  1146. filp->f_pos++;
  1147. /* fall through */
  1148. default:
  1149. i -= 2;
  1150. if (i >= nents) {
  1151. ret = 1;
  1152. goto out;
  1153. }
  1154. p = ents + i;
  1155. while (p->name) {
  1156. if (filldir(dirent, p->name, p->len, filp->f_pos,
  1157. fake_ino(pid, p->type), p->mode >> 12) < 0)
  1158. goto out;
  1159. filp->f_pos++;
  1160. p++;
  1161. }
  1162. }
  1163. ret = 1;
  1164. out:
  1165. return ret;
  1166. }
  1167. static int proc_tgid_base_readdir(struct file * filp,
  1168. void * dirent, filldir_t filldir)
  1169. {
  1170. return proc_pident_readdir(filp,dirent,filldir,
  1171. tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
  1172. }
  1173. static int proc_tid_base_readdir(struct file * filp,
  1174. void * dirent, filldir_t filldir)
  1175. {
  1176. return proc_pident_readdir(filp,dirent,filldir,
  1177. tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
  1178. }
  1179. /* building an inode */
  1180. static int task_dumpable(struct task_struct *task)
  1181. {
  1182. int dumpable = 0;
  1183. struct mm_struct *mm;
  1184. task_lock(task);
  1185. mm = task->mm;
  1186. if (mm)
  1187. dumpable = mm->dumpable;
  1188. task_unlock(task);
  1189. if(dumpable == 1)
  1190. return 1;
  1191. return 0;
  1192. }
  1193. static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task, int ino)
  1194. {
  1195. struct inode * inode;
  1196. struct proc_inode *ei;
  1197. /* We need a new inode */
  1198. inode = new_inode(sb);
  1199. if (!inode)
  1200. goto out;
  1201. /* Common stuff */
  1202. ei = PROC_I(inode);
  1203. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  1204. inode->i_ino = fake_ino(task->pid, ino);
  1205. /*
  1206. * grab the reference to task.
  1207. */
  1208. ei->tref = tref_get_by_task(task);
  1209. if (!tref_task(ei->tref))
  1210. goto out_unlock;
  1211. inode->i_uid = 0;
  1212. inode->i_gid = 0;
  1213. if (task_dumpable(task)) {
  1214. inode->i_uid = task->euid;
  1215. inode->i_gid = task->egid;
  1216. }
  1217. security_task_to_inode(task, inode);
  1218. out:
  1219. return inode;
  1220. out_unlock:
  1221. iput(inode);
  1222. return NULL;
  1223. }
  1224. /* dentry stuff */
  1225. /*
  1226. * Exceptional case: normally we are not allowed to unhash a busy
  1227. * directory. In this case, however, we can do it - no aliasing problems
  1228. * due to the way we treat inodes.
  1229. *
  1230. * Rewrite the inode's ownerships here because the owning task may have
  1231. * performed a setuid(), etc.
  1232. *
  1233. * Before the /proc/pid/status file was created the only way to read
  1234. * the effective uid of a /process was to stat /proc/pid. Reading
  1235. * /proc/pid/status is slow enough that procps and other packages
  1236. * kept stating /proc/pid. To keep the rules in /proc simple I have
  1237. * made this apply to all per process world readable and executable
  1238. * directories.
  1239. */
  1240. static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
  1241. {
  1242. struct inode *inode = dentry->d_inode;
  1243. struct task_struct *task = get_proc_task(inode);
  1244. if (task) {
  1245. if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
  1246. task_dumpable(task)) {
  1247. inode->i_uid = task->euid;
  1248. inode->i_gid = task->egid;
  1249. } else {
  1250. inode->i_uid = 0;
  1251. inode->i_gid = 0;
  1252. }
  1253. security_task_to_inode(task, inode);
  1254. put_task_struct(task);
  1255. return 1;
  1256. }
  1257. d_drop(dentry);
  1258. return 0;
  1259. }
  1260. static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
  1261. {
  1262. struct inode *inode = dentry->d_inode;
  1263. struct task_struct *task;
  1264. generic_fillattr(inode, stat);
  1265. rcu_read_lock();
  1266. stat->uid = 0;
  1267. stat->gid = 0;
  1268. task = pid_task(proc_pid(inode), PIDTYPE_PID);
  1269. if (task) {
  1270. if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
  1271. task_dumpable(task)) {
  1272. stat->uid = task->euid;
  1273. stat->gid = task->egid;
  1274. }
  1275. }
  1276. rcu_read_unlock();
  1277. return 0;
  1278. }
  1279. static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
  1280. {
  1281. struct inode *inode = dentry->d_inode;
  1282. struct task_struct *task = get_proc_task(inode);
  1283. int fd = proc_fd(inode);
  1284. struct files_struct *files;
  1285. if (task) {
  1286. files = get_files_struct(task);
  1287. if (files) {
  1288. rcu_read_lock();
  1289. if (fcheck_files(files, fd)) {
  1290. rcu_read_unlock();
  1291. put_files_struct(files);
  1292. if (task_dumpable(task)) {
  1293. inode->i_uid = task->euid;
  1294. inode->i_gid = task->egid;
  1295. } else {
  1296. inode->i_uid = 0;
  1297. inode->i_gid = 0;
  1298. }
  1299. security_task_to_inode(task, inode);
  1300. put_task_struct(task);
  1301. return 1;
  1302. }
  1303. rcu_read_unlock();
  1304. put_files_struct(files);
  1305. }
  1306. put_task_struct(task);
  1307. }
  1308. d_drop(dentry);
  1309. return 0;
  1310. }
  1311. static int pid_delete_dentry(struct dentry * dentry)
  1312. {
  1313. /* Is the task we represent dead?
  1314. * If so, then don't put the dentry on the lru list,
  1315. * kill it immediately.
  1316. */
  1317. return !proc_tref(dentry->d_inode)->task;
  1318. }
  1319. static struct dentry_operations tid_fd_dentry_operations =
  1320. {
  1321. .d_revalidate = tid_fd_revalidate,
  1322. .d_delete = pid_delete_dentry,
  1323. };
  1324. static struct dentry_operations pid_dentry_operations =
  1325. {
  1326. .d_revalidate = pid_revalidate,
  1327. .d_delete = pid_delete_dentry,
  1328. };
  1329. /* Lookups */
  1330. static unsigned name_to_int(struct dentry *dentry)
  1331. {
  1332. const char *name = dentry->d_name.name;
  1333. int len = dentry->d_name.len;
  1334. unsigned n = 0;
  1335. if (len > 1 && *name == '0')
  1336. goto out;
  1337. while (len-- > 0) {
  1338. unsigned c = *name++ - '0';
  1339. if (c > 9)
  1340. goto out;
  1341. if (n >= (~0U-9)/10)
  1342. goto out;
  1343. n *= 10;
  1344. n += c;
  1345. }
  1346. return n;
  1347. out:
  1348. return ~0U;
  1349. }
  1350. /* SMP-safe */
  1351. static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
  1352. {
  1353. struct task_struct *task = get_proc_task(dir);
  1354. unsigned fd = name_to_int(dentry);
  1355. struct dentry *result = ERR_PTR(-ENOENT);
  1356. struct file * file;
  1357. struct files_struct * files;
  1358. struct inode *inode;
  1359. struct proc_inode *ei;
  1360. if (!task)
  1361. goto out_no_task;
  1362. if (fd == ~0U)
  1363. goto out;
  1364. inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_FD_DIR+fd);
  1365. if (!inode)
  1366. goto out;
  1367. ei = PROC_I(inode);
  1368. ei->fd = fd;
  1369. files = get_files_struct(task);
  1370. if (!files)
  1371. goto out_unlock;
  1372. inode->i_mode = S_IFLNK;
  1373. /*
  1374. * We are not taking a ref to the file structure, so we must
  1375. * hold ->file_lock.
  1376. */
  1377. spin_lock(&files->file_lock);
  1378. file = fcheck_files(files, fd);
  1379. if (!file)
  1380. goto out_unlock2;
  1381. if (file->f_mode & 1)
  1382. inode->i_mode |= S_IRUSR | S_IXUSR;
  1383. if (file->f_mode & 2)
  1384. inode->i_mode |= S_IWUSR | S_IXUSR;
  1385. spin_unlock(&files->file_lock);
  1386. put_files_struct(files);
  1387. inode->i_op = &proc_pid_link_inode_operations;
  1388. inode->i_size = 64;
  1389. ei->op.proc_get_link = proc_fd_link;
  1390. dentry->d_op = &tid_fd_dentry_operations;
  1391. d_add(dentry, inode);
  1392. /* Close the race of the process dying before we return the dentry */
  1393. if (tid_fd_revalidate(dentry, NULL))
  1394. result = NULL;
  1395. out:
  1396. put_task_struct(task);
  1397. out_no_task:
  1398. return result;
  1399. out_unlock2:
  1400. spin_unlock(&files->file_lock);
  1401. put_files_struct(files);
  1402. out_unlock:
  1403. iput(inode);
  1404. goto out;
  1405. }
  1406. static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir);
  1407. static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd);
  1408. static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat);
  1409. static struct file_operations proc_fd_operations = {
  1410. .read = generic_read_dir,
  1411. .readdir = proc_readfd,
  1412. };
  1413. static struct file_operations proc_task_operations = {
  1414. .read = generic_read_dir,
  1415. .readdir = proc_task_readdir,
  1416. };
  1417. /*
  1418. * proc directories can do almost nothing..
  1419. */
  1420. static struct inode_operations proc_fd_inode_operations = {
  1421. .lookup = proc_lookupfd,
  1422. };
  1423. static struct inode_operations proc_task_inode_operations = {
  1424. .lookup = proc_task_lookup,
  1425. .getattr = proc_task_getattr,
  1426. };
  1427. #ifdef CONFIG_SECURITY
  1428. static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
  1429. size_t count, loff_t *ppos)
  1430. {
  1431. struct inode * inode = file->f_dentry->d_inode;
  1432. unsigned long page;
  1433. ssize_t length;
  1434. struct task_struct *task = get_proc_task(inode);
  1435. length = -ESRCH;
  1436. if (!task)
  1437. goto out_no_task;
  1438. if (count > PAGE_SIZE)
  1439. count = PAGE_SIZE;
  1440. length = -ENOMEM;
  1441. if (!(page = __get_free_page(GFP_KERNEL)))
  1442. goto out;
  1443. length = security_getprocattr(task,
  1444. (char*)file->f_dentry->d_name.name,
  1445. (void*)page, count);
  1446. if (length >= 0)
  1447. length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
  1448. free_page(page);
  1449. out:
  1450. put_task_struct(task);
  1451. out_no_task:
  1452. return length;
  1453. }
  1454. static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
  1455. size_t count, loff_t *ppos)
  1456. {
  1457. struct inode * inode = file->f_dentry->d_inode;
  1458. char *page;
  1459. ssize_t length;
  1460. struct task_struct *task = get_proc_task(inode);
  1461. length = -ESRCH;
  1462. if (!task)
  1463. goto out_no_task;
  1464. if (count > PAGE_SIZE)
  1465. count = PAGE_SIZE;
  1466. /* No partial writes. */
  1467. length = -EINVAL;
  1468. if (*ppos != 0)
  1469. goto out;
  1470. length = -ENOMEM;
  1471. page = (char*)__get_free_page(GFP_USER);
  1472. if (!page)
  1473. goto out;
  1474. length = -EFAULT;
  1475. if (copy_from_user(page, buf, count))
  1476. goto out_free;
  1477. length = security_setprocattr(task,
  1478. (char*)file->f_dentry->d_name.name,
  1479. (void*)page, count);
  1480. out_free:
  1481. free_page((unsigned long) page);
  1482. out:
  1483. put_task_struct(task);
  1484. out_no_task:
  1485. return length;
  1486. }
  1487. static struct file_operations proc_pid_attr_operations = {
  1488. .read = proc_pid_attr_read,
  1489. .write = proc_pid_attr_write,
  1490. };
  1491. static struct file_operations proc_tid_attr_operations;
  1492. static struct inode_operations proc_tid_attr_inode_operations;
  1493. static struct file_operations proc_tgid_attr_operations;
  1494. static struct inode_operations proc_tgid_attr_inode_operations;
  1495. #endif
  1496. /* SMP-safe */
  1497. static struct dentry *proc_pident_lookup(struct inode *dir,
  1498. struct dentry *dentry,
  1499. struct pid_entry *ents)
  1500. {
  1501. struct inode *inode;
  1502. struct dentry *error;
  1503. struct task_struct *task = get_proc_task(dir);
  1504. struct pid_entry *p;
  1505. struct proc_inode *ei;
  1506. error = ERR_PTR(-ENOENT);
  1507. inode = NULL;
  1508. if (!task)
  1509. goto out_no_task;
  1510. for (p = ents; p->name; p++) {
  1511. if (p->len != dentry->d_name.len)
  1512. continue;
  1513. if (!memcmp(dentry->d_name.name, p->name, p->len))
  1514. break;
  1515. }
  1516. if (!p->name)
  1517. goto out;
  1518. error = ERR_PTR(-EINVAL);
  1519. inode = proc_pid_make_inode(dir->i_sb, task, p->type);
  1520. if (!inode)
  1521. goto out;
  1522. ei = PROC_I(inode);
  1523. inode->i_mode = p->mode;
  1524. /*
  1525. * Yes, it does not scale. And it should not. Don't add
  1526. * new entries into /proc/<tgid>/ without very good reasons.
  1527. */
  1528. switch(p->type) {
  1529. case PROC_TGID_TASK:
  1530. inode->i_nlink = 2;
  1531. inode->i_op = &proc_task_inode_operations;
  1532. inode->i_fop = &proc_task_operations;
  1533. break;
  1534. case PROC_TID_FD:
  1535. case PROC_TGID_FD:
  1536. inode->i_nlink = 2;
  1537. inode->i_op = &proc_fd_inode_operations;
  1538. inode->i_fop = &proc_fd_operations;
  1539. break;
  1540. case PROC_TID_EXE:
  1541. case PROC_TGID_EXE:
  1542. inode->i_op = &proc_pid_link_inode_operations;
  1543. ei->op.proc_get_link = proc_exe_link;
  1544. break;
  1545. case PROC_TID_CWD:
  1546. case PROC_TGID_CWD:
  1547. inode->i_op = &proc_pid_link_inode_operations;
  1548. ei->op.proc_get_link = proc_cwd_link;
  1549. break;
  1550. case PROC_TID_ROOT:
  1551. case PROC_TGID_ROOT:
  1552. inode->i_op = &proc_pid_link_inode_operations;
  1553. ei->op.proc_get_link = proc_root_link;
  1554. break;
  1555. case PROC_TID_ENVIRON:
  1556. case PROC_TGID_ENVIRON:
  1557. inode->i_fop = &proc_info_file_operations;
  1558. ei->op.proc_read = proc_pid_environ;
  1559. break;
  1560. case PROC_TID_AUXV:
  1561. case PROC_TGID_AUXV:
  1562. inode->i_fop = &proc_info_file_operations;
  1563. ei->op.proc_read = proc_pid_auxv;
  1564. break;
  1565. case PROC_TID_STATUS:
  1566. case PROC_TGID_STATUS:
  1567. inode->i_fop = &proc_info_file_operations;
  1568. ei->op.proc_read = proc_pid_status;
  1569. break;
  1570. case PROC_TID_STAT:
  1571. inode->i_fop = &proc_info_file_operations;
  1572. ei->op.proc_read = proc_tid_stat;
  1573. break;
  1574. case PROC_TGID_STAT:
  1575. inode->i_fop = &proc_info_file_operations;
  1576. ei->op.proc_read = proc_tgid_stat;
  1577. break;
  1578. case PROC_TID_CMDLINE:
  1579. case PROC_TGID_CMDLINE:
  1580. inode->i_fop = &proc_info_file_operations;
  1581. ei->op.proc_read = proc_pid_cmdline;
  1582. break;
  1583. case PROC_TID_STATM:
  1584. case PROC_TGID_STATM:
  1585. inode->i_fop = &proc_info_file_operations;
  1586. ei->op.proc_read = proc_pid_statm;
  1587. break;
  1588. case PROC_TID_MAPS:
  1589. case PROC_TGID_MAPS:
  1590. inode->i_fop = &proc_maps_operations;
  1591. break;
  1592. #ifdef CONFIG_NUMA
  1593. case PROC_TID_NUMA_MAPS:
  1594. case PROC_TGID_NUMA_MAPS:
  1595. inode->i_fop = &proc_numa_maps_operations;
  1596. break;
  1597. #endif
  1598. case PROC_TID_MEM:
  1599. case PROC_TGID_MEM:
  1600. inode->i_fop = &proc_mem_operations;
  1601. break;
  1602. #ifdef CONFIG_SECCOMP
  1603. case PROC_TID_SECCOMP:
  1604. case PROC_TGID_SECCOMP:
  1605. inode->i_fop = &proc_seccomp_operations;
  1606. break;
  1607. #endif /* CONFIG_SECCOMP */
  1608. case PROC_TID_MOUNTS:
  1609. case PROC_TGID_MOUNTS:
  1610. inode->i_fop = &proc_mounts_operations;
  1611. break;
  1612. #ifdef CONFIG_MMU
  1613. case PROC_TID_SMAPS:
  1614. case PROC_TGID_SMAPS:
  1615. inode->i_fop = &proc_smaps_operations;
  1616. break;
  1617. #endif
  1618. case PROC_TID_MOUNTSTATS:
  1619. case PROC_TGID_MOUNTSTATS:
  1620. inode->i_fop = &proc_mountstats_operations;
  1621. break;
  1622. #ifdef CONFIG_SECURITY
  1623. case PROC_TID_ATTR:
  1624. inode->i_nlink = 2;
  1625. inode->i_op = &proc_tid_attr_inode_operations;
  1626. inode->i_fop = &proc_tid_attr_operations;
  1627. break;
  1628. case PROC_TGID_ATTR:
  1629. inode->i_nlink = 2;
  1630. inode->i_op = &proc_tgid_attr_inode_operations;
  1631. inode->i_fop = &proc_tgid_attr_operations;
  1632. break;
  1633. case PROC_TID_ATTR_CURRENT:
  1634. case PROC_TGID_ATTR_CURRENT:
  1635. case PROC_TID_ATTR_PREV:
  1636. case PROC_TGID_ATTR_PREV:
  1637. case PROC_TID_ATTR_EXEC:
  1638. case PROC_TGID_ATTR_EXEC:
  1639. case PROC_TID_ATTR_FSCREATE:
  1640. case PROC_TGID_ATTR_FSCREATE:
  1641. case PROC_TID_ATTR_KEYCREATE:
  1642. case PROC_TGID_ATTR_KEYCREATE:
  1643. inode->i_fop = &proc_pid_attr_operations;
  1644. break;
  1645. #endif
  1646. #ifdef CONFIG_KALLSYMS
  1647. case PROC_TID_WCHAN:
  1648. case PROC_TGID_WCHAN:
  1649. inode->i_fop = &proc_info_file_operations;
  1650. ei->op.proc_read = proc_pid_wchan;
  1651. break;
  1652. #endif
  1653. #ifdef CONFIG_SCHEDSTATS
  1654. case PROC_TID_SCHEDSTAT:
  1655. case PROC_TGID_SCHEDSTAT:
  1656. inode->i_fop = &proc_info_file_operations;
  1657. ei->op.proc_read = proc_pid_schedstat;
  1658. break;
  1659. #endif
  1660. #ifdef CONFIG_CPUSETS
  1661. case PROC_TID_CPUSET:
  1662. case PROC_TGID_CPUSET:
  1663. inode->i_fop = &proc_cpuset_operations;
  1664. break;
  1665. #endif
  1666. case PROC_TID_OOM_SCORE:
  1667. case PROC_TGID_OOM_SCORE:
  1668. inode->i_fop = &proc_info_file_operations;
  1669. ei->op.proc_read = proc_oom_score;
  1670. break;
  1671. case PROC_TID_OOM_ADJUST:
  1672. case PROC_TGID_OOM_ADJUST:
  1673. inode->i_fop = &proc_oom_adjust_operations;
  1674. break;
  1675. #ifdef CONFIG_AUDITSYSCALL
  1676. case PROC_TID_LOGINUID:
  1677. case PROC_TGID_LOGINUID:
  1678. inode->i_fop = &proc_loginuid_operations;
  1679. break;
  1680. #endif
  1681. default:
  1682. printk("procfs: impossible type (%d)",p->type);
  1683. iput(inode);
  1684. error = ERR_PTR(-EINVAL);
  1685. goto out;
  1686. }
  1687. dentry->d_op = &pid_dentry_operations;
  1688. d_add(dentry, inode);
  1689. /* Close the race of the process dying before we return the dentry */
  1690. if (pid_revalidate(dentry, NULL))
  1691. error = NULL;
  1692. out:
  1693. put_task_struct(task);
  1694. out_no_task:
  1695. return error;
  1696. }
  1697. static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
  1698. return proc_pident_lookup(dir, dentry, tgid_base_stuff);
  1699. }
  1700. static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
  1701. return proc_pident_lookup(dir, dentry, tid_base_stuff);
  1702. }
  1703. static struct file_operations proc_tgid_base_operations = {
  1704. .read = generic_read_dir,
  1705. .readdir = proc_tgid_base_readdir,
  1706. };
  1707. static struct file_operations proc_tid_base_operations = {
  1708. .read = generic_read_dir,
  1709. .readdir = proc_tid_base_readdir,
  1710. };
  1711. static struct inode_operations proc_tgid_base_inode_operations = {
  1712. .lookup = proc_tgid_base_lookup,
  1713. .getattr = pid_getattr,
  1714. };
  1715. static struct inode_operations proc_tid_base_inode_operations = {
  1716. .lookup = proc_tid_base_lookup,
  1717. .getattr = pid_getattr,
  1718. };
  1719. #ifdef CONFIG_SECURITY
  1720. static int proc_tgid_attr_readdir(struct file * filp,
  1721. void * dirent, filldir_t filldir)
  1722. {
  1723. return proc_pident_readdir(filp,dirent,filldir,
  1724. tgid_attr_stuff,ARRAY_SIZE(tgid_attr_stuff));
  1725. }
  1726. static int proc_tid_attr_readdir(struct file * filp,
  1727. void * dirent, filldir_t filldir)
  1728. {
  1729. return proc_pident_readdir(filp,dirent,filldir,
  1730. tid_attr_stuff,ARRAY_SIZE(tid_attr_stuff));
  1731. }
  1732. static struct file_operations proc_tgid_attr_operations = {
  1733. .read = generic_read_dir,
  1734. .readdir = proc_tgid_attr_readdir,
  1735. };
  1736. static struct file_operations proc_tid_attr_operations = {
  1737. .read = generic_read_dir,
  1738. .readdir = proc_tid_attr_readdir,
  1739. };
  1740. static struct dentry *proc_tgid_attr_lookup(struct inode *dir,
  1741. struct dentry *dentry, struct nameidata *nd)
  1742. {
  1743. return proc_pident_lookup(dir, dentry, tgid_attr_stuff);
  1744. }
  1745. static struct dentry *proc_tid_attr_lookup(struct inode *dir,
  1746. struct dentry *dentry, struct nameidata *nd)
  1747. {
  1748. return proc_pident_lookup(dir, dentry, tid_attr_stuff);
  1749. }
  1750. static struct inode_operations proc_tgid_attr_inode_operations = {
  1751. .lookup = proc_tgid_attr_lookup,
  1752. .getattr = pid_getattr,
  1753. };
  1754. static struct inode_operations proc_tid_attr_inode_operations = {
  1755. .lookup = proc_tid_attr_lookup,
  1756. .getattr = pid_getattr,
  1757. };
  1758. #endif
  1759. /*
  1760. * /proc/self:
  1761. */
  1762. static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
  1763. int buflen)
  1764. {
  1765. char tmp[PROC_NUMBUF];
  1766. sprintf(tmp, "%d", current->tgid);
  1767. return vfs_readlink(dentry,buffer,buflen,tmp);
  1768. }
  1769. static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
  1770. {
  1771. char tmp[PROC_NUMBUF];
  1772. sprintf(tmp, "%d", current->tgid);
  1773. return ERR_PTR(vfs_follow_link(nd,tmp));
  1774. }
  1775. static struct inode_operations proc_self_inode_operations = {
  1776. .readlink = proc_self_readlink,
  1777. .follow_link = proc_self_follow_link,
  1778. };
  1779. /**
  1780. * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
  1781. *
  1782. * @task: task that should be flushed.
  1783. *
  1784. * Looks in the dcache for
  1785. * /proc/@pid
  1786. * /proc/@tgid/task/@pid
  1787. * if either directory is present flushes it and all of it'ts children
  1788. * from the dcache.
  1789. *
  1790. * It is safe and reasonable to cache /proc entries for a task until
  1791. * that task exits. After that they just clog up the dcache with
  1792. * useless entries, possibly causing useful dcache entries to be
  1793. * flushed instead. This routine is proved to flush those useless
  1794. * dcache entries at process exit time.
  1795. *
  1796. * NOTE: This routine is just an optimization so it does not guarantee
  1797. * that no dcache entries will exist at process exit time it
  1798. * just makes it very unlikely that any will persist.
  1799. */
  1800. void proc_flush_task(struct task_struct *task)
  1801. {
  1802. struct dentry *dentry, *leader, *dir;
  1803. char buf[PROC_NUMBUF];
  1804. struct qstr name;
  1805. name.name = buf;
  1806. name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
  1807. dentry = d_hash_and_lookup(proc_mnt->mnt_root, &name);
  1808. if (dentry) {
  1809. shrink_dcache_parent(dentry);
  1810. d_drop(dentry);
  1811. dput(dentry);
  1812. }
  1813. if (thread_group_leader(task))
  1814. goto out;
  1815. name.name = buf;
  1816. name.len = snprintf(buf, sizeof(buf), "%d", task->tgid);
  1817. leader = d_hash_and_lookup(proc_mnt->mnt_root, &name);
  1818. if (!leader)
  1819. goto out;
  1820. name.name = "task";
  1821. name.len = strlen(name.name);
  1822. dir = d_hash_and_lookup(leader, &name);
  1823. if (!dir)
  1824. goto out_put_leader;
  1825. name.name = buf;
  1826. name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
  1827. dentry = d_hash_and_lookup(dir, &name);
  1828. if (dentry) {
  1829. shrink_dcache_parent(dentry);
  1830. d_drop(dentry);
  1831. dput(dentry);
  1832. }
  1833. dput(dir);
  1834. out_put_leader:
  1835. dput(leader);
  1836. out:
  1837. return;
  1838. }
  1839. /* SMP-safe */
  1840. struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
  1841. {
  1842. struct dentry *result = ERR_PTR(-ENOENT);
  1843. struct task_struct *task;
  1844. struct inode *inode;
  1845. struct proc_inode *ei;
  1846. unsigned tgid;
  1847. if (dentry->d_name.len == 4 && !memcmp(dentry->d_name.name,"self",4)) {
  1848. inode = new_inode(dir->i_sb);
  1849. if (!inode)
  1850. return ERR_PTR(-ENOMEM);
  1851. ei = PROC_I(inode);
  1852. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  1853. inode->i_ino = fake_ino(0, PROC_TGID_INO);
  1854. ei->pde = NULL;
  1855. inode->i_mode = S_IFLNK|S_IRWXUGO;
  1856. inode->i_uid = inode->i_gid = 0;
  1857. inode->i_size = 64;
  1858. inode->i_op = &proc_self_inode_operations;
  1859. d_add(dentry, inode);
  1860. return NULL;
  1861. }
  1862. tgid = name_to_int(dentry);
  1863. if (tgid == ~0U)
  1864. goto out;
  1865. rcu_read_lock();
  1866. task = find_task_by_pid(tgid);
  1867. if (task)
  1868. get_task_struct(task);
  1869. rcu_read_unlock();
  1870. if (!task)
  1871. goto out;
  1872. inode = proc_pid_make_inode(dir->i_sb, task, PROC_TGID_INO);
  1873. if (!inode)
  1874. goto out_put_task;
  1875. inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
  1876. inode->i_op = &proc_tgid_base_inode_operations;
  1877. inode->i_fop = &proc_tgid_base_operations;
  1878. inode->i_flags|=S_IMMUTABLE;
  1879. #ifdef CONFIG_SECURITY
  1880. inode->i_nlink = 5;
  1881. #else
  1882. inode->i_nlink = 4;
  1883. #endif
  1884. dentry->d_op = &pid_dentry_operations;
  1885. d_add(dentry, inode);
  1886. /* Close the race of the process dying before we return the dentry */
  1887. if (pid_revalidate(dentry, NULL))
  1888. result = NULL;
  1889. out_put_task:
  1890. put_task_struct(task);
  1891. out:
  1892. return result;
  1893. }
  1894. /* SMP-safe */
  1895. static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
  1896. {
  1897. struct dentry *result = ERR_PTR(-ENOENT);
  1898. struct task_struct *task;
  1899. struct task_struct *leader = get_proc_task(dir);
  1900. struct inode *inode;
  1901. unsigned tid;
  1902. if (!leader)
  1903. goto out_no_task;
  1904. tid = name_to_int(dentry);
  1905. if (tid == ~0U)
  1906. goto out;
  1907. rcu_read_lock();
  1908. task = find_task_by_pid(tid);
  1909. if (task)
  1910. get_task_struct(task);
  1911. rcu_read_unlock();
  1912. if (!task)
  1913. goto out;
  1914. if (leader->tgid != task->tgid)
  1915. goto out_drop_task;
  1916. inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_INO);
  1917. if (!inode)
  1918. goto out_drop_task;
  1919. inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
  1920. inode->i_op = &proc_tid_base_inode_operations;
  1921. inode->i_fop = &proc_tid_base_operations;
  1922. inode->i_flags|=S_IMMUTABLE;
  1923. #ifdef CONFIG_SECURITY
  1924. inode->i_nlink = 4;
  1925. #else
  1926. inode->i_nlink = 3;
  1927. #endif
  1928. dentry->d_op = &pid_dentry_operations;
  1929. d_add(dentry, inode);
  1930. /* Close the race of the process dying before we return the dentry */
  1931. if (pid_revalidate(dentry, NULL))
  1932. result = NULL;
  1933. out_drop_task:
  1934. put_task_struct(task);
  1935. out:
  1936. put_task_struct(leader);
  1937. out_no_task:
  1938. return result;
  1939. }
  1940. /*
  1941. * Find the first tgid to return to user space.
  1942. *
  1943. * Usually this is just whatever follows &init_task, but if the users
  1944. * buffer was too small to hold the full list or there was a seek into
  1945. * the middle of the directory we have more work to do.
  1946. *
  1947. * In the case of a short read we start with find_task_by_pid.
  1948. *
  1949. * In the case of a seek we start with &init_task and walk nr
  1950. * threads past it.
  1951. */
  1952. static struct task_struct *first_tgid(int tgid, unsigned int nr)
  1953. {
  1954. struct task_struct *pos;
  1955. rcu_read_lock();
  1956. if (tgid && nr) {
  1957. pos = find_task_by_pid(tgid);
  1958. if (pos && thread_group_leader(pos))
  1959. goto found;
  1960. }
  1961. /* If nr exceeds the number of processes get out quickly */
  1962. pos = NULL;
  1963. if (nr && nr >= nr_processes())
  1964. goto done;
  1965. /* If we haven't found our starting place yet start with
  1966. * the init_task and walk nr tasks forward.
  1967. */
  1968. for (pos = next_task(&init_task); nr > 0; --nr) {
  1969. pos = next_task(pos);
  1970. if (pos == &init_task) {
  1971. pos = NULL;
  1972. goto done;
  1973. }
  1974. }
  1975. found:
  1976. get_task_struct(pos);
  1977. done:
  1978. rcu_read_unlock();
  1979. return pos;
  1980. }
  1981. /*
  1982. * Find the next task in the task list.
  1983. * Return NULL if we loop or there is any error.
  1984. *
  1985. * The reference to the input task_struct is released.
  1986. */
  1987. static struct task_struct *next_tgid(struct task_struct *start)
  1988. {
  1989. struct task_struct *pos;
  1990. rcu_read_lock();
  1991. pos = start;
  1992. if (pid_alive(start))
  1993. pos = next_task(start);
  1994. if (pid_alive(pos) && (pos != &init_task)) {
  1995. get_task_struct(pos);
  1996. goto done;
  1997. }
  1998. pos = NULL;
  1999. done:
  2000. rcu_read_unlock();
  2001. put_task_struct(start);
  2002. return pos;
  2003. }
  2004. /* for the /proc/ directory itself, after non-process stuff has been done */
  2005. int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
  2006. {
  2007. char buf[PROC_NUMBUF];
  2008. unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
  2009. struct task_struct *task;
  2010. int tgid;
  2011. if (!nr) {
  2012. ino_t ino = fake_ino(0,PROC_TGID_INO);
  2013. if (filldir(dirent, "self", 4, filp->f_pos, ino, DT_LNK) < 0)
  2014. return 0;
  2015. filp->f_pos++;
  2016. nr++;
  2017. }
  2018. nr -= 1;
  2019. /* f_version caches the tgid value that the last readdir call couldn't
  2020. * return. lseek aka telldir automagically resets f_version to 0.
  2021. */
  2022. tgid = filp->f_version;
  2023. filp->f_version = 0;
  2024. for (task = first_tgid(tgid, nr);
  2025. task;
  2026. task = next_tgid(task), filp->f_pos++) {
  2027. int len;
  2028. ino_t ino;
  2029. tgid = task->pid;
  2030. len = snprintf(buf, sizeof(buf), "%d", tgid);
  2031. ino = fake_ino(tgid, PROC_TGID_INO);
  2032. if (filldir(dirent, buf, len, filp->f_pos, ino, DT_DIR) < 0) {
  2033. /* returning this tgid failed, save it as the first
  2034. * pid for the next readir call */
  2035. filp->f_version = tgid;
  2036. put_task_struct(task);
  2037. break;
  2038. }
  2039. }
  2040. return 0;
  2041. }
  2042. /*
  2043. * Find the first tid of a thread group to return to user space.
  2044. *
  2045. * Usually this is just the thread group leader, but if the users
  2046. * buffer was too small or there was a seek into the middle of the
  2047. * directory we have more work todo.
  2048. *
  2049. * In the case of a short read we start with find_task_by_pid.
  2050. *
  2051. * In the case of a seek we start with the leader and walk nr
  2052. * threads past it.
  2053. */
  2054. static struct task_struct *first_tid(struct task_struct *leader, int tid, int nr)
  2055. {
  2056. struct task_struct *pos = NULL;
  2057. read_lock(&tasklist_lock);
  2058. /* Attempt to start with the pid of a thread */
  2059. if (tid && (nr > 0)) {
  2060. pos = find_task_by_pid(tid);
  2061. if (pos && (pos->group_leader != leader))
  2062. pos = NULL;
  2063. if (pos)
  2064. nr = 0;
  2065. }
  2066. /* If nr exceeds the number of threads there is nothing todo */
  2067. if (nr) {
  2068. if (nr >= get_nr_threads(leader))
  2069. goto done;
  2070. }
  2071. /* If we haven't found our starting place yet start with the
  2072. * leader and walk nr threads forward.
  2073. */
  2074. if (!pos && (nr >= 0))
  2075. pos = leader;
  2076. for (; pos && pid_alive(pos); pos = next_thread(pos)) {
  2077. if (--nr > 0)
  2078. continue;
  2079. get_task_struct(pos);
  2080. goto done;
  2081. }
  2082. pos = NULL;
  2083. done:
  2084. read_unlock(&tasklist_lock);
  2085. return pos;
  2086. }
  2087. /*
  2088. * Find the next thread in the thread list.
  2089. * Return NULL if there is an error or no next thread.
  2090. *
  2091. * The reference to the input task_struct is released.
  2092. */
  2093. static struct task_struct *next_tid(struct task_struct *start)
  2094. {
  2095. struct task_struct *pos;
  2096. read_lock(&tasklist_lock);
  2097. pos = start;
  2098. if (pid_alive(start))
  2099. pos = next_thread(start);
  2100. if (pid_alive(pos) && (pos != start->group_leader))
  2101. get_task_struct(pos);
  2102. else
  2103. pos = NULL;
  2104. read_unlock(&tasklist_lock);
  2105. put_task_struct(start);
  2106. return pos;
  2107. }
  2108. /* for the /proc/TGID/task/ directories */
  2109. static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
  2110. {
  2111. char buf[PROC_NUMBUF];
  2112. struct dentry *dentry = filp->f_dentry;
  2113. struct inode *inode = dentry->d_inode;
  2114. struct task_struct *leader = get_proc_task(inode);
  2115. struct task_struct *task;
  2116. int retval = -ENOENT;
  2117. ino_t ino;
  2118. int tid;
  2119. unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
  2120. if (!leader)
  2121. goto out_no_task;
  2122. retval = 0;
  2123. switch (pos) {
  2124. case 0:
  2125. ino = inode->i_ino;
  2126. if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
  2127. goto out;
  2128. pos++;
  2129. /* fall through */
  2130. case 1:
  2131. ino = parent_ino(dentry);
  2132. if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
  2133. goto out;
  2134. pos++;
  2135. /* fall through */
  2136. }
  2137. /* f_version caches the tgid value that the last readdir call couldn't
  2138. * return. lseek aka telldir automagically resets f_version to 0.
  2139. */
  2140. tid = filp->f_version;
  2141. filp->f_version = 0;
  2142. for (task = first_tid(leader, tid, pos - 2);
  2143. task;
  2144. task = next_tid(task), pos++) {
  2145. int len;
  2146. tid = task->pid;
  2147. len = snprintf(buf, sizeof(buf), "%d", tid);
  2148. ino = fake_ino(tid, PROC_TID_INO);
  2149. if (filldir(dirent, buf, len, pos, ino, DT_DIR < 0)) {
  2150. /* returning this tgid failed, save it as the first
  2151. * pid for the next readir call */
  2152. filp->f_version = tid;
  2153. put_task_struct(task);
  2154. break;
  2155. }
  2156. }
  2157. out:
  2158. filp->f_pos = pos;
  2159. put_task_struct(leader);
  2160. out_no_task:
  2161. return retval;
  2162. }
  2163. static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
  2164. {
  2165. struct inode *inode = dentry->d_inode;
  2166. struct task_struct *p = get_proc_task(inode);
  2167. generic_fillattr(inode, stat);
  2168. if (p) {
  2169. rcu_read_lock();
  2170. stat->nlink += get_nr_threads(p);
  2171. rcu_read_unlock();
  2172. put_task_struct(p);
  2173. }
  2174. return 0;
  2175. }