Skip to contentSkip to conversation

copy_from_user/copy_to_user question

DISCUSSION Vinay Venkataraghavan 7 participants 12 messages Dec 2, 2005 – Dec 5, 2005

lore
Share & tools
lore
#0 lore raw

I have a question regarding copy_to_user and copy_from_user, specifically the conditons and situations when they can be used.

Firstly, I guess it is always safe to use these funtions when making an ioctl call.

But my question is: Are there any specific circumstances or conditions when these functions don't have to be used, but at the same time ensure that no page fault occurs and crashes the system.

The reason I ask is, there is some software that I am dealing with that just don't use these functions.

Secondly, they seem to use memcpy as opposed to using copy_to_user/copy_from_user which is also very dangerous.

Any thoughts?

Vinay

__________________________________________
Yahoo! DSL – Something to write home about. Just $16.99/mo. or less.
dsl.yahoo.com

#1 lore raw
Quoted 6 lines · Vinay Venkataraghavan

I have a question regarding copy_to_user and copy_from_user, specifically the conditons and situations when they can be used.

Firstly, I guess it is always safe to use these funtions when making an ioctl call.

It's basically safe whenever you can schedule, and you are running on behalf of a user task (as appose to a kernel thread).

Quoted 5 lines

But my question is: Are there any specific circumstances or conditions when these functions don't have to be used, but at the same time ensure that no page fault occurs and crashes the system.

Sure, they don't need to be used if you don't need to get data to or from user context.

Quoted 3 lines

The reason I ask is, there is some software that I am dealing with that just don't use these functions.

What is this code and what is it doing?

Quoted 4 lines

Secondly, they seem to use memcpy as opposed to using copy_to_user/copy_from_user which is also very dangerous.

If they are grabbing data from user context into kernel (or vise versa) that could easily cause an oops. Not to mention it is a security risk.

-- Steve

#2 lore raw
Quoted 6 lines · Steven Rostedt
Quoted 3 lines

Secondly, they seem to use memcpy as opposed to using copy_to_user/copy_from_user which is also very dangerous.

If they are grabbing data from user context into kernel (or vise versa) that could easily cause an oops. Not to mention it is a security risk.

Not to mention it simply won't work on a many platforms, no matter what...

#3 lore raw
Quoted 9 lines · Al Viro
Quoted 6 lines · Steven Rostedt
Quoted 3 lines

Secondly, they seem to use memcpy as opposed to using copy_to_user/copy_from_user which is also very dangerous.

If they are grabbing data from user context into kernel (or vise versa) that could easily cause an oops. Not to mention it is a security risk.

Not to mention it simply won't work on a many platforms, no matter what...

Hmm, I've only worked with a few platforms (i386, x86_64, ppc, mips, and a little arm but I don't remember that much). I believe that a memcpy could work on all these platforms (error prone of course, but if the memory is mapped its OK). When entering a system call, the kernel still has access to the memory locations assigned to the user.

It's been a while since I had to deal with the semantics of copy_to/from_user. So what platforms can not access a user space area directly. Is there a special assembly command to use to copy from user?

I haven't dealt (yet) with the copy_user of x86_64. Is there a problem when one tries to copy to/from a 32 bit address while in a 64 bit address space?

Just curious, sorry for my ignorance here.

-- Steve

#4 lore raw
Quoted 15 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

Secondly, they seem to use memcpy as opposed

to using

Quoted 4 lines
Quoted 4 lines
Quoted 2 lines

copy_to_user/copy_from_user which is also very
dangerous.

If they are grabbing data from user context into

kernel (or vise versa)

Quoted 1 line
Quoted 1 line

that could easily cause an oops. Not to mention

it is a security risk.

Quoted 2 lines

Not to mention it simply won't work on a many

platforms, no matter what...

Quoted 7 lines

Hmm, I've only worked with a few platforms (i386,
x86_64, ppc, mips, and
a little arm but I don't remember that much). I
believe that a memcpy
could work on all these platforms (error prone of
course, but if the
memory is mapped its OK).

When entering a system

Quoted 4 lines

call, the kernel still
has access to the memory locations assigned to the user.

But this is not always the case right. The point that you mention above is specifically why I posted this
question. It could well be the case that the user
space page could be swapped out when the user space process is blocked. So when the ioctl is serviced in kernel space, there is no guarantee that the page is still mapped. This could cause a page fault. I think this is why we need to do a copy_to_user/copy_from_user.

