base.c 84 KB

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