base.c 91 KB

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