Skip to content

Commit 0f5c4c6

Browse files
Christoph Hellwigrafaeljw
authored andcommitted
PM / sleep: handle the compat case in snapshot_set_swap_area()
Use in_compat_syscall to copy directly from the 32-bit ABI structure. Signed-off-by: Christoph Hellwig <[email protected]> Signed-off-by: Rafael J. Wysocki <[email protected]>
1 parent 88a7755 commit 0f5c4c6

File tree

1 file changed

+22
-32
lines changed

1 file changed

+22
-32
lines changed

kernel/power/user.c

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -196,28 +196,44 @@ static ssize_t snapshot_write(struct file *filp, const char __user *buf,
196196
return res;
197197
}
198198

199+
struct compat_resume_swap_area {
200+
compat_loff_t offset;
201+
u32 dev;
202+
} __packed;
203+
199204
static int snapshot_set_swap_area(struct snapshot_data *data,
200205
void __user *argp)
201206
{
202-
struct resume_swap_area swap_area;
203207
sector_t offset;
204208
dev_t swdev;
205209

206210
if (swsusp_swap_in_use())
207211
return -EPERM;
208-
if (copy_from_user(&swap_area, argp, sizeof(swap_area)))
209-
return -EFAULT;
212+
213+
if (in_compat_syscall()) {
214+
struct compat_resume_swap_area swap_area;
215+
216+
if (copy_from_user(&swap_area, argp, sizeof(swap_area)))
217+
return -EFAULT;
218+
swdev = new_decode_dev(swap_area.dev);
219+
offset = swap_area.offset;
220+
} else {
221+
struct resume_swap_area swap_area;
222+
223+
if (copy_from_user(&swap_area, argp, sizeof(swap_area)))
224+
return -EFAULT;
225+
swdev = new_decode_dev(swap_area.dev);
226+
offset = swap_area.offset;
227+
}
210228

211229
/*
212230
* User space encodes device types as two-byte values,
213231
* so we need to recode them
214232
*/
215-
swdev = new_decode_dev(swap_area.dev);
216233
if (!swdev) {
217234
data->swap = -1;
218235
return -EINVAL;
219236
}
220-
offset = swap_area.offset;
221237
data->swap = swap_type_of(swdev, offset, NULL);
222238
if (data->swap < 0)
223239
return -ENODEV;
@@ -394,12 +410,6 @@ static long snapshot_ioctl(struct file *filp, unsigned int cmd,
394410
}
395411

396412
#ifdef CONFIG_COMPAT
397-
398-
struct compat_resume_swap_area {
399-
compat_loff_t offset;
400-
u32 dev;
401-
} __packed;
402-
403413
static long
404414
snapshot_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
405415
{
@@ -410,33 +420,13 @@ snapshot_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
410420
case SNAPSHOT_AVAIL_SWAP_SIZE:
411421
case SNAPSHOT_ALLOC_SWAP_PAGE:
412422
case SNAPSHOT_CREATE_IMAGE:
423+
case SNAPSHOT_SET_SWAP_AREA:
413424
return snapshot_ioctl(file, cmd,
414425
(unsigned long) compat_ptr(arg));
415-
416-
case SNAPSHOT_SET_SWAP_AREA: {
417-
struct compat_resume_swap_area __user *u_swap_area =
418-
compat_ptr(arg);
419-
struct resume_swap_area swap_area;
420-
mm_segment_t old_fs;
421-
int err;
422-
423-
err = get_user(swap_area.offset, &u_swap_area->offset);
424-
err |= get_user(swap_area.dev, &u_swap_area->dev);
425-
if (err)
426-
return -EFAULT;
427-
old_fs = get_fs();
428-
set_fs(KERNEL_DS);
429-
err = snapshot_ioctl(file, SNAPSHOT_SET_SWAP_AREA,
430-
(unsigned long) &swap_area);
431-
set_fs(old_fs);
432-
return err;
433-
}
434-
435426
default:
436427
return snapshot_ioctl(file, cmd, arg);
437428
}
438429
}
439-
440430
#endif /* CONFIG_COMPAT */
441431

442432
static const struct file_operations snapshot_fops = {

0 commit comments

Comments
 (0)