Skip to content

Commit 9fd91a9

Browse files
djwongdchinner
authored andcommitted
vfs: strengthen checking of file range inputs to generic_remap_checks
File range remapping, if allowed to run past the destination file's EOF, is an optimization on a regular file write. Regular file writes that extend the file length are subject to various constraints which are not checked by range cloning. This is a correctness problem because we're never allowed to touch ranges that the page cache can't support (s_maxbytes); we're not supposed to deal with large offsets (MAX_NON_LFS) if O_LARGEFILE isn't set; and we must obey resource limits (RLIMIT_FSIZE). Therefore, add these checks to the new generic_remap_checks function so that we curtail unexpected behavior. Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com> Reviewed-by: Amir Goldstein <amir73il@gmail.com> Reviewed-by: Christoph Hellwig <hch@lst.de> Signed-off-by: Dave Chinner <david@fromorbit.com>
1 parent 2c5773f commit 9fd91a9

File tree

1 file changed

+52
-32
lines changed

1 file changed

+52
-32
lines changed

mm/filemap.c

Lines changed: 52 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2915,6 +2915,42 @@ struct page *read_cache_page_gfp(struct address_space *mapping,
29152915
}
29162916
EXPORT_SYMBOL(read_cache_page_gfp);
29172917

2918+
/*
2919+
* Don't operate on ranges the page cache doesn't support, and don't exceed the
2920+
* LFS limits. If pos is under the limit it becomes a short access. If it
2921+
* exceeds the limit we return -EFBIG.
2922+
*/
2923+
static int generic_access_check_limits(struct file *file, loff_t pos,
2924+
loff_t *count)
2925+
{
2926+
struct inode *inode = file->f_mapping->host;
2927+
loff_t max_size = inode->i_sb->s_maxbytes;
2928+
2929+
if (!(file->f_flags & O_LARGEFILE))
2930+
max_size = MAX_NON_LFS;
2931+
2932+
if (unlikely(pos >= max_size))
2933+
return -EFBIG;
2934+
*count = min(*count, max_size - pos);
2935+
return 0;
2936+
}
2937+
2938+
static int generic_write_check_limits(struct file *file, loff_t pos,
2939+
loff_t *count)
2940+
{
2941+
loff_t limit = rlimit(RLIMIT_FSIZE);
2942+
2943+
if (limit != RLIM_INFINITY) {
2944+
if (pos >= limit) {
2945+
send_sig(SIGXFSZ, current, 0);
2946+
return -EFBIG;
2947+
}
2948+
*count = min(*count, limit - pos);
2949+
}
2950+
2951+
return generic_access_check_limits(file, pos, count);
2952+
}
2953+
29182954
/*
29192955
* Performs necessary checks before doing a write
29202956
*
@@ -2926,8 +2962,8 @@ inline ssize_t generic_write_checks(struct kiocb *iocb, struct iov_iter *from)
29262962
{
29272963
struct file *file = iocb->ki_filp;
29282964
struct inode *inode = file->f_mapping->host;
2929-
unsigned long limit = rlimit(RLIMIT_FSIZE);
2930-
loff_t pos;
2965+
loff_t count;
2966+
int ret;
29312967

29322968
if (!iov_iter_count(from))
29332969
return 0;
@@ -2936,40 +2972,15 @@ inline ssize_t generic_write_checks(struct kiocb *iocb, struct iov_iter *from)
29362972
if (iocb->ki_flags & IOCB_APPEND)
29372973
iocb->ki_pos = i_size_read(inode);
29382974

2939-
pos = iocb->ki_pos;
2940-
29412975
if ((iocb->ki_flags & IOCB_NOWAIT) && !(iocb->ki_flags & IOCB_DIRECT))
29422976
return -EINVAL;
29432977

2944-
if (limit != RLIM_INFINITY) {
2945-
if (iocb->ki_pos >= limit) {
2946-
send_sig(SIGXFSZ, current, 0);
2947-
return -EFBIG;
2948-
}
2949-
iov_iter_truncate(from, limit - (unsigned long)pos);
2950-
}
2951-
2952-
/*
2953-
* LFS rule
2954-
*/
2955-
if (unlikely(pos + iov_iter_count(from) > MAX_NON_LFS &&
2956-
!(file->f_flags & O_LARGEFILE))) {
2957-
if (pos >= MAX_NON_LFS)
2958-
return -EFBIG;
2959-
iov_iter_truncate(from, MAX_NON_LFS - (unsigned long)pos);
2960-
}
2961-
2962-
/*
2963-
* Are we about to exceed the fs block limit ?
2964-
*
2965-
* If we have written data it becomes a short write. If we have
2966-
* exceeded without writing data we send a signal and return EFBIG.
2967-
* Linus frestrict idea will clean these up nicely..
2968-
*/
2969-
if (unlikely(pos >= inode->i_sb->s_maxbytes))
2970-
return -EFBIG;
2978+
count = iov_iter_count(from);
2979+
ret = generic_write_check_limits(file, iocb->ki_pos, &count);
2980+
if (ret)
2981+
return ret;
29712982

2972-
iov_iter_truncate(from, inode->i_sb->s_maxbytes - pos);
2983+
iov_iter_truncate(from, count);
29732984
return iov_iter_count(from);
29742985
}
29752986
EXPORT_SYMBOL(generic_write_checks);
@@ -2991,6 +3002,7 @@ int generic_remap_checks(struct file *file_in, loff_t pos_in,
29913002
uint64_t bcount;
29923003
loff_t size_in, size_out;
29933004
loff_t bs = inode_out->i_sb->s_blocksize;
3005+
int ret;
29943006

29953007
/* The start of both ranges must be aligned to an fs block. */
29963008
if (!IS_ALIGNED(pos_in, bs) || !IS_ALIGNED(pos_out, bs))
@@ -3014,6 +3026,14 @@ int generic_remap_checks(struct file *file_in, loff_t pos_in,
30143026
return -EINVAL;
30153027
count = min(count, size_in - (uint64_t)pos_in);
30163028

3029+
ret = generic_access_check_limits(file_in, pos_in, &count);
3030+
if (ret)
3031+
return ret;
3032+
3033+
ret = generic_write_check_limits(file_out, pos_out, &count);
3034+
if (ret)
3035+
return ret;
3036+
30173037
/*
30183038
* If the user wanted us to link to the infile's EOF, round up to the
30193039
* next block boundary for this check.

0 commit comments

Comments
 (0)