Find the answer to your Linux question:
Results 1 to 4 of 4
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 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    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 .

  2. #2
    Linux Newbie dilbert's Avatar
    Join Date
    Sep 2006
    Location
    Yorkshire, GB
    Posts
    237
    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.

  3. #3
    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

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Newbie dilbert's Avatar
    Join Date
    Sep 2006
    Location
    Yorkshire, GB
    Posts
    237
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •