proc_sysctl.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346
  1. /*
  2. * /proc/sys support
  3. */
  4. #include <linux/init.h>
  5. #include <linux/sysctl.h>
  6. #include <linux/poll.h>
  7. #include <linux/proc_fs.h>
  8. #include <linux/security.h>
  9. #include <linux/namei.h>
  10. #include <linux/module.h>
  11. #include "internal.h"
  12. static const struct dentry_operations proc_sys_dentry_operations;
  13. static const struct file_operations proc_sys_file_operations;
  14. static const struct inode_operations proc_sys_inode_operations;
  15. static const struct file_operations proc_sys_dir_file_operations;
  16. static const struct inode_operations proc_sys_dir_operations;
  17. void proc_sys_poll_notify(struct ctl_table_poll *poll)
  18. {
  19. if (!poll)
  20. return;
  21. atomic_inc(&poll->event);
  22. wake_up_interruptible(&poll->wait);
  23. }
  24. static struct ctl_table root_table[] = {
  25. {
  26. .procname = "",
  27. .mode = S_IRUGO|S_IXUGO,
  28. .child = &root_table[1],
  29. },
  30. { }
  31. };
  32. static struct ctl_table_root sysctl_table_root;
  33. static struct ctl_table_header root_table_header = {
  34. {{.count = 1,
  35. .nreg = 1,
  36. .ctl_table = root_table,
  37. .ctl_entry = LIST_HEAD_INIT(sysctl_table_root.default_set.list),}},
  38. .root = &sysctl_table_root,
  39. .set = &sysctl_table_root.default_set,
  40. };
  41. static struct ctl_table_root sysctl_table_root = {
  42. .root_list = LIST_HEAD_INIT(sysctl_table_root.root_list),
  43. .default_set.list = LIST_HEAD_INIT(root_table_header.ctl_entry),
  44. };
  45. static DEFINE_SPINLOCK(sysctl_lock);
  46. static int namecmp(const char *name1, int len1, const char *name2, int len2)
  47. {
  48. int minlen;
  49. int cmp;
  50. minlen = len1;
  51. if (minlen > len2)
  52. minlen = len2;
  53. cmp = memcmp(name1, name2, minlen);
  54. if (cmp == 0)
  55. cmp = len1 - len2;
  56. return cmp;
  57. }
  58. static struct ctl_table *find_entry(struct ctl_table_header **phead,
  59. struct ctl_table_set *set,
  60. struct ctl_table_header *dir_head, struct ctl_table *dir,
  61. const char *name, int namelen)
  62. {
  63. struct ctl_table_header *head;
  64. struct ctl_table *entry;
  65. if (dir_head->set == set) {
  66. for (entry = dir; entry->procname; entry++) {
  67. const char *procname = entry->procname;
  68. if (namecmp(procname, strlen(procname), name, namelen) == 0) {
  69. *phead = dir_head;
  70. return entry;
  71. }
  72. }
  73. }
  74. list_for_each_entry(head, &set->list, ctl_entry) {
  75. if (head->unregistering)
  76. continue;
  77. if (head->attached_to != dir)
  78. continue;
  79. for (entry = head->attached_by; entry->procname; entry++) {
  80. const char *procname = entry->procname;
  81. if (namecmp(procname, strlen(procname), name, namelen) == 0) {
  82. *phead = head;
  83. return entry;
  84. }
  85. }
  86. }
  87. return NULL;
  88. }
  89. static void init_header(struct ctl_table_header *head,
  90. struct ctl_table_root *root, struct ctl_table_set *set,
  91. struct ctl_table *table)
  92. {
  93. head->ctl_table_arg = table;
  94. INIT_LIST_HEAD(&head->ctl_entry);
  95. head->used = 0;
  96. head->count = 1;
  97. head->nreg = 1;
  98. head->unregistering = NULL;
  99. head->root = root;
  100. head->set = set;
  101. head->parent = NULL;
  102. }
  103. static void erase_header(struct ctl_table_header *head)
  104. {
  105. list_del_init(&head->ctl_entry);
  106. }
  107. static void insert_header(struct ctl_table_header *header)
  108. {
  109. header->parent->count++;
  110. list_add_tail(&header->ctl_entry, &header->set->list);
  111. }
  112. /* called under sysctl_lock */
  113. static int use_table(struct ctl_table_header *p)
  114. {
  115. if (unlikely(p->unregistering))
  116. return 0;
  117. p->used++;
  118. return 1;
  119. }
  120. /* called under sysctl_lock */
  121. static void unuse_table(struct ctl_table_header *p)
  122. {
  123. if (!--p->used)
  124. if (unlikely(p->unregistering))
  125. complete(p->unregistering);
  126. }
  127. /* called under sysctl_lock, will reacquire if has to wait */
  128. static void start_unregistering(struct ctl_table_header *p)
  129. {
  130. /*
  131. * if p->used is 0, nobody will ever touch that entry again;
  132. * we'll eliminate all paths to it before dropping sysctl_lock
  133. */
  134. if (unlikely(p->used)) {
  135. struct completion wait;
  136. init_completion(&wait);
  137. p->unregistering = &wait;
  138. spin_unlock(&sysctl_lock);
  139. wait_for_completion(&wait);
  140. spin_lock(&sysctl_lock);
  141. } else {
  142. /* anything non-NULL; we'll never dereference it */
  143. p->unregistering = ERR_PTR(-EINVAL);
  144. }
  145. /*
  146. * do not remove from the list until nobody holds it; walking the
  147. * list in do_sysctl() relies on that.
  148. */
  149. erase_header(p);
  150. }
  151. static void sysctl_head_get(struct ctl_table_header *head)
  152. {
  153. spin_lock(&sysctl_lock);
  154. head->count++;
  155. spin_unlock(&sysctl_lock);
  156. }
  157. void sysctl_head_put(struct ctl_table_header *head)
  158. {
  159. spin_lock(&sysctl_lock);
  160. if (!--head->count)
  161. kfree_rcu(head, rcu);
  162. spin_unlock(&sysctl_lock);
  163. }
  164. static struct ctl_table_header *sysctl_head_grab(struct ctl_table_header *head)
  165. {
  166. if (!head)
  167. BUG();
  168. spin_lock(&sysctl_lock);
  169. if (!use_table(head))
  170. head = ERR_PTR(-ENOENT);
  171. spin_unlock(&sysctl_lock);
  172. return head;
  173. }
  174. static void sysctl_head_finish(struct ctl_table_header *head)
  175. {
  176. if (!head)
  177. return;
  178. spin_lock(&sysctl_lock);
  179. unuse_table(head);
  180. spin_unlock(&sysctl_lock);
  181. }
  182. static struct ctl_table_set *
  183. lookup_header_set(struct ctl_table_root *root, struct nsproxy *namespaces)
  184. {
  185. struct ctl_table_set *set = &root->default_set;
  186. if (root->lookup)
  187. set = root->lookup(root, namespaces);
  188. return set;
  189. }
  190. static struct list_head *
  191. lookup_header_list(struct ctl_table_root *root, struct nsproxy *namespaces)
  192. {
  193. struct ctl_table_set *set = lookup_header_set(root, namespaces);
  194. return &set->list;
  195. }
  196. static struct ctl_table *lookup_entry(struct ctl_table_header **phead,
  197. struct ctl_table_header *dir_head,
  198. struct ctl_table *dir,
  199. const char *name, int namelen)
  200. {
  201. struct ctl_table_header *head;
  202. struct ctl_table *entry;
  203. struct ctl_table_root *root;
  204. struct ctl_table_set *set;
  205. spin_lock(&sysctl_lock);
  206. root = &sysctl_table_root;
  207. do {
  208. set = lookup_header_set(root, current->nsproxy);
  209. entry = find_entry(&head, set, dir_head, dir, name, namelen);
  210. if (entry && use_table(head))
  211. *phead = head;
  212. else
  213. entry = NULL;
  214. root = list_entry(root->root_list.next,
  215. struct ctl_table_root, root_list);
  216. } while (!entry && root != &sysctl_table_root);
  217. spin_unlock(&sysctl_lock);
  218. return entry;
  219. }
  220. static struct ctl_table_header *__sysctl_head_next(struct nsproxy *namespaces,
  221. struct ctl_table_header *prev)
  222. {
  223. struct ctl_table_root *root;
  224. struct list_head *header_list;
  225. struct ctl_table_header *head;
  226. struct list_head *tmp;
  227. spin_lock(&sysctl_lock);
  228. if (prev) {
  229. head = prev;
  230. tmp = &prev->ctl_entry;
  231. unuse_table(prev);
  232. goto next;
  233. }
  234. tmp = &root_table_header.ctl_entry;
  235. for (;;) {
  236. head = list_entry(tmp, struct ctl_table_header, ctl_entry);
  237. if (!use_table(head))
  238. goto next;
  239. spin_unlock(&sysctl_lock);
  240. return head;
  241. next:
  242. root = head->root;
  243. tmp = tmp->next;
  244. header_list = lookup_header_list(root, namespaces);
  245. if (tmp != header_list)
  246. continue;
  247. do {
  248. root = list_entry(root->root_list.next,
  249. struct ctl_table_root, root_list);
  250. if (root == &sysctl_table_root)
  251. goto out;
  252. header_list = lookup_header_list(root, namespaces);
  253. } while (list_empty(header_list));
  254. tmp = header_list->next;
  255. }
  256. out:
  257. spin_unlock(&sysctl_lock);
  258. return NULL;
  259. }
  260. static struct ctl_table_header *sysctl_head_next(struct ctl_table_header *prev)
  261. {
  262. return __sysctl_head_next(current->nsproxy, prev);
  263. }
  264. void register_sysctl_root(struct ctl_table_root *root)
  265. {
  266. spin_lock(&sysctl_lock);
  267. list_add_tail(&root->root_list, &sysctl_table_root.root_list);
  268. spin_unlock(&sysctl_lock);
  269. }
  270. /*
  271. * sysctl_perm does NOT grant the superuser all rights automatically, because
  272. * some sysctl variables are readonly even to root.
  273. */
  274. static int test_perm(int mode, int op)
  275. {
  276. if (!current_euid())
  277. mode >>= 6;
  278. else if (in_egroup_p(0))
  279. mode >>= 3;
  280. if ((op & ~mode & (MAY_READ|MAY_WRITE|MAY_EXEC)) == 0)
  281. return 0;
  282. return -EACCES;
  283. }
  284. static int sysctl_perm(struct ctl_table_root *root, struct ctl_table *table, int op)
  285. {
  286. int mode;
  287. if (root->permissions)
  288. mode = root->permissions(root, current->nsproxy, table);
  289. else
  290. mode = table->mode;
  291. return test_perm(mode, op);
  292. }
  293. static struct inode *proc_sys_make_inode(struct super_block *sb,
  294. struct ctl_table_header *head, struct ctl_table *table)
  295. {
  296. struct inode *inode;
  297. struct proc_inode *ei;
  298. inode = new_inode(sb);
  299. if (!inode)
  300. goto out;
  301. inode->i_ino = get_next_ino();
  302. sysctl_head_get(head);
  303. ei = PROC_I(inode);
  304. ei->sysctl = head;
  305. ei->sysctl_entry = table;
  306. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  307. inode->i_mode = table->mode;
  308. if (!table->child) {
  309. inode->i_mode |= S_IFREG;
  310. inode->i_op = &proc_sys_inode_operations;
  311. inode->i_fop = &proc_sys_file_operations;
  312. } else {
  313. inode->i_mode |= S_IFDIR;
  314. inode->i_op = &proc_sys_dir_operations;
  315. inode->i_fop = &proc_sys_dir_file_operations;
  316. }
  317. out:
  318. return inode;
  319. }
  320. static struct ctl_table_header *grab_header(struct inode *inode)
  321. {
  322. struct ctl_table_header *head = PROC_I(inode)->sysctl;
  323. if (!head)
  324. head = &root_table_header;
  325. return sysctl_head_grab(head);
  326. }
  327. static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry,
  328. struct nameidata *nd)
  329. {
  330. struct ctl_table_header *head = grab_header(dir);
  331. struct ctl_table *table = PROC_I(dir)->sysctl_entry;
  332. struct ctl_table_header *h = NULL;
  333. struct qstr *name = &dentry->d_name;
  334. struct ctl_table *p;
  335. struct inode *inode;
  336. struct dentry *err = ERR_PTR(-ENOENT);
  337. if (IS_ERR(head))
  338. return ERR_CAST(head);
  339. if (table && !table->child) {
  340. WARN_ON(1);
  341. goto out;
  342. }
  343. table = table ? table->child : &head->ctl_table[1];
  344. p = lookup_entry(&h, head, table, name->name, name->len);
  345. if (!p)
  346. goto out;
  347. err = ERR_PTR(-ENOMEM);
  348. inode = proc_sys_make_inode(dir->i_sb, h ? h : head, p);
  349. if (h)
  350. sysctl_head_finish(h);
  351. if (!inode)
  352. goto out;
  353. err = NULL;
  354. d_set_d_op(dentry, &proc_sys_dentry_operations);
  355. d_add(dentry, inode);
  356. out:
  357. sysctl_head_finish(head);
  358. return err;
  359. }
  360. static ssize_t proc_sys_call_handler(struct file *filp, void __user *buf,
  361. size_t count, loff_t *ppos, int write)
  362. {
  363. struct inode *inode = filp->f_path.dentry->d_inode;
  364. struct ctl_table_header *head = grab_header(inode);
  365. struct ctl_table *table = PROC_I(inode)->sysctl_entry;
  366. ssize_t error;
  367. size_t res;
  368. if (IS_ERR(head))
  369. return PTR_ERR(head);
  370. /*
  371. * At this point we know that the sysctl was not unregistered
  372. * and won't be until we finish.
  373. */
  374. error = -EPERM;
  375. if (sysctl_perm(head->root, table, write ? MAY_WRITE : MAY_READ))
  376. goto out;
  377. /* if that can happen at all, it should be -EINVAL, not -EISDIR */
  378. error = -EINVAL;
  379. if (!table->proc_handler)
  380. goto out;
  381. /* careful: calling conventions are nasty here */
  382. res = count;
  383. error = table->proc_handler(table, write, buf, &res, ppos);
  384. if (!error)
  385. error = res;
  386. out:
  387. sysctl_head_finish(head);
  388. return error;
  389. }
  390. static ssize_t proc_sys_read(struct file *filp, char __user *buf,
  391. size_t count, loff_t *ppos)
  392. {
  393. return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 0);
  394. }
  395. static ssize_t proc_sys_write(struct file *filp, const char __user *buf,
  396. size_t count, loff_t *ppos)
  397. {
  398. return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 1);
  399. }
  400. static int proc_sys_open(struct inode *inode, struct file *filp)
  401. {
  402. struct ctl_table *table = PROC_I(inode)->sysctl_entry;
  403. if (table->poll)
  404. filp->private_data = proc_sys_poll_event(table->poll);
  405. return 0;
  406. }
  407. static unsigned int proc_sys_poll(struct file *filp, poll_table *wait)
  408. {
  409. struct inode *inode = filp->f_path.dentry->d_inode;
  410. struct ctl_table *table = PROC_I(inode)->sysctl_entry;
  411. unsigned long event = (unsigned long)filp->private_data;
  412. unsigned int ret = DEFAULT_POLLMASK;
  413. if (!table->proc_handler)
  414. goto out;
  415. if (!table->poll)
  416. goto out;
  417. poll_wait(filp, &table->poll->wait, wait);
  418. if (event != atomic_read(&table->poll->event)) {
  419. filp->private_data = proc_sys_poll_event(table->poll);
  420. ret = POLLIN | POLLRDNORM | POLLERR | POLLPRI;
  421. }
  422. out:
  423. return ret;
  424. }
  425. static int proc_sys_fill_cache(struct file *filp, void *dirent,
  426. filldir_t filldir,
  427. struct ctl_table_header *head,
  428. struct ctl_table *table)
  429. {
  430. struct dentry *child, *dir = filp->f_path.dentry;
  431. struct inode *inode;
  432. struct qstr qname;
  433. ino_t ino = 0;
  434. unsigned type = DT_UNKNOWN;
  435. qname.name = table->procname;
  436. qname.len = strlen(table->procname);
  437. qname.hash = full_name_hash(qname.name, qname.len);
  438. child = d_lookup(dir, &qname);
  439. if (!child) {
  440. child = d_alloc(dir, &qname);
  441. if (child) {
  442. inode = proc_sys_make_inode(dir->d_sb, head, table);
  443. if (!inode) {
  444. dput(child);
  445. return -ENOMEM;
  446. } else {
  447. d_set_d_op(child, &proc_sys_dentry_operations);
  448. d_add(child, inode);
  449. }
  450. } else {
  451. return -ENOMEM;
  452. }
  453. }
  454. inode = child->d_inode;
  455. ino = inode->i_ino;
  456. type = inode->i_mode >> 12;
  457. dput(child);
  458. return !!filldir(dirent, qname.name, qname.len, filp->f_pos, ino, type);
  459. }
  460. static int scan(struct ctl_table_header *head, ctl_table *table,
  461. unsigned long *pos, struct file *file,
  462. void *dirent, filldir_t filldir)
  463. {
  464. for (; table->procname; table++, (*pos)++) {
  465. int res;
  466. if (*pos < file->f_pos)
  467. continue;
  468. res = proc_sys_fill_cache(file, dirent, filldir, head, table);
  469. if (res)
  470. return res;
  471. file->f_pos = *pos + 1;
  472. }
  473. return 0;
  474. }
  475. static int proc_sys_readdir(struct file *filp, void *dirent, filldir_t filldir)
  476. {
  477. struct dentry *dentry = filp->f_path.dentry;
  478. struct inode *inode = dentry->d_inode;
  479. struct ctl_table_header *head = grab_header(inode);
  480. struct ctl_table *table = PROC_I(inode)->sysctl_entry;
  481. struct ctl_table_header *h = NULL;
  482. unsigned long pos;
  483. int ret = -EINVAL;
  484. if (IS_ERR(head))
  485. return PTR_ERR(head);
  486. if (table && !table->child) {
  487. WARN_ON(1);
  488. goto out;
  489. }
  490. table = table ? table->child : &head->ctl_table[1];
  491. ret = 0;
  492. /* Avoid a switch here: arm builds fail with missing __cmpdi2 */
  493. if (filp->f_pos == 0) {
  494. if (filldir(dirent, ".", 1, filp->f_pos,
  495. inode->i_ino, DT_DIR) < 0)
  496. goto out;
  497. filp->f_pos++;
  498. }
  499. if (filp->f_pos == 1) {
  500. if (filldir(dirent, "..", 2, filp->f_pos,
  501. parent_ino(dentry), DT_DIR) < 0)
  502. goto out;
  503. filp->f_pos++;
  504. }
  505. pos = 2;
  506. ret = scan(head, table, &pos, filp, dirent, filldir);
  507. if (ret)
  508. goto out;
  509. for (h = sysctl_head_next(NULL); h; h = sysctl_head_next(h)) {
  510. if (h->attached_to != table)
  511. continue;
  512. ret = scan(h, h->attached_by, &pos, filp, dirent, filldir);
  513. if (ret) {
  514. sysctl_head_finish(h);
  515. break;
  516. }
  517. }
  518. ret = 1;
  519. out:
  520. sysctl_head_finish(head);
  521. return ret;
  522. }
  523. static int proc_sys_permission(struct inode *inode, int mask)
  524. {
  525. /*
  526. * sysctl entries that are not writeable,
  527. * are _NOT_ writeable, capabilities or not.
  528. */
  529. struct ctl_table_header *head;
  530. struct ctl_table *table;
  531. int error;
  532. /* Executable files are not allowed under /proc/sys/ */
  533. if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))
  534. return -EACCES;
  535. head = grab_header(inode);
  536. if (IS_ERR(head))
  537. return PTR_ERR(head);
  538. table = PROC_I(inode)->sysctl_entry;
  539. if (!table) /* global root - r-xr-xr-x */
  540. error = mask & MAY_WRITE ? -EACCES : 0;
  541. else /* Use the permissions on the sysctl table entry */
  542. error = sysctl_perm(head->root, table, mask & ~MAY_NOT_BLOCK);
  543. sysctl_head_finish(head);
  544. return error;
  545. }
  546. static int proc_sys_setattr(struct dentry *dentry, struct iattr *attr)
  547. {
  548. struct inode *inode = dentry->d_inode;
  549. int error;
  550. if (attr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
  551. return -EPERM;
  552. error = inode_change_ok(inode, attr);
  553. if (error)
  554. return error;
  555. if ((attr->ia_valid & ATTR_SIZE) &&
  556. attr->ia_size != i_size_read(inode)) {
  557. error = vmtruncate(inode, attr->ia_size);
  558. if (error)
  559. return error;
  560. }
  561. setattr_copy(inode, attr);
  562. mark_inode_dirty(inode);
  563. return 0;
  564. }
  565. static int proc_sys_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
  566. {
  567. struct inode *inode = dentry->d_inode;
  568. struct ctl_table_header *head = grab_header(inode);
  569. struct ctl_table *table = PROC_I(inode)->sysctl_entry;
  570. if (IS_ERR(head))
  571. return PTR_ERR(head);
  572. generic_fillattr(inode, stat);
  573. if (table)
  574. stat->mode = (stat->mode & S_IFMT) | table->mode;
  575. sysctl_head_finish(head);
  576. return 0;
  577. }
  578. static const struct file_operations proc_sys_file_operations = {
  579. .open = proc_sys_open,
  580. .poll = proc_sys_poll,
  581. .read = proc_sys_read,
  582. .write = proc_sys_write,
  583. .llseek = default_llseek,
  584. };
  585. static const struct file_operations proc_sys_dir_file_operations = {
  586. .read = generic_read_dir,
  587. .readdir = proc_sys_readdir,
  588. .llseek = generic_file_llseek,
  589. };
  590. static const struct inode_operations proc_sys_inode_operations = {
  591. .permission = proc_sys_permission,
  592. .setattr = proc_sys_setattr,
  593. .getattr = proc_sys_getattr,
  594. };
  595. static const struct inode_operations proc_sys_dir_operations = {
  596. .lookup = proc_sys_lookup,
  597. .permission = proc_sys_permission,
  598. .setattr = proc_sys_setattr,
  599. .getattr = proc_sys_getattr,
  600. };
  601. static int proc_sys_revalidate(struct dentry *dentry, struct nameidata *nd)
  602. {
  603. if (nd->flags & LOOKUP_RCU)
  604. return -ECHILD;
  605. return !PROC_I(dentry->d_inode)->sysctl->unregistering;
  606. }
  607. static int proc_sys_delete(const struct dentry *dentry)
  608. {
  609. return !!PROC_I(dentry->d_inode)->sysctl->unregistering;
  610. }
  611. static int sysctl_is_seen(struct ctl_table_header *p)
  612. {
  613. struct ctl_table_set *set = p->set;
  614. int res;
  615. spin_lock(&sysctl_lock);
  616. if (p->unregistering)
  617. res = 0;
  618. else if (!set->is_seen)
  619. res = 1;
  620. else
  621. res = set->is_seen(set);
  622. spin_unlock(&sysctl_lock);
  623. return res;
  624. }
  625. static int proc_sys_compare(const struct dentry *parent,
  626. const struct inode *pinode,
  627. const struct dentry *dentry, const struct inode *inode,
  628. unsigned int len, const char *str, const struct qstr *name)
  629. {
  630. struct ctl_table_header *head;
  631. /* Although proc doesn't have negative dentries, rcu-walk means
  632. * that inode here can be NULL */
  633. /* AV: can it, indeed? */
  634. if (!inode)
  635. return 1;
  636. if (name->len != len)
  637. return 1;
  638. if (memcmp(name->name, str, len))
  639. return 1;
  640. head = rcu_dereference(PROC_I(inode)->sysctl);
  641. return !head || !sysctl_is_seen(head);
  642. }
  643. static const struct dentry_operations proc_sys_dentry_operations = {
  644. .d_revalidate = proc_sys_revalidate,
  645. .d_delete = proc_sys_delete,
  646. .d_compare = proc_sys_compare,
  647. };
  648. static struct ctl_table *is_branch_in(struct ctl_table *branch,
  649. struct ctl_table *table)
  650. {
  651. struct ctl_table *p;
  652. const char *s = branch->procname;
  653. /* branch should have named subdirectory as its first element */
  654. if (!s || !branch->child)
  655. return NULL;
  656. /* ... and nothing else */
  657. if (branch[1].procname)
  658. return NULL;
  659. /* table should contain subdirectory with the same name */
  660. for (p = table; p->procname; p++) {
  661. if (!p->child)
  662. continue;
  663. if (p->procname && strcmp(p->procname, s) == 0)
  664. return p;
  665. }
  666. return NULL;
  667. }
  668. /* see if attaching q to p would be an improvement */
  669. static void try_attach(struct ctl_table_header *p, struct ctl_table_header *q)
  670. {
  671. struct ctl_table *to = p->ctl_table, *by = q->ctl_table;
  672. struct ctl_table *next;
  673. int is_better = 0;
  674. int not_in_parent = !p->attached_by;
  675. while ((next = is_branch_in(by, to)) != NULL) {
  676. if (by == q->attached_by)
  677. is_better = 1;
  678. if (to == p->attached_by)
  679. not_in_parent = 1;
  680. by = by->child;
  681. to = next->child;
  682. }
  683. if (is_better && not_in_parent) {
  684. q->attached_by = by;
  685. q->attached_to = to;
  686. q->parent = p;
  687. }
  688. }
  689. static int sysctl_check_table_dups(const char *path, struct ctl_table *old,
  690. struct ctl_table *table)
  691. {
  692. struct ctl_table *entry, *test;
  693. int error = 0;
  694. for (entry = old; entry->procname; entry++) {
  695. for (test = table; test->procname; test++) {
  696. if (strcmp(entry->procname, test->procname) == 0) {
  697. printk(KERN_ERR "sysctl duplicate entry: %s/%s\n",
  698. path, test->procname);
  699. error = -EEXIST;
  700. }
  701. }
  702. }
  703. return error;
  704. }
  705. static int sysctl_check_dups(struct nsproxy *namespaces,
  706. struct ctl_table_header *header,
  707. const char *path, struct ctl_table *table)
  708. {
  709. struct ctl_table_root *root;
  710. struct ctl_table_set *set;
  711. struct ctl_table_header *dir_head, *head;
  712. struct ctl_table *dir_table;
  713. int error = 0;
  714. /* No dups if we are the only member of our directory */
  715. if (header->attached_by != table)
  716. return 0;
  717. dir_head = header->parent;
  718. dir_table = header->attached_to;
  719. error = sysctl_check_table_dups(path, dir_table, table);
  720. root = &sysctl_table_root;
  721. do {
  722. set = lookup_header_set(root, namespaces);
  723. list_for_each_entry(head, &set->list, ctl_entry) {
  724. if (head->unregistering)
  725. continue;
  726. if (head->attached_to != dir_table)
  727. continue;
  728. error = sysctl_check_table_dups(path, head->attached_by,
  729. table);
  730. }
  731. root = list_entry(root->root_list.next,
  732. struct ctl_table_root, root_list);
  733. } while (root != &sysctl_table_root);
  734. return error;
  735. }
  736. static int sysctl_err(const char *path, struct ctl_table *table, char *fmt, ...)
  737. {
  738. struct va_format vaf;
  739. va_list args;
  740. va_start(args, fmt);
  741. vaf.fmt = fmt;
  742. vaf.va = &args;
  743. printk(KERN_ERR "sysctl table check failed: %s/%s %pV\n",
  744. path, table->procname, &vaf);
  745. va_end(args);
  746. return -EINVAL;
  747. }
  748. static int sysctl_check_table(const char *path, struct ctl_table *table)
  749. {
  750. int err = 0;
  751. for (; table->procname; table++) {
  752. if (table->child)
  753. err = sysctl_err(path, table, "Not a file");
  754. if ((table->proc_handler == proc_dostring) ||
  755. (table->proc_handler == proc_dointvec) ||
  756. (table->proc_handler == proc_dointvec_minmax) ||
  757. (table->proc_handler == proc_dointvec_jiffies) ||
  758. (table->proc_handler == proc_dointvec_userhz_jiffies) ||
  759. (table->proc_handler == proc_dointvec_ms_jiffies) ||
  760. (table->proc_handler == proc_doulongvec_minmax) ||
  761. (table->proc_handler == proc_doulongvec_ms_jiffies_minmax)) {
  762. if (!table->data)
  763. err = sysctl_err(path, table, "No data");
  764. if (!table->maxlen)
  765. err = sysctl_err(path, table, "No maxlen");
  766. }
  767. if (!table->proc_handler)
  768. err = sysctl_err(path, table, "No proc_handler");
  769. if ((table->mode & (S_IRUGO|S_IWUGO)) != table->mode)
  770. err = sysctl_err(path, table, "bogus .mode 0%o",
  771. table->mode);
  772. }
  773. return err;
  774. }
  775. /**
  776. * __register_sysctl_table - register a leaf sysctl table
  777. * @root: List of sysctl headers to register on
  778. * @namespaces: Data to compute which lists of sysctl entries are visible
  779. * @path: The path to the directory the sysctl table is in.
  780. * @table: the top-level table structure
  781. *
  782. * Register a sysctl table hierarchy. @table should be a filled in ctl_table
  783. * array. A completely 0 filled entry terminates the table.
  784. *
  785. * The members of the &struct ctl_table structure are used as follows:
  786. *
  787. * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not
  788. * enter a sysctl file
  789. *
  790. * data - a pointer to data for use by proc_handler
  791. *
  792. * maxlen - the maximum size in bytes of the data
  793. *
  794. * mode - the file permissions for the /proc/sys file
  795. *
  796. * child - must be %NULL.
  797. *
  798. * proc_handler - the text handler routine (described below)
  799. *
  800. * extra1, extra2 - extra pointers usable by the proc handler routines
  801. *
  802. * Leaf nodes in the sysctl tree will be represented by a single file
  803. * under /proc; non-leaf nodes will be represented by directories.
  804. *
  805. * There must be a proc_handler routine for any terminal nodes.
  806. * Several default handlers are available to cover common cases -
  807. *
  808. * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(),
  809. * proc_dointvec_userhz_jiffies(), proc_dointvec_minmax(),
  810. * proc_doulongvec_ms_jiffies_minmax(), proc_doulongvec_minmax()
  811. *
  812. * It is the handler's job to read the input buffer from user memory
  813. * and process it. The handler should return 0 on success.
  814. *
  815. * This routine returns %NULL on a failure to register, and a pointer
  816. * to the table header on success.
  817. */
  818. struct ctl_table_header *__register_sysctl_table(
  819. struct ctl_table_root *root,
  820. struct nsproxy *namespaces,
  821. const char *path, struct ctl_table *table)
  822. {
  823. struct ctl_table_header *header;
  824. struct ctl_table *new, **prevp;
  825. const char *name, *nextname;
  826. unsigned int npath = 0;
  827. struct ctl_table_set *set;
  828. size_t path_bytes = 0;
  829. char *new_name;
  830. /* Count the path components */
  831. for (name = path; name; name = nextname) {
  832. int namelen;
  833. nextname = strchr(name, '/');
  834. if (nextname) {
  835. namelen = nextname - name;
  836. nextname++;
  837. } else {
  838. namelen = strlen(name);
  839. }
  840. if (namelen == 0)
  841. continue;
  842. path_bytes += namelen + 1;
  843. npath++;
  844. }
  845. /*
  846. * For each path component, allocate a 2-element ctl_table array.
  847. * The first array element will be filled with the sysctl entry
  848. * for this, the second will be the sentinel (procname == 0).
  849. *
  850. * We allocate everything in one go so that we don't have to
  851. * worry about freeing additional memory in unregister_sysctl_table.
  852. */
  853. header = kzalloc(sizeof(struct ctl_table_header) + path_bytes +
  854. (2 * npath * sizeof(struct ctl_table)), GFP_KERNEL);
  855. if (!header)
  856. return NULL;
  857. new = (struct ctl_table *) (header + 1);
  858. new_name = (char *)(new + (2 * npath));
  859. /* Now connect the dots */
  860. prevp = &header->ctl_table;
  861. for (name = path; name; name = nextname) {
  862. int namelen;
  863. nextname = strchr(name, '/');
  864. if (nextname) {
  865. namelen = nextname - name;
  866. nextname++;
  867. } else {
  868. namelen = strlen(name);
  869. }
  870. if (namelen == 0)
  871. continue;
  872. memcpy(new_name, name, namelen);
  873. new_name[namelen] = '\0';
  874. new->procname = new_name;
  875. new->mode = 0555;
  876. *prevp = new;
  877. prevp = &new->child;
  878. new += 2;
  879. new_name += namelen + 1;
  880. }
  881. *prevp = table;
  882. init_header(header, root, NULL, table);
  883. if (sysctl_check_table(path, table))
  884. goto fail;
  885. spin_lock(&sysctl_lock);
  886. header->set = lookup_header_set(root, namespaces);
  887. header->attached_by = header->ctl_table;
  888. header->attached_to = &root_table[1];
  889. header->parent = &root_table_header;
  890. set = header->set;
  891. root = header->root;
  892. for (;;) {
  893. struct ctl_table_header *p;
  894. list_for_each_entry(p, &set->list, ctl_entry) {
  895. if (p->unregistering)
  896. continue;
  897. try_attach(p, header);
  898. }
  899. if (root == &sysctl_table_root)
  900. break;
  901. root = list_entry(root->root_list.prev,
  902. struct ctl_table_root, root_list);
  903. set = lookup_header_set(root, namespaces);
  904. }
  905. if (sysctl_check_dups(namespaces, header, path, table))
  906. goto fail_locked;
  907. insert_header(header);
  908. spin_unlock(&sysctl_lock);
  909. return header;
  910. fail_locked:
  911. spin_unlock(&sysctl_lock);
  912. fail:
  913. kfree(header);
  914. dump_stack();
  915. return NULL;
  916. }
  917. static char *append_path(const char *path, char *pos, const char *name)
  918. {
  919. int namelen;
  920. namelen = strlen(name);
  921. if (((pos - path) + namelen + 2) >= PATH_MAX)
  922. return NULL;
  923. memcpy(pos, name, namelen);
  924. pos[namelen] = '/';
  925. pos[namelen + 1] = '\0';
  926. pos += namelen + 1;
  927. return pos;
  928. }
  929. static int count_subheaders(struct ctl_table *table)
  930. {
  931. int has_files = 0;
  932. int nr_subheaders = 0;
  933. struct ctl_table *entry;
  934. /* special case: no directory and empty directory */
  935. if (!table || !table->procname)
  936. return 1;
  937. for (entry = table; entry->procname; entry++) {
  938. if (entry->child)
  939. nr_subheaders += count_subheaders(entry->child);
  940. else
  941. has_files = 1;
  942. }
  943. return nr_subheaders + has_files;
  944. }
  945. static int register_leaf_sysctl_tables(const char *path, char *pos,
  946. struct ctl_table_header ***subheader,
  947. struct ctl_table_root *root, struct nsproxy *namespaces,
  948. struct ctl_table *table)
  949. {
  950. struct ctl_table *ctl_table_arg = NULL;
  951. struct ctl_table *entry, *files;
  952. int nr_files = 0;
  953. int nr_dirs = 0;
  954. int err = -ENOMEM;
  955. for (entry = table; entry->procname; entry++) {
  956. if (entry->child)
  957. nr_dirs++;
  958. else
  959. nr_files++;
  960. }
  961. files = table;
  962. /* If there are mixed files and directories we need a new table */
  963. if (nr_dirs && nr_files) {
  964. struct ctl_table *new;
  965. files = kzalloc(sizeof(struct ctl_table) * (nr_files + 1),
  966. GFP_KERNEL);
  967. if (!files)
  968. goto out;
  969. ctl_table_arg = files;
  970. for (new = files, entry = table; entry->procname; entry++) {
  971. if (entry->child)
  972. continue;
  973. *new = *entry;
  974. new++;
  975. }
  976. }
  977. /* Register everything except a directory full of subdirectories */
  978. if (nr_files || !nr_dirs) {
  979. struct ctl_table_header *header;
  980. header = __register_sysctl_table(root, namespaces, path, files);
  981. if (!header) {
  982. kfree(ctl_table_arg);
  983. goto out;
  984. }
  985. /* Remember if we need to free the file table */
  986. header->ctl_table_arg = ctl_table_arg;
  987. **subheader = header;
  988. (*subheader)++;
  989. }
  990. /* Recurse into the subdirectories. */
  991. for (entry = table; entry->procname; entry++) {
  992. char *child_pos;
  993. if (!entry->child)
  994. continue;
  995. err = -ENAMETOOLONG;
  996. child_pos = append_path(path, pos, entry->procname);
  997. if (!child_pos)
  998. goto out;
  999. err = register_leaf_sysctl_tables(path, child_pos, subheader,
  1000. root, namespaces, entry->child);
  1001. pos[0] = '\0';
  1002. if (err)
  1003. goto out;
  1004. }
  1005. err = 0;
  1006. out:
  1007. /* On failure our caller will unregister all registered subheaders */
  1008. return err;
  1009. }
  1010. /**
  1011. * __register_sysctl_paths - register a sysctl table hierarchy
  1012. * @root: List of sysctl headers to register on
  1013. * @namespaces: Data to compute which lists of sysctl entries are visible
  1014. * @path: The path to the directory the sysctl table is in.
  1015. * @table: the top-level table structure
  1016. *
  1017. * Register a sysctl table hierarchy. @table should be a filled in ctl_table
  1018. * array. A completely 0 filled entry terminates the table.
  1019. *
  1020. * See __register_sysctl_table for more details.
  1021. */
  1022. struct ctl_table_header *__register_sysctl_paths(
  1023. struct ctl_table_root *root,
  1024. struct nsproxy *namespaces,
  1025. const struct ctl_path *path, struct ctl_table *table)
  1026. {
  1027. struct ctl_table *ctl_table_arg = table;
  1028. int nr_subheaders = count_subheaders(table);
  1029. struct ctl_table_header *header = NULL, **subheaders, **subheader;
  1030. const struct ctl_path *component;
  1031. char *new_path, *pos;
  1032. pos = new_path = kmalloc(PATH_MAX, GFP_KERNEL);
  1033. if (!new_path)
  1034. return NULL;
  1035. pos[0] = '\0';
  1036. for (component = path; component->procname; component++) {
  1037. pos = append_path(new_path, pos, component->procname);
  1038. if (!pos)
  1039. goto out;
  1040. }
  1041. while (table->procname && table->child && !table[1].procname) {
  1042. pos = append_path(new_path, pos, table->procname);
  1043. if (!pos)
  1044. goto out;
  1045. table = table->child;
  1046. }
  1047. if (nr_subheaders == 1) {
  1048. header = __register_sysctl_table(root, namespaces, new_path, table);
  1049. if (header)
  1050. header->ctl_table_arg = ctl_table_arg;
  1051. } else {
  1052. header = kzalloc(sizeof(*header) +
  1053. sizeof(*subheaders)*nr_subheaders, GFP_KERNEL);
  1054. if (!header)
  1055. goto out;
  1056. subheaders = (struct ctl_table_header **) (header + 1);
  1057. subheader = subheaders;
  1058. header->ctl_table_arg = ctl_table_arg;
  1059. if (register_leaf_sysctl_tables(new_path, pos, &subheader,
  1060. root, namespaces, table))
  1061. goto err_register_leaves;
  1062. }
  1063. out:
  1064. kfree(new_path);
  1065. return header;
  1066. err_register_leaves:
  1067. while (subheader > subheaders) {
  1068. struct ctl_table_header *subh = *(--subheader);
  1069. struct ctl_table *table = subh->ctl_table_arg;
  1070. unregister_sysctl_table(subh);
  1071. kfree(table);
  1072. }
  1073. kfree(header);
  1074. header = NULL;
  1075. goto out;
  1076. }
  1077. /**
  1078. * register_sysctl_table_path - register a sysctl table hierarchy
  1079. * @path: The path to the directory the sysctl table is in.
  1080. * @table: the top-level table structure
  1081. *
  1082. * Register a sysctl table hierarchy. @table should be a filled in ctl_table
  1083. * array. A completely 0 filled entry terminates the table.
  1084. *
  1085. * See __register_sysctl_paths for more details.
  1086. */
  1087. struct ctl_table_header *register_sysctl_paths(const struct ctl_path *path,
  1088. struct ctl_table *table)
  1089. {
  1090. return __register_sysctl_paths(&sysctl_table_root, current->nsproxy,
  1091. path, table);
  1092. }
  1093. EXPORT_SYMBOL(register_sysctl_paths);
  1094. /**
  1095. * register_sysctl_table - register a sysctl table hierarchy
  1096. * @table: the top-level table structure
  1097. *
  1098. * Register a sysctl table hierarchy. @table should be a filled in ctl_table
  1099. * array. A completely 0 filled entry terminates the table.
  1100. *
  1101. * See register_sysctl_paths for more details.
  1102. */
  1103. struct ctl_table_header *register_sysctl_table(struct ctl_table *table)
  1104. {
  1105. static const struct ctl_path null_path[] = { {} };
  1106. return register_sysctl_paths(null_path, table);
  1107. }
  1108. EXPORT_SYMBOL(register_sysctl_table);
  1109. static void drop_sysctl_table(struct ctl_table_header *header)
  1110. {
  1111. if (--header->nreg)
  1112. return;
  1113. start_unregistering(header);
  1114. if (!--header->parent->count) {
  1115. WARN_ON(1);
  1116. kfree_rcu(header->parent, rcu);
  1117. }
  1118. if (!--header->count)
  1119. kfree_rcu(header, rcu);
  1120. }
  1121. /**
  1122. * unregister_sysctl_table - unregister a sysctl table hierarchy
  1123. * @header: the header returned from register_sysctl_table
  1124. *
  1125. * Unregisters the sysctl table and all children. proc entries may not
  1126. * actually be removed until they are no longer used by anyone.
  1127. */
  1128. void unregister_sysctl_table(struct ctl_table_header * header)
  1129. {
  1130. int nr_subheaders;
  1131. might_sleep();
  1132. if (header == NULL)
  1133. return;
  1134. nr_subheaders = count_subheaders(header->ctl_table_arg);
  1135. if (unlikely(nr_subheaders > 1)) {
  1136. struct ctl_table_header **subheaders;
  1137. int i;
  1138. subheaders = (struct ctl_table_header **)(header + 1);
  1139. for (i = nr_subheaders -1; i >= 0; i--) {
  1140. struct ctl_table_header *subh = subheaders[i];
  1141. struct ctl_table *table = subh->ctl_table_arg;
  1142. unregister_sysctl_table(subh);
  1143. kfree(table);
  1144. }
  1145. kfree(header);
  1146. return;
  1147. }
  1148. spin_lock(&sysctl_lock);
  1149. drop_sysctl_table(header);
  1150. spin_unlock(&sysctl_lock);
  1151. }
  1152. EXPORT_SYMBOL(unregister_sysctl_table);
  1153. void setup_sysctl_set(struct ctl_table_set *p,
  1154. int (*is_seen)(struct ctl_table_set *))
  1155. {
  1156. INIT_LIST_HEAD(&p->list);
  1157. p->is_seen = is_seen;
  1158. }
  1159. void retire_sysctl_set(struct ctl_table_set *set)
  1160. {
  1161. WARN_ON(!list_empty(&set->list));
  1162. }
  1163. int __init proc_sys_init(void)
  1164. {
  1165. struct proc_dir_entry *proc_sys_root;
  1166. proc_sys_root = proc_mkdir("sys", NULL);
  1167. proc_sys_root->proc_iops = &proc_sys_dir_operations;
  1168. proc_sys_root->proc_fops = &proc_sys_dir_file_operations;
  1169. proc_sys_root->nlink = 0;
  1170. return sysctl_init();
  1171. }