use symbolic constants in generic lseek code
cover use symbolic constants in generic lseek code
The generic lseek code in fs/read_write.c uses hardcoded values for SEEK_{SET,CUR,END}.
Patch 1 fixes the case statements to use the symbolic constants in include/linux/fs.h, and should not be at all controversial.
Patch 2 adds a SEEK_MAX and uses it to validate user arguments. This makes the code a little cleaner and also enables future extensions (such as SEEK_DATA and SEEK_HOLE). If anyone has a problem with this, please speak up.
-- Chris
PATCH 1/2 use symbolic constants in generic lseek code
From: Chris Snook <[email protected]>
Convert magic numbers to SEEK_* values from fs.h
--
fs/read_write.c +6 -6
@@ -37,10 +37,10 @@ loff_t generic_file_llseek(struct file *mutex_lock (&inode->i_mutex);switch (origin) { -case 2 : +case SEEK_END : offset += inode->i_size;break ; -case 1 : +case SEEK_CUR : offset += file->f_pos; } retval = -EINVAL ;
@@ -63,10 +63,10 @@ loff_t remote_llseek(struct file *file,lock_kernel ();switch (origin) { -case 2 : +case SEEK_END : offset +=i_size_read (file->f_path.dentry->d_inode);break ; -case 1 : +case SEEK_CUR : offset += file->f_pos; } retval = -EINVAL ;
@@ -94,10 +94,10 @@ loff_t default_llseek(struct file *file,lock_kernel ();switch (origin) { -case 2 : +case SEEK_END : offset +=i_size_read (file->f_path.dentry->d_inode);break ; -case 1 : +case SEEK_CUR : offset += file->f_pos; } retval = -EINVAL ;
PATCH 2/2 use use SEEK_MAX to validate user lseek arguments
In reply to Chris SnookFrom: Chris Snook <[email protected]>
Add SEEK_MAX and use it to validate lseek arguments from userspace.
--
fs/read_write.c +2 -2
@@ -139,7 +139,7 @@ asmlinkage off_t sys_lseek(unsigned int goto bad; retval = -EINVAL ; -if (origin <=2 ) { +if (origin <=SEEK_MAX ) {loff_t res =vfs_llseek (file, offset, origin); retval = res;if (res != (loff_t )retval)
@@ -166,7 +166,7 @@ asmlinkage long sys_llseek(unsigned int goto bad; retval = -EINVAL ; -if (origin >2 ) +if (origin >SEEK_MAX ) goto out_putf; offset = vfs_llseek(file, ((loff_t) offset_high << 32) | offset_low,
include/linux/fs.h +1 -0
@@ -30,6 +30,7 @@#define SEEK_SET 0/* seek relative to beginning of file */ #define SEEK_CUR 1/* seek relative to current file position */ #define SEEK_END 2/* seek relative to end of file */ +#define SEEK_MAX SEEK_END/* And dynamically-tunable limits and defaults: */ struct files_stat_struct {
Quoted 6 lines · Chris Snook
Patch 1 fixes the case statements to use the symbolic constants in
include/linux/fs.h, and should not be at all controversial.Patch 2 adds a
SEEK_MAXand uses it to validate user arguments. This makes the code a little cleaner and also enables future extensions (such asSEEK_DATAandSEEK_HOLE). If anyone has a problem with this, please speak up.
Seems reasonable.