proc_sysctl.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270
  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. static int sysctl_check_table_dups(const char *path, struct ctl_table *old,
  621. struct ctl_table *table)
  622. {
  623. struct ctl_table *entry, *test;
  624. int error = 0;
  625. for (entry = old; entry->procname; entry++) {
  626. for (test = table; test->procname; test++) {
  627. if (strcmp(entry->procname, test->procname) == 0) {
  628. printk(KERN_ERR "sysctl duplicate entry: %s/%s\n",
  629. path, test->procname);
  630. error = -EEXIST;
  631. }
  632. }
  633. }
  634. return error;
  635. }
  636. static int sysctl_check_dups(struct nsproxy *namespaces,
  637. struct ctl_table_header *header,
  638. const char *path, struct ctl_table *table)
  639. {
  640. struct ctl_table_root *root;
  641. struct ctl_table_set *set;
  642. struct ctl_table_header *dir_head, *head;
  643. struct ctl_table *dir_table;
  644. int error = 0;
  645. /* No dups if we are the only member of our directory */
  646. if (header->attached_by != table)
  647. return 0;
  648. dir_head = header->parent;
  649. dir_table = header->attached_to;
  650. error = sysctl_check_table_dups(path, dir_table, table);
  651. root = &sysctl_table_root;
  652. do {
  653. set = lookup_header_set(root, namespaces);
  654. list_for_each_entry(head, &set->list, ctl_entry) {
  655. if (head->unregistering)
  656. continue;
  657. if (head->attached_to != dir_table)
  658. continue;
  659. error = sysctl_check_table_dups(path, head->attached_by,
  660. table);
  661. }
  662. root = list_entry(root->root_list.next,
  663. struct ctl_table_root, root_list);
  664. } while (root != &sysctl_table_root);
  665. return error;
  666. }
  667. static int sysctl_err(const char *path, struct ctl_table *table, char *fmt, ...)
  668. {
  669. struct va_format vaf;
  670. va_list args;
  671. va_start(args, fmt);
  672. vaf.fmt = fmt;
  673. vaf.va = &args;
  674. printk(KERN_ERR "sysctl table check failed: %s/%s %pV\n",
  675. path, table->procname, &vaf);
  676. va_end(args);
  677. return -EINVAL;
  678. }
  679. static int sysctl_check_table(const char *path, struct ctl_table *table)
  680. {
  681. int err = 0;
  682. for (; table->procname; table++) {
  683. if (table->child)
  684. err = sysctl_err(path, table, "Not a file");
  685. if ((table->proc_handler == proc_dostring) ||
  686. (table->proc_handler == proc_dointvec) ||
  687. (table->proc_handler == proc_dointvec_minmax) ||
  688. (table->proc_handler == proc_dointvec_jiffies) ||
  689. (table->proc_handler == proc_dointvec_userhz_jiffies) ||
  690. (table->proc_handler == proc_dointvec_ms_jiffies) ||
  691. (table->proc_handler == proc_doulongvec_minmax) ||
  692. (table->proc_handler == proc_doulongvec_ms_jiffies_minmax)) {
  693. if (!table->data)
  694. err = sysctl_err(path, table, "No data");
  695. if (!table->maxlen)
  696. err = sysctl_err(path, table, "No maxlen");
  697. }
  698. if (!table->proc_handler)
  699. err = sysctl_err(path, table, "No proc_handler");
  700. if ((table->mode & (S_IRUGO|S_IWUGO)) != table->mode)
  701. err = sysctl_err(path, table, "bogus .mode 0%o",
  702. table->mode);
  703. }
  704. return err;
  705. }
  706. /**
  707. * __register_sysctl_table - register a leaf sysctl table
  708. * @root: List of sysctl headers to register on
  709. * @namespaces: Data to compute which lists of sysctl entries are visible
  710. * @path: The path to the directory the sysctl table is in.
  711. * @table: the top-level table structure
  712. *
  713. * Register a sysctl table hierarchy. @table should be a filled in ctl_table
  714. * array. A completely 0 filled entry terminates the table.
  715. *
  716. * The members of the &struct ctl_table structure are used as follows:
  717. *
  718. * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not
  719. * enter a sysctl file
  720. *
  721. * data - a pointer to data for use by proc_handler
  722. *
  723. * maxlen - the maximum size in bytes of the data
  724. *
  725. * mode - the file permissions for the /proc/sys file
  726. *
  727. * child - must be %NULL.
  728. *
  729. * proc_handler - the text handler routine (described below)
  730. *
  731. * extra1, extra2 - extra pointers usable by the proc handler routines
  732. *
  733. * Leaf nodes in the sysctl tree will be represented by a single file
  734. * under /proc; non-leaf nodes will be represented by directories.
  735. *
  736. * There must be a proc_handler routine for any terminal nodes.
  737. * Several default handlers are available to cover common cases -
  738. *
  739. * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(),
  740. * proc_dointvec_userhz_jiffies(), proc_dointvec_minmax(),
  741. * proc_doulongvec_ms_jiffies_minmax(), proc_doulongvec_minmax()
  742. *
  743. * It is the handler's job to read the input buffer from user memory
  744. * and process it. The handler should return 0 on success.
  745. *
  746. * This routine returns %NULL on a failure to register, and a pointer
  747. * to the table header on success.
  748. */
  749. struct ctl_table_header *__register_sysctl_table(
  750. struct ctl_table_root *root,
  751. struct nsproxy *namespaces,
  752. const char *path, struct ctl_table *table)
  753. {
  754. struct ctl_table_header *header;
  755. struct ctl_table *new, **prevp;
  756. const char *name, *nextname;
  757. unsigned int npath = 0;
  758. struct ctl_table_set *set;
  759. size_t path_bytes = 0;
  760. char *new_name;
  761. /* Count the path components */
  762. for (name = path; name; name = nextname) {
  763. int namelen;
  764. nextname = strchr(name, '/');
  765. if (nextname) {
  766. namelen = nextname - name;
  767. nextname++;
  768. } else {
  769. namelen = strlen(name);
  770. }
  771. if (namelen == 0)
  772. continue;
  773. path_bytes += namelen + 1;
  774. npath++;
  775. }
  776. /*
  777. * For each path component, allocate a 2-element ctl_table array.
  778. * The first array element will be filled with the sysctl entry
  779. * for this, the second will be the sentinel (procname == 0).
  780. *
  781. * We allocate everything in one go so that we don't have to
  782. * worry about freeing additional memory in unregister_sysctl_table.
  783. */
  784. header = kzalloc(sizeof(struct ctl_table_header) + path_bytes +
  785. (2 * npath * sizeof(struct ctl_table)), GFP_KERNEL);
  786. if (!header)
  787. return NULL;
  788. new = (struct ctl_table *) (header + 1);
  789. new_name = (char *)(new + (2 * npath));
  790. /* Now connect the dots */
  791. prevp = &header->ctl_table;
  792. for (name = path; name; name = nextname) {
  793. int namelen;
  794. nextname = strchr(name, '/');
  795. if (nextname) {
  796. namelen = nextname - name;
  797. nextname++;
  798. } else {
  799. namelen = strlen(name);
  800. }
  801. if (namelen == 0)
  802. continue;
  803. memcpy(new_name, name, namelen);
  804. new_name[namelen] = '\0';
  805. new->procname = new_name;
  806. new->mode = 0555;
  807. *prevp = new;
  808. prevp = &new->child;
  809. new += 2;
  810. new_name += namelen + 1;
  811. }
  812. *prevp = table;
  813. header->ctl_table_arg = table;
  814. INIT_LIST_HEAD(&header->ctl_entry);
  815. header->used = 0;
  816. header->unregistering = NULL;
  817. header->root = root;
  818. sysctl_set_parent(NULL, header->ctl_table);
  819. header->count = 1;
  820. if (sysctl_check_table(path, table))
  821. goto fail;
  822. spin_lock(&sysctl_lock);
  823. header->set = lookup_header_set(root, namespaces);
  824. header->attached_by = header->ctl_table;
  825. header->attached_to = root_table;
  826. header->parent = &root_table_header;
  827. set = header->set;
  828. root = header->root;
  829. for (;;) {
  830. struct ctl_table_header *p;
  831. list_for_each_entry(p, &set->list, ctl_entry) {
  832. if (p->unregistering)
  833. continue;
  834. try_attach(p, header);
  835. }
  836. if (root == &sysctl_table_root)
  837. break;
  838. root = list_entry(root->root_list.prev,
  839. struct ctl_table_root, root_list);
  840. set = lookup_header_set(root, namespaces);
  841. }
  842. if (sysctl_check_dups(namespaces, header, path, table))
  843. goto fail_locked;
  844. header->parent->count++;
  845. list_add_tail(&header->ctl_entry, &header->set->list);
  846. spin_unlock(&sysctl_lock);
  847. return header;
  848. fail_locked:
  849. spin_unlock(&sysctl_lock);
  850. fail:
  851. kfree(header);
  852. dump_stack();
  853. return NULL;
  854. }
  855. static char *append_path(const char *path, char *pos, const char *name)
  856. {
  857. int namelen;
  858. namelen = strlen(name);
  859. if (((pos - path) + namelen + 2) >= PATH_MAX)
  860. return NULL;
  861. memcpy(pos, name, namelen);
  862. pos[namelen] = '/';
  863. pos[namelen + 1] = '\0';
  864. pos += namelen + 1;
  865. return pos;
  866. }
  867. static int count_subheaders(struct ctl_table *table)
  868. {
  869. int has_files = 0;
  870. int nr_subheaders = 0;
  871. struct ctl_table *entry;
  872. /* special case: no directory and empty directory */
  873. if (!table || !table->procname)
  874. return 1;
  875. for (entry = table; entry->procname; entry++) {
  876. if (entry->child)
  877. nr_subheaders += count_subheaders(entry->child);
  878. else
  879. has_files = 1;
  880. }
  881. return nr_subheaders + has_files;
  882. }
  883. static int register_leaf_sysctl_tables(const char *path, char *pos,
  884. struct ctl_table_header ***subheader,
  885. struct ctl_table_root *root, struct nsproxy *namespaces,
  886. struct ctl_table *table)
  887. {
  888. struct ctl_table *ctl_table_arg = NULL;
  889. struct ctl_table *entry, *files;
  890. int nr_files = 0;
  891. int nr_dirs = 0;
  892. int err = -ENOMEM;
  893. for (entry = table; entry->procname; entry++) {
  894. if (entry->child)
  895. nr_dirs++;
  896. else
  897. nr_files++;
  898. }
  899. files = table;
  900. /* If there are mixed files and directories we need a new table */
  901. if (nr_dirs && nr_files) {
  902. struct ctl_table *new;
  903. files = kzalloc(sizeof(struct ctl_table) * (nr_files + 1),
  904. GFP_KERNEL);
  905. if (!files)
  906. goto out;
  907. ctl_table_arg = files;
  908. for (new = files, entry = table; entry->procname; entry++) {
  909. if (entry->child)
  910. continue;
  911. *new = *entry;
  912. new++;
  913. }
  914. }
  915. /* Register everything except a directory full of subdirectories */
  916. if (nr_files || !nr_dirs) {
  917. struct ctl_table_header *header;
  918. header = __register_sysctl_table(root, namespaces, path, files);
  919. if (!header) {
  920. kfree(ctl_table_arg);
  921. goto out;
  922. }
  923. /* Remember if we need to free the file table */
  924. header->ctl_table_arg = ctl_table_arg;
  925. **subheader = header;
  926. (*subheader)++;
  927. }
  928. /* Recurse into the subdirectories. */
  929. for (entry = table; entry->procname; entry++) {
  930. char *child_pos;
  931. if (!entry->child)
  932. continue;
  933. err = -ENAMETOOLONG;
  934. child_pos = append_path(path, pos, entry->procname);
  935. if (!child_pos)
  936. goto out;
  937. err = register_leaf_sysctl_tables(path, child_pos, subheader,
  938. root, namespaces, entry->child);
  939. pos[0] = '\0';
  940. if (err)
  941. goto out;
  942. }
  943. err = 0;
  944. out:
  945. /* On failure our caller will unregister all registered subheaders */
  946. return err;
  947. }
  948. /**
  949. * __register_sysctl_paths - register a sysctl table hierarchy
  950. * @root: List of sysctl headers to register on
  951. * @namespaces: Data to compute which lists of sysctl entries are visible
  952. * @path: The path to the directory the sysctl table is in.
  953. * @table: the top-level table structure
  954. *
  955. * Register a sysctl table hierarchy. @table should be a filled in ctl_table
  956. * array. A completely 0 filled entry terminates the table.
  957. *
  958. * See __register_sysctl_table for more details.
  959. */
  960. struct ctl_table_header *__register_sysctl_paths(
  961. struct ctl_table_root *root,
  962. struct nsproxy *namespaces,
  963. const struct ctl_path *path, struct ctl_table *table)
  964. {
  965. struct ctl_table *ctl_table_arg = table;
  966. int nr_subheaders = count_subheaders(table);
  967. struct ctl_table_header *header = NULL, **subheaders, **subheader;
  968. const struct ctl_path *component;
  969. char *new_path, *pos;
  970. pos = new_path = kmalloc(PATH_MAX, GFP_KERNEL);
  971. if (!new_path)
  972. return NULL;
  973. pos[0] = '\0';
  974. for (component = path; component->procname; component++) {
  975. pos = append_path(new_path, pos, component->procname);
  976. if (!pos)
  977. goto out;
  978. }
  979. while (table->procname && table->child && !table[1].procname) {
  980. pos = append_path(new_path, pos, table->procname);
  981. if (!pos)
  982. goto out;
  983. table = table->child;
  984. }
  985. if (nr_subheaders == 1) {
  986. header = __register_sysctl_table(root, namespaces, new_path, table);
  987. if (header)
  988. header->ctl_table_arg = ctl_table_arg;
  989. } else {
  990. header = kzalloc(sizeof(*header) +
  991. sizeof(*subheaders)*nr_subheaders, GFP_KERNEL);
  992. if (!header)
  993. goto out;
  994. subheaders = (struct ctl_table_header **) (header + 1);
  995. subheader = subheaders;
  996. header->ctl_table_arg = ctl_table_arg;
  997. if (register_leaf_sysctl_tables(new_path, pos, &subheader,
  998. root, namespaces, table))
  999. goto err_register_leaves;
  1000. }
  1001. out:
  1002. kfree(new_path);
  1003. return header;
  1004. err_register_leaves:
  1005. while (subheader > subheaders) {
  1006. struct ctl_table_header *subh = *(--subheader);
  1007. struct ctl_table *table = subh->ctl_table_arg;
  1008. unregister_sysctl_table(subh);
  1009. kfree(table);
  1010. }
  1011. kfree(header);
  1012. header = NULL;
  1013. goto out;
  1014. }
  1015. /**
  1016. * register_sysctl_table_path - register a sysctl table hierarchy
  1017. * @path: The path to the directory the sysctl table is in.
  1018. * @table: the top-level table structure
  1019. *
  1020. * Register a sysctl table hierarchy. @table should be a filled in ctl_table
  1021. * array. A completely 0 filled entry terminates the table.
  1022. *
  1023. * See __register_sysctl_paths for more details.
  1024. */
  1025. struct ctl_table_header *register_sysctl_paths(const struct ctl_path *path,
  1026. struct ctl_table *table)
  1027. {
  1028. return __register_sysctl_paths(&sysctl_table_root, current->nsproxy,
  1029. path, table);
  1030. }
  1031. EXPORT_SYMBOL(register_sysctl_paths);
  1032. /**
  1033. * register_sysctl_table - register a sysctl table hierarchy
  1034. * @table: the top-level table structure
  1035. *
  1036. * Register a sysctl table hierarchy. @table should be a filled in ctl_table
  1037. * array. A completely 0 filled entry terminates the table.
  1038. *
  1039. * See register_sysctl_paths for more details.
  1040. */
  1041. struct ctl_table_header *register_sysctl_table(struct ctl_table *table)
  1042. {
  1043. static const struct ctl_path null_path[] = { {} };
  1044. return register_sysctl_paths(null_path, table);
  1045. }
  1046. EXPORT_SYMBOL(register_sysctl_table);
  1047. /**
  1048. * unregister_sysctl_table - unregister a sysctl table hierarchy
  1049. * @header: the header returned from register_sysctl_table
  1050. *
  1051. * Unregisters the sysctl table and all children. proc entries may not
  1052. * actually be removed until they are no longer used by anyone.
  1053. */
  1054. void unregister_sysctl_table(struct ctl_table_header * header)
  1055. {
  1056. int nr_subheaders;
  1057. might_sleep();
  1058. if (header == NULL)
  1059. return;
  1060. nr_subheaders = count_subheaders(header->ctl_table_arg);
  1061. if (unlikely(nr_subheaders > 1)) {
  1062. struct ctl_table_header **subheaders;
  1063. int i;
  1064. subheaders = (struct ctl_table_header **)(header + 1);
  1065. for (i = nr_subheaders -1; i >= 0; i--) {
  1066. struct ctl_table_header *subh = subheaders[i];
  1067. struct ctl_table *table = subh->ctl_table_arg;
  1068. unregister_sysctl_table(subh);
  1069. kfree(table);
  1070. }
  1071. kfree(header);
  1072. return;
  1073. }
  1074. spin_lock(&sysctl_lock);
  1075. start_unregistering(header);
  1076. if (!--header->parent->count) {
  1077. WARN_ON(1);
  1078. kfree_rcu(header->parent, rcu);
  1079. }
  1080. if (!--header->count)
  1081. kfree_rcu(header, rcu);
  1082. spin_unlock(&sysctl_lock);
  1083. }
  1084. EXPORT_SYMBOL(unregister_sysctl_table);
  1085. void setup_sysctl_set(struct ctl_table_set *p,
  1086. int (*is_seen)(struct ctl_table_set *))
  1087. {
  1088. INIT_LIST_HEAD(&p->list);
  1089. p->is_seen = is_seen;
  1090. }
  1091. void retire_sysctl_set(struct ctl_table_set *set)
  1092. {
  1093. WARN_ON(!list_empty(&set->list));
  1094. }
  1095. int __init proc_sys_init(void)
  1096. {
  1097. struct proc_dir_entry *proc_sys_root;
  1098. proc_sys_root = proc_mkdir("sys", NULL);
  1099. proc_sys_root->proc_iops = &proc_sys_dir_operations;
  1100. proc_sys_root->proc_fops = &proc_sys_dir_file_operations;
  1101. proc_sys_root->nlink = 0;
  1102. return sysctl_init();
  1103. }