LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-30-2018, 03:32 AM   #1
Phuti
LQ Newbie
 
Registered: Jul 2018
Posts: 4

Rep: Reputation: Disabled
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?

Last edited by Phuti; 07-30-2018 at 03:34 AM.
 
Old 07-30-2018, 03:41 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,871
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
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 */
}

Last edited by NevemTeve; 07-30-2018 at 03:45 AM.
 
1 members found this post helpful.
Old 07-30-2018, 05:08 AM   #3
Phuti
LQ Newbie
 
Registered: Jul 2018
Posts: 4

Original Poster
Rep: Reputation: Disabled
I am not allowed to change the function prototype is there another way to do it???
 
Old 07-31-2018, 10:14 AM   #4
Phuti
LQ Newbie
 
Registered: Jul 2018
Posts: 4

Original Poster
Rep: Reputation: Disabled
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.
 
  


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
Free user space pages of different user processes from inside kernel space trueskyte Linux - Kernel 1 10-22-2010 04:37 PM
Division of Logical Memory Space in to User Space and Kernel Space shreshtha Linux - Newbie 2 01-14-2010 09:59 AM
Do we have any chance of calling user space callback function from kernel space? ravishankar.g Linux - Newbie 1 09-22-2009 07:14 PM
how to call socket prog code written in user space from kernel space???HELP kurt2 Programming 2 07-15-2009 09:56 PM
Increasing hard-disk space allocated to user guam Linux - General 10 03-01-2005 07:31 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:32 PM.

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