Welcome to Linux Forums! With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.
Write an article for LinuxForums Today! Win Great Prizes!
Find the answer to your Linux question:
New to Linux Forums? Register here for free!
    Linux Forums > GNU Linux Zone > The Linux Kernel > a question about kernel copy userspace data

Forgot Password?
 The Linux Kernel   Compiling, theory, programming or other discussion about the linux kernel

Site Navigation
Linux Articles
Linux Forums
Linux Downloads
Linux Hosting
Free Magazines
Job Board
IRC Chat
RSS Feeds
Linux Forum Topics
Linux Forums
Your Distro
Linux Resources
GNU Linux Zone
The Community
Reply
 
Thread Tools Display Modes
Old 03-28-2007   #1 (permalink)
Just Joined!
 
Join Date: Mar 2007
Posts: 5
a question about kernel copy userspace data

hello ,
i am programming a trial firewall based on netfilter ,which needs the module to access the data of user space ,so i use copy_from_user() but it can't work ,the code(simple test code) is like this:
-------user space program-----
#include <stdlib.h>
#include <stdio.h>

int main(int argc ,char *argv[]) {
char para[100]="insmod test.ko ";
int c=24;
unsigned int point=&c;
printf("%u\n",point);
char b[11]="0000000000"; /*convert the unsigned to chars */
int j=10;
int siz=0;
while(point>0) {
b[--j]=point%10+48;
point=point/10;
siz++;
}
char ips[30]="point=";
strcat(ips,&b[10-siz]);
strcat(para,ips);
puts(para);
system("make");
system(para);
}
-------kernel module-----------
#include <linux/module.h>
#include <linux/netfilter_ipv4.h>
#include <linux/tcp.h>
#include <linux/moduleparam.h>

MODULE_LICENSE("Dual BSD/GPL");

static unsigned int *point;
module_param(point, uint , S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
MODULE_PARM_DESC(point, "the pointer of user space");

static unsigned int
linuxmag_hook(unsigned int hook, struct sk_buff **pskb,
const struct net_device *indev, const
struct net_device *outdev, int
(*okfn)(struct sk_buff *))
{
printk("world\n");
printk(" %u\n",point);
int *b;
b=kmalloc(sizeof(int),GFP_KERNEL);
copy_from_user(b,(int *)point,sizeof(int));
printk("user space's value=%u\n",*b);
}

static struct nf_hook_ops linuxmag_ops
= { .hook = linuxmag_hook,
.owner = THIS_MODULE,
.pf = PF_INET,
.hooknum = NF_IP_LOCAL_OUT,
.priority = NF_IP_PRI_FILTER-1
};

static int __init init(void)
{
return nf_register_hook(&linuxmag_ops);
}

static void __exit fini(void)
{
nf_unregister_hook(&linuxmag_ops);
}
module_init(init);
module_exit(fini);

i check the /var/log/messages.
the output: the user space's value is 0
not the right value 24 which i defined in main().
this question puzzled me for a few days,and i am a fresh man about linux ,especially the kernel.is there anyone can help me?
any answer would be appreciated very much .
linwenyuan is offline  



Reply With Quote
Old 03-28-2007   #2 (permalink)
Linux Newbie
 
dilbert's Avatar
 
Join Date: Sep 2006
Location: Yorkshire, GB
Posts: 221
A few threads below yours is this one:

http://www.linuxforums.org/forum/lin...ss-kernel.html

In there, I inserted an example that is not complete as I simply deleted much of company specific code.

But If you make first an if-statement like in this example, then you can also print out with printk the return value from copy_from_user(). There is presumably something more than only EFAULT.

If this doesn't help, look into your kernel source for the search string "copy_from_user". There are many examples. Surely, difficult to sort out what is useful for you as many examples won't, but that's the best way I think.

I only remember that the structures you are using in this function, must be of a special type. "caddr_xxx" or so.

I think there were also manpages, not of copy_from_user() function name, but maybe you'll find something.

I'm sorry, but out of my head I cannot say more.

One last hint:
When I tried out this ioctl feature, I disabled the device, rmmod'ed the module and loaded it again with a short script. So, I compiled the driver, launched the script, and tested it, over and over without restarting the box.
__________________
Bus Error: Passengers dumped. Hech gap yo'q.
dilbert is offline   Reply With Quote
Old 03-29-2007   #3 (permalink)
Just Joined!
 
Join Date: Mar 2007
Posts: 5
ok ,thank you very much ,i will try under your guide .but as you say,it is difficult to sort out useful info from google ,i tried that ,but a few of them also are unsuccessful example....
anyhow,thanks
linwenyuan is offline   Reply With Quote
Old 03-29-2007   #4 (permalink)
Linux Newbie
 
dilbert's Avatar
 
Join Date: Sep 2006
Location: Yorkshire, GB
Posts: 221
Yes, in this case, reading the source code in the driver section of your Linux distribution can be helpful.

I thought this is the suggested way to learn Linux.

But if I remember right, the most important thing is the definition of that structure. The special definition is important to share kernel and user space.
__________________
Bus Error: Passengers dumped. Hech gap yo'q.
dilbert is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Free Magazines
A Newbie's Getting Started Guide to Linux
Learn the basics of the Linux operating systems. Get to know what it is all about, and familiarize yourself with the practical side. Basically, if you're a complete Linux newbie and looking for a quick and easy guide to get you started this is it.
subscribe
Run Your Own Web Server Using Linux & Apache - Free 191 Page Preview
Learn about everything you'll need to build and maintain your Linux servers, and to deploy Web applications to them.
subscribe
Open Source Security Myths Dispelled
Dispel the five major myths surrounding Open Source Security and gain the tools necessary to make a truly informed decision for your IT organization
subscribe
InformationWeek
InformationWeek is the only newsweekly you'll need to stay on top of the latest developments in information technology.
subscribe



All times are GMT. The time now is 07:21 PM.






© 2000 - - All Rights Reserved - Property of  MAS Media

Content Relevant URLs by vBSEO 3.3.1