proc_sysctl.c 41 KB

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