proc_sysctl.c 49 KB

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