proc_sysctl.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * /proc/sys support
  4. */
  5. #include <linux/init.h>
  6. #include <linux/sysctl.h>
  7. #include <linux/poll.h>
  8. #include <linux/proc_fs.h>
  9. #include <linux/printk.h>
  10. #include <linux/security.h>
  11. #include <linux/sched.h>
  12. #include <linux/cred.h>
  13. #include <linux/namei.h>
  14. #include <linux/mm.h>
  15. #include <linux/module.h>
  16. #include "internal.h"
  17. static const struct dentry_operations proc_sys_dentry_operations;
  18. static const struct file_operations proc_sys_file_operations;
  19. static const struct inode_operations proc_sys_inode_operations;
  20. static const struct file_operations proc_sys_dir_file_operations;
  21. static const struct inode_operations proc_sys_dir_operations;
  22. /* Support for permanently empty directories */
  23. struct ctl_table sysctl_mount_point[] = {
  24. { }
  25. };
  26. static bool is_empty_dir(struct ctl_table_header *head)
  27. {
  28. return head->ctl_table[0].child == sysctl_mount_point;
  29. }
  30. static void set_empty_dir(struct ctl_dir *dir)
  31. {
  32. dir->header.ctl_table[0].child = sysctl_mount_point;
  33. }
  34. static void clear_empty_dir(struct ctl_dir *dir)
  35. {
  36. dir->header.ctl_table[0].child = NULL;
  37. }
  38. void proc_sys_poll_notify(struct ctl_table_poll *poll)
  39. {
  40. if (!poll)
  41. return;
  42. atomic_inc(&poll->event);
  43. wake_up_interruptible(&poll->wait);
  44. }
  45. static struct ctl_table root_table[] = {
  46. {
  47. .procname = "",
  48. .mode = S_IFDIR|S_IRUGO|S_IXUGO,
  49. },
  50. { }
  51. };
  52. static struct ctl_table_root sysctl_table_root = {
  53. .default_set.dir.header = {
  54. {{.count = 1,
  55. .nreg = 1,
  56. .ctl_table = root_table }},
  57. .ctl_table_arg = root_table,
  58. .root = &sysctl_table_root,
  59. .set = &sysctl_table_root.default_set,
  60. },
  61. };
  62. static DEFINE_SPINLOCK(sysctl_lock);
  63. static void drop_sysctl_table(struct ctl_table_header *header);
  64. static int sysctl_follow_link(struct ctl_table_header **phead,
  65. struct ctl_table **pentry);
  66. static int insert_links(struct ctl_table_header *head);
  67. static void put_links(struct ctl_table_header *header);
  68. static void sysctl_print_dir(struct ctl_dir *dir)
  69. {
  70. if (dir->header.parent)
  71. sysctl_print_dir(dir->header.parent);
  72. pr_cont("%s/", dir->header.ctl_table[0].procname);
  73. }
  74. static int namecmp(const char *name1, int len1, const char *name2, int len2)
  75. {
  76. int minlen;
  77. int cmp;
  78. minlen = len1;
  79. if (minlen > len2)
  80. minlen = len2;
  81. cmp = memcmp(name1, name2, minlen);
  82. if (cmp == 0)
  83. cmp = len1 - len2;
  84. return cmp;
  85. }
  86. /* Called under sysctl_lock */
  87. static struct ctl_table *find_entry(struct ctl_table_header **phead,
  88. struct ctl_dir *dir, const char *name, int namelen)
  89. {
  90. struct ctl_table_header *head;
  91. struct ctl_table *entry;
  92. struct rb_node *node = dir->root.rb_node;
  93. while (node)
  94. {
  95. struct ctl_node *ctl_node;
  96. const char *procname;
  97. int cmp;
  98. ctl_node = rb_entry(node, struct ctl_node, node);
  99. head = ctl_node->header;
  100. entry = &head->ctl_table[ctl_node - head->node];
  101. procname = entry->procname;
  102. cmp = namecmp(name, namelen, procname, strlen(procname));
  103. if (cmp < 0)
  104. node = node->rb_left;
  105. else if (cmp > 0)
  106. node = node->rb_right;
  107. else {
  108. *phead = head;
  109. return entry;
  110. }
  111. }
  112. return NULL;
  113. }
  114. static int insert_entry(struct ctl_table_header *head, struct ctl_table *entry)
  115. {
  116. struct rb_node *node = &head->node[entry - head->ctl_table].node;
  117. struct rb_node **p = &head->parent->root.rb_node;
  118. struct rb_node *parent = NULL;
  119. const char *name = entry->procname;
  120. int namelen = strlen(name);
  121. while (*p) {
  122. struct ctl_table_header *parent_head;
  123. struct ctl_table *parent_entry;
  124. struct ctl_node *parent_node;
  125. const char *parent_name;
  126. int cmp;
  127. parent = *p;
  128. parent_node = rb_entry(parent, struct ctl_node, node);
  129. parent_head = parent_node->header;
  130. parent_entry = &parent_head->ctl_table[parent_node - parent_head->node];
  131. parent_name = parent_entry->procname;
  132. cmp = namecmp(name, namelen, parent_name, strlen(parent_name));
  133. if (cmp < 0)
  134. p = &(*p)->rb_left;
  135. else if (cmp > 0)
  136. p = &(*p)->rb_right;
  137. else {
  138. pr_err("sysctl duplicate entry: ");
  139. sysctl_print_dir(head->parent);
  140. pr_cont("/%s\n", entry->procname);
  141. return -EEXIST;
  142. }
  143. }
  144. rb_link_node(node, parent, p);
  145. rb_insert_color(node, &head->parent->root);
  146. return 0;
  147. }
  148. static void erase_entry(struct ctl_table_header *head, struct ctl_table *entry)
  149. {
  150. struct rb_node *node = &head->node[entry - head->ctl_table].node;
  151. rb_erase(node, &head->parent->root);
  152. }
  153. static void init_header(struct ctl_table_header *head,
  154. struct ctl_table_root *root, struct ctl_table_set *set,
  155. struct ctl_node *node, struct ctl_table *table)
  156. {
  157. head->ctl_table = table;
  158. head->ctl_table_arg = table;
  159. head->used = 0;
  160. head->count = 1;
  161. head->nreg = 1;
  162. head->unregistering = NULL;
  163. head->root = root;
  164. head->set = set;
  165. head->parent = NULL;
  166. head->node = node;
  167. INIT_HLIST_HEAD(&head->inodes);
  168. if (node) {
  169. struct ctl_table *entry;
  170. for (entry = table; entry->procname; entry++, node++)
  171. node->header = head;
  172. }
  173. }
  174. static void erase_header(struct ctl_table_header *head)
  175. {
  176. struct ctl_table *entry;
  177. for (entry = head->ctl_table; entry->procname; entry++)
  178. erase_entry(head, entry);
  179. }
  180. static int insert_header(struct ctl_dir *dir, struct ctl_table_header *header)
  181. {
  182. struct ctl_table *entry;
  183. int err;
  184. /* Is this a permanently empty directory? */
  185. if (is_empty_dir(&dir->header))
  186. return -EROFS;
  187. /* Am I creating a permanently empty directory? */
  188. if (header->ctl_table == sysctl_mount_point) {
  189. if (!RB_EMPTY_ROOT(&dir->root))
  190. return -EINVAL;
  191. set_empty_dir(dir);
  192. }
  193. dir->header.nreg++;
  194. header->parent = dir;
  195. err = insert_links(header);
  196. if (err)
  197. goto fail_links;
  198. for (entry = header->ctl_table; entry->procname; entry++) {
  199. err = insert_entry(header, entry);
  200. if (err)
  201. goto fail;
  202. }
  203. return 0;
  204. fail:
  205. erase_header(header);
  206. put_links(header);
  207. fail_links:
  208. if (header->ctl_table == sysctl_mount_point)
  209. clear_empty_dir(dir);
  210. header->parent = NULL;
  211. drop_sysctl_table(&dir->header);
  212. return err;
  213. }
  214. /* called under sysctl_lock */
  215. static int use_table(struct ctl_table_header *p)
  216. {
  217. if (unlikely(p->unregistering))
  218. return 0;
  219. p->used++;
  220. return 1;
  221. }
  222. /* called under sysctl_lock */
  223. static void unuse_table(struct ctl_table_header *p)
  224. {
  225. if (!--p->used)
  226. if (unlikely(p->unregistering))
  227. complete(p->unregistering);
  228. }
  229. static void proc_sys_prune_dcache(struct ctl_table_header *head)
  230. {
  231. struct inode *inode;
  232. struct proc_inode *ei;
  233. struct hlist_node *node;
  234. struct super_block *sb;
  235. rcu_read_lock();
  236. for (;;) {
  237. node = hlist_first_rcu(&head->inodes);
  238. if (!node)
  239. break;
  240. ei = hlist_entry(node, struct proc_inode, sysctl_inodes);
  241. spin_lock(&sysctl_lock);
  242. hlist_del_init_rcu(&ei->sysctl_inodes);
  243. spin_unlock(&sysctl_lock);
  244. inode = &ei->vfs_inode;
  245. sb = inode->i_sb;
  246. if (!atomic_inc_not_zero(&sb->s_active))
  247. continue;
  248. inode = igrab(inode);
  249. rcu_read_unlock();
  250. if (unlikely(!inode)) {
  251. deactivate_super(sb);
  252. rcu_read_lock();
  253. continue;
  254. }
  255. d_prune_aliases(inode);
  256. iput(inode);
  257. deactivate_super(sb);
  258. rcu_read_lock();
  259. }
  260. rcu_read_unlock();
  261. }
  262. /* called under sysctl_lock, will reacquire if has to wait */
  263. static void start_unregistering(struct ctl_table_header *p)
  264. {
  265. /*
  266. * if p->used is 0, nobody will ever touch that entry again;
  267. * we'll eliminate all paths to it before dropping sysctl_lock
  268. */
  269. if (unlikely(p->used)) {
  270. struct completion wait;
  271. init_completion(&wait);
  272. p->unregistering = &wait;
  273. spin_unlock(&sysctl_lock);
  274. wait_for_completion(&wait);
  275. } else {
  276. /* anything non-NULL; we'll never dereference it */
  277. p->unregistering = ERR_PTR(-EINVAL);
  278. spin_unlock(&sysctl_lock);
  279. }
  280. /*
  281. * Prune dentries for unregistered sysctls: namespaced sysctls
  282. * can have duplicate names and contaminate dcache very badly.
  283. */
  284. proc_sys_prune_dcache(p);
  285. /*
  286. * do not remove from the list until nobody holds it; walking the
  287. * list in do_sysctl() relies on that.
  288. */
  289. spin_lock(&sysctl_lock);
  290. erase_header(p);
  291. }
  292. static struct ctl_table_header *sysctl_head_grab(struct ctl_table_header *head)
  293. {
  294. BUG_ON(!head);
  295. spin_lock(&sysctl_lock);
  296. if (!use_table(head))
  297. head = ERR_PTR(-ENOENT);
  298. spin_unlock(&sysctl_lock);
  299. return head;
  300. }
  301. static void sysctl_head_finish(struct ctl_table_header *head)
  302. {
  303. if (!head)
  304. return;
  305. spin_lock(&sysctl_lock);
  306. unuse_table(head);
  307. spin_unlock(&sysctl_lock);
  308. }
  309. static struct ctl_table_set *
  310. lookup_header_set(struct ctl_table_root *root)
  311. {
  312. struct ctl_table_set *set = &root->default_set;
  313. if (root->lookup)
  314. set = root->lookup(root);
  315. return set;
  316. }
  317. static struct ctl_table *lookup_entry(struct ctl_table_header **phead,
  318. struct ctl_dir *dir,
  319. const char *name, int namelen)
  320. {
  321. struct ctl_table_header *head;
  322. struct ctl_table *entry;
  323. spin_lock(&sysctl_lock);
  324. entry = find_entry(&head, dir, name, namelen);
  325. if (entry && use_table(head))
  326. *phead = head;
  327. else
  328. entry = NULL;
  329. spin_unlock(&sysctl_lock);
  330. return entry;
  331. }
  332. static struct ctl_node *first_usable_entry(struct rb_node *node)
  333. {
  334. struct ctl_node *ctl_node;
  335. for (;node; node = rb_next(node)) {
  336. ctl_node = rb_entry(node, struct ctl_node, node);
  337. if (use_table(ctl_node->header))
  338. return ctl_node;
  339. }
  340. return NULL;
  341. }
  342. static void first_entry(struct ctl_dir *dir,
  343. struct ctl_table_header **phead, struct ctl_table **pentry)
  344. {
  345. struct ctl_table_header *head = NULL;
  346. struct ctl_table *entry = NULL;
  347. struct ctl_node *ctl_node;
  348. spin_lock(&sysctl_lock);
  349. ctl_node = first_usable_entry(rb_first(&dir->root));
  350. spin_unlock(&sysctl_lock);
  351. if (ctl_node) {
  352. head = ctl_node->header;
  353. entry = &head->ctl_table[ctl_node - head->node];
  354. }
  355. *phead = head;
  356. *pentry = entry;
  357. }
  358. static void next_entry(struct ctl_table_header **phead, struct ctl_table **pentry)
  359. {
  360. struct ctl_table_header *head = *phead;
  361. struct ctl_table *entry = *pentry;
  362. struct ctl_node *ctl_node = &head->node[entry - head->ctl_table];
  363. spin_lock(&sysctl_lock);
  364. unuse_table(head);
  365. ctl_node = first_usable_entry(rb_next(&ctl_node->node));
  366. spin_unlock(&sysctl_lock);
  367. head = NULL;
  368. if (ctl_node) {
  369. head = ctl_node->header;
  370. entry = &head->ctl_table[ctl_node - head->node];
  371. }
  372. *phead = head;
  373. *pentry = entry;
  374. }
  375. /*
  376. * sysctl_perm does NOT grant the superuser all rights automatically, because
  377. * some sysctl variables are readonly even to root.
  378. */
  379. static int test_perm(int mode, int op)
  380. {
  381. if (uid_eq(current_euid(), GLOBAL_ROOT_UID))
  382. mode >>= 6;
  383. else if (in_egroup_p(GLOBAL_ROOT_GID))
  384. mode >>= 3;
  385. if ((op & ~mode & (MAY_READ|MAY_WRITE|MAY_EXEC)) == 0)
  386. return 0;
  387. return -EACCES;
  388. }
  389. static int sysctl_perm(struct ctl_table_header *head, struct ctl_table *table, int op)
  390. {
  391. struct ctl_table_root *root = head->root;
  392. int mode;
  393. if (root->permissions)
  394. mode = root->permissions(head, table);
  395. else
  396. mode = table->mode;
  397. return test_perm(mode, op);
  398. }
  399. static struct inode *proc_sys_make_inode(struct super_block *sb,
  400. struct ctl_table_header *head, struct ctl_table *table)
  401. {
  402. struct ctl_table_root *root = head->root;
  403. struct inode *inode;
  404. struct proc_inode *ei;
  405. inode = new_inode(sb);
  406. if (!inode)
  407. goto out;
  408. inode->i_ino = get_next_ino();
  409. ei = PROC_I(inode);
  410. spin_lock(&sysctl_lock);
  411. if (unlikely(head->unregistering)) {
  412. spin_unlock(&sysctl_lock);
  413. iput(inode);
  414. inode = NULL;
  415. goto out;
  416. }
  417. ei->sysctl = head;
  418. ei->sysctl_entry = table;
  419. hlist_add_head_rcu(&ei->sysctl_inodes, &head->inodes);
  420. head->count++;
  421. spin_unlock(&sysctl_lock);
  422. inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
  423. inode->i_mode = table->mode;
  424. if (!S_ISDIR(table->mode)) {
  425. inode->i_mode |= S_IFREG;
  426. inode->i_op = &proc_sys_inode_operations;
  427. inode->i_fop = &proc_sys_file_operations;
  428. } else {
  429. inode->i_mode |= S_IFDIR;
  430. inode->i_op = &proc_sys_dir_operations;
  431. inode->i_fop = &proc_sys_dir_file_operations;
  432. if (is_empty_dir(head))
  433. make_empty_dir_inode(inode);
  434. }
  435. if (root->set_ownership)
  436. root->set_ownership(head, table, &inode->i_uid, &inode->i_gid);
  437. out:
  438. return inode;
  439. }
  440. void proc_sys_evict_inode(struct inode *inode, struct ctl_table_header *head)
  441. {
  442. spin_lock(&sysctl_lock);
  443. hlist_del_init_rcu(&PROC_I(inode)->sysctl_inodes);
  444. if (!--head->count)
  445. kfree_rcu(head, rcu);
  446. spin_unlock(&sysctl_lock);
  447. }
  448. static struct ctl_table_header *grab_header(struct inode *inode)
  449. {
  450. struct ctl_table_header *head = PROC_I(inode)->sysctl;
  451. if (!head)
  452. head = &sysctl_table_root.default_set.dir.header;
  453. return sysctl_head_grab(head);
  454. }
  455. static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry,
  456. unsigned int flags)
  457. {
  458. struct ctl_table_header *head = grab_header(dir);
  459. struct ctl_table_header *h = NULL;
  460. const struct qstr *name = &dentry->d_name;
  461. struct ctl_table *p;
  462. struct inode *inode;
  463. struct dentry *err = ERR_PTR(-ENOENT);
  464. struct ctl_dir *ctl_dir;
  465. int ret;
  466. if (IS_ERR(head))
  467. return ERR_CAST(head);
  468. ctl_dir = container_of(head, struct ctl_dir, header);
  469. p = lookup_entry(&h, ctl_dir, name->name, name->len);
  470. if (!p)
  471. goto out;
  472. if (S_ISLNK(p->mode)) {
  473. ret = sysctl_follow_link(&h, &p);
  474. err = ERR_PTR(ret);
  475. if (ret)
  476. goto out;
  477. }
  478. err = ERR_PTR(-ENOMEM);
  479. inode = proc_sys_make_inode(dir->i_sb, h ? h : head, p);
  480. if (!inode)
  481. goto out;
  482. err = NULL;
  483. d_set_d_op(dentry, &proc_sys_dentry_operations);
  484. d_add(dentry, inode);
  485. out:
  486. if (h)
  487. sysctl_head_finish(h);
  488. sysctl_head_finish(head);
  489. return err;
  490. }
  491. static ssize_t proc_sys_call_handler(struct file *filp, void __user *buf,
  492. size_t count, loff_t *ppos, int write)
  493. {
  494. struct inode *inode = file_inode(filp);
  495. struct ctl_table_header *head = grab_header(inode);
  496. struct ctl_table *table = PROC_I(inode)->sysctl_entry;
  497. ssize_t error;
  498. size_t res;
  499. if (IS_ERR(head))
  500. return PTR_ERR(head);
  501. /*
  502. * At this point we know that the sysctl was not unregistered
  503. * and won't be until we finish.
  504. */
  505. error = -EPERM;
  506. if (sysctl_perm(head, table, write ? MAY_WRITE : MAY_READ))
  507. goto out;
  508. /* if that can happen at all, it should be -EINVAL, not -EISDIR */
  509. error = -EINVAL;
  510. if (!table->proc_handler)
  511. goto out;
  512. /* careful: calling conventions are nasty here */
  513. res = count;
  514. error = table->proc_handler(table, write, buf, &res, ppos);
  515. if (!error)
  516. error = res;
  517. out:
  518. sysctl_head_finish(head);
  519. return error;
  520. }
  521. static ssize_t proc_sys_read(struct file *filp, char __user *buf,
  522. size_t count, loff_t *ppos)
  523. {
  524. return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 0);
  525. }
  526. static ssize_t proc_sys_write(struct file *filp, const char __user *buf,
  527. size_t count, loff_t *ppos)
  528. {
  529. return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 1);
  530. }
  531. static int proc_sys_open(struct inode *inode, struct file *filp)
  532. {
  533. struct ctl_table_header *head = grab_header(inode);
  534. struct ctl_table *table = PROC_I(inode)->sysctl_entry;
  535. /* sysctl was unregistered */
  536. if (IS_ERR(head))
  537. return PTR_ERR(head);
  538. if (table->poll)
  539. filp->private_data = proc_sys_poll_event(table->poll);
  540. sysctl_head_finish(head);
  541. return 0;
  542. }
  543. static __poll_t proc_sys_poll(struct file *filp, poll_table *wait)
  544. {
  545. struct inode *inode = file_inode(filp);
  546. struct ctl_table_header *head = grab_header(inode);
  547. struct ctl_table *table = PROC_I(inode)->sysctl_entry;
  548. __poll_t ret = DEFAULT_POLLMASK;
  549. unsigned long event;
  550. /* sysctl was unregistered */
  551. if (IS_ERR(head))
  552. return EPOLLERR | EPOLLHUP;
  553. if (!table->proc_handler)
  554. goto out;
  555. if (!table->poll)
  556. goto out;
  557. event = (unsigned long)filp->private_data;
  558. poll_wait(filp, &table->poll->wait, wait);
  559. if (event != atomic_read(&table->poll->event)) {
  560. filp->private_data = proc_sys_poll_event(table->poll);
  561. ret = EPOLLIN | EPOLLRDNORM | EPOLLERR | EPOLLPRI;
  562. }
  563. out:
  564. sysctl_head_finish(head);
  565. return ret;
  566. }
  567. static bool proc_sys_fill_cache(struct file *file,
  568. struct dir_context *ctx,
  569. struct ctl_table_header *head,
  570. struct ctl_table *table)
  571. {
  572. struct dentry *child, *dir = file->f_path.dentry;
  573. struct inode *inode;
  574. struct qstr qname;
  575. ino_t ino = 0;
  576. unsigned type = DT_UNKNOWN;
  577. qname.name = table->procname;
  578. qname.len = strlen(table->procname);
  579. qname.hash = full_name_hash(dir, qname.name, qname.len);
  580. child = d_lookup(dir, &qname);
  581. if (!child) {
  582. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
  583. child = d_alloc_parallel(dir, &qname, &wq);
  584. if (IS_ERR(child))
  585. return false;
  586. if (d_in_lookup(child)) {
  587. inode = proc_sys_make_inode(dir->d_sb, head, table);
  588. if (!inode) {
  589. d_lookup_done(child);
  590. dput(child);
  591. return false;
  592. }
  593. d_set_d_op(child, &proc_sys_dentry_operations);
  594. d_add(child, inode);
  595. }
  596. }
  597. inode = d_inode(child);
  598. ino = inode->i_ino;
  599. type = inode->i_mode >> 12;
  600. dput(child);
  601. return dir_emit(ctx, qname.name, qname.len, ino, type);
  602. }
  603. static bool proc_sys_link_fill_cache(struct file *file,
  604. struct dir_context *ctx,
  605. struct ctl_table_header *head,
  606. struct ctl_table *table)
  607. {
  608. bool ret = true;
  609. head = sysctl_head_grab(head);
  610. if (S_ISLNK(table->mode)) {
  611. /* It is not an error if we can not follow the link ignore it */
  612. int err = sysctl_follow_link(&head, &table);
  613. if (err)
  614. goto out;
  615. }
  616. ret = proc_sys_fill_cache(file, ctx, head, table);
  617. out:
  618. sysctl_head_finish(head);
  619. return ret;
  620. }
  621. static int scan(struct ctl_table_header *head, struct ctl_table *table,
  622. unsigned long *pos, struct file *file,
  623. struct dir_context *ctx)
  624. {
  625. bool res;
  626. if ((*pos)++ < ctx->pos)
  627. return true;
  628. if (unlikely(S_ISLNK(table->mode)))
  629. res = proc_sys_link_fill_cache(file, ctx, head, table);
  630. else
  631. res = proc_sys_fill_cache(file, ctx, head, table);
  632. if (res)
  633. ctx->pos = *pos;
  634. return res;
  635. }
  636. static int proc_sys_readdir(struct file *file, struct dir_context *ctx)
  637. {
  638. struct ctl_table_header *head = grab_header(file_inode(file));
  639. struct ctl_table_header *h = NULL;
  640. struct ctl_table *entry;
  641. struct ctl_dir *ctl_dir;
  642. unsigned long pos;
  643. if (IS_ERR(head))
  644. return PTR_ERR(head);
  645. ctl_dir = container_of(head, struct ctl_dir, header);
  646. if (!dir_emit_dots(file, ctx))
  647. goto out;
  648. pos = 2;
  649. for (first_entry(ctl_dir, &h, &entry); h; next_entry(&h, &entry)) {
  650. if (!scan(h, entry, &pos, file, ctx)) {
  651. sysctl_head_finish(h);
  652. break;
  653. }
  654. }
  655. out:
  656. sysctl_head_finish(head);
  657. return 0;
  658. }
  659. static int proc_sys_permission(struct inode *inode, int mask)
  660. {
  661. /*
  662. * sysctl entries that are not writeable,
  663. * are _NOT_ writeable, capabilities or not.
  664. */
  665. struct ctl_table_header *head;
  666. struct ctl_table *table;
  667. int error;
  668. /* Executable files are not allowed under /proc/sys/ */
  669. if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))
  670. return -EACCES;
  671. head = grab_header(inode);
  672. if (IS_ERR(head))
  673. return PTR_ERR(head);
  674. table = PROC_I(inode)->sysctl_entry;
  675. if (!table) /* global root - r-xr-xr-x */
  676. error = mask & MAY_WRITE ? -EACCES : 0;
  677. else /* Use the permissions on the sysctl table entry */
  678. error = sysctl_perm(head, table, mask & ~MAY_NOT_BLOCK);
  679. sysctl_head_finish(head);
  680. return error;
  681. }
  682. static int proc_sys_setattr(struct dentry *dentry, struct iattr *attr)
  683. {
  684. struct inode *inode = d_inode(dentry);
  685. int error;
  686. if (attr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
  687. return -EPERM;
  688. error = setattr_prepare(dentry, attr);
  689. if (error)
  690. return error;
  691. setattr_copy(inode, attr);
  692. mark_inode_dirty(inode);
  693. return 0;
  694. }
  695. static int proc_sys_getattr(const struct path *path, struct kstat *stat,
  696. u32 request_mask, unsigned int query_flags)
  697. {
  698. struct inode *inode = d_inode(path->dentry);
  699. struct ctl_table_header *head = grab_header(inode);
  700. struct ctl_table *table = PROC_I(inode)->sysctl_entry;
  701. if (IS_ERR(head))
  702. return PTR_ERR(head);
  703. generic_fillattr(inode, stat);
  704. if (table)
  705. stat->mode = (stat->mode & S_IFMT) | table->mode;
  706. sysctl_head_finish(head);
  707. return 0;
  708. }
  709. static const struct file_operations proc_sys_file_operations = {
  710. .open = proc_sys_open,
  711. .poll = proc_sys_poll,
  712. .read = proc_sys_read,
  713. .write = proc_sys_write,
  714. .llseek = default_llseek,
  715. };
  716. static const struct file_operations proc_sys_dir_file_operations = {
  717. .read = generic_read_dir,
  718. .iterate_shared = proc_sys_readdir,
  719. .llseek = generic_file_llseek,
  720. };
  721. static const struct inode_operations proc_sys_inode_operations = {
  722. .permission = proc_sys_permission,
  723. .setattr = proc_sys_setattr,
  724. .getattr = proc_sys_getattr,
  725. };
  726. static const struct inode_operations proc_sys_dir_operations = {
  727. .lookup = proc_sys_lookup,
  728. .permission = proc_sys_permission,
  729. .setattr = proc_sys_setattr,
  730. .getattr = proc_sys_getattr,
  731. };
  732. static int proc_sys_revalidate(struct dentry *dentry, unsigned int flags)
  733. {
  734. if (flags & LOOKUP_RCU)
  735. return -ECHILD;
  736. return !PROC_I(d_inode(dentry))->sysctl->unregistering;
  737. }
  738. static int proc_sys_delete(const struct dentry *dentry)
  739. {
  740. return !!PROC_I(d_inode(dentry))->sysctl->unregistering;
  741. }
  742. static int sysctl_is_seen(struct ctl_table_header *p)
  743. {
  744. struct ctl_table_set *set = p->set;
  745. int res;
  746. spin_lock(&sysctl_lock);
  747. if (p->unregistering)
  748. res = 0;
  749. else if (!set->is_seen)
  750. res = 1;
  751. else
  752. res = set->is_seen(set);
  753. spin_unlock(&sysctl_lock);
  754. return res;
  755. }
  756. static int proc_sys_compare(const struct dentry *dentry,
  757. unsigned int len, const char *str, const struct qstr *name)
  758. {
  759. struct ctl_table_header *head;
  760. struct inode *inode;
  761. /* Although proc doesn't have negative dentries, rcu-walk means
  762. * that inode here can be NULL */
  763. /* AV: can it, indeed? */
  764. inode = d_inode_rcu(dentry);
  765. if (!inode)
  766. return 1;
  767. if (name->len != len)
  768. return 1;
  769. if (memcmp(name->name, str, len))
  770. return 1;
  771. head = rcu_dereference(PROC_I(inode)->sysctl);
  772. return !head || !sysctl_is_seen(head);
  773. }
  774. static const struct dentry_operations proc_sys_dentry_operations = {
  775. .d_revalidate = proc_sys_revalidate,
  776. .d_delete = proc_sys_delete,
  777. .d_compare = proc_sys_compare,
  778. };
  779. static struct ctl_dir *find_subdir(struct ctl_dir *dir,
  780. const char *name, int namelen)
  781. {
  782. struct ctl_table_header *head;
  783. struct ctl_table *entry;
  784. entry = find_entry(&head, dir, name, namelen);
  785. if (!entry)
  786. return ERR_PTR(-ENOENT);
  787. if (!S_ISDIR(entry->mode))
  788. return ERR_PTR(-ENOTDIR);
  789. return container_of(head, struct ctl_dir, header);
  790. }
  791. static struct ctl_dir *new_dir(struct ctl_table_set *set,
  792. const char *name, int namelen)
  793. {
  794. struct ctl_table *table;
  795. struct ctl_dir *new;
  796. struct ctl_node *node;
  797. char *new_name;
  798. new = kzalloc(sizeof(*new) + sizeof(struct ctl_node) +
  799. sizeof(struct ctl_table)*2 + namelen + 1,
  800. GFP_KERNEL);
  801. if (!new)
  802. return NULL;
  803. node = (struct ctl_node *)(new + 1);
  804. table = (struct ctl_table *)(node + 1);
  805. new_name = (char *)(table + 2);
  806. memcpy(new_name, name, namelen);
  807. new_name[namelen] = '\0';
  808. table[0].procname = new_name;
  809. table[0].mode = S_IFDIR|S_IRUGO|S_IXUGO;
  810. init_header(&new->header, set->dir.header.root, set, node, table);
  811. return new;
  812. }
  813. /**
  814. * get_subdir - find or create a subdir with the specified name.
  815. * @dir: Directory to create the subdirectory in
  816. * @name: The name of the subdirectory to find or create
  817. * @namelen: The length of name
  818. *
  819. * Takes a directory with an elevated reference count so we know that
  820. * if we drop the lock the directory will not go away. Upon success
  821. * the reference is moved from @dir to the returned subdirectory.
  822. * Upon error an error code is returned and the reference on @dir is
  823. * simply dropped.
  824. */
  825. static struct ctl_dir *get_subdir(struct ctl_dir *dir,
  826. const char *name, int namelen)
  827. {
  828. struct ctl_table_set *set = dir->header.set;
  829. struct ctl_dir *subdir, *new = NULL;
  830. int err;
  831. spin_lock(&sysctl_lock);
  832. subdir = find_subdir(dir, name, namelen);
  833. if (!IS_ERR(subdir))
  834. goto found;
  835. if (PTR_ERR(subdir) != -ENOENT)
  836. goto failed;
  837. spin_unlock(&sysctl_lock);
  838. new = new_dir(set, name, namelen);
  839. spin_lock(&sysctl_lock);
  840. subdir = ERR_PTR(-ENOMEM);
  841. if (!new)
  842. goto failed;
  843. /* Was the subdir added while we dropped the lock? */
  844. subdir = find_subdir(dir, name, namelen);
  845. if (!IS_ERR(subdir))
  846. goto found;
  847. if (PTR_ERR(subdir) != -ENOENT)
  848. goto failed;
  849. /* Nope. Use the our freshly made directory entry. */
  850. err = insert_header(dir, &new->header);
  851. subdir = ERR_PTR(err);
  852. if (err)
  853. goto failed;
  854. subdir = new;
  855. found:
  856. subdir->header.nreg++;
  857. failed:
  858. if (IS_ERR(subdir)) {
  859. pr_err("sysctl could not get directory: ");
  860. sysctl_print_dir(dir);
  861. pr_cont("/%*.*s %ld\n",
  862. namelen, namelen, name, PTR_ERR(subdir));
  863. }
  864. drop_sysctl_table(&dir->header);
  865. if (new)
  866. drop_sysctl_table(&new->header);
  867. spin_unlock(&sysctl_lock);
  868. return subdir;
  869. }
  870. static struct ctl_dir *xlate_dir(struct ctl_table_set *set, struct ctl_dir *dir)
  871. {
  872. struct ctl_dir *parent;
  873. const char *procname;
  874. if (!dir->header.parent)
  875. return &set->dir;
  876. parent = xlate_dir(set, dir->header.parent);
  877. if (IS_ERR(parent))
  878. return parent;
  879. procname = dir->header.ctl_table[0].procname;
  880. return find_subdir(parent, procname, strlen(procname));
  881. }
  882. static int sysctl_follow_link(struct ctl_table_header **phead,
  883. struct ctl_table **pentry)
  884. {
  885. struct ctl_table_header *head;
  886. struct ctl_table_root *root;
  887. struct ctl_table_set *set;
  888. struct ctl_table *entry;
  889. struct ctl_dir *dir;
  890. int ret;
  891. ret = 0;
  892. spin_lock(&sysctl_lock);
  893. root = (*pentry)->data;
  894. set = lookup_header_set(root);
  895. dir = xlate_dir(set, (*phead)->parent);
  896. if (IS_ERR(dir))
  897. ret = PTR_ERR(dir);
  898. else {
  899. const char *procname = (*pentry)->procname;
  900. head = NULL;
  901. entry = find_entry(&head, dir, procname, strlen(procname));
  902. ret = -ENOENT;
  903. if (entry && use_table(head)) {
  904. unuse_table(*phead);
  905. *phead = head;
  906. *pentry = entry;
  907. ret = 0;
  908. }
  909. }
  910. spin_unlock(&sysctl_lock);
  911. return ret;
  912. }
  913. static int sysctl_err(const char *path, struct ctl_table *table, char *fmt, ...)
  914. {
  915. struct va_format vaf;
  916. va_list args;
  917. va_start(args, fmt);
  918. vaf.fmt = fmt;
  919. vaf.va = &args;
  920. pr_err("sysctl table check failed: %s/%s %pV\n",
  921. path, table->procname, &vaf);
  922. va_end(args);
  923. return -EINVAL;
  924. }
  925. static int sysctl_check_table_array(const char *path, struct ctl_table *table)
  926. {
  927. int err = 0;
  928. if ((table->proc_handler == proc_douintvec) ||
  929. (table->proc_handler == proc_douintvec_minmax)) {
  930. if (table->maxlen != sizeof(unsigned int))
  931. err |= sysctl_err(path, table, "array now allowed");
  932. }
  933. return err;
  934. }
  935. static int sysctl_check_table(const char *path, struct ctl_table *table)
  936. {
  937. int err = 0;
  938. for (; table->procname; table++) {
  939. if (table->child)
  940. err |= sysctl_err(path, table, "Not a file");
  941. if ((table->proc_handler == proc_dostring) ||
  942. (table->proc_handler == proc_dointvec) ||
  943. (table->proc_handler == proc_douintvec) ||
  944. (table->proc_handler == proc_douintvec_minmax) ||
  945. (table->proc_handler == proc_dointvec_minmax) ||
  946. (table->proc_handler == proc_dointvec_jiffies) ||
  947. (table->proc_handler == proc_dointvec_userhz_jiffies) ||
  948. (table->proc_handler == proc_dointvec_ms_jiffies) ||
  949. (table->proc_handler == proc_doulongvec_minmax) ||
  950. (table->proc_handler == proc_doulongvec_ms_jiffies_minmax)) {
  951. if (!table->data)
  952. err |= sysctl_err(path, table, "No data");
  953. if (!table->maxlen)
  954. err |= sysctl_err(path, table, "No maxlen");
  955. else
  956. err |= sysctl_check_table_array(path, table);
  957. }
  958. if (!table->proc_handler)
  959. err |= sysctl_err(path, table, "No proc_handler");
  960. if ((table->mode & (S_IRUGO|S_IWUGO)) != table->mode)
  961. err |= sysctl_err(path, table, "bogus .mode 0%o",
  962. table->mode);
  963. }
  964. return err;
  965. }
  966. static struct ctl_table_header *new_links(struct ctl_dir *dir, struct ctl_table *table,
  967. struct ctl_table_root *link_root)
  968. {
  969. struct ctl_table *link_table, *entry, *link;
  970. struct ctl_table_header *links;
  971. struct ctl_node *node;
  972. char *link_name;
  973. int nr_entries, name_bytes;
  974. name_bytes = 0;
  975. nr_entries = 0;
  976. for (entry = table; entry->procname; entry++) {
  977. nr_entries++;
  978. name_bytes += strlen(entry->procname) + 1;
  979. }
  980. links = kzalloc(sizeof(struct ctl_table_header) +
  981. sizeof(struct ctl_node)*nr_entries +
  982. sizeof(struct ctl_table)*(nr_entries + 1) +
  983. name_bytes,
  984. GFP_KERNEL);
  985. if (!links)
  986. return NULL;
  987. node = (struct ctl_node *)(links + 1);
  988. link_table = (struct ctl_table *)(node + nr_entries);
  989. link_name = (char *)&link_table[nr_entries + 1];
  990. for (link = link_table, entry = table; entry->procname; link++, entry++) {
  991. int len = strlen(entry->procname) + 1;
  992. memcpy(link_name, entry->procname, len);
  993. link->procname = link_name;
  994. link->mode = S_IFLNK|S_IRWXUGO;
  995. link->data = link_root;
  996. link_name += len;
  997. }
  998. init_header(links, dir->header.root, dir->header.set, node, link_table);
  999. links->nreg = nr_entries;
  1000. return links;
  1001. }
  1002. static bool get_links(struct ctl_dir *dir,
  1003. struct ctl_table *table, struct ctl_table_root *link_root)
  1004. {
  1005. struct ctl_table_header *head;
  1006. struct ctl_table *entry, *link;
  1007. /* Are there links available for every entry in table? */
  1008. for (entry = table; entry->procname; entry++) {
  1009. const char *procname = entry->procname;
  1010. link = find_entry(&head, dir, procname, strlen(procname));
  1011. if (!link)
  1012. return false;
  1013. if (S_ISDIR(link->mode) && S_ISDIR(entry->mode))
  1014. continue;
  1015. if (S_ISLNK(link->mode) && (link->data == link_root))
  1016. continue;
  1017. return false;
  1018. }
  1019. /* The checks passed. Increase the registration count on the links */
  1020. for (entry = table; entry->procname; entry++) {
  1021. const char *procname = entry->procname;
  1022. link = find_entry(&head, dir, procname, strlen(procname));
  1023. head->nreg++;
  1024. }
  1025. return true;
  1026. }
  1027. static int insert_links(struct ctl_table_header *head)
  1028. {
  1029. struct ctl_table_set *root_set = &sysctl_table_root.default_set;
  1030. struct ctl_dir *core_parent = NULL;
  1031. struct ctl_table_header *links;
  1032. int err;
  1033. if (head->set == root_set)
  1034. return 0;
  1035. core_parent = xlate_dir(root_set, head->parent);
  1036. if (IS_ERR(core_parent))
  1037. return 0;
  1038. if (get_links(core_parent, head->ctl_table, head->root))
  1039. return 0;
  1040. core_parent->header.nreg++;
  1041. spin_unlock(&sysctl_lock);
  1042. links = new_links(core_parent, head->ctl_table, head->root);
  1043. spin_lock(&sysctl_lock);
  1044. err = -ENOMEM;
  1045. if (!links)
  1046. goto out;
  1047. err = 0;
  1048. if (get_links(core_parent, head->ctl_table, head->root)) {
  1049. kfree(links);
  1050. goto out;
  1051. }
  1052. err = insert_header(core_parent, links);
  1053. if (err)
  1054. kfree(links);
  1055. out:
  1056. drop_sysctl_table(&core_parent->header);
  1057. return err;
  1058. }
  1059. /**
  1060. * __register_sysctl_table - register a leaf sysctl table
  1061. * @set: Sysctl tree to register on
  1062. * @path: The path to the directory the sysctl table is in.
  1063. * @table: the top-level table structure
  1064. *
  1065. * Register a sysctl table hierarchy. @table should be a filled in ctl_table
  1066. * array. A completely 0 filled entry terminates the table.
  1067. *
  1068. * The members of the &struct ctl_table structure are used as follows:
  1069. *
  1070. * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not
  1071. * enter a sysctl file
  1072. *
  1073. * data - a pointer to data for use by proc_handler
  1074. *
  1075. * maxlen - the maximum size in bytes of the data
  1076. *
  1077. * mode - the file permissions for the /proc/sys file
  1078. *
  1079. * child - must be %NULL.
  1080. *
  1081. * proc_handler - the text handler routine (described below)
  1082. *
  1083. * extra1, extra2 - extra pointers usable by the proc handler routines
  1084. *
  1085. * Leaf nodes in the sysctl tree will be represented by a single file
  1086. * under /proc; non-leaf nodes will be represented by directories.
  1087. *
  1088. * There must be a proc_handler routine for any terminal nodes.
  1089. * Several default handlers are available to cover common cases -
  1090. *
  1091. * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(),
  1092. * proc_dointvec_userhz_jiffies(), proc_dointvec_minmax(),
  1093. * proc_doulongvec_ms_jiffies_minmax(), proc_doulongvec_minmax()
  1094. *
  1095. * It is the handler's job to read the input buffer from user memory
  1096. * and process it. The handler should return 0 on success.
  1097. *
  1098. * This routine returns %NULL on a failure to register, and a pointer
  1099. * to the table header on success.
  1100. */
  1101. struct ctl_table_header *__register_sysctl_table(
  1102. struct ctl_table_set *set,
  1103. const char *path, struct ctl_table *table)
  1104. {
  1105. struct ctl_table_root *root = set->dir.header.root;
  1106. struct ctl_table_header *header;
  1107. const char *name, *nextname;
  1108. struct ctl_dir *dir;
  1109. struct ctl_table *entry;
  1110. struct ctl_node *node;
  1111. int nr_entries = 0;
  1112. for (entry = table; entry->procname; entry++)
  1113. nr_entries++;
  1114. header = kzalloc(sizeof(struct ctl_table_header) +
  1115. sizeof(struct ctl_node)*nr_entries, GFP_KERNEL);
  1116. if (!header)
  1117. return NULL;
  1118. node = (struct ctl_node *)(header + 1);
  1119. init_header(header, root, set, node, table);
  1120. if (sysctl_check_table(path, table))
  1121. goto fail;
  1122. spin_lock(&sysctl_lock);
  1123. dir = &set->dir;
  1124. /* Reference moved down the diretory tree get_subdir */
  1125. dir->header.nreg++;
  1126. spin_unlock(&sysctl_lock);
  1127. /* Find the directory for the ctl_table */
  1128. for (name = path; name; name = nextname) {
  1129. int namelen;
  1130. nextname = strchr(name, '/');
  1131. if (nextname) {
  1132. namelen = nextname - name;
  1133. nextname++;
  1134. } else {
  1135. namelen = strlen(name);
  1136. }
  1137. if (namelen == 0)
  1138. continue;
  1139. dir = get_subdir(dir, name, namelen);
  1140. if (IS_ERR(dir))
  1141. goto fail;
  1142. }
  1143. spin_lock(&sysctl_lock);
  1144. if (insert_header(dir, header))
  1145. goto fail_put_dir_locked;
  1146. drop_sysctl_table(&dir->header);
  1147. spin_unlock(&sysctl_lock);
  1148. return header;
  1149. fail_put_dir_locked:
  1150. drop_sysctl_table(&dir->header);
  1151. spin_unlock(&sysctl_lock);
  1152. fail:
  1153. kfree(header);
  1154. dump_stack();
  1155. return NULL;
  1156. }
  1157. /**
  1158. * register_sysctl - register a sysctl table
  1159. * @path: The path to the directory the sysctl table is in.
  1160. * @table: the table structure
  1161. *
  1162. * Register a sysctl table. @table should be a filled in ctl_table
  1163. * array. A completely 0 filled entry terminates the table.
  1164. *
  1165. * See __register_sysctl_table for more details.
  1166. */
  1167. struct ctl_table_header *register_sysctl(const char *path, struct ctl_table *table)
  1168. {
  1169. return __register_sysctl_table(&sysctl_table_root.default_set,
  1170. path, table);
  1171. }
  1172. EXPORT_SYMBOL(register_sysctl);
  1173. static char *append_path(const char *path, char *pos, const char *name)
  1174. {
  1175. int namelen;
  1176. namelen = strlen(name);
  1177. if (((pos - path) + namelen + 2) >= PATH_MAX)
  1178. return NULL;
  1179. memcpy(pos, name, namelen);
  1180. pos[namelen] = '/';
  1181. pos[namelen + 1] = '\0';
  1182. pos += namelen + 1;
  1183. return pos;
  1184. }
  1185. static int count_subheaders(struct ctl_table *table)
  1186. {
  1187. int has_files = 0;
  1188. int nr_subheaders = 0;
  1189. struct ctl_table *entry;
  1190. /* special case: no directory and empty directory */
  1191. if (!table || !table->procname)
  1192. return 1;
  1193. for (entry = table; entry->procname; entry++) {
  1194. if (entry->child)
  1195. nr_subheaders += count_subheaders(entry->child);
  1196. else
  1197. has_files = 1;
  1198. }
  1199. return nr_subheaders + has_files;
  1200. }
  1201. static int register_leaf_sysctl_tables(const char *path, char *pos,
  1202. struct ctl_table_header ***subheader, struct ctl_table_set *set,
  1203. struct ctl_table *table)
  1204. {
  1205. struct ctl_table *ctl_table_arg = NULL;
  1206. struct ctl_table *entry, *files;
  1207. int nr_files = 0;
  1208. int nr_dirs = 0;
  1209. int err = -ENOMEM;
  1210. for (entry = table; entry->procname; entry++) {
  1211. if (entry->child)
  1212. nr_dirs++;
  1213. else
  1214. nr_files++;
  1215. }
  1216. files = table;
  1217. /* If there are mixed files and directories we need a new table */
  1218. if (nr_dirs && nr_files) {
  1219. struct ctl_table *new;
  1220. files = kzalloc(sizeof(struct ctl_table) * (nr_files + 1),
  1221. GFP_KERNEL);
  1222. if (!files)
  1223. goto out;
  1224. ctl_table_arg = files;
  1225. for (new = files, entry = table; entry->procname; entry++) {
  1226. if (entry->child)
  1227. continue;
  1228. *new = *entry;
  1229. new++;
  1230. }
  1231. }
  1232. /* Register everything except a directory full of subdirectories */
  1233. if (nr_files || !nr_dirs) {
  1234. struct ctl_table_header *header;
  1235. header = __register_sysctl_table(set, path, files);
  1236. if (!header) {
  1237. kfree(ctl_table_arg);
  1238. goto out;
  1239. }
  1240. /* Remember if we need to free the file table */
  1241. header->ctl_table_arg = ctl_table_arg;
  1242. **subheader = header;
  1243. (*subheader)++;
  1244. }
  1245. /* Recurse into the subdirectories. */
  1246. for (entry = table; entry->procname; entry++) {
  1247. char *child_pos;
  1248. if (!entry->child)
  1249. continue;
  1250. err = -ENAMETOOLONG;
  1251. child_pos = append_path(path, pos, entry->procname);
  1252. if (!child_pos)
  1253. goto out;
  1254. err = register_leaf_sysctl_tables(path, child_pos, subheader,
  1255. set, entry->child);
  1256. pos[0] = '\0';
  1257. if (err)
  1258. goto out;
  1259. }
  1260. err = 0;
  1261. out:
  1262. /* On failure our caller will unregister all registered subheaders */
  1263. return err;
  1264. }
  1265. /**
  1266. * __register_sysctl_paths - register a sysctl table hierarchy
  1267. * @set: Sysctl tree to register on
  1268. * @path: The path to the directory the sysctl table is in.
  1269. * @table: the top-level table structure
  1270. *
  1271. * Register a sysctl table hierarchy. @table should be a filled in ctl_table
  1272. * array. A completely 0 filled entry terminates the table.
  1273. *
  1274. * See __register_sysctl_table for more details.
  1275. */
  1276. struct ctl_table_header *__register_sysctl_paths(
  1277. struct ctl_table_set *set,
  1278. const struct ctl_path *path, struct ctl_table *table)
  1279. {
  1280. struct ctl_table *ctl_table_arg = table;
  1281. int nr_subheaders = count_subheaders(table);
  1282. struct ctl_table_header *header = NULL, **subheaders, **subheader;
  1283. const struct ctl_path *component;
  1284. char *new_path, *pos;
  1285. pos = new_path = kmalloc(PATH_MAX, GFP_KERNEL);
  1286. if (!new_path)
  1287. return NULL;
  1288. pos[0] = '\0';
  1289. for (component = path; component->procname; component++) {
  1290. pos = append_path(new_path, pos, component->procname);
  1291. if (!pos)
  1292. goto out;
  1293. }
  1294. while (table->procname && table->child && !table[1].procname) {
  1295. pos = append_path(new_path, pos, table->procname);
  1296. if (!pos)
  1297. goto out;
  1298. table = table->child;
  1299. }
  1300. if (nr_subheaders == 1) {
  1301. header = __register_sysctl_table(set, new_path, table);
  1302. if (header)
  1303. header->ctl_table_arg = ctl_table_arg;
  1304. } else {
  1305. header = kzalloc(sizeof(*header) +
  1306. sizeof(*subheaders)*nr_subheaders, GFP_KERNEL);
  1307. if (!header)
  1308. goto out;
  1309. subheaders = (struct ctl_table_header **) (header + 1);
  1310. subheader = subheaders;
  1311. header->ctl_table_arg = ctl_table_arg;
  1312. if (register_leaf_sysctl_tables(new_path, pos, &subheader,
  1313. set, table))
  1314. goto err_register_leaves;
  1315. }
  1316. out:
  1317. kfree(new_path);
  1318. return header;
  1319. err_register_leaves:
  1320. while (subheader > subheaders) {
  1321. struct ctl_table_header *subh = *(--subheader);
  1322. struct ctl_table *table = subh->ctl_table_arg;
  1323. unregister_sysctl_table(subh);
  1324. kfree(table);
  1325. }
  1326. kfree(header);
  1327. header = NULL;
  1328. goto out;
  1329. }
  1330. /**
  1331. * register_sysctl_table_path - register a sysctl table hierarchy
  1332. * @path: The path to the directory the sysctl table is in.
  1333. * @table: the top-level table structure
  1334. *
  1335. * Register a sysctl table hierarchy. @table should be a filled in ctl_table
  1336. * array. A completely 0 filled entry terminates the table.
  1337. *
  1338. * See __register_sysctl_paths for more details.
  1339. */
  1340. struct ctl_table_header *register_sysctl_paths(const struct ctl_path *path,
  1341. struct ctl_table *table)
  1342. {
  1343. return __register_sysctl_paths(&sysctl_table_root.default_set,
  1344. path, table);
  1345. }
  1346. EXPORT_SYMBOL(register_sysctl_paths);
  1347. /**
  1348. * register_sysctl_table - register a sysctl table hierarchy
  1349. * @table: the top-level table structure
  1350. *
  1351. * Register a sysctl table hierarchy. @table should be a filled in ctl_table
  1352. * array. A completely 0 filled entry terminates the table.
  1353. *
  1354. * See register_sysctl_paths for more details.
  1355. */
  1356. struct ctl_table_header *register_sysctl_table(struct ctl_table *table)
  1357. {
  1358. static const struct ctl_path null_path[] = { {} };
  1359. return register_sysctl_paths(null_path, table);
  1360. }
  1361. EXPORT_SYMBOL(register_sysctl_table);
  1362. static void put_links(struct ctl_table_header *header)
  1363. {
  1364. struct ctl_table_set *root_set = &sysctl_table_root.default_set;
  1365. struct ctl_table_root *root = header->root;
  1366. struct ctl_dir *parent = header->parent;
  1367. struct ctl_dir *core_parent;
  1368. struct ctl_table *entry;
  1369. if (header->set == root_set)
  1370. return;
  1371. core_parent = xlate_dir(root_set, parent);
  1372. if (IS_ERR(core_parent))
  1373. return;
  1374. for (entry = header->ctl_table; entry->procname; entry++) {
  1375. struct ctl_table_header *link_head;
  1376. struct ctl_table *link;
  1377. const char *name = entry->procname;
  1378. link = find_entry(&link_head, core_parent, name, strlen(name));
  1379. if (link &&
  1380. ((S_ISDIR(link->mode) && S_ISDIR(entry->mode)) ||
  1381. (S_ISLNK(link->mode) && (link->data == root)))) {
  1382. drop_sysctl_table(link_head);
  1383. }
  1384. else {
  1385. pr_err("sysctl link missing during unregister: ");
  1386. sysctl_print_dir(parent);
  1387. pr_cont("/%s\n", name);
  1388. }
  1389. }
  1390. }
  1391. static void drop_sysctl_table(struct ctl_table_header *header)
  1392. {
  1393. struct ctl_dir *parent = header->parent;
  1394. if (--header->nreg)
  1395. return;
  1396. put_links(header);
  1397. start_unregistering(header);
  1398. if (!--header->count)
  1399. kfree_rcu(header, rcu);
  1400. if (parent)
  1401. drop_sysctl_table(&parent->header);
  1402. }
  1403. /**
  1404. * unregister_sysctl_table - unregister a sysctl table hierarchy
  1405. * @header: the header returned from register_sysctl_table
  1406. *
  1407. * Unregisters the sysctl table and all children. proc entries may not
  1408. * actually be removed until they are no longer used by anyone.
  1409. */
  1410. void unregister_sysctl_table(struct ctl_table_header * header)
  1411. {
  1412. int nr_subheaders;
  1413. might_sleep();
  1414. if (header == NULL)
  1415. return;
  1416. nr_subheaders = count_subheaders(header->ctl_table_arg);
  1417. if (unlikely(nr_subheaders > 1)) {
  1418. struct ctl_table_header **subheaders;
  1419. int i;
  1420. subheaders = (struct ctl_table_header **)(header + 1);
  1421. for (i = nr_subheaders -1; i >= 0; i--) {
  1422. struct ctl_table_header *subh = subheaders[i];
  1423. struct ctl_table *table = subh->ctl_table_arg;
  1424. unregister_sysctl_table(subh);
  1425. kfree(table);
  1426. }
  1427. kfree(header);
  1428. return;
  1429. }
  1430. spin_lock(&sysctl_lock);
  1431. drop_sysctl_table(header);
  1432. spin_unlock(&sysctl_lock);
  1433. }
  1434. EXPORT_SYMBOL(unregister_sysctl_table);
  1435. void setup_sysctl_set(struct ctl_table_set *set,
  1436. struct ctl_table_root *root,
  1437. int (*is_seen)(struct ctl_table_set *))
  1438. {
  1439. memset(set, 0, sizeof(*set));
  1440. set->is_seen = is_seen;
  1441. init_header(&set->dir.header, root, set, NULL, root_table);
  1442. }
  1443. void retire_sysctl_set(struct ctl_table_set *set)
  1444. {
  1445. WARN_ON(!RB_EMPTY_ROOT(&set->dir.root));
  1446. }
  1447. int __init proc_sys_init(void)
  1448. {
  1449. struct proc_dir_entry *proc_sys_root;
  1450. proc_sys_root = proc_mkdir("sys", NULL);
  1451. proc_sys_root->proc_iops = &proc_sys_dir_operations;
  1452. proc_sys_root->proc_fops = &proc_sys_dir_file_operations;
  1453. proc_sys_root->nlink = 0;
  1454. return sysctl_init();
  1455. }