On Fri, May 10, 2024 at 05:48:05AM +0100, Al Viro wrote: > On Fri, May 10, 2024 at 03:26:08AM +0000, Justin Stitt wrote: > > > This feels like a case of accidental correctness. You demonstrated that > > even with overflow we end up going down a control path that returns an > > error code so all is good. > > No. It's about a very simple arithmetical fact: the smallest number that > wraps to 0 is 2^N, which is more than twice the maximal signed N-bit > value. So wraparound on adding a signed N-bit to non-negative signed N-bit > will always end up with negative result. That's *NOT* a hard math. Really. > > As for the rest... SEEK_CUR semantics is "seek to current position + offset"; > just about any ->llseek() instance will have that shape - calculate the > position we want to get to, then forget about the difference between > SEEK_SET and SEEK_CUR. So noticing that wraparound ends with negative > is enough - we reject straight SEEK_SET to negatives anyway, so no > extra logics is needed. > > > However, I think finding the solution > > shouldn't require as much mental gymnastics. We clearly don't want our > > file offsets to wraparound and a plain-and-simple check for that lets > > readers of the code understand this. > > No comments that would be suitable for any kind of polite company. FWIW, exchange of nasty cracks aside, I believe that this kind of whack-a-mole in ->llseek() instances is just plain wrong. We have 80-odd instances in the tree. Sure, a lot of them a wrappers for standard helpers, but that's still way too many places to spill that stuff over. And just about every instance that supports SEEK_CUR has exact same kind of logics. As the matter of fact, it would be interesting to find out which instances, if any, do *not* have that relationship between SEEK_CUR and SEEK_SET. If such are rare, it might make sense to mark them as such in file_operations and have vfs_llseek() check that - it would've killed a whole lot of boilerplate. And there it a careful handling of overflow checks (or a clear comment explaining what's going on) would make a lot more sense. IF we know that an instance deals with SEEK_CUR as SEEK_SET to offset + ->f_pos, we can translate SEEK_CUR into SEEK_SET in the caller.