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