base.c 83 KB

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