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