proc_sysctl.c 9.4 KB

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