base.c 86 KB

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