Re: [PATCH] lib: introduce strdup_from_user

From: Timur Tabi
Date: Fri Jun 03 2011 - 14:35:07 EST


Alexey Dobriyan wrote:

> If mm is shared, data can or will change under you.

Ah, that's a good point. The only side-effect I can see of that is that it will
copy the string incorrectly, but it won't overwrite memory.

Would it be better if I did this:

str = kzalloc(max, GFP_KERNEL);
if (!str)
return ERR_PTR(-ENOMEM);

if (copy_from_user(str, ustr, max - 1)) {
kfree(str);
return ERR_PTR(-EFAULT);
}

return krealloc(str, strlen(str) + 1, GFP_KERNEL);

Maybe the krealloc() is overkill.

>> > How else is it supposed to know how much to allocate
>> > without using strlen first?
> I don't know.
> What I know is that your function doesn't guarantee NUL-termination.

That's a bug. The copy_from_user() should look like this:

if (copy_from_user(str, ustr, len - 1)) {

--
Timur Tabi
Linux kernel developer at Freescale

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/