base.c 83 KB

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