strncpy_from_user called from invalid context?

Dave Hylands dhylands at gmail.com
Mon Dec 20 00:19:35 EST 2010


Hi Greg,

On Sat, Dec 18, 2010 at 2:27 PM, Greg Kerr <kerrgi at gmail.com> wrote:
> In order to learn the jprobes interface, I am a writing a fairly silly example
> jprobe. However, I'm having issues copying a string from userspace....
> Basically somehow I wind up calling a function that may sleep from an invalid
> context (interrupts disabled or something).
>
> I have the source of the function in question and the stack trace below. If
> anyone could give me a few pointers it would be greatly appreciated.

I'd get rid of the 1K stack variable. I think on x86, the kernel stack
is only 4K.
Use kmalloc instead. If your stack variable is crossing the page
boundary and nothing is mapped into memory there, then that could
cause the page fault.

I'd also recommend using strlcpy rather than strncpy.
See: <http://www.gratisoft.us/todd/papers/strlcpy.html>
Your use of strncpy is incomplete. To be complete, you need to do:

strncpy( dest, src, sizeof( dest ));
dest[ sizeof( dest ) - 1 ] = '\0';

I'm not 100% sure about strncpy_from_user but with regular strncpy, if
the length of the source is >= sizeof the destination, then your
destination string won't be null terminated.

And of course, you should make sure that the user-mode program passed
in a valid pointer.

-- 
Dave Hylands
Shuswap, BC, Canada
http://www.DaveHylands.com/



More information about the Kernelnewbies mailing list