• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <linux/blkdev.h>
2 #include <linux/blkpg.h>
3 #include <linux/blktrace_api.h>
4 #include <linux/cdrom.h>
5 #include <linux/compat.h>
6 #include <linux/elevator.h>
7 #include <linux/hdreg.h>
8 #include <linux/pr.h>
9 #include <linux/slab.h>
10 #include <linux/syscalls.h>
11 #include <linux/types.h>
12 #include <linux/uaccess.h>
13 
compat_put_ushort(unsigned long arg,unsigned short val)14 static int compat_put_ushort(unsigned long arg, unsigned short val)
15 {
16 	return put_user(val, (unsigned short __user *)compat_ptr(arg));
17 }
18 
compat_put_int(unsigned long arg,int val)19 static int compat_put_int(unsigned long arg, int val)
20 {
21 	return put_user(val, (compat_int_t __user *)compat_ptr(arg));
22 }
23 
compat_put_uint(unsigned long arg,unsigned int val)24 static int compat_put_uint(unsigned long arg, unsigned int val)
25 {
26 	return put_user(val, (compat_uint_t __user *)compat_ptr(arg));
27 }
28 
compat_put_long(unsigned long arg,long val)29 static int compat_put_long(unsigned long arg, long val)
30 {
31 	return put_user(val, (compat_long_t __user *)compat_ptr(arg));
32 }
33 
compat_put_ulong(unsigned long arg,compat_ulong_t val)34 static int compat_put_ulong(unsigned long arg, compat_ulong_t val)
35 {
36 	return put_user(val, (compat_ulong_t __user *)compat_ptr(arg));
37 }
38 
compat_put_u64(unsigned long arg,u64 val)39 static int compat_put_u64(unsigned long arg, u64 val)
40 {
41 	return put_user(val, (compat_u64 __user *)compat_ptr(arg));
42 }
43 
44 struct compat_hd_geometry {
45 	unsigned char heads;
46 	unsigned char sectors;
47 	unsigned short cylinders;
48 	u32 start;
49 };
50 
compat_hdio_getgeo(struct gendisk * disk,struct block_device * bdev,struct compat_hd_geometry __user * ugeo)51 static int compat_hdio_getgeo(struct gendisk *disk, struct block_device *bdev,
52 			struct compat_hd_geometry __user *ugeo)
53 {
54 	struct hd_geometry geo;
55 	int ret;
56 
57 	if (!ugeo)
58 		return -EINVAL;
59 	if (!disk->fops->getgeo)
60 		return -ENOTTY;
61 
62 	memset(&geo, 0, sizeof(geo));
63 	/*
64 	 * We need to set the startsect first, the driver may
65 	 * want to override it.
66 	 */
67 	geo.start = get_start_sect(bdev);
68 	ret = disk->fops->getgeo(bdev, &geo);
69 	if (ret)
70 		return ret;
71 
72 	ret = copy_to_user(ugeo, &geo, 4);
73 	ret |= put_user(geo.start, &ugeo->start);
74 	if (ret)
75 		ret = -EFAULT;
76 
77 	return ret;
78 }
79 
compat_hdio_ioctl(struct block_device * bdev,fmode_t mode,unsigned int cmd,unsigned long arg)80 static int compat_hdio_ioctl(struct block_device *bdev, fmode_t mode,
81 		unsigned int cmd, unsigned long arg)
82 {
83 	mm_segment_t old_fs = get_fs();
84 	unsigned long kval;
85 	unsigned int __user *uvp;
86 	int error;
87 
88 	set_fs(KERNEL_DS);
89 	error = __blkdev_driver_ioctl(bdev, mode,
90 				cmd, (unsigned long)(&kval));
91 	set_fs(old_fs);
92 
93 	if (error == 0) {
94 		uvp = compat_ptr(arg);
95 		if (put_user(kval, uvp))
96 			error = -EFAULT;
97 	}
98 	return error;
99 }
100 
101 struct compat_cdrom_read_audio {
102 	union cdrom_addr	addr;
103 	u8			addr_format;
104 	compat_int_t		nframes;
105 	compat_caddr_t		buf;
106 };
107 
108 struct compat_cdrom_generic_command {
109 	unsigned char	cmd[CDROM_PACKET_SIZE];
110 	compat_caddr_t	buffer;
111 	compat_uint_t	buflen;
112 	compat_int_t	stat;
113 	compat_caddr_t	sense;
114 	unsigned char	data_direction;
115 	compat_int_t	quiet;
116 	compat_int_t	timeout;
117 	compat_caddr_t	reserved[1];
118 };
119 
compat_cdrom_read_audio(struct block_device * bdev,fmode_t mode,unsigned int cmd,unsigned long arg)120 static int compat_cdrom_read_audio(struct block_device *bdev, fmode_t mode,
121 		unsigned int cmd, unsigned long arg)
122 {
123 	struct cdrom_read_audio __user *cdread_audio;
124 	struct compat_cdrom_read_audio __user *cdread_audio32;
125 	__u32 data;
126 	void __user *datap;
127 
128 	cdread_audio = compat_alloc_user_space(sizeof(*cdread_audio));
129 	cdread_audio32 = compat_ptr(arg);
130 
131 	if (copy_in_user(&cdread_audio->addr,
132 			 &cdread_audio32->addr,
133 			 (sizeof(*cdread_audio32) -
134 			  sizeof(compat_caddr_t))))
135 		return -EFAULT;
136 
137 	if (get_user(data, &cdread_audio32->buf))
138 		return -EFAULT;
139 	datap = compat_ptr(data);
140 	if (put_user(datap, &cdread_audio->buf))
141 		return -EFAULT;
142 
143 	return __blkdev_driver_ioctl(bdev, mode, cmd,
144 			(unsigned long)cdread_audio);
145 }
146 
compat_cdrom_generic_command(struct block_device * bdev,fmode_t mode,unsigned int cmd,unsigned long arg)147 static int compat_cdrom_generic_command(struct block_device *bdev, fmode_t mode,
148 		unsigned int cmd, unsigned long arg)
149 {
150 	struct cdrom_generic_command __user *cgc;
151 	struct compat_cdrom_generic_command __user *cgc32;
152 	u32 data;
153 	unsigned char dir;
154 	int itmp;
155 
156 	cgc = compat_alloc_user_space(sizeof(*cgc));
157 	cgc32 = compat_ptr(arg);
158 
159 	if (copy_in_user(&cgc->cmd, &cgc32->cmd, sizeof(cgc->cmd)) ||
160 	    get_user(data, &cgc32->buffer) ||
161 	    put_user(compat_ptr(data), &cgc->buffer) ||
162 	    copy_in_user(&cgc->buflen, &cgc32->buflen,
163 			 (sizeof(unsigned int) + sizeof(int))) ||
164 	    get_user(data, &cgc32->sense) ||
165 	    put_user(compat_ptr(data), &cgc->sense) ||
166 	    get_user(dir, &cgc32->data_direction) ||
167 	    put_user(dir, &cgc->data_direction) ||
168 	    get_user(itmp, &cgc32->quiet) ||
169 	    put_user(itmp, &cgc->quiet) ||
170 	    get_user(itmp, &cgc32->timeout) ||
171 	    put_user(itmp, &cgc->timeout) ||
172 	    get_user(data, &cgc32->reserved[0]) ||
173 	    put_user(compat_ptr(data), &cgc->reserved[0]))
174 		return -EFAULT;
175 
176 	return __blkdev_driver_ioctl(bdev, mode, cmd, (unsigned long)cgc);
177 }
178 
179 struct compat_blkpg_ioctl_arg {
180 	compat_int_t op;
181 	compat_int_t flags;
182 	compat_int_t datalen;
183 	compat_caddr_t data;
184 };
185 
compat_blkpg_ioctl(struct block_device * bdev,fmode_t mode,unsigned int cmd,struct compat_blkpg_ioctl_arg __user * ua32)186 static int compat_blkpg_ioctl(struct block_device *bdev, fmode_t mode,
187 		unsigned int cmd, struct compat_blkpg_ioctl_arg __user *ua32)
188 {
189 	struct blkpg_ioctl_arg __user *a = compat_alloc_user_space(sizeof(*a));
190 	compat_caddr_t udata;
191 	compat_int_t n;
192 	int err;
193 
194 	err = get_user(n, &ua32->op);
195 	err |= put_user(n, &a->op);
196 	err |= get_user(n, &ua32->flags);
197 	err |= put_user(n, &a->flags);
198 	err |= get_user(n, &ua32->datalen);
199 	err |= put_user(n, &a->datalen);
200 	err |= get_user(udata, &ua32->data);
201 	err |= put_user(compat_ptr(udata), &a->data);
202 	if (err)
203 		return err;
204 
205 	return blkdev_ioctl(bdev, mode, cmd, (unsigned long)a);
206 }
207 
208 #define BLKBSZGET_32		_IOR(0x12, 112, int)
209 #define BLKBSZSET_32		_IOW(0x12, 113, int)
210 #define BLKGETSIZE64_32		_IOR(0x12, 114, int)
211 
compat_blkdev_driver_ioctl(struct block_device * bdev,fmode_t mode,unsigned cmd,unsigned long arg)212 static int compat_blkdev_driver_ioctl(struct block_device *bdev, fmode_t mode,
213 			unsigned cmd, unsigned long arg)
214 {
215 	switch (cmd) {
216 	case HDIO_GET_UNMASKINTR:
217 	case HDIO_GET_MULTCOUNT:
218 	case HDIO_GET_KEEPSETTINGS:
219 	case HDIO_GET_32BIT:
220 	case HDIO_GET_NOWERR:
221 	case HDIO_GET_DMA:
222 	case HDIO_GET_NICE:
223 	case HDIO_GET_WCACHE:
224 	case HDIO_GET_ACOUSTIC:
225 	case HDIO_GET_ADDRESS:
226 	case HDIO_GET_BUSSTATE:
227 		return compat_hdio_ioctl(bdev, mode, cmd, arg);
228 	case CDROMREADAUDIO:
229 		return compat_cdrom_read_audio(bdev, mode, cmd, arg);
230 	case CDROM_SEND_PACKET:
231 		return compat_cdrom_generic_command(bdev, mode, cmd, arg);
232 
233 	/*
234 	 * No handler required for the ones below, we just need to
235 	 * convert arg to a 64 bit pointer.
236 	 */
237 	case BLKSECTSET:
238 	/*
239 	 * 0x03 -- HD/IDE ioctl's used by hdparm and friends.
240 	 *         Some need translations, these do not.
241 	 */
242 	case HDIO_GET_IDENTITY:
243 	case HDIO_DRIVE_TASK:
244 	case HDIO_DRIVE_CMD:
245 	/* 0x330 is reserved -- it used to be HDIO_GETGEO_BIG */
246 	case 0x330:
247 	/* CDROM stuff */
248 	case CDROMPAUSE:
249 	case CDROMRESUME:
250 	case CDROMPLAYMSF:
251 	case CDROMPLAYTRKIND:
252 	case CDROMREADTOCHDR:
253 	case CDROMREADTOCENTRY:
254 	case CDROMSTOP:
255 	case CDROMSTART:
256 	case CDROMEJECT:
257 	case CDROMVOLCTRL:
258 	case CDROMSUBCHNL:
259 	case CDROMMULTISESSION:
260 	case CDROM_GET_MCN:
261 	case CDROMRESET:
262 	case CDROMVOLREAD:
263 	case CDROMSEEK:
264 	case CDROMPLAYBLK:
265 	case CDROMCLOSETRAY:
266 	case CDROM_DISC_STATUS:
267 	case CDROM_CHANGER_NSLOTS:
268 	case CDROM_GET_CAPABILITY:
269 	/* Ignore cdrom.h about these next 5 ioctls, they absolutely do
270 	 * not take a struct cdrom_read, instead they take a struct cdrom_msf
271 	 * which is compatible.
272 	 */
273 	case CDROMREADMODE2:
274 	case CDROMREADMODE1:
275 	case CDROMREADRAW:
276 	case CDROMREADCOOKED:
277 	case CDROMREADALL:
278 	/* DVD ioctls */
279 	case DVD_READ_STRUCT:
280 	case DVD_WRITE_STRUCT:
281 	case DVD_AUTH:
282 		arg = (unsigned long)compat_ptr(arg);
283 	/* These intepret arg as an unsigned long, not as a pointer,
284 	 * so we must not do compat_ptr() conversion. */
285 	case HDIO_SET_MULTCOUNT:
286 	case HDIO_SET_UNMASKINTR:
287 	case HDIO_SET_KEEPSETTINGS:
288 	case HDIO_SET_32BIT:
289 	case HDIO_SET_NOWERR:
290 	case HDIO_SET_DMA:
291 	case HDIO_SET_PIO_MODE:
292 	case HDIO_SET_NICE:
293 	case HDIO_SET_WCACHE:
294 	case HDIO_SET_ACOUSTIC:
295 	case HDIO_SET_BUSSTATE:
296 	case HDIO_SET_ADDRESS:
297 	case CDROMEJECT_SW:
298 	case CDROM_SET_OPTIONS:
299 	case CDROM_CLEAR_OPTIONS:
300 	case CDROM_SELECT_SPEED:
301 	case CDROM_SELECT_DISC:
302 	case CDROM_MEDIA_CHANGED:
303 	case CDROM_DRIVE_STATUS:
304 	case CDROM_LOCKDOOR:
305 	case CDROM_DEBUG:
306 		break;
307 	default:
308 		/* unknown ioctl number */
309 		return -ENOIOCTLCMD;
310 	}
311 
312 	return __blkdev_driver_ioctl(bdev, mode, cmd, arg);
313 }
314 
315 /* Most of the generic ioctls are handled in the normal fallback path.
316    This assumes the blkdev's low level compat_ioctl always returns
317    ENOIOCTLCMD for unknown ioctls. */
compat_blkdev_ioctl(struct file * file,unsigned cmd,unsigned long arg)318 long compat_blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg)
319 {
320 	int ret = -ENOIOCTLCMD;
321 	struct inode *inode = file->f_mapping->host;
322 	struct block_device *bdev = inode->i_bdev;
323 	struct gendisk *disk = bdev->bd_disk;
324 	fmode_t mode = file->f_mode;
325 	struct backing_dev_info *bdi;
326 	loff_t size;
327 	unsigned int max_sectors;
328 
329 	/*
330 	 * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
331 	 * to updated it before every ioctl.
332 	 */
333 	if (file->f_flags & O_NDELAY)
334 		mode |= FMODE_NDELAY;
335 	else
336 		mode &= ~FMODE_NDELAY;
337 
338 	switch (cmd) {
339 	case HDIO_GETGEO:
340 		return compat_hdio_getgeo(disk, bdev, compat_ptr(arg));
341 	case BLKPBSZGET:
342 		return compat_put_uint(arg, bdev_physical_block_size(bdev));
343 	case BLKIOMIN:
344 		return compat_put_uint(arg, bdev_io_min(bdev));
345 	case BLKIOOPT:
346 		return compat_put_uint(arg, bdev_io_opt(bdev));
347 	case BLKALIGNOFF:
348 		return compat_put_int(arg, bdev_alignment_offset(bdev));
349 	case BLKDISCARDZEROES:
350 		return compat_put_uint(arg, bdev_discard_zeroes_data(bdev));
351 	case BLKFLSBUF:
352 	case BLKROSET:
353 	case BLKDISCARD:
354 	case BLKSECDISCARD:
355 	case BLKZEROOUT:
356 	/*
357 	 * the ones below are implemented in blkdev_locked_ioctl,
358 	 * but we call blkdev_ioctl, which gets the lock for us
359 	 */
360 	case BLKRRPART:
361 		return blkdev_ioctl(bdev, mode, cmd,
362 				(unsigned long)compat_ptr(arg));
363 	case BLKBSZSET_32:
364 		return blkdev_ioctl(bdev, mode, BLKBSZSET,
365 				(unsigned long)compat_ptr(arg));
366 	case BLKPG:
367 		return compat_blkpg_ioctl(bdev, mode, cmd, compat_ptr(arg));
368 	case BLKRAGET:
369 	case BLKFRAGET:
370 		if (!arg)
371 			return -EINVAL;
372 		bdi = blk_get_backing_dev_info(bdev);
373 		return compat_put_long(arg,
374 				       (bdi->ra_pages * PAGE_CACHE_SIZE) / 512);
375 	case BLKROGET: /* compatible */
376 		return compat_put_int(arg, bdev_read_only(bdev) != 0);
377 	case BLKBSZGET_32: /* get the logical block size (cf. BLKSSZGET) */
378 		return compat_put_int(arg, block_size(bdev));
379 	case BLKSSZGET: /* get block device hardware sector size */
380 		return compat_put_int(arg, bdev_logical_block_size(bdev));
381 	case BLKSECTGET:
382 		max_sectors = min_t(unsigned int, USHRT_MAX,
383 				    queue_max_sectors(bdev_get_queue(bdev)));
384 		return compat_put_ushort(arg, max_sectors);
385 	case BLKROTATIONAL:
386 		return compat_put_ushort(arg,
387 					 !blk_queue_nonrot(bdev_get_queue(bdev)));
388 	case BLKRASET: /* compatible, but no compat_ptr (!) */
389 	case BLKFRASET:
390 		if (!capable(CAP_SYS_ADMIN))
391 			return -EACCES;
392 		bdi = blk_get_backing_dev_info(bdev);
393 		bdi->ra_pages = (arg * 512) / PAGE_CACHE_SIZE;
394 		return 0;
395 	case BLKGETSIZE:
396 		size = i_size_read(bdev->bd_inode);
397 		if ((size >> 9) > ~0UL)
398 			return -EFBIG;
399 		return compat_put_ulong(arg, size >> 9);
400 
401 	case BLKGETSIZE64_32:
402 		return compat_put_u64(arg, i_size_read(bdev->bd_inode));
403 
404 	case BLKTRACESETUP32:
405 	case BLKTRACESTART: /* compatible */
406 	case BLKTRACESTOP:  /* compatible */
407 	case BLKTRACETEARDOWN: /* compatible */
408 		ret = blk_trace_ioctl(bdev, cmd, compat_ptr(arg));
409 		return ret;
410 	case IOC_PR_REGISTER:
411 	case IOC_PR_RESERVE:
412 	case IOC_PR_RELEASE:
413 	case IOC_PR_PREEMPT:
414 	case IOC_PR_PREEMPT_ABORT:
415 	case IOC_PR_CLEAR:
416 		return blkdev_ioctl(bdev, mode, cmd,
417 				(unsigned long)compat_ptr(arg));
418 	default:
419 		if (disk->fops->compat_ioctl)
420 			ret = disk->fops->compat_ioctl(bdev, mode, cmd, arg);
421 		if (ret == -ENOIOCTLCMD)
422 			ret = compat_blkdev_driver_ioctl(bdev, mode, cmd, arg);
423 		return ret;
424 	}
425 }
426