Skip to contentSkip to conversation

large copy_to_user fills only one page?

DISCUSSION Emmanuel Michon 3 participants 3 messages May 30, 2002

lore
Share & tools
lore
#0 lore raw

Hi,

I'm working with linux-2.4.18, and writing some
trivial code to get from kernel a grabbed image working this way:

#define IMSIZE 350000

user mode runs:
u_p=malloc(IMSIZE);
ioctl(grabberfd,DOGRAB,u_p);
write *u_p to disk
free(u_p);

kernelmode runs:
case DOGRAB:

        char *u_p,*k_p;
        copy_from_user(u_p,arg,sizeof(char *));
        k_p=vmalloc(IMSIZE);
        kernelgrabs(k_p);
        copy_to_user(u_p,k_p,IMSIZE);
        vfree(k_p);
        break;

What I get actually is only 4K filled in userland, but copy_to_user returns IMSIZE!

If I memset the memory area *u_p to any value, the grab happens properly.

I guess memset'ing faults the good pages in, I'm quite surprised this does not happen smoothly by itself ;-(

Any clue?

Sincerely yours,

Signature · 4 lines
Emmanuel Michon
Chef de projet
REALmagic France SAS
Mobile: 0614372733 GPGkeyID: D2997E42  
#1 lore raw
Quoted 3 lines · Emmanuel Michon

case DOGRAB:

        char *u_p,*k_p;
        copy_from_user(u_p,arg,sizeof(char *));
                         ^^^

You copy into random stack garbage here. After that it goes downwards.

-Andi

#2 lore raw
In reply to Emmanuel Michon

Hi Emmanuel,

Quoted 4 lines · Emmanuel Michon

I'm working with linux-2.4.18, and writing some
trivial code to get from kernel a grabbed image working this way:
What I get actually is only 4K filled in userland, but copy_to_user returns IMSIZE!

copy_to_user() returns the number of bytes NOT copied. same for copy_from_user().

This is a common mistake and your code using this functions should look like this:

if (copy_from_user(to,uptr,size)) {
return -EFAULT;
}

/* Do some processing */

if (copy_to_user(uptr,from,size)) {
return -EFAULT;
}

PS: CC'ed linux-kernel, that you will not get 1000 answers ;-)

Regards

Ingo Oeser

Signature · 1 line
Science is what we can tell a computer. Art is everything else. --- D.E.Knuth