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