proc_sysctl.c 32 KB

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