proc_sysctl.c 11 KB

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