--- 2.2.10-SuSE/fs/ext2/file.c.~1~ Thu Jul 22 02:06:04 1999 +++ 2.2.10-SuSE/fs/ext2/file.c Thu Jul 22 03:08:13 1999 @@ -167,6 +167,11 @@ /* POSIX: mtime/ctime may not change for 0 count */ if (!count) return 0; + /* This makes the bounds-checking arithmetic later on much more + * sane. */ + if (((signed) count) < 0) + return -EINVAL; + write_error = buffercount = 0; if (!inode) { printk("ext2_file_write: inode = NULL\n"); @@ -200,9 +205,18 @@ /* Check for overflow.. */ #if BITS_PER_LONG < 64 - if (pos > (__u32) (pos + count)) { - count = ~pos; /* == 0xFFFFFFFF - pos */ - if (!count) + /* If the fd's pos is already greater than or equal to the file + * descriptor's offset maximum, then we need to return EFBIG for + * any non-zero count (and we already tested for zero above). */ + if (((unsigned) pos) >= 0x7FFFFFFFUL) + return -EFBIG; + + /* If we are about to overflow the maximum file size, we also + * need to return the error, but only if no bytes can be written + * successfully. */ + if (((unsigned) pos + count) > 0x7FFFFFFFUL) { + count = 0x7FFFFFFFL - pos; + if (((signed) count) < 0) return -EFBIG; } #else