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