base.c 79 KB

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