proc_sysctl.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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 "internal.h"
  11. static const struct dentry_operations proc_sys_dentry_operations;
  12. static const struct file_operations proc_sys_file_operations;
  13. static const struct inode_operations proc_sys_inode_operations;
  14. static const struct file_operations proc_sys_dir_file_operations;
  15. static const struct inode_operations proc_sys_dir_operations;
  16. void proc_sys_poll_notify(struct ctl_table_poll *poll)
  17. {
  18. if (!poll)
  19. return;
  20. atomic_inc(&poll->event);
  21. wake_up_interruptible(&poll->wait);
  22. }
  23. static struct inode *proc_sys_make_inode(struct super_block *sb,
  24. struct ctl_table_header *head, struct ctl_table *table)
  25. {
  26. struct inode *inode;
  27. struct proc_inode *ei;
  28. inode = new_inode(sb);
  29. if (!inode)
  30. goto out;
  31. inode->i_ino = get_next_ino();
  32. sysctl_head_get(head);
  33. ei = PROC_I(inode);
  34. ei->sysctl = head;
  35. ei->sysctl_entry = table;
  36. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  37. inode->i_mode = table->mode;
  38. if (!table->child) {
  39. inode->i_mode |= S_IFREG;
  40. inode->i_op = &proc_sys_inode_operations;
  41. inode->i_fop = &proc_sys_file_operations;
  42. } else {
  43. inode->i_mode |= S_IFDIR;
  44. clear_nlink(inode);
  45. inode->i_op = &proc_sys_dir_operations;
  46. inode->i_fop = &proc_sys_dir_file_operations;
  47. }
  48. out:
  49. return inode;
  50. }
  51. static struct ctl_table *find_in_table(struct ctl_table *p, struct qstr *name)
  52. {
  53. int len;
  54. for ( ; p->procname; p++) {
  55. if (!p->procname)
  56. continue;
  57. len = strlen(p->procname);
  58. if (len != name->len)
  59. continue;
  60. if (memcmp(p->procname, name->name, len) != 0)
  61. continue;
  62. /* I have a match */
  63. return p;
  64. }
  65. return NULL;
  66. }
  67. static struct ctl_table_header *grab_header(struct inode *inode)
  68. {
  69. if (PROC_I(inode)->sysctl)
  70. return sysctl_head_grab(PROC_I(inode)->sysctl);
  71. else
  72. return sysctl_head_next(NULL);
  73. }
  74. static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry,
  75. struct nameidata *nd)
  76. {
  77. struct ctl_table_header *head = grab_header(dir);
  78. struct ctl_table *table = PROC_I(dir)->sysctl_entry;
  79. struct ctl_table_header *h = NULL;
  80. struct qstr *name = &dentry->d_name;
  81. struct ctl_table *p;
  82. struct inode *inode;
  83. struct dentry *err = ERR_PTR(-ENOENT);
  84. if (IS_ERR(head))
  85. return ERR_CAST(head);
  86. if (table && !table->child) {
  87. WARN_ON(1);
  88. goto out;
  89. }
  90. table = table ? table->child : head->ctl_table;
  91. p = find_in_table(table, name);
  92. if (!p) {
  93. for (h = sysctl_head_next(NULL); h; h = sysctl_head_next(h)) {
  94. if (h->attached_to != table)
  95. continue;
  96. p = find_in_table(h->attached_by, name);
  97. if (p)
  98. break;
  99. }
  100. }
  101. if (!p)
  102. goto out;
  103. err = ERR_PTR(-ENOMEM);
  104. inode = proc_sys_make_inode(dir->i_sb, h ? h : head, p);
  105. if (h)
  106. sysctl_head_finish(h);
  107. if (!inode)
  108. goto out;
  109. err = NULL;
  110. d_set_d_op(dentry, &proc_sys_dentry_operations);
  111. d_add(dentry, inode);
  112. out:
  113. sysctl_head_finish(head);
  114. return err;
  115. }
  116. static ssize_t proc_sys_call_handler(struct file *filp, void __user *buf,
  117. size_t count, loff_t *ppos, int write)
  118. {
  119. struct inode *inode = filp->f_path.dentry->d_inode;
  120. struct ctl_table_header *head = grab_header(inode);
  121. struct ctl_table *table = PROC_I(inode)->sysctl_entry;
  122. ssize_t error;
  123. size_t res;
  124. if (IS_ERR(head))
  125. return PTR_ERR(head);
  126. /*
  127. * At this point we know that the sysctl was not unregistered
  128. * and won't be until we finish.
  129. */
  130. error = -EPERM;
  131. if (sysctl_perm(head->root, table, write ? MAY_WRITE : MAY_READ))
  132. goto out;
  133. /* if that can happen at all, it should be -EINVAL, not -EISDIR */
  134. error = -EINVAL;
  135. if (!table->proc_handler)
  136. goto out;
  137. /* careful: calling conventions are nasty here */
  138. res = count;
  139. error = table->proc_handler(table, write, buf, &res, ppos);
  140. if (!error)
  141. error = res;
  142. out:
  143. sysctl_head_finish(head);
  144. return error;
  145. }
  146. static ssize_t proc_sys_read(struct file *filp, char __user *buf,
  147. size_t count, loff_t *ppos)
  148. {
  149. return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 0);
  150. }
  151. static ssize_t proc_sys_write(struct file *filp, const char __user *buf,
  152. size_t count, loff_t *ppos)
  153. {
  154. return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 1);
  155. }
  156. static int proc_sys_open(struct inode *inode, struct file *filp)
  157. {
  158. struct ctl_table *table = PROC_I(inode)->sysctl_entry;
  159. if (table->poll)
  160. filp->private_data = proc_sys_poll_event(table->poll);
  161. return 0;
  162. }
  163. static unsigned int proc_sys_poll(struct file *filp, poll_table *wait)
  164. {
  165. struct inode *inode = filp->f_path.dentry->d_inode;
  166. struct ctl_table *table = PROC_I(inode)->sysctl_entry;
  167. unsigned long event = (unsigned long)filp->private_data;
  168. unsigned int ret = DEFAULT_POLLMASK;
  169. if (!table->proc_handler)
  170. goto out;
  171. if (!table->poll)
  172. goto out;
  173. poll_wait(filp, &table->poll->wait, wait);
  174. if (event != atomic_read(&table->poll->event)) {
  175. filp->private_data = proc_sys_poll_event(table->poll);
  176. ret = POLLIN | POLLRDNORM | POLLERR | POLLPRI;
  177. }
  178. out:
  179. return ret;
  180. }
  181. static int proc_sys_fill_cache(struct file *filp, void *dirent,
  182. filldir_t filldir,
  183. struct ctl_table_header *head,
  184. struct ctl_table *table)
  185. {
  186. struct dentry *child, *dir = filp->f_path.dentry;
  187. struct inode *inode;
  188. struct qstr qname;
  189. ino_t ino = 0;
  190. unsigned type = DT_UNKNOWN;
  191. qname.name = table->procname;
  192. qname.len = strlen(table->procname);
  193. qname.hash = full_name_hash(qname.name, qname.len);
  194. child = d_lookup(dir, &qname);
  195. if (!child) {
  196. child = d_alloc(dir, &qname);
  197. if (child) {
  198. inode = proc_sys_make_inode(dir->d_sb, head, table);
  199. if (!inode) {
  200. dput(child);
  201. return -ENOMEM;
  202. } else {
  203. d_set_d_op(child, &proc_sys_dentry_operations);
  204. d_add(child, inode);
  205. }
  206. } else {
  207. return -ENOMEM;
  208. }
  209. }
  210. inode = child->d_inode;
  211. ino = inode->i_ino;
  212. type = inode->i_mode >> 12;
  213. dput(child);
  214. return !!filldir(dirent, qname.name, qname.len, filp->f_pos, ino, type);
  215. }
  216. static int scan(struct ctl_table_header *head, ctl_table *table,
  217. unsigned long *pos, struct file *file,
  218. void *dirent, filldir_t filldir)
  219. {
  220. for (; table->procname; table++, (*pos)++) {
  221. int res;
  222. /* Can't do anything without a proc name */
  223. if (!table->procname)
  224. continue;
  225. if (*pos < file->f_pos)
  226. continue;
  227. res = proc_sys_fill_cache(file, dirent, filldir, head, table);
  228. if (res)
  229. return res;
  230. file->f_pos = *pos + 1;
  231. }
  232. return 0;
  233. }
  234. static int proc_sys_readdir(struct file *filp, void *dirent, filldir_t filldir)
  235. {
  236. struct dentry *dentry = filp->f_path.dentry;
  237. struct inode *inode = dentry->d_inode;
  238. struct ctl_table_header *head = grab_header(inode);
  239. struct ctl_table *table = PROC_I(inode)->sysctl_entry;
  240. struct ctl_table_header *h = NULL;
  241. unsigned long pos;
  242. int ret = -EINVAL;
  243. if (IS_ERR(head))
  244. return PTR_ERR(head);
  245. if (table && !table->child) {
  246. WARN_ON(1);
  247. goto out;
  248. }
  249. table = table ? table->child : head->ctl_table;
  250. ret = 0;
  251. /* Avoid a switch here: arm builds fail with missing __cmpdi2 */
  252. if (filp->f_pos == 0) {
  253. if (filldir(dirent, ".", 1, filp->f_pos,
  254. inode->i_ino, DT_DIR) < 0)
  255. goto out;
  256. filp->f_pos++;
  257. }
  258. if (filp->f_pos == 1) {
  259. if (filldir(dirent, "..", 2, filp->f_pos,
  260. parent_ino(dentry), DT_DIR) < 0)
  261. goto out;
  262. filp->f_pos++;
  263. }
  264. pos = 2;
  265. ret = scan(head, table, &pos, filp, dirent, filldir);
  266. if (ret)
  267. goto out;
  268. for (h = sysctl_head_next(NULL); h; h = sysctl_head_next(h)) {
  269. if (h->attached_to != table)
  270. continue;
  271. ret = scan(h, h->attached_by, &pos, filp, dirent, filldir);
  272. if (ret) {
  273. sysctl_head_finish(h);
  274. break;
  275. }
  276. }
  277. ret = 1;
  278. out:
  279. sysctl_head_finish(head);
  280. return ret;
  281. }
  282. static int proc_sys_permission(struct inode *inode, int mask)
  283. {
  284. /*
  285. * sysctl entries that are not writeable,
  286. * are _NOT_ writeable, capabilities or not.
  287. */
  288. struct ctl_table_header *head;
  289. struct ctl_table *table;
  290. int error;
  291. /* Executable files are not allowed under /proc/sys/ */
  292. if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))
  293. return -EACCES;
  294. head = grab_header(inode);
  295. if (IS_ERR(head))
  296. return PTR_ERR(head);
  297. table = PROC_I(inode)->sysctl_entry;
  298. if (!table) /* global root - r-xr-xr-x */
  299. error = mask & MAY_WRITE ? -EACCES : 0;
  300. else /* Use the permissions on the sysctl table entry */
  301. error = sysctl_perm(head->root, table, mask & ~MAY_NOT_BLOCK);
  302. sysctl_head_finish(head);
  303. return error;
  304. }
  305. static int proc_sys_setattr(struct dentry *dentry, struct iattr *attr)
  306. {
  307. struct inode *inode = dentry->d_inode;
  308. int error;
  309. if (attr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
  310. return -EPERM;
  311. error = inode_change_ok(inode, attr);
  312. if (error)
  313. return error;
  314. if ((attr->ia_valid & ATTR_SIZE) &&
  315. attr->ia_size != i_size_read(inode)) {
  316. error = vmtruncate(inode, attr->ia_size);
  317. if (error)
  318. return error;
  319. }
  320. setattr_copy(inode, attr);
  321. mark_inode_dirty(inode);
  322. return 0;
  323. }
  324. static int proc_sys_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
  325. {
  326. struct inode *inode = dentry->d_inode;
  327. struct ctl_table_header *head = grab_header(inode);
  328. struct ctl_table *table = PROC_I(inode)->sysctl_entry;
  329. if (IS_ERR(head))
  330. return PTR_ERR(head);
  331. generic_fillattr(inode, stat);
  332. if (table)
  333. stat->mode = (stat->mode & S_IFMT) | table->mode;
  334. sysctl_head_finish(head);
  335. return 0;
  336. }
  337. static const struct file_operations proc_sys_file_operations = {
  338. .open = proc_sys_open,
  339. .poll = proc_sys_poll,
  340. .read = proc_sys_read,
  341. .write = proc_sys_write,
  342. .llseek = default_llseek,
  343. };
  344. static const struct file_operations proc_sys_dir_file_operations = {
  345. .read = generic_read_dir,
  346. .readdir = proc_sys_readdir,
  347. .llseek = generic_file_llseek,
  348. };
  349. static const struct inode_operations proc_sys_inode_operations = {
  350. .permission = proc_sys_permission,
  351. .setattr = proc_sys_setattr,
  352. .getattr = proc_sys_getattr,
  353. };
  354. static const struct inode_operations proc_sys_dir_operations = {
  355. .lookup = proc_sys_lookup,
  356. .permission = proc_sys_permission,
  357. .setattr = proc_sys_setattr,
  358. .getattr = proc_sys_getattr,
  359. };
  360. static int proc_sys_revalidate(struct dentry *dentry, struct nameidata *nd)
  361. {
  362. if (nd->flags & LOOKUP_RCU)
  363. return -ECHILD;
  364. return !PROC_I(dentry->d_inode)->sysctl->unregistering;
  365. }
  366. static int proc_sys_delete(const struct dentry *dentry)
  367. {
  368. return !!PROC_I(dentry->d_inode)->sysctl->unregistering;
  369. }
  370. static int proc_sys_compare(const struct dentry *parent,
  371. const struct inode *pinode,
  372. const struct dentry *dentry, const struct inode *inode,
  373. unsigned int len, const char *str, const struct qstr *name)
  374. {
  375. struct ctl_table_header *head;
  376. /* Although proc doesn't have negative dentries, rcu-walk means
  377. * that inode here can be NULL */
  378. /* AV: can it, indeed? */
  379. if (!inode)
  380. return 1;
  381. if (name->len != len)
  382. return 1;
  383. if (memcmp(name->name, str, len))
  384. return 1;
  385. head = rcu_dereference(PROC_I(inode)->sysctl);
  386. return !head || !sysctl_is_seen(head);
  387. }
  388. static const struct dentry_operations proc_sys_dentry_operations = {
  389. .d_revalidate = proc_sys_revalidate,
  390. .d_delete = proc_sys_delete,
  391. .d_compare = proc_sys_compare,
  392. };
  393. int __init proc_sys_init(void)
  394. {
  395. struct proc_dir_entry *proc_sys_root;
  396. proc_sys_root = proc_mkdir("sys", NULL);
  397. proc_sys_root->proc_iops = &proc_sys_dir_operations;
  398. proc_sys_root->proc_fops = &proc_sys_dir_file_operations;
  399. proc_sys_root->nlink = 0;
  400. return 0;
  401. }