proc_sysctl.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098
  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[1];
  25. static struct ctl_table_root sysctl_table_root;
  26. static struct ctl_table_header root_table_header = {
  27. {{.count = 1,
  28. .ctl_table = root_table,
  29. .ctl_entry = LIST_HEAD_INIT(sysctl_table_root.default_set.list),}},
  30. .root = &sysctl_table_root,
  31. .set = &sysctl_table_root.default_set,
  32. };
  33. static struct ctl_table_root sysctl_table_root = {
  34. .root_list = LIST_HEAD_INIT(sysctl_table_root.root_list),
  35. .default_set.list = LIST_HEAD_INIT(root_table_header.ctl_entry),
  36. };
  37. static DEFINE_SPINLOCK(sysctl_lock);
  38. /* called under sysctl_lock */
  39. static int use_table(struct ctl_table_header *p)
  40. {
  41. if (unlikely(p->unregistering))
  42. return 0;
  43. p->used++;
  44. return 1;
  45. }
  46. /* called under sysctl_lock */
  47. static void unuse_table(struct ctl_table_header *p)
  48. {
  49. if (!--p->used)
  50. if (unlikely(p->unregistering))
  51. complete(p->unregistering);
  52. }
  53. /* called under sysctl_lock, will reacquire if has to wait */
  54. static void start_unregistering(struct ctl_table_header *p)
  55. {
  56. /*
  57. * if p->used is 0, nobody will ever touch that entry again;
  58. * we'll eliminate all paths to it before dropping sysctl_lock
  59. */
  60. if (unlikely(p->used)) {
  61. struct completion wait;
  62. init_completion(&wait);
  63. p->unregistering = &wait;
  64. spin_unlock(&sysctl_lock);
  65. wait_for_completion(&wait);
  66. spin_lock(&sysctl_lock);
  67. } else {
  68. /* anything non-NULL; we'll never dereference it */
  69. p->unregistering = ERR_PTR(-EINVAL);
  70. }
  71. /*
  72. * do not remove from the list until nobody holds it; walking the
  73. * list in do_sysctl() relies on that.
  74. */
  75. list_del_init(&p->ctl_entry);
  76. }
  77. static void sysctl_head_get(struct ctl_table_header *head)
  78. {
  79. spin_lock(&sysctl_lock);
  80. head->count++;
  81. spin_unlock(&sysctl_lock);
  82. }
  83. void sysctl_head_put(struct ctl_table_header *head)
  84. {
  85. spin_lock(&sysctl_lock);
  86. if (!--head->count)
  87. kfree_rcu(head, rcu);
  88. spin_unlock(&sysctl_lock);
  89. }
  90. static struct ctl_table_header *sysctl_head_grab(struct ctl_table_header *head)
  91. {
  92. if (!head)
  93. BUG();
  94. spin_lock(&sysctl_lock);
  95. if (!use_table(head))
  96. head = ERR_PTR(-ENOENT);
  97. spin_unlock(&sysctl_lock);
  98. return head;
  99. }
  100. static void sysctl_head_finish(struct ctl_table_header *head)
  101. {
  102. if (!head)
  103. return;
  104. spin_lock(&sysctl_lock);
  105. unuse_table(head);
  106. spin_unlock(&sysctl_lock);
  107. }
  108. static struct ctl_table_set *
  109. lookup_header_set(struct ctl_table_root *root, struct nsproxy *namespaces)
  110. {
  111. struct ctl_table_set *set = &root->default_set;
  112. if (root->lookup)
  113. set = root->lookup(root, namespaces);
  114. return set;
  115. }
  116. static struct list_head *
  117. lookup_header_list(struct ctl_table_root *root, struct nsproxy *namespaces)
  118. {
  119. struct ctl_table_set *set = lookup_header_set(root, namespaces);
  120. return &set->list;
  121. }
  122. static struct ctl_table_header *__sysctl_head_next(struct nsproxy *namespaces,
  123. struct ctl_table_header *prev)
  124. {
  125. struct ctl_table_root *root;
  126. struct list_head *header_list;
  127. struct ctl_table_header *head;
  128. struct list_head *tmp;
  129. spin_lock(&sysctl_lock);
  130. if (prev) {
  131. head = prev;
  132. tmp = &prev->ctl_entry;
  133. unuse_table(prev);
  134. goto next;
  135. }
  136. tmp = &root_table_header.ctl_entry;
  137. for (;;) {
  138. head = list_entry(tmp, struct ctl_table_header, ctl_entry);
  139. if (!use_table(head))
  140. goto next;
  141. spin_unlock(&sysctl_lock);
  142. return head;
  143. next:
  144. root = head->root;
  145. tmp = tmp->next;
  146. header_list = lookup_header_list(root, namespaces);
  147. if (tmp != header_list)
  148. continue;
  149. do {
  150. root = list_entry(root->root_list.next,
  151. struct ctl_table_root, root_list);
  152. if (root == &sysctl_table_root)
  153. goto out;
  154. header_list = lookup_header_list(root, namespaces);
  155. } while (list_empty(header_list));
  156. tmp = header_list->next;
  157. }
  158. out:
  159. spin_unlock(&sysctl_lock);
  160. return NULL;
  161. }
  162. static struct ctl_table_header *sysctl_head_next(struct ctl_table_header *prev)
  163. {
  164. return __sysctl_head_next(current->nsproxy, prev);
  165. }
  166. void register_sysctl_root(struct ctl_table_root *root)
  167. {
  168. spin_lock(&sysctl_lock);
  169. list_add_tail(&root->root_list, &sysctl_table_root.root_list);
  170. spin_unlock(&sysctl_lock);
  171. }
  172. /*
  173. * sysctl_perm does NOT grant the superuser all rights automatically, because
  174. * some sysctl variables are readonly even to root.
  175. */
  176. static int test_perm(int mode, int op)
  177. {
  178. if (!current_euid())
  179. mode >>= 6;
  180. else if (in_egroup_p(0))
  181. mode >>= 3;
  182. if ((op & ~mode & (MAY_READ|MAY_WRITE|MAY_EXEC)) == 0)
  183. return 0;
  184. return -EACCES;
  185. }
  186. static int sysctl_perm(struct ctl_table_root *root, struct ctl_table *table, int op)
  187. {
  188. int mode;
  189. if (root->permissions)
  190. mode = root->permissions(root, current->nsproxy, table);
  191. else
  192. mode = table->mode;
  193. return test_perm(mode, op);
  194. }
  195. static void sysctl_set_parent(struct ctl_table *parent, struct ctl_table *table)
  196. {
  197. for (; table->procname; table++) {
  198. table->parent = parent;
  199. if (table->child)
  200. sysctl_set_parent(table, table->child);
  201. }
  202. }
  203. static struct inode *proc_sys_make_inode(struct super_block *sb,
  204. struct ctl_table_header *head, struct ctl_table *table)
  205. {
  206. struct inode *inode;
  207. struct proc_inode *ei;
  208. inode = new_inode(sb);
  209. if (!inode)
  210. goto out;
  211. inode->i_ino = get_next_ino();
  212. sysctl_head_get(head);
  213. ei = PROC_I(inode);
  214. ei->sysctl = head;
  215. ei->sysctl_entry = table;
  216. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  217. inode->i_mode = table->mode;
  218. if (!table->child) {
  219. inode->i_mode |= S_IFREG;
  220. inode->i_op = &proc_sys_inode_operations;
  221. inode->i_fop = &proc_sys_file_operations;
  222. } else {
  223. inode->i_mode |= S_IFDIR;
  224. inode->i_op = &proc_sys_dir_operations;
  225. inode->i_fop = &proc_sys_dir_file_operations;
  226. }
  227. out:
  228. return inode;
  229. }
  230. static struct ctl_table *find_in_table(struct ctl_table *p, struct qstr *name)
  231. {
  232. for ( ; p->procname; p++) {
  233. if (strlen(p->procname) != name->len)
  234. continue;
  235. if (memcmp(p->procname, name->name, name->len) != 0)
  236. continue;
  237. /* I have a match */
  238. return p;
  239. }
  240. return NULL;
  241. }
  242. static struct ctl_table_header *grab_header(struct inode *inode)
  243. {
  244. if (PROC_I(inode)->sysctl)
  245. return sysctl_head_grab(PROC_I(inode)->sysctl);
  246. else
  247. return sysctl_head_next(NULL);
  248. }
  249. static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry,
  250. struct nameidata *nd)
  251. {
  252. struct ctl_table_header *head = grab_header(dir);
  253. struct ctl_table *table = PROC_I(dir)->sysctl_entry;
  254. struct ctl_table_header *h = NULL;
  255. struct qstr *name = &dentry->d_name;
  256. struct ctl_table *p;
  257. struct inode *inode;
  258. struct dentry *err = ERR_PTR(-ENOENT);
  259. if (IS_ERR(head))
  260. return ERR_CAST(head);
  261. if (table && !table->child) {
  262. WARN_ON(1);
  263. goto out;
  264. }
  265. table = table ? table->child : head->ctl_table;
  266. p = find_in_table(table, name);
  267. if (!p) {
  268. for (h = sysctl_head_next(NULL); h; h = sysctl_head_next(h)) {
  269. if (h->attached_to != table)
  270. continue;
  271. p = find_in_table(h->attached_by, name);
  272. if (p)
  273. break;
  274. }
  275. }
  276. if (!p)
  277. goto out;
  278. err = ERR_PTR(-ENOMEM);
  279. inode = proc_sys_make_inode(dir->i_sb, h ? h : head, p);
  280. if (h)
  281. sysctl_head_finish(h);
  282. if (!inode)
  283. goto out;
  284. err = NULL;
  285. d_set_d_op(dentry, &proc_sys_dentry_operations);
  286. d_add(dentry, inode);
  287. out:
  288. sysctl_head_finish(head);
  289. return err;
  290. }
  291. static ssize_t proc_sys_call_handler(struct file *filp, void __user *buf,
  292. size_t count, loff_t *ppos, int write)
  293. {
  294. struct inode *inode = filp->f_path.dentry->d_inode;
  295. struct ctl_table_header *head = grab_header(inode);
  296. struct ctl_table *table = PROC_I(inode)->sysctl_entry;
  297. ssize_t error;
  298. size_t res;
  299. if (IS_ERR(head))
  300. return PTR_ERR(head);
  301. /*
  302. * At this point we know that the sysctl was not unregistered
  303. * and won't be until we finish.
  304. */
  305. error = -EPERM;
  306. if (sysctl_perm(head->root, table, write ? MAY_WRITE : MAY_READ))
  307. goto out;
  308. /* if that can happen at all, it should be -EINVAL, not -EISDIR */
  309. error = -EINVAL;
  310. if (!table->proc_handler)
  311. goto out;
  312. /* careful: calling conventions are nasty here */
  313. res = count;
  314. error = table->proc_handler(table, write, buf, &res, ppos);
  315. if (!error)
  316. error = res;
  317. out:
  318. sysctl_head_finish(head);
  319. return error;
  320. }
  321. static ssize_t proc_sys_read(struct file *filp, char __user *buf,
  322. size_t count, loff_t *ppos)
  323. {
  324. return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 0);
  325. }
  326. static ssize_t proc_sys_write(struct file *filp, const char __user *buf,
  327. size_t count, loff_t *ppos)
  328. {
  329. return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 1);
  330. }
  331. static int proc_sys_open(struct inode *inode, struct file *filp)
  332. {
  333. struct ctl_table *table = PROC_I(inode)->sysctl_entry;
  334. if (table->poll)
  335. filp->private_data = proc_sys_poll_event(table->poll);
  336. return 0;
  337. }
  338. static unsigned int proc_sys_poll(struct file *filp, poll_table *wait)
  339. {
  340. struct inode *inode = filp->f_path.dentry->d_inode;
  341. struct ctl_table *table = PROC_I(inode)->sysctl_entry;
  342. unsigned long event = (unsigned long)filp->private_data;
  343. unsigned int ret = DEFAULT_POLLMASK;
  344. if (!table->proc_handler)
  345. goto out;
  346. if (!table->poll)
  347. goto out;
  348. poll_wait(filp, &table->poll->wait, wait);
  349. if (event != atomic_read(&table->poll->event)) {
  350. filp->private_data = proc_sys_poll_event(table->poll);
  351. ret = POLLIN | POLLRDNORM | POLLERR | POLLPRI;
  352. }
  353. out:
  354. return ret;
  355. }
  356. static int proc_sys_fill_cache(struct file *filp, void *dirent,
  357. filldir_t filldir,
  358. struct ctl_table_header *head,
  359. struct ctl_table *table)
  360. {
  361. struct dentry *child, *dir = filp->f_path.dentry;
  362. struct inode *inode;
  363. struct qstr qname;
  364. ino_t ino = 0;
  365. unsigned type = DT_UNKNOWN;
  366. qname.name = table->procname;
  367. qname.len = strlen(table->procname);
  368. qname.hash = full_name_hash(qname.name, qname.len);
  369. child = d_lookup(dir, &qname);
  370. if (!child) {
  371. child = d_alloc(dir, &qname);
  372. if (child) {
  373. inode = proc_sys_make_inode(dir->d_sb, head, table);
  374. if (!inode) {
  375. dput(child);
  376. return -ENOMEM;
  377. } else {
  378. d_set_d_op(child, &proc_sys_dentry_operations);
  379. d_add(child, inode);
  380. }
  381. } else {
  382. return -ENOMEM;
  383. }
  384. }
  385. inode = child->d_inode;
  386. ino = inode->i_ino;
  387. type = inode->i_mode >> 12;
  388. dput(child);
  389. return !!filldir(dirent, qname.name, qname.len, filp->f_pos, ino, type);
  390. }
  391. static int scan(struct ctl_table_header *head, ctl_table *table,
  392. unsigned long *pos, struct file *file,
  393. void *dirent, filldir_t filldir)
  394. {
  395. for (; table->procname; table++, (*pos)++) {
  396. int res;
  397. if (*pos < file->f_pos)
  398. continue;
  399. res = proc_sys_fill_cache(file, dirent, filldir, head, table);
  400. if (res)
  401. return res;
  402. file->f_pos = *pos + 1;
  403. }
  404. return 0;
  405. }
  406. static int proc_sys_readdir(struct file *filp, void *dirent, filldir_t filldir)
  407. {
  408. struct dentry *dentry = filp->f_path.dentry;
  409. struct inode *inode = dentry->d_inode;
  410. struct ctl_table_header *head = grab_header(inode);
  411. struct ctl_table *table = PROC_I(inode)->sysctl_entry;
  412. struct ctl_table_header *h = NULL;
  413. unsigned long pos;
  414. int ret = -EINVAL;
  415. if (IS_ERR(head))
  416. return PTR_ERR(head);
  417. if (table && !table->child) {
  418. WARN_ON(1);
  419. goto out;
  420. }
  421. table = table ? table->child : head->ctl_table;
  422. ret = 0;
  423. /* Avoid a switch here: arm builds fail with missing __cmpdi2 */
  424. if (filp->f_pos == 0) {
  425. if (filldir(dirent, ".", 1, filp->f_pos,
  426. inode->i_ino, DT_DIR) < 0)
  427. goto out;
  428. filp->f_pos++;
  429. }
  430. if (filp->f_pos == 1) {
  431. if (filldir(dirent, "..", 2, filp->f_pos,
  432. parent_ino(dentry), DT_DIR) < 0)
  433. goto out;
  434. filp->f_pos++;
  435. }
  436. pos = 2;
  437. ret = scan(head, table, &pos, filp, dirent, filldir);
  438. if (ret)
  439. goto out;
  440. for (h = sysctl_head_next(NULL); h; h = sysctl_head_next(h)) {
  441. if (h->attached_to != table)
  442. continue;
  443. ret = scan(h, h->attached_by, &pos, filp, dirent, filldir);
  444. if (ret) {
  445. sysctl_head_finish(h);
  446. break;
  447. }
  448. }
  449. ret = 1;
  450. out:
  451. sysctl_head_finish(head);
  452. return ret;
  453. }
  454. static int proc_sys_permission(struct inode *inode, int mask)
  455. {
  456. /*
  457. * sysctl entries that are not writeable,
  458. * are _NOT_ writeable, capabilities or not.
  459. */
  460. struct ctl_table_header *head;
  461. struct ctl_table *table;
  462. int error;
  463. /* Executable files are not allowed under /proc/sys/ */
  464. if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))
  465. return -EACCES;
  466. head = grab_header(inode);
  467. if (IS_ERR(head))
  468. return PTR_ERR(head);
  469. table = PROC_I(inode)->sysctl_entry;
  470. if (!table) /* global root - r-xr-xr-x */
  471. error = mask & MAY_WRITE ? -EACCES : 0;
  472. else /* Use the permissions on the sysctl table entry */
  473. error = sysctl_perm(head->root, table, mask & ~MAY_NOT_BLOCK);
  474. sysctl_head_finish(head);
  475. return error;
  476. }
  477. static int proc_sys_setattr(struct dentry *dentry, struct iattr *attr)
  478. {
  479. struct inode *inode = dentry->d_inode;
  480. int error;
  481. if (attr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
  482. return -EPERM;
  483. error = inode_change_ok(inode, attr);
  484. if (error)
  485. return error;
  486. if ((attr->ia_valid & ATTR_SIZE) &&
  487. attr->ia_size != i_size_read(inode)) {
  488. error = vmtruncate(inode, attr->ia_size);
  489. if (error)
  490. return error;
  491. }
  492. setattr_copy(inode, attr);
  493. mark_inode_dirty(inode);
  494. return 0;
  495. }
  496. static int proc_sys_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
  497. {
  498. struct inode *inode = dentry->d_inode;
  499. struct ctl_table_header *head = grab_header(inode);
  500. struct ctl_table *table = PROC_I(inode)->sysctl_entry;
  501. if (IS_ERR(head))
  502. return PTR_ERR(head);
  503. generic_fillattr(inode, stat);
  504. if (table)
  505. stat->mode = (stat->mode & S_IFMT) | table->mode;
  506. sysctl_head_finish(head);
  507. return 0;
  508. }
  509. static const struct file_operations proc_sys_file_operations = {
  510. .open = proc_sys_open,
  511. .poll = proc_sys_poll,
  512. .read = proc_sys_read,
  513. .write = proc_sys_write,
  514. .llseek = default_llseek,
  515. };
  516. static const struct file_operations proc_sys_dir_file_operations = {
  517. .read = generic_read_dir,
  518. .readdir = proc_sys_readdir,
  519. .llseek = generic_file_llseek,
  520. };
  521. static const struct inode_operations proc_sys_inode_operations = {
  522. .permission = proc_sys_permission,
  523. .setattr = proc_sys_setattr,
  524. .getattr = proc_sys_getattr,
  525. };
  526. static const struct inode_operations proc_sys_dir_operations = {
  527. .lookup = proc_sys_lookup,
  528. .permission = proc_sys_permission,
  529. .setattr = proc_sys_setattr,
  530. .getattr = proc_sys_getattr,
  531. };
  532. static int proc_sys_revalidate(struct dentry *dentry, struct nameidata *nd)
  533. {
  534. if (nd->flags & LOOKUP_RCU)
  535. return -ECHILD;
  536. return !PROC_I(dentry->d_inode)->sysctl->unregistering;
  537. }
  538. static int proc_sys_delete(const struct dentry *dentry)
  539. {
  540. return !!PROC_I(dentry->d_inode)->sysctl->unregistering;
  541. }
  542. static int sysctl_is_seen(struct ctl_table_header *p)
  543. {
  544. struct ctl_table_set *set = p->set;
  545. int res;
  546. spin_lock(&sysctl_lock);
  547. if (p->unregistering)
  548. res = 0;
  549. else if (!set->is_seen)
  550. res = 1;
  551. else
  552. res = set->is_seen(set);
  553. spin_unlock(&sysctl_lock);
  554. return res;
  555. }
  556. static int proc_sys_compare(const struct dentry *parent,
  557. const struct inode *pinode,
  558. const struct dentry *dentry, const struct inode *inode,
  559. unsigned int len, const char *str, const struct qstr *name)
  560. {
  561. struct ctl_table_header *head;
  562. /* Although proc doesn't have negative dentries, rcu-walk means
  563. * that inode here can be NULL */
  564. /* AV: can it, indeed? */
  565. if (!inode)
  566. return 1;
  567. if (name->len != len)
  568. return 1;
  569. if (memcmp(name->name, str, len))
  570. return 1;
  571. head = rcu_dereference(PROC_I(inode)->sysctl);
  572. return !head || !sysctl_is_seen(head);
  573. }
  574. static const struct dentry_operations proc_sys_dentry_operations = {
  575. .d_revalidate = proc_sys_revalidate,
  576. .d_delete = proc_sys_delete,
  577. .d_compare = proc_sys_compare,
  578. };
  579. static struct ctl_table *is_branch_in(struct ctl_table *branch,
  580. struct ctl_table *table)
  581. {
  582. struct ctl_table *p;
  583. const char *s = branch->procname;
  584. /* branch should have named subdirectory as its first element */
  585. if (!s || !branch->child)
  586. return NULL;
  587. /* ... and nothing else */
  588. if (branch[1].procname)
  589. return NULL;
  590. /* table should contain subdirectory with the same name */
  591. for (p = table; p->procname; p++) {
  592. if (!p->child)
  593. continue;
  594. if (p->procname && strcmp(p->procname, s) == 0)
  595. return p;
  596. }
  597. return NULL;
  598. }
  599. /* see if attaching q to p would be an improvement */
  600. static void try_attach(struct ctl_table_header *p, struct ctl_table_header *q)
  601. {
  602. struct ctl_table *to = p->ctl_table, *by = q->ctl_table;
  603. struct ctl_table *next;
  604. int is_better = 0;
  605. int not_in_parent = !p->attached_by;
  606. while ((next = is_branch_in(by, to)) != NULL) {
  607. if (by == q->attached_by)
  608. is_better = 1;
  609. if (to == p->attached_by)
  610. not_in_parent = 1;
  611. by = by->child;
  612. to = next->child;
  613. }
  614. if (is_better && not_in_parent) {
  615. q->attached_by = by;
  616. q->attached_to = to;
  617. q->parent = p;
  618. }
  619. }
  620. #ifdef CONFIG_SYSCTL_SYSCALL_CHECK
  621. static int sysctl_depth(struct ctl_table *table)
  622. {
  623. struct ctl_table *tmp;
  624. int depth;
  625. depth = 0;
  626. for (tmp = table; tmp->parent; tmp = tmp->parent)
  627. depth++;
  628. return depth;
  629. }
  630. static struct ctl_table *sysctl_parent(struct ctl_table *table, int n)
  631. {
  632. int i;
  633. for (i = 0; table && i < n; i++)
  634. table = table->parent;
  635. return table;
  636. }
  637. static void sysctl_print_path(struct ctl_table *table)
  638. {
  639. struct ctl_table *tmp;
  640. int depth, i;
  641. depth = sysctl_depth(table);
  642. if (table->procname) {
  643. for (i = depth; i >= 0; i--) {
  644. tmp = sysctl_parent(table, i);
  645. printk("/%s", tmp->procname?tmp->procname:"");
  646. }
  647. }
  648. printk(" ");
  649. }
  650. static struct ctl_table *sysctl_check_lookup(struct nsproxy *namespaces,
  651. struct ctl_table *table)
  652. {
  653. struct ctl_table_header *head;
  654. struct ctl_table *ref, *test;
  655. int depth, cur_depth;
  656. depth = sysctl_depth(table);
  657. for (head = __sysctl_head_next(namespaces, NULL); head;
  658. head = __sysctl_head_next(namespaces, head)) {
  659. cur_depth = depth;
  660. ref = head->ctl_table;
  661. repeat:
  662. test = sysctl_parent(table, cur_depth);
  663. for (; ref->procname; ref++) {
  664. int match = 0;
  665. if (cur_depth && !ref->child)
  666. continue;
  667. if (test->procname && ref->procname &&
  668. (strcmp(test->procname, ref->procname) == 0))
  669. match++;
  670. if (match) {
  671. if (cur_depth != 0) {
  672. cur_depth--;
  673. ref = ref->child;
  674. goto repeat;
  675. }
  676. goto out;
  677. }
  678. }
  679. }
  680. ref = NULL;
  681. out:
  682. sysctl_head_finish(head);
  683. return ref;
  684. }
  685. static void set_fail(const char **fail, struct ctl_table *table, const char *str)
  686. {
  687. if (*fail) {
  688. printk(KERN_ERR "sysctl table check failed: ");
  689. sysctl_print_path(table);
  690. printk(" %s\n", *fail);
  691. dump_stack();
  692. }
  693. *fail = str;
  694. }
  695. static void sysctl_check_leaf(struct nsproxy *namespaces,
  696. struct ctl_table *table, const char **fail)
  697. {
  698. struct ctl_table *ref;
  699. ref = sysctl_check_lookup(namespaces, table);
  700. if (ref && (ref != table))
  701. set_fail(fail, table, "Sysctl already exists");
  702. }
  703. static int sysctl_check_table(struct nsproxy *namespaces, struct ctl_table *table)
  704. {
  705. int error = 0;
  706. for (; table->procname; table++) {
  707. const char *fail = NULL;
  708. if (table->parent) {
  709. if (!table->parent->procname)
  710. set_fail(&fail, table, "Parent without procname");
  711. }
  712. if (table->child) {
  713. if (table->data)
  714. set_fail(&fail, table, "Directory with data?");
  715. if (table->maxlen)
  716. set_fail(&fail, table, "Directory with maxlen?");
  717. if ((table->mode & (S_IRUGO|S_IXUGO)) != table->mode)
  718. set_fail(&fail, table, "Writable sysctl directory");
  719. if (table->proc_handler)
  720. set_fail(&fail, table, "Directory with proc_handler");
  721. if (table->extra1)
  722. set_fail(&fail, table, "Directory with extra1");
  723. if (table->extra2)
  724. set_fail(&fail, table, "Directory with extra2");
  725. } else {
  726. if ((table->proc_handler == proc_dostring) ||
  727. (table->proc_handler == proc_dointvec) ||
  728. (table->proc_handler == proc_dointvec_minmax) ||
  729. (table->proc_handler == proc_dointvec_jiffies) ||
  730. (table->proc_handler == proc_dointvec_userhz_jiffies) ||
  731. (table->proc_handler == proc_dointvec_ms_jiffies) ||
  732. (table->proc_handler == proc_doulongvec_minmax) ||
  733. (table->proc_handler == proc_doulongvec_ms_jiffies_minmax)) {
  734. if (!table->data)
  735. set_fail(&fail, table, "No data");
  736. if (!table->maxlen)
  737. set_fail(&fail, table, "No maxlen");
  738. }
  739. #ifdef CONFIG_PROC_SYSCTL
  740. if (!table->proc_handler)
  741. set_fail(&fail, table, "No proc_handler");
  742. #endif
  743. sysctl_check_leaf(namespaces, table, &fail);
  744. }
  745. if (table->mode > 0777)
  746. set_fail(&fail, table, "bogus .mode");
  747. if (fail) {
  748. set_fail(&fail, table, NULL);
  749. error = -EINVAL;
  750. }
  751. if (table->child)
  752. error |= sysctl_check_table(namespaces, table->child);
  753. }
  754. return error;
  755. }
  756. #endif /* CONFIG_SYSCTL_SYSCALL_CHECK */
  757. /**
  758. * __register_sysctl_paths - register a sysctl hierarchy
  759. * @root: List of sysctl headers to register on
  760. * @namespaces: Data to compute which lists of sysctl entries are visible
  761. * @path: The path to the directory the sysctl table is in.
  762. * @table: the top-level table structure
  763. *
  764. * Register a sysctl table hierarchy. @table should be a filled in ctl_table
  765. * array. A completely 0 filled entry terminates the table.
  766. *
  767. * The members of the &struct ctl_table structure are used as follows:
  768. *
  769. * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not
  770. * enter a sysctl file
  771. *
  772. * data - a pointer to data for use by proc_handler
  773. *
  774. * maxlen - the maximum size in bytes of the data
  775. *
  776. * mode - the file permissions for the /proc/sys file, and for sysctl(2)
  777. *
  778. * child - a pointer to the child sysctl table if this entry is a directory, or
  779. * %NULL.
  780. *
  781. * proc_handler - the text handler routine (described below)
  782. *
  783. * de - for internal use by the sysctl routines
  784. *
  785. * extra1, extra2 - extra pointers usable by the proc handler routines
  786. *
  787. * Leaf nodes in the sysctl tree will be represented by a single file
  788. * under /proc; non-leaf nodes will be represented by directories.
  789. *
  790. * sysctl(2) can automatically manage read and write requests through
  791. * the sysctl table. The data and maxlen fields of the ctl_table
  792. * struct enable minimal validation of the values being written to be
  793. * performed, and the mode field allows minimal authentication.
  794. *
  795. * There must be a proc_handler routine for any terminal nodes
  796. * mirrored under /proc/sys (non-terminals are handled by a built-in
  797. * directory handler). Several default handlers are available to
  798. * cover common cases -
  799. *
  800. * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(),
  801. * proc_dointvec_userhz_jiffies(), proc_dointvec_minmax(),
  802. * proc_doulongvec_ms_jiffies_minmax(), proc_doulongvec_minmax()
  803. *
  804. * It is the handler's job to read the input buffer from user memory
  805. * and process it. The handler should return 0 on success.
  806. *
  807. * This routine returns %NULL on a failure to register, and a pointer
  808. * to the table header on success.
  809. */
  810. struct ctl_table_header *__register_sysctl_paths(
  811. struct ctl_table_root *root,
  812. struct nsproxy *namespaces,
  813. const struct ctl_path *path, struct ctl_table *table)
  814. {
  815. struct ctl_table_header *header;
  816. struct ctl_table *new, **prevp;
  817. unsigned int n, npath;
  818. struct ctl_table_set *set;
  819. /* Count the path components */
  820. for (npath = 0; path[npath].procname; ++npath)
  821. ;
  822. /*
  823. * For each path component, allocate a 2-element ctl_table array.
  824. * The first array element will be filled with the sysctl entry
  825. * for this, the second will be the sentinel (procname == 0).
  826. *
  827. * We allocate everything in one go so that we don't have to
  828. * worry about freeing additional memory in unregister_sysctl_table.
  829. */
  830. header = kzalloc(sizeof(struct ctl_table_header) +
  831. (2 * npath * sizeof(struct ctl_table)), GFP_KERNEL);
  832. if (!header)
  833. return NULL;
  834. new = (struct ctl_table *) (header + 1);
  835. /* Now connect the dots */
  836. prevp = &header->ctl_table;
  837. for (n = 0; n < npath; ++n, ++path) {
  838. /* Copy the procname */
  839. new->procname = path->procname;
  840. new->mode = 0555;
  841. *prevp = new;
  842. prevp = &new->child;
  843. new += 2;
  844. }
  845. *prevp = table;
  846. header->ctl_table_arg = table;
  847. INIT_LIST_HEAD(&header->ctl_entry);
  848. header->used = 0;
  849. header->unregistering = NULL;
  850. header->root = root;
  851. sysctl_set_parent(NULL, header->ctl_table);
  852. header->count = 1;
  853. #ifdef CONFIG_SYSCTL_SYSCALL_CHECK
  854. if (sysctl_check_table(namespaces, header->ctl_table)) {
  855. kfree(header);
  856. return NULL;
  857. }
  858. #endif
  859. spin_lock(&sysctl_lock);
  860. header->set = lookup_header_set(root, namespaces);
  861. header->attached_by = header->ctl_table;
  862. header->attached_to = root_table;
  863. header->parent = &root_table_header;
  864. for (set = header->set; set; set = set->parent) {
  865. struct ctl_table_header *p;
  866. list_for_each_entry(p, &set->list, ctl_entry) {
  867. if (p->unregistering)
  868. continue;
  869. try_attach(p, header);
  870. }
  871. }
  872. header->parent->count++;
  873. list_add_tail(&header->ctl_entry, &header->set->list);
  874. spin_unlock(&sysctl_lock);
  875. return header;
  876. }
  877. /**
  878. * register_sysctl_table_path - register a sysctl table hierarchy
  879. * @path: The path to the directory the sysctl table is in.
  880. * @table: the top-level table structure
  881. *
  882. * Register a sysctl table hierarchy. @table should be a filled in ctl_table
  883. * array. A completely 0 filled entry terminates the table.
  884. *
  885. * See __register_sysctl_paths for more details.
  886. */
  887. struct ctl_table_header *register_sysctl_paths(const struct ctl_path *path,
  888. struct ctl_table *table)
  889. {
  890. return __register_sysctl_paths(&sysctl_table_root, current->nsproxy,
  891. path, table);
  892. }
  893. EXPORT_SYMBOL(register_sysctl_paths);
  894. /**
  895. * register_sysctl_table - register a sysctl table hierarchy
  896. * @table: the top-level table structure
  897. *
  898. * Register a sysctl table hierarchy. @table should be a filled in ctl_table
  899. * array. A completely 0 filled entry terminates the table.
  900. *
  901. * See register_sysctl_paths for more details.
  902. */
  903. struct ctl_table_header *register_sysctl_table(struct ctl_table *table)
  904. {
  905. static const struct ctl_path null_path[] = { {} };
  906. return register_sysctl_paths(null_path, table);
  907. }
  908. EXPORT_SYMBOL(register_sysctl_table);
  909. /**
  910. * unregister_sysctl_table - unregister a sysctl table hierarchy
  911. * @header: the header returned from register_sysctl_table
  912. *
  913. * Unregisters the sysctl table and all children. proc entries may not
  914. * actually be removed until they are no longer used by anyone.
  915. */
  916. void unregister_sysctl_table(struct ctl_table_header * header)
  917. {
  918. might_sleep();
  919. if (header == NULL)
  920. return;
  921. spin_lock(&sysctl_lock);
  922. start_unregistering(header);
  923. if (!--header->parent->count) {
  924. WARN_ON(1);
  925. kfree_rcu(header->parent, rcu);
  926. }
  927. if (!--header->count)
  928. kfree_rcu(header, rcu);
  929. spin_unlock(&sysctl_lock);
  930. }
  931. EXPORT_SYMBOL(unregister_sysctl_table);
  932. void setup_sysctl_set(struct ctl_table_set *p,
  933. struct ctl_table_set *parent,
  934. int (*is_seen)(struct ctl_table_set *))
  935. {
  936. INIT_LIST_HEAD(&p->list);
  937. p->parent = parent ? parent : &sysctl_table_root.default_set;
  938. p->is_seen = is_seen;
  939. }
  940. void retire_sysctl_set(struct ctl_table_set *set)
  941. {
  942. WARN_ON(!list_empty(&set->list));
  943. }
  944. int __init proc_sys_init(void)
  945. {
  946. struct proc_dir_entry *proc_sys_root;
  947. proc_sys_root = proc_mkdir("sys", NULL);
  948. proc_sys_root->proc_iops = &proc_sys_dir_operations;
  949. proc_sys_root->proc_fops = &proc_sys_dir_file_operations;
  950. proc_sys_root->nlink = 0;
  951. return sysctl_init();
  952. }