proc_sysctl.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319
  1. /*
  2. * /proc/sys support
  3. */
  4. #include <linux/init.h>
  5. #include <linux/sysctl.h>
  6. #include <linux/poll.h>
  7. #include <linux/proc_fs.h>
  8. #include <linux/security.h>
  9. #include <linux/namei.h>
  10. #include <linux/module.h>
  11. #include "internal.h"
  12. static const struct dentry_operations proc_sys_dentry_operations;
  13. static const struct file_operations proc_sys_file_operations;
  14. static const struct inode_operations proc_sys_inode_operations;
  15. static const struct file_operations proc_sys_dir_file_operations;
  16. static const struct inode_operations proc_sys_dir_operations;
  17. void proc_sys_poll_notify(struct ctl_table_poll *poll)
  18. {
  19. if (!poll)
  20. return;
  21. atomic_inc(&poll->event);
  22. wake_up_interruptible(&poll->wait);
  23. }
  24. static struct ctl_table root_table[] = {
  25. {
  26. .procname = "",
  27. .mode = S_IFDIR|S_IRUGO|S_IXUGO,
  28. },
  29. { }
  30. };
  31. static struct ctl_table_root sysctl_table_root;
  32. static struct ctl_dir sysctl_root_dir = {
  33. .header = {
  34. {{.count = 1,
  35. .nreg = 1,
  36. .ctl_table = root_table,
  37. .ctl_entry = LIST_HEAD_INIT(sysctl_table_root.default_set.list),}},
  38. .root = &sysctl_table_root,
  39. .set = &sysctl_table_root.default_set,
  40. },
  41. };
  42. static struct ctl_table_root sysctl_table_root = {
  43. .root_list = LIST_HEAD_INIT(sysctl_table_root.root_list),
  44. .default_set.list = LIST_HEAD_INIT(sysctl_root_dir.header.ctl_entry),
  45. .default_set.root = &sysctl_table_root,
  46. };
  47. static DEFINE_SPINLOCK(sysctl_lock);
  48. static void drop_sysctl_table(struct ctl_table_header *header);
  49. static int namecmp(const char *name1, int len1, const char *name2, int len2)
  50. {
  51. int minlen;
  52. int cmp;
  53. minlen = len1;
  54. if (minlen > len2)
  55. minlen = len2;
  56. cmp = memcmp(name1, name2, minlen);
  57. if (cmp == 0)
  58. cmp = len1 - len2;
  59. return cmp;
  60. }
  61. static struct ctl_table *find_entry(struct ctl_table_header **phead,
  62. struct ctl_table_set *set, struct ctl_dir *dir,
  63. const char *name, int namelen)
  64. {
  65. struct ctl_table_header *head;
  66. struct ctl_table *entry;
  67. list_for_each_entry(head, &set->list, ctl_entry) {
  68. if (head->unregistering)
  69. continue;
  70. if (head->parent != dir)
  71. continue;
  72. for (entry = head->ctl_table; entry->procname; entry++) {
  73. const char *procname = entry->procname;
  74. if (namecmp(procname, strlen(procname), name, namelen) == 0) {
  75. *phead = head;
  76. return entry;
  77. }
  78. }
  79. }
  80. return NULL;
  81. }
  82. static void init_header(struct ctl_table_header *head,
  83. struct ctl_table_root *root, struct ctl_table_set *set,
  84. struct ctl_table *table)
  85. {
  86. head->ctl_table = table;
  87. head->ctl_table_arg = table;
  88. INIT_LIST_HEAD(&head->ctl_entry);
  89. head->used = 0;
  90. head->count = 1;
  91. head->nreg = 1;
  92. head->unregistering = NULL;
  93. head->root = root;
  94. head->set = set;
  95. head->parent = NULL;
  96. }
  97. static void erase_header(struct ctl_table_header *head)
  98. {
  99. list_del_init(&head->ctl_entry);
  100. }
  101. static void insert_header(struct ctl_dir *dir, struct ctl_table_header *header)
  102. {
  103. header->parent = dir;
  104. header->parent->header.nreg++;
  105. list_add_tail(&header->ctl_entry, &header->set->list);
  106. }
  107. /* called under sysctl_lock */
  108. static int use_table(struct ctl_table_header *p)
  109. {
  110. if (unlikely(p->unregistering))
  111. return 0;
  112. p->used++;
  113. return 1;
  114. }
  115. /* called under sysctl_lock */
  116. static void unuse_table(struct ctl_table_header *p)
  117. {
  118. if (!--p->used)
  119. if (unlikely(p->unregistering))
  120. complete(p->unregistering);
  121. }
  122. /* called under sysctl_lock, will reacquire if has to wait */
  123. static void start_unregistering(struct ctl_table_header *p)
  124. {
  125. /*
  126. * if p->used is 0, nobody will ever touch that entry again;
  127. * we'll eliminate all paths to it before dropping sysctl_lock
  128. */
  129. if (unlikely(p->used)) {
  130. struct completion wait;
  131. init_completion(&wait);
  132. p->unregistering = &wait;
  133. spin_unlock(&sysctl_lock);
  134. wait_for_completion(&wait);
  135. spin_lock(&sysctl_lock);
  136. } else {
  137. /* anything non-NULL; we'll never dereference it */
  138. p->unregistering = ERR_PTR(-EINVAL);
  139. }
  140. /*
  141. * do not remove from the list until nobody holds it; walking the
  142. * list in do_sysctl() relies on that.
  143. */
  144. erase_header(p);
  145. }
  146. static void sysctl_head_get(struct ctl_table_header *head)
  147. {
  148. spin_lock(&sysctl_lock);
  149. head->count++;
  150. spin_unlock(&sysctl_lock);
  151. }
  152. void sysctl_head_put(struct ctl_table_header *head)
  153. {
  154. spin_lock(&sysctl_lock);
  155. if (!--head->count)
  156. kfree_rcu(head, rcu);
  157. spin_unlock(&sysctl_lock);
  158. }
  159. static struct ctl_table_header *sysctl_head_grab(struct ctl_table_header *head)
  160. {
  161. if (!head)
  162. BUG();
  163. spin_lock(&sysctl_lock);
  164. if (!use_table(head))
  165. head = ERR_PTR(-ENOENT);
  166. spin_unlock(&sysctl_lock);
  167. return head;
  168. }
  169. static void sysctl_head_finish(struct ctl_table_header *head)
  170. {
  171. if (!head)
  172. return;
  173. spin_lock(&sysctl_lock);
  174. unuse_table(head);
  175. spin_unlock(&sysctl_lock);
  176. }
  177. static struct ctl_table_set *
  178. lookup_header_set(struct ctl_table_root *root, struct nsproxy *namespaces)
  179. {
  180. struct ctl_table_set *set = &root->default_set;
  181. if (root->lookup)
  182. set = root->lookup(root, namespaces);
  183. return set;
  184. }
  185. static struct list_head *
  186. lookup_header_list(struct ctl_table_root *root, struct nsproxy *namespaces)
  187. {
  188. struct ctl_table_set *set = lookup_header_set(root, namespaces);
  189. return &set->list;
  190. }
  191. static struct ctl_table *lookup_entry(struct ctl_table_header **phead,
  192. struct ctl_dir *dir,
  193. const char *name, int namelen)
  194. {
  195. struct ctl_table_header *head;
  196. struct ctl_table *entry;
  197. struct ctl_table_root *root;
  198. struct ctl_table_set *set;
  199. spin_lock(&sysctl_lock);
  200. root = &sysctl_table_root;
  201. do {
  202. set = lookup_header_set(root, current->nsproxy);
  203. entry = find_entry(&head, set, dir, name, namelen);
  204. if (entry && use_table(head))
  205. *phead = head;
  206. else
  207. entry = NULL;
  208. root = list_entry(root->root_list.next,
  209. struct ctl_table_root, root_list);
  210. } while (!entry && root != &sysctl_table_root);
  211. spin_unlock(&sysctl_lock);
  212. return entry;
  213. }
  214. static struct ctl_table_header *next_usable_entry(struct ctl_dir *dir,
  215. struct ctl_table_root *root, struct list_head *tmp)
  216. {
  217. struct nsproxy *namespaces = current->nsproxy;
  218. struct list_head *header_list;
  219. struct ctl_table_header *head;
  220. goto next;
  221. for (;;) {
  222. head = list_entry(tmp, struct ctl_table_header, ctl_entry);
  223. root = head->root;
  224. if (head->parent != dir ||
  225. !head->ctl_table->procname ||
  226. !use_table(head))
  227. goto next;
  228. return head;
  229. next:
  230. tmp = tmp->next;
  231. header_list = lookup_header_list(root, namespaces);
  232. if (tmp != header_list)
  233. continue;
  234. do {
  235. root = list_entry(root->root_list.next,
  236. struct ctl_table_root, root_list);
  237. if (root == &sysctl_table_root)
  238. goto out;
  239. header_list = lookup_header_list(root, namespaces);
  240. } while (list_empty(header_list));
  241. tmp = header_list->next;
  242. }
  243. out:
  244. return NULL;
  245. }
  246. static void first_entry(struct ctl_dir *dir,
  247. struct ctl_table_header **phead, struct ctl_table **pentry)
  248. {
  249. struct ctl_table_header *head;
  250. struct ctl_table *entry = NULL;
  251. spin_lock(&sysctl_lock);
  252. head = next_usable_entry(dir, &sysctl_table_root,
  253. &sysctl_table_root.default_set.list);
  254. spin_unlock(&sysctl_lock);
  255. if (head)
  256. entry = head->ctl_table;
  257. *phead = head;
  258. *pentry = entry;
  259. }
  260. static void next_entry(struct ctl_table_header **phead, struct ctl_table **pentry)
  261. {
  262. struct ctl_table_header *head = *phead;
  263. struct ctl_table *entry = *pentry;
  264. entry++;
  265. if (!entry->procname) {
  266. spin_lock(&sysctl_lock);
  267. unuse_table(head);
  268. head = next_usable_entry(head->parent, head->root, &head->ctl_entry);
  269. spin_unlock(&sysctl_lock);
  270. if (head)
  271. entry = head->ctl_table;
  272. }
  273. *phead = head;
  274. *pentry = entry;
  275. }
  276. void register_sysctl_root(struct ctl_table_root *root)
  277. {
  278. spin_lock(&sysctl_lock);
  279. list_add_tail(&root->root_list, &sysctl_table_root.root_list);
  280. spin_unlock(&sysctl_lock);
  281. }
  282. /*
  283. * sysctl_perm does NOT grant the superuser all rights automatically, because
  284. * some sysctl variables are readonly even to root.
  285. */
  286. static int test_perm(int mode, int op)
  287. {
  288. if (!current_euid())
  289. mode >>= 6;
  290. else if (in_egroup_p(0))
  291. mode >>= 3;
  292. if ((op & ~mode & (MAY_READ|MAY_WRITE|MAY_EXEC)) == 0)
  293. return 0;
  294. return -EACCES;
  295. }
  296. static int sysctl_perm(struct ctl_table_root *root, struct ctl_table *table, int op)
  297. {
  298. int mode;
  299. if (root->permissions)
  300. mode = root->permissions(root, current->nsproxy, table);
  301. else
  302. mode = table->mode;
  303. return test_perm(mode, op);
  304. }
  305. static struct inode *proc_sys_make_inode(struct super_block *sb,
  306. struct ctl_table_header *head, struct ctl_table *table)
  307. {
  308. struct inode *inode;
  309. struct proc_inode *ei;
  310. inode = new_inode(sb);
  311. if (!inode)
  312. goto out;
  313. inode->i_ino = get_next_ino();
  314. sysctl_head_get(head);
  315. ei = PROC_I(inode);
  316. ei->sysctl = head;
  317. ei->sysctl_entry = table;
  318. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  319. inode->i_mode = table->mode;
  320. if (!S_ISDIR(table->mode)) {
  321. inode->i_mode |= S_IFREG;
  322. inode->i_op = &proc_sys_inode_operations;
  323. inode->i_fop = &proc_sys_file_operations;
  324. } else {
  325. inode->i_mode |= S_IFDIR;
  326. inode->i_op = &proc_sys_dir_operations;
  327. inode->i_fop = &proc_sys_dir_file_operations;
  328. }
  329. out:
  330. return inode;
  331. }
  332. static struct ctl_table_header *grab_header(struct inode *inode)
  333. {
  334. struct ctl_table_header *head = PROC_I(inode)->sysctl;
  335. if (!head)
  336. head = &sysctl_root_dir.header;
  337. return sysctl_head_grab(head);
  338. }
  339. static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry,
  340. struct nameidata *nd)
  341. {
  342. struct ctl_table_header *head = grab_header(dir);
  343. struct ctl_table_header *h = NULL;
  344. struct qstr *name = &dentry->d_name;
  345. struct ctl_table *p;
  346. struct inode *inode;
  347. struct dentry *err = ERR_PTR(-ENOENT);
  348. struct ctl_dir *ctl_dir;
  349. if (IS_ERR(head))
  350. return ERR_CAST(head);
  351. ctl_dir = container_of(head, struct ctl_dir, header);
  352. p = lookup_entry(&h, ctl_dir, name->name, name->len);
  353. if (!p)
  354. goto out;
  355. err = ERR_PTR(-ENOMEM);
  356. inode = proc_sys_make_inode(dir->i_sb, h ? h : head, p);
  357. if (h)
  358. sysctl_head_finish(h);
  359. if (!inode)
  360. goto out;
  361. err = NULL;
  362. d_set_d_op(dentry, &proc_sys_dentry_operations);
  363. d_add(dentry, inode);
  364. out:
  365. sysctl_head_finish(head);
  366. return err;
  367. }
  368. static ssize_t proc_sys_call_handler(struct file *filp, void __user *buf,
  369. size_t count, loff_t *ppos, int write)
  370. {
  371. struct inode *inode = filp->f_path.dentry->d_inode;
  372. struct ctl_table_header *head = grab_header(inode);
  373. struct ctl_table *table = PROC_I(inode)->sysctl_entry;
  374. ssize_t error;
  375. size_t res;
  376. if (IS_ERR(head))
  377. return PTR_ERR(head);
  378. /*
  379. * At this point we know that the sysctl was not unregistered
  380. * and won't be until we finish.
  381. */
  382. error = -EPERM;
  383. if (sysctl_perm(head->root, table, write ? MAY_WRITE : MAY_READ))
  384. goto out;
  385. /* if that can happen at all, it should be -EINVAL, not -EISDIR */
  386. error = -EINVAL;
  387. if (!table->proc_handler)
  388. goto out;
  389. /* careful: calling conventions are nasty here */
  390. res = count;
  391. error = table->proc_handler(table, write, buf, &res, ppos);
  392. if (!error)
  393. error = res;
  394. out:
  395. sysctl_head_finish(head);
  396. return error;
  397. }
  398. static ssize_t proc_sys_read(struct file *filp, char __user *buf,
  399. size_t count, loff_t *ppos)
  400. {
  401. return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 0);
  402. }
  403. static ssize_t proc_sys_write(struct file *filp, const char __user *buf,
  404. size_t count, loff_t *ppos)
  405. {
  406. return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 1);
  407. }
  408. static int proc_sys_open(struct inode *inode, struct file *filp)
  409. {
  410. struct ctl_table *table = PROC_I(inode)->sysctl_entry;
  411. if (table->poll)
  412. filp->private_data = proc_sys_poll_event(table->poll);
  413. return 0;
  414. }
  415. static unsigned int proc_sys_poll(struct file *filp, poll_table *wait)
  416. {
  417. struct inode *inode = filp->f_path.dentry->d_inode;
  418. struct ctl_table *table = PROC_I(inode)->sysctl_entry;
  419. unsigned long event = (unsigned long)filp->private_data;
  420. unsigned int ret = DEFAULT_POLLMASK;
  421. if (!table->proc_handler)
  422. goto out;
  423. if (!table->poll)
  424. goto out;
  425. poll_wait(filp, &table->poll->wait, wait);
  426. if (event != atomic_read(&table->poll->event)) {
  427. filp->private_data = proc_sys_poll_event(table->poll);
  428. ret = POLLIN | POLLRDNORM | POLLERR | POLLPRI;
  429. }
  430. out:
  431. return ret;
  432. }
  433. static int proc_sys_fill_cache(struct file *filp, void *dirent,
  434. filldir_t filldir,
  435. struct ctl_table_header *head,
  436. struct ctl_table *table)
  437. {
  438. struct dentry *child, *dir = filp->f_path.dentry;
  439. struct inode *inode;
  440. struct qstr qname;
  441. ino_t ino = 0;
  442. unsigned type = DT_UNKNOWN;
  443. qname.name = table->procname;
  444. qname.len = strlen(table->procname);
  445. qname.hash = full_name_hash(qname.name, qname.len);
  446. child = d_lookup(dir, &qname);
  447. if (!child) {
  448. child = d_alloc(dir, &qname);
  449. if (child) {
  450. inode = proc_sys_make_inode(dir->d_sb, head, table);
  451. if (!inode) {
  452. dput(child);
  453. return -ENOMEM;
  454. } else {
  455. d_set_d_op(child, &proc_sys_dentry_operations);
  456. d_add(child, inode);
  457. }
  458. } else {
  459. return -ENOMEM;
  460. }
  461. }
  462. inode = child->d_inode;
  463. ino = inode->i_ino;
  464. type = inode->i_mode >> 12;
  465. dput(child);
  466. return !!filldir(dirent, qname.name, qname.len, filp->f_pos, ino, type);
  467. }
  468. static int scan(struct ctl_table_header *head, ctl_table *table,
  469. unsigned long *pos, struct file *file,
  470. void *dirent, filldir_t filldir)
  471. {
  472. int res;
  473. if ((*pos)++ < file->f_pos)
  474. return 0;
  475. res = proc_sys_fill_cache(file, dirent, filldir, head, table);
  476. if (res == 0)
  477. file->f_pos = *pos;
  478. return res;
  479. }
  480. static int proc_sys_readdir(struct file *filp, void *dirent, filldir_t filldir)
  481. {
  482. struct dentry *dentry = filp->f_path.dentry;
  483. struct inode *inode = dentry->d_inode;
  484. struct ctl_table_header *head = grab_header(inode);
  485. struct ctl_table_header *h = NULL;
  486. struct ctl_table *entry;
  487. struct ctl_dir *ctl_dir;
  488. unsigned long pos;
  489. int ret = -EINVAL;
  490. if (IS_ERR(head))
  491. return PTR_ERR(head);
  492. ctl_dir = container_of(head, struct ctl_dir, header);
  493. ret = 0;
  494. /* Avoid a switch here: arm builds fail with missing __cmpdi2 */
  495. if (filp->f_pos == 0) {
  496. if (filldir(dirent, ".", 1, filp->f_pos,
  497. inode->i_ino, DT_DIR) < 0)
  498. goto out;
  499. filp->f_pos++;
  500. }
  501. if (filp->f_pos == 1) {
  502. if (filldir(dirent, "..", 2, filp->f_pos,
  503. parent_ino(dentry), DT_DIR) < 0)
  504. goto out;
  505. filp->f_pos++;
  506. }
  507. pos = 2;
  508. for (first_entry(ctl_dir, &h, &entry); h; next_entry(&h, &entry)) {
  509. ret = scan(h, entry, &pos, filp, dirent, filldir);
  510. if (ret) {
  511. sysctl_head_finish(h);
  512. break;
  513. }
  514. }
  515. ret = 1;
  516. out:
  517. sysctl_head_finish(head);
  518. return ret;
  519. }
  520. static int proc_sys_permission(struct inode *inode, int mask)
  521. {
  522. /*
  523. * sysctl entries that are not writeable,
  524. * are _NOT_ writeable, capabilities or not.
  525. */
  526. struct ctl_table_header *head;
  527. struct ctl_table *table;
  528. int error;
  529. /* Executable files are not allowed under /proc/sys/ */
  530. if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))
  531. return -EACCES;
  532. head = grab_header(inode);
  533. if (IS_ERR(head))
  534. return PTR_ERR(head);
  535. table = PROC_I(inode)->sysctl_entry;
  536. if (!table) /* global root - r-xr-xr-x */
  537. error = mask & MAY_WRITE ? -EACCES : 0;
  538. else /* Use the permissions on the sysctl table entry */
  539. error = sysctl_perm(head->root, table, mask & ~MAY_NOT_BLOCK);
  540. sysctl_head_finish(head);
  541. return error;
  542. }
  543. static int proc_sys_setattr(struct dentry *dentry, struct iattr *attr)
  544. {
  545. struct inode *inode = dentry->d_inode;
  546. int error;
  547. if (attr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
  548. return -EPERM;
  549. error = inode_change_ok(inode, attr);
  550. if (error)
  551. return error;
  552. if ((attr->ia_valid & ATTR_SIZE) &&
  553. attr->ia_size != i_size_read(inode)) {
  554. error = vmtruncate(inode, attr->ia_size);
  555. if (error)
  556. return error;
  557. }
  558. setattr_copy(inode, attr);
  559. mark_inode_dirty(inode);
  560. return 0;
  561. }
  562. static int proc_sys_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
  563. {
  564. struct inode *inode = dentry->d_inode;
  565. struct ctl_table_header *head = grab_header(inode);
  566. struct ctl_table *table = PROC_I(inode)->sysctl_entry;
  567. if (IS_ERR(head))
  568. return PTR_ERR(head);
  569. generic_fillattr(inode, stat);
  570. if (table)
  571. stat->mode = (stat->mode & S_IFMT) | table->mode;
  572. sysctl_head_finish(head);
  573. return 0;
  574. }
  575. static const struct file_operations proc_sys_file_operations = {
  576. .open = proc_sys_open,
  577. .poll = proc_sys_poll,
  578. .read = proc_sys_read,
  579. .write = proc_sys_write,
  580. .llseek = default_llseek,
  581. };
  582. static const struct file_operations proc_sys_dir_file_operations = {
  583. .read = generic_read_dir,
  584. .readdir = proc_sys_readdir,
  585. .llseek = generic_file_llseek,
  586. };
  587. static const struct inode_operations proc_sys_inode_operations = {
  588. .permission = proc_sys_permission,
  589. .setattr = proc_sys_setattr,
  590. .getattr = proc_sys_getattr,
  591. };
  592. static const struct inode_operations proc_sys_dir_operations = {
  593. .lookup = proc_sys_lookup,
  594. .permission = proc_sys_permission,
  595. .setattr = proc_sys_setattr,
  596. .getattr = proc_sys_getattr,
  597. };
  598. static int proc_sys_revalidate(struct dentry *dentry, struct nameidata *nd)
  599. {
  600. if (nd->flags & LOOKUP_RCU)
  601. return -ECHILD;
  602. return !PROC_I(dentry->d_inode)->sysctl->unregistering;
  603. }
  604. static int proc_sys_delete(const struct dentry *dentry)
  605. {
  606. return !!PROC_I(dentry->d_inode)->sysctl->unregistering;
  607. }
  608. static int sysctl_is_seen(struct ctl_table_header *p)
  609. {
  610. struct ctl_table_set *set = p->set;
  611. int res;
  612. spin_lock(&sysctl_lock);
  613. if (p->unregistering)
  614. res = 0;
  615. else if (!set->is_seen)
  616. res = 1;
  617. else
  618. res = set->is_seen(set);
  619. spin_unlock(&sysctl_lock);
  620. return res;
  621. }
  622. static int proc_sys_compare(const struct dentry *parent,
  623. const struct inode *pinode,
  624. const struct dentry *dentry, const struct inode *inode,
  625. unsigned int len, const char *str, const struct qstr *name)
  626. {
  627. struct ctl_table_header *head;
  628. /* Although proc doesn't have negative dentries, rcu-walk means
  629. * that inode here can be NULL */
  630. /* AV: can it, indeed? */
  631. if (!inode)
  632. return 1;
  633. if (name->len != len)
  634. return 1;
  635. if (memcmp(name->name, str, len))
  636. return 1;
  637. head = rcu_dereference(PROC_I(inode)->sysctl);
  638. return !head || !sysctl_is_seen(head);
  639. }
  640. static const struct dentry_operations proc_sys_dentry_operations = {
  641. .d_revalidate = proc_sys_revalidate,
  642. .d_delete = proc_sys_delete,
  643. .d_compare = proc_sys_compare,
  644. };
  645. static struct ctl_dir *find_subdir(struct ctl_table_set *set, struct ctl_dir *dir,
  646. const char *name, int namelen)
  647. {
  648. struct ctl_table_header *head;
  649. struct ctl_table *entry;
  650. entry = find_entry(&head, set, dir, name, namelen);
  651. if (!entry)
  652. return ERR_PTR(-ENOENT);
  653. if (S_ISDIR(entry->mode))
  654. return container_of(head, struct ctl_dir, header);
  655. return ERR_PTR(-ENOTDIR);
  656. }
  657. static struct ctl_dir *new_dir(struct ctl_table_set *set,
  658. const char *name, int namelen)
  659. {
  660. struct ctl_table *table;
  661. struct ctl_dir *new;
  662. char *new_name;
  663. new = kzalloc(sizeof(*new) + sizeof(struct ctl_table)*2 +
  664. namelen + 1, GFP_KERNEL);
  665. if (!new)
  666. return NULL;
  667. table = (struct ctl_table *)(new + 1);
  668. new_name = (char *)(table + 2);
  669. memcpy(new_name, name, namelen);
  670. new_name[namelen] = '\0';
  671. table[0].procname = new_name;
  672. table[0].mode = S_IFDIR|S_IRUGO|S_IXUGO;
  673. init_header(&new->header, set->root, set, table);
  674. return new;
  675. }
  676. static struct ctl_dir *get_subdir(struct ctl_table_set *set,
  677. struct ctl_dir *dir, const char *name, int namelen)
  678. {
  679. struct ctl_dir *subdir, *new = NULL;
  680. spin_lock(&sysctl_lock);
  681. subdir = find_subdir(dir->header.set, dir, name, namelen);
  682. if (!IS_ERR(subdir))
  683. goto found;
  684. if ((PTR_ERR(subdir) == -ENOENT) && set != dir->header.set)
  685. subdir = find_subdir(set, dir, name, namelen);
  686. if (!IS_ERR(subdir))
  687. goto found;
  688. if (PTR_ERR(subdir) != -ENOENT)
  689. goto failed;
  690. spin_unlock(&sysctl_lock);
  691. new = new_dir(set, name, namelen);
  692. spin_lock(&sysctl_lock);
  693. subdir = ERR_PTR(-ENOMEM);
  694. if (!new)
  695. goto failed;
  696. subdir = find_subdir(set, dir, name, namelen);
  697. if (!IS_ERR(subdir))
  698. goto found;
  699. if (PTR_ERR(subdir) != -ENOENT)
  700. goto failed;
  701. insert_header(dir, &new->header);
  702. subdir = new;
  703. found:
  704. subdir->header.nreg++;
  705. failed:
  706. if (unlikely(IS_ERR(subdir))) {
  707. printk(KERN_ERR "sysctl could not get directory: %*.*s %ld\n",
  708. namelen, namelen, name, PTR_ERR(subdir));
  709. }
  710. drop_sysctl_table(&dir->header);
  711. if (new)
  712. drop_sysctl_table(&new->header);
  713. spin_unlock(&sysctl_lock);
  714. return subdir;
  715. }
  716. static int sysctl_check_table_dups(const char *path, struct ctl_table *old,
  717. struct ctl_table *table)
  718. {
  719. struct ctl_table *entry, *test;
  720. int error = 0;
  721. for (entry = old; entry->procname; entry++) {
  722. for (test = table; test->procname; test++) {
  723. if (strcmp(entry->procname, test->procname) == 0) {
  724. printk(KERN_ERR "sysctl duplicate entry: %s/%s\n",
  725. path, test->procname);
  726. error = -EEXIST;
  727. }
  728. }
  729. }
  730. return error;
  731. }
  732. static int sysctl_check_dups(struct nsproxy *namespaces,
  733. struct ctl_dir *dir,
  734. const char *path, struct ctl_table *table)
  735. {
  736. struct ctl_table_root *root;
  737. struct ctl_table_set *set;
  738. struct ctl_table_header *head;
  739. int error = 0;
  740. root = &sysctl_table_root;
  741. do {
  742. set = lookup_header_set(root, namespaces);
  743. list_for_each_entry(head, &set->list, ctl_entry) {
  744. if (head->unregistering)
  745. continue;
  746. if (head->parent != dir)
  747. continue;
  748. error = sysctl_check_table_dups(path, head->ctl_table,
  749. table);
  750. }
  751. root = list_entry(root->root_list.next,
  752. struct ctl_table_root, root_list);
  753. } while (root != &sysctl_table_root);
  754. return error;
  755. }
  756. static int sysctl_err(const char *path, struct ctl_table *table, char *fmt, ...)
  757. {
  758. struct va_format vaf;
  759. va_list args;
  760. va_start(args, fmt);
  761. vaf.fmt = fmt;
  762. vaf.va = &args;
  763. printk(KERN_ERR "sysctl table check failed: %s/%s %pV\n",
  764. path, table->procname, &vaf);
  765. va_end(args);
  766. return -EINVAL;
  767. }
  768. static int sysctl_check_table(const char *path, struct ctl_table *table)
  769. {
  770. int err = 0;
  771. for (; table->procname; table++) {
  772. if (table->child)
  773. err = sysctl_err(path, table, "Not a file");
  774. if ((table->proc_handler == proc_dostring) ||
  775. (table->proc_handler == proc_dointvec) ||
  776. (table->proc_handler == proc_dointvec_minmax) ||
  777. (table->proc_handler == proc_dointvec_jiffies) ||
  778. (table->proc_handler == proc_dointvec_userhz_jiffies) ||
  779. (table->proc_handler == proc_dointvec_ms_jiffies) ||
  780. (table->proc_handler == proc_doulongvec_minmax) ||
  781. (table->proc_handler == proc_doulongvec_ms_jiffies_minmax)) {
  782. if (!table->data)
  783. err = sysctl_err(path, table, "No data");
  784. if (!table->maxlen)
  785. err = sysctl_err(path, table, "No maxlen");
  786. }
  787. if (!table->proc_handler)
  788. err = sysctl_err(path, table, "No proc_handler");
  789. if ((table->mode & (S_IRUGO|S_IWUGO)) != table->mode)
  790. err = sysctl_err(path, table, "bogus .mode 0%o",
  791. table->mode);
  792. }
  793. return err;
  794. }
  795. /**
  796. * __register_sysctl_table - register a leaf sysctl table
  797. * @root: List of sysctl headers to register on
  798. * @namespaces: Data to compute which lists of sysctl entries are visible
  799. * @path: The path to the directory the sysctl table is in.
  800. * @table: the top-level table structure
  801. *
  802. * Register a sysctl table hierarchy. @table should be a filled in ctl_table
  803. * array. A completely 0 filled entry terminates the table.
  804. *
  805. * The members of the &struct ctl_table structure are used as follows:
  806. *
  807. * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not
  808. * enter a sysctl file
  809. *
  810. * data - a pointer to data for use by proc_handler
  811. *
  812. * maxlen - the maximum size in bytes of the data
  813. *
  814. * mode - the file permissions for the /proc/sys file
  815. *
  816. * child - must be %NULL.
  817. *
  818. * proc_handler - the text handler routine (described below)
  819. *
  820. * extra1, extra2 - extra pointers usable by the proc handler routines
  821. *
  822. * Leaf nodes in the sysctl tree will be represented by a single file
  823. * under /proc; non-leaf nodes will be represented by directories.
  824. *
  825. * There must be a proc_handler routine for any terminal nodes.
  826. * Several default handlers are available to cover common cases -
  827. *
  828. * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(),
  829. * proc_dointvec_userhz_jiffies(), proc_dointvec_minmax(),
  830. * proc_doulongvec_ms_jiffies_minmax(), proc_doulongvec_minmax()
  831. *
  832. * It is the handler's job to read the input buffer from user memory
  833. * and process it. The handler should return 0 on success.
  834. *
  835. * This routine returns %NULL on a failure to register, and a pointer
  836. * to the table header on success.
  837. */
  838. struct ctl_table_header *__register_sysctl_table(
  839. struct ctl_table_root *root,
  840. struct nsproxy *namespaces,
  841. const char *path, struct ctl_table *table)
  842. {
  843. struct ctl_table_header *header;
  844. const char *name, *nextname;
  845. struct ctl_table_set *set;
  846. struct ctl_dir *dir;
  847. header = kzalloc(sizeof(struct ctl_table_header), GFP_KERNEL);
  848. if (!header)
  849. return NULL;
  850. init_header(header, root, NULL, table);
  851. if (sysctl_check_table(path, table))
  852. goto fail;
  853. spin_lock(&sysctl_lock);
  854. header->set = set = lookup_header_set(root, namespaces);
  855. dir = &sysctl_root_dir;
  856. dir->header.nreg++;
  857. spin_unlock(&sysctl_lock);
  858. /* Find the directory for the ctl_table */
  859. for (name = path; name; name = nextname) {
  860. int namelen;
  861. nextname = strchr(name, '/');
  862. if (nextname) {
  863. namelen = nextname - name;
  864. nextname++;
  865. } else {
  866. namelen = strlen(name);
  867. }
  868. if (namelen == 0)
  869. continue;
  870. dir = get_subdir(set, dir, name, namelen);
  871. if (IS_ERR(dir))
  872. goto fail;
  873. }
  874. spin_lock(&sysctl_lock);
  875. if (sysctl_check_dups(namespaces, dir, path, table))
  876. goto fail_put_dir_locked;
  877. insert_header(dir, header);
  878. drop_sysctl_table(&dir->header);
  879. spin_unlock(&sysctl_lock);
  880. return header;
  881. fail_put_dir_locked:
  882. drop_sysctl_table(&dir->header);
  883. spin_unlock(&sysctl_lock);
  884. fail:
  885. kfree(header);
  886. dump_stack();
  887. return NULL;
  888. }
  889. static char *append_path(const char *path, char *pos, const char *name)
  890. {
  891. int namelen;
  892. namelen = strlen(name);
  893. if (((pos - path) + namelen + 2) >= PATH_MAX)
  894. return NULL;
  895. memcpy(pos, name, namelen);
  896. pos[namelen] = '/';
  897. pos[namelen + 1] = '\0';
  898. pos += namelen + 1;
  899. return pos;
  900. }
  901. static int count_subheaders(struct ctl_table *table)
  902. {
  903. int has_files = 0;
  904. int nr_subheaders = 0;
  905. struct ctl_table *entry;
  906. /* special case: no directory and empty directory */
  907. if (!table || !table->procname)
  908. return 1;
  909. for (entry = table; entry->procname; entry++) {
  910. if (entry->child)
  911. nr_subheaders += count_subheaders(entry->child);
  912. else
  913. has_files = 1;
  914. }
  915. return nr_subheaders + has_files;
  916. }
  917. static int register_leaf_sysctl_tables(const char *path, char *pos,
  918. struct ctl_table_header ***subheader,
  919. struct ctl_table_root *root, struct nsproxy *namespaces,
  920. struct ctl_table *table)
  921. {
  922. struct ctl_table *ctl_table_arg = NULL;
  923. struct ctl_table *entry, *files;
  924. int nr_files = 0;
  925. int nr_dirs = 0;
  926. int err = -ENOMEM;
  927. for (entry = table; entry->procname; entry++) {
  928. if (entry->child)
  929. nr_dirs++;
  930. else
  931. nr_files++;
  932. }
  933. files = table;
  934. /* If there are mixed files and directories we need a new table */
  935. if (nr_dirs && nr_files) {
  936. struct ctl_table *new;
  937. files = kzalloc(sizeof(struct ctl_table) * (nr_files + 1),
  938. GFP_KERNEL);
  939. if (!files)
  940. goto out;
  941. ctl_table_arg = files;
  942. for (new = files, entry = table; entry->procname; entry++) {
  943. if (entry->child)
  944. continue;
  945. *new = *entry;
  946. new++;
  947. }
  948. }
  949. /* Register everything except a directory full of subdirectories */
  950. if (nr_files || !nr_dirs) {
  951. struct ctl_table_header *header;
  952. header = __register_sysctl_table(root, namespaces, path, files);
  953. if (!header) {
  954. kfree(ctl_table_arg);
  955. goto out;
  956. }
  957. /* Remember if we need to free the file table */
  958. header->ctl_table_arg = ctl_table_arg;
  959. **subheader = header;
  960. (*subheader)++;
  961. }
  962. /* Recurse into the subdirectories. */
  963. for (entry = table; entry->procname; entry++) {
  964. char *child_pos;
  965. if (!entry->child)
  966. continue;
  967. err = -ENAMETOOLONG;
  968. child_pos = append_path(path, pos, entry->procname);
  969. if (!child_pos)
  970. goto out;
  971. err = register_leaf_sysctl_tables(path, child_pos, subheader,
  972. root, namespaces, entry->child);
  973. pos[0] = '\0';
  974. if (err)
  975. goto out;
  976. }
  977. err = 0;
  978. out:
  979. /* On failure our caller will unregister all registered subheaders */
  980. return err;
  981. }
  982. /**
  983. * __register_sysctl_paths - register a sysctl table hierarchy
  984. * @root: List of sysctl headers to register on
  985. * @namespaces: Data to compute which lists of sysctl entries are visible
  986. * @path: The path to the directory the sysctl table is in.
  987. * @table: the top-level table structure
  988. *
  989. * Register a sysctl table hierarchy. @table should be a filled in ctl_table
  990. * array. A completely 0 filled entry terminates the table.
  991. *
  992. * See __register_sysctl_table for more details.
  993. */
  994. struct ctl_table_header *__register_sysctl_paths(
  995. struct ctl_table_root *root,
  996. struct nsproxy *namespaces,
  997. const struct ctl_path *path, struct ctl_table *table)
  998. {
  999. struct ctl_table *ctl_table_arg = table;
  1000. int nr_subheaders = count_subheaders(table);
  1001. struct ctl_table_header *header = NULL, **subheaders, **subheader;
  1002. const struct ctl_path *component;
  1003. char *new_path, *pos;
  1004. pos = new_path = kmalloc(PATH_MAX, GFP_KERNEL);
  1005. if (!new_path)
  1006. return NULL;
  1007. pos[0] = '\0';
  1008. for (component = path; component->procname; component++) {
  1009. pos = append_path(new_path, pos, component->procname);
  1010. if (!pos)
  1011. goto out;
  1012. }
  1013. while (table->procname && table->child && !table[1].procname) {
  1014. pos = append_path(new_path, pos, table->procname);
  1015. if (!pos)
  1016. goto out;
  1017. table = table->child;
  1018. }
  1019. if (nr_subheaders == 1) {
  1020. header = __register_sysctl_table(root, namespaces, new_path, table);
  1021. if (header)
  1022. header->ctl_table_arg = ctl_table_arg;
  1023. } else {
  1024. header = kzalloc(sizeof(*header) +
  1025. sizeof(*subheaders)*nr_subheaders, GFP_KERNEL);
  1026. if (!header)
  1027. goto out;
  1028. subheaders = (struct ctl_table_header **) (header + 1);
  1029. subheader = subheaders;
  1030. header->ctl_table_arg = ctl_table_arg;
  1031. if (register_leaf_sysctl_tables(new_path, pos, &subheader,
  1032. root, namespaces, table))
  1033. goto err_register_leaves;
  1034. }
  1035. out:
  1036. kfree(new_path);
  1037. return header;
  1038. err_register_leaves:
  1039. while (subheader > subheaders) {
  1040. struct ctl_table_header *subh = *(--subheader);
  1041. struct ctl_table *table = subh->ctl_table_arg;
  1042. unregister_sysctl_table(subh);
  1043. kfree(table);
  1044. }
  1045. kfree(header);
  1046. header = NULL;
  1047. goto out;
  1048. }
  1049. /**
  1050. * register_sysctl_table_path - register a sysctl table hierarchy
  1051. * @path: The path to the directory the sysctl table is in.
  1052. * @table: the top-level table structure
  1053. *
  1054. * Register a sysctl table hierarchy. @table should be a filled in ctl_table
  1055. * array. A completely 0 filled entry terminates the table.
  1056. *
  1057. * See __register_sysctl_paths for more details.
  1058. */
  1059. struct ctl_table_header *register_sysctl_paths(const struct ctl_path *path,
  1060. struct ctl_table *table)
  1061. {
  1062. return __register_sysctl_paths(&sysctl_table_root, current->nsproxy,
  1063. path, table);
  1064. }
  1065. EXPORT_SYMBOL(register_sysctl_paths);
  1066. /**
  1067. * register_sysctl_table - register a sysctl table hierarchy
  1068. * @table: the top-level table structure
  1069. *
  1070. * Register a sysctl table hierarchy. @table should be a filled in ctl_table
  1071. * array. A completely 0 filled entry terminates the table.
  1072. *
  1073. * See register_sysctl_paths for more details.
  1074. */
  1075. struct ctl_table_header *register_sysctl_table(struct ctl_table *table)
  1076. {
  1077. static const struct ctl_path null_path[] = { {} };
  1078. return register_sysctl_paths(null_path, table);
  1079. }
  1080. EXPORT_SYMBOL(register_sysctl_table);
  1081. static void drop_sysctl_table(struct ctl_table_header *header)
  1082. {
  1083. struct ctl_dir *parent = header->parent;
  1084. if (--header->nreg)
  1085. return;
  1086. start_unregistering(header);
  1087. if (!--header->count)
  1088. kfree_rcu(header, rcu);
  1089. if (parent)
  1090. drop_sysctl_table(&parent->header);
  1091. }
  1092. /**
  1093. * unregister_sysctl_table - unregister a sysctl table hierarchy
  1094. * @header: the header returned from register_sysctl_table
  1095. *
  1096. * Unregisters the sysctl table and all children. proc entries may not
  1097. * actually be removed until they are no longer used by anyone.
  1098. */
  1099. void unregister_sysctl_table(struct ctl_table_header * header)
  1100. {
  1101. int nr_subheaders;
  1102. might_sleep();
  1103. if (header == NULL)
  1104. return;
  1105. nr_subheaders = count_subheaders(header->ctl_table_arg);
  1106. if (unlikely(nr_subheaders > 1)) {
  1107. struct ctl_table_header **subheaders;
  1108. int i;
  1109. subheaders = (struct ctl_table_header **)(header + 1);
  1110. for (i = nr_subheaders -1; i >= 0; i--) {
  1111. struct ctl_table_header *subh = subheaders[i];
  1112. struct ctl_table *table = subh->ctl_table_arg;
  1113. unregister_sysctl_table(subh);
  1114. kfree(table);
  1115. }
  1116. kfree(header);
  1117. return;
  1118. }
  1119. spin_lock(&sysctl_lock);
  1120. drop_sysctl_table(header);
  1121. spin_unlock(&sysctl_lock);
  1122. }
  1123. EXPORT_SYMBOL(unregister_sysctl_table);
  1124. void setup_sysctl_set(struct ctl_table_set *p,
  1125. struct ctl_table_root *root,
  1126. int (*is_seen)(struct ctl_table_set *))
  1127. {
  1128. INIT_LIST_HEAD(&p->list);
  1129. p->root = root;
  1130. p->is_seen = is_seen;
  1131. }
  1132. void retire_sysctl_set(struct ctl_table_set *set)
  1133. {
  1134. WARN_ON(!list_empty(&set->list));
  1135. }
  1136. int __init proc_sys_init(void)
  1137. {
  1138. struct proc_dir_entry *proc_sys_root;
  1139. proc_sys_root = proc_mkdir("sys", NULL);
  1140. proc_sys_root->proc_iops = &proc_sys_dir_operations;
  1141. proc_sys_root->proc_fops = &proc_sys_dir_file_operations;
  1142. proc_sys_root->nlink = 0;
  1143. return sysctl_init();
  1144. }