base.c 79 KB

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