base.c 96 KB

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