base.c 95 KB

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