base.c 82 KB

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