base.c 96 KB

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