proc_sysctl.c 33 KB

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