Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Eric W. Biederman | afa588b | 2009-04-02 23:44:59 -0700 | [diff] [blame] | 2 | #include <linux/stat.h> |
| 3 | #include <linux/sysctl.h> |
Christoph Hellwig | c59d87c | 2011-08-12 16:21:35 -0500 | [diff] [blame] | 4 | #include "../fs/xfs/xfs_sysctl.h" |
Eric W. Biederman | afa588b | 2009-04-02 23:44:59 -0700 | [diff] [blame] | 5 | #include <linux/sunrpc/debug.h> |
| 6 | #include <linux/string.h> |
Eric W. Biederman | afa588b | 2009-04-02 23:44:59 -0700 | [diff] [blame] | 7 | #include <linux/syscalls.h> |
| 8 | #include <linux/namei.h> |
| 9 | #include <linux/mount.h> |
| 10 | #include <linux/fs.h> |
| 11 | #include <linux/nsproxy.h> |
| 12 | #include <linux/pid_namespace.h> |
| 13 | #include <linux/file.h> |
| 14 | #include <linux/ctype.h> |
Eric W. Biederman | 63395b6 | 2009-11-12 00:35:55 -0800 | [diff] [blame] | 15 | #include <linux/netdevice.h> |
Andy Shevchenko | 69e4469 | 2010-05-24 14:33:26 -0700 | [diff] [blame] | 16 | #include <linux/kernel.h> |
Andy Shevchenko | ede9c277 | 2016-05-20 17:01:10 -0700 | [diff] [blame] | 17 | #include <linux/uuid.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 18 | #include <linux/slab.h> |
Al Viro | c5ddd20 | 2013-03-19 14:25:51 -0400 | [diff] [blame] | 19 | #include <linux/compat.h> |
Eric W. Biederman | afa588b | 2009-04-02 23:44:59 -0700 | [diff] [blame] | 20 | |
Eric W. Biederman | 26a7034 | 2009-11-05 05:26:41 -0800 | [diff] [blame] | 21 | static ssize_t binary_sysctl(const int *name, int nlen, |
| 22 | void __user *oldval, size_t oldlen, void __user *newval, size_t newlen) |
Eric W. Biederman | afa588b | 2009-04-02 23:44:59 -0700 | [diff] [blame] | 23 | { |
Eric W. Biederman | 2830b68 | 2009-04-03 00:09:33 -0700 | [diff] [blame] | 24 | return -ENOSYS; |
Eric W. Biederman | afa588b | 2009-04-02 23:44:59 -0700 | [diff] [blame] | 25 | } |
| 26 | |
Eric W. Biederman | 2830b68 | 2009-04-03 00:09:33 -0700 | [diff] [blame] | 27 | static void deprecated_sysctl_warning(const int *name, int nlen) |
Eric W. Biederman | afa588b | 2009-04-02 23:44:59 -0700 | [diff] [blame] | 28 | { |
Eric W. Biederman | afa588b | 2009-04-02 23:44:59 -0700 | [diff] [blame] | 29 | int i; |
| 30 | |
Andi Kleen | 61cf6931 | 2009-12-16 12:28:44 +0100 | [diff] [blame] | 31 | /* |
| 32 | * CTL_KERN/KERN_VERSION is used by older glibc and cannot |
| 33 | * ever go away. |
| 34 | */ |
Mateusz Jurczyk | 9380fa6 | 2017-07-12 14:34:01 -0700 | [diff] [blame] | 35 | if (nlen >= 2 && name[0] == CTL_KERN && name[1] == KERN_VERSION) |
Andi Kleen | 61cf6931 | 2009-12-16 12:28:44 +0100 | [diff] [blame] | 36 | return; |
| 37 | |
Eric W. Biederman | 2fb10732 | 2009-11-11 19:32:48 -0800 | [diff] [blame] | 38 | if (printk_ratelimit()) { |
Eric W. Biederman | afa588b | 2009-04-02 23:44:59 -0700 | [diff] [blame] | 39 | printk(KERN_INFO |
| 40 | "warning: process `%s' used the deprecated sysctl " |
| 41 | "system call with ", current->comm); |
Eric W. Biederman | 2830b68 | 2009-04-03 00:09:33 -0700 | [diff] [blame] | 42 | for (i = 0; i < nlen; i++) |
Tetsuo Handa | 7560ef3 | 2016-12-14 15:04:26 -0800 | [diff] [blame] | 43 | printk(KERN_CONT "%d.", name[i]); |
| 44 | printk(KERN_CONT "\n"); |
Eric W. Biederman | afa588b | 2009-04-02 23:44:59 -0700 | [diff] [blame] | 45 | } |
Eric W. Biederman | 2830b68 | 2009-04-03 00:09:33 -0700 | [diff] [blame] | 46 | return; |
| 47 | } |
| 48 | |
Andi Kleen | 4440095 | 2009-12-23 21:00:20 +0100 | [diff] [blame] | 49 | #define WARN_ONCE_HASH_BITS 8 |
| 50 | #define WARN_ONCE_HASH_SIZE (1<<WARN_ONCE_HASH_BITS) |
| 51 | |
| 52 | static DECLARE_BITMAP(warn_once_bitmap, WARN_ONCE_HASH_SIZE); |
| 53 | |
| 54 | #define FNV32_OFFSET 2166136261U |
| 55 | #define FNV32_PRIME 0x01000193 |
| 56 | |
| 57 | /* |
| 58 | * Print each legacy sysctl (approximately) only once. |
| 59 | * To avoid making the tables non-const use a external |
| 60 | * hash-table instead. |
| 61 | * Worst case hash collision: 6, but very rarely. |
| 62 | * NOTE! We don't use the SMP-safe bit tests. We simply |
| 63 | * don't care enough. |
| 64 | */ |
| 65 | static void warn_on_bintable(const int *name, int nlen) |
| 66 | { |
| 67 | int i; |
| 68 | u32 hash = FNV32_OFFSET; |
| 69 | |
| 70 | for (i = 0; i < nlen; i++) |
| 71 | hash = (hash ^ name[i]) * FNV32_PRIME; |
| 72 | hash %= WARN_ONCE_HASH_SIZE; |
| 73 | if (__test_and_set_bit(hash, warn_once_bitmap)) |
| 74 | return; |
| 75 | deprecated_sysctl_warning(name, nlen); |
| 76 | } |
| 77 | |
Eric W. Biederman | 26a7034 | 2009-11-05 05:26:41 -0800 | [diff] [blame] | 78 | static ssize_t do_sysctl(int __user *args_name, int nlen, |
| 79 | void __user *oldval, size_t oldlen, void __user *newval, size_t newlen) |
Eric W. Biederman | 2830b68 | 2009-04-03 00:09:33 -0700 | [diff] [blame] | 80 | { |
| 81 | int name[CTL_MAXNAME]; |
Eric W. Biederman | 2830b68 | 2009-04-03 00:09:33 -0700 | [diff] [blame] | 82 | int i; |
| 83 | |
Eric W. Biederman | 26a7034 | 2009-11-05 05:26:41 -0800 | [diff] [blame] | 84 | /* Check args->nlen. */ |
| 85 | if (nlen < 0 || nlen > CTL_MAXNAME) |
Eric W. Biederman | 2830b68 | 2009-04-03 00:09:33 -0700 | [diff] [blame] | 86 | return -ENOTDIR; |
Eric W. Biederman | 2830b68 | 2009-04-03 00:09:33 -0700 | [diff] [blame] | 87 | /* Read in the sysctl name for simplicity */ |
| 88 | for (i = 0; i < nlen; i++) |
| 89 | if (get_user(name[i], args_name + i)) |
| 90 | return -EFAULT; |
| 91 | |
Andi Kleen | 4440095 | 2009-12-23 21:00:20 +0100 | [diff] [blame] | 92 | warn_on_bintable(name, nlen); |
Eric W. Biederman | 2830b68 | 2009-04-03 00:09:33 -0700 | [diff] [blame] | 93 | |
Eric W. Biederman | 26a7034 | 2009-11-05 05:26:41 -0800 | [diff] [blame] | 94 | return binary_sysctl(name, nlen, oldval, oldlen, newval, newlen); |
Eric W. Biederman | 2830b68 | 2009-04-03 00:09:33 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Eric W. Biederman | 2830b68 | 2009-04-03 00:09:33 -0700 | [diff] [blame] | 97 | SYSCALL_DEFINE1(sysctl, struct __sysctl_args __user *, args) |
| 98 | { |
| 99 | struct __sysctl_args tmp; |
Eric W. Biederman | 26a7034 | 2009-11-05 05:26:41 -0800 | [diff] [blame] | 100 | size_t oldlen = 0; |
| 101 | ssize_t result; |
Eric W. Biederman | 2830b68 | 2009-04-03 00:09:33 -0700 | [diff] [blame] | 102 | |
| 103 | if (copy_from_user(&tmp, args, sizeof(tmp))) |
| 104 | return -EFAULT; |
| 105 | |
Eric W. Biederman | 26a7034 | 2009-11-05 05:26:41 -0800 | [diff] [blame] | 106 | if (tmp.oldval && !tmp.oldlenp) |
| 107 | return -EFAULT; |
Eric W. Biederman | 2830b68 | 2009-04-03 00:09:33 -0700 | [diff] [blame] | 108 | |
Eric W. Biederman | 26a7034 | 2009-11-05 05:26:41 -0800 | [diff] [blame] | 109 | if (tmp.oldlenp && get_user(oldlen, tmp.oldlenp)) |
| 110 | return -EFAULT; |
| 111 | |
| 112 | result = do_sysctl(tmp.name, tmp.nlen, tmp.oldval, oldlen, |
| 113 | tmp.newval, tmp.newlen); |
| 114 | |
| 115 | if (result >= 0) { |
| 116 | oldlen = result; |
| 117 | result = 0; |
| 118 | } |
| 119 | |
| 120 | if (tmp.oldlenp && put_user(oldlen, tmp.oldlenp)) |
| 121 | return -EFAULT; |
| 122 | |
| 123 | return result; |
Eric W. Biederman | afa588b | 2009-04-02 23:44:59 -0700 | [diff] [blame] | 124 | } |
Eric W. Biederman | da3f6f9 | 2009-04-03 00:36:27 -0700 | [diff] [blame] | 125 | |
Eric W. Biederman | 26a7034 | 2009-11-05 05:26:41 -0800 | [diff] [blame] | 126 | |
Eric W. Biederman | da3f6f9 | 2009-04-03 00:36:27 -0700 | [diff] [blame] | 127 | #ifdef CONFIG_COMPAT |
Eric W. Biederman | da3f6f9 | 2009-04-03 00:36:27 -0700 | [diff] [blame] | 128 | |
| 129 | struct compat_sysctl_args { |
| 130 | compat_uptr_t name; |
| 131 | int nlen; |
| 132 | compat_uptr_t oldval; |
| 133 | compat_uptr_t oldlenp; |
| 134 | compat_uptr_t newval; |
| 135 | compat_size_t newlen; |
| 136 | compat_ulong_t __unused[4]; |
| 137 | }; |
| 138 | |
Al Viro | c5ddd20 | 2013-03-19 14:25:51 -0400 | [diff] [blame] | 139 | COMPAT_SYSCALL_DEFINE1(sysctl, struct compat_sysctl_args __user *, args) |
Eric W. Biederman | da3f6f9 | 2009-04-03 00:36:27 -0700 | [diff] [blame] | 140 | { |
| 141 | struct compat_sysctl_args tmp; |
| 142 | compat_size_t __user *compat_oldlenp; |
Eric W. Biederman | da3f6f9 | 2009-04-03 00:36:27 -0700 | [diff] [blame] | 143 | size_t oldlen = 0; |
| 144 | ssize_t result; |
| 145 | |
| 146 | if (copy_from_user(&tmp, args, sizeof(tmp))) |
| 147 | return -EFAULT; |
| 148 | |
Eric W. Biederman | 26a7034 | 2009-11-05 05:26:41 -0800 | [diff] [blame] | 149 | if (tmp.oldval && !tmp.oldlenp) |
| 150 | return -EFAULT; |
| 151 | |
Eric W. Biederman | da3f6f9 | 2009-04-03 00:36:27 -0700 | [diff] [blame] | 152 | compat_oldlenp = compat_ptr(tmp.oldlenp); |
Eric W. Biederman | 26a7034 | 2009-11-05 05:26:41 -0800 | [diff] [blame] | 153 | if (compat_oldlenp && get_user(oldlen, compat_oldlenp)) |
| 154 | return -EFAULT; |
Eric W. Biederman | da3f6f9 | 2009-04-03 00:36:27 -0700 | [diff] [blame] | 155 | |
Eric W. Biederman | da3f6f9 | 2009-04-03 00:36:27 -0700 | [diff] [blame] | 156 | result = do_sysctl(compat_ptr(tmp.name), tmp.nlen, |
Eric W. Biederman | 26a7034 | 2009-11-05 05:26:41 -0800 | [diff] [blame] | 157 | compat_ptr(tmp.oldval), oldlen, |
Eric W. Biederman | da3f6f9 | 2009-04-03 00:36:27 -0700 | [diff] [blame] | 158 | compat_ptr(tmp.newval), tmp.newlen); |
Eric W. Biederman | da3f6f9 | 2009-04-03 00:36:27 -0700 | [diff] [blame] | 159 | |
Eric W. Biederman | 26a7034 | 2009-11-05 05:26:41 -0800 | [diff] [blame] | 160 | if (result >= 0) { |
| 161 | oldlen = result; |
| 162 | result = 0; |
Eric W. Biederman | da3f6f9 | 2009-04-03 00:36:27 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Eric W. Biederman | 26a7034 | 2009-11-05 05:26:41 -0800 | [diff] [blame] | 165 | if (compat_oldlenp && put_user(oldlen, compat_oldlenp)) |
| 166 | return -EFAULT; |
| 167 | |
Eric W. Biederman | da3f6f9 | 2009-04-03 00:36:27 -0700 | [diff] [blame] | 168 | return result; |
| 169 | } |
| 170 | |
| 171 | #endif /* CONFIG_COMPAT */ |