base.c 84 KB

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