base.c 86 KB

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