base.c 85 KB

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