LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Password
Linux - Kernel This forum is for all discussion relating to the Linux kernel.

Notices


Reply
  Search this Thread
Old 09-16-2022, 06:25 AM   #1
Ishpuini
LQ Newbie
 
Registered: Sep 2022
Posts: 2

Rep: Reputation: 0
Pointer Dereferencing causes Permission Violation in Kernel Module


I am trying to allocate memory for a pointer in another function in Linux Kernel module file, and it causes error_code(0x0001) - permissions violation .

Here is a simpler reproduction of what i am trying to do.

Code:
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/slab.h>

int another(char **name){
    *name = kmalloc(16, GFP_KERNEL);
    return 0;
}

static int __init test_init(void){
    char *name;
    another(&name);
    printk(KERN_INFO "Hello!");
    return 0;
}

static void __exit test_exit(void){
    printk(KERN_INFO "Goodbye!");
}

MODULE_LICENSE("GPL");

module_init(test_init);
module_exit(test_exit);
I did this a lot in userspace and it works just fine, but here it crashes every time. Please let me know if dmesg info is needed.

Last edited by Ishpuini; 09-19-2022 at 08:49 AM.
 
Old 09-19-2022, 08:40 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,930

Rep: Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320
No, it is just wrong. But anyway would be nice to use code tags to make your post readable.
1. char *name; (in test_init)
This will create a variable named "name" which is a pointer to a char. Not initialized, therefore it does not point to a valid/usable memory address.
2. *name = kmalloc(16, GFP_KERNEL);
This first calls kmalloc and the result will be stored at the location pointed by the local variable name, which is not the same as name in test_init, but actually it contains the same value.
3.
The result is that you want to write into a random address (based on an uninitialized variable), which will actually cause that permission denied.
4.
No it does not work in userspace too.
 
Old 09-19-2022, 08:58 AM   #3
Ishpuini
LQ Newbie
 
Registered: Sep 2022
Posts: 2

Original Poster
Rep: Reputation: 0
Hi and thanx for replying!

Quote:
Originally Posted by pan64 View Post
No it does not work in userspace too.
This compiles and works just fine though...

Code:
#include <stdlib.h>

void custom_alloc(char **ptr){
	*ptr = malloc(128);
}

int main(int argc, char **argv){
	char *ptr;
	custom_alloc(&ptr);
	free(ptr);
	return 0;
}
However in Kernel i am getting error even while just passing the pointer without allocating memory, like this`

Code:
int another(char **name){
    return 0;
}

static int __init test_init(void){
    char *name;
    another(&name);
    printk(KERN_INFO "Hello!");
    return 0;
}
Also it works if i make name static.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
modify the kernel and compiled but got "dereferencing pointer to incomplete type" err kindlike Linux - Kernel 3 04-09-2011 02:01 PM
tai64nlocal.c:55: error: dereferencing pointer to incomplete type ExCIA Linux - General 1 03-31-2009 09:49 AM
dereferencing pointer to incomplete type (just built a new kernel) 144419855310001 Linux - Kernel 0 10-03-2007 03:58 PM
"dereferencing `void *' pointer" but how else?!? f0rmula Programming 6 03-24-2005 05:32 AM
C error "dereferencing pointer to incomplete type" lucs Slackware 6 02-21-2005 09:33 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel

All times are GMT -5. The time now is 08:53 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration