2006-06-09 07:30:57 +00:00
|
|
|
/*
|
|
|
|
* Module for modifying the secmark field of the skb, for use by
|
|
|
|
* security subsystems.
|
|
|
|
*
|
|
|
|
* Based on the nfmark match by:
|
|
|
|
* (C) 1999-2001 Marc Boucher <marc@mbsi.ca>
|
|
|
|
*
|
2008-06-09 22:57:24 +00:00
|
|
|
* (C) 2006,2008 Red Hat, Inc., James Morris <jmorris@redhat.com>
|
2006-06-09 07:30:57 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/skbuff.h>
|
|
|
|
#include <linux/selinux.h>
|
|
|
|
#include <linux/netfilter/x_tables.h>
|
|
|
|
#include <linux/netfilter/xt_SECMARK.h>
|
|
|
|
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
MODULE_AUTHOR("James Morris <jmorris@redhat.com>");
|
2008-01-15 07:42:28 +00:00
|
|
|
MODULE_DESCRIPTION("Xtables: packet security mark modification");
|
2006-06-09 07:30:57 +00:00
|
|
|
MODULE_ALIAS("ipt_SECMARK");
|
|
|
|
MODULE_ALIAS("ip6t_SECMARK");
|
|
|
|
|
|
|
|
#define PFX "SECMARK: "
|
|
|
|
|
|
|
|
static u8 mode;
|
|
|
|
|
2007-12-05 07:24:03 +00:00
|
|
|
static unsigned int
|
|
|
|
secmark_tg(struct sk_buff *skb, const struct net_device *in,
|
|
|
|
const struct net_device *out, unsigned int hooknum,
|
|
|
|
const struct xt_target *target, const void *targinfo)
|
2006-06-09 07:30:57 +00:00
|
|
|
{
|
|
|
|
u32 secmark = 0;
|
|
|
|
const struct xt_secmark_target_info *info = targinfo;
|
|
|
|
|
|
|
|
BUG_ON(info->mode != mode);
|
|
|
|
|
|
|
|
switch (mode) {
|
|
|
|
case SECMARK_MODE_SEL:
|
|
|
|
secmark = info->u.sel.selsid;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
BUG();
|
|
|
|
}
|
|
|
|
|
2007-10-15 07:53:15 +00:00
|
|
|
skb->secmark = secmark;
|
2006-06-09 07:30:57 +00:00
|
|
|
return XT_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2007-07-08 05:16:26 +00:00
|
|
|
static bool checkentry_selinux(struct xt_secmark_target_info *info)
|
2006-06-09 07:30:57 +00:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
struct xt_secmark_target_selinux_info *sel = &info->u.sel;
|
2007-02-12 19:15:49 +00:00
|
|
|
|
2006-07-31 03:46:38 +00:00
|
|
|
sel->selctx[SECMARK_SELCTX_MAX - 1] = '\0';
|
2006-06-09 07:30:57 +00:00
|
|
|
|
|
|
|
err = selinux_string_to_sid(sel->selctx, &sel->selsid);
|
|
|
|
if (err) {
|
|
|
|
if (err == -EINVAL)
|
|
|
|
printk(KERN_INFO PFX "invalid SELinux context \'%s\'\n",
|
|
|
|
sel->selctx);
|
2007-07-08 05:16:26 +00:00
|
|
|
return false;
|
2006-06-09 07:30:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!sel->selsid) {
|
|
|
|
printk(KERN_INFO PFX "unable to map SELinux context \'%s\'\n",
|
|
|
|
sel->selctx);
|
2007-07-08 05:16:26 +00:00
|
|
|
return false;
|
2006-06-09 07:30:57 +00:00
|
|
|
}
|
|
|
|
|
2008-01-29 13:43:36 +00:00
|
|
|
err = selinux_secmark_relabel_packet_permission(sel->selsid);
|
2006-06-09 07:30:57 +00:00
|
|
|
if (err) {
|
|
|
|
printk(KERN_INFO PFX "unable to obtain relabeling permission\n");
|
2007-07-08 05:16:26 +00:00
|
|
|
return false;
|
2006-06-09 07:30:57 +00:00
|
|
|
}
|
|
|
|
|
2008-01-29 13:43:36 +00:00
|
|
|
selinux_secmark_refcount_inc();
|
2007-07-08 05:16:26 +00:00
|
|
|
return true;
|
2006-06-09 07:30:57 +00:00
|
|
|
}
|
|
|
|
|
2007-12-05 07:24:03 +00:00
|
|
|
static bool
|
|
|
|
secmark_tg_check(const char *tablename, const void *entry,
|
|
|
|
const struct xt_target *target, void *targinfo,
|
|
|
|
unsigned int hook_mask)
|
2006-06-09 07:30:57 +00:00
|
|
|
{
|
|
|
|
struct xt_secmark_target_info *info = targinfo;
|
|
|
|
|
2008-06-09 22:57:24 +00:00
|
|
|
if (strcmp(tablename, "mangle") && strcmp(tablename, "security")) {
|
|
|
|
printk(KERN_INFO PFX "target only valid in the \'mangle\' "
|
|
|
|
"or \'security\' tables, not \'%s\'.\n", tablename);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2006-06-09 07:30:57 +00:00
|
|
|
if (mode && mode != info->mode) {
|
|
|
|
printk(KERN_INFO PFX "mode already set to %hu cannot mix with "
|
|
|
|
"rules for mode %hu\n", mode, info->mode);
|
2007-07-08 05:16:26 +00:00
|
|
|
return false;
|
2006-06-09 07:30:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (info->mode) {
|
|
|
|
case SECMARK_MODE_SEL:
|
|
|
|
if (!checkentry_selinux(info))
|
2007-07-08 05:16:26 +00:00
|
|
|
return false;
|
2006-06-09 07:30:57 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
printk(KERN_INFO PFX "invalid mode: %hu\n", info->mode);
|
2007-07-08 05:16:26 +00:00
|
|
|
return false;
|
2006-06-09 07:30:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!mode)
|
|
|
|
mode = info->mode;
|
2007-07-08 05:16:26 +00:00
|
|
|
return true;
|
2006-06-09 07:30:57 +00:00
|
|
|
}
|
|
|
|
|
2008-02-14 01:41:39 +00:00
|
|
|
static void secmark_tg_destroy(const struct xt_target *target, void *targinfo)
|
2008-01-29 13:43:36 +00:00
|
|
|
{
|
|
|
|
switch (mode) {
|
|
|
|
case SECMARK_MODE_SEL:
|
|
|
|
selinux_secmark_refcount_dec();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-05 07:24:03 +00:00
|
|
|
static struct xt_target secmark_tg_reg[] __read_mostly = {
|
2006-08-22 07:34:04 +00:00
|
|
|
{
|
|
|
|
.name = "SECMARK",
|
2008-10-08 09:35:01 +00:00
|
|
|
.family = NFPROTO_IPV4,
|
2007-12-05 07:24:03 +00:00
|
|
|
.checkentry = secmark_tg_check,
|
2008-01-29 13:43:36 +00:00
|
|
|
.destroy = secmark_tg_destroy,
|
2007-12-05 07:24:03 +00:00
|
|
|
.target = secmark_tg,
|
2006-08-22 07:34:04 +00:00
|
|
|
.targetsize = sizeof(struct xt_secmark_target_info),
|
|
|
|
.me = THIS_MODULE,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.name = "SECMARK",
|
2008-10-08 09:35:01 +00:00
|
|
|
.family = NFPROTO_IPV6,
|
2007-12-05 07:24:03 +00:00
|
|
|
.checkentry = secmark_tg_check,
|
2008-01-29 13:43:36 +00:00
|
|
|
.destroy = secmark_tg_destroy,
|
2007-12-05 07:24:03 +00:00
|
|
|
.target = secmark_tg,
|
2006-08-22 07:34:04 +00:00
|
|
|
.targetsize = sizeof(struct xt_secmark_target_info),
|
|
|
|
.me = THIS_MODULE,
|
|
|
|
},
|
2006-06-09 07:30:57 +00:00
|
|
|
};
|
|
|
|
|
2007-12-05 07:24:03 +00:00
|
|
|
static int __init secmark_tg_init(void)
|
2006-06-09 07:30:57 +00:00
|
|
|
{
|
2007-12-05 07:24:03 +00:00
|
|
|
return xt_register_targets(secmark_tg_reg, ARRAY_SIZE(secmark_tg_reg));
|
2006-06-09 07:30:57 +00:00
|
|
|
}
|
|
|
|
|
2007-12-05 07:24:03 +00:00
|
|
|
static void __exit secmark_tg_exit(void)
|
2006-06-09 07:30:57 +00:00
|
|
|
{
|
2007-12-05 07:24:03 +00:00
|
|
|
xt_unregister_targets(secmark_tg_reg, ARRAY_SIZE(secmark_tg_reg));
|
2006-06-09 07:30:57 +00:00
|
|
|
}
|
|
|
|
|
2007-12-05 07:24:03 +00:00
|
|
|
module_init(secmark_tg_init);
|
|
|
|
module_exit(secmark_tg_exit);
|