Adel Gadllah | 0b07de8 | 2008-06-26 13:48:27 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2004 Peter M. Jones <pjones@redhat.com> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 as |
| 6 | * published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public Licens |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111- |
| 17 | * |
| 18 | */ |
| 19 | |
| 20 | #include <linux/list.h> |
| 21 | #include <linux/genhd.h> |
| 22 | #include <linux/spinlock.h> |
Adel Gadllah | 0b07de8 | 2008-06-26 13:48:27 +0200 | [diff] [blame] | 23 | #include <linux/capability.h> |
| 24 | #include <linux/bitops.h> |
| 25 | |
| 26 | #include <scsi/scsi.h> |
| 27 | #include <linux/cdrom.h> |
| 28 | |
FUJITA Tomonori | 4beab5c | 2008-07-26 18:03:25 +0900 | [diff] [blame] | 29 | int blk_verify_command(struct blk_cmd_filter *filter, |
FUJITA Tomonori | abf5439 | 2008-08-16 14:10:05 +0900 | [diff] [blame] | 30 | unsigned char *cmd, int has_write_perm) |
Adel Gadllah | 0b07de8 | 2008-06-26 13:48:27 +0200 | [diff] [blame] | 31 | { |
| 32 | /* root can do any command. */ |
| 33 | if (capable(CAP_SYS_RAWIO)) |
| 34 | return 0; |
| 35 | |
| 36 | /* if there's no filter set, assume we're filtering everything out */ |
| 37 | if (!filter) |
| 38 | return -EPERM; |
| 39 | |
| 40 | /* Anybody who can open the device can do a read-safe command */ |
| 41 | if (test_bit(cmd[0], filter->read_ok)) |
| 42 | return 0; |
| 43 | |
| 44 | /* Write-safe commands require a writable open */ |
FUJITA Tomonori | abf5439 | 2008-08-16 14:10:05 +0900 | [diff] [blame] | 45 | if (test_bit(cmd[0], filter->write_ok) && has_write_perm) |
Adel Gadllah | 0b07de8 | 2008-06-26 13:48:27 +0200 | [diff] [blame] | 46 | return 0; |
| 47 | |
| 48 | return -EPERM; |
| 49 | } |
Adel Gadllah | 0b07de8 | 2008-06-26 13:48:27 +0200 | [diff] [blame] | 50 | EXPORT_SYMBOL(blk_verify_command); |
| 51 | |
| 52 | /* and now, the sysfs stuff */ |
FUJITA Tomonori | 4beab5c | 2008-07-26 18:03:25 +0900 | [diff] [blame] | 53 | static ssize_t rcf_cmds_show(struct blk_cmd_filter *filter, char *page, |
Adel Gadllah | 0b07de8 | 2008-06-26 13:48:27 +0200 | [diff] [blame] | 54 | int rw) |
| 55 | { |
| 56 | char *npage = page; |
| 57 | unsigned long *okbits; |
| 58 | int i; |
| 59 | |
| 60 | if (rw == READ) |
| 61 | okbits = filter->read_ok; |
| 62 | else |
| 63 | okbits = filter->write_ok; |
| 64 | |
| 65 | for (i = 0; i < BLK_SCSI_MAX_CMDS; i++) { |
| 66 | if (test_bit(i, okbits)) { |
Adel Gadllah | a4a7789 | 2008-08-16 14:21:06 +0900 | [diff] [blame] | 67 | npage += sprintf(npage, "0x%02x", i); |
Adel Gadllah | 0b07de8 | 2008-06-26 13:48:27 +0200 | [diff] [blame] | 68 | if (i < BLK_SCSI_MAX_CMDS - 1) |
| 69 | sprintf(npage++, " "); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | if (npage != page) |
| 74 | npage += sprintf(npage, "\n"); |
| 75 | |
| 76 | return npage - page; |
| 77 | } |
| 78 | |
FUJITA Tomonori | 4beab5c | 2008-07-26 18:03:25 +0900 | [diff] [blame] | 79 | static ssize_t rcf_readcmds_show(struct blk_cmd_filter *filter, char *page) |
Adel Gadllah | 0b07de8 | 2008-06-26 13:48:27 +0200 | [diff] [blame] | 80 | { |
| 81 | return rcf_cmds_show(filter, page, READ); |
| 82 | } |
| 83 | |
FUJITA Tomonori | 4beab5c | 2008-07-26 18:03:25 +0900 | [diff] [blame] | 84 | static ssize_t rcf_writecmds_show(struct blk_cmd_filter *filter, |
Adel Gadllah | 0b07de8 | 2008-06-26 13:48:27 +0200 | [diff] [blame] | 85 | char *page) |
| 86 | { |
| 87 | return rcf_cmds_show(filter, page, WRITE); |
| 88 | } |
| 89 | |
FUJITA Tomonori | 4beab5c | 2008-07-26 18:03:25 +0900 | [diff] [blame] | 90 | static ssize_t rcf_cmds_store(struct blk_cmd_filter *filter, |
Adel Gadllah | 0b07de8 | 2008-06-26 13:48:27 +0200 | [diff] [blame] | 91 | const char *page, size_t count, int rw) |
| 92 | { |
Adel Gadllah | 0b07de8 | 2008-06-26 13:48:27 +0200 | [diff] [blame] | 93 | unsigned long okbits[BLK_SCSI_CMD_PER_LONG], *target_okbits; |
Adel Gadllah | a4a7789 | 2008-08-16 14:21:06 +0900 | [diff] [blame] | 94 | int cmd, set; |
| 95 | char *p, *status; |
Adel Gadllah | 0b07de8 | 2008-06-26 13:48:27 +0200 | [diff] [blame] | 96 | |
Adel Gadllah | a4a7789 | 2008-08-16 14:21:06 +0900 | [diff] [blame] | 97 | if (rw == READ) { |
| 98 | memcpy(&okbits, filter->read_ok, sizeof(okbits)); |
| 99 | target_okbits = filter->read_ok; |
| 100 | } else { |
| 101 | memcpy(&okbits, filter->write_ok, sizeof(okbits)); |
| 102 | target_okbits = filter->write_ok; |
Adel Gadllah | 0b07de8 | 2008-06-26 13:48:27 +0200 | [diff] [blame] | 103 | } |
| 104 | |
Adel Gadllah | a4a7789 | 2008-08-16 14:21:06 +0900 | [diff] [blame] | 105 | while ((p = strsep((char **)&page, " ")) != NULL) { |
| 106 | set = 1; |
Adel Gadllah | 0b07de8 | 2008-06-26 13:48:27 +0200 | [diff] [blame] | 107 | |
Adel Gadllah | a4a7789 | 2008-08-16 14:21:06 +0900 | [diff] [blame] | 108 | if (p[0] == '+') { |
| 109 | p++; |
| 110 | } else if (p[0] == '-') { |
| 111 | set = 0; |
| 112 | p++; |
| 113 | } |
| 114 | |
| 115 | cmd = simple_strtol(p, &status, 16); |
| 116 | |
| 117 | /* either of these cases means invalid input, so do nothing. */ |
| 118 | if ((status == p) || cmd >= BLK_SCSI_MAX_CMDS) |
| 119 | return -EINVAL; |
| 120 | |
| 121 | if (set) |
| 122 | __set_bit(cmd, okbits); |
| 123 | else |
| 124 | __clear_bit(cmd, okbits); |
| 125 | } |
| 126 | |
| 127 | memcpy(target_okbits, okbits, sizeof(okbits)); |
Adel Gadllah | 0b07de8 | 2008-06-26 13:48:27 +0200 | [diff] [blame] | 128 | return count; |
| 129 | } |
| 130 | |
FUJITA Tomonori | 4beab5c | 2008-07-26 18:03:25 +0900 | [diff] [blame] | 131 | static ssize_t rcf_readcmds_store(struct blk_cmd_filter *filter, |
Adel Gadllah | 0b07de8 | 2008-06-26 13:48:27 +0200 | [diff] [blame] | 132 | const char *page, size_t count) |
| 133 | { |
| 134 | return rcf_cmds_store(filter, page, count, READ); |
| 135 | } |
| 136 | |
FUJITA Tomonori | 4beab5c | 2008-07-26 18:03:25 +0900 | [diff] [blame] | 137 | static ssize_t rcf_writecmds_store(struct blk_cmd_filter *filter, |
Adel Gadllah | 0b07de8 | 2008-06-26 13:48:27 +0200 | [diff] [blame] | 138 | const char *page, size_t count) |
| 139 | { |
| 140 | return rcf_cmds_store(filter, page, count, WRITE); |
| 141 | } |
| 142 | |
| 143 | struct rcf_sysfs_entry { |
| 144 | struct attribute attr; |
FUJITA Tomonori | 4beab5c | 2008-07-26 18:03:25 +0900 | [diff] [blame] | 145 | ssize_t (*show)(struct blk_cmd_filter *, char *); |
| 146 | ssize_t (*store)(struct blk_cmd_filter *, const char *, size_t); |
Adel Gadllah | 0b07de8 | 2008-06-26 13:48:27 +0200 | [diff] [blame] | 147 | }; |
| 148 | |
| 149 | static struct rcf_sysfs_entry rcf_readcmds_entry = { |
| 150 | .attr = { .name = "read_table", .mode = S_IRUGO | S_IWUSR }, |
| 151 | .show = rcf_readcmds_show, |
| 152 | .store = rcf_readcmds_store, |
| 153 | }; |
| 154 | |
| 155 | static struct rcf_sysfs_entry rcf_writecmds_entry = { |
| 156 | .attr = {.name = "write_table", .mode = S_IRUGO | S_IWUSR }, |
| 157 | .show = rcf_writecmds_show, |
| 158 | .store = rcf_writecmds_store, |
| 159 | }; |
| 160 | |
| 161 | static struct attribute *default_attrs[] = { |
| 162 | &rcf_readcmds_entry.attr, |
| 163 | &rcf_writecmds_entry.attr, |
| 164 | NULL, |
| 165 | }; |
| 166 | |
| 167 | #define to_rcf(atr) container_of((atr), struct rcf_sysfs_entry, attr) |
| 168 | |
| 169 | static ssize_t |
| 170 | rcf_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 171 | { |
| 172 | struct rcf_sysfs_entry *entry = to_rcf(attr); |
FUJITA Tomonori | 4beab5c | 2008-07-26 18:03:25 +0900 | [diff] [blame] | 173 | struct blk_cmd_filter *filter; |
Adel Gadllah | 0b07de8 | 2008-06-26 13:48:27 +0200 | [diff] [blame] | 174 | |
FUJITA Tomonori | 4beab5c | 2008-07-26 18:03:25 +0900 | [diff] [blame] | 175 | filter = container_of(kobj, struct blk_cmd_filter, kobj); |
Adel Gadllah | 0b07de8 | 2008-06-26 13:48:27 +0200 | [diff] [blame] | 176 | if (entry->show) |
| 177 | return entry->show(filter, page); |
| 178 | |
| 179 | return 0; |
| 180 | } |
| 181 | |
| 182 | static ssize_t |
| 183 | rcf_attr_store(struct kobject *kobj, struct attribute *attr, |
| 184 | const char *page, size_t length) |
| 185 | { |
| 186 | struct rcf_sysfs_entry *entry = to_rcf(attr); |
FUJITA Tomonori | 4beab5c | 2008-07-26 18:03:25 +0900 | [diff] [blame] | 187 | struct blk_cmd_filter *filter; |
Adel Gadllah | 0b07de8 | 2008-06-26 13:48:27 +0200 | [diff] [blame] | 188 | |
| 189 | if (!capable(CAP_SYS_RAWIO)) |
| 190 | return -EPERM; |
| 191 | |
| 192 | if (!entry->store) |
| 193 | return -EINVAL; |
| 194 | |
FUJITA Tomonori | 4beab5c | 2008-07-26 18:03:25 +0900 | [diff] [blame] | 195 | filter = container_of(kobj, struct blk_cmd_filter, kobj); |
Adel Gadllah | 0b07de8 | 2008-06-26 13:48:27 +0200 | [diff] [blame] | 196 | return entry->store(filter, page, length); |
| 197 | } |
| 198 | |
| 199 | static struct sysfs_ops rcf_sysfs_ops = { |
| 200 | .show = rcf_attr_show, |
| 201 | .store = rcf_attr_store, |
| 202 | }; |
| 203 | |
| 204 | static struct kobj_type rcf_ktype = { |
| 205 | .sysfs_ops = &rcf_sysfs_ops, |
| 206 | .default_attrs = default_attrs, |
| 207 | }; |
| 208 | |
Adel Gadllah | 0b07de8 | 2008-06-26 13:48:27 +0200 | [diff] [blame] | 209 | int blk_register_filter(struct gendisk *disk) |
| 210 | { |
| 211 | int ret; |
FUJITA Tomonori | 4beab5c | 2008-07-26 18:03:25 +0900 | [diff] [blame] | 212 | struct blk_cmd_filter *filter = &disk->queue->cmd_filter; |
Adel Gadllah | 0b07de8 | 2008-06-26 13:48:27 +0200 | [diff] [blame] | 213 | struct kobject *parent = kobject_get(disk->holder_dir->parent); |
| 214 | |
| 215 | if (!parent) |
| 216 | return -ENODEV; |
| 217 | |
| 218 | ret = kobject_init_and_add(&filter->kobj, &rcf_ktype, parent, |
FUJITA Tomonori | abf5439 | 2008-08-16 14:10:05 +0900 | [diff] [blame] | 219 | "%s", "cmd_filter"); |
Adel Gadllah | 0b07de8 | 2008-06-26 13:48:27 +0200 | [diff] [blame] | 220 | |
| 221 | if (ret < 0) |
| 222 | return ret; |
| 223 | |
Adel Gadllah | 0b07de8 | 2008-06-26 13:48:27 +0200 | [diff] [blame] | 224 | return 0; |
| 225 | } |
FUJITA Tomonori | bb23b43 | 2008-08-29 11:47:07 +0200 | [diff] [blame] | 226 | EXPORT_SYMBOL(blk_register_filter); |
Adel Gadllah | 0b07de8 | 2008-06-26 13:48:27 +0200 | [diff] [blame] | 227 | |
| 228 | void blk_unregister_filter(struct gendisk *disk) |
| 229 | { |
FUJITA Tomonori | 4beab5c | 2008-07-26 18:03:25 +0900 | [diff] [blame] | 230 | struct blk_cmd_filter *filter = &disk->queue->cmd_filter; |
Adel Gadllah | 0b07de8 | 2008-06-26 13:48:27 +0200 | [diff] [blame] | 231 | |
| 232 | kobject_put(&filter->kobj); |
| 233 | kobject_put(disk->holder_dir->parent); |
| 234 | } |
FUJITA Tomonori | bb23b43 | 2008-08-29 11:47:07 +0200 | [diff] [blame] | 235 | EXPORT_SYMBOL(blk_unregister_filter); |