base.c 87 KB

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