Re: COPY_TO_USER PROBLEMS!

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hello,

 I have an additional query in the same situation. I was reading the 'Linux kernel module programming 2.6'. For a simple char device driver, the sample code was using 'sprintf' routine to copy the a message from kernel space to user space. I want to know is this valid. If so, then how is the snprintf routine working? Is it a kernel Provided routine also? IF not valid, then how is that when i tried that code, it worked.


Thanks in advance
sandeep

On 1/28/06, Gaurav Dhiman <gauravd.chd@xxxxxxxxx> wrote:
On 1/27/06, Medeiros Edward M NPRI <MedeirosEM@xxxxxxxxxxxxxxxxx > wrote:
>
>
>
> Hello,
>
>
>
> I have a basic "char" driver that controls a PCI device with DMA capability.
>  I have a read routine that utilizes a "copy_to_user()" call to move data
> from a kernel-space buffer to a user-space buffer.   The kernel space buffer
> was created by calling by "dma_alloc_coherent" as follows:
>
>
>
>             kernel_space_buffer_address =
> dma_alloc_coherent(&(pci_dev->dev), 1024*128,
> dma_handle_address, GFP_KERNEL);
>
>
>
> I check that the "kernel_space_buffer_address" is not NULL and I print the
> address, which looks reasonable.  Then later in my driver's read routine I
> have the following code:
>
>
>
>
>
>             static  ssize_t pci_read(struct file *file, char __user
> *ubuffer, size_t length, loff_t *offset)  {
>
>
>
>                         bytes_read = copy_to_user((void __user *)ubuffer,
> (const void *)kernel_space_buffer_address), 10);
>
>
>
>                         }
>
>
>
> I have a simple user-space program that opens the device and calls the above
> read routine with:
>
>
>
>
>
>             main()
>
>             {
>
>                         buffer = (char *)malloc(1024*8);
>
>                         fd = open("/dev/pci_drv", O_RDONLY);
>
>                         bytes_read = read(fd, buffer 10);
>

check the user buffer before passing it to system call, it might be
NULL id malloc is unable to allocate memory. If you pass invaliduser
address copy_to_user will make the kernel panic.

-Gaurav


>                         close(fd);
>
>                         free(buffer);
>
>             }
>
>
>
> As soon as I hit the "read" I instantly get a kernel panic!!!   Am I missing
> something with my use of "copy_to_user"??
>
>
>
> Thank you for your time.

--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
FAQ:           http://kernelnewbies.org/faq/



[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux