base.c 88 KB

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