base.c 83 KB

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