diff options
Diffstat (limited to 'kernel/sysctl.c')
| -rw-r--r-- | kernel/sysctl.c | 170 |
1 files changed, 152 insertions, 18 deletions
diff --git a/kernel/sysctl.c b/kernel/sysctl.c index 1a8299d1fe59..911d846f0503 100644 --- a/kernel/sysctl.c +++ b/kernel/sysctl.c | |||
| @@ -160,12 +160,13 @@ static struct ctl_table root_table[]; | |||
| 160 | static struct ctl_table_root sysctl_table_root; | 160 | static struct ctl_table_root sysctl_table_root; |
| 161 | static struct ctl_table_header root_table_header = { | 161 | static struct ctl_table_header root_table_header = { |
| 162 | .ctl_table = root_table, | 162 | .ctl_table = root_table, |
| 163 | .ctl_entry = LIST_HEAD_INIT(sysctl_table_root.header_list), | 163 | .ctl_entry = LIST_HEAD_INIT(sysctl_table_root.default_set.list), |
| 164 | .root = &sysctl_table_root, | 164 | .root = &sysctl_table_root, |
| 165 | .set = &sysctl_table_root.default_set, | ||
| 165 | }; | 166 | }; |
| 166 | static struct ctl_table_root sysctl_table_root = { | 167 | static struct ctl_table_root sysctl_table_root = { |
| 167 | .root_list = LIST_HEAD_INIT(sysctl_table_root.root_list), | 168 | .root_list = LIST_HEAD_INIT(sysctl_table_root.root_list), |
| 168 | .header_list = LIST_HEAD_INIT(root_table_header.ctl_entry), | 169 | .default_set.list = LIST_HEAD_INIT(root_table_header.ctl_entry), |
| 169 | }; | 170 | }; |
| 170 | 171 | ||
| 171 | static struct ctl_table kern_table[]; | 172 | static struct ctl_table kern_table[]; |
| @@ -624,7 +625,7 @@ static struct ctl_table kern_table[] = { | |||
| 624 | { | 625 | { |
| 625 | .ctl_name = KERN_PRINTK_RATELIMIT, | 626 | .ctl_name = KERN_PRINTK_RATELIMIT, |
| 626 | .procname = "printk_ratelimit", | 627 | .procname = "printk_ratelimit", |
| 627 | .data = &printk_ratelimit_jiffies, | 628 | .data = &printk_ratelimit_state.interval, |
| 628 | .maxlen = sizeof(int), | 629 | .maxlen = sizeof(int), |
| 629 | .mode = 0644, | 630 | .mode = 0644, |
| 630 | .proc_handler = &proc_dointvec_jiffies, | 631 | .proc_handler = &proc_dointvec_jiffies, |
| @@ -633,7 +634,7 @@ static struct ctl_table kern_table[] = { | |||
| 633 | { | 634 | { |
| 634 | .ctl_name = KERN_PRINTK_RATELIMIT_BURST, | 635 | .ctl_name = KERN_PRINTK_RATELIMIT_BURST, |
| 635 | .procname = "printk_ratelimit_burst", | 636 | .procname = "printk_ratelimit_burst", |
| 636 | .data = &printk_ratelimit_burst, | 637 | .data = &printk_ratelimit_state.burst, |
| 637 | .maxlen = sizeof(int), | 638 | .maxlen = sizeof(int), |
| 638 | .mode = 0644, | 639 | .mode = 0644, |
| 639 | .proc_handler = &proc_dointvec, | 640 | .proc_handler = &proc_dointvec, |
| @@ -1386,6 +1387,9 @@ static void start_unregistering(struct ctl_table_header *p) | |||
| 1386 | spin_unlock(&sysctl_lock); | 1387 | spin_unlock(&sysctl_lock); |
| 1387 | wait_for_completion(&wait); | 1388 | wait_for_completion(&wait); |
| 1388 | spin_lock(&sysctl_lock); | 1389 | spin_lock(&sysctl_lock); |
| 1390 | } else { | ||
| 1391 | /* anything non-NULL; we'll never dereference it */ | ||
| 1392 | p->unregistering = ERR_PTR(-EINVAL); | ||
| 1389 | } | 1393 | } |
| 1390 | /* | 1394 | /* |
| 1391 | * do not remove from the list until nobody holds it; walking the | 1395 | * do not remove from the list until nobody holds it; walking the |
| @@ -1394,6 +1398,32 @@ static void start_unregistering(struct ctl_table_header *p) | |||
| 1394 | list_del_init(&p->ctl_entry); | 1398 | list_del_init(&p->ctl_entry); |
| 1395 | } | 1399 | } |
| 1396 | 1400 | ||
| 1401 | void sysctl_head_get(struct ctl_table_header *head) | ||
| 1402 | { | ||
| 1403 | spin_lock(&sysctl_lock); | ||
| 1404 | head->count++; | ||
| 1405 | spin_unlock(&sysctl_lock); | ||
| 1406 | } | ||
| 1407 | |||
| 1408 | void sysctl_head_put(struct ctl_table_header *head) | ||
| 1409 | { | ||
| 1410 | spin_lock(&sysctl_lock); | ||
| 1411 | if (!--head->count) | ||
| 1412 | kfree(head); | ||
| 1413 | spin_unlock(&sysctl_lock); | ||
| 1414 | } | ||
| 1415 | |||
| 1416 | struct ctl_table_header *sysctl_head_grab(struct ctl_table_header *head) | ||
| 1417 | { | ||
| 1418 | if (!head) | ||
| 1419 | BUG(); | ||
| 1420 | spin_lock(&sysctl_lock); | ||
| 1421 | if (!use_table(head)) | ||
| 1422 | head = ERR_PTR(-ENOENT); | ||
| 1423 | spin_unlock(&sysctl_lock); | ||
| 1424 | return head; | ||
| 1425 | } | ||
| 1426 | |||
| 1397 | void sysctl_head_finish(struct ctl_table_header *head) | 1427 | void sysctl_head_finish(struct ctl_table_header *head) |
| 1398 | { | 1428 | { |
| 1399 | if (!head) | 1429 | if (!head) |
| @@ -1403,14 +1433,20 @@ void sysctl_head_finish(struct ctl_table_header *head) | |||
| 1403 | spin_unlock(&sysctl_lock); | 1433 | spin_unlock(&sysctl_lock); |
| 1404 | } | 1434 | } |
| 1405 | 1435 | ||
| 1436 | static struct ctl_table_set * | ||
| 1437 | lookup_header_set(struct ctl_table_root *root, struct nsproxy *namespaces) | ||
| 1438 | { | ||
| 1439 | struct ctl_table_set *set = &root->default_set; | ||
| 1440 | if (root->lookup) | ||
| 1441 | set = root->lookup(root, namespaces); | ||
| 1442 | return set; | ||
| 1443 | } | ||
| 1444 | |||
| 1406 | static struct list_head * | 1445 | static struct list_head * |
| 1407 | lookup_header_list(struct ctl_table_root *root, struct nsproxy *namespaces) | 1446 | lookup_header_list(struct ctl_table_root *root, struct nsproxy *namespaces) |
| 1408 | { | 1447 | { |
| 1409 | struct list_head *header_list; | 1448 | struct ctl_table_set *set = lookup_header_set(root, namespaces); |
| 1410 | header_list = &root->header_list; | 1449 | return &set->list; |
| 1411 | if (root->lookup) | ||
| 1412 | header_list = root->lookup(root, namespaces); | ||
| 1413 | return header_list; | ||
| 1414 | } | 1450 | } |
| 1415 | 1451 | ||
| 1416 | struct ctl_table_header *__sysctl_head_next(struct nsproxy *namespaces, | 1452 | struct ctl_table_header *__sysctl_head_next(struct nsproxy *namespaces, |
| @@ -1480,9 +1516,9 @@ static int do_sysctl_strategy(struct ctl_table_root *root, | |||
| 1480 | int op = 0, rc; | 1516 | int op = 0, rc; |
| 1481 | 1517 | ||
| 1482 | if (oldval) | 1518 | if (oldval) |
| 1483 | op |= 004; | 1519 | op |= MAY_READ; |
| 1484 | if (newval) | 1520 | if (newval) |
| 1485 | op |= 002; | 1521 | op |= MAY_WRITE; |
| 1486 | if (sysctl_perm(root, table, op)) | 1522 | if (sysctl_perm(root, table, op)) |
| 1487 | return -EPERM; | 1523 | return -EPERM; |
| 1488 | 1524 | ||
| @@ -1524,7 +1560,7 @@ repeat: | |||
| 1524 | if (n == table->ctl_name) { | 1560 | if (n == table->ctl_name) { |
| 1525 | int error; | 1561 | int error; |
| 1526 | if (table->child) { | 1562 | if (table->child) { |
| 1527 | if (sysctl_perm(root, table, 001)) | 1563 | if (sysctl_perm(root, table, MAY_EXEC)) |
| 1528 | return -EPERM; | 1564 | return -EPERM; |
| 1529 | name++; | 1565 | name++; |
| 1530 | nlen--; | 1566 | nlen--; |
| @@ -1599,7 +1635,7 @@ static int test_perm(int mode, int op) | |||
| 1599 | mode >>= 6; | 1635 | mode >>= 6; |
| 1600 | else if (in_egroup_p(0)) | 1636 | else if (in_egroup_p(0)) |
| 1601 | mode >>= 3; | 1637 | mode >>= 3; |
| 1602 | if ((mode & op & 0007) == op) | 1638 | if ((op & ~mode & (MAY_READ|MAY_WRITE|MAY_EXEC)) == 0) |
| 1603 | return 0; | 1639 | return 0; |
| 1604 | return -EACCES; | 1640 | return -EACCES; |
| 1605 | } | 1641 | } |
| @@ -1609,7 +1645,7 @@ int sysctl_perm(struct ctl_table_root *root, struct ctl_table *table, int op) | |||
| 1609 | int error; | 1645 | int error; |
| 1610 | int mode; | 1646 | int mode; |
| 1611 | 1647 | ||
| 1612 | error = security_sysctl(table, op); | 1648 | error = security_sysctl(table, op & (MAY_READ | MAY_WRITE | MAY_EXEC)); |
| 1613 | if (error) | 1649 | if (error) |
| 1614 | return error; | 1650 | return error; |
| 1615 | 1651 | ||
| @@ -1644,6 +1680,52 @@ static __init int sysctl_init(void) | |||
| 1644 | 1680 | ||
| 1645 | core_initcall(sysctl_init); | 1681 | core_initcall(sysctl_init); |
| 1646 | 1682 | ||
| 1683 | static int is_branch_in(struct ctl_table *branch, struct ctl_table *table) | ||
| 1684 | { | ||
| 1685 | struct ctl_table *p; | ||
| 1686 | const char *s = branch->procname; | ||
| 1687 | |||
| 1688 | /* branch should have named subdirectory as its first element */ | ||
| 1689 | if (!s || !branch->child) | ||
| 1690 | return 0; | ||
| 1691 | |||
| 1692 | /* ... and nothing else */ | ||
| 1693 | if (branch[1].procname || branch[1].ctl_name) | ||
| 1694 | return 0; | ||
| 1695 | |||
| 1696 | /* table should contain subdirectory with the same name */ | ||
| 1697 | for (p = table; p->procname || p->ctl_name; p++) { | ||
| 1698 | if (!p->child) | ||
| 1699 | continue; | ||
| 1700 | if (p->procname && strcmp(p->procname, s) == 0) | ||
| 1701 | return 1; | ||
| 1702 | } | ||
| 1703 | return 0; | ||
| 1704 | } | ||
| 1705 | |||
| 1706 | /* see if attaching q to p would be an improvement */ | ||
| 1707 | static void try_attach(struct ctl_table_header *p, struct ctl_table_header *q) | ||
| 1708 | { | ||
| 1709 | struct ctl_table *to = p->ctl_table, *by = q->ctl_table; | ||
| 1710 | int is_better = 0; | ||
| 1711 | int not_in_parent = !p->attached_by; | ||
| 1712 | |||
| 1713 | while (is_branch_in(by, to)) { | ||
| 1714 | if (by == q->attached_by) | ||
| 1715 | is_better = 1; | ||
| 1716 | if (to == p->attached_by) | ||
| 1717 | not_in_parent = 1; | ||
| 1718 | by = by->child; | ||
| 1719 | to = to->child; | ||
| 1720 | } | ||
| 1721 | |||
| 1722 | if (is_better && not_in_parent) { | ||
| 1723 | q->attached_by = by; | ||
| 1724 | q->attached_to = to; | ||
| 1725 | q->parent = p; | ||
| 1726 | } | ||
| 1727 | } | ||
| 1728 | |||
| 1647 | /** | 1729 | /** |
| 1648 | * __register_sysctl_paths - register a sysctl hierarchy | 1730 | * __register_sysctl_paths - register a sysctl hierarchy |
| 1649 | * @root: List of sysctl headers to register on | 1731 | * @root: List of sysctl headers to register on |
| @@ -1720,10 +1802,10 @@ struct ctl_table_header *__register_sysctl_paths( | |||
| 1720 | struct nsproxy *namespaces, | 1802 | struct nsproxy *namespaces, |
| 1721 | const struct ctl_path *path, struct ctl_table *table) | 1803 | const struct ctl_path *path, struct ctl_table *table) |
| 1722 | { | 1804 | { |
| 1723 | struct list_head *header_list; | ||
| 1724 | struct ctl_table_header *header; | 1805 | struct ctl_table_header *header; |
| 1725 | struct ctl_table *new, **prevp; | 1806 | struct ctl_table *new, **prevp; |
| 1726 | unsigned int n, npath; | 1807 | unsigned int n, npath; |
| 1808 | struct ctl_table_set *set; | ||
| 1727 | 1809 | ||
| 1728 | /* Count the path components */ | 1810 | /* Count the path components */ |
| 1729 | for (npath = 0; path[npath].ctl_name || path[npath].procname; ++npath) | 1811 | for (npath = 0; path[npath].ctl_name || path[npath].procname; ++npath) |
| @@ -1765,6 +1847,7 @@ struct ctl_table_header *__register_sysctl_paths( | |||
| 1765 | header->unregistering = NULL; | 1847 | header->unregistering = NULL; |
| 1766 | header->root = root; | 1848 | header->root = root; |
| 1767 | sysctl_set_parent(NULL, header->ctl_table); | 1849 | sysctl_set_parent(NULL, header->ctl_table); |
| 1850 | header->count = 1; | ||
| 1768 | #ifdef CONFIG_SYSCTL_SYSCALL_CHECK | 1851 | #ifdef CONFIG_SYSCTL_SYSCALL_CHECK |
| 1769 | if (sysctl_check_table(namespaces, header->ctl_table)) { | 1852 | if (sysctl_check_table(namespaces, header->ctl_table)) { |
| 1770 | kfree(header); | 1853 | kfree(header); |
| @@ -1772,8 +1855,20 @@ struct ctl_table_header *__register_sysctl_paths( | |||
| 1772 | } | 1855 | } |
| 1773 | #endif | 1856 | #endif |
| 1774 | spin_lock(&sysctl_lock); | 1857 | spin_lock(&sysctl_lock); |
| 1775 | header_list = lookup_header_list(root, namespaces); | 1858 | header->set = lookup_header_set(root, namespaces); |
| 1776 | list_add_tail(&header->ctl_entry, header_list); | 1859 | header->attached_by = header->ctl_table; |
| 1860 | header->attached_to = root_table; | ||
| 1861 | header->parent = &root_table_header; | ||
| 1862 | for (set = header->set; set; set = set->parent) { | ||
| 1863 | struct ctl_table_header *p; | ||
| 1864 | list_for_each_entry(p, &set->list, ctl_entry) { | ||
| 1865 | if (p->unregistering) | ||
| 1866 | continue; | ||
| 1867 | try_attach(p, header); | ||
| 1868 | } | ||
| 1869 | } | ||
| 1870 | header->parent->count++; | ||
| 1871 | list_add_tail(&header->ctl_entry, &header->set->list); | ||
| 1777 | spin_unlock(&sysctl_lock); | 1872 | spin_unlock(&sysctl_lock); |
| 1778 | 1873 | ||
| 1779 | return header; | 1874 | return header; |
| @@ -1828,8 +1923,37 @@ void unregister_sysctl_table(struct ctl_table_header * header) | |||
| 1828 | 1923 | ||
| 1829 | spin_lock(&sysctl_lock); | 1924 | spin_lock(&sysctl_lock); |
| 1830 | start_unregistering(header); | 1925 | start_unregistering(header); |
| 1926 | if (!--header->parent->count) { | ||
| 1927 | WARN_ON(1); | ||
| 1928 | kfree(header->parent); | ||
| 1929 | } | ||
| 1930 | if (!--header->count) | ||
| 1931 | kfree(header); | ||
| 1831 | spin_unlock(&sysctl_lock); | 1932 | spin_unlock(&sysctl_lock); |
| 1832 | kfree(header); | 1933 | } |
| 1934 | |||
| 1935 | int sysctl_is_seen(struct ctl_table_header *p) | ||
| 1936 | { | ||
| 1937 | struct ctl_table_set *set = p->set; | ||
| 1938 | int res; | ||
| 1939 | spin_lock(&sysctl_lock); | ||
| 1940 | if (p->unregistering) | ||
| 1941 | res = 0; | ||
| 1942 | else if (!set->is_seen) | ||
| 1943 | res = 1; | ||
| 1944 | else | ||
| 1945 | res = set->is_seen(set); | ||
| 1946 | spin_unlock(&sysctl_lock); | ||
| 1947 | return res; | ||
| 1948 | } | ||
| 1949 | |||
| 1950 | void setup_sysctl_set(struct ctl_table_set *p, | ||
| 1951 | struct ctl_table_set *parent, | ||
| 1952 | int (*is_seen)(struct ctl_table_set *)) | ||
| 1953 | { | ||
| 1954 | INIT_LIST_HEAD(&p->list); | ||
| 1955 | p->parent = parent ? parent : &sysctl_table_root.default_set; | ||
| 1956 | p->is_seen = is_seen; | ||
| 1833 | } | 1957 | } |
| 1834 | 1958 | ||
| 1835 | #else /* !CONFIG_SYSCTL */ | 1959 | #else /* !CONFIG_SYSCTL */ |
| @@ -1848,6 +1972,16 @@ void unregister_sysctl_table(struct ctl_table_header * table) | |||
| 1848 | { | 1972 | { |
| 1849 | } | 1973 | } |
| 1850 | 1974 | ||
| 1975 | void setup_sysctl_set(struct ctl_table_set *p, | ||
| 1976 | struct ctl_table_set *parent, | ||
| 1977 | int (*is_seen)(struct ctl_table_set *)) | ||
| 1978 | { | ||
| 1979 | } | ||
| 1980 | |||
| 1981 | void sysctl_head_put(struct ctl_table_header *head) | ||
| 1982 | { | ||
| 1983 | } | ||
| 1984 | |||
| 1851 | #endif /* CONFIG_SYSCTL */ | 1985 | #endif /* CONFIG_SYSCTL */ |
| 1852 | 1986 | ||
| 1853 | /* | 1987 | /* |
