negative seek offsets in VFS
My x86-64 users are complaining again that they cannot reach kernel text addresses in /dev/kmem. The reason is that they are negative and the the VFS read and seek code just EINVALs them. For seek I could fix it in drivers/char/mem.c, but for read/pread/write etc. it needs VFS changes.
I dont quite get why they are there anyways, the super block has max file size field and checking against that should be enough for all the filesystems, no?
Opinions?
-Andi
on den 25.05.2005 Klokka 18:39 (+0200) skreiv Andi Kleen:
Quoted 9 lines
My x86-64 users are complaining again that they cannot reach kernel text addresses in
/dev/kmem. The reason is that they are negative and the the VFS read and seek code just EINVALs them. For seek I could fix it indrivers/char/mem.c, but for read/pread/write etc. it needs VFS changes.I dont quite get why they are there anyways, the super block has max file size field and checking against that should be enough for all the filesystems, no?
Isn't /dev/kmem overriding the default llseek()?
AFAICS, drivers/char/mem.c defines "memory_lseek()" for precisely the above reason.
Cheers,
Trond
Quoted 7 lines · Trond Myklebust
on den 25.05.2005 Klokka 18:39 (+0200) skreiv Andi Kleen:
Quoted 6 lines
My x86-64 users are complaining again that they cannot reach kernel text addresses in
/dev/kmem. The reason is that they are negative and the the VFS read and seek code just EINVALs them. For seek I could fix it indrivers/char/mem.c, but for read/pread/write etc. it needs VFS changes.
Quoted 8 lines
Quoted 3 lines
I dont quite get why they are there anyways, the super block has max file size field and checking against that should be enough for all the filesystems, no?
Isn't
/dev/kmemoverriding the defaultllseek()?AFAICS,
drivers/char/mem.cdefines "memory_lseek()" for precisely the above reason.
Yes, but that is not enough. read/write have these checks too, even pread/pwrite.
-Andi
Quoted 3 lines
My x86-64 users are complaining again that they cannot reach kernel text addresses in
/dev/kmem. The reason is that they are negative and the the VFS read and seek code just EINVALs them.
Come now -- the kernel addresses are not negative, and neither is any file offset.
You apparently mean that when you coerce a kernel address which exceeds the range of a file offset type into a file offset type, it comes out negative.
Quoted 3 lines
I dont quite get why they are there anyways, the super block has max file size field and checking against that should be enough for all the filesystems, no?
But this isn't about exceeding a maximum file size -- it's about exceeding the range of offsets that is representable in this C data type.
So I guess the real question is why is the loff_t type signed, thereby making it incapable of representing sufficiently large offsets? The answer is that there are POSIX interfaces that overload a single data structure as both a file offset or size and a status code. If a loff_t value is positive, it is a file offset, but if it's negative, it's a status code. Consider lseek -- if you allowed a negative offset and just declared that it stands for the large positive offset you'd get if you coerced it to an unsigned 64 bit integer, then how would you tell a success from a failure in the return code?
Quoted 6 lines · Bryan Henderson
Quoted 3 lines
My x86-64 users are complaining again that they cannot reach kernel text addresses in
/dev/kmem. The reason is that they are negative and the the VFS read and seek code just EINVALs them.Come now -- the kernel addresses are not negative, and neither is any file offset.
x86-64 addresses are negative.
Quoted 4 lines
You apparently mean that when you coerce a kernel address which exceeds the range of a file offset type into a file offset type, it comes out negative.
No I meant what I wrote.
Quoted 9 lines
So I guess the real question is why is the
loff_ttype signed, thereby making it incapable of representing sufficiently large offsets? The answer is that there are POSIX interfaces that overload a single data structure as both a file offset or size and a status code. If aloff_tvalue is positive, it is a file offset, but if it's negative, it's a status code. Consider lseek -- if you allowed a negative offset and just declared that it stands for the large positive offset you'd get if you coerced it to an unsigned 64 bit integer, then how would you tell a success from a failure in the return code?
The same as any other Linux kernel interfaces do it. The range from -4096 to -1 is reserved for error codes, the rest is free to use.
This is what glibc uses too. It has nothing to do with POSIX.
-Andi
Quoted 10 lines · Andi Kleen
My x86-64 users are complaining again that they cannot reach kernel text addresses in
/dev/kmem. The reason is that they are negative and the the VFS read and seek code just EINVALs them. For seek I could fix it indrivers/char/mem.c, but for read/pread/write etc. it needs VFS changes.I dont quite get why they are there anyways, the super block has max file size field and checking against that should be enough for all the filesystems, no?
Most of the really bad cases are not in filesystems - devices tend to be much more broken. So no, check for max size doesn't help here.
We could add a check in
if (unlikely((pos < 0) || (loff_t) (pos + count) < 0))
goto Einval;for "no, it's really OK to do that for this file" without harming the fast path.
How about
if (unlikely((pos < 0) || (loff_t) (pos + count) < 0)) if (!(file->f_mode & FMODE_ANY_OFFSET)) goto Einval;
instead + adding
#define FMODE_ANY_OFFSET 16 /* we don't need any offset checks */ in fs.h + having kmem ->open() set it?
Linus, it's your code. Do you have any objections to the above?
Quoted 11 lines · Al Viro
How about
if (unlikely((pos < 0) || (loff_t) (pos + count) < 0)) if (!(file->f_mode & FMODE_ANY_OFFSET)) goto Einval;instead + adding
#defineFMODE_ANY_OFFSET16 /* we don't need any offset checks */ infs.h+ having kmem ->open() set it?Linus, it's your code. Do you have any objections to the above?
No. But if so, the test should be changed: right now it not only detects negative offsets, it also detects wrap-around. And I think that wrap-around is _always_ wrong.
I don't think there is a "positive loff_t", so I guess the code would have to be something like
/* Wraparound? */ if ((u64)(pos + count) < count) goto Einval; if ((loff_t) (pos + count) < 0) if (!(file->f_mode & FMODE_ANY_OFFSET)) goto Einval;
instead.
Linus
Quoted 6 lines
I don't think there is a "positive
loff_t", so I guess the code would have to be something like/* Wraparound? */ if ((u64)(pos + count) < count) goto Einval;
Sometimes I think we should have a nice inline asm macro for that that checks carry.
Quoted 4 lines
if ((loff_t) (pos + count) < 0) if (!(file->f_mode & FMODE_ANY_OFFSET)) goto Einval;
Looks good. But how to handle the broken devices viro worries about? I would prefer not to open any new security holes, but it is a bit too much code to audit all. Flag would be possible, but ugly.
Any other choice?
-Andi
Quoted 10 lines · Andi Kleen
Quoted 7 lines
I don't think there is a "positive
loff_t", so I guess the code would have to be something like/* Wraparound? */ if ((u64)(pos + count) < count) goto Einval;Sometimes I think we should have a nice inline asm macro for that that checks carry.
Well, we'd need one in every size, and it's not common enough for us to care about performance, so..
Quoted 7 lines
Quoted 3 lines
if ((loff_t) (pos + count) < 0) if (!(file->f_mode & FMODE_ANY_OFFSET)) goto Einval;Looks good. But how to handle the broken devices viro worries about? I would prefer not to open any new security holes, but it is a bit too much code to audit all. Flag would be possible, but ugly.
I would _only_ ever set that FMODE_ANY_OFFSET for /dev/mem or other devices that are known to be ok. IOW, this is not something that ever gets set by default, and the user cannot set it.
Linus