proc_sysctl.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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 struct inode_operations proc_sys_inode_operations;
  11. static void proc_sys_refresh_inode(struct inode *inode, struct ctl_table *table)
  12. {
  13. /* Refresh the cached information bits in the inode */
  14. if (table) {
  15. inode->i_uid = 0;
  16. inode->i_gid = 0;
  17. inode->i_mode = table->mode;
  18. if (table->proc_handler) {
  19. inode->i_mode |= S_IFREG;
  20. inode->i_nlink = 1;
  21. } else {
  22. inode->i_mode |= S_IFDIR;
  23. inode->i_nlink = 0; /* It is too hard to figure out */
  24. }
  25. }
  26. }
  27. static struct inode *proc_sys_make_inode(struct inode *dir, struct ctl_table *table)
  28. {
  29. struct inode *inode;
  30. struct proc_inode *dir_ei, *ei;
  31. int depth;
  32. inode = new_inode(dir->i_sb);
  33. if (!inode)
  34. goto out;
  35. /* A directory is always one deeper than it's parent */
  36. dir_ei = PROC_I(dir);
  37. depth = dir_ei->fd + 1;
  38. ei = PROC_I(inode);
  39. ei->fd = depth;
  40. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  41. inode->i_op = &proc_sys_inode_operations;
  42. inode->i_fop = &proc_sys_file_operations;
  43. inode->i_flags |= S_PRIVATE; /* tell selinux to ignore this inode */
  44. proc_sys_refresh_inode(inode, table);
  45. out:
  46. return inode;
  47. }
  48. static struct dentry *proc_sys_ancestor(struct dentry *dentry, int depth)
  49. {
  50. for (;;) {
  51. struct proc_inode *ei;
  52. ei = PROC_I(dentry->d_inode);
  53. if (ei->fd == depth)
  54. break; /* found */
  55. dentry = dentry->d_parent;
  56. }
  57. return dentry;
  58. }
  59. static struct ctl_table *proc_sys_lookup_table_one(struct ctl_table *table,
  60. struct qstr *name)
  61. {
  62. int len;
  63. for ( ; table->ctl_name || table->procname; table++) {
  64. if (!table->procname)
  65. continue;
  66. len = strlen(table->procname);
  67. if (len != name->len)
  68. continue;
  69. if (memcmp(table->procname, name->name, len) != 0)
  70. continue;
  71. /* I have a match */
  72. return table;
  73. }
  74. return NULL;
  75. }
  76. static struct ctl_table *proc_sys_lookup_table(struct dentry *dentry,
  77. struct ctl_table *table)
  78. {
  79. struct dentry *ancestor;
  80. struct proc_inode *ei;
  81. int depth, i;
  82. ei = PROC_I(dentry->d_inode);
  83. depth = ei->fd;
  84. if (depth == 0)
  85. return table;
  86. for (i = 1; table && (i <= depth); i++) {
  87. ancestor = proc_sys_ancestor(dentry, i);
  88. table = proc_sys_lookup_table_one(table, &ancestor->d_name);
  89. if (table)
  90. table = table->child;
  91. }
  92. return table;
  93. }
  94. static struct ctl_table *proc_sys_lookup_entry(struct dentry *dparent,
  95. struct qstr *name,
  96. struct ctl_table *table)
  97. {
  98. table = proc_sys_lookup_table(dparent, table);
  99. if (table)
  100. table = proc_sys_lookup_table_one(table, name);
  101. return table;
  102. }
  103. static struct ctl_table *do_proc_sys_lookup(struct dentry *parent,
  104. struct qstr *name,
  105. struct ctl_table_header **ptr)
  106. {
  107. struct ctl_table_header *head;
  108. struct ctl_table *table = NULL;
  109. for (head = sysctl_head_next(NULL); head;
  110. head = sysctl_head_next(head)) {
  111. table = proc_sys_lookup_entry(parent, name, head->ctl_table);
  112. if (table)
  113. break;
  114. }
  115. *ptr = head;
  116. return table;
  117. }
  118. static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry,
  119. struct nameidata *nd)
  120. {
  121. struct ctl_table_header *head;
  122. struct inode *inode;
  123. struct dentry *err;
  124. struct ctl_table *table;
  125. err = ERR_PTR(-ENOENT);
  126. table = do_proc_sys_lookup(dentry->d_parent, &dentry->d_name, &head);
  127. if (!table)
  128. goto out;
  129. err = ERR_PTR(-ENOMEM);
  130. inode = proc_sys_make_inode(dir, table);
  131. if (!inode)
  132. goto out;
  133. err = NULL;
  134. dentry->d_op = &proc_sys_dentry_operations;
  135. d_add(dentry, inode);
  136. out:
  137. sysctl_head_finish(head);
  138. return err;
  139. }
  140. static ssize_t proc_sys_read(struct file *filp, char __user *buf,
  141. size_t count, loff_t *ppos)
  142. {
  143. struct dentry *dentry = filp->f_dentry;
  144. struct ctl_table_header *head;
  145. struct ctl_table *table;
  146. ssize_t error, res;
  147. table = do_proc_sys_lookup(dentry->d_parent, &dentry->d_name, &head);
  148. /* Has the sysctl entry disappeared on us? */
  149. error = -ENOENT;
  150. if (!table)
  151. goto out;
  152. /* Has the sysctl entry been replaced by a directory? */
  153. error = -EISDIR;
  154. if (!table->proc_handler)
  155. goto out;
  156. /*
  157. * At this point we know that the sysctl was not unregistered
  158. * and won't be until we finish.
  159. */
  160. error = -EPERM;
  161. if (sysctl_perm(table, MAY_READ))
  162. goto out;
  163. /* careful: calling conventions are nasty here */
  164. res = count;
  165. error = table->proc_handler(table, 0, filp, buf, &res, ppos);
  166. if (!error)
  167. error = res;
  168. out:
  169. sysctl_head_finish(head);
  170. return error;
  171. }
  172. static ssize_t proc_sys_write(struct file *filp, const char __user *buf,
  173. size_t count, loff_t *ppos)
  174. {
  175. struct dentry *dentry = filp->f_dentry;
  176. struct ctl_table_header *head;
  177. struct ctl_table *table;
  178. ssize_t error, res;
  179. table = do_proc_sys_lookup(dentry->d_parent, &dentry->d_name, &head);
  180. /* Has the sysctl entry disappeared on us? */
  181. error = -ENOENT;
  182. if (!table)
  183. goto out;
  184. /* Has the sysctl entry been replaced by a directory? */
  185. error = -EISDIR;
  186. if (!table->proc_handler)
  187. goto out;
  188. /*
  189. * At this point we know that the sysctl was not unregistered
  190. * and won't be until we finish.
  191. */
  192. error = -EPERM;
  193. if (sysctl_perm(table, MAY_WRITE))
  194. goto out;
  195. /* careful: calling conventions are nasty here */
  196. res = count;
  197. error = table->proc_handler(table, 1, filp, (char __user *)buf,
  198. &res, ppos);
  199. if (!error)
  200. error = res;
  201. out:
  202. sysctl_head_finish(head);
  203. return error;
  204. }
  205. static int proc_sys_fill_cache(struct file *filp, void *dirent,
  206. filldir_t filldir, struct ctl_table *table)
  207. {
  208. struct ctl_table_header *head;
  209. struct ctl_table *child_table = NULL;
  210. struct dentry *child, *dir = filp->f_path.dentry;
  211. struct inode *inode;
  212. struct qstr qname;
  213. ino_t ino = 0;
  214. unsigned type = DT_UNKNOWN;
  215. int ret;
  216. qname.name = table->procname;
  217. qname.len = strlen(table->procname);
  218. qname.hash = full_name_hash(qname.name, qname.len);
  219. /* Suppress duplicates.
  220. * Only fill a directory entry if it is the value that
  221. * an ordinary lookup of that name returns. Hide all
  222. * others.
  223. *
  224. * If we ever cache this translation in the dcache
  225. * I should do a dcache lookup first. But for now
  226. * it is just simpler not to.
  227. */
  228. ret = 0;
  229. child_table = do_proc_sys_lookup(dir, &qname, &head);
  230. sysctl_head_finish(head);
  231. if (child_table != table)
  232. return 0;
  233. child = d_lookup(dir, &qname);
  234. if (!child) {
  235. struct dentry *new;
  236. new = d_alloc(dir, &qname);
  237. if (new) {
  238. inode = proc_sys_make_inode(dir->d_inode, table);
  239. if (!inode)
  240. child = ERR_PTR(-ENOMEM);
  241. else {
  242. new->d_op = &proc_sys_dentry_operations;
  243. d_add(new, inode);
  244. }
  245. if (child)
  246. dput(new);
  247. else
  248. child = new;
  249. }
  250. }
  251. if (!child || IS_ERR(child) || !child->d_inode)
  252. goto end_instantiate;
  253. inode = child->d_inode;
  254. if (inode) {
  255. ino = inode->i_ino;
  256. type = inode->i_mode >> 12;
  257. }
  258. dput(child);
  259. end_instantiate:
  260. if (!ino)
  261. ino= find_inode_number(dir, &qname);
  262. if (!ino)
  263. ino = 1;
  264. return filldir(dirent, qname.name, qname.len, filp->f_pos, ino, type);
  265. }
  266. static int proc_sys_readdir(struct file *filp, void *dirent, filldir_t filldir)
  267. {
  268. struct dentry *dentry = filp->f_dentry;
  269. struct inode *inode = dentry->d_inode;
  270. struct ctl_table_header *head = NULL;
  271. struct ctl_table *table;
  272. unsigned long pos;
  273. int ret;
  274. ret = -ENOTDIR;
  275. if (!S_ISDIR(inode->i_mode))
  276. goto out;
  277. ret = 0;
  278. /* Avoid a switch here: arm builds fail with missing __cmpdi2 */
  279. if (filp->f_pos == 0) {
  280. if (filldir(dirent, ".", 1, filp->f_pos,
  281. inode->i_ino, DT_DIR) < 0)
  282. goto out;
  283. filp->f_pos++;
  284. }
  285. if (filp->f_pos == 1) {
  286. if (filldir(dirent, "..", 2, filp->f_pos,
  287. parent_ino(dentry), DT_DIR) < 0)
  288. goto out;
  289. filp->f_pos++;
  290. }
  291. pos = 2;
  292. /* - Find each instance of the directory
  293. * - Read all entries in each instance
  294. * - Before returning an entry to user space lookup the entry
  295. * by name and if I find a different entry don't return
  296. * this one because it means it is a buried dup.
  297. * For sysctl this should only happen for directory entries.
  298. */
  299. for (head = sysctl_head_next(NULL); head; head = sysctl_head_next(head)) {
  300. table = proc_sys_lookup_table(dentry, head->ctl_table);
  301. if (!table)
  302. continue;
  303. for (; table->ctl_name || table->procname; table++, pos++) {
  304. /* Can't do anything without a proc name */
  305. if (!table->procname)
  306. continue;
  307. if (pos < filp->f_pos)
  308. continue;
  309. if (proc_sys_fill_cache(filp, dirent, filldir, table) < 0)
  310. goto out;
  311. filp->f_pos = pos + 1;
  312. }
  313. }
  314. ret = 1;
  315. out:
  316. sysctl_head_finish(head);
  317. return ret;
  318. }
  319. static int proc_sys_permission(struct inode *inode, int mask, struct nameidata *nd)
  320. {
  321. /*
  322. * sysctl entries that are not writeable,
  323. * are _NOT_ writeable, capabilities or not.
  324. */
  325. struct ctl_table_header *head;
  326. struct ctl_table *table;
  327. struct dentry *dentry;
  328. int mode;
  329. int depth;
  330. int error;
  331. head = NULL;
  332. depth = PROC_I(inode)->fd;
  333. /* First check the cached permissions, in case we don't have
  334. * enough information to lookup the sysctl table entry.
  335. */
  336. error = -EACCES;
  337. mode = inode->i_mode;
  338. if (current->euid == 0)
  339. mode >>= 6;
  340. else if (in_group_p(0))
  341. mode >>= 3;
  342. if ((mode & mask & (MAY_READ|MAY_WRITE|MAY_EXEC)) == mask)
  343. error = 0;
  344. /* If we can't get a sysctl table entry the permission
  345. * checks on the cached mode will have to be enough.
  346. */
  347. if (!nd || !depth)
  348. goto out;
  349. dentry = nd->dentry;
  350. table = do_proc_sys_lookup(dentry->d_parent, &dentry->d_name, &head);
  351. /* If the entry does not exist deny permission */
  352. error = -EACCES;
  353. if (!table)
  354. goto out;
  355. /* Use the permissions on the sysctl table entry */
  356. error = sysctl_perm(table, mask);
  357. out:
  358. sysctl_head_finish(head);
  359. return error;
  360. }
  361. static int proc_sys_setattr(struct dentry *dentry, struct iattr *attr)
  362. {
  363. struct inode *inode = dentry->d_inode;
  364. int error;
  365. if (attr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
  366. return -EPERM;
  367. error = inode_change_ok(inode, attr);
  368. if (!error) {
  369. error = security_inode_setattr(dentry, attr);
  370. if (!error)
  371. error = inode_setattr(inode, attr);
  372. }
  373. return error;
  374. }
  375. /* I'm lazy and don't distinguish between files and directories,
  376. * until access time.
  377. */
  378. static const struct file_operations proc_sys_file_operations = {
  379. .read = proc_sys_read,
  380. .write = proc_sys_write,
  381. .readdir = proc_sys_readdir,
  382. };
  383. static struct inode_operations proc_sys_inode_operations = {
  384. .lookup = proc_sys_lookup,
  385. .permission = proc_sys_permission,
  386. .setattr = proc_sys_setattr,
  387. };
  388. static int proc_sys_revalidate(struct dentry *dentry, struct nameidata *nd)
  389. {
  390. struct ctl_table_header *head;
  391. struct ctl_table *table;
  392. table = do_proc_sys_lookup(dentry->d_parent, &dentry->d_name, &head);
  393. proc_sys_refresh_inode(dentry->d_inode, table);
  394. sysctl_head_finish(head);
  395. return !!table;
  396. }
  397. static struct dentry_operations proc_sys_dentry_operations = {
  398. .d_revalidate = proc_sys_revalidate,
  399. };
  400. static struct proc_dir_entry *proc_sys_root;
  401. int proc_sys_init(void)
  402. {
  403. proc_sys_root = proc_mkdir("sys", NULL);
  404. proc_sys_root->proc_iops = &proc_sys_inode_operations;
  405. proc_sys_root->proc_fops = &proc_sys_file_operations;
  406. proc_sys_root->nlink = 0;
  407. return 0;
  408. }