All of lore.kernel.org
 help / color / mirror / Atom feed
From: Stephen Hemminger <shemminger@vyatta.com>
To: Stefan Hellkvist <hellkvist@gmail.com>
Cc: netfilter-devel@vger.kernel.org
Subject: Re: Understanding execution context of netfilter hooks
Date: Fri, 17 Jul 2009 15:57:03 -0700	[thread overview]
Message-ID: <20090717155703.0a70a8ae@nehalam> (raw)
In-Reply-To: <704c29930907170950l6e2f9881j83dbe338b9c03bdf@mail.gmail.com>

On Fri, 17 Jul 2009 18:50:52 +0200
Stefan Hellkvist <hellkvist@gmail.com> wrote:

> Hi,
> 
> Could anyone help me understand the execution context under which
> netfilter hooks are being executed? I played around with some code in
> order to learn things and noticed that the code executed differently
> in a netfilter hook than in, for instance, the init method of a module
> and I fail to understand why that is (possibly due to lack of
> understanding of the kernel in general).
> 
> I can give a very simplified example. Take the following rediculous
> code which reads a few bytes from a file in the file system (yes, a
> very unlikely example I know, but the question about writing or
> reading files from kernel space is not in my interest right now):
> 
> static void
> readshadow() {
>   struct file *fp;
>   char buf[1024];
> 
>   fp = filp_open("/etc/shadow", O_RDONLY, 0);
>   if (fp != NULL) {
>     int retval = kernel_read(fp, 0, buf, 20);
>     if (retval != 20) {
>       printk("disaster!\n");
>     }
>     buf[20] = '\0';
>     printk("first 20 chars: \"%s\"\n", buf);
>     filp_close(fp, 0);
>   }
> }


One of the repeating mantra's of kernel development is:
 "Don't do file i/o in kernel code."

Your code is wrong not just because kernel i/o can sleep,
but also because there really is not just one namespace,
so what is /etc/shadow!

The right way to do something like this is to read/parse
the file in a utility and pass the necessary data into the
kernel module through other mechanisms (/proc,netlink, debugfs, ...)
--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

  parent reply	other threads:[~2009-07-17 22:57 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-07-17 16:50 Understanding execution context of netfilter hooks Stefan Hellkvist
2009-07-17 19:19 ` Jan Engelhardt
2009-07-17 20:12   ` Stefan Hellkvist
2009-07-17 22:02     ` Jan Engelhardt
2009-07-17 22:57 ` Stephen Hemminger [this message]
2009-07-18  7:39   ` Stefan Hellkvist

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20090717155703.0a70a8ae@nehalam \
    --to=shemminger@vyatta.com \
    --cc=hellkvist@gmail.com \
    --cc=netfilter-devel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.