proc_sysctl.c 10 KB

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