Skip to content

Commit 853fe1b

Browse files
keesaxboe
authored andcommitted
cdrom: Make device operations read-only
Since function tables are a common target for attackers, it's best to keep them in read-only memory. As such, this makes the CDROM device ops tables const. This drops additionally n_minors, since it isn't used meaningfully, and sets the only user of cdrom_dummy_generic_packet explicitly so the variables can all be const. Inspired by similar changes in grsecurity/PaX. Signed-off-by: Kees Cook <keescook@chromium.org> Acked-by: David S. Miller <davem@davemloft.net> Signed-off-by: Jens Axboe <axboe@fb.com>
1 parent d1a987f commit 853fe1b

7 files changed

Lines changed: 37 additions & 45 deletions

File tree

‎Documentation/cdrom/cdrom-standard.tex‎

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,6 @@
249249
unsigned\ long);\cr
250250
\noalign{\medskip}
251251
&const\ int& capability;& capability flags \cr
252-
&int& n_minors;& number of active minor devices \cr
253252
\};\cr
254253
}
255254
$$
@@ -258,13 +257,7 @@
258257
function is not implemented, however, this $struct$ should contain a
259258
NULL instead. The $capability$ flags specify the capabilities of the
260259
\cdrom\ hardware and/or low-level \cdrom\ driver when a \cdrom\ drive
261-
is registered with the \UCD. The value $n_minors$ should be a positive
262-
value indicating the number of minor devices that are supported by
263-
the low-level device driver, normally~1. Although these two variables
264-
are `informative' rather than `operational,' they are included in
265-
$cdrom_device_ops$ because they describe the capability of the {\em
266-
driver\/} rather than the {\em drive}. Nomenclature has always been
267-
difficult in computer programming.
260+
is registered with the \UCD.
268261

269262
Note that most functions have fewer parameters than their
270263
$blkdev_fops$ counterparts. This is because very little of the

‎drivers/block/paride/pcd.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ static const struct block_device_operations pcd_bdops = {
273273
.check_events = pcd_block_check_events,
274274
};
275275

