LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to move pointer allocated in kernel space to user space. (https://www.linuxquestions.org/questions/programming-9/how-to-move-pointer-allocated-in-kernel-space-to-user-space-4175635234/)

Phuti 07-30-2018 03:32 AM

How to move pointer allocated in kernel space to user space.
 
Hi am new to kernel programming and I am trying to write a system call in linux that get information from the kernel and send the information to user space program in a struct.

My function prototype is : long sys_get_info(struct info *info);

My struct has :

Code:

struct info {
      int pid;
      char *root_path;
      char *pwd_path;
};

My sys_call returns information about the current process calling my syscall.

My question is can I kmalloc a char pointer to store the root_path and then assign that to the value of my struct and access it in user space?

NevemTeve 07-30-2018 03:41 AM

It would be used by haxors to exhaust kernel-memory.
Instead ask the caller to provide space.
Code:

int sys_get_info (void *buff, size_t bufsize, size_t *retsize);
...
char mybuf [64];
size_t retlsize;

int rc= sys_get_info (mybuf, sizeof mybuf, &retsize);
if (rc==0) {
    /* sucess, retsize is the actual used size */
    struct info *p = (struct info *)mybuf;
} else {
    /* failure, retsize is the required size */
}


Phuti 07-30-2018 05:08 AM

I am not allowed to change the function prototype is there another way to do it???

Phuti 07-31-2018 10:14 AM

Ok I changed my struct to :

Code:

struct info {
      int pid;
      char root_path[4096];
      char pwd_path[4096];
};

And I was able to copy the path to root_path using copy_to_user and print it in the kernel log before returning from my system call.
After running my syscall test in a c program and running dmesg to see if it printed the root_path kernel side, it did print it.
But in user program root_path disappeared as when I execute the program it prints nothing. Can somebody please help me I dont know what I am doing wrong.


All times are GMT -5. The time now is 01:49 AM.