base.c 94 KB

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