Skip to content

Commit 69baa3a

Browse files
Loic Poulainaxboe
authored andcommitted
block: Deny writable memory mapping if block is read-only
User should not be able to write block device if it is read-only at block level (e.g force_ro attribute). This is ensured in the regular fops write operation (blkdev_write_iter) but not when writing via user mapping (mmap), allowing user to actually write a read-only block device via a PROT_WRITE mapping. Example: This can lead to integrity issue of eMMC boot partition (e.g mmcblk0boot0) which is read-only by default. To fix this issue, simply deny shared writable mapping if the block is readonly. Note: Block remains writable if switch to read-only is performed after the initial mapping, but this is expected behavior according to commit a32e236 ("Partially revert "block: fail op_is_write() requests to read-only partitions"")'. Signed-off-by: Loic Poulain <loic.poulain@linaro.org> Reviewed-by: Christoph Hellwig <hch@lst.de> Link: https://lore.kernel.org/r/20230510074223.991297-1-loic.poulain@linaro.org Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent c99bff3 commit 69baa3a

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

block/fops.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -678,14 +678,24 @@ static long blkdev_fallocate(struct file *file, int mode, loff_t start,
678678
return error;
679679
}
680680

681+
static int blkdev_mmap(struct file *file, struct vm_area_struct *vma)
682+
{
683+
struct inode *bd_inode = bdev_file_inode(file);
684+
685+
if (bdev_read_only(I_BDEV(bd_inode)))
686+
return generic_file_readonly_mmap(file, vma);
687+
688+
return generic_file_mmap(file, vma);
689+
}
690+
681691
const struct file_operations def_blk_fops = {
682692
.open = blkdev_open,
683693
.release = blkdev_close,
684694
.llseek = blkdev_llseek,
685695
.read_iter = blkdev_read_iter,
686696
.write_iter = blkdev_write_iter,
687697
.iopoll = iocb_bio_iopoll,
688-
.mmap = generic_file_mmap,
698+
.mmap = blkdev_mmap,
689699
.fsync = blkdev_fsync,
690700
.unlocked_ioctl = blkdev_ioctl,
691701
#ifdef CONFIG_COMPAT

0 commit comments

Comments
 (0)