base.c 86 KB

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