base.c 84 KB

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