base.c 79 KB

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