cmd-filter.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. #include <linux/list.h>
  20. #include <linux/genhd.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/parser.h>
  23. #include <linux/capability.h>
  24. #include <linux/bitops.h>
  25. #include <scsi/scsi.h>
  26. #include <linux/cdrom.h>
  27. int blk_cmd_filter_verify_command(struct blk_scsi_cmd_filter *filter,
  28. unsigned char *cmd, mode_t *f_mode)
  29. {
  30. /* root can do any command. */
  31. if (capable(CAP_SYS_RAWIO))
  32. return 0;
  33. /* if there's no filter set, assume we're filtering everything out */
  34. if (!filter)
  35. return -EPERM;
  36. /* Anybody who can open the device can do a read-safe command */
  37. if (test_bit(cmd[0], filter->read_ok))
  38. return 0;
  39. /* Write-safe commands require a writable open */
  40. if (test_bit(cmd[0], filter->write_ok) && (*f_mode & FMODE_WRITE))
  41. return 0;
  42. return -EPERM;
  43. }
  44. EXPORT_SYMBOL(blk_cmd_filter_verify_command);
  45. int blk_verify_command(struct file *file, unsigned char *cmd)
  46. {
  47. struct gendisk *disk;
  48. struct inode *inode;
  49. if (!file)
  50. return -EINVAL;
  51. inode = file->f_dentry->d_inode;
  52. if (!inode)
  53. return -EINVAL;
  54. disk = inode->i_bdev->bd_disk;
  55. return blk_cmd_filter_verify_command(&disk->cmd_filter,
  56. cmd, &file->f_mode);
  57. }
  58. EXPORT_SYMBOL(blk_verify_command);
  59. /* and now, the sysfs stuff */
  60. static ssize_t rcf_cmds_show(struct blk_scsi_cmd_filter *filter, char *page,
  61. int rw)
  62. {
  63. char *npage = page;
  64. unsigned long *okbits;
  65. int i;
  66. if (rw == READ)
  67. okbits = filter->read_ok;
  68. else
  69. okbits = filter->write_ok;
  70. for (i = 0; i < BLK_SCSI_MAX_CMDS; i++) {
  71. if (test_bit(i, okbits)) {
  72. sprintf(npage, "%02x", i);
  73. npage += 2;
  74. if (i < BLK_SCSI_MAX_CMDS - 1)
  75. sprintf(npage++, " ");
  76. }
  77. }
  78. if (npage != page)
  79. npage += sprintf(npage, "\n");
  80. return npage - page;
  81. }
  82. static ssize_t rcf_readcmds_show(struct blk_scsi_cmd_filter *filter, char *page)
  83. {
  84. return rcf_cmds_show(filter, page, READ);
  85. }
  86. static ssize_t rcf_writecmds_show(struct blk_scsi_cmd_filter *filter,
  87. char *page)
  88. {
  89. return rcf_cmds_show(filter, page, WRITE);
  90. }
  91. static ssize_t rcf_cmds_store(struct blk_scsi_cmd_filter *filter,
  92. const char *page, size_t count, int rw)
  93. {
  94. ssize_t ret = 0;
  95. unsigned long okbits[BLK_SCSI_CMD_PER_LONG], *target_okbits;
  96. int cmd, status, len;
  97. substring_t ss;
  98. memset(&okbits, 0, sizeof(okbits));
  99. for (len = strlen(page); len > 0; len -= 3) {
  100. if (len < 2)
  101. break;
  102. ss.from = (char *) page + ret;
  103. ss.to = (char *) page + ret + 2;
  104. ret += 3;
  105. status = match_hex(&ss, &cmd);
  106. /* either of these cases means invalid input, so do nothing. */
  107. if (status || cmd >= BLK_SCSI_MAX_CMDS)
  108. return -EINVAL;
  109. __set_bit(cmd, okbits);
  110. }
  111. if (rw == READ)
  112. target_okbits = filter->read_ok;
  113. else
  114. target_okbits = filter->write_ok;
  115. memmove(target_okbits, okbits, sizeof(okbits));
  116. return count;
  117. }
  118. static ssize_t rcf_readcmds_store(struct blk_scsi_cmd_filter *filter,
  119. const char *page, size_t count)
  120. {
  121. return rcf_cmds_store(filter, page, count, READ);
  122. }
  123. static ssize_t rcf_writecmds_store(struct blk_scsi_cmd_filter *filter,
  124. const char *page, size_t count)
  125. {
  126. return rcf_cmds_store(filter, page, count, WRITE);
  127. }
  128. struct rcf_sysfs_entry {
  129. struct attribute attr;
  130. ssize_t (*show)(struct blk_scsi_cmd_filter *, char *);
  131. ssize_t (*store)(struct blk_scsi_cmd_filter *, const char *, size_t);
  132. };
  133. static struct rcf_sysfs_entry rcf_readcmds_entry = {
  134. .attr = { .name = "read_table", .mode = S_IRUGO | S_IWUSR },
  135. .show = rcf_readcmds_show,
  136. .store = rcf_readcmds_store,
  137. };
  138. static struct rcf_sysfs_entry rcf_writecmds_entry = {
  139. .attr = {.name = "write_table", .mode = S_IRUGO | S_IWUSR },
  140. .show = rcf_writecmds_show,
  141. .store = rcf_writecmds_store,
  142. };
  143. static struct attribute *default_attrs[] = {
  144. &rcf_readcmds_entry.attr,
  145. &rcf_writecmds_entry.attr,
  146. NULL,
  147. };
  148. #define to_rcf(atr) container_of((atr), struct rcf_sysfs_entry, attr)
  149. static ssize_t
  150. rcf_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
  151. {
  152. struct rcf_sysfs_entry *entry = to_rcf(attr);
  153. struct blk_scsi_cmd_filter *filter;
  154. filter = container_of(kobj, struct blk_scsi_cmd_filter, kobj);
  155. if (entry->show)
  156. return entry->show(filter, page);
  157. return 0;
  158. }
  159. static ssize_t
  160. rcf_attr_store(struct kobject *kobj, struct attribute *attr,
  161. const char *page, size_t length)
  162. {
  163. struct rcf_sysfs_entry *entry = to_rcf(attr);
  164. struct blk_scsi_cmd_filter *filter;
  165. if (!capable(CAP_SYS_RAWIO))
  166. return -EPERM;
  167. if (!entry->store)
  168. return -EINVAL;
  169. filter = container_of(kobj, struct blk_scsi_cmd_filter, kobj);
  170. return entry->store(filter, page, length);
  171. }
  172. static struct sysfs_ops rcf_sysfs_ops = {
  173. .show = rcf_attr_show,
  174. .store = rcf_attr_store,
  175. };
  176. static struct kobj_type rcf_ktype = {
  177. .sysfs_ops = &rcf_sysfs_ops,
  178. .default_attrs = default_attrs,
  179. };
  180. #ifndef MAINTENANCE_IN_CMD
  181. #define MAINTENANCE_IN_CMD 0xa3
  182. #endif
  183. static void rcf_set_defaults(struct blk_scsi_cmd_filter *filter)
  184. {
  185. /* Basic read-only commands */
  186. __set_bit(TEST_UNIT_READY, filter->read_ok);
  187. __set_bit(REQUEST_SENSE, filter->read_ok);
  188. __set_bit(READ_6, filter->read_ok);
  189. __set_bit(READ_10, filter->read_ok);
  190. __set_bit(READ_12, filter->read_ok);
  191. __set_bit(READ_16, filter->read_ok);
  192. __set_bit(READ_BUFFER, filter->read_ok);
  193. __set_bit(READ_DEFECT_DATA, filter->read_ok);
  194. __set_bit(READ_CAPACITY, filter->read_ok);
  195. __set_bit(READ_LONG, filter->read_ok);
  196. __set_bit(INQUIRY, filter->read_ok);
  197. __set_bit(MODE_SENSE, filter->read_ok);
  198. __set_bit(MODE_SENSE_10, filter->read_ok);
  199. __set_bit(LOG_SENSE, filter->read_ok);
  200. __set_bit(START_STOP, filter->read_ok);
  201. __set_bit(GPCMD_VERIFY_10, filter->read_ok);
  202. __set_bit(VERIFY_16, filter->read_ok);
  203. __set_bit(REPORT_LUNS, filter->read_ok);
  204. __set_bit(SERVICE_ACTION_IN, filter->read_ok);
  205. __set_bit(RECEIVE_DIAGNOSTIC, filter->read_ok);
  206. __set_bit(MAINTENANCE_IN_CMD, filter->read_ok);
  207. __set_bit(GPCMD_READ_BUFFER_CAPACITY, filter->read_ok);
  208. /* Audio CD commands */
  209. __set_bit(GPCMD_PLAY_CD, filter->read_ok);
  210. __set_bit(GPCMD_PLAY_AUDIO_10, filter->read_ok);
  211. __set_bit(GPCMD_PLAY_AUDIO_MSF, filter->read_ok);
  212. __set_bit(GPCMD_PLAY_AUDIO_TI, filter->read_ok);
  213. __set_bit(GPCMD_PAUSE_RESUME, filter->read_ok);
  214. /* CD/DVD data reading */
  215. __set_bit(GPCMD_READ_CD, filter->read_ok);
  216. __set_bit(GPCMD_READ_CD_MSF, filter->read_ok);
  217. __set_bit(GPCMD_READ_DISC_INFO, filter->read_ok);
  218. __set_bit(GPCMD_READ_CDVD_CAPACITY, filter->read_ok);
  219. __set_bit(GPCMD_READ_DVD_STRUCTURE, filter->read_ok);
  220. __set_bit(GPCMD_READ_HEADER, filter->read_ok);
  221. __set_bit(GPCMD_READ_TRACK_RZONE_INFO, filter->read_ok);
  222. __set_bit(GPCMD_READ_SUBCHANNEL, filter->read_ok);
  223. __set_bit(GPCMD_READ_TOC_PMA_ATIP, filter->read_ok);
  224. __set_bit(GPCMD_REPORT_KEY, filter->read_ok);
  225. __set_bit(GPCMD_SCAN, filter->read_ok);
  226. __set_bit(GPCMD_GET_CONFIGURATION, filter->read_ok);
  227. __set_bit(GPCMD_READ_FORMAT_CAPACITIES, filter->read_ok);
  228. __set_bit(GPCMD_GET_EVENT_STATUS_NOTIFICATION, filter->read_ok);
  229. __set_bit(GPCMD_GET_PERFORMANCE, filter->read_ok);
  230. __set_bit(GPCMD_SEEK, filter->read_ok);
  231. __set_bit(GPCMD_STOP_PLAY_SCAN, filter->read_ok);
  232. /* Basic writing commands */
  233. __set_bit(WRITE_6, filter->write_ok);
  234. __set_bit(WRITE_10, filter->write_ok);
  235. __set_bit(WRITE_VERIFY, filter->write_ok);
  236. __set_bit(WRITE_12, filter->write_ok);
  237. __set_bit(WRITE_VERIFY_12, filter->write_ok);
  238. __set_bit(WRITE_16, filter->write_ok);
  239. __set_bit(WRITE_LONG, filter->write_ok);
  240. __set_bit(WRITE_LONG_2, filter->write_ok);
  241. __set_bit(ERASE, filter->write_ok);
  242. __set_bit(GPCMD_MODE_SELECT_10, filter->write_ok);
  243. __set_bit(MODE_SELECT, filter->write_ok);
  244. __set_bit(LOG_SELECT, filter->write_ok);
  245. __set_bit(GPCMD_BLANK, filter->write_ok);
  246. __set_bit(GPCMD_CLOSE_TRACK, filter->write_ok);
  247. __set_bit(GPCMD_FLUSH_CACHE, filter->write_ok);
  248. __set_bit(GPCMD_FORMAT_UNIT, filter->write_ok);
  249. __set_bit(GPCMD_REPAIR_RZONE_TRACK, filter->write_ok);
  250. __set_bit(GPCMD_RESERVE_RZONE_TRACK, filter->write_ok);
  251. __set_bit(GPCMD_SEND_DVD_STRUCTURE, filter->write_ok);
  252. __set_bit(GPCMD_SEND_EVENT, filter->write_ok);
  253. __set_bit(GPCMD_SEND_KEY, filter->write_ok);
  254. __set_bit(GPCMD_SEND_OPC, filter->write_ok);
  255. __set_bit(GPCMD_SEND_CUE_SHEET, filter->write_ok);
  256. __set_bit(GPCMD_SET_SPEED, filter->write_ok);
  257. __set_bit(GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL, filter->write_ok);
  258. __set_bit(GPCMD_LOAD_UNLOAD, filter->write_ok);
  259. __set_bit(GPCMD_SET_STREAMING, filter->write_ok);
  260. }
  261. int blk_register_filter(struct gendisk *disk)
  262. {
  263. int ret;
  264. struct blk_scsi_cmd_filter *filter = &disk->cmd_filter;
  265. struct kobject *parent = kobject_get(disk->holder_dir->parent);
  266. if (!parent)
  267. return -ENODEV;
  268. ret = kobject_init_and_add(&filter->kobj, &rcf_ktype, parent,
  269. "%s", "cmd_filter");
  270. if (ret < 0)
  271. return ret;
  272. rcf_set_defaults(filter);
  273. return 0;
  274. }
  275. void blk_unregister_filter(struct gendisk *disk)
  276. {
  277. struct blk_scsi_cmd_filter *filter = &disk->cmd_filter;
  278. kobject_put(&filter->kobj);
  279. kobject_put(disk->holder_dir->parent);
  280. }