base.c 83 KB

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