proc_sysctl.c 11 KB

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