This is my lab assignment: I am a beginner in kernel space.
I did a simple system call implementation and it works as expected. But, I wanted to go a level further and look into giving incorrect data to my system call and see if it handles it.
For eg., I used access_ok in my system call. I really want to pass a invalid user space pointer and see if I used the access_ok appropriately inside my system call.
How to do this? I saw that, access_ok will fail if I pass kernel space pointer. But how do I write a driver program to do this?
As you are trying to pass an invalid user space pointer to the macro access_ok(type,addr,size);.
so it just checks that the provided pointer is not in kernel-space area if it’s in kernel-space memory then will return zero(0) else it will return true so in this way you can check your desired result.. And if you are providing any address outside kernel-memory may be accepted(its possible that the provided address is not it the address-space of the process which is calling this system call in this case the type provided as VERIFY_READ or VERIFY_WRITE will decide what to do).
From man page of access_ok() refer to http://mirror.linux.org.au/linux-mandocs/2.6.12.6/access_ok.html. Note that, depending on architecture, this function probably just checks that the pointer is in the user space range – after calling this function, memory access functions may still return -EFAULT
If you want to pass the pointer to the kernel then you can do it as
1-> define one structure which will contain pointer address.
2-> add the same structure in your kernel module or driver. So the kernel & user know about the structure.
3-> Add one parameter in your system call as
char __user var.4-> create one structure variable in your user space of the defined structure & pass it in the system call as you call in your user-space application.
5-> Inside kernel-module or driver where the code is defined for your system-call, create a structure variable of the same structure as you used in user-space. And now do what-ever you want.
`
`