Re: [GIT PULL] configfs fix for Linux 5.14

From: Linus Torvalds
Date: Wed Jul 14 2021 - 16:08:49 EST


On Wed, Jul 14, 2021 at 9:33 AM Christoph Hellwig <hch@xxxxxxxxxxxxx> wrote:
>
> configfs fix for Linux 5.14
>
> - fix the read and write iterators (Bart Van Assche)

I've pulled this, but I'm somewhat disgusted by it.

The overflow "protection" is just wrong:

+ to_copy = SIMPLE_ATTR_SIZE - 1 - pos;
+ if (to_copy <= 0)
+ return 0;

because if users control "pos", then that "to_copy" could be a huge
positive value even after overflow protection.

I hope/think that we always end up checking 'pos' in the VFS layer so
that this isn't a bug in practice, but people - the above is just
fundamentally bad code.

It's simply not the correct way to check limits. It does it badly, and
it's hard to read (*).

If you want to check limits, then do it (a) the obvious way and (b) right.

Something like

if (pos < 0 || pos >= SIMPLE_ATTR_SIZE - 1)
return 0;
to_copy = SIMPLE_ATTR_SIZE - 1 - pos;

would have been a hell of a lot more obvious, would have been CORRECT,
and a compiler would likely be able to equally good code for it.

Doing a "x <0 || x > C" test is actually nice and cheap, and compilers
should all be smart enough to turn it into a single (unsigned)
comparison.

Possibly it even generates better code, since "to_copy" could then -
and should - no longer be a 64-bit loff_t, since it's pointless. We've
just checked the range of the values, so it can be the natural size
for the machine.

Although from a small test, gcc does seem to be too simple to take
advantage of that, and on 32-bit x86 it does the range check using
64-bit arithmetic even when unnecessary (it should just check "are the
upper 32 bits zero" rather than play around with doing a 64-bit
sub/sbb - I'm surprised, because I thought gcc already knew about
this, but maybe compiler people are starting to forget about 32-bit
stuff too).

But even if the compiler doesn't figure it out, the simple "just check
the limits" is a lot more readable for humans, and avoids the whole
overflow issue. And maybe some compilers will do better at it.

Linus

(*) Ok, it's easy to read if you ignore the overflow possibility. IOW,
it's easy to read WRONG.