276-
static struct cdrom_device_ops pcd_dops = {
276+
static const struct cdrom_device_ops pcd_dops = {
277277
.open = pcd_open,
278278
.release = pcd_release,
279279
.drive_status = pcd_drive_status,

‎drivers/cdrom/cdrom.c‎

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -342,8 +342,8 @@ static void cdrom_sysctl_register(void);
342342

343343
static LIST_HEAD(cdrom_list);
344344

345-
static int cdrom_dummy_generic_packet(struct cdrom_device_info *cdi,
346-
struct packet_command *cgc)
345+
int cdrom_dummy_generic_packet(struct cdrom_device_info *cdi,
346+
struct packet_command *cgc)
347347
{
348348
if (cgc->sense) {
349349
cgc->sense->sense_key = 0x05;
@@ -354,6 +354,7 @@ static int cdrom_dummy_generic_packet(struct cdrom_device_info *cdi,
354354
cgc->stat = -EIO;
355355
return -EIO;
356356
}
357+
EXPORT_SYMBOL(cdrom_dummy_generic_packet);
357358

358359
static int cdrom_flush_cache(struct cdrom_device_info *cdi)
359360
{
@@ -371,7 +372,7 @@ static int cdrom_flush_cache(struct cdrom_device_info *cdi)
371372
static int cdrom_get_disc_info(struct cdrom_device_info *cdi,
372373
disc_information *di)
373374
{
374-
struct cdrom_device_ops *cdo = cdi->ops;
375+
const struct cdrom_device_ops *cdo = cdi->ops;
375376
struct packet_command cgc;
376377
int ret, buflen;
377378

@@ -586,7 +587,7 @@ static int cdrom_mrw_set_lba_space(struct cdrom_device_info *cdi, int space)
586587
int register_cdrom(struct cdrom_device_info *cdi)
587588
{
588589
static char banner_printed;
589-
struct cdrom_device_ops *cdo = cdi->ops;
590+
const struct cdrom_device_ops *cdo = cdi->ops;
590591
int *change_capability = (int *)&cdo->capability; /* hack */
591592

592593
cd_dbg(CD_OPEN, "entering register_cdrom\n");
@@ -610,7 +611,6 @@ int register_cdrom(struct cdrom_device_info *cdi)
610611
ENSURE(reset, CDC_RESET);
611612
ENSURE(generic_packet, CDC_GENERIC_PACKET);
612613
cdi->mc_flags = 0;
613-
cdo->n_minors = 0;
614614
cdi->options = CDO_USE_FFLAGS;
615615

616616
if (autoclose == 1 && CDROM_CAN(CDC_CLOSE_TRAY))
@@ -630,8 +630,7 @@ int register_cdrom(struct cdrom_device_info *cdi)
630630
else
631631
cdi->cdda_method = CDDA_OLD;
632632

633-
if (!cdo->generic_packet)
634-
cdo->generic_packet = cdrom_dummy_generic_packet;
633+
WARN_ON(!cdo->generic_packet);
635634

636635
cd_dbg(CD_REG_UNREG, "drive \"/dev/%s\" registered\n", cdi->name);
637636
mutex_lock(&cdrom_mutex);
@@ -652,7 +651,6 @@ void unregister_cdrom(struct cdrom_device_info *cdi)
652651
if (cdi->exit)
653652
cdi->exit(cdi);
654653

655-
cdi->ops->n_minors--;
656654
cd_dbg(CD_REG_UNREG, "drive \"/dev/%s\" unregistered\n", cdi->name);
657655
}
658656

@@ -1036,7 +1034,7 @@ static
10361034
int open_for_data(struct cdrom_device_info *cdi)
10371035
{
10381036
int ret;
1039-
struct cdrom_device_ops *cdo = cdi->ops;
1037+
const struct cdrom_device_ops *cdo = cdi->ops;
10401038
tracktype tracks;
10411039
cd_dbg(CD_OPEN, "entering open_for_data\n");
10421040
/* Check if the driver can report drive status. If it can, we
@@ -1198,8 +1196,8 @@ int cdrom_open(struct cdrom_device_info *cdi, struct block_device *bdev,
11981196
/* This code is similar to that in open_for_data. The routine is called
11991197
whenever an audio play operation is requested.
12001198
*/
1201-
static int check_for_audio_disc(struct cdrom_device_info * cdi,
1202-
struct cdrom_device_ops * cdo)
1199+
static int check_for_audio_disc(struct cdrom_device_info *cdi,
1200+
const struct cdrom_device_ops *cdo)
12031201
{
12041202
int ret;
12051203
tracktype tracks;
@@ -1254,7 +1252,7 @@ static int check_for_audio_disc(struct cdrom_device_info * cdi,
12541252

12551253
void cdrom_release(struct cdrom_device_info *cdi, fmode_t mode)
12561254
{
1257-
struct cdrom_device_ops *cdo = cdi->ops;
1255+
const struct cdrom_device_ops *cdo = cdi->ops;
12581256
int opened_for_data;
12591257

12601258
cd_dbg(CD_CLOSE, "entering cdrom_release\n");
@@ -1294,7 +1292,7 @@ static int cdrom_read_mech_status(struct cdrom_device_info *cdi,
12941292
struct cdrom_changer_info *buf)
12951293
{
12961294
struct packet_command cgc;
1297-
struct cdrom_device_ops *cdo = cdi->ops;
1295+
const struct cdrom_device_ops *cdo = cdi->ops;
12981296
int length;
12991297

13001298
/*
@@ -1643,7 +1641,7 @@ static int dvd_do_auth(struct cdrom_device_info *cdi, dvd_authinfo *ai)
16431641
int ret;
16441642
u_char buf[20];
16451643
struct packet_command cgc;
1646-
struct cdrom_device_ops *cdo = cdi->ops;
1644+
const struct cdrom_device_ops *cdo = cdi->ops;
16471645
rpc_state_t rpc_state;
16481646

16491647
memset(buf, 0, sizeof(buf));
@@ -1791,7 +1789,7 @@ static int dvd_read_physical(struct cdrom_device_info *cdi, dvd_struct *s,
17911789
{
17921790
unsigned char buf[21], *base;
17931791
struct dvd_layer *layer;
1794-
struct cdrom_device_ops *cdo = cdi->ops;
1792+
const struct cdrom_device_ops *cdo = cdi->ops;
17951793
int ret, layer_num = s->physical.layer_num;
17961794

17971795
if (layer_num >= DVD_LAYERS)
@@ -1842,7 +1840,7 @@ static int dvd_read_copyright(struct cdrom_device_info *cdi, dvd_struct *s,
18421840
{
18431841
int ret;
18441842
u_char buf[8];
1845-
struct cdrom_device_ops *cdo = cdi->ops;
1843+
const struct cdrom_device_ops *cdo = cdi->ops;
18461844

18471845
init_cdrom_command(cgc, buf, sizeof(buf), CGC_DATA_READ);
18481846
cgc->cmd[0] = GPCMD_READ_DVD_STRUCTURE;
@@ -1866,7 +1864,7 @@ static int dvd_read_disckey(struct cdrom_device_info *cdi, dvd_struct *s,
18661864
{
18671865
int ret, size;
18681866
u_char *buf;
1869-
struct cdrom_device_ops *cdo = cdi->ops;
1867+
const struct cdrom_device_ops *cdo = cdi->ops;
18701868

18711869
size = sizeof(s->disckey.value) + 4;
18721870

@@ -1894,7 +1892,7 @@ static int dvd_read_bca(struct cdrom_device_info *cdi, dvd_struct *s,
18941892
{
18951893
int ret, size = 4 + 188;
18961894
u_char *buf;
1897-
struct cdrom_device_ops *cdo = cdi->ops;
1895+
const struct cdrom_device_ops *cdo = cdi->ops;
18981896

18991897
buf = kmalloc(size, GFP_KERNEL);
19001898
if (!buf)
@@ -1928,7 +1926,7 @@ static int dvd_read_manufact(struct cdrom_device_info *cdi, dvd_struct *s,
19281926
{
19291927
int ret = 0, size;
19301928
u_char *buf;
1931-
struct cdrom_device_ops *cdo = cdi->ops;
1929+
const struct cdrom_device_ops *cdo = cdi->ops;
19321930

19331931
size = sizeof(s->manufact.value) + 4;
19341932

@@ -1995,7 +1993,7 @@ int cdrom_mode_sense(struct cdrom_device_info *cdi,
19951993
struct packet_command *cgc,
19961994
int page_code, int page_control)
19971995
{
1998-
struct cdrom_device_ops *cdo = cdi->ops;
1996+
const struct cdrom_device_ops *cdo = cdi->ops;
19991997

20001998
memset(cgc->cmd, 0, sizeof(cgc->cmd));
20011999

@@ -2010,7 +2008,7 @@ int cdrom_mode_sense(struct cdrom_device_info *cdi,
20102008
int cdrom_mode_select(struct cdrom_device_info *cdi,
20112009
struct packet_command *cgc)
20122010
{
2013-
struct cdrom_device_ops *cdo = cdi->ops;
2011+
const struct cdrom_device_ops *cdo = cdi->ops;
20142012

20152013
memset(cgc->cmd, 0, sizeof(cgc->cmd));
20162014
memset(cgc->buffer, 0, 2);
@@ -2025,7 +2023,7 @@ int cdrom_mode_select(struct cdrom_device_info *cdi,
20252023
static int cdrom_read_subchannel(struct cdrom_device_info *cdi,
20262024
struct cdrom_subchnl *subchnl, int mcn)
20272025
{
2028-
struct cdrom_device_ops *cdo = cdi->ops;
2026+
const struct cdrom_device_ops *cdo = cdi->ops;
20292027
struct packet_command cgc;
20302028
char buffer[32];
20312029
int ret;
@@ -2073,7 +2071,7 @@ static int cdrom_read_cd(struct cdrom_device_info *cdi,
20732071
struct packet_command *cgc, int lba,
20742072
int blocksize, int nblocks)
20752073
{
2076-
struct cdrom_device_ops *cdo = cdi->ops;
2074+
const struct cdrom_device_ops *cdo = cdi->ops;
20772075

20782076
memset(&cgc->cmd, 0, sizeof(cgc->cmd));
20792077
cgc->cmd[0] = GPCMD_READ_10;
@@ -2093,7 +2091,7 @@ static int cdrom_read_block(struct cdrom_device_info *cdi,
20932091
struct packet_command *cgc,
20942092
int lba, int nblocks, int format, int blksize)
20952093
{
2096-
struct cdrom_device_ops *cdo = cdi->ops;
2094+
const struct cdrom_device_ops *cdo = cdi->ops;
20972095

20982096
memset(&cgc->cmd, 0, sizeof(cgc->cmd));
20992097
cgc->cmd[0] = GPCMD_READ_CD;
@@ -2764,7 +2762,7 @@ static int cdrom_ioctl_audioctl(struct cdrom_device_info *cdi,
27642762
*/
27652763
static int cdrom_switch_blocksize(struct cdrom_device_info *cdi, int size)
27662764
{
2767-
struct cdrom_device_ops *cdo = cdi->ops;
2765+
const struct cdrom_device_ops *cdo = cdi->ops;
27682766
struct packet_command cgc;
27692767
struct modesel_head mh;
27702768

@@ -2790,7 +2788,7 @@ static int cdrom_switch_blocksize(struct cdrom_device_info *cdi, int size)
27902788
static int cdrom_get_track_info(struct cdrom_device_info *cdi,
27912789
__u16 track, __u8 type, track_information *ti)
27922790
{
2793-
struct cdrom_device_ops *cdo = cdi->ops;
2791+
const struct cdrom_device_ops *cdo = cdi->ops;
27942792
struct packet_command cgc;
27952793
int ret, buflen;
27962794

@@ -3049,7 +3047,7 @@ static noinline int mmc_ioctl_cdrom_play_msf(struct cdrom_device_info *cdi,
30493047
void __user *arg,
30503048
struct packet_command *cgc)
30513049
{
3052-
struct cdrom_device_ops *cdo = cdi->ops;
3050+
const struct cdrom_device_ops *cdo = cdi->ops;
30533051
struct cdrom_msf msf;
30543052
cd_dbg(CD_DO_IOCTL, "entering CDROMPLAYMSF\n");
30553053
if (copy_from_user(&msf, (struct cdrom_msf __user *)arg, sizeof(msf)))
@@ -3069,7 +3067,7 @@ static noinline int mmc_ioctl_cdrom_play_blk(struct cdrom_device_info *cdi,
30693067
void __user *arg,
30703068
struct packet_command *cgc)
30713069
{
3072-
struct cdrom_device_ops *cdo = cdi->ops;
3070+
const struct cdrom_device_ops *cdo = cdi->ops;
30733071
struct cdrom_blk blk;
30743072
cd_dbg(CD_DO_IOCTL, "entering CDROMPLAYBLK\n");
30753073
if (copy_from_user(&blk, (struct cdrom_blk __user *)arg, sizeof(blk)))
@@ -3164,7 +3162,7 @@ static noinline int mmc_ioctl_cdrom_start_stop(struct cdrom_device_info *cdi,
31643162
struct packet_command *cgc,
31653163
int cmd)
31663164
{
3167-
struct cdrom_device_ops *cdo = cdi->ops;
3165+
const struct cdrom_device_ops *cdo = cdi->ops;
31683166
cd_dbg(CD_DO_IOCTL, "entering CDROMSTART/CDROMSTOP\n");
31693167
cgc->cmd[0] = GPCMD_START_STOP_UNIT;
31703168
cgc->cmd[1] = 1;
@@ -3177,7 +3175,7 @@ static noinline int mmc_ioctl_cdrom_pause_resume(struct cdrom_device_info *cdi,
31773175
struct packet_command *cgc,
31783176
int cmd)
31793177
{
3180-
struct cdrom_device_ops *cdo = cdi->ops;
3178+
const struct cdrom_device_ops *cdo = cdi->ops;
31813179
cd_dbg(CD_DO_IOCTL, "entering CDROMPAUSE/CDROMRESUME\n");
31823180
cgc->cmd[0] = GPCMD_PAUSE_RESUME;
31833181
cgc->cmd[8] = (cmd == CDROMRESUME) ? 1 : 0;

‎drivers/cdrom/gdrom.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -481,17 +481,17 @@ static int gdrom_audio_ioctl(struct cdrom_device_info *cdi, unsigned int cmd,
481481
return -EINVAL;
482482
}
483483

484-
static struct cdrom_device_ops gdrom_ops = {
484+
static const struct cdrom_device_ops gdrom_ops = {
485485
.open = gdrom_open,
486486
.release = gdrom_release,
487487
.drive_status = gdrom_drivestatus,
488488
.check_events = gdrom_check_events,
489489
.get_last_session = gdrom_get_last_session,
490490
.reset = gdrom_hardreset,
491491
.audio_ioctl = gdrom_audio_ioctl,
492+
.generic_packet = cdrom_dummy_generic_packet,
492493
.capability = CDC_MULTI_SESSION | CDC_MEDIA_CHANGED |
493494
CDC_RESET | CDC_DRIVE_STATUS | CDC_CD_R,
494-
.n_minors = 1,
495495
};
496496

497497
static int gdrom_bdops_open(struct block_device *bdev, fmode_t mode)

‎drivers/ide/ide-cd.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1166,7 +1166,7 @@ void ide_cdrom_update_speed(ide_drive_t *drive, u8 *buf)
11661166
CDC_CD_RW | CDC_DVD | CDC_DVD_R | CDC_DVD_RAM | CDC_GENERIC_PACKET | \
11671167
CDC_MO_DRIVE | CDC_MRW | CDC_MRW_W | CDC_RAM)
11681168

1169-
static struct cdrom_device_ops ide_cdrom_dops = {
1169+
static const struct cdrom_device_ops ide_cdrom_dops = {
11701170
.open = ide_cdrom_open_real,
11711171
.release = ide_cdrom_release_real,
11721172
.drive_status = ide_cdrom_drive_status,

‎drivers/scsi/sr.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ static unsigned int sr_check_events(struct cdrom_device_info *cdi,
117117
unsigned int clearing, int slot);
118118
static int sr_packet(struct cdrom_device_info *, struct packet_command *);
119119

120-
static struct cdrom_device_ops sr_dops = {
120+
static const struct cdrom_device_ops sr_dops = {
121121
.open = sr_open,
122122
.release = sr_release,
123123
.drive_status = sr_drive_status,

‎include/linux/cdrom.h‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ struct packet_command
3636

3737
/* Uniform cdrom data structures for cdrom.c */
3838
struct cdrom_device_info {
39-
struct cdrom_device_ops *ops; /* link to device_ops */
39+
const struct cdrom_device_ops *ops; /* link to device_ops */
4040
struct list_head list; /* linked list of all device_info */
4141
struct gendisk *disk; /* matching block layer disk */
4242
void *handle; /* driver-dependent data */
@@ -87,7 +87,6 @@ struct cdrom_device_ops {
8787

8888
/* driver specifications */
8989
const int capability; /* capability flags */
90-
int n_minors; /* number of active minor devices */
9190
/* handle uniform packets for scsi type devices (scsi,atapi) */
9291
int (*generic_packet) (struct cdrom_device_info *,
9392
struct packet_command *);
@@ -123,6 +122,8 @@ extern int cdrom_mode_sense(struct cdrom_device_info *cdi,
123122
int page_code, int page_control);
124123
extern void init_cdrom_command(struct packet_command *cgc,
125124
void *buffer, int len, int type);
125+
extern int cdrom_dummy_generic_packet(struct cdrom_device_info *cdi,
126+
struct packet_command *cgc);
126127

127128
/* The SCSI spec says there could be 256 slots. */
128129
#define CDROM_MAX_SLOTS 256

0 commit comments

Comments
 (0)