base.c 85 KB

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