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