Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Linux - Kernel This forum is for all discussion relating to the Linux kernel.


  Search this Thread
Old 02-19-2012, 05:54 AM   #1
LQ Newbie
Registered: Jan 2012
Posts: 4

Rep: Reputation: Disabled
memcpy and copy_to/from_user

hi everyone,

I just want to know, what is the importance of copy_to/from_user memcpy?

I went through the some definitions which explain to avoid illegal handling of memory from the user. But, I didn't understand, while read/write also refers to the user buffer while copying the data (memcpy).
When should exactly we have to use copy_to/from_user and when memcpy.

thanks in advance,

Old 02-19-2012, 08:14 AM   #2
Senior Member
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,538

Rep: Reputation: 1058Reputation: 1058Reputation: 1058Reputation: 1058Reputation: 1058Reputation: 1058Reputation: 1058Reputation: 1058
Best I can tell you is to offer an example of how to use memcpy in a C program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void    main    (void)
        static  char    buf1 [] = { "I am to be copied" };
        char            buf2 [25], *strptr;
        static  int     data1 [5] = { 1, 2, 3, 4, 5 };
        int             data2 [5], i, compare;

        /*      copy characters from buf1 to buf2       */
        (void) memcpy ((void *) buf2, (void *) buf1, sizeof (buf1));
        (void) fprintf (stdout, "%s\n", buf2);
        /*      copy data1 array to data2               */
        (void) memcpy ((void *) data2, (void *) data1, sizeof (data1));
        for (i = 0; i < 5; i++)
                (void) fprintf (stdout, "%d ", data2 [i]);
        (void) fprintf (stdout, "\n");
        /*      scan buf1 for charcter 't'              */
        strptr = memchr ((void *) buf1, 't', sizeof (buf1));
        (void) fprintf (stdout, "%s\n", strptr);
        /*      compare buf1 to buf2                    */
        compare = memcmp ((void *) buf1, (void *) buf2, sizeof (buf1));
        (void) fprintf (stdout, "%d\n", compare);
        /*      set buf2 array to all 'x' characters    */
        (void) memset ((void *) buf2, 'x', sizeof (buf2));
        for (i = 0; i < sizeof (buf2); ++i)
                (void) fprintf (stdout, "%c", buf2 [i]);
        (void) fprintf (stdout, "\n");
        exit (EXIT_SUCCESS);
The above demonstrates memcpy, memchr, memcmp and memset -- read the manual pages for each for descriptions.

The important thing to keep in mind is that you must allocate sufficient memory to use these functions.

In the above, the buffers are declared: buf1 will be the size of the string (I am to be copied) and buf2 is given a size (25 byte) and is a string pointer. You can, typically, use the token BUFSIZ for buffer size declaration (it's 8192 on my system, yours may vary); BUFSIZ is defined in stdio.h. On modern systems with gigabytes of RAM it's not going to hurt anything to set aside 8K for buffers.

The real reason for using the mem functions is that they're fast; copying stuff is usually done in some sort of a loop (for, while) and that's slower, more overhead. Now, "slower" is a relative term when you have a fast processor and wads of memory but you get the idea; every microsecond counts sometimes.

And, like any other copying into buffers (or arrays), if you try to copy more than you've allocated you'll get a crash (or maybe even a core dump but those don't happen too often anymore).

Hope this helps some.
Old 02-19-2012, 10:02 AM   #3
Nominal Animal
Senior Member
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 946Reputation: 946Reputation: 946Reputation: 946Reputation: 946Reputation: 946Reputation: 946Reputation: 946
Originally Posted by rbhsrh View Post
I just want to know, what is the importance of copy_to/from_user memcpy?
copy_to_user() and copy_from_user() do a bit of magic to ensure that it only copies from/to userspace, and then only from valid address ranges. If the userspace provided an invalid pointer and/or length, the functions will return the number of bytes that could not be copied. In real life, you check if they return zero, and if not, you return -EFAULT to indicate the pointer/length was bogus.

memcpy() does nothing of that sort. If the user provides a cleverly crafted pointer, memcpy() will happily copy kernel data; it does absolutely no sanity checks. copy_to_user() and copy_from_user() do all the necessary checks and preparations.

In kernel sources, pointers received from userspace are usually annotated __user. This is just make sure you (the kernel programmer) remembers to use copy_to_user()/copy_from_user() to reference the data pointed at.

The simple rule is: Whenever the pointer is specified by the userspace process, either indirectly or directly, you use copy_to_user()/copy_from_user(). You only use memcpy() with pointers internal to the kernel that are never supplied to userspace.

There are certain exceptions and more complex cases, but those are best described in Linux Device Drivers, Third Edition.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] segmentation fault during memcpy() TheIndependentAquarius Programming 10 02-20-2010 04:41 AM
segmentation fault in memcpy George2 Programming 6 08-25-2007 11:33 PM
memcpy or mmap or other problems snowing Programming 3 12-26-2005 11:49 PM
how can i speed up memcpy? Thinking Programming 9 10-14-2005 07:53 AM
memcpy problems alaios Programming 4 09-17-2005 07:26 AM

All times are GMT -5. The time now is 09:14 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration