Linux Kernel Programming: “Unable to handle kernel NULL pointer dereference at virtual address [address]”
For a class assignment, we are writing a custom syscall which pulls certain information about the existing process tree. The syscall is working fine for the most part and gets the appropriate information. However, a few processes it, in crashes with the error message, "Unable to handle kernel NULL pointer dereference at virtual address [address]". What I don't understand is that I'm testing if the pointer is NULL before accessing it, and yet, it still fails.
Example: In the code below, current_process is a valid pointer to a task_struct and k_buf is valid
printk("Setting parent process\n");
parent_process = current_process->real_parent;
printk("Parent process set\n");
if (parent_process != NULL) {
printk("Parent process is not null and getting pid\n");
k_buf[i].parent_pid = parent_process->pid;
} else {
k_buf[i].parent_pid = 0;
}
printk("Done with parent process\n");
When run, the program prints:
Setting parent process
Parent process set
Parent process is not null and getting pid
Done with parent process
a couple of times, and then
Setting parent process
Parent process set
Parent process is not null and getting pid
before throwing the error and going into kernel panic.
What am I doing wrong? Any thoughts?
EDIT:
For the time being, I commented out the above code so I could continue working on the rest of the system call. When I try to access the pid of a child process (again after a couple of successful attempts), it gives me a "Unable to handle kernel paging request at virtual address" error. As far as I understand, I have the correct locks in place for reading this data. However,开发者_开发技巧 is there something else I need to do to check the memory before I access it?
I'm speculating here but could parent_process->pid
being NULL
be the cause of your "kernel panic"? If so, you could check for that too.
Its either that, or some issue with accessing the i
th element of k_buf
array ie. *(k_buf+i)
You don't seem to be testing kbuf
of kbuf[i]
before access. Also, you can printk
these pointers, that way you'd catch non-null but obviously invalid addresses (such as 0xbfff0c3a
)
I wrote a system call to list all processes and got this error:
[ 260.613411] BUG: unable to handle kernel NULL pointer dereference at 0000000000000040 [ 260.613416] PGD 0 P4D 0 [ 260.613422] Oops: 0000 [#1] SMP PTI [ 260.613427] CPU: 3 PID: 1793 Comm: a.out Tainted: P OE 4.19.5 #2
i was checking , if( process->parent) then print its PID,
Now i think i should try if( process->parent !=NULL)
Answer to Question : Dear Bro, You can try, ppid = task_pid_nr(child_process->parent) to get pid of parent....!
精彩评论