proc_sysctl.c 44 KB

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