Skip to contentSkip to conversation

get_user() not clearing destination on failure

arc Vineet Gupta 2 participants 2 messages Aug 19, 2016 1 message not in the archive Synopsys ARC architecture

lore
Share & tools
lore
#1 lore raw
In reply to a message not in the archive
Quoted 40 lines · Al Viro

First of all, my apologies if I'd managed to misread the ARC assembler in uaccess.h. AFAICS, neither __arc_get_user_one() nor __arc_get_user_one_64() end up zeroing the destination upon exception. Your __get_user_fn() is

({                                                              \
        long __ret = 0; /* success by default */        \
        switch (sz) {                                           \
        case 1: __arc_get_user_one(*(k), u, "ldb", __ret); break;       \
        case 2: __arc_get_user_one(*(k), u, "ldw", __ret); break;       \
        case 4: __arc_get_user_one(*(k), u, "ld", __ret);  break;       \
        case 8: __arc_get_user_one_64(*(k), u, __ret);     break;       \
        }                                                       \
        __ret;                                                  \
})
so it doesn't do that on its own either.  Neither does __get_user() (from
asm-generic/uaccess.h) nor get_user() (ditto), so unless I've missed something
subtle about your exception handling, after
	char c = 0;
	int y = get_user(c, unmapped_address);
you will get -EFAULT in y and random junk in c.  We get to __get_user(c, ...),
which turns into
	int __gu_err = -EFAULT;
	unsigned char __x;	// uninitialized
	__gu_err = __get_user_fn(1, unmapped_address, &__x);
	c = (char) __x;
	y = __gu_err;
i.e.
	int __gu_err = -EFAULT;
	unsigned char __x;
	long __ret = 0;
        __arc_get_user_one(*(&__x), unmapped_address, "ldb", __ret);
	__gu_err = __ret;
	c = (char) __x;
	y = __gu_err;
and AFAICS your assembler in __arc_get_user_one() will boil down to something
along the lines of
	r2 = unmapped_address;
1:	r1 = *(unsigned char *)r2;
2:
	__x = r1;
Quoted 6 lines

fixup(1):

	__ret = -EFAULT;
	goto 2;

with nothing to clear r1 on the fixup path.

*IF* the above is correct

It is - you got it all right

Quoted 3 lines

(and I'm really unfamiliar with the architecture
in question), you have a problem. get_user() and __get_user() ought to clear the destination even in case of EFAULT.

Just curious - why is that. The typical usage paradigm is check for return value and if 0 only then proceed to use the actual value.

Also for discussion sake, will eliminating the intermediate __x be helpful - so it would use the actual variable defined in caller of get_user() so if it is init properly, the value would be "retained" for -EFAULT. Anyways all of this is just for understanding better.

Quoted 5 lines

There definitely is a bug
in asm-generic get_user() (it doesn't clear destination in case of access_ok() failure), but that's not ARC-specific. I won't try to slap together a fix (no idea which forms of "zero that register" are better, for starters), but something like mov %1,0 in the fixup part might be needed.

I'll send that over tomorrow - after a bit of testing.

Quoted 3 lines

And I've really
no idea whatsoever about the syntax in __arc_get_user_one_64() - it needs to clear the entire 64bit value there.

Yeah the %R asm modifier seems to be undocumented in gcc manual - and it has been there forever. But I checked that it correctly generates a register pair for the 64-bit word.

-Vineet

#2 lore raw
Quoted 9 lines · Vineet Gupta

On 08/17/2016 08:00 PM, Al Viro wrote:

Just curious - why is that. The typical usage paradigm is check for return value and if 0 only then proceed to use the actual value.

Also for discussion sake, will eliminating the intermediate __x be helpful - so it would use the actual variable defined in caller of get_user() so if it is init properly, the value would be "retained" for -EFAULT. Anyways all of this is just for understanding better.

The effect of __get_user(v, p) should mirror that of v = *p had p been a kernel pointer. Including the sign expansion/truncation/etc.

That's what those casts and assignments are about, and unless you want to play with doing usual arithmetical conversion in your assembler bits, it has to stay in C. Sure, we could have __get_user(v, p) use v instead of (in your case) __x and do v = (__typeof(*p))v; to trigger the conversions, but... that's where the second problem bites: __get_user() has to be a macro, and __get_user(a[i++], p) would obviously break with that approach.

So there's no realistic way to get rid of that intermediate - not without a massive PITA with folding conversions into asm part and playing with __builtin_choose_expr/__builtin_types_compatible_p, which is going to be both painful and very easy to get wrong.

As for checking for error and discarding the results if it has happened... not always possible or reasonable. If you are using __get_user() in the first place, you are likely to be on a fairly hot path (otherwise the normal get_user() would've worked just as well). And having explicit test and branch after each of those can be unpleasant enough. Which means that you do something like a series of err |= __get_user(...); and check err once in the end (or have something like __get_user_err(..., err) or __get_user_ex() as x86 does, etc.). Which means that by the time you get to "OK, we sod off now" path, you don't know which of __get_user() has failed. If any of the destinations are not immediately destroyed, you get a fun questions along the lines of "which ones do I clean?", and even simpler "how do I make sure that none of them is missed by an accident?"

The same goes for get_user(), except that instead of "we use it on hot paths" we have "it's used by arseloads of ioctls in arseloads of drivers, with general code quality being what one would expect"...