Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 1 | /* |
| 2 | * /proc/sys support |
| 3 | */ |
Alexey Dobriyan | 1e0edd3 | 2008-10-17 05:07:44 +0400 | [diff] [blame] | 4 | #include <linux/init.h> |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 5 | #include <linux/sysctl.h> |
Lucas De Marchi | f1ecf06 | 2011-11-02 13:39:22 -0700 | [diff] [blame^] | 6 | #include <linux/poll.h> |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 7 | #include <linux/proc_fs.h> |
| 8 | #include <linux/security.h> |
Nick Piggin | 34286d6 | 2011-01-07 17:49:57 +1100 | [diff] [blame] | 9 | #include <linux/namei.h> |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 10 | #include "internal.h" |
| 11 | |
Al Viro | d72f71e | 2009-02-20 05:58:47 +0000 | [diff] [blame] | 12 | static const struct dentry_operations proc_sys_dentry_operations; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 13 | static const struct file_operations proc_sys_file_operations; |
Jan Engelhardt | 03a4482 | 2008-02-08 04:21:19 -0800 | [diff] [blame] | 14 | static const struct inode_operations proc_sys_inode_operations; |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 15 | static const struct file_operations proc_sys_dir_file_operations; |
| 16 | static const struct inode_operations proc_sys_dir_operations; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 17 | |
Lucas De Marchi | f1ecf06 | 2011-11-02 13:39:22 -0700 | [diff] [blame^] | 18 | void proc_sys_poll_notify(struct ctl_table_poll *poll) |
| 19 | { |
| 20 | if (!poll) |
| 21 | return; |
| 22 | |
| 23 | atomic_inc(&poll->event); |
| 24 | wake_up_interruptible(&poll->wait); |
| 25 | } |
| 26 | |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 27 | static struct inode *proc_sys_make_inode(struct super_block *sb, |
| 28 | struct ctl_table_header *head, struct ctl_table *table) |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 29 | { |
| 30 | struct inode *inode; |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 31 | struct proc_inode *ei; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 32 | |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 33 | inode = new_inode(sb); |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 34 | if (!inode) |
| 35 | goto out; |
| 36 | |
Christoph Hellwig | 85fe402 | 2010-10-23 11:19:54 -0400 | [diff] [blame] | 37 | inode->i_ino = get_next_ino(); |
| 38 | |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 39 | sysctl_head_get(head); |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 40 | ei = PROC_I(inode); |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 41 | ei->sysctl = head; |
| 42 | ei->sysctl_entry = table; |
| 43 | |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 44 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 45 | inode->i_mode = table->mode; |
| 46 | if (!table->child) { |
| 47 | inode->i_mode |= S_IFREG; |
| 48 | inode->i_op = &proc_sys_inode_operations; |
| 49 | inode->i_fop = &proc_sys_file_operations; |
| 50 | } else { |
| 51 | inode->i_mode |= S_IFDIR; |
| 52 | inode->i_nlink = 0; |
| 53 | inode->i_op = &proc_sys_dir_operations; |
| 54 | inode->i_fop = &proc_sys_dir_file_operations; |
| 55 | } |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 56 | out: |
| 57 | return inode; |
| 58 | } |
| 59 | |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 60 | static struct ctl_table *find_in_table(struct ctl_table *p, struct qstr *name) |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 61 | { |
| 62 | int len; |
Eric W. Biederman | 2315ffa | 2009-04-03 03:18:02 -0700 | [diff] [blame] | 63 | for ( ; p->procname; p++) { |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 64 | |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 65 | if (!p->procname) |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 66 | continue; |
| 67 | |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 68 | len = strlen(p->procname); |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 69 | if (len != name->len) |
| 70 | continue; |
| 71 | |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 72 | if (memcmp(p->procname, name->name, len) != 0) |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 73 | continue; |
| 74 | |
| 75 | /* I have a match */ |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 76 | return p; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 77 | } |
| 78 | return NULL; |
| 79 | } |
| 80 | |
Adrian Bunk | 8132436 | 2008-10-03 00:33:54 +0400 | [diff] [blame] | 81 | static struct ctl_table_header *grab_header(struct inode *inode) |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 82 | { |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 83 | if (PROC_I(inode)->sysctl) |
| 84 | return sysctl_head_grab(PROC_I(inode)->sysctl); |
| 85 | else |
| 86 | return sysctl_head_next(NULL); |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry, |
| 90 | struct nameidata *nd) |
| 91 | { |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 92 | struct ctl_table_header *head = grab_header(dir); |
| 93 | struct ctl_table *table = PROC_I(dir)->sysctl_entry; |
| 94 | struct ctl_table_header *h = NULL; |
| 95 | struct qstr *name = &dentry->d_name; |
| 96 | struct ctl_table *p; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 97 | struct inode *inode; |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 98 | struct dentry *err = ERR_PTR(-ENOENT); |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 99 | |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 100 | if (IS_ERR(head)) |
| 101 | return ERR_CAST(head); |
| 102 | |
| 103 | if (table && !table->child) { |
| 104 | WARN_ON(1); |
| 105 | goto out; |
| 106 | } |
| 107 | |
| 108 | table = table ? table->child : head->ctl_table; |
| 109 | |
| 110 | p = find_in_table(table, name); |
| 111 | if (!p) { |
| 112 | for (h = sysctl_head_next(NULL); h; h = sysctl_head_next(h)) { |
| 113 | if (h->attached_to != table) |
| 114 | continue; |
| 115 | p = find_in_table(h->attached_by, name); |
| 116 | if (p) |
| 117 | break; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | if (!p) |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 122 | goto out; |
| 123 | |
| 124 | err = ERR_PTR(-ENOMEM); |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 125 | inode = proc_sys_make_inode(dir->i_sb, h ? h : head, p); |
| 126 | if (h) |
| 127 | sysctl_head_finish(h); |
| 128 | |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 129 | if (!inode) |
| 130 | goto out; |
| 131 | |
| 132 | err = NULL; |
Nick Piggin | fb045ad | 2011-01-07 17:49:55 +1100 | [diff] [blame] | 133 | d_set_d_op(dentry, &proc_sys_dentry_operations); |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 134 | d_add(dentry, inode); |
| 135 | |
| 136 | out: |
| 137 | sysctl_head_finish(head); |
| 138 | return err; |
| 139 | } |
| 140 | |
Pavel Emelyanov | 7708bfb | 2008-04-29 01:02:40 -0700 | [diff] [blame] | 141 | static ssize_t proc_sys_call_handler(struct file *filp, void __user *buf, |
| 142 | size_t count, loff_t *ppos, int write) |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 143 | { |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 144 | struct inode *inode = filp->f_path.dentry->d_inode; |
| 145 | struct ctl_table_header *head = grab_header(inode); |
| 146 | struct ctl_table *table = PROC_I(inode)->sysctl_entry; |
David Howells | 2a2da53 | 2007-10-25 15:27:40 +0100 | [diff] [blame] | 147 | ssize_t error; |
| 148 | size_t res; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 149 | |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 150 | if (IS_ERR(head)) |
| 151 | return PTR_ERR(head); |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 152 | |
| 153 | /* |
| 154 | * At this point we know that the sysctl was not unregistered |
| 155 | * and won't be until we finish. |
| 156 | */ |
| 157 | error = -EPERM; |
Pavel Emelyanov | d7321cd | 2008-04-29 01:02:44 -0700 | [diff] [blame] | 158 | if (sysctl_perm(head->root, table, write ? MAY_WRITE : MAY_READ)) |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 159 | goto out; |
| 160 | |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 161 | /* if that can happen at all, it should be -EINVAL, not -EISDIR */ |
| 162 | error = -EINVAL; |
| 163 | if (!table->proc_handler) |
| 164 | goto out; |
| 165 | |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 166 | /* careful: calling conventions are nasty here */ |
| 167 | res = count; |
Alexey Dobriyan | 8d65af7 | 2009-09-23 15:57:19 -0700 | [diff] [blame] | 168 | error = table->proc_handler(table, write, buf, &res, ppos); |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 169 | if (!error) |
| 170 | error = res; |
| 171 | out: |
| 172 | sysctl_head_finish(head); |
| 173 | |
| 174 | return error; |
| 175 | } |
| 176 | |
Pavel Emelyanov | 7708bfb | 2008-04-29 01:02:40 -0700 | [diff] [blame] | 177 | static ssize_t proc_sys_read(struct file *filp, char __user *buf, |
| 178 | size_t count, loff_t *ppos) |
| 179 | { |
| 180 | return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 0); |
| 181 | } |
| 182 | |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 183 | static ssize_t proc_sys_write(struct file *filp, const char __user *buf, |
| 184 | size_t count, loff_t *ppos) |
| 185 | { |
Pavel Emelyanov | 7708bfb | 2008-04-29 01:02:40 -0700 | [diff] [blame] | 186 | return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 1); |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 187 | } |
| 188 | |
Lucas De Marchi | f1ecf06 | 2011-11-02 13:39:22 -0700 | [diff] [blame^] | 189 | static int proc_sys_open(struct inode *inode, struct file *filp) |
| 190 | { |
| 191 | struct ctl_table *table = PROC_I(inode)->sysctl_entry; |
| 192 | |
| 193 | if (table->poll) |
| 194 | filp->private_data = proc_sys_poll_event(table->poll); |
| 195 | |
| 196 | return 0; |
| 197 | } |
| 198 | |
| 199 | static unsigned int proc_sys_poll(struct file *filp, poll_table *wait) |
| 200 | { |
| 201 | struct inode *inode = filp->f_path.dentry->d_inode; |
| 202 | struct ctl_table *table = PROC_I(inode)->sysctl_entry; |
| 203 | unsigned long event = (unsigned long)filp->private_data; |
| 204 | unsigned int ret = DEFAULT_POLLMASK; |
| 205 | |
| 206 | if (!table->proc_handler) |
| 207 | goto out; |
| 208 | |
| 209 | if (!table->poll) |
| 210 | goto out; |
| 211 | |
| 212 | poll_wait(filp, &table->poll->wait, wait); |
| 213 | |
| 214 | if (event != atomic_read(&table->poll->event)) { |
| 215 | filp->private_data = proc_sys_poll_event(table->poll); |
| 216 | ret = POLLIN | POLLRDNORM | POLLERR | POLLPRI; |
| 217 | } |
| 218 | |
| 219 | out: |
| 220 | return ret; |
| 221 | } |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 222 | |
| 223 | static int proc_sys_fill_cache(struct file *filp, void *dirent, |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 224 | filldir_t filldir, |
| 225 | struct ctl_table_header *head, |
| 226 | struct ctl_table *table) |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 227 | { |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 228 | struct dentry *child, *dir = filp->f_path.dentry; |
| 229 | struct inode *inode; |
| 230 | struct qstr qname; |
| 231 | ino_t ino = 0; |
| 232 | unsigned type = DT_UNKNOWN; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 233 | |
| 234 | qname.name = table->procname; |
| 235 | qname.len = strlen(table->procname); |
| 236 | qname.hash = full_name_hash(qname.name, qname.len); |
| 237 | |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 238 | child = d_lookup(dir, &qname); |
| 239 | if (!child) { |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 240 | child = d_alloc(dir, &qname); |
| 241 | if (child) { |
| 242 | inode = proc_sys_make_inode(dir->d_sb, head, table); |
| 243 | if (!inode) { |
| 244 | dput(child); |
| 245 | return -ENOMEM; |
| 246 | } else { |
Nick Piggin | fb045ad | 2011-01-07 17:49:55 +1100 | [diff] [blame] | 247 | d_set_d_op(child, &proc_sys_dentry_operations); |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 248 | d_add(child, inode); |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 249 | } |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 250 | } else { |
| 251 | return -ENOMEM; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 252 | } |
| 253 | } |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 254 | inode = child->d_inode; |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 255 | ino = inode->i_ino; |
| 256 | type = inode->i_mode >> 12; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 257 | dput(child); |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 258 | return !!filldir(dirent, qname.name, qname.len, filp->f_pos, ino, type); |
| 259 | } |
| 260 | |
| 261 | static int scan(struct ctl_table_header *head, ctl_table *table, |
| 262 | unsigned long *pos, struct file *file, |
| 263 | void *dirent, filldir_t filldir) |
| 264 | { |
| 265 | |
Eric W. Biederman | 2315ffa | 2009-04-03 03:18:02 -0700 | [diff] [blame] | 266 | for (; table->procname; table++, (*pos)++) { |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 267 | int res; |
| 268 | |
| 269 | /* Can't do anything without a proc name */ |
| 270 | if (!table->procname) |
| 271 | continue; |
| 272 | |
| 273 | if (*pos < file->f_pos) |
| 274 | continue; |
| 275 | |
| 276 | res = proc_sys_fill_cache(file, dirent, filldir, head, table); |
| 277 | if (res) |
| 278 | return res; |
| 279 | |
| 280 | file->f_pos = *pos + 1; |
| 281 | } |
| 282 | return 0; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | static int proc_sys_readdir(struct file *filp, void *dirent, filldir_t filldir) |
| 286 | { |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 287 | struct dentry *dentry = filp->f_path.dentry; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 288 | struct inode *inode = dentry->d_inode; |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 289 | struct ctl_table_header *head = grab_header(inode); |
| 290 | struct ctl_table *table = PROC_I(inode)->sysctl_entry; |
| 291 | struct ctl_table_header *h = NULL; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 292 | unsigned long pos; |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 293 | int ret = -EINVAL; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 294 | |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 295 | if (IS_ERR(head)) |
| 296 | return PTR_ERR(head); |
| 297 | |
| 298 | if (table && !table->child) { |
| 299 | WARN_ON(1); |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 300 | goto out; |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | table = table ? table->child : head->ctl_table; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 304 | |
| 305 | ret = 0; |
| 306 | /* Avoid a switch here: arm builds fail with missing __cmpdi2 */ |
| 307 | if (filp->f_pos == 0) { |
| 308 | if (filldir(dirent, ".", 1, filp->f_pos, |
| 309 | inode->i_ino, DT_DIR) < 0) |
| 310 | goto out; |
| 311 | filp->f_pos++; |
| 312 | } |
| 313 | if (filp->f_pos == 1) { |
| 314 | if (filldir(dirent, "..", 2, filp->f_pos, |
| 315 | parent_ino(dentry), DT_DIR) < 0) |
| 316 | goto out; |
| 317 | filp->f_pos++; |
| 318 | } |
| 319 | pos = 2; |
| 320 | |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 321 | ret = scan(head, table, &pos, filp, dirent, filldir); |
| 322 | if (ret) |
| 323 | goto out; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 324 | |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 325 | for (h = sysctl_head_next(NULL); h; h = sysctl_head_next(h)) { |
| 326 | if (h->attached_to != table) |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 327 | continue; |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 328 | ret = scan(h, h->attached_by, &pos, filp, dirent, filldir); |
| 329 | if (ret) { |
| 330 | sysctl_head_finish(h); |
| 331 | break; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 332 | } |
| 333 | } |
| 334 | ret = 1; |
| 335 | out: |
| 336 | sysctl_head_finish(head); |
| 337 | return ret; |
| 338 | } |
| 339 | |
Al Viro | 10556cb | 2011-06-20 19:28:19 -0400 | [diff] [blame] | 340 | static int proc_sys_permission(struct inode *inode, int mask) |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 341 | { |
| 342 | /* |
| 343 | * sysctl entries that are not writeable, |
| 344 | * are _NOT_ writeable, capabilities or not. |
| 345 | */ |
Miklos Szeredi | f696a36 | 2008-07-31 13:41:58 +0200 | [diff] [blame] | 346 | struct ctl_table_header *head; |
| 347 | struct ctl_table *table; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 348 | int error; |
| 349 | |
Miklos Szeredi | f696a36 | 2008-07-31 13:41:58 +0200 | [diff] [blame] | 350 | /* Executable files are not allowed under /proc/sys/ */ |
| 351 | if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) |
| 352 | return -EACCES; |
| 353 | |
| 354 | head = grab_header(inode); |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 355 | if (IS_ERR(head)) |
| 356 | return PTR_ERR(head); |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 357 | |
Miklos Szeredi | f696a36 | 2008-07-31 13:41:58 +0200 | [diff] [blame] | 358 | table = PROC_I(inode)->sysctl_entry; |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 359 | if (!table) /* global root - r-xr-xr-x */ |
| 360 | error = mask & MAY_WRITE ? -EACCES : 0; |
| 361 | else /* Use the permissions on the sysctl table entry */ |
Al Viro | 1fc0f78 | 2011-06-20 18:59:02 -0400 | [diff] [blame] | 362 | error = sysctl_perm(head->root, table, mask & ~MAY_NOT_BLOCK); |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 363 | |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 364 | sysctl_head_finish(head); |
| 365 | return error; |
| 366 | } |
| 367 | |
| 368 | static int proc_sys_setattr(struct dentry *dentry, struct iattr *attr) |
| 369 | { |
| 370 | struct inode *inode = dentry->d_inode; |
| 371 | int error; |
| 372 | |
| 373 | if (attr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID)) |
| 374 | return -EPERM; |
| 375 | |
| 376 | error = inode_change_ok(inode, attr); |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 377 | if (error) |
| 378 | return error; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 379 | |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 380 | if ((attr->ia_valid & ATTR_SIZE) && |
| 381 | attr->ia_size != i_size_read(inode)) { |
| 382 | error = vmtruncate(inode, attr->ia_size); |
| 383 | if (error) |
| 384 | return error; |
| 385 | } |
| 386 | |
| 387 | setattr_copy(inode, attr); |
| 388 | mark_inode_dirty(inode); |
| 389 | return 0; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 390 | } |
| 391 | |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 392 | static int proc_sys_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) |
| 393 | { |
| 394 | struct inode *inode = dentry->d_inode; |
| 395 | struct ctl_table_header *head = grab_header(inode); |
| 396 | struct ctl_table *table = PROC_I(inode)->sysctl_entry; |
| 397 | |
| 398 | if (IS_ERR(head)) |
| 399 | return PTR_ERR(head); |
| 400 | |
| 401 | generic_fillattr(inode, stat); |
| 402 | if (table) |
| 403 | stat->mode = (stat->mode & S_IFMT) | table->mode; |
| 404 | |
| 405 | sysctl_head_finish(head); |
| 406 | return 0; |
| 407 | } |
| 408 | |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 409 | static const struct file_operations proc_sys_file_operations = { |
Lucas De Marchi | f1ecf06 | 2011-11-02 13:39:22 -0700 | [diff] [blame^] | 410 | .open = proc_sys_open, |
| 411 | .poll = proc_sys_poll, |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 412 | .read = proc_sys_read, |
| 413 | .write = proc_sys_write, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 414 | .llseek = default_llseek, |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 415 | }; |
| 416 | |
| 417 | static const struct file_operations proc_sys_dir_file_operations = { |
Pavel Emelyanov | 887df07 | 2011-11-02 13:38:42 -0700 | [diff] [blame] | 418 | .read = generic_read_dir, |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 419 | .readdir = proc_sys_readdir, |
Christoph Hellwig | 3222a3e | 2008-09-03 21:53:01 +0200 | [diff] [blame] | 420 | .llseek = generic_file_llseek, |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 421 | }; |
| 422 | |
Jan Engelhardt | 03a4482 | 2008-02-08 04:21:19 -0800 | [diff] [blame] | 423 | static const struct inode_operations proc_sys_inode_operations = { |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 424 | .permission = proc_sys_permission, |
| 425 | .setattr = proc_sys_setattr, |
| 426 | .getattr = proc_sys_getattr, |
| 427 | }; |
| 428 | |
| 429 | static const struct inode_operations proc_sys_dir_operations = { |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 430 | .lookup = proc_sys_lookup, |
| 431 | .permission = proc_sys_permission, |
| 432 | .setattr = proc_sys_setattr, |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 433 | .getattr = proc_sys_getattr, |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 434 | }; |
| 435 | |
| 436 | static int proc_sys_revalidate(struct dentry *dentry, struct nameidata *nd) |
| 437 | { |
Nick Piggin | 34286d6 | 2011-01-07 17:49:57 +1100 | [diff] [blame] | 438 | if (nd->flags & LOOKUP_RCU) |
| 439 | return -ECHILD; |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 440 | return !PROC_I(dentry->d_inode)->sysctl->unregistering; |
| 441 | } |
| 442 | |
Nick Piggin | fe15ce4 | 2011-01-07 17:49:23 +1100 | [diff] [blame] | 443 | static int proc_sys_delete(const struct dentry *dentry) |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 444 | { |
| 445 | return !!PROC_I(dentry->d_inode)->sysctl->unregistering; |
| 446 | } |
| 447 | |
Nick Piggin | 621e155 | 2011-01-07 17:49:27 +1100 | [diff] [blame] | 448 | static int proc_sys_compare(const struct dentry *parent, |
| 449 | const struct inode *pinode, |
| 450 | const struct dentry *dentry, const struct inode *inode, |
| 451 | unsigned int len, const char *str, const struct qstr *name) |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 452 | { |
Al Viro | dfef6dcd3 | 2011-03-08 01:25:28 -0500 | [diff] [blame] | 453 | struct ctl_table_header *head; |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 454 | /* Although proc doesn't have negative dentries, rcu-walk means |
| 455 | * that inode here can be NULL */ |
Al Viro | dfef6dcd3 | 2011-03-08 01:25:28 -0500 | [diff] [blame] | 456 | /* AV: can it, indeed? */ |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 457 | if (!inode) |
Al Viro | dfef6dcd3 | 2011-03-08 01:25:28 -0500 | [diff] [blame] | 458 | return 1; |
Nick Piggin | 621e155 | 2011-01-07 17:49:27 +1100 | [diff] [blame] | 459 | if (name->len != len) |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 460 | return 1; |
Nick Piggin | 621e155 | 2011-01-07 17:49:27 +1100 | [diff] [blame] | 461 | if (memcmp(name->name, str, len)) |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 462 | return 1; |
Al Viro | dfef6dcd3 | 2011-03-08 01:25:28 -0500 | [diff] [blame] | 463 | head = rcu_dereference(PROC_I(inode)->sysctl); |
| 464 | return !head || !sysctl_is_seen(head); |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 465 | } |
| 466 | |
Al Viro | d72f71e | 2009-02-20 05:58:47 +0000 | [diff] [blame] | 467 | static const struct dentry_operations proc_sys_dentry_operations = { |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 468 | .d_revalidate = proc_sys_revalidate, |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 469 | .d_delete = proc_sys_delete, |
| 470 | .d_compare = proc_sys_compare, |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 471 | }; |
| 472 | |
Alexey Dobriyan | 1e0edd3 | 2008-10-17 05:07:44 +0400 | [diff] [blame] | 473 | int __init proc_sys_init(void) |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 474 | { |
Alexey Dobriyan | e167523 | 2008-10-03 00:23:32 +0400 | [diff] [blame] | 475 | struct proc_dir_entry *proc_sys_root; |
| 476 | |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 477 | proc_sys_root = proc_mkdir("sys", NULL); |
Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 478 | proc_sys_root->proc_iops = &proc_sys_dir_operations; |
| 479 | proc_sys_root->proc_fops = &proc_sys_dir_file_operations; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 480 | proc_sys_root->nlink = 0; |
| 481 | return 0; |
| 482 | } |