proc_sysctl.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700
  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. d_set_d_op(dentry, &proc_sys_dentry_operations);
  483. err = d_splice_alias(inode, dentry);
  484. out:
  485. if (h)
  486. sysctl_head_finish(h);
  487. sysctl_head_finish(head);
  488. return err;
  489. }
  490. static ssize_t proc_sys_call_handler(struct file *filp, void __user *buf,
  491. size_t count, loff_t *ppos, int write)
  492. {
  493. struct inode *inode = file_inode(filp);
  494. struct ctl_table_header *head = grab_header(inode);
  495. struct ctl_table *table = PROC_I(inode)->sysctl_entry;
  496. ssize_t error;
  497. size_t res;
  498. if (IS_ERR(head))
  499. return PTR_ERR(head);
  500. /*
  501. * At this point we know that the sysctl was not unregistered
  502. * and won't be until we finish.
  503. */
  504. error = -EPERM;
  505. if (sysctl_perm(head, table, write ? MAY_WRITE : MAY_READ))
  506. goto out;
  507. /* if that can happen at all, it should be -EINVAL, not -EISDIR */
  508. error = -EINVAL;
  509. if (!table->proc_handler)
  510. goto out;
  511. /* careful: calling conventions are nasty here */
  512. res = count;
  513. error = table->proc_handler(table, write, buf, &res, ppos);
  514. if (!error)
  515. error = res;
  516. out:
  517. sysctl_head_finish(head);
  518. return error;
  519. }
  520. static ssize_t proc_sys_read(struct file *filp, char __user *buf,
  521. size_t count, loff_t *ppos)
  522. {
  523. return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 0);
  524. }
  525. static ssize_t proc_sys_write(struct file *filp, const char __user *buf,
  526. size_t count, loff_t *ppos)
  527. {
  528. return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 1);
  529. }
  530. static int proc_sys_open(struct inode *inode, struct file *filp)
  531. {
  532. struct ctl_table_header *head = grab_header(inode);
  533. struct ctl_table *table = PROC_I(inode)->sysctl_entry;
  534. /* sysctl was unregistered */
  535. if (IS_ERR(head))
  536. return PTR_ERR(head);
  537. if (table->poll)
  538. filp->private_data = proc_sys_poll_event(table->poll);
  539. sysctl_head_finish(head);
  540. return 0;
  541. }
  542. static __poll_t proc_sys_poll(struct file *filp, poll_table *wait)
  543. {
  544. struct inode *inode = file_inode(filp);
  545. struct ctl_table_header *head = grab_header(inode);
  546. struct ctl_table *table = PROC_I(inode)->sysctl_entry;
  547. __poll_t ret = DEFAULT_POLLMASK;
  548. unsigned long event;
  549. /* sysctl was unregistered */
  550. if (IS_ERR(head))
  551. return EPOLLERR | EPOLLHUP;
  552. if (!table->proc_handler)
  553. goto out;
  554. if (!table->poll)
  555. goto out;
  556. event = (unsigned long)filp->private_data;
  557. poll_wait(filp, &table->poll->wait, wait);
  558. if (event != atomic_read(&table->poll->event)) {
  559. filp->private_data = proc_sys_poll_event(table->poll);
  560. ret = EPOLLIN | EPOLLRDNORM | EPOLLERR | EPOLLPRI;
  561. }
  562. out:
  563. sysctl_head_finish(head);
  564. return ret;
  565. }
  566. static bool proc_sys_fill_cache(struct file *file,
  567. struct dir_context *ctx,
  568. struct ctl_table_header *head,
  569. struct ctl_table *table)
  570. {
  571. struct dentry *child, *dir = file->f_path.dentry;
  572. struct inode *inode;
  573. struct qstr qname;
  574. ino_t ino = 0;
  575. unsigned type = DT_UNKNOWN;
  576. qname.name = table->procname;
  577. qname.len = strlen(table->procname);
  578. qname.hash = full_name_hash(dir, qname.name, qname.len);
  579. child = d_lookup(dir, &qname);
  580. if (!child) {
  581. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
  582. child = d_alloc_parallel(dir, &qname, &wq);
  583. if (IS_ERR(child))
  584. return false;
  585. if (d_in_lookup(child)) {
  586. struct dentry *res;
  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. res = d_splice_alias(inode, child);
  595. d_lookup_done(child);
  596. if (unlikely(res)) {
  597. if (IS_ERR(res)) {
  598. dput(child);
  599. return false;
  600. }
  601. dput(child);
  602. child = res;
  603. }
  604. }
  605. }
  606. inode = d_inode(child);
  607. ino = inode->i_ino;
  608. type = inode->i_mode >> 12;
  609. dput(child);
  610. return dir_emit(ctx, qname.name, qname.len, ino, type);
  611. }
  612. static bool proc_sys_link_fill_cache(struct file *file,
  613. struct dir_context *ctx,
  614. struct ctl_table_header *head,
  615. struct ctl_table *table)
  616. {
  617. bool ret = true;
  618. head = sysctl_head_grab(head);
  619. if (IS_ERR(head))
  620. return false;
  621. /* It is not an error if we can not follow the link ignore it */
  622. if (sysctl_follow_link(&head, &table))
  623. goto out;
  624. ret = proc_sys_fill_cache(file, ctx, head, table);
  625. out:
  626. sysctl_head_finish(head);
  627. return ret;
  628. }
  629. static int scan(struct ctl_table_header *head, struct ctl_table *table,
  630. unsigned long *pos, struct file *file,
  631. struct dir_context *ctx)
  632. {
  633. bool res;
  634. if ((*pos)++ < ctx->pos)
  635. return true;
  636. if (unlikely(S_ISLNK(table->mode)))
  637. res = proc_sys_link_fill_cache(file, ctx, head, table);
  638. else
  639. res = proc_sys_fill_cache(file, ctx, head, table);
  640. if (res)
  641. ctx->pos = *pos;
  642. return res;
  643. }
  644. static int proc_sys_readdir(struct file *file, struct dir_context *ctx)
  645. {
  646. struct ctl_table_header *head = grab_header(file_inode(file));
  647. struct ctl_table_header *h = NULL;
  648. struct ctl_table *entry;
  649. struct ctl_dir *ctl_dir;
  650. unsigned long pos;
  651. if (IS_ERR(head))
  652. return PTR_ERR(head);
  653. ctl_dir = container_of(head, struct ctl_dir, header);
  654. if (!dir_emit_dots(file, ctx))
  655. goto out;
  656. pos = 2;
  657. for (first_entry(ctl_dir, &h, &entry); h; next_entry(&h, &entry)) {
  658. if (!scan(h, entry, &pos, file, ctx)) {
  659. sysctl_head_finish(h);
  660. break;
  661. }
  662. }
  663. out:
  664. sysctl_head_finish(head);
  665. return 0;
  666. }
  667. static int proc_sys_permission(struct inode *inode, int mask)
  668. {
  669. /*
  670. * sysctl entries that are not writeable,
  671. * are _NOT_ writeable, capabilities or not.
  672. */
  673. struct ctl_table_header *head;
  674. struct ctl_table *table;
  675. int error;
  676. /* Executable files are not allowed under /proc/sys/ */
  677. if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))
  678. return -EACCES;
  679. head = grab_header(inode);
  680. if (IS_ERR(head))
  681. return PTR_ERR(head);
  682. table = PROC_I(inode)->sysctl_entry;
  683. if (!table) /* global root - r-xr-xr-x */
  684. error = mask & MAY_WRITE ? -EACCES : 0;
  685. else /* Use the permissions on the sysctl table entry */
  686. error = sysctl_perm(head, table, mask & ~MAY_NOT_BLOCK);
  687. sysctl_head_finish(head);
  688. return error;
  689. }
  690. static int proc_sys_setattr(struct dentry *dentry, struct iattr *attr)
  691. {
  692. struct inode *inode = d_inode(dentry);
  693. int error;
  694. if (attr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
  695. return -EPERM;
  696. error = setattr_prepare(dentry, attr);
  697. if (error)
  698. return error;
  699. setattr_copy(inode, attr);
  700. mark_inode_dirty(inode);
  701. return 0;
  702. }
  703. static int proc_sys_getattr(const struct path *path, struct kstat *stat,
  704. u32 request_mask, unsigned int query_flags)
  705. {
  706. struct inode *inode = d_inode(path->dentry);
  707. struct ctl_table_header *head = grab_header(inode);
  708. struct ctl_table *table = PROC_I(inode)->sysctl_entry;
  709. if (IS_ERR(head))
  710. return PTR_ERR(head);
  711. generic_fillattr(inode, stat);
  712. if (table)
  713. stat->mode = (stat->mode & S_IFMT) | table->mode;
  714. sysctl_head_finish(head);
  715. return 0;
  716. }
  717. static const struct file_operations proc_sys_file_operations = {
  718. .open = proc_sys_open,
  719. .poll = proc_sys_poll,
  720. .read = proc_sys_read,
  721. .write = proc_sys_write,
  722. .llseek = default_llseek,
  723. };
  724. static const struct file_operations proc_sys_dir_file_operations = {
  725. .read = generic_read_dir,
  726. .iterate_shared = proc_sys_readdir,
  727. .llseek = generic_file_llseek,
  728. };
  729. static const struct inode_operations proc_sys_inode_operations = {
  730. .permission = proc_sys_permission,
  731. .setattr = proc_sys_setattr,
  732. .getattr = proc_sys_getattr,
  733. };
  734. static const struct inode_operations proc_sys_dir_operations = {
  735. .lookup = proc_sys_lookup,
  736. .permission = proc_sys_permission,
  737. .setattr = proc_sys_setattr,
  738. .getattr = proc_sys_getattr,
  739. };
  740. static int proc_sys_revalidate(struct dentry *dentry, unsigned int flags)
  741. {
  742. if (flags & LOOKUP_RCU)
  743. return -ECHILD;
  744. return !PROC_I(d_inode(dentry))->sysctl->unregistering;
  745. }
  746. static int proc_sys_delete(const struct dentry *dentry)
  747. {
  748. return !!PROC_I(d_inode(dentry))->sysctl->unregistering;
  749. }
  750. static int sysctl_is_seen(struct ctl_table_header *p)
  751. {
  752. struct ctl_table_set *set = p->set;
  753. int res;
  754. spin_lock(&sysctl_lock);
  755. if (p->unregistering)
  756. res = 0;
  757. else if (!set->is_seen)
  758. res = 1;
  759. else
  760. res = set->is_seen(set);
  761. spin_unlock(&sysctl_lock);
  762. return res;
  763. }
  764. static int proc_sys_compare(const struct dentry *dentry,
  765. unsigned int len, const char *str, const struct qstr *name)
  766. {
  767. struct ctl_table_header *head;
  768. struct inode *inode;
  769. /* Although proc doesn't have negative dentries, rcu-walk means
  770. * that inode here can be NULL */
  771. /* AV: can it, indeed? */
  772. inode = d_inode_rcu(dentry);
  773. if (!inode)
  774. return 1;
  775. if (name->len != len)
  776. return 1;
  777. if (memcmp(name->name, str, len))
  778. return 1;
  779. head = rcu_dereference(PROC_I(inode)->sysctl);
  780. return !head || !sysctl_is_seen(head);
  781. }
  782. static const struct dentry_operations proc_sys_dentry_operations = {
  783. .d_revalidate = proc_sys_revalidate,
  784. .d_delete = proc_sys_delete,
  785. .d_compare = proc_sys_compare,
  786. };
  787. static struct ctl_dir *find_subdir(struct ctl_dir *dir,
  788. const char *name, int namelen)
  789. {
  790. struct ctl_table_header *head;
  791. struct ctl_table *entry;
  792. entry = find_entry(&head, dir, name, namelen);
  793. if (!entry)
  794. return ERR_PTR(-ENOENT);
  795. if (!S_ISDIR(entry->mode))
  796. return ERR_PTR(-ENOTDIR);
  797. return container_of(head, struct ctl_dir, header);
  798. }
  799. static struct ctl_dir *new_dir(struct ctl_table_set *set,
  800. const char *name, int namelen)
  801. {
  802. struct ctl_table *table;
  803. struct ctl_dir *new;
  804. struct ctl_node *node;
  805. char *new_name;
  806. new = kzalloc(sizeof(*new) + sizeof(struct ctl_node) +
  807. sizeof(struct ctl_table)*2 + namelen + 1,
  808. GFP_KERNEL);
  809. if (!new)
  810. return NULL;
  811. node = (struct ctl_node *)(new + 1);
  812. table = (struct ctl_table *)(node + 1);
  813. new_name = (char *)(table + 2);
  814. memcpy(new_name, name, namelen);
  815. new_name[namelen] = '\0';
  816. table[0].procname = new_name;
  817. table[0].mode = S_IFDIR|S_IRUGO|S_IXUGO;
  818. init_header(&new->header, set->dir.header.root, set, node, table);
  819. return new;
  820. }
  821. /**
  822. * get_subdir - find or create a subdir with the specified name.
  823. * @dir: Directory to create the subdirectory in
  824. * @name: The name of the subdirectory to find or create
  825. * @namelen: The length of name
  826. *
  827. * Takes a directory with an elevated reference count so we know that
  828. * if we drop the lock the directory will not go away. Upon success
  829. * the reference is moved from @dir to the returned subdirectory.
  830. * Upon error an error code is returned and the reference on @dir is
  831. * simply dropped.
  832. */
  833. static struct ctl_dir *get_subdir(struct ctl_dir *dir,
  834. const char *name, int namelen)
  835. {
  836. struct ctl_table_set *set = dir->header.set;
  837. struct ctl_dir *subdir, *new = NULL;
  838. int err;
  839. spin_lock(&sysctl_lock);
  840. subdir = find_subdir(dir, name, namelen);
  841. if (!IS_ERR(subdir))
  842. goto found;
  843. if (PTR_ERR(subdir) != -ENOENT)
  844. goto failed;
  845. spin_unlock(&sysctl_lock);
  846. new = new_dir(set, name, namelen);
  847. spin_lock(&sysctl_lock);
  848. subdir = ERR_PTR(-ENOMEM);
  849. if (!new)
  850. goto failed;
  851. /* Was the subdir added while we dropped the lock? */
  852. subdir = find_subdir(dir, name, namelen);
  853. if (!IS_ERR(subdir))
  854. goto found;
  855. if (PTR_ERR(subdir) != -ENOENT)
  856. goto failed;
  857. /* Nope. Use the our freshly made directory entry. */
  858. err = insert_header(dir, &new->header);
  859. subdir = ERR_PTR(err);
  860. if (err)
  861. goto failed;
  862. subdir = new;
  863. found:
  864. subdir->header.nreg++;
  865. failed:
  866. if (IS_ERR(subdir)) {
  867. pr_err("sysctl could not get directory: ");
  868. sysctl_print_dir(dir);
  869. pr_cont("/%*.*s %ld\n",
  870. namelen, namelen, name, PTR_ERR(subdir));
  871. }
  872. drop_sysctl_table(&dir->header);
  873. if (new)
  874. drop_sysctl_table(&new->header);
  875. spin_unlock(&sysctl_lock);
  876. return subdir;
  877. }
  878. static struct ctl_dir *xlate_dir(struct ctl_table_set *set, struct ctl_dir *dir)
  879. {
  880. struct ctl_dir *parent;
  881. const char *procname;
  882. if (!dir->header.parent)
  883. return &set->dir;
  884. parent = xlate_dir(set, dir->header.parent);
  885. if (IS_ERR(parent))
  886. return parent;
  887. procname = dir->header.ctl_table[0].procname;
  888. return find_subdir(parent, procname, strlen(procname));
  889. }
  890. static int sysctl_follow_link(struct ctl_table_header **phead,
  891. struct ctl_table **pentry)
  892. {
  893. struct ctl_table_header *head;
  894. struct ctl_table_root *root;
  895. struct ctl_table_set *set;
  896. struct ctl_table *entry;
  897. struct ctl_dir *dir;
  898. int ret;
  899. ret = 0;
  900. spin_lock(&sysctl_lock);
  901. root = (*pentry)->data;
  902. set = lookup_header_set(root);
  903. dir = xlate_dir(set, (*phead)->parent);
  904. if (IS_ERR(dir))
  905. ret = PTR_ERR(dir);
  906. else {
  907. const char *procname = (*pentry)->procname;
  908. head = NULL;
  909. entry = find_entry(&head, dir, procname, strlen(procname));
  910. ret = -ENOENT;
  911. if (entry && use_table(head)) {
  912. unuse_table(*phead);
  913. *phead = head;
  914. *pentry = entry;
  915. ret = 0;
  916. }
  917. }
  918. spin_unlock(&sysctl_lock);
  919. return ret;
  920. }
  921. static int sysctl_err(const char *path, struct ctl_table *table, char *fmt, ...)
  922. {
  923. struct va_format vaf;
  924. va_list args;
  925. va_start(args, fmt);
  926. vaf.fmt = fmt;
  927. vaf.va = &args;
  928. pr_err("sysctl table check failed: %s/%s %pV\n",
  929. path, table->procname, &vaf);
  930. va_end(args);
  931. return -EINVAL;
  932. }
  933. static int sysctl_check_table_array(const char *path, struct ctl_table *table)
  934. {
  935. int err = 0;
  936. if ((table->proc_handler == proc_douintvec) ||
  937. (table->proc_handler == proc_douintvec_minmax)) {
  938. if (table->maxlen != sizeof(unsigned int))
  939. err |= sysctl_err(path, table, "array not allowed");
  940. }
  941. return err;
  942. }
  943. static int sysctl_check_table(const char *path, struct ctl_table *table)
  944. {
  945. int err = 0;
  946. for (; table->procname; table++) {
  947. if (table->child)
  948. err |= sysctl_err(path, table, "Not a file");
  949. if ((table->proc_handler == proc_dostring) ||
  950. (table->proc_handler == proc_dointvec) ||
  951. (table->proc_handler == proc_douintvec) ||
  952. (table->proc_handler == proc_douintvec_minmax) ||
  953. (table->proc_handler == proc_dointvec_minmax) ||
  954. (table->proc_handler == proc_dointvec_jiffies) ||
  955. (table->proc_handler == proc_dointvec_userhz_jiffies) ||
  956. (table->proc_handler == proc_dointvec_ms_jiffies) ||
  957. (table->proc_handler == proc_doulongvec_minmax) ||
  958. (table->proc_handler == proc_doulongvec_ms_jiffies_minmax)) {
  959. if (!table->data)
  960. err |= sysctl_err(path, table, "No data");
  961. if (!table->maxlen)
  962. err |= sysctl_err(path, table, "No maxlen");
  963. else
  964. err |= sysctl_check_table_array(path, table);
  965. }
  966. if (!table->proc_handler)
  967. err |= sysctl_err(path, table, "No proc_handler");
  968. if ((table->mode & (S_IRUGO|S_IWUGO)) != table->mode)
  969. err |= sysctl_err(path, table, "bogus .mode 0%o",
  970. table->mode);
  971. }
  972. return err;
  973. }
  974. static struct ctl_table_header *new_links(struct ctl_dir *dir, struct ctl_table *table,
  975. struct ctl_table_root *link_root)
  976. {
  977. struct ctl_table *link_table, *entry, *link;
  978. struct ctl_table_header *links;
  979. struct ctl_node *node;
  980. char *link_name;
  981. int nr_entries, name_bytes;
  982. name_bytes = 0;
  983. nr_entries = 0;
  984. for (entry = table; entry->procname; entry++) {
  985. nr_entries++;
  986. name_bytes += strlen(entry->procname) + 1;
  987. }
  988. links = kzalloc(sizeof(struct ctl_table_header) +
  989. sizeof(struct ctl_node)*nr_entries +
  990. sizeof(struct ctl_table)*(nr_entries + 1) +
  991. name_bytes,
  992. GFP_KERNEL);
  993. if (!links)
  994. return NULL;
  995. node = (struct ctl_node *)(links + 1);
  996. link_table = (struct ctl_table *)(node + nr_entries);
  997. link_name = (char *)&link_table[nr_entries + 1];
  998. for (link = link_table, entry = table; entry->procname; link++, entry++) {
  999. int len = strlen(entry->procname) + 1;
  1000. memcpy(link_name, entry->procname, len);
  1001. link->procname = link_name;
  1002. link->mode = S_IFLNK|S_IRWXUGO;
  1003. link->data = link_root;
  1004. link_name += len;
  1005. }
  1006. init_header(links, dir->header.root, dir->header.set, node, link_table);
  1007. links->nreg = nr_entries;
  1008. return links;
  1009. }
  1010. static bool get_links(struct ctl_dir *dir,
  1011. struct ctl_table *table, struct ctl_table_root *link_root)
  1012. {
  1013. struct ctl_table_header *head;
  1014. struct ctl_table *entry, *link;
  1015. /* Are there links available for every entry in table? */
  1016. for (entry = table; entry->procname; entry++) {
  1017. const char *procname = entry->procname;
  1018. link = find_entry(&head, dir, procname, strlen(procname));
  1019. if (!link)
  1020. return false;
  1021. if (S_ISDIR(link->mode) && S_ISDIR(entry->mode))
  1022. continue;
  1023. if (S_ISLNK(link->mode) && (link->data == link_root))
  1024. continue;
  1025. return false;
  1026. }
  1027. /* The checks passed. Increase the registration count on the links */
  1028. for (entry = table; entry->procname; entry++) {
  1029. const char *procname = entry->procname;
  1030. link = find_entry(&head, dir, procname, strlen(procname));
  1031. head->nreg++;
  1032. }
  1033. return true;
  1034. }
  1035. static int insert_links(struct ctl_table_header *head)
  1036. {
  1037. struct ctl_table_set *root_set = &sysctl_table_root.default_set;
  1038. struct ctl_dir *core_parent = NULL;
  1039. struct ctl_table_header *links;
  1040. int err;
  1041. if (head->set == root_set)
  1042. return 0;
  1043. core_parent = xlate_dir(root_set, head->parent);
  1044. if (IS_ERR(core_parent))
  1045. return 0;
  1046. if (get_links(core_parent, head->ctl_table, head->root))
  1047. return 0;
  1048. core_parent->header.nreg++;
  1049. spin_unlock(&sysctl_lock);
  1050. links = new_links(core_parent, head->ctl_table, head->root);
  1051. spin_lock(&sysctl_lock);
  1052. err = -ENOMEM;
  1053. if (!links)
  1054. goto out;
  1055. err = 0;
  1056. if (get_links(core_parent, head->ctl_table, head->root)) {
  1057. kfree(links);
  1058. goto out;
  1059. }
  1060. err = insert_header(core_parent, links);
  1061. if (err)
  1062. kfree(links);
  1063. out:
  1064. drop_sysctl_table(&core_parent->header);
  1065. return err;
  1066. }
  1067. /**
  1068. * __register_sysctl_table - register a leaf sysctl table
  1069. * @set: Sysctl tree to register on
  1070. * @path: The path to the directory the sysctl table is in.
  1071. * @table: the top-level table structure
  1072. *
  1073. * Register a sysctl table hierarchy. @table should be a filled in ctl_table
  1074. * array. A completely 0 filled entry terminates the table.
  1075. *
  1076. * The members of the &struct ctl_table structure are used as follows:
  1077. *
  1078. * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not
  1079. * enter a sysctl file
  1080. *
  1081. * data - a pointer to data for use by proc_handler
  1082. *
  1083. * maxlen - the maximum size in bytes of the data
  1084. *
  1085. * mode - the file permissions for the /proc/sys file
  1086. *
  1087. * child - must be %NULL.
  1088. *
  1089. * proc_handler - the text handler routine (described below)
  1090. *
  1091. * extra1, extra2 - extra pointers usable by the proc handler routines
  1092. *
  1093. * Leaf nodes in the sysctl tree will be represented by a single file
  1094. * under /proc; non-leaf nodes will be represented by directories.
  1095. *
  1096. * There must be a proc_handler routine for any terminal nodes.
  1097. * Several default handlers are available to cover common cases -
  1098. *
  1099. * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(),
  1100. * proc_dointvec_userhz_jiffies(), proc_dointvec_minmax(),
  1101. * proc_doulongvec_ms_jiffies_minmax(), proc_doulongvec_minmax()
  1102. *
  1103. * It is the handler's job to read the input buffer from user memory
  1104. * and process it. The handler should return 0 on success.
  1105. *
  1106. * This routine returns %NULL on a failure to register, and a pointer
  1107. * to the table header on success.
  1108. */
  1109. struct ctl_table_header *__register_sysctl_table(
  1110. struct ctl_table_set *set,
  1111. const char *path, struct ctl_table *table)
  1112. {
  1113. struct ctl_table_root *root = set->dir.header.root;
  1114. struct ctl_table_header *header;
  1115. const char *name, *nextname;
  1116. struct ctl_dir *dir;
  1117. struct ctl_table *entry;
  1118. struct ctl_node *node;
  1119. int nr_entries = 0;
  1120. for (entry = table; entry->procname; entry++)
  1121. nr_entries++;
  1122. header = kzalloc(sizeof(struct ctl_table_header) +
  1123. sizeof(struct ctl_node)*nr_entries, GFP_KERNEL);
  1124. if (!header)
  1125. return NULL;
  1126. node = (struct ctl_node *)(header + 1);
  1127. init_header(header, root, set, node, table);
  1128. if (sysctl_check_table(path, table))
  1129. goto fail;
  1130. spin_lock(&sysctl_lock);
  1131. dir = &set->dir;
  1132. /* Reference moved down the diretory tree get_subdir */
  1133. dir->header.nreg++;
  1134. spin_unlock(&sysctl_lock);
  1135. /* Find the directory for the ctl_table */
  1136. for (name = path; name; name = nextname) {
  1137. int namelen;
  1138. nextname = strchr(name, '/');
  1139. if (nextname) {
  1140. namelen = nextname - name;
  1141. nextname++;
  1142. } else {
  1143. namelen = strlen(name);
  1144. }
  1145. if (namelen == 0)
  1146. continue;
  1147. dir = get_subdir(dir, name, namelen);
  1148. if (IS_ERR(dir))
  1149. goto fail;
  1150. }
  1151. spin_lock(&sysctl_lock);
  1152. if (insert_header(dir, header))
  1153. goto fail_put_dir_locked;
  1154. drop_sysctl_table(&dir->header);
  1155. spin_unlock(&sysctl_lock);
  1156. return header;
  1157. fail_put_dir_locked:
  1158. drop_sysctl_table(&dir->header);
  1159. spin_unlock(&sysctl_lock);
  1160. fail:
  1161. kfree(header);
  1162. dump_stack();
  1163. return NULL;
  1164. }
  1165. /**
  1166. * register_sysctl - register a sysctl table
  1167. * @path: The path to the directory the sysctl table is in.
  1168. * @table: the table structure
  1169. *
  1170. * Register a sysctl table. @table should be a filled in ctl_table
  1171. * array. A completely 0 filled entry terminates the table.
  1172. *
  1173. * See __register_sysctl_table for more details.
  1174. */
  1175. struct ctl_table_header *register_sysctl(const char *path, struct ctl_table *table)
  1176. {
  1177. return __register_sysctl_table(&sysctl_table_root.default_set,
  1178. path, table);
  1179. }
  1180. EXPORT_SYMBOL(register_sysctl);
  1181. static char *append_path(const char *path, char *pos, const char *name)
  1182. {
  1183. int namelen;
  1184. namelen = strlen(name);
  1185. if (((pos - path) + namelen + 2) >= PATH_MAX)
  1186. return NULL;
  1187. memcpy(pos, name, namelen);
  1188. pos[namelen] = '/';
  1189. pos[namelen + 1] = '\0';
  1190. pos += namelen + 1;
  1191. return pos;
  1192. }
  1193. static int count_subheaders(struct ctl_table *table)
  1194. {
  1195. int has_files = 0;
  1196. int nr_subheaders = 0;
  1197. struct ctl_table *entry;
  1198. /* special case: no directory and empty directory */
  1199. if (!table || !table->procname)
  1200. return 1;
  1201. for (entry = table; entry->procname; entry++) {
  1202. if (entry->child)
  1203. nr_subheaders += count_subheaders(entry->child);
  1204. else
  1205. has_files = 1;
  1206. }
  1207. return nr_subheaders + has_files;
  1208. }
  1209. static int register_leaf_sysctl_tables(const char *path, char *pos,
  1210. struct ctl_table_header ***subheader, struct ctl_table_set *set,
  1211. struct ctl_table *table)
  1212. {
  1213. struct ctl_table *ctl_table_arg = NULL;
  1214. struct ctl_table *entry, *files;
  1215. int nr_files = 0;
  1216. int nr_dirs = 0;
  1217. int err = -ENOMEM;
  1218. for (entry = table; entry->procname; entry++) {
  1219. if (entry->child)
  1220. nr_dirs++;
  1221. else
  1222. nr_files++;
  1223. }
  1224. files = table;
  1225. /* If there are mixed files and directories we need a new table */
  1226. if (nr_dirs && nr_files) {
  1227. struct ctl_table *new;
  1228. files = kcalloc(nr_files + 1, sizeof(struct ctl_table),
  1229. GFP_KERNEL);
  1230. if (!files)
  1231. goto out;
  1232. ctl_table_arg = files;
  1233. for (new = files, entry = table; entry->procname; entry++) {
  1234. if (entry->child)
  1235. continue;
  1236. *new = *entry;
  1237. new++;
  1238. }
  1239. }
  1240. /* Register everything except a directory full of subdirectories */
  1241. if (nr_files || !nr_dirs) {
  1242. struct ctl_table_header *header;
  1243. header = __register_sysctl_table(set, path, files);
  1244. if (!header) {
  1245. kfree(ctl_table_arg);
  1246. goto out;
  1247. }
  1248. /* Remember if we need to free the file table */
  1249. header->ctl_table_arg = ctl_table_arg;
  1250. **subheader = header;
  1251. (*subheader)++;
  1252. }
  1253. /* Recurse into the subdirectories. */
  1254. for (entry = table; entry->procname; entry++) {
  1255. char *child_pos;
  1256. if (!entry->child)
  1257. continue;
  1258. err = -ENAMETOOLONG;
  1259. child_pos = append_path(path, pos, entry->procname);
  1260. if (!child_pos)
  1261. goto out;
  1262. err = register_leaf_sysctl_tables(path, child_pos, subheader,
  1263. set, entry->child);
  1264. pos[0] = '\0';
  1265. if (err)
  1266. goto out;
  1267. }
  1268. err = 0;
  1269. out:
  1270. /* On failure our caller will unregister all registered subheaders */
  1271. return err;
  1272. }
  1273. /**
  1274. * __register_sysctl_paths - register a sysctl table hierarchy
  1275. * @set: Sysctl tree to register on
  1276. * @path: The path to the directory the sysctl table is in.
  1277. * @table: the top-level table structure
  1278. *
  1279. * Register a sysctl table hierarchy. @table should be a filled in ctl_table
  1280. * array. A completely 0 filled entry terminates the table.
  1281. *
  1282. * See __register_sysctl_table for more details.
  1283. */
  1284. struct ctl_table_header *__register_sysctl_paths(
  1285. struct ctl_table_set *set,
  1286. const struct ctl_path *path, struct ctl_table *table)
  1287. {
  1288. struct ctl_table *ctl_table_arg = table;
  1289. int nr_subheaders = count_subheaders(table);
  1290. struct ctl_table_header *header = NULL, **subheaders, **subheader;
  1291. const struct ctl_path *component;
  1292. char *new_path, *pos;
  1293. pos = new_path = kmalloc(PATH_MAX, GFP_KERNEL);
  1294. if (!new_path)
  1295. return NULL;
  1296. pos[0] = '\0';
  1297. for (component = path; component->procname; component++) {
  1298. pos = append_path(new_path, pos, component->procname);
  1299. if (!pos)
  1300. goto out;
  1301. }
  1302. while (table->procname && table->child && !table[1].procname) {
  1303. pos = append_path(new_path, pos, table->procname);
  1304. if (!pos)
  1305. goto out;
  1306. table = table->child;
  1307. }
  1308. if (nr_subheaders == 1) {
  1309. header = __register_sysctl_table(set, new_path, table);
  1310. if (header)
  1311. header->ctl_table_arg = ctl_table_arg;
  1312. } else {
  1313. header = kzalloc(sizeof(*header) +
  1314. sizeof(*subheaders)*nr_subheaders, GFP_KERNEL);
  1315. if (!header)
  1316. goto out;
  1317. subheaders = (struct ctl_table_header **) (header + 1);
  1318. subheader = subheaders;
  1319. header->ctl_table_arg = ctl_table_arg;
  1320. if (register_leaf_sysctl_tables(new_path, pos, &subheader,
  1321. set, table))
  1322. goto err_register_leaves;
  1323. }
  1324. out:
  1325. kfree(new_path);
  1326. return header;
  1327. err_register_leaves:
  1328. while (subheader > subheaders) {
  1329. struct ctl_table_header *subh = *(--subheader);
  1330. struct ctl_table *table = subh->ctl_table_arg;
  1331. unregister_sysctl_table(subh);
  1332. kfree(table);
  1333. }
  1334. kfree(header);
  1335. header = NULL;
  1336. goto out;
  1337. }
  1338. /**
  1339. * register_sysctl_table_path - register a sysctl table hierarchy
  1340. * @path: The path to the directory the sysctl table is in.
  1341. * @table: the top-level table structure
  1342. *
  1343. * Register a sysctl table hierarchy. @table should be a filled in ctl_table
  1344. * array. A completely 0 filled entry terminates the table.
  1345. *
  1346. * See __register_sysctl_paths for more details.
  1347. */
  1348. struct ctl_table_header *register_sysctl_paths(const struct ctl_path *path,
  1349. struct ctl_table *table)
  1350. {
  1351. return __register_sysctl_paths(&sysctl_table_root.default_set,
  1352. path, table);
  1353. }
  1354. EXPORT_SYMBOL(register_sysctl_paths);
  1355. /**
  1356. * register_sysctl_table - register a sysctl table hierarchy
  1357. * @table: the top-level table structure
  1358. *
  1359. * Register a sysctl table hierarchy. @table should be a filled in ctl_table
  1360. * array. A completely 0 filled entry terminates the table.
  1361. *
  1362. * See register_sysctl_paths for more details.
  1363. */
  1364. struct ctl_table_header *register_sysctl_table(struct ctl_table *table)
  1365. {
  1366. static const struct ctl_path null_path[] = { {} };
  1367. return register_sysctl_paths(null_path, table);
  1368. }
  1369. EXPORT_SYMBOL(register_sysctl_table);
  1370. static void put_links(struct ctl_table_header *header)
  1371. {
  1372. struct ctl_table_set *root_set = &sysctl_table_root.default_set;
  1373. struct ctl_table_root *root = header->root;
  1374. struct ctl_dir *parent = header->parent;
  1375. struct ctl_dir *core_parent;
  1376. struct ctl_table *entry;
  1377. if (header->set == root_set)
  1378. return;
  1379. core_parent = xlate_dir(root_set, parent);
  1380. if (IS_ERR(core_parent))
  1381. return;
  1382. for (entry = header->ctl_table; entry->procname; entry++) {
  1383. struct ctl_table_header *link_head;
  1384. struct ctl_table *link;
  1385. const char *name = entry->procname;
  1386. link = find_entry(&link_head, core_parent, name, strlen(name));
  1387. if (link &&
  1388. ((S_ISDIR(link->mode) && S_ISDIR(entry->mode)) ||
  1389. (S_ISLNK(link->mode) && (link->data == root)))) {
  1390. drop_sysctl_table(link_head);
  1391. }
  1392. else {
  1393. pr_err("sysctl link missing during unregister: ");
  1394. sysctl_print_dir(parent);
  1395. pr_cont("/%s\n", name);
  1396. }
  1397. }
  1398. }
  1399. static void drop_sysctl_table(struct ctl_table_header *header)
  1400. {
  1401. struct ctl_dir *parent = header->parent;
  1402. if (--header->nreg)
  1403. return;
  1404. put_links(header);
  1405. start_unregistering(header);
  1406. if (!--header->count)
  1407. kfree_rcu(header, rcu);
  1408. if (parent)
  1409. drop_sysctl_table(&parent->header);
  1410. }
  1411. /**
  1412. * unregister_sysctl_table - unregister a sysctl table hierarchy
  1413. * @header: the header returned from register_sysctl_table
  1414. *
  1415. * Unregisters the sysctl table and all children. proc entries may not
  1416. * actually be removed until they are no longer used by anyone.
  1417. */
  1418. void unregister_sysctl_table(struct ctl_table_header * header)
  1419. {
  1420. int nr_subheaders;
  1421. might_sleep();
  1422. if (header == NULL)
  1423. return;
  1424. nr_subheaders = count_subheaders(header->ctl_table_arg);
  1425. if (unlikely(nr_subheaders > 1)) {
  1426. struct ctl_table_header **subheaders;
  1427. int i;
  1428. subheaders = (struct ctl_table_header **)(header + 1);
  1429. for (i = nr_subheaders -1; i >= 0; i--) {
  1430. struct ctl_table_header *subh = subheaders[i];
  1431. struct ctl_table *table = subh->ctl_table_arg;
  1432. unregister_sysctl_table(subh);
  1433. kfree(table);
  1434. }
  1435. kfree(header);
  1436. return;
  1437. }
  1438. spin_lock(&sysctl_lock);
  1439. drop_sysctl_table(header);
  1440. spin_unlock(&sysctl_lock);
  1441. }
  1442. EXPORT_SYMBOL(unregister_sysctl_table);
  1443. void setup_sysctl_set(struct ctl_table_set *set,
  1444. struct ctl_table_root *root,
  1445. int (*is_seen)(struct ctl_table_set *))
  1446. {
  1447. memset(set, 0, sizeof(*set));
  1448. set->is_seen = is_seen;
  1449. init_header(&set->dir.header, root, set, NULL, root_table);
  1450. }
  1451. void retire_sysctl_set(struct ctl_table_set *set)
  1452. {
  1453. WARN_ON(!RB_EMPTY_ROOT(&set->dir.root));
  1454. }
  1455. int __init proc_sys_init(void)
  1456. {
  1457. struct proc_dir_entry *proc_sys_root;
  1458. proc_sys_root = proc_mkdir("sys", NULL);
  1459. proc_sys_root->proc_iops = &proc_sys_dir_operations;
  1460. proc_sys_root->proc_fops = &proc_sys_dir_file_operations;
  1461. proc_sys_root->nlink = 0;
  1462. return sysctl_init();
  1463. }