2
votes

without entering too much in details, I'm writing this device driver for an fpga pci board, DMA-capable. There is a situation where the board dma-writes an ACK in memory with some data. This data is actually a pointer to a struct in memory i previously allocated, the pointer is notified to the board by using registers. So, I use the data written by the board to dereference a memory region. Since we are still in the debugging stage for this board, I cannot be totally sure about the data DMA-written, so it happens to de-reference an invalid pointer (because the data DMA-written are inconsistent).

My question is: is there a way to check a memory pointer (from kernel space) before accessing it, without generate invalid access and/or a kernel panic?

1
What about checking PTE & PTE flags?Alex Hoppus
Instead of using absolute memory addresses (which you don't know how to easily validate), consider using indices or relative memory addresses (which can be validated).sawdust

1 Answers

1
votes

On a 32 bit system, the virtual memory addressing is diveded in the following way

0x00000000             0xc0000000  0xffffffff 
    |                        |          |
    +------------------------+----------+
    |  User                  |  Kernel  |
    |  space                 |  space   |
    +------------------------+----------+

So if I'm not wrong, you could always check whether your pointer is inside the following range (which is low memory)

0xc0000000 - 0xffffffff
3'221'225'472 - 4'294'967'295

But I'm not a linux-kernel expert, so it might be there are better and safer ways to achieve this