base.c 88 KB

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