2007-02-20 21:58:58

by Chris Snook

[permalink] [raw]
Subject: [PATCH 0/2] 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


2007-02-20 22:02:57

by Chris Snook

[permalink] [raw]
Subject: [PATCH 1/2] use symbolic constants in generic lseek code

From: Chris Snook <[email protected]>

Convert magic numbers to SEEK_* values from fs.h

Signed-off-by: Chris Snook <[email protected]>
--
--- a/fs/read_write.c 2007-02-20 14:49:45.000000000 -0500
+++ b/fs/read_write.c 2007-02-20 16:48:39.000000000 -0500
@@ -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;

2007-02-20 22:17:58

by Chris Snook

[permalink] [raw]
Subject: [PATCH 2/2] use use SEEK_MAX to validate user lseek arguments

From: Chris Snook <[email protected]>

Add SEEK_MAX and use it to validate lseek arguments from userspace.

Signed-off-by: Chris Snook <[email protected]>
--
diff -urp b/fs/read_write.c c/fs/read_write.c
--- b/fs/read_write.c 2007-02-20 16:48:39.000000000 -0500
+++ c/fs/read_write.c 2007-02-20 16:55:46.000000000 -0500
@@ -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,
diff -urp b/include/linux/fs.h c/include/linux/fs.h
--- b/include/linux/fs.h 2007-02-20 14:49:46.000000000 -0500
+++ c/include/linux/fs.h 2007-02-20 16:54:30.000000000 -0500
@@ -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 {

2007-02-21 11:39:40

by David Howells

[permalink] [raw]
Subject: Re: [PATCH 0/2] use symbolic constants in generic lseek code

Chris Snook <[email protected]> wrote:

> 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.

Seems reasonable.

Acked-By: David Howells <[email protected]>