base.c 86 KB

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