base.c 83 KB

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