base.c 85 KB

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