The piece of code that I am talking about is part of a driver code. Unfortunately I am not at liberty to divulge the name of the company. So in the driver then are not using copy_to_user and copy_from_user. That is what puzzles me. Moreover, where they are using these functions they use memcpy which is a big security risk.

Thanks once again.
Vinay

__________________________________________
Yahoo! DSL – Something to write home about. Just $16.99/mo. or less.
dsl.yahoo.com

#5 lore raw

The other point that I want to mention is that I don't think that there is any guarantee that the user space structure will be locked in memory. There is every likely hood of the page being swapped out.

Correct me if I am wrong.
Thanks,
Vinay

--- Vinay Venkataraghavan <[email protected]>

Quoted 61 lines · wrote:
Quoted 5 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

Secondly, they seem to use memcpy as opposed

to using

Quoted 1 line
Quoted 1 line
Quoted 1 line

copy_to_user/copy_from_user which is also

very

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line

dangerous.

If they are grabbing data from user context

into

Quoted 2 lines

kernel (or vise versa)

Quoted 1 line
Quoted 1 line

that could easily cause an oops. Not to

mention

Quoted 5 lines

it is a security risk.

Quoted 2 lines

Not to mention it simply won't work on a many

platforms, no matter what...

Quoted 7 lines

Hmm, I've only worked with a few platforms (i386,
x86_64, ppc, mips, and
a little arm but I don't remember that much). I
believe that a memcpy
could work on all these platforms (error prone of
course, but if the
memory is mapped its OK).

When entering a system

Quoted 4 lines

call, the kernel still
has access to the memory locations assigned to the user.

But this is not always the case right. The point that
you mention above is specifically why I posted this
question. It could well be the case that the user
space page could be swapped out when the user space process is blocked. So when the ioctl is serviced in kernel space, there is no guarantee that the page is still mapped. This could cause a page fault. I think this is why we need to do a copy_to_user/copy_from_user.

The piece of code that I am talking about is part of a
driver code. Unfortunately I am not at liberty to divulge the name of the company. So in the driver then
are not using copy_to_user and copy_from_user. That is
what puzzles me. Moreover, where they are using these
functions they use memcpy which is a big security risk.

Thanks once again.
Vinay

Quoted 6 lines

__________________________________________
Yahoo! DSL – Something to write home about. Just $16.99/mo. or less.
dsl.yahoo.com

__________________________________________
Yahoo! DSL – Something to write home about. Just $16.99/mo. or less.
dsl.yahoo.com

#6 lore raw
In reply to Vinay Venkataraghavan
Quoted 9 lines · Vinay Venkataraghavan
Quoted 8 lines

Hmm, I've only worked with a few platforms (i386,
x86_64, ppc, mips, and
a little arm but I don't remember that much). I
believe that a memcpy
could work on all these platforms (error prone of
course, but if the
memory is mapped its OK).

Quoted 7 lines

When entering a system

Quoted 4 lines

call, the kernel still
has access to the memory locations assigned to the user.

But this is not always the case right.

Right.

Quoted 9 lines

The point that
you mention above is specifically why I posted this
question. It could well be the case that the user
space page could be swapped out when the user space process is blocked. So when the ioctl is serviced in kernel space, there is no guarantee that the page is still mapped. This could cause a page fault. I think this is why we need to do a copy_to_user/copy_from_user.

Yes that is a reason to use copy_to/from_user, and it also checks to see if the memory being used is allowed by the user.

Quoted 8 lines

The piece of code that I am talking about is part of a driver code. Unfortunately I am not at liberty to divulge the name of the company. So in the driver then are not using copy_to_user and copy_from_user. That is what puzzles me. Moreover, where they are using these functions they use memcpy which is a big security risk.

Sounds like the authors of this driver don't know Linux kernel programming very well. If you want to test it, I would write a userland program that uses this ioctl and pass in a bad pointer (NULL or better yet, the location of schedule found in System.map). If the machine crashes, you got your answer ;) Passing in an address of code might not crash right away. You may need to wait till the data cache writes it out to memory, and the instruction cache reads it. These two caches are not always in sync on all platforms.

-- Steve

#7 lore raw
In reply to Vinay Venkataraghavan
Quoted 8 lines

The piece of code that I am talking about is part of a driver code. Unfortunately I am not at liberty to divulge the name of the company. So in the driver then are not using copy_to_user and copy_from_user. That is what puzzles me. Moreover, where they are using these functions they use memcpy which is a big security risk.

I guess this is why you're not allowed to mention it... they'd be all over bugtraq..

thank god for open source so that 1) customers can see the quality and
2) everyone can fix it....

#8 lore raw
In reply to Steven Rostedt
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
Quoted 3 lines

Secondly, they seem to use memcpy as opposed to using copy_to_user/copy_from_user which is also very dangerous.

If they are grabbing data from user context into kernel (or vise versa) that could easily cause an oops. Not to mention it is a security risk.

Quoted 7 lines
Quoted 1 line

Not to mention it simply won't work on a many platforms, no matter what...

Hmm, I've only worked with a few platforms (i386, x86_64, ppc, mips, and a little arm but I don't remember that much). I believe that a memcpy could work on all these platforms (error prone of course, but if the memory is mapped its OK). When entering a system call, the kernel still has access to the memory locations assigned to the user.

Won't work at all on platforms that have distinct address spaces for user and kernel space. E.g. on s390/s390x it wouldn't work. And yes, there are special instructions to copy data between address spaces.

Heiko

#9 lore raw
Quoted 15 lines · Heiko Carstens
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
Quoted 3 lines

Secondly, they seem to use memcpy as opposed to using copy_to_user/copy_from_user which is also very dangerous.

If they are grabbing data from user context into kernel (or vise versa) that could easily cause an oops. Not to mention it is a security risk.

Quoted 7 lines
Quoted 1 line

Not to mention it simply won't work on a many platforms, no matter what...

Hmm, I've only worked with a few platforms (i386, x86_64, ppc, mips, and a little arm but I don't remember that much). I believe that a memcpy could work on all these platforms (error prone of course, but if the memory is mapped its OK). When entering a system call, the kernel still has access to the memory locations assigned to the user.

Quoted 3 lines

Won't work at all on platforms that have distinct address spaces for user and kernel space. E.g. on s390/s390x it wouldn't work. And yes, there are special instructions to copy data between address spaces.

Thanks for the update. That's why I love working in an open environment. You learn something new every day ;)

-- Steve

#10 lore raw
In reply to Steven Rostedt
Quoted 4 lines · Steven Rostedt

I haven't dealt (yet) with the copy_user of x86_64. Is there a problem when one tries to copy to/from a 32 bit address while in a 64 bit address space?

No problem, except on UML/x86-64 which has fully separate address spaces.

Architectures where it doesn't work include s390,m68k,pa-risc,sparc64, i386 with 4/4 patches among others.

-Andi

#11 lore raw
In reply to Steven Rostedt
Quoted 27 lines · Steven Rostedt
Quoted 6 lines · Vinay Venkataraghavan

I have a question regarding copy_to_user and copy_from_user, specifically the conditons and situations when they can be used.

Firstly, I guess it is always safe to use these funtions when making an ioctl call.

It's basically safe whenever you can schedule, and you are running on behalf of a user task (as appose to a kernel thread).

Quoted 5 lines

But my question is: Are there any specific circumstances or conditions when these functions don't have to be used, but at the same time ensure that no page fault occurs and crashes the system.

Sure, they don't need to be used if you don't need to get data to or from user context.

Quoted 3 lines

The reason I ask is, there is some software that I am dealing with that just don't use these functions.

What is this code and what is it doing?

Quoted 1 line
Quoted 9 lines
Quoted 3 lines

Secondly, they seem to use memcpy as opposed to using copy_to_user/copy_from_user which is also very dangerous.

If they are grabbing data from user context into kernel (or vise versa) that could easily cause an oops. Not to mention it is a security risk.

-- Steve

The kernel has the privileges to trash anything from kernel-space. This means that an incorrect pointer or length-variable, passed from user-mode code could trash everything including the kernel if the kernel code just used memcpy().

So, copy_to/from_user was developed so that the user's privs and page ownership would be used during the copy operation. This means that if the user provides the wrong location or byte-count, the improper operation is trapped and the kernel code can return an error code, usually -EFAULT.

Drivers that use memcpy() when accessing user-provided space (such as in read(), write(), ioctl(), etc.) are simply broken and should not be used. They can cause other modules and/or kernel functions to fail in mysterious ways.

Cheers,
Dick Johnson
Penguin : Linux version 2.6.13.4 on an i686 machine (5589.44 BogoMips). Warning : 98.36% of all statistics are fiction.
.

****************************************************************
The information transmitted in this message is confidential and may be privileged. Any review, retransmission, dissemination, or other use of this information by persons or entities other than the intended recipient is prohibited. If you are not the intended recipient, please notify Analogic Corporation immediately - by replying to this message or by sending an email to [email protected] - and destroy all copies of this information, including any attachments, without reading or disclosing them.

Thank you.