proc_sysctl.c 9.6 KB

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