hc
2024-01-05 071106ecf68c401173c58808b1cf5f68cc50d390
kernel/drivers/ide/ide-ioctls.c
....@@ -1,11 +1,21 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * IDE ioctls handling.
34 */
45
6
+#include <linux/compat.h>
57 #include <linux/export.h>
68 #include <linux/hdreg.h>
79 #include <linux/ide.h>
810 #include <linux/slab.h>
11
+
12
+static int put_user_long(long val, unsigned long arg)
13
+{
14
+ if (in_compat_syscall())
15
+ return put_user(val, (compat_long_t __user *)compat_ptr(arg));
16
+
17
+ return put_user(val, (long __user *)arg);
18
+}
919
1020 static const struct ide_ioctl_devset ide_ioctl_settings[] = {
1121 { HDIO_GET_32BIT, HDIO_SET_32BIT, &ide_devset_io_32bit },
....@@ -36,10 +46,10 @@
3646 mutex_lock(&ide_setting_mtx);
3747 err = ds->get(drive);
3848 mutex_unlock(&ide_setting_mtx);
39
- return err >= 0 ? put_user(err, (long __user *)arg) : err;
49
+ return err >= 0 ? put_user_long(err, arg) : err;
4050
4151 set_val:
42
- if (bdev != bdev->bd_contains)
52
+ if (bdev_is_partition(bdev))
4353 err = -EINVAL;
4454 else {
4555 if (!capable(CAP_SYS_ADMIN))
....@@ -55,7 +65,7 @@
5565 EXPORT_SYMBOL_GPL(ide_setting_ioctl);
5666
5767 static int ide_get_identity_ioctl(ide_drive_t *drive, unsigned int cmd,
58
- unsigned long arg)
68
+ void __user *argp)
5969 {
6070 u16 *id = NULL;
6171 int size = (cmd == HDIO_GET_IDENTITY) ? (ATA_ID_WORDS * 2) : 142;
....@@ -76,7 +86,7 @@
7686 memcpy(id, drive->id, size);
7787 ata_id_to_hd_driveid(id);
7888
79
- if (copy_to_user((void __user *)arg, id, size))
89
+ if (copy_to_user(argp, id, size))
8090 rc = -EFAULT;
8191
8292 kfree(id);
....@@ -86,10 +96,10 @@
8696
8797 static int ide_get_nice_ioctl(ide_drive_t *drive, unsigned long arg)
8898 {
89
- return put_user((!!(drive->dev_flags & IDE_DFLAG_DSC_OVERLAP)
99
+ return put_user_long((!!(drive->dev_flags & IDE_DFLAG_DSC_OVERLAP)
90100 << IDE_NICE_DSC_OVERLAP) |
91101 (!!(drive->dev_flags & IDE_DFLAG_NICE1)
92
- << IDE_NICE_1), (long __user *)arg);
102
+ << IDE_NICE_1), arg);
93103 }
94104
95105 static int ide_set_nice_ioctl(ide_drive_t *drive, unsigned long arg)
....@@ -114,7 +124,7 @@
114124 return 0;
115125 }
116126
117
-static int ide_cmd_ioctl(ide_drive_t *drive, unsigned long arg)
127
+static int ide_cmd_ioctl(ide_drive_t *drive, void __user *argp)
118128 {
119129 u8 *buf = NULL;
120130 int bufsize = 0, err = 0;
....@@ -122,7 +132,7 @@
122132 struct ide_cmd cmd;
123133 struct ide_taskfile *tf = &cmd.tf;
124134
125
- if (NULL == (void *) arg) {
135
+ if (NULL == argp) {
126136 struct request *rq;
127137
128138 rq = blk_get_request(drive->queue, REQ_OP_DRV_IN, 0);
....@@ -134,7 +144,7 @@
134144 return err;
135145 }
136146
137
- if (copy_from_user(args, (void __user *)arg, 4))
147
+ if (copy_from_user(args, argp, 4))
138148 return -EFAULT;
139149
140150 memset(&cmd, 0, sizeof(cmd));
....@@ -180,19 +190,18 @@
180190 args[1] = tf->error;
181191 args[2] = tf->nsect;
182192 abort:
183
- if (copy_to_user((void __user *)arg, &args, 4))
193
+ if (copy_to_user(argp, &args, 4))
184194 err = -EFAULT;
185195 if (buf) {
186
- if (copy_to_user((void __user *)(arg + 4), buf, bufsize))
196
+ if (copy_to_user((argp + 4), buf, bufsize))
187197 err = -EFAULT;
188198 kfree(buf);
189199 }
190200 return err;
191201 }
192202
193
-static int ide_task_ioctl(ide_drive_t *drive, unsigned long arg)
203
+static int ide_task_ioctl(ide_drive_t *drive, void __user *p)
194204 {
195
- void __user *p = (void __user *)arg;
196205 int err = 0;
197206 u8 args[7];
198207 struct ide_cmd cmd;
....@@ -236,6 +245,10 @@
236245 unsigned int cmd, unsigned long arg)
237246 {
238247 int err;
248
+ void __user *argp = (void __user *)arg;
249
+
250
+ if (in_compat_syscall())
251
+ argp = compat_ptr(arg);
239252
240253 err = ide_setting_ioctl(drive, bdev, cmd, arg, ide_ioctl_settings);
241254 if (err != -EOPNOTSUPP)
....@@ -244,9 +257,9 @@
244257 switch (cmd) {
245258 case HDIO_OBSOLETE_IDENTITY:
246259 case HDIO_GET_IDENTITY:
247
- if (bdev != bdev->bd_contains)
260
+ if (bdev_is_partition(bdev))
248261 return -EINVAL;
249
- return ide_get_identity_ioctl(drive, cmd, arg);
262
+ return ide_get_identity_ioctl(drive, cmd, argp);
250263 case HDIO_GET_NICE:
251264 return ide_get_nice_ioctl(drive, arg);
252265 case HDIO_SET_NICE:
....@@ -257,6 +270,9 @@
257270 case HDIO_DRIVE_TASKFILE:
258271 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
259272 return -EACCES;
273
+ /* missing compat handler for HDIO_DRIVE_TASKFILE */
274
+ if (in_compat_syscall())
275
+ return -ENOTTY;
260276 if (drive->media == ide_disk)
261277 return ide_taskfile_ioctl(drive, arg);
262278 return -ENOMSG;
....@@ -264,11 +280,11 @@
264280 case HDIO_DRIVE_CMD:
265281 if (!capable(CAP_SYS_RAWIO))
266282 return -EACCES;
267
- return ide_cmd_ioctl(drive, arg);
283
+ return ide_cmd_ioctl(drive, argp);
268284 case HDIO_DRIVE_TASK:
269285 if (!capable(CAP_SYS_RAWIO))
270286 return -EACCES;
271
- return ide_task_ioctl(drive, arg);
287
+ return ide_task_ioctl(drive, argp);
272288 case HDIO_DRIVE_RESET:
273289 if (!capable(CAP_SYS_ADMIN))
274290 return -EACCES;
....@@ -276,7 +292,7 @@
276292 case HDIO_GET_BUSSTATE:
277293 if (!capable(CAP_SYS_ADMIN))
278294 return -EACCES;
279
- if (put_user(BUSSTATE_ON, (long __user *)arg))
295
+ if (put_user_long(BUSSTATE_ON, arg))
280296 return -EFAULT;
281297 return 0;
282298 case HDIO_SET_BUSSTATE: