base.c 79 KB

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