[NCLUG] newbie::Write to superblocks

Michael Dwyer mdwyer at sixthdimension.com
Tue Jul 31 16:09:06 MDT 2001


> int fd = open(afile, O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0644);
> int n1, n2, n3;
> ....
> off64_t offset = (off64_t) (n1*n2*n3*sizeof(float)); 
> if(lseek64(fd, offset, SEEK_SET) != offset) exit(-1);
> write(fd, blablah...);

Unless you run your programs as root, this should not 
have destroyed superblocks.  lseek (and presumably lseek64)
return EINVAL if you try to seek outside your file.  That
is, 
 if(lseek64()!=offset)
should have been true, lseek returning a -1, and your 
program would have exited -- not overwritten boot blocks.

So in summary, unless there is a problem in the underlying
OS code, that code should protect you from being able to
seek outside the current file.  In theory, the only way 
to destroy the superblocks from userland is to use lseek
and write on the special files /dev/hda1 (for instance).

Also, there isn't really a direct correlation between file
position and disk position.  It is possible (and likely)
that your file is written to non-consecutive blocks.  So
stepping backwards through your file could actually be
stepping forwards on the disk...  I hope that made sense. :(

> Is that true? If yes, the file system will have no protection at all!

It shouldn't be true.  It is possible you have found an
OS bug, though.  If that is the case, you might want to 
visit the kernel mailing lists.





More information about the NCLUG mailing list