2019-06-04 08:11:33 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
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
|
|
|
*/
|
2010-03-17 15:04:40 +00:00
|
|
|
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
|
2006-06-09 07:30:57 +00:00
|
|
|
#include <linux/module.h>
|
2010-10-13 20:24:41 +00:00
|
|
|
#include <linux/security.h>
|
2006-06-09 07:30:57 +00:00
|
|
|
#include <linux/skbuff.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");
|
|
|
|
|
|
|
|
static u8 mode;
|
|
|
|
|
2007-12-05 07:24:03 +00:00
|
|
|
static unsigned int
|
2021-04-30 12:00:13 +00:00
|
|
|
secmark_tg(struct sk_buff *skb, const struct xt_secmark_target_info_v1 *info)
|
2006-06-09 07:30:57 +00:00
|
|
|
{
|
|
|
|
u32 secmark = 0;
|
|
|
|
|
|
|
|
switch (mode) {
|
|
|
|
case SECMARK_MODE_SEL:
|
2010-10-13 20:24:41 +00:00
|
|
|
secmark = info->secid;
|
2006-06-09 07:30:57 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-04-30 12:00:13 +00:00
|
|
|
static int checkentry_lsm(struct xt_secmark_target_info_v1 *info)
|
2006-06-09 07:30:57 +00:00
|
|
|
{
|
|
|
|
int err;
|
2007-02-12 19:15:49 +00:00
|
|
|
|
2010-10-13 20:24:41 +00:00
|
|
|
info->secctx[SECMARK_SECCTX_MAX - 1] = '\0';
|
|
|
|
info->secid = 0;
|
2006-06-09 07:30:57 +00:00
|
|
|
|
2010-10-13 20:24:41 +00:00
|
|
|
err = security_secctx_to_secid(info->secctx, strlen(info->secctx),
|
|
|
|
&info->secid);
|
2006-06-09 07:30:57 +00:00
|
|
|
if (err) {
|
|
|
|
if (err == -EINVAL)
|
2018-02-09 14:52:07 +00:00
|
|
|
pr_info_ratelimited("invalid security context \'%s\'\n",
|
|
|
|
info->secctx);
|
2010-03-19 16:32:59 +00:00
|
|
|
return err;
|
2006-06-09 07:30:57 +00:00
|
|
|
}
|
|
|
|
|
2010-10-13 20:24:41 +00:00
|
|
|
if (!info->secid) {
|
2018-02-09 14:52:07 +00:00
|
|
|
pr_info_ratelimited("unable to map security context \'%s\'\n",
|
|
|
|
info->secctx);
|
2010-03-19 16:32:59 +00:00
|
|
|
return -ENOENT;
|
2006-06-09 07:30:57 +00:00
|
|
|
}
|
|
|
|
|
2010-10-13 20:24:41 +00:00
|
|
|
err = security_secmark_relabel_packet(info->secid);
|
2006-06-09 07:30:57 +00:00
|
|
|
if (err) {
|
2018-02-09 14:52:07 +00:00
|
|
|
pr_info_ratelimited("unable to obtain relabeling permission\n");
|
2010-03-19 16:32:59 +00:00
|
|
|
return err;
|
2006-06-09 07:30:57 +00:00
|
|
|
}
|
|
|
|
|
2010-10-13 20:24:41 +00:00
|
|
|
security_secmark_refcount_inc();
|
2010-03-19 16:32:59 +00:00
|
|
|
return 0;
|
2006-06-09 07:30:57 +00:00
|
|
|
}
|
|
|
|
|
2021-04-30 12:00:13 +00:00
|
|
|
static int
|
|
|
|
secmark_tg_check(const char *table, struct xt_secmark_target_info_v1 *info)
|
2006-06-09 07:30:57 +00:00
|
|
|
{
|
2010-03-19 16:32:59 +00:00
|
|
|
int err;
|
2006-06-09 07:30:57 +00:00
|
|
|
|
2021-04-30 12:00:13 +00:00
|
|
|
if (strcmp(table, "mangle") != 0 &&
|
|
|
|
strcmp(table, "security") != 0) {
|
2018-02-09 14:52:05 +00:00
|
|
|
pr_info_ratelimited("only valid in \'mangle\' or \'security\' table, not \'%s\'\n",
|
2021-04-30 12:00:13 +00:00
|
|
|
table);
|
2010-03-25 15:34:45 +00:00
|
|
|
return -EINVAL;
|
2008-06-09 22:57:24 +00:00
|
|
|
}
|
|
|
|
|
2006-06-09 07:30:57 +00:00
|
|
|
if (mode && mode != info->mode) {
|
2018-02-09 14:52:07 +00:00
|
|
|
pr_info_ratelimited("mode already set to %hu cannot mix with rules for mode %hu\n",
|
|
|
|
mode, info->mode);
|
2010-03-25 15:34:45 +00:00
|
|
|
return -EINVAL;
|
2006-06-09 07:30:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (info->mode) {
|
|
|
|
case SECMARK_MODE_SEL:
|
|
|
|
break;
|
|
|
|
default:
|
2018-02-09 14:52:07 +00:00
|
|
|
pr_info_ratelimited("invalid mode: %hu\n", info->mode);
|
2010-03-25 15:34:45 +00:00
|
|
|
return -EINVAL;
|
2006-06-09 07:30:57 +00:00
|
|
|
}
|
|
|
|
|
2010-10-13 20:24:41 +00:00
|
|
|
err = checkentry_lsm(info);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
2006-06-09 07:30:57 +00:00
|
|
|
if (!mode)
|
|
|
|
mode = info->mode;
|
2010-03-25 15:34:45 +00:00
|
|
|
return 0;
|
2006-06-09 07:30:57 +00:00
|
|
|
}
|
|
|
|
|
2008-10-08 09:35:19 +00:00
|
|
|
static void secmark_tg_destroy(const struct xt_tgdtor_param *par)
|
2008-01-29 13:43:36 +00:00
|
|
|
{
|
|
|
|
switch (mode) {
|
|
|
|
case SECMARK_MODE_SEL:
|
2010-10-13 20:24:41 +00:00
|
|
|
security_secmark_refcount_dec();
|
2008-01-29 13:43:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-30 12:00:13 +00:00
|
|
|
static int secmark_tg_check_v0(const struct xt_tgchk_param *par)
|
|
|
|
{
|
|
|
|
struct xt_secmark_target_info *info = par->targinfo;
|
|
|
|
struct xt_secmark_target_info_v1 newinfo = {
|
|
|
|
.mode = info->mode,
|
|
|
|
};
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
memcpy(newinfo.secctx, info->secctx, SECMARK_SECCTX_MAX);
|
|
|
|
|
|
|
|
ret = secmark_tg_check(par->table, &newinfo);
|
|
|
|
info->secid = newinfo.secid;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned int
|
|
|
|
secmark_tg_v0(struct sk_buff *skb, const struct xt_action_param *par)
|
|
|
|
{
|
|
|
|
const struct xt_secmark_target_info *info = par->targinfo;
|
|
|
|
struct xt_secmark_target_info_v1 newinfo = {
|
|
|
|
.secid = info->secid,
|
|
|
|
};
|
|
|
|
|
|
|
|
return secmark_tg(skb, &newinfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int secmark_tg_check_v1(const struct xt_tgchk_param *par)
|
|
|
|
{
|
|
|
|
return secmark_tg_check(par->table, par->targinfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned int
|
|
|
|
secmark_tg_v1(struct sk_buff *skb, const struct xt_action_param *par)
|
|
|
|
{
|
|
|
|
return secmark_tg(skb, par->targinfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct xt_target secmark_tg_reg[] __read_mostly = {
|
|
|
|
{
|
|
|
|
.name = "SECMARK",
|
|
|
|
.revision = 0,
|
|
|
|
.family = NFPROTO_UNSPEC,
|
|
|
|
.checkentry = secmark_tg_check_v0,
|
|
|
|
.destroy = secmark_tg_destroy,
|
|
|
|
.target = secmark_tg_v0,
|
|
|
|
.targetsize = sizeof(struct xt_secmark_target_info),
|
|
|
|
.me = THIS_MODULE,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.name = "SECMARK",
|
|
|
|
.revision = 1,
|
|
|
|
.family = NFPROTO_UNSPEC,
|
|
|
|
.checkentry = secmark_tg_check_v1,
|
|
|
|
.destroy = secmark_tg_destroy,
|
|
|
|
.target = secmark_tg_v1,
|
|
|
|
.targetsize = sizeof(struct xt_secmark_target_info_v1),
|
|
|
|
.usersize = offsetof(struct xt_secmark_target_info_v1, secid),
|
|
|
|
.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
|
|
|
{
|
2021-04-30 12:00:13 +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
|
|
|
{
|
2021-04-30 12:00:13 +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);
|