Skip to content

Commit 4d03e3c

Browse files
Christoph HellwigAl Viro
authored andcommitted
fs: don't allow kernel reads and writes without iter ops
Don't allow calling ->read or ->write with set_fs as a preparation for killing off set_fs. All the instances that we use kernel_read/write on are using the iter ops already. If a file has both the regular ->read/->write methods and the iter variants those could have different semantics for messed up enough drivers. Also fails the kernel access to them in that case. Signed-off-by: Christoph Hellwig <[email protected]> Reviewed-by: Kees Cook <[email protected]> Signed-off-by: Al Viro <[email protected]>
1 parent 4bd6a73 commit 4d03e3c

File tree

1 file changed

+42
-25
lines changed

1 file changed

+42
-25
lines changed

fs/read_write.c

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -419,27 +419,41 @@ static ssize_t new_sync_read(struct file *filp, char __user *buf, size_t len, lo
419419
return ret;
420420
}
421421

422+
static int warn_unsupported(struct file *file, const char *op)
423+
{
424+
pr_warn_ratelimited(
425+
"kernel %s not supported for file %pD4 (pid: %d comm: %.20s)\n",
426+
op, file, current->pid, current->comm);
427+
return -EINVAL;
428+
}
429+
422430
ssize_t __kernel_read(struct file *file, void *buf, size_t count, loff_t *pos)
423431
{
424-
mm_segment_t old_fs = get_fs();
432+
struct kvec iov = {
433+
.iov_base = buf,
434+
.iov_len = min_t(size_t, count, MAX_RW_COUNT),
435+
};
436+
struct kiocb kiocb;
437+
struct iov_iter iter;
425438
ssize_t ret;
426439

427440
if (WARN_ON_ONCE(!(file->f_mode & FMODE_READ)))
428441
return -EINVAL;
429442
if (!(file->f_mode & FMODE_CAN_READ))
430443
return -EINVAL;
444+
/*
445+
* Also fail if ->read_iter and ->read are both wired up as that
446+
* implies very convoluted semantics.
447+
*/
448+
if (unlikely(!file->f_op->read_iter || file->f_op->read))
449+
return warn_unsupported(file, "read");
431450

432-
if (count > MAX_RW_COUNT)
433-
count = MAX_RW_COUNT;
434-
set_fs(KERNEL_DS);
435-
if (file->f_op->read)
436-
ret = file->f_op->read(file, (void __user *)buf, count, pos);
437-
else if (file->f_op->read_iter)
438-
ret = new_sync_read(file, (void __user *)buf, count, pos);
439-
else
440-
ret = -EINVAL;
441-
set_fs(old_fs);
451+
init_sync_kiocb(&kiocb, file);
452+
kiocb.ki_pos = *pos;
453+
iov_iter_kvec(&iter, READ, &iov, 1, iov.iov_len);
454+
ret = file->f_op->read_iter(&kiocb, &iter);
442455
if (ret > 0) {
456+
*pos = kiocb.ki_pos;
443457
fsnotify_access(file);
444458
add_rchar(current, ret);
445459
}
@@ -510,28 +524,31 @@ static ssize_t new_sync_write(struct file *filp, const char __user *buf, size_t
510524
/* caller is responsible for file_start_write/file_end_write */
511525
ssize_t __kernel_write(struct file *file, const void *buf, size_t count, loff_t *pos)
512526
{
513-
mm_segment_t old_fs;
514-
const char __user *p;
527+
struct kvec iov = {
528+
.iov_base = (void *)buf,
529+
.iov_len = min_t(size_t, count, MAX_RW_COUNT),
530+
};
531+
struct kiocb kiocb;
532+
struct iov_iter iter;
515533
ssize_t ret;
516534

517535
if (WARN_ON_ONCE(!(file->f_mode & FMODE_WRITE)))
518536
return -EBADF;
519537
if (!(file->f_mode & FMODE_CAN_WRITE))
520538
return -EINVAL;
539+
/*
540+
* Also fail if ->write_iter and ->write are both wired up as that
541+
* implies very convoluted semantics.
542+
*/
543+
if (unlikely(!file->f_op->write_iter || file->f_op->write))
544+
return warn_unsupported(file, "write");
521545

522-
old_fs = get_fs();
523-
set_fs(KERNEL_DS);
524-
p = (__force const char __user *)buf;
525-
if (count > MAX_RW_COUNT)
526-
count = MAX_RW_COUNT;
527-
if (file->f_op->write)
528-
ret = file->f_op->write(file, p, count, pos);
529-
else if (file->f_op->write_iter)
530-
ret = new_sync_write(file, p, count, pos);
531-
else
532-
ret = -EINVAL;
533-
set_fs(old_fs);
546+
init_sync_kiocb(&kiocb, file);
547+
kiocb.ki_pos = *pos;
548+
iov_iter_kvec(&iter, WRITE, &iov, 1, iov.iov_len);
549+
ret = file->f_op->write_iter(&kiocb, &iter);
534550
if (ret > 0) {
551+
*pos = kiocb.ki_pos;
535552
fsnotify_modify(file);
536553
add_wchar(current, ret);
537554
}

0 commit comments

Comments
 (0)