f



linux kernel user space

I am developing a kernel module, and as to prevent a page fault from
occuring when accessing data from user space, I need to copy it to
kernel space. Or so I thought. Until I was told that the linux kernel
is in user space, so doing that is unneeded and all that I need to do
is just use access_ok() on the address before using the data to which
the pointer points to. Is this correct? If not, how should I handle
user space addresses?

Also, I have heard that the syscall table can no longer be modified in
2.5? What is the validity of that?


--
nethlek
0
nethlek
12/5/2003 9:46:27 AM
comp.linux.development.system 5436 articles. 0 followers. zixenus (12) is leader. Post Follow

5 Replies
233 Views

Similar Articles

[PageSpeed] 17

Mantorok Redgormor wrote:
> 
> I am developing a kernel module, and as to prevent a page fault from
> occuring when accessing data from user space, I need to copy it to
> kernel space.

That is not the only reason to copy the data.

> Or so I thought. Until I was told that the linux kernel
> is in user space,

That is not correct. The kernel is in kernel space. But the
kernel can access user space under some configurations. And
that is actually what copy_from_user and copy_to_user does
on those architectures.

> so doing that is unneeded and all that I need to do
> is just use access_ok() on the address before using the data to which
> the pointer points to. Is this correct?

I'm not sure about that. I think there will be cases where
it doesn't work.

> If not, how should I handle user space addresses?

Depends on the amount of data. If you need to access a
single element of 1, 2, or 4 bytes, you can use get_user
and put_user. For other sizes you can use copy_to_user
and copy_from_user. For large amounts of data with
special performance requirements you can do something
more complicated.

> 
> Also, I have heard that the syscall table can no longer be modified in
> 2.5? What is the validity of that?

You can do it, if you really want to. But it has always
been a bad idea to do so.

http://www.daimi.au.dk/~kasperd/comp.os.linux.development.faq.html#syscall

-- 
Kasper Dupont -- der bruger for meget tid paa usenet.
For sending spam use mailto:aaarep@daimi.au.dk
/* Would you like fries with that? */
0
Kasper
12/5/2003 11:15:27 AM
Mantorok Redgormor wrote:
> I am developing a kernel module, and as to prevent a page fault from
> occuring when accessing data from user space, I need to copy it to
> kernel space. Or so I thought. Until I was told that the linux kernel
> is in user space, so doing that is unneeded and all that I need to do
> is just use access_ok() on the address before using the data to which
> the pointer points to. Is this correct? If not, how should I handle
> user space addresses?
You should definately use copy_to_user and copy_from_user.  On some 
architectures the MMU operates in such a way that the calling process 
has its address map accessible from kernel space during its execution 
lifetime in the kernel (ppc does this IIRC).  however, if some other 
execution context calls a function in your module which copies data from 
a pointer that is _supposed_ to be in user space but isn't, or if your 
code is ported to an arch which doesn support this shared address map in 
  kernel space, you either won't operate properly, or you're begging for 
an oops.  copy_to/from_user solves this by preforming other memory map 
tricks on the architectures that don't do shared mapping space so that 
your module will be that much more portable.
> 
> Also, I have heard that the syscall table can no longer be modified in
> 2.5? What is the validity of that?
I think the API to register/modify syscalls is gone.  You can always 
modify the data strucutres manually, but as kasper said, its a lousy idea.

Neil


-- 
/***************************************************
  *Neil Horman
  *Software Engineer
  *Red Hat, Inc., http://people.redhat.com/nhorman
  *gpg keyid: 1024D / 0x92A74FA1
  *http://www.keyserver.net
  ***************************************************/

0
Neil
12/5/2003 1:30:19 PM
On Fri, 05 Dec 2003 08:30:19 -0500, Neil Horman wrote:
> Mantorok Redgormor wrote:

> You should definately use copy_to_user and copy_from_user.  On some 
> architectures the MMU operates in such a way that the calling process 
> has its address map accessible from kernel space during its execution 
> lifetime in the kernel (ppc does this IIRC).

The i386 in 4:4 mode is the same. Accessing user pointers
oopses immediately. Most distros use 1:3 split though, so
you can get away with cheating.

-- Pete

0
Pete
12/5/2003 10:36:05 PM
Kasper Dupont <kasperd@daimi.au.dk> wrote in message news:<3FD068CF.A5F288B0@daimi.au.dk>...
> Mantorok Redgormor wrote:
> > 
> > I am developing a kernel module, and as to prevent a page fault from
> > occuring when accessing data from user space, I need to copy it to
> > kernel space.
> 
> That is not the only reason to copy the data.
> 
> > Or so I thought. Until I was told that the linux kernel
> > is in user space,
> 
> That is not correct. The kernel is in kernel space. But the
> kernel can access user space under some configurations. And
> that is actually what copy_from_user and copy_to_user does
> on those architectures.
> 
> > so doing that is unneeded and all that I need to do
> > is just use access_ok() on the address before using the data to which
> > the pointer points to. Is this correct?
> 
> I'm not sure about that. I think there will be cases where
> it doesn't work.
> 
> > If not, how should I handle user space addresses?
> 
> Depends on the amount of data. If you need to access a
> single element of 1, 2, or 4 bytes, you can use get_user
> and put_user. For other sizes you can use copy_to_user
> and copy_from_user. For large amounts of data with
> special performance requirements you can do something
> more complicated.
> 
> > 
> > Also, I have heard that the syscall table can no longer be modified in
> > 2.5? What is the validity of that?
> 
> You can do it, if you really want to. But it has always
> been a bad idea to do so.
> 
> http://www.daimi.au.dk/~kasperd/comp.os.linux.development.faq.html#syscall

The problem I ran into with using copy_from_user was the fact that you
have to allocate enough space for the user space data but you don't
know how much to allocate because you first have to get the size of an
argument passed in the case of a function which doesn't pass a size
argument from user space.

So if I have a hypothetical function named "filename" that accepts an
argument of type pointer to char qualified by const how am I suppose
to get the correct size to allocate to store the data pointer to by
the user space pointer so I can do whatever operation with that
particular
filename in kernel space?

Hopefully that makes sense

Thanks for the replies :)

--
nethlek
0
nethlek
12/7/2003 12:02:05 AM
Mantorok Redgormor wrote:
> 
> So if I have a hypothetical function named "filename" that accepts an
> argument of type pointer to char qualified by const how am I suppose
> to get the correct size to allocate to store the data pointer to by
> the user space pointer so I can do whatever operation with that
> particular
> filename in kernel space?

If you want to copy a filename from userspace to kernelspace,
the normal way to do that is using getname() and putname().
What getname() actually does is allocate one page and use
strncpy_from_user. Longer paths are not supported.

-- 
Kasper Dupont -- der bruger for meget tid paa usenet.
For sending spam use mailto:aaarep@daimi.au.dk
/* Would you like fries with that? */
0
Kasper
12/7/2003 4:30:59 PM
Reply: