Skip to content

Commit 4a3956c

Browse files
hkamezawaAl Viro
authored andcommitted
vfs: introduce FMODE_UNSIGNED_OFFSET for allowing negative f_pos
Now, rw_verify_area() checsk f_pos is negative or not. And if negative, returns -EINVAL. But, some special files as /dev/(k)mem and /proc/<pid>/mem etc.. has negative offsets. And we can't do any access via read/write to the file(device). So introduce FMODE_UNSIGNED_OFFSET to allow negative file offsets. Signed-off-by: Wu Fengguang <fengguang.wu@intel.com> Signed-off-by: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com> Cc: Al Viro <viro@ZenIV.linux.org.uk> Cc: Heiko Carstens <heiko.carstens@de.ibm.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
1 parent ba10f48 commit 4a3956c

4 files changed

Lines changed: 33 additions & 4 deletions

File tree

‎drivers/char/mem.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -876,6 +876,10 @@ static int memory_open(struct inode *inode, struct file *filp)
876876
if (dev->dev_info)
877877
filp->f_mapping->backing_dev_info = dev->dev_info;
878878

879+
/* Is /dev/mem or /dev/kmem ? */
880+
if (dev->dev_info == &directly_mappable_cdev_bdi)
881+
filp->f_mode |= FMODE_UNSIGNED_OFFSET;
882+
879883
if (dev->fops->open)
880884
return dev->fops->open(inode, filp);
881885

‎fs/proc/base.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,8 @@ static const struct file_operations proc_single_file_operations = {
771771
static int mem_open(struct inode* inode, struct file* file)
772772
{
773773
file->private_data = (void*)((long)current->self_exec_id);
774+
/* OK to pass negative loff_t, we can catch out-of-range */
775+
file->f_mode |= FMODE_UNSIGNED_OFFSET;
774776
return 0;
775777
}
776778

‎fs/read_write.c‎

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,20 @@ const struct file_operations generic_ro_fops = {
3131

3232
EXPORT_SYMBOL(generic_ro_fops);
3333

34+
static int
35+
__negative_fpos_check(struct file *file, loff_t pos, size_t count)
36+
{
37+
/*
38+
* pos or pos+count is negative here, check overflow.
39+
* too big "count" will be caught in rw_verify_area().
40+
*/
41+
if ((pos < 0) && (pos + count < pos))
42+
return -EOVERFLOW;
43+
if (file->f_mode & FMODE_UNSIGNED_OFFSET)
44+
return 0;
45+
return -EINVAL;
46+
}
47+
3448
/**
3549
* generic_file_llseek_unlocked - lockless generic llseek implementation
3650
* @file: file structure to seek on
@@ -62,7 +76,9 @@ generic_file_llseek_unlocked(struct file *file, loff_t offset, int origin)
6276
break;
6377
}
6478

65-
if (offset < 0 || offset > inode->i_sb->s_maxbytes)
79+
if (offset < 0 && __negative_fpos_check(file, offset, 0))
80+
return -EINVAL;
81+
if (offset > inode->i_sb->s_maxbytes)
6682
return -EINVAL;
6783

6884
/* Special lock needed here? */
@@ -137,7 +153,7 @@ loff_t default_llseek(struct file *file, loff_t offset, int origin)
137153
offset += file->f_pos;
138154
}
139155
retval = -EINVAL;
140-
if (offset >= 0) {
156+
if (offset >= 0 || !__negative_fpos_check(file, offset, 0)) {
141157
if (offset != file->f_pos) {
142158
file->f_pos = offset;
143159
file->f_version = 0;
@@ -221,6 +237,7 @@ SYSCALL_DEFINE5(llseek, unsigned int, fd, unsigned long, offset_high,
221237
}
222238
#endif
223239

240+
224241
/*
225242
* rw_verify_area doesn't like huge counts. We limit
226243
* them to something that fits in "int" so that others
@@ -238,8 +255,11 @@ int rw_verify_area(int read_write, struct file *file, loff_t *ppos, size_t count
238255
if (unlikely((ssize_t) count < 0))
239256
return retval;
240257
pos = *ppos;
241-
if (unlikely((pos < 0) || (loff_t) (pos + count) < 0))
242-
return retval;
258+
if (unlikely((pos < 0) || (loff_t) (pos + count) < 0)) {
259+
retval = __negative_fpos_check(file, pos, count);
260+
if (retval)
261+
return retval;
262+
}
243263

244264
if (unlikely(inode->i_flock && mandatory_lock(inode))) {
245265
retval = locks_mandatory_area(

‎include/linux/fs.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ struct inodes_stat_t {
9292
/* Expect random access pattern */
9393
#define FMODE_RANDOM ((__force fmode_t)0x1000)
9494

95+
/* File is huge (eg. /dev/kmem): treat loff_t as unsigned */
96+
#define FMODE_UNSIGNED_OFFSET ((__force fmode_t)0x2000)
97+
9598
/* File was opened by fanotify and shouldn't generate fanotify events */
9699
#define FMODE_NONOTIFY ((__force fmode_t)0x1000000)
97100

0 commit comments

Comments
 (0)