base.c 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543
  1. /*
  2. * linux/fs/proc/base.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. *
  6. * proc base directory handling functions
  7. *
  8. * 1999, Al Viro. Rewritten. Now it covers the whole per-process part.
  9. * Instead of using magical inumbers to determine the kind of object
  10. * we allocate and fill in-core inodes upon lookup. They don't even
  11. * go into icache. We cache the reference to task_struct upon lookup too.
  12. * Eventually it should become a filesystem in its own. We don't use the
  13. * rest of procfs anymore.
  14. *
  15. *
  16. * Changelog:
  17. * 17-Jan-2005
  18. * Allan Bezerra
  19. * Bruna Moreira <bruna.moreira@indt.org.br>
  20. * Edjard Mota <edjard.mota@indt.org.br>
  21. * Ilias Biris <ilias.biris@indt.org.br>
  22. * Mauricio Lin <mauricio.lin@indt.org.br>
  23. *
  24. * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
  25. *
  26. * A new process specific entry (smaps) included in /proc. It shows the
  27. * size of rss for each memory area. The maps entry lacks information
  28. * about physical memory size (rss) for each mapped file, i.e.,
  29. * rss information for executables and library files.
  30. * This additional information is useful for any tools that need to know
  31. * about physical memory consumption for a process specific library.
  32. *
  33. * Changelog:
  34. * 21-Feb-2005
  35. * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
  36. * Pud inclusion in the page table walking.
  37. *
  38. * ChangeLog:
  39. * 10-Mar-2005
  40. * 10LE Instituto Nokia de Tecnologia - INdT:
  41. * A better way to walks through the page table as suggested by Hugh Dickins.
  42. *
  43. * Simo Piiroinen <simo.piiroinen@nokia.com>:
  44. * Smaps information related to shared, private, clean and dirty pages.
  45. *
  46. * Paul Mundt <paul.mundt@nokia.com>:
  47. * Overall revision about smaps.
  48. */
  49. #include <asm/uaccess.h>
  50. #include <linux/errno.h>
  51. #include <linux/time.h>
  52. #include <linux/proc_fs.h>
  53. #include <linux/stat.h>
  54. #include <linux/init.h>
  55. #include <linux/capability.h>
  56. #include <linux/file.h>
  57. #include <linux/string.h>
  58. #include <linux/seq_file.h>
  59. #include <linux/namei.h>
  60. #include <linux/mnt_namespace.h>
  61. #include <linux/mm.h>
  62. #include <linux/rcupdate.h>
  63. #include <linux/kallsyms.h>
  64. #include <linux/module.h>
  65. #include <linux/mount.h>
  66. #include <linux/security.h>
  67. #include <linux/ptrace.h>
  68. #include <linux/seccomp.h>
  69. #include <linux/cpuset.h>
  70. #include <linux/audit.h>
  71. #include <linux/poll.h>
  72. #include <linux/nsproxy.h>
  73. #include <linux/oom.h>
  74. #include "internal.h"
  75. /* NOTE:
  76. * Implementing inode permission operations in /proc is almost
  77. * certainly an error. Permission checks need to happen during
  78. * each system call not at open time. The reason is that most of
  79. * what we wish to check for permissions in /proc varies at runtime.
  80. *
  81. * The classic example of a problem is opening file descriptors
  82. * in /proc for a task before it execs a suid executable.
  83. */
  84. /* Worst case buffer size needed for holding an integer. */
  85. #define PROC_NUMBUF 13
  86. struct pid_entry {
  87. char *name;
  88. int len;
  89. mode_t mode;
  90. const struct inode_operations *iop;
  91. const struct file_operations *fop;
  92. union proc_op op;
  93. };
  94. #define NOD(NAME, MODE, IOP, FOP, OP) { \
  95. .name = (NAME), \
  96. .len = sizeof(NAME) - 1, \
  97. .mode = MODE, \
  98. .iop = IOP, \
  99. .fop = FOP, \
  100. .op = OP, \
  101. }
  102. #define DIR(NAME, MODE, OTYPE) \
  103. NOD(NAME, (S_IFDIR|(MODE)), \
  104. &proc_##OTYPE##_inode_operations, &proc_##OTYPE##_operations, \
  105. {} )
  106. #define LNK(NAME, OTYPE) \
  107. NOD(NAME, (S_IFLNK|S_IRWXUGO), \
  108. &proc_pid_link_inode_operations, NULL, \
  109. { .proc_get_link = &proc_##OTYPE##_link } )
  110. #define REG(NAME, MODE, OTYPE) \
  111. NOD(NAME, (S_IFREG|(MODE)), NULL, \
  112. &proc_##OTYPE##_operations, {})
  113. #define INF(NAME, MODE, OTYPE) \
  114. NOD(NAME, (S_IFREG|(MODE)), \
  115. NULL, &proc_info_file_operations, \
  116. { .proc_read = &proc_##OTYPE } )
  117. int maps_protect;
  118. EXPORT_SYMBOL(maps_protect);
  119. static struct fs_struct *get_fs_struct(struct task_struct *task)
  120. {
  121. struct fs_struct *fs;
  122. task_lock(task);
  123. fs = task->fs;
  124. if(fs)
  125. atomic_inc(&fs->count);
  126. task_unlock(task);
  127. return fs;
  128. }
  129. static int get_nr_threads(struct task_struct *tsk)
  130. {
  131. /* Must be called with the rcu_read_lock held */
  132. unsigned long flags;
  133. int count = 0;
  134. if (lock_task_sighand(tsk, &flags)) {
  135. count = atomic_read(&tsk->signal->count);
  136. unlock_task_sighand(tsk, &flags);
  137. }
  138. return count;
  139. }
  140. static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
  141. {
  142. struct task_struct *task = get_proc_task(inode);
  143. struct fs_struct *fs = NULL;
  144. int result = -ENOENT;
  145. if (task) {
  146. fs = get_fs_struct(task);
  147. put_task_struct(task);
  148. }
  149. if (fs) {
  150. read_lock(&fs->lock);
  151. *mnt = mntget(fs->pwdmnt);
  152. *dentry = dget(fs->pwd);
  153. read_unlock(&fs->lock);
  154. result = 0;
  155. put_fs_struct(fs);
  156. }
  157. return result;
  158. }
  159. static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
  160. {
  161. struct task_struct *task = get_proc_task(inode);
  162. struct fs_struct *fs = NULL;
  163. int result = -ENOENT;
  164. if (task) {
  165. fs = get_fs_struct(task);
  166. put_task_struct(task);
  167. }
  168. if (fs) {
  169. read_lock(&fs->lock);
  170. *mnt = mntget(fs->rootmnt);
  171. *dentry = dget(fs->root);
  172. read_unlock(&fs->lock);
  173. result = 0;
  174. put_fs_struct(fs);
  175. }
  176. return result;
  177. }
  178. #define MAY_PTRACE(task) \
  179. (task == current || \
  180. (task->parent == current && \
  181. (task->ptrace & PT_PTRACED) && \
  182. (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
  183. security_ptrace(current,task) == 0))
  184. static int proc_pid_environ(struct task_struct *task, char * buffer)
  185. {
  186. int res = 0;
  187. struct mm_struct *mm = get_task_mm(task);
  188. if (mm) {
  189. unsigned int len = mm->env_end - mm->env_start;
  190. if (len > PAGE_SIZE)
  191. len = PAGE_SIZE;
  192. res = access_process_vm(task, mm->env_start, buffer, len, 0);
  193. if (!ptrace_may_attach(task))
  194. res = -ESRCH;
  195. mmput(mm);
  196. }
  197. return res;
  198. }
  199. static int proc_pid_cmdline(struct task_struct *task, char * buffer)
  200. {
  201. int res = 0;
  202. unsigned int len;
  203. struct mm_struct *mm = get_task_mm(task);
  204. if (!mm)
  205. goto out;
  206. if (!mm->arg_end)
  207. goto out_mm; /* Shh! No looking before we're done */
  208. len = mm->arg_end - mm->arg_start;
  209. if (len > PAGE_SIZE)
  210. len = PAGE_SIZE;
  211. res = access_process_vm(task, mm->arg_start, buffer, len, 0);
  212. // If the nul at the end of args has been overwritten, then
  213. // assume application is using setproctitle(3).
  214. if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
  215. len = strnlen(buffer, res);
  216. if (len < res) {
  217. res = len;
  218. } else {
  219. len = mm->env_end - mm->env_start;
  220. if (len > PAGE_SIZE - res)
  221. len = PAGE_SIZE - res;
  222. res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
  223. res = strnlen(buffer, res);
  224. }
  225. }
  226. out_mm:
  227. mmput(mm);
  228. out:
  229. return res;
  230. }
  231. static int proc_pid_auxv(struct task_struct *task, char *buffer)
  232. {
  233. int res = 0;
  234. struct mm_struct *mm = get_task_mm(task);
  235. if (mm) {
  236. unsigned int nwords = 0;
  237. do
  238. nwords += 2;
  239. while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
  240. res = nwords * sizeof(mm->saved_auxv[0]);
  241. if (res > PAGE_SIZE)
  242. res = PAGE_SIZE;
  243. memcpy(buffer, mm->saved_auxv, res);
  244. mmput(mm);
  245. }
  246. return res;
  247. }
  248. #ifdef CONFIG_KALLSYMS
  249. /*
  250. * Provides a wchan file via kallsyms in a proper one-value-per-file format.
  251. * Returns the resolved symbol. If that fails, simply return the address.
  252. */
  253. static int proc_pid_wchan(struct task_struct *task, char *buffer)
  254. {
  255. unsigned long wchan;
  256. char symname[KSYM_NAME_LEN+1];
  257. wchan = get_wchan(task);
  258. if (lookup_symbol_name(wchan, symname) < 0)
  259. return sprintf(buffer, "%lu", wchan);
  260. else
  261. return sprintf(buffer, "%s", symname);
  262. }
  263. #endif /* CONFIG_KALLSYMS */
  264. #ifdef CONFIG_SCHEDSTATS
  265. /*
  266. * Provides /proc/PID/schedstat
  267. */
  268. static int proc_pid_schedstat(struct task_struct *task, char *buffer)
  269. {
  270. return sprintf(buffer, "%lu %lu %lu\n",
  271. task->sched_info.cpu_time,
  272. task->sched_info.run_delay,
  273. task->sched_info.pcnt);
  274. }
  275. #endif
  276. /* The badness from the OOM killer */
  277. unsigned long badness(struct task_struct *p, unsigned long uptime);
  278. static int proc_oom_score(struct task_struct *task, char *buffer)
  279. {
  280. unsigned long points;
  281. struct timespec uptime;
  282. do_posix_clock_monotonic_gettime(&uptime);
  283. read_lock(&tasklist_lock);
  284. points = badness(task, uptime.tv_sec);
  285. read_unlock(&tasklist_lock);
  286. return sprintf(buffer, "%lu\n", points);
  287. }
  288. /************************************************************************/
  289. /* Here the fs part begins */
  290. /************************************************************************/
  291. /* permission checks */
  292. static int proc_fd_access_allowed(struct inode *inode)
  293. {
  294. struct task_struct *task;
  295. int allowed = 0;
  296. /* Allow access to a task's file descriptors if it is us or we
  297. * may use ptrace attach to the process and find out that
  298. * information.
  299. */
  300. task = get_proc_task(inode);
  301. if (task) {
  302. allowed = ptrace_may_attach(task);
  303. put_task_struct(task);
  304. }
  305. return allowed;
  306. }
  307. static int proc_setattr(struct dentry *dentry, struct iattr *attr)
  308. {
  309. int error;
  310. struct inode *inode = dentry->d_inode;
  311. if (attr->ia_valid & ATTR_MODE)
  312. return -EPERM;
  313. error = inode_change_ok(inode, attr);
  314. if (!error)
  315. error = inode_setattr(inode, attr);
  316. return error;
  317. }
  318. static const struct inode_operations proc_def_inode_operations = {
  319. .setattr = proc_setattr,
  320. };
  321. extern struct seq_operations mounts_op;
  322. struct proc_mounts {
  323. struct seq_file m;
  324. int event;
  325. };
  326. static int mounts_open(struct inode *inode, struct file *file)
  327. {
  328. struct task_struct *task = get_proc_task(inode);
  329. struct mnt_namespace *ns = NULL;
  330. struct proc_mounts *p;
  331. int ret = -EINVAL;
  332. if (task) {
  333. task_lock(task);
  334. if (task->nsproxy) {
  335. ns = task->nsproxy->mnt_ns;
  336. if (ns)
  337. get_mnt_ns(ns);
  338. }
  339. task_unlock(task);
  340. put_task_struct(task);
  341. }
  342. if (ns) {
  343. ret = -ENOMEM;
  344. p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
  345. if (p) {
  346. file->private_data = &p->m;
  347. ret = seq_open(file, &mounts_op);
  348. if (!ret) {
  349. p->m.private = ns;
  350. p->event = ns->event;
  351. return 0;
  352. }
  353. kfree(p);
  354. }
  355. put_mnt_ns(ns);
  356. }
  357. return ret;
  358. }
  359. static int mounts_release(struct inode *inode, struct file *file)
  360. {
  361. struct seq_file *m = file->private_data;
  362. struct mnt_namespace *ns = m->private;
  363. put_mnt_ns(ns);
  364. return seq_release(inode, file);
  365. }
  366. static unsigned mounts_poll(struct file *file, poll_table *wait)
  367. {
  368. struct proc_mounts *p = file->private_data;
  369. struct mnt_namespace *ns = p->m.private;
  370. unsigned res = 0;
  371. poll_wait(file, &ns->poll, wait);
  372. spin_lock(&vfsmount_lock);
  373. if (p->event != ns->event) {
  374. p->event = ns->event;
  375. res = POLLERR;
  376. }
  377. spin_unlock(&vfsmount_lock);
  378. return res;
  379. }
  380. static const struct file_operations proc_mounts_operations = {
  381. .open = mounts_open,
  382. .read = seq_read,
  383. .llseek = seq_lseek,
  384. .release = mounts_release,
  385. .poll = mounts_poll,
  386. };
  387. extern struct seq_operations mountstats_op;
  388. static int mountstats_open(struct inode *inode, struct file *file)
  389. {
  390. int ret = seq_open(file, &mountstats_op);
  391. if (!ret) {
  392. struct seq_file *m = file->private_data;
  393. struct mnt_namespace *mnt_ns = NULL;
  394. struct task_struct *task = get_proc_task(inode);
  395. if (task) {
  396. task_lock(task);
  397. if (task->nsproxy)
  398. mnt_ns = task->nsproxy->mnt_ns;
  399. if (mnt_ns)
  400. get_mnt_ns(mnt_ns);
  401. task_unlock(task);
  402. put_task_struct(task);
  403. }
  404. if (mnt_ns)
  405. m->private = mnt_ns;
  406. else {
  407. seq_release(inode, file);
  408. ret = -EINVAL;
  409. }
  410. }
  411. return ret;
  412. }
  413. static const struct file_operations proc_mountstats_operations = {
  414. .open = mountstats_open,
  415. .read = seq_read,
  416. .llseek = seq_lseek,
  417. .release = mounts_release,
  418. };
  419. #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
  420. static ssize_t proc_info_read(struct file * file, char __user * buf,
  421. size_t count, loff_t *ppos)
  422. {
  423. struct inode * inode = file->f_path.dentry->d_inode;
  424. unsigned long page;
  425. ssize_t length;
  426. struct task_struct *task = get_proc_task(inode);
  427. length = -ESRCH;
  428. if (!task)
  429. goto out_no_task;
  430. if (count > PROC_BLOCK_SIZE)
  431. count = PROC_BLOCK_SIZE;
  432. length = -ENOMEM;
  433. if (!(page = __get_free_page(GFP_KERNEL)))
  434. goto out;
  435. length = PROC_I(inode)->op.proc_read(task, (char*)page);
  436. if (length >= 0)
  437. length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
  438. free_page(page);
  439. out:
  440. put_task_struct(task);
  441. out_no_task:
  442. return length;
  443. }
  444. static const struct file_operations proc_info_file_operations = {
  445. .read = proc_info_read,
  446. };
  447. static int mem_open(struct inode* inode, struct file* file)
  448. {
  449. file->private_data = (void*)((long)current->self_exec_id);
  450. return 0;
  451. }
  452. static ssize_t mem_read(struct file * file, char __user * buf,
  453. size_t count, loff_t *ppos)
  454. {
  455. struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
  456. char *page;
  457. unsigned long src = *ppos;
  458. int ret = -ESRCH;
  459. struct mm_struct *mm;
  460. if (!task)
  461. goto out_no_task;
  462. if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
  463. goto out;
  464. ret = -ENOMEM;
  465. page = (char *)__get_free_page(GFP_USER);
  466. if (!page)
  467. goto out;
  468. ret = 0;
  469. mm = get_task_mm(task);
  470. if (!mm)
  471. goto out_free;
  472. ret = -EIO;
  473. if (file->private_data != (void*)((long)current->self_exec_id))
  474. goto out_put;
  475. ret = 0;
  476. while (count > 0) {
  477. int this_len, retval;
  478. this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
  479. retval = access_process_vm(task, src, page, this_len, 0);
  480. if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
  481. if (!ret)
  482. ret = -EIO;
  483. break;
  484. }
  485. if (copy_to_user(buf, page, retval)) {
  486. ret = -EFAULT;
  487. break;
  488. }
  489. ret += retval;
  490. src += retval;
  491. buf += retval;
  492. count -= retval;
  493. }
  494. *ppos = src;
  495. out_put:
  496. mmput(mm);
  497. out_free:
  498. free_page((unsigned long) page);
  499. out:
  500. put_task_struct(task);
  501. out_no_task:
  502. return ret;
  503. }
  504. #define mem_write NULL
  505. #ifndef mem_write
  506. /* This is a security hazard */
  507. static ssize_t mem_write(struct file * file, const char __user *buf,
  508. size_t count, loff_t *ppos)
  509. {
  510. int copied;
  511. char *page;
  512. struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
  513. unsigned long dst = *ppos;
  514. copied = -ESRCH;
  515. if (!task)
  516. goto out_no_task;
  517. if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
  518. goto out;
  519. copied = -ENOMEM;
  520. page = (char *)__get_free_page(GFP_USER);
  521. if (!page)
  522. goto out;
  523. copied = 0;
  524. while (count > 0) {
  525. int this_len, retval;
  526. this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
  527. if (copy_from_user(page, buf, this_len)) {
  528. copied = -EFAULT;
  529. break;
  530. }
  531. retval = access_process_vm(task, dst, page, this_len, 1);
  532. if (!retval) {
  533. if (!copied)
  534. copied = -EIO;
  535. break;
  536. }
  537. copied += retval;
  538. buf += retval;
  539. dst += retval;
  540. count -= retval;
  541. }
  542. *ppos = dst;
  543. free_page((unsigned long) page);
  544. out:
  545. put_task_struct(task);
  546. out_no_task:
  547. return copied;
  548. }
  549. #endif
  550. static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
  551. {
  552. switch (orig) {
  553. case 0:
  554. file->f_pos = offset;
  555. break;
  556. case 1:
  557. file->f_pos += offset;
  558. break;
  559. default:
  560. return -EINVAL;
  561. }
  562. force_successful_syscall_return();
  563. return file->f_pos;
  564. }
  565. static const struct file_operations proc_mem_operations = {
  566. .llseek = mem_lseek,
  567. .read = mem_read,
  568. .write = mem_write,
  569. .open = mem_open,
  570. };
  571. static ssize_t oom_adjust_read(struct file *file, char __user *buf,
  572. size_t count, loff_t *ppos)
  573. {
  574. struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
  575. char buffer[PROC_NUMBUF];
  576. size_t len;
  577. int oom_adjust;
  578. if (!task)
  579. return -ESRCH;
  580. oom_adjust = task->oomkilladj;
  581. put_task_struct(task);
  582. len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
  583. return simple_read_from_buffer(buf, count, ppos, buffer, len);
  584. }
  585. static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
  586. size_t count, loff_t *ppos)
  587. {
  588. struct task_struct *task;
  589. char buffer[PROC_NUMBUF], *end;
  590. int oom_adjust;
  591. memset(buffer, 0, sizeof(buffer));
  592. if (count > sizeof(buffer) - 1)
  593. count = sizeof(buffer) - 1;
  594. if (copy_from_user(buffer, buf, count))
  595. return -EFAULT;
  596. oom_adjust = simple_strtol(buffer, &end, 0);
  597. if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
  598. oom_adjust != OOM_DISABLE)
  599. return -EINVAL;
  600. if (*end == '\n')
  601. end++;
  602. task = get_proc_task(file->f_path.dentry->d_inode);
  603. if (!task)
  604. return -ESRCH;
  605. if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) {
  606. put_task_struct(task);
  607. return -EACCES;
  608. }
  609. task->oomkilladj = oom_adjust;
  610. put_task_struct(task);
  611. if (end - buffer == 0)
  612. return -EIO;
  613. return end - buffer;
  614. }
  615. static const struct file_operations proc_oom_adjust_operations = {
  616. .read = oom_adjust_read,
  617. .write = oom_adjust_write,
  618. };
  619. #ifdef CONFIG_MMU
  620. static ssize_t clear_refs_write(struct file *file, const char __user *buf,
  621. size_t count, loff_t *ppos)
  622. {
  623. struct task_struct *task;
  624. char buffer[PROC_NUMBUF], *end;
  625. struct mm_struct *mm;
  626. memset(buffer, 0, sizeof(buffer));
  627. if (count > sizeof(buffer) - 1)
  628. count = sizeof(buffer) - 1;
  629. if (copy_from_user(buffer, buf, count))
  630. return -EFAULT;
  631. if (!simple_strtol(buffer, &end, 0))
  632. return -EINVAL;
  633. if (*end == '\n')
  634. end++;
  635. task = get_proc_task(file->f_path.dentry->d_inode);
  636. if (!task)
  637. return -ESRCH;
  638. mm = get_task_mm(task);
  639. if (mm) {
  640. clear_refs_smap(mm);
  641. mmput(mm);
  642. }
  643. put_task_struct(task);
  644. if (end - buffer == 0)
  645. return -EIO;
  646. return end - buffer;
  647. }
  648. static struct file_operations proc_clear_refs_operations = {
  649. .write = clear_refs_write,
  650. };
  651. #endif
  652. #ifdef CONFIG_AUDITSYSCALL
  653. #define TMPBUFLEN 21
  654. static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
  655. size_t count, loff_t *ppos)
  656. {
  657. struct inode * inode = file->f_path.dentry->d_inode;
  658. struct task_struct *task = get_proc_task(inode);
  659. ssize_t length;
  660. char tmpbuf[TMPBUFLEN];
  661. if (!task)
  662. return -ESRCH;
  663. length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
  664. audit_get_loginuid(task->audit_context));
  665. put_task_struct(task);
  666. return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
  667. }
  668. static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
  669. size_t count, loff_t *ppos)
  670. {
  671. struct inode * inode = file->f_path.dentry->d_inode;
  672. char *page, *tmp;
  673. ssize_t length;
  674. uid_t loginuid;
  675. if (!capable(CAP_AUDIT_CONTROL))
  676. return -EPERM;
  677. if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
  678. return -EPERM;
  679. if (count >= PAGE_SIZE)
  680. count = PAGE_SIZE - 1;
  681. if (*ppos != 0) {
  682. /* No partial writes. */
  683. return -EINVAL;
  684. }
  685. page = (char*)__get_free_page(GFP_USER);
  686. if (!page)
  687. return -ENOMEM;
  688. length = -EFAULT;
  689. if (copy_from_user(page, buf, count))
  690. goto out_free_page;
  691. page[count] = '\0';
  692. loginuid = simple_strtoul(page, &tmp, 10);
  693. if (tmp == page) {
  694. length = -EINVAL;
  695. goto out_free_page;
  696. }
  697. length = audit_set_loginuid(current, loginuid);
  698. if (likely(length == 0))
  699. length = count;
  700. out_free_page:
  701. free_page((unsigned long) page);
  702. return length;
  703. }
  704. static const struct file_operations proc_loginuid_operations = {
  705. .read = proc_loginuid_read,
  706. .write = proc_loginuid_write,
  707. };
  708. #endif
  709. #ifdef CONFIG_SECCOMP
  710. static ssize_t seccomp_read(struct file *file, char __user *buf,
  711. size_t count, loff_t *ppos)
  712. {
  713. struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
  714. char __buf[20];
  715. size_t len;
  716. if (!tsk)
  717. return -ESRCH;
  718. /* no need to print the trailing zero, so use only len */
  719. len = sprintf(__buf, "%u\n", tsk->seccomp.mode);
  720. put_task_struct(tsk);
  721. return simple_read_from_buffer(buf, count, ppos, __buf, len);
  722. }
  723. static ssize_t seccomp_write(struct file *file, const char __user *buf,
  724. size_t count, loff_t *ppos)
  725. {
  726. struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
  727. char __buf[20], *end;
  728. unsigned int seccomp_mode;
  729. ssize_t result;
  730. result = -ESRCH;
  731. if (!tsk)
  732. goto out_no_task;
  733. /* can set it only once to be even more secure */
  734. result = -EPERM;
  735. if (unlikely(tsk->seccomp.mode))
  736. goto out;
  737. result = -EFAULT;
  738. memset(__buf, 0, sizeof(__buf));
  739. count = min(count, sizeof(__buf) - 1);
  740. if (copy_from_user(__buf, buf, count))
  741. goto out;
  742. seccomp_mode = simple_strtoul(__buf, &end, 0);
  743. if (*end == '\n')
  744. end++;
  745. result = -EINVAL;
  746. if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) {
  747. tsk->seccomp.mode = seccomp_mode;
  748. set_tsk_thread_flag(tsk, TIF_SECCOMP);
  749. } else
  750. goto out;
  751. result = -EIO;
  752. if (unlikely(!(end - __buf)))
  753. goto out;
  754. result = end - __buf;
  755. out:
  756. put_task_struct(tsk);
  757. out_no_task:
  758. return result;
  759. }
  760. static const struct file_operations proc_seccomp_operations = {
  761. .read = seccomp_read,
  762. .write = seccomp_write,
  763. };
  764. #endif /* CONFIG_SECCOMP */
  765. #ifdef CONFIG_FAULT_INJECTION
  766. static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
  767. size_t count, loff_t *ppos)
  768. {
  769. struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
  770. char buffer[PROC_NUMBUF];
  771. size_t len;
  772. int make_it_fail;
  773. if (!task)
  774. return -ESRCH;
  775. make_it_fail = task->make_it_fail;
  776. put_task_struct(task);
  777. len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
  778. return simple_read_from_buffer(buf, count, ppos, buffer, len);
  779. }
  780. static ssize_t proc_fault_inject_write(struct file * file,
  781. const char __user * buf, size_t count, loff_t *ppos)
  782. {
  783. struct task_struct *task;
  784. char buffer[PROC_NUMBUF], *end;
  785. int make_it_fail;
  786. if (!capable(CAP_SYS_RESOURCE))
  787. return -EPERM;
  788. memset(buffer, 0, sizeof(buffer));
  789. if (count > sizeof(buffer) - 1)
  790. count = sizeof(buffer) - 1;
  791. if (copy_from_user(buffer, buf, count))
  792. return -EFAULT;
  793. make_it_fail = simple_strtol(buffer, &end, 0);
  794. if (*end == '\n')
  795. end++;
  796. task = get_proc_task(file->f_dentry->d_inode);
  797. if (!task)
  798. return -ESRCH;
  799. task->make_it_fail = make_it_fail;
  800. put_task_struct(task);
  801. if (end - buffer == 0)
  802. return -EIO;
  803. return end - buffer;
  804. }
  805. static const struct file_operations proc_fault_inject_operations = {
  806. .read = proc_fault_inject_read,
  807. .write = proc_fault_inject_write,
  808. };
  809. #endif
  810. static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
  811. {
  812. struct inode *inode = dentry->d_inode;
  813. int error = -EACCES;
  814. /* We don't need a base pointer in the /proc filesystem */
  815. path_release(nd);
  816. /* Are we allowed to snoop on the tasks file descriptors? */
  817. if (!proc_fd_access_allowed(inode))
  818. goto out;
  819. error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
  820. nd->last_type = LAST_BIND;
  821. out:
  822. return ERR_PTR(error);
  823. }
  824. static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
  825. char __user *buffer, int buflen)
  826. {
  827. struct inode * inode;
  828. char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
  829. int len;
  830. if (!tmp)
  831. return -ENOMEM;
  832. inode = dentry->d_inode;
  833. path = d_path(dentry, mnt, tmp, PAGE_SIZE);
  834. len = PTR_ERR(path);
  835. if (IS_ERR(path))
  836. goto out;
  837. len = tmp + PAGE_SIZE - 1 - path;
  838. if (len > buflen)
  839. len = buflen;
  840. if (copy_to_user(buffer, path, len))
  841. len = -EFAULT;
  842. out:
  843. free_page((unsigned long)tmp);
  844. return len;
  845. }
  846. static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
  847. {
  848. int error = -EACCES;
  849. struct inode *inode = dentry->d_inode;
  850. struct dentry *de;
  851. struct vfsmount *mnt = NULL;
  852. /* Are we allowed to snoop on the tasks file descriptors? */
  853. if (!proc_fd_access_allowed(inode))
  854. goto out;
  855. error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
  856. if (error)
  857. goto out;
  858. error = do_proc_readlink(de, mnt, buffer, buflen);
  859. dput(de);
  860. mntput(mnt);
  861. out:
  862. return error;
  863. }
  864. static const struct inode_operations proc_pid_link_inode_operations = {
  865. .readlink = proc_pid_readlink,
  866. .follow_link = proc_pid_follow_link,
  867. .setattr = proc_setattr,
  868. };
  869. /* building an inode */
  870. static int task_dumpable(struct task_struct *task)
  871. {
  872. int dumpable = 0;
  873. struct mm_struct *mm;
  874. task_lock(task);
  875. mm = task->mm;
  876. if (mm)
  877. dumpable = mm->dumpable;
  878. task_unlock(task);
  879. if(dumpable == 1)
  880. return 1;
  881. return 0;
  882. }
  883. static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
  884. {
  885. struct inode * inode;
  886. struct proc_inode *ei;
  887. /* We need a new inode */
  888. inode = new_inode(sb);
  889. if (!inode)
  890. goto out;
  891. /* Common stuff */
  892. ei = PROC_I(inode);
  893. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  894. inode->i_op = &proc_def_inode_operations;
  895. /*
  896. * grab the reference to task.
  897. */
  898. ei->pid = get_task_pid(task, PIDTYPE_PID);
  899. if (!ei->pid)
  900. goto out_unlock;
  901. inode->i_uid = 0;
  902. inode->i_gid = 0;
  903. if (task_dumpable(task)) {
  904. inode->i_uid = task->euid;
  905. inode->i_gid = task->egid;
  906. }
  907. security_task_to_inode(task, inode);
  908. out:
  909. return inode;
  910. out_unlock:
  911. iput(inode);
  912. return NULL;
  913. }
  914. static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
  915. {
  916. struct inode *inode = dentry->d_inode;
  917. struct task_struct *task;
  918. generic_fillattr(inode, stat);
  919. rcu_read_lock();
  920. stat->uid = 0;
  921. stat->gid = 0;
  922. task = pid_task(proc_pid(inode), PIDTYPE_PID);
  923. if (task) {
  924. if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
  925. task_dumpable(task)) {
  926. stat->uid = task->euid;
  927. stat->gid = task->egid;
  928. }
  929. }
  930. rcu_read_unlock();
  931. return 0;
  932. }
  933. /* dentry stuff */
  934. /*
  935. * Exceptional case: normally we are not allowed to unhash a busy
  936. * directory. In this case, however, we can do it - no aliasing problems
  937. * due to the way we treat inodes.
  938. *
  939. * Rewrite the inode's ownerships here because the owning task may have
  940. * performed a setuid(), etc.
  941. *
  942. * Before the /proc/pid/status file was created the only way to read
  943. * the effective uid of a /process was to stat /proc/pid. Reading
  944. * /proc/pid/status is slow enough that procps and other packages
  945. * kept stating /proc/pid. To keep the rules in /proc simple I have
  946. * made this apply to all per process world readable and executable
  947. * directories.
  948. */
  949. static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
  950. {
  951. struct inode *inode = dentry->d_inode;
  952. struct task_struct *task = get_proc_task(inode);
  953. if (task) {
  954. if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
  955. task_dumpable(task)) {
  956. inode->i_uid = task->euid;
  957. inode->i_gid = task->egid;
  958. } else {
  959. inode->i_uid = 0;
  960. inode->i_gid = 0;
  961. }
  962. inode->i_mode &= ~(S_ISUID | S_ISGID);
  963. security_task_to_inode(task, inode);
  964. put_task_struct(task);
  965. return 1;
  966. }
  967. d_drop(dentry);
  968. return 0;
  969. }
  970. static int pid_delete_dentry(struct dentry * dentry)
  971. {
  972. /* Is the task we represent dead?
  973. * If so, then don't put the dentry on the lru list,
  974. * kill it immediately.
  975. */
  976. return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
  977. }
  978. static struct dentry_operations pid_dentry_operations =
  979. {
  980. .d_revalidate = pid_revalidate,
  981. .d_delete = pid_delete_dentry,
  982. };
  983. /* Lookups */
  984. typedef struct dentry *instantiate_t(struct inode *, struct dentry *,
  985. struct task_struct *, const void *);
  986. /*
  987. * Fill a directory entry.
  988. *
  989. * If possible create the dcache entry and derive our inode number and
  990. * file type from dcache entry.
  991. *
  992. * Since all of the proc inode numbers are dynamically generated, the inode
  993. * numbers do not exist until the inode is cache. This means creating the
  994. * the dcache entry in readdir is necessary to keep the inode numbers
  995. * reported by readdir in sync with the inode numbers reported
  996. * by stat.
  997. */
  998. static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
  999. char *name, int len,
  1000. instantiate_t instantiate, struct task_struct *task, const void *ptr)
  1001. {
  1002. struct dentry *child, *dir = filp->f_path.dentry;
  1003. struct inode *inode;
  1004. struct qstr qname;
  1005. ino_t ino = 0;
  1006. unsigned type = DT_UNKNOWN;
  1007. qname.name = name;
  1008. qname.len = len;
  1009. qname.hash = full_name_hash(name, len);
  1010. child = d_lookup(dir, &qname);
  1011. if (!child) {
  1012. struct dentry *new;
  1013. new = d_alloc(dir, &qname);
  1014. if (new) {
  1015. child = instantiate(dir->d_inode, new, task, ptr);
  1016. if (child)
  1017. dput(new);
  1018. else
  1019. child = new;
  1020. }
  1021. }
  1022. if (!child || IS_ERR(child) || !child->d_inode)
  1023. goto end_instantiate;
  1024. inode = child->d_inode;
  1025. if (inode) {
  1026. ino = inode->i_ino;
  1027. type = inode->i_mode >> 12;
  1028. }
  1029. dput(child);
  1030. end_instantiate:
  1031. if (!ino)
  1032. ino = find_inode_number(dir, &qname);
  1033. if (!ino)
  1034. ino = 1;
  1035. return filldir(dirent, name, len, filp->f_pos, ino, type);
  1036. }
  1037. static unsigned name_to_int(struct dentry *dentry)
  1038. {
  1039. const char *name = dentry->d_name.name;
  1040. int len = dentry->d_name.len;
  1041. unsigned n = 0;
  1042. if (len > 1 && *name == '0')
  1043. goto out;
  1044. while (len-- > 0) {
  1045. unsigned c = *name++ - '0';
  1046. if (c > 9)
  1047. goto out;
  1048. if (n >= (~0U-9)/10)
  1049. goto out;
  1050. n *= 10;
  1051. n += c;
  1052. }
  1053. return n;
  1054. out:
  1055. return ~0U;
  1056. }
  1057. #define PROC_FDINFO_MAX 64
  1058. static int proc_fd_info(struct inode *inode, struct dentry **dentry,
  1059. struct vfsmount **mnt, char *info)
  1060. {
  1061. struct task_struct *task = get_proc_task(inode);
  1062. struct files_struct *files = NULL;
  1063. struct file *file;
  1064. int fd = proc_fd(inode);
  1065. if (task) {
  1066. files = get_files_struct(task);
  1067. put_task_struct(task);
  1068. }
  1069. if (files) {
  1070. /*
  1071. * We are not taking a ref to the file structure, so we must
  1072. * hold ->file_lock.
  1073. */
  1074. spin_lock(&files->file_lock);
  1075. file = fcheck_files(files, fd);
  1076. if (file) {
  1077. if (mnt)
  1078. *mnt = mntget(file->f_path.mnt);
  1079. if (dentry)
  1080. *dentry = dget(file->f_path.dentry);
  1081. if (info)
  1082. snprintf(info, PROC_FDINFO_MAX,
  1083. "pos:\t%lli\n"
  1084. "flags:\t0%o\n",
  1085. (long long) file->f_pos,
  1086. file->f_flags);
  1087. spin_unlock(&files->file_lock);
  1088. put_files_struct(files);
  1089. return 0;
  1090. }
  1091. spin_unlock(&files->file_lock);
  1092. put_files_struct(files);
  1093. }
  1094. return -ENOENT;
  1095. }
  1096. static int proc_fd_link(struct inode *inode, struct dentry **dentry,
  1097. struct vfsmount **mnt)
  1098. {
  1099. return proc_fd_info(inode, dentry, mnt, NULL);
  1100. }
  1101. static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
  1102. {
  1103. struct inode *inode = dentry->d_inode;
  1104. struct task_struct *task = get_proc_task(inode);
  1105. int fd = proc_fd(inode);
  1106. struct files_struct *files;
  1107. if (task) {
  1108. files = get_files_struct(task);
  1109. if (files) {
  1110. rcu_read_lock();
  1111. if (fcheck_files(files, fd)) {
  1112. rcu_read_unlock();
  1113. put_files_struct(files);
  1114. if (task_dumpable(task)) {
  1115. inode->i_uid = task->euid;
  1116. inode->i_gid = task->egid;
  1117. } else {
  1118. inode->i_uid = 0;
  1119. inode->i_gid = 0;
  1120. }
  1121. inode->i_mode &= ~(S_ISUID | S_ISGID);
  1122. security_task_to_inode(task, inode);
  1123. put_task_struct(task);
  1124. return 1;
  1125. }
  1126. rcu_read_unlock();
  1127. put_files_struct(files);
  1128. }
  1129. put_task_struct(task);
  1130. }
  1131. d_drop(dentry);
  1132. return 0;
  1133. }
  1134. static struct dentry_operations tid_fd_dentry_operations =
  1135. {
  1136. .d_revalidate = tid_fd_revalidate,
  1137. .d_delete = pid_delete_dentry,
  1138. };
  1139. static struct dentry *proc_fd_instantiate(struct inode *dir,
  1140. struct dentry *dentry, struct task_struct *task, const void *ptr)
  1141. {
  1142. unsigned fd = *(const unsigned *)ptr;
  1143. struct file *file;
  1144. struct files_struct *files;
  1145. struct inode *inode;
  1146. struct proc_inode *ei;
  1147. struct dentry *error = ERR_PTR(-ENOENT);
  1148. inode = proc_pid_make_inode(dir->i_sb, task);
  1149. if (!inode)
  1150. goto out;
  1151. ei = PROC_I(inode);
  1152. ei->fd = fd;
  1153. files = get_files_struct(task);
  1154. if (!files)
  1155. goto out_iput;
  1156. inode->i_mode = S_IFLNK;
  1157. /*
  1158. * We are not taking a ref to the file structure, so we must
  1159. * hold ->file_lock.
  1160. */
  1161. spin_lock(&files->file_lock);
  1162. file = fcheck_files(files, fd);
  1163. if (!file)
  1164. goto out_unlock;
  1165. if (file->f_mode & 1)
  1166. inode->i_mode |= S_IRUSR | S_IXUSR;
  1167. if (file->f_mode & 2)
  1168. inode->i_mode |= S_IWUSR | S_IXUSR;
  1169. spin_unlock(&files->file_lock);
  1170. put_files_struct(files);
  1171. inode->i_op = &proc_pid_link_inode_operations;
  1172. inode->i_size = 64;
  1173. ei->op.proc_get_link = proc_fd_link;
  1174. dentry->d_op = &tid_fd_dentry_operations;
  1175. d_add(dentry, inode);
  1176. /* Close the race of the process dying before we return the dentry */
  1177. if (tid_fd_revalidate(dentry, NULL))
  1178. error = NULL;
  1179. out:
  1180. return error;
  1181. out_unlock:
  1182. spin_unlock(&files->file_lock);
  1183. put_files_struct(files);
  1184. out_iput:
  1185. iput(inode);
  1186. goto out;
  1187. }
  1188. static struct dentry *proc_lookupfd_common(struct inode *dir,
  1189. struct dentry *dentry,
  1190. instantiate_t instantiate)
  1191. {
  1192. struct task_struct *task = get_proc_task(dir);
  1193. unsigned fd = name_to_int(dentry);
  1194. struct dentry *result = ERR_PTR(-ENOENT);
  1195. if (!task)
  1196. goto out_no_task;
  1197. if (fd == ~0U)
  1198. goto out;
  1199. result = instantiate(dir, dentry, task, &fd);
  1200. out:
  1201. put_task_struct(task);
  1202. out_no_task:
  1203. return result;
  1204. }
  1205. static int proc_readfd_common(struct file * filp, void * dirent,
  1206. filldir_t filldir, instantiate_t instantiate)
  1207. {
  1208. struct dentry *dentry = filp->f_path.dentry;
  1209. struct inode *inode = dentry->d_inode;
  1210. struct task_struct *p = get_proc_task(inode);
  1211. unsigned int fd, tid, ino;
  1212. int retval;
  1213. struct files_struct * files;
  1214. struct fdtable *fdt;
  1215. retval = -ENOENT;
  1216. if (!p)
  1217. goto out_no_task;
  1218. retval = 0;
  1219. tid = p->pid;
  1220. fd = filp->f_pos;
  1221. switch (fd) {
  1222. case 0:
  1223. if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
  1224. goto out;
  1225. filp->f_pos++;
  1226. case 1:
  1227. ino = parent_ino(dentry);
  1228. if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
  1229. goto out;
  1230. filp->f_pos++;
  1231. default:
  1232. files = get_files_struct(p);
  1233. if (!files)
  1234. goto out;
  1235. rcu_read_lock();
  1236. fdt = files_fdtable(files);
  1237. for (fd = filp->f_pos-2;
  1238. fd < fdt->max_fds;
  1239. fd++, filp->f_pos++) {
  1240. char name[PROC_NUMBUF];
  1241. int len;
  1242. if (!fcheck_files(files, fd))
  1243. continue;
  1244. rcu_read_unlock();
  1245. len = snprintf(name, sizeof(name), "%d", fd);
  1246. if (proc_fill_cache(filp, dirent, filldir,
  1247. name, len, instantiate,
  1248. p, &fd) < 0) {
  1249. rcu_read_lock();
  1250. break;
  1251. }
  1252. rcu_read_lock();
  1253. }
  1254. rcu_read_unlock();
  1255. put_files_struct(files);
  1256. }
  1257. out:
  1258. put_task_struct(p);
  1259. out_no_task:
  1260. return retval;
  1261. }
  1262. static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
  1263. struct nameidata *nd)
  1264. {
  1265. return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
  1266. }
  1267. static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
  1268. {
  1269. return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
  1270. }
  1271. static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
  1272. size_t len, loff_t *ppos)
  1273. {
  1274. char tmp[PROC_FDINFO_MAX];
  1275. int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, NULL, tmp);
  1276. if (!err)
  1277. err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
  1278. return err;
  1279. }
  1280. static const struct file_operations proc_fdinfo_file_operations = {
  1281. .open = nonseekable_open,
  1282. .read = proc_fdinfo_read,
  1283. };
  1284. static const struct file_operations proc_fd_operations = {
  1285. .read = generic_read_dir,
  1286. .readdir = proc_readfd,
  1287. };
  1288. /*
  1289. * /proc/pid/fd needs a special permission handler so that a process can still
  1290. * access /proc/self/fd after it has executed a setuid().
  1291. */
  1292. static int proc_fd_permission(struct inode *inode, int mask,
  1293. struct nameidata *nd)
  1294. {
  1295. int rv;
  1296. rv = generic_permission(inode, mask, NULL);
  1297. if (rv == 0)
  1298. return 0;
  1299. if (task_pid(current) == proc_pid(inode))
  1300. rv = 0;
  1301. return rv;
  1302. }
  1303. /*
  1304. * proc directories can do almost nothing..
  1305. */
  1306. static const struct inode_operations proc_fd_inode_operations = {
  1307. .lookup = proc_lookupfd,
  1308. .permission = proc_fd_permission,
  1309. .setattr = proc_setattr,
  1310. };
  1311. static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
  1312. struct dentry *dentry, struct task_struct *task, const void *ptr)
  1313. {
  1314. unsigned fd = *(unsigned *)ptr;
  1315. struct inode *inode;
  1316. struct proc_inode *ei;
  1317. struct dentry *error = ERR_PTR(-ENOENT);
  1318. inode = proc_pid_make_inode(dir->i_sb, task);
  1319. if (!inode)
  1320. goto out;
  1321. ei = PROC_I(inode);
  1322. ei->fd = fd;
  1323. inode->i_mode = S_IFREG | S_IRUSR;
  1324. inode->i_fop = &proc_fdinfo_file_operations;
  1325. dentry->d_op = &tid_fd_dentry_operations;
  1326. d_add(dentry, inode);
  1327. /* Close the race of the process dying before we return the dentry */
  1328. if (tid_fd_revalidate(dentry, NULL))
  1329. error = NULL;
  1330. out:
  1331. return error;
  1332. }
  1333. static struct dentry *proc_lookupfdinfo(struct inode *dir,
  1334. struct dentry *dentry,
  1335. struct nameidata *nd)
  1336. {
  1337. return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
  1338. }
  1339. static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
  1340. {
  1341. return proc_readfd_common(filp, dirent, filldir,
  1342. proc_fdinfo_instantiate);
  1343. }
  1344. static const struct file_operations proc_fdinfo_operations = {
  1345. .read = generic_read_dir,
  1346. .readdir = proc_readfdinfo,
  1347. };
  1348. /*
  1349. * proc directories can do almost nothing..
  1350. */
  1351. static const struct inode_operations proc_fdinfo_inode_operations = {
  1352. .lookup = proc_lookupfdinfo,
  1353. .setattr = proc_setattr,
  1354. };
  1355. static struct dentry *proc_pident_instantiate(struct inode *dir,
  1356. struct dentry *dentry, struct task_struct *task, const void *ptr)
  1357. {
  1358. const struct pid_entry *p = ptr;
  1359. struct inode *inode;
  1360. struct proc_inode *ei;
  1361. struct dentry *error = ERR_PTR(-EINVAL);
  1362. inode = proc_pid_make_inode(dir->i_sb, task);
  1363. if (!inode)
  1364. goto out;
  1365. ei = PROC_I(inode);
  1366. inode->i_mode = p->mode;
  1367. if (S_ISDIR(inode->i_mode))
  1368. inode->i_nlink = 2; /* Use getattr to fix if necessary */
  1369. if (p->iop)
  1370. inode->i_op = p->iop;
  1371. if (p->fop)
  1372. inode->i_fop = p->fop;
  1373. ei->op = p->op;
  1374. dentry->d_op = &pid_dentry_operations;
  1375. d_add(dentry, inode);
  1376. /* Close the race of the process dying before we return the dentry */
  1377. if (pid_revalidate(dentry, NULL))
  1378. error = NULL;
  1379. out:
  1380. return error;
  1381. }
  1382. static struct dentry *proc_pident_lookup(struct inode *dir,
  1383. struct dentry *dentry,
  1384. const struct pid_entry *ents,
  1385. unsigned int nents)
  1386. {
  1387. struct inode *inode;
  1388. struct dentry *error;
  1389. struct task_struct *task = get_proc_task(dir);
  1390. const struct pid_entry *p, *last;
  1391. error = ERR_PTR(-ENOENT);
  1392. inode = NULL;
  1393. if (!task)
  1394. goto out_no_task;
  1395. /*
  1396. * Yes, it does not scale. And it should not. Don't add
  1397. * new entries into /proc/<tgid>/ without very good reasons.
  1398. */
  1399. last = &ents[nents - 1];
  1400. for (p = ents; p <= last; p++) {
  1401. if (p->len != dentry->d_name.len)
  1402. continue;
  1403. if (!memcmp(dentry->d_name.name, p->name, p->len))
  1404. break;
  1405. }
  1406. if (p > last)
  1407. goto out;
  1408. error = proc_pident_instantiate(dir, dentry, task, p);
  1409. out:
  1410. put_task_struct(task);
  1411. out_no_task:
  1412. return error;
  1413. }
  1414. static int proc_pident_fill_cache(struct file *filp, void *dirent,
  1415. filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
  1416. {
  1417. return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
  1418. proc_pident_instantiate, task, p);
  1419. }
  1420. static int proc_pident_readdir(struct file *filp,
  1421. void *dirent, filldir_t filldir,
  1422. const struct pid_entry *ents, unsigned int nents)
  1423. {
  1424. int i;
  1425. int pid;
  1426. struct dentry *dentry = filp->f_path.dentry;
  1427. struct inode *inode = dentry->d_inode;
  1428. struct task_struct *task = get_proc_task(inode);
  1429. const struct pid_entry *p, *last;
  1430. ino_t ino;
  1431. int ret;
  1432. ret = -ENOENT;
  1433. if (!task)
  1434. goto out_no_task;
  1435. ret = 0;
  1436. pid = task->pid;
  1437. i = filp->f_pos;
  1438. switch (i) {
  1439. case 0:
  1440. ino = inode->i_ino;
  1441. if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
  1442. goto out;
  1443. i++;
  1444. filp->f_pos++;
  1445. /* fall through */
  1446. case 1:
  1447. ino = parent_ino(dentry);
  1448. if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
  1449. goto out;
  1450. i++;
  1451. filp->f_pos++;
  1452. /* fall through */
  1453. default:
  1454. i -= 2;
  1455. if (i >= nents) {
  1456. ret = 1;
  1457. goto out;
  1458. }
  1459. p = ents + i;
  1460. last = &ents[nents - 1];
  1461. while (p <= last) {
  1462. if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
  1463. goto out;
  1464. filp->f_pos++;
  1465. p++;
  1466. }
  1467. }
  1468. ret = 1;
  1469. out:
  1470. put_task_struct(task);
  1471. out_no_task:
  1472. return ret;
  1473. }
  1474. #ifdef CONFIG_SECURITY
  1475. static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
  1476. size_t count, loff_t *ppos)
  1477. {
  1478. struct inode * inode = file->f_path.dentry->d_inode;
  1479. char *p = NULL;
  1480. ssize_t length;
  1481. struct task_struct *task = get_proc_task(inode);
  1482. if (!task)
  1483. return -ESRCH;
  1484. length = security_getprocattr(task,
  1485. (char*)file->f_path.dentry->d_name.name,
  1486. &p);
  1487. put_task_struct(task);
  1488. if (length > 0)
  1489. length = simple_read_from_buffer(buf, count, ppos, p, length);
  1490. kfree(p);
  1491. return length;
  1492. }
  1493. static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
  1494. size_t count, loff_t *ppos)
  1495. {
  1496. struct inode * inode = file->f_path.dentry->d_inode;
  1497. char *page;
  1498. ssize_t length;
  1499. struct task_struct *task = get_proc_task(inode);
  1500. length = -ESRCH;
  1501. if (!task)
  1502. goto out_no_task;
  1503. if (count > PAGE_SIZE)
  1504. count = PAGE_SIZE;
  1505. /* No partial writes. */
  1506. length = -EINVAL;
  1507. if (*ppos != 0)
  1508. goto out;
  1509. length = -ENOMEM;
  1510. page = (char*)__get_free_page(GFP_USER);
  1511. if (!page)
  1512. goto out;
  1513. length = -EFAULT;
  1514. if (copy_from_user(page, buf, count))
  1515. goto out_free;
  1516. length = security_setprocattr(task,
  1517. (char*)file->f_path.dentry->d_name.name,
  1518. (void*)page, count);
  1519. out_free:
  1520. free_page((unsigned long) page);
  1521. out:
  1522. put_task_struct(task);
  1523. out_no_task:
  1524. return length;
  1525. }
  1526. static const struct file_operations proc_pid_attr_operations = {
  1527. .read = proc_pid_attr_read,
  1528. .write = proc_pid_attr_write,
  1529. };
  1530. static const struct pid_entry attr_dir_stuff[] = {
  1531. REG("current", S_IRUGO|S_IWUGO, pid_attr),
  1532. REG("prev", S_IRUGO, pid_attr),
  1533. REG("exec", S_IRUGO|S_IWUGO, pid_attr),
  1534. REG("fscreate", S_IRUGO|S_IWUGO, pid_attr),
  1535. REG("keycreate", S_IRUGO|S_IWUGO, pid_attr),
  1536. REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr),
  1537. };
  1538. static int proc_attr_dir_readdir(struct file * filp,
  1539. void * dirent, filldir_t filldir)
  1540. {
  1541. return proc_pident_readdir(filp,dirent,filldir,
  1542. attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
  1543. }
  1544. static const struct file_operations proc_attr_dir_operations = {
  1545. .read = generic_read_dir,
  1546. .readdir = proc_attr_dir_readdir,
  1547. };
  1548. static struct dentry *proc_attr_dir_lookup(struct inode *dir,
  1549. struct dentry *dentry, struct nameidata *nd)
  1550. {
  1551. return proc_pident_lookup(dir, dentry,
  1552. attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
  1553. }
  1554. static const struct inode_operations proc_attr_dir_inode_operations = {
  1555. .lookup = proc_attr_dir_lookup,
  1556. .getattr = pid_getattr,
  1557. .setattr = proc_setattr,
  1558. };
  1559. #endif
  1560. /*
  1561. * /proc/self:
  1562. */
  1563. static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
  1564. int buflen)
  1565. {
  1566. char tmp[PROC_NUMBUF];
  1567. sprintf(tmp, "%d", current->tgid);
  1568. return vfs_readlink(dentry,buffer,buflen,tmp);
  1569. }
  1570. static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
  1571. {
  1572. char tmp[PROC_NUMBUF];
  1573. sprintf(tmp, "%d", current->tgid);
  1574. return ERR_PTR(vfs_follow_link(nd,tmp));
  1575. }
  1576. static const struct inode_operations proc_self_inode_operations = {
  1577. .readlink = proc_self_readlink,
  1578. .follow_link = proc_self_follow_link,
  1579. };
  1580. /*
  1581. * proc base
  1582. *
  1583. * These are the directory entries in the root directory of /proc
  1584. * that properly belong to the /proc filesystem, as they describe
  1585. * describe something that is process related.
  1586. */
  1587. static const struct pid_entry proc_base_stuff[] = {
  1588. NOD("self", S_IFLNK|S_IRWXUGO,
  1589. &proc_self_inode_operations, NULL, {}),
  1590. };
  1591. /*
  1592. * Exceptional case: normally we are not allowed to unhash a busy
  1593. * directory. In this case, however, we can do it - no aliasing problems
  1594. * due to the way we treat inodes.
  1595. */
  1596. static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
  1597. {
  1598. struct inode *inode = dentry->d_inode;
  1599. struct task_struct *task = get_proc_task(inode);
  1600. if (task) {
  1601. put_task_struct(task);
  1602. return 1;
  1603. }
  1604. d_drop(dentry);
  1605. return 0;
  1606. }
  1607. static struct dentry_operations proc_base_dentry_operations =
  1608. {
  1609. .d_revalidate = proc_base_revalidate,
  1610. .d_delete = pid_delete_dentry,
  1611. };
  1612. static struct dentry *proc_base_instantiate(struct inode *dir,
  1613. struct dentry *dentry, struct task_struct *task, const void *ptr)
  1614. {
  1615. const struct pid_entry *p = ptr;
  1616. struct inode *inode;
  1617. struct proc_inode *ei;
  1618. struct dentry *error = ERR_PTR(-EINVAL);
  1619. /* Allocate the inode */
  1620. error = ERR_PTR(-ENOMEM);
  1621. inode = new_inode(dir->i_sb);
  1622. if (!inode)
  1623. goto out;
  1624. /* Initialize the inode */
  1625. ei = PROC_I(inode);
  1626. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  1627. /*
  1628. * grab the reference to the task.
  1629. */
  1630. ei->pid = get_task_pid(task, PIDTYPE_PID);
  1631. if (!ei->pid)
  1632. goto out_iput;
  1633. inode->i_uid = 0;
  1634. inode->i_gid = 0;
  1635. inode->i_mode = p->mode;
  1636. if (S_ISDIR(inode->i_mode))
  1637. inode->i_nlink = 2;
  1638. if (S_ISLNK(inode->i_mode))
  1639. inode->i_size = 64;
  1640. if (p->iop)
  1641. inode->i_op = p->iop;
  1642. if (p->fop)
  1643. inode->i_fop = p->fop;
  1644. ei->op = p->op;
  1645. dentry->d_op = &proc_base_dentry_operations;
  1646. d_add(dentry, inode);
  1647. error = NULL;
  1648. out:
  1649. return error;
  1650. out_iput:
  1651. iput(inode);
  1652. goto out;
  1653. }
  1654. static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
  1655. {
  1656. struct dentry *error;
  1657. struct task_struct *task = get_proc_task(dir);
  1658. const struct pid_entry *p, *last;
  1659. error = ERR_PTR(-ENOENT);
  1660. if (!task)
  1661. goto out_no_task;
  1662. /* Lookup the directory entry */
  1663. last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
  1664. for (p = proc_base_stuff; p <= last; p++) {
  1665. if (p->len != dentry->d_name.len)
  1666. continue;
  1667. if (!memcmp(dentry->d_name.name, p->name, p->len))
  1668. break;
  1669. }
  1670. if (p > last)
  1671. goto out;
  1672. error = proc_base_instantiate(dir, dentry, task, p);
  1673. out:
  1674. put_task_struct(task);
  1675. out_no_task:
  1676. return error;
  1677. }
  1678. static int proc_base_fill_cache(struct file *filp, void *dirent,
  1679. filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
  1680. {
  1681. return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
  1682. proc_base_instantiate, task, p);
  1683. }
  1684. #ifdef CONFIG_TASK_IO_ACCOUNTING
  1685. static int proc_pid_io_accounting(struct task_struct *task, char *buffer)
  1686. {
  1687. return sprintf(buffer,
  1688. #ifdef CONFIG_TASK_XACCT
  1689. "rchar: %llu\n"
  1690. "wchar: %llu\n"
  1691. "syscr: %llu\n"
  1692. "syscw: %llu\n"
  1693. #endif
  1694. "read_bytes: %llu\n"
  1695. "write_bytes: %llu\n"
  1696. "cancelled_write_bytes: %llu\n",
  1697. #ifdef CONFIG_TASK_XACCT
  1698. (unsigned long long)task->rchar,
  1699. (unsigned long long)task->wchar,
  1700. (unsigned long long)task->syscr,
  1701. (unsigned long long)task->syscw,
  1702. #endif
  1703. (unsigned long long)task->ioac.read_bytes,
  1704. (unsigned long long)task->ioac.write_bytes,
  1705. (unsigned long long)task->ioac.cancelled_write_bytes);
  1706. }
  1707. #endif
  1708. /*
  1709. * Thread groups
  1710. */
  1711. static const struct file_operations proc_task_operations;
  1712. static const struct inode_operations proc_task_inode_operations;
  1713. static const struct pid_entry tgid_base_stuff[] = {
  1714. DIR("task", S_IRUGO|S_IXUGO, task),
  1715. DIR("fd", S_IRUSR|S_IXUSR, fd),
  1716. DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
  1717. INF("environ", S_IRUSR, pid_environ),
  1718. INF("auxv", S_IRUSR, pid_auxv),
  1719. INF("status", S_IRUGO, pid_status),
  1720. INF("cmdline", S_IRUGO, pid_cmdline),
  1721. INF("stat", S_IRUGO, tgid_stat),
  1722. INF("statm", S_IRUGO, pid_statm),
  1723. REG("maps", S_IRUGO, maps),
  1724. #ifdef CONFIG_NUMA
  1725. REG("numa_maps", S_IRUGO, numa_maps),
  1726. #endif
  1727. REG("mem", S_IRUSR|S_IWUSR, mem),
  1728. #ifdef CONFIG_SECCOMP
  1729. REG("seccomp", S_IRUSR|S_IWUSR, seccomp),
  1730. #endif
  1731. LNK("cwd", cwd),
  1732. LNK("root", root),
  1733. LNK("exe", exe),
  1734. REG("mounts", S_IRUGO, mounts),
  1735. REG("mountstats", S_IRUSR, mountstats),
  1736. #ifdef CONFIG_MMU
  1737. REG("clear_refs", S_IWUSR, clear_refs),
  1738. REG("smaps", S_IRUGO, smaps),
  1739. #endif
  1740. #ifdef CONFIG_SECURITY
  1741. DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
  1742. #endif
  1743. #ifdef CONFIG_KALLSYMS
  1744. INF("wchan", S_IRUGO, pid_wchan),
  1745. #endif
  1746. #ifdef CONFIG_SCHEDSTATS
  1747. INF("schedstat", S_IRUGO, pid_schedstat),
  1748. #endif
  1749. #ifdef CONFIG_CPUSETS
  1750. REG("cpuset", S_IRUGO, cpuset),
  1751. #endif
  1752. INF("oom_score", S_IRUGO, oom_score),
  1753. REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
  1754. #ifdef CONFIG_AUDITSYSCALL
  1755. REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
  1756. #endif
  1757. #ifdef CONFIG_FAULT_INJECTION
  1758. REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
  1759. #endif
  1760. #ifdef CONFIG_TASK_IO_ACCOUNTING
  1761. INF("io", S_IRUGO, pid_io_accounting),
  1762. #endif
  1763. };
  1764. static int proc_tgid_base_readdir(struct file * filp,
  1765. void * dirent, filldir_t filldir)
  1766. {
  1767. return proc_pident_readdir(filp,dirent,filldir,
  1768. tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
  1769. }
  1770. static const struct file_operations proc_tgid_base_operations = {
  1771. .read = generic_read_dir,
  1772. .readdir = proc_tgid_base_readdir,
  1773. };
  1774. static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
  1775. return proc_pident_lookup(dir, dentry,
  1776. tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
  1777. }
  1778. static const struct inode_operations proc_tgid_base_inode_operations = {
  1779. .lookup = proc_tgid_base_lookup,
  1780. .getattr = pid_getattr,
  1781. .setattr = proc_setattr,
  1782. };
  1783. /**
  1784. * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
  1785. *
  1786. * @task: task that should be flushed.
  1787. *
  1788. * Looks in the dcache for
  1789. * /proc/@pid
  1790. * /proc/@tgid/task/@pid
  1791. * if either directory is present flushes it and all of it'ts children
  1792. * from the dcache.
  1793. *
  1794. * It is safe and reasonable to cache /proc entries for a task until
  1795. * that task exits. After that they just clog up the dcache with
  1796. * useless entries, possibly causing useful dcache entries to be
  1797. * flushed instead. This routine is proved to flush those useless
  1798. * dcache entries at process exit time.
  1799. *
  1800. * NOTE: This routine is just an optimization so it does not guarantee
  1801. * that no dcache entries will exist at process exit time it
  1802. * just makes it very unlikely that any will persist.
  1803. */
  1804. void proc_flush_task(struct task_struct *task)
  1805. {
  1806. struct dentry *dentry, *leader, *dir;
  1807. char buf[PROC_NUMBUF];
  1808. struct qstr name;
  1809. name.name = buf;
  1810. name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
  1811. dentry = d_hash_and_lookup(proc_mnt->mnt_root, &name);
  1812. if (dentry) {
  1813. shrink_dcache_parent(dentry);
  1814. d_drop(dentry);
  1815. dput(dentry);
  1816. }
  1817. if (thread_group_leader(task))
  1818. goto out;
  1819. name.name = buf;
  1820. name.len = snprintf(buf, sizeof(buf), "%d", task->tgid);
  1821. leader = d_hash_and_lookup(proc_mnt->mnt_root, &name);
  1822. if (!leader)
  1823. goto out;
  1824. name.name = "task";
  1825. name.len = strlen(name.name);
  1826. dir = d_hash_and_lookup(leader, &name);
  1827. if (!dir)
  1828. goto out_put_leader;
  1829. name.name = buf;
  1830. name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
  1831. dentry = d_hash_and_lookup(dir, &name);
  1832. if (dentry) {
  1833. shrink_dcache_parent(dentry);
  1834. d_drop(dentry);
  1835. dput(dentry);
  1836. }
  1837. dput(dir);
  1838. out_put_leader:
  1839. dput(leader);
  1840. out:
  1841. return;
  1842. }
  1843. static struct dentry *proc_pid_instantiate(struct inode *dir,
  1844. struct dentry * dentry,
  1845. struct task_struct *task, const void *ptr)
  1846. {
  1847. struct dentry *error = ERR_PTR(-ENOENT);
  1848. struct inode *inode;
  1849. inode = proc_pid_make_inode(dir->i_sb, task);
  1850. if (!inode)
  1851. goto out;
  1852. inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
  1853. inode->i_op = &proc_tgid_base_inode_operations;
  1854. inode->i_fop = &proc_tgid_base_operations;
  1855. inode->i_flags|=S_IMMUTABLE;
  1856. inode->i_nlink = 5;
  1857. #ifdef CONFIG_SECURITY
  1858. inode->i_nlink += 1;
  1859. #endif
  1860. dentry->d_op = &pid_dentry_operations;
  1861. d_add(dentry, inode);
  1862. /* Close the race of the process dying before we return the dentry */
  1863. if (pid_revalidate(dentry, NULL))
  1864. error = NULL;
  1865. out:
  1866. return error;
  1867. }
  1868. struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
  1869. {
  1870. struct dentry *result = ERR_PTR(-ENOENT);
  1871. struct task_struct *task;
  1872. unsigned tgid;
  1873. result = proc_base_lookup(dir, dentry);
  1874. if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
  1875. goto out;
  1876. tgid = name_to_int(dentry);
  1877. if (tgid == ~0U)
  1878. goto out;
  1879. rcu_read_lock();
  1880. task = find_task_by_pid(tgid);
  1881. if (task)
  1882. get_task_struct(task);
  1883. rcu_read_unlock();
  1884. if (!task)
  1885. goto out;
  1886. result = proc_pid_instantiate(dir, dentry, task, NULL);
  1887. put_task_struct(task);
  1888. out:
  1889. return result;
  1890. }
  1891. /*
  1892. * Find the first task with tgid >= tgid
  1893. *
  1894. */
  1895. static struct task_struct *next_tgid(unsigned int tgid)
  1896. {
  1897. struct task_struct *task;
  1898. struct pid *pid;
  1899. rcu_read_lock();
  1900. retry:
  1901. task = NULL;
  1902. pid = find_ge_pid(tgid);
  1903. if (pid) {
  1904. tgid = pid->nr + 1;
  1905. task = pid_task(pid, PIDTYPE_PID);
  1906. /* What we to know is if the pid we have find is the
  1907. * pid of a thread_group_leader. Testing for task
  1908. * being a thread_group_leader is the obvious thing
  1909. * todo but there is a window when it fails, due to
  1910. * the pid transfer logic in de_thread.
  1911. *
  1912. * So we perform the straight forward test of seeing
  1913. * if the pid we have found is the pid of a thread
  1914. * group leader, and don't worry if the task we have
  1915. * found doesn't happen to be a thread group leader.
  1916. * As we don't care in the case of readdir.
  1917. */
  1918. if (!task || !has_group_leader_pid(task))
  1919. goto retry;
  1920. get_task_struct(task);
  1921. }
  1922. rcu_read_unlock();
  1923. return task;
  1924. }
  1925. #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
  1926. static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
  1927. struct task_struct *task, int tgid)
  1928. {
  1929. char name[PROC_NUMBUF];
  1930. int len = snprintf(name, sizeof(name), "%d", tgid);
  1931. return proc_fill_cache(filp, dirent, filldir, name, len,
  1932. proc_pid_instantiate, task, NULL);
  1933. }
  1934. /* for the /proc/ directory itself, after non-process stuff has been done */
  1935. int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
  1936. {
  1937. unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
  1938. struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
  1939. struct task_struct *task;
  1940. int tgid;
  1941. if (!reaper)
  1942. goto out_no_task;
  1943. for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
  1944. const struct pid_entry *p = &proc_base_stuff[nr];
  1945. if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
  1946. goto out;
  1947. }
  1948. tgid = filp->f_pos - TGID_OFFSET;
  1949. for (task = next_tgid(tgid);
  1950. task;
  1951. put_task_struct(task), task = next_tgid(tgid + 1)) {
  1952. tgid = task->pid;
  1953. filp->f_pos = tgid + TGID_OFFSET;
  1954. if (proc_pid_fill_cache(filp, dirent, filldir, task, tgid) < 0) {
  1955. put_task_struct(task);
  1956. goto out;
  1957. }
  1958. }
  1959. filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
  1960. out:
  1961. put_task_struct(reaper);
  1962. out_no_task:
  1963. return 0;
  1964. }
  1965. /*
  1966. * Tasks
  1967. */
  1968. static const struct pid_entry tid_base_stuff[] = {
  1969. DIR("fd", S_IRUSR|S_IXUSR, fd),
  1970. DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
  1971. INF("environ", S_IRUSR, pid_environ),
  1972. INF("auxv", S_IRUSR, pid_auxv),
  1973. INF("status", S_IRUGO, pid_status),
  1974. INF("cmdline", S_IRUGO, pid_cmdline),
  1975. INF("stat", S_IRUGO, tid_stat),
  1976. INF("statm", S_IRUGO, pid_statm),
  1977. REG("maps", S_IRUGO, maps),
  1978. #ifdef CONFIG_NUMA
  1979. REG("numa_maps", S_IRUGO, numa_maps),
  1980. #endif
  1981. REG("mem", S_IRUSR|S_IWUSR, mem),
  1982. #ifdef CONFIG_SECCOMP
  1983. REG("seccomp", S_IRUSR|S_IWUSR, seccomp),
  1984. #endif
  1985. LNK("cwd", cwd),
  1986. LNK("root", root),
  1987. LNK("exe", exe),
  1988. REG("mounts", S_IRUGO, mounts),
  1989. #ifdef CONFIG_MMU
  1990. REG("clear_refs", S_IWUSR, clear_refs),
  1991. REG("smaps", S_IRUGO, smaps),
  1992. #endif
  1993. #ifdef CONFIG_SECURITY
  1994. DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
  1995. #endif
  1996. #ifdef CONFIG_KALLSYMS
  1997. INF("wchan", S_IRUGO, pid_wchan),
  1998. #endif
  1999. #ifdef CONFIG_SCHEDSTATS
  2000. INF("schedstat", S_IRUGO, pid_schedstat),
  2001. #endif
  2002. #ifdef CONFIG_CPUSETS
  2003. REG("cpuset", S_IRUGO, cpuset),
  2004. #endif
  2005. INF("oom_score", S_IRUGO, oom_score),
  2006. REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
  2007. #ifdef CONFIG_AUDITSYSCALL
  2008. REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
  2009. #endif
  2010. #ifdef CONFIG_FAULT_INJECTION
  2011. REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
  2012. #endif
  2013. };
  2014. static int proc_tid_base_readdir(struct file * filp,
  2015. void * dirent, filldir_t filldir)
  2016. {
  2017. return proc_pident_readdir(filp,dirent,filldir,
  2018. tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
  2019. }
  2020. static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
  2021. return proc_pident_lookup(dir, dentry,
  2022. tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
  2023. }
  2024. static const struct file_operations proc_tid_base_operations = {
  2025. .read = generic_read_dir,
  2026. .readdir = proc_tid_base_readdir,
  2027. };
  2028. static const struct inode_operations proc_tid_base_inode_operations = {
  2029. .lookup = proc_tid_base_lookup,
  2030. .getattr = pid_getattr,
  2031. .setattr = proc_setattr,
  2032. };
  2033. static struct dentry *proc_task_instantiate(struct inode *dir,
  2034. struct dentry *dentry, struct task_struct *task, const void *ptr)
  2035. {
  2036. struct dentry *error = ERR_PTR(-ENOENT);
  2037. struct inode *inode;
  2038. inode = proc_pid_make_inode(dir->i_sb, task);
  2039. if (!inode)
  2040. goto out;
  2041. inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
  2042. inode->i_op = &proc_tid_base_inode_operations;
  2043. inode->i_fop = &proc_tid_base_operations;
  2044. inode->i_flags|=S_IMMUTABLE;
  2045. inode->i_nlink = 4;
  2046. #ifdef CONFIG_SECURITY
  2047. inode->i_nlink += 1;
  2048. #endif
  2049. dentry->d_op = &pid_dentry_operations;
  2050. d_add(dentry, inode);
  2051. /* Close the race of the process dying before we return the dentry */
  2052. if (pid_revalidate(dentry, NULL))
  2053. error = NULL;
  2054. out:
  2055. return error;
  2056. }
  2057. static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
  2058. {
  2059. struct dentry *result = ERR_PTR(-ENOENT);
  2060. struct task_struct *task;
  2061. struct task_struct *leader = get_proc_task(dir);
  2062. unsigned tid;
  2063. if (!leader)
  2064. goto out_no_task;
  2065. tid = name_to_int(dentry);
  2066. if (tid == ~0U)
  2067. goto out;
  2068. rcu_read_lock();
  2069. task = find_task_by_pid(tid);
  2070. if (task)
  2071. get_task_struct(task);
  2072. rcu_read_unlock();
  2073. if (!task)
  2074. goto out;
  2075. if (leader->tgid != task->tgid)
  2076. goto out_drop_task;
  2077. result = proc_task_instantiate(dir, dentry, task, NULL);
  2078. out_drop_task:
  2079. put_task_struct(task);
  2080. out:
  2081. put_task_struct(leader);
  2082. out_no_task:
  2083. return result;
  2084. }
  2085. /*
  2086. * Find the first tid of a thread group to return to user space.
  2087. *
  2088. * Usually this is just the thread group leader, but if the users
  2089. * buffer was too small or there was a seek into the middle of the
  2090. * directory we have more work todo.
  2091. *
  2092. * In the case of a short read we start with find_task_by_pid.
  2093. *
  2094. * In the case of a seek we start with the leader and walk nr
  2095. * threads past it.
  2096. */
  2097. static struct task_struct *first_tid(struct task_struct *leader,
  2098. int tid, int nr)
  2099. {
  2100. struct task_struct *pos;
  2101. rcu_read_lock();
  2102. /* Attempt to start with the pid of a thread */
  2103. if (tid && (nr > 0)) {
  2104. pos = find_task_by_pid(tid);
  2105. if (pos && (pos->group_leader == leader))
  2106. goto found;
  2107. }
  2108. /* If nr exceeds the number of threads there is nothing todo */
  2109. pos = NULL;
  2110. if (nr && nr >= get_nr_threads(leader))
  2111. goto out;
  2112. /* If we haven't found our starting place yet start
  2113. * with the leader and walk nr threads forward.
  2114. */
  2115. for (pos = leader; nr > 0; --nr) {
  2116. pos = next_thread(pos);
  2117. if (pos == leader) {
  2118. pos = NULL;
  2119. goto out;
  2120. }
  2121. }
  2122. found:
  2123. get_task_struct(pos);
  2124. out:
  2125. rcu_read_unlock();
  2126. return pos;
  2127. }
  2128. /*
  2129. * Find the next thread in the thread list.
  2130. * Return NULL if there is an error or no next thread.
  2131. *
  2132. * The reference to the input task_struct is released.
  2133. */
  2134. static struct task_struct *next_tid(struct task_struct *start)
  2135. {
  2136. struct task_struct *pos = NULL;
  2137. rcu_read_lock();
  2138. if (pid_alive(start)) {
  2139. pos = next_thread(start);
  2140. if (thread_group_leader(pos))
  2141. pos = NULL;
  2142. else
  2143. get_task_struct(pos);
  2144. }
  2145. rcu_read_unlock();
  2146. put_task_struct(start);
  2147. return pos;
  2148. }
  2149. static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
  2150. struct task_struct *task, int tid)
  2151. {
  2152. char name[PROC_NUMBUF];
  2153. int len = snprintf(name, sizeof(name), "%d", tid);
  2154. return proc_fill_cache(filp, dirent, filldir, name, len,
  2155. proc_task_instantiate, task, NULL);
  2156. }
  2157. /* for the /proc/TGID/task/ directories */
  2158. static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
  2159. {
  2160. struct dentry *dentry = filp->f_path.dentry;
  2161. struct inode *inode = dentry->d_inode;
  2162. struct task_struct *leader = NULL;
  2163. struct task_struct *task;
  2164. int retval = -ENOENT;
  2165. ino_t ino;
  2166. int tid;
  2167. unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
  2168. task = get_proc_task(inode);
  2169. if (!task)
  2170. goto out_no_task;
  2171. rcu_read_lock();
  2172. if (pid_alive(task)) {
  2173. leader = task->group_leader;
  2174. get_task_struct(leader);
  2175. }
  2176. rcu_read_unlock();
  2177. put_task_struct(task);
  2178. if (!leader)
  2179. goto out_no_task;
  2180. retval = 0;
  2181. switch (pos) {
  2182. case 0:
  2183. ino = inode->i_ino;
  2184. if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
  2185. goto out;
  2186. pos++;
  2187. /* fall through */
  2188. case 1:
  2189. ino = parent_ino(dentry);
  2190. if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
  2191. goto out;
  2192. pos++;
  2193. /* fall through */
  2194. }
  2195. /* f_version caches the tgid value that the last readdir call couldn't
  2196. * return. lseek aka telldir automagically resets f_version to 0.
  2197. */
  2198. tid = filp->f_version;
  2199. filp->f_version = 0;
  2200. for (task = first_tid(leader, tid, pos - 2);
  2201. task;
  2202. task = next_tid(task), pos++) {
  2203. tid = task->pid;
  2204. if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
  2205. /* returning this tgid failed, save it as the first
  2206. * pid for the next readir call */
  2207. filp->f_version = tid;
  2208. put_task_struct(task);
  2209. break;
  2210. }
  2211. }
  2212. out:
  2213. filp->f_pos = pos;
  2214. put_task_struct(leader);
  2215. out_no_task:
  2216. return retval;
  2217. }
  2218. static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
  2219. {
  2220. struct inode *inode = dentry->d_inode;
  2221. struct task_struct *p = get_proc_task(inode);
  2222. generic_fillattr(inode, stat);
  2223. if (p) {
  2224. rcu_read_lock();
  2225. stat->nlink += get_nr_threads(p);
  2226. rcu_read_unlock();
  2227. put_task_struct(p);
  2228. }
  2229. return 0;
  2230. }
  2231. static const struct inode_operations proc_task_inode_operations = {
  2232. .lookup = proc_task_lookup,
  2233. .getattr = proc_task_getattr,
  2234. .setattr = proc_setattr,
  2235. };
  2236. static const struct file_operations proc_task_operations = {
  2237. .read = generic_read_dir,
  2238. .readdir = proc_task_readdir,
  2239. };