base.c 87 KB

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