blob: 94f87ee7552b0708b855ce998a3a355e71b13af4 [file] [log] [blame]
James Morris5e6874cd2006-06-09 00:30:57 -07001/*
2 * Module for modifying the secmark field of the skb, for use by
3 * security subsystems.
4 *
5 * Based on the nfmark match by:
6 * (C) 1999-2001 Marc Boucher <marc@mbsi.ca>
7 *
James Morris560ee652008-06-09 15:57:24 -07008 * (C) 2006,2008 Red Hat, Inc., James Morris <jmorris@redhat.com>
James Morris5e6874cd2006-06-09 00:30:57 -07009 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 *
14 */
15#include <linux/module.h>
16#include <linux/skbuff.h>
17#include <linux/selinux.h>
18#include <linux/netfilter/x_tables.h>
19#include <linux/netfilter/xt_SECMARK.h>
20
21MODULE_LICENSE("GPL");
22MODULE_AUTHOR("James Morris <jmorris@redhat.com>");
Jan Engelhardt2ae15b62008-01-14 23:42:28 -080023MODULE_DESCRIPTION("Xtables: packet security mark modification");
James Morris5e6874cd2006-06-09 00:30:57 -070024MODULE_ALIAS("ipt_SECMARK");
25MODULE_ALIAS("ip6t_SECMARK");
26
27#define PFX "SECMARK: "
28
29static u8 mode;
30
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080031static unsigned int
32secmark_tg(struct sk_buff *skb, const struct net_device *in,
33 const struct net_device *out, unsigned int hooknum,
34 const struct xt_target *target, const void *targinfo)
James Morris5e6874cd2006-06-09 00:30:57 -070035{
36 u32 secmark = 0;
37 const struct xt_secmark_target_info *info = targinfo;
38
39 BUG_ON(info->mode != mode);
40
41 switch (mode) {
42 case SECMARK_MODE_SEL:
43 secmark = info->u.sel.selsid;
44 break;
45
46 default:
47 BUG();
48 }
49
Herbert Xu3db05fe2007-10-15 00:53:15 -070050 skb->secmark = secmark;
James Morris5e6874cd2006-06-09 00:30:57 -070051 return XT_CONTINUE;
52}
53
Jan Engelhardte1931b72007-07-07 22:16:26 -070054static bool checkentry_selinux(struct xt_secmark_target_info *info)
James Morris5e6874cd2006-06-09 00:30:57 -070055{
56 int err;
57 struct xt_secmark_target_selinux_info *sel = &info->u.sel;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080058
James Morrisa280b892006-07-30 20:46:38 -070059 sel->selctx[SECMARK_SELCTX_MAX - 1] = '\0';
James Morris5e6874cd2006-06-09 00:30:57 -070060
61 err = selinux_string_to_sid(sel->selctx, &sel->selsid);
62 if (err) {
63 if (err == -EINVAL)
64 printk(KERN_INFO PFX "invalid SELinux context \'%s\'\n",
65 sel->selctx);
Jan Engelhardte1931b72007-07-07 22:16:26 -070066 return false;
James Morris5e6874cd2006-06-09 00:30:57 -070067 }
68
69 if (!sel->selsid) {
70 printk(KERN_INFO PFX "unable to map SELinux context \'%s\'\n",
71 sel->selctx);
Jan Engelhardte1931b72007-07-07 22:16:26 -070072 return false;
James Morris5e6874cd2006-06-09 00:30:57 -070073 }
74
Paul Moored621d352008-01-29 08:43:36 -050075 err = selinux_secmark_relabel_packet_permission(sel->selsid);
James Morris5e6874cd2006-06-09 00:30:57 -070076 if (err) {
77 printk(KERN_INFO PFX "unable to obtain relabeling permission\n");
Jan Engelhardte1931b72007-07-07 22:16:26 -070078 return false;
James Morris5e6874cd2006-06-09 00:30:57 -070079 }
80
Paul Moored621d352008-01-29 08:43:36 -050081 selinux_secmark_refcount_inc();
Jan Engelhardte1931b72007-07-07 22:16:26 -070082 return true;
James Morris5e6874cd2006-06-09 00:30:57 -070083}
84
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080085static bool
86secmark_tg_check(const char *tablename, const void *entry,
87 const struct xt_target *target, void *targinfo,
88 unsigned int hook_mask)
James Morris5e6874cd2006-06-09 00:30:57 -070089{
90 struct xt_secmark_target_info *info = targinfo;
91
James Morris560ee652008-06-09 15:57:24 -070092 if (strcmp(tablename, "mangle") && strcmp(tablename, "security")) {
93 printk(KERN_INFO PFX "target only valid in the \'mangle\' "
94 "or \'security\' tables, not \'%s\'.\n", tablename);
95 return false;
96 }
97
James Morris5e6874cd2006-06-09 00:30:57 -070098 if (mode && mode != info->mode) {
99 printk(KERN_INFO PFX "mode already set to %hu cannot mix with "
100 "rules for mode %hu\n", mode, info->mode);
Jan Engelhardte1931b72007-07-07 22:16:26 -0700101 return false;
James Morris5e6874cd2006-06-09 00:30:57 -0700102 }
103
104 switch (info->mode) {
105 case SECMARK_MODE_SEL:
106 if (!checkentry_selinux(info))
Jan Engelhardte1931b72007-07-07 22:16:26 -0700107 return false;
James Morris5e6874cd2006-06-09 00:30:57 -0700108 break;
109
110 default:
111 printk(KERN_INFO PFX "invalid mode: %hu\n", info->mode);
Jan Engelhardte1931b72007-07-07 22:16:26 -0700112 return false;
James Morris5e6874cd2006-06-09 00:30:57 -0700113 }
114
115 if (!mode)
116 mode = info->mode;
Jan Engelhardte1931b72007-07-07 22:16:26 -0700117 return true;
James Morris5e6874cd2006-06-09 00:30:57 -0700118}
119
Adrian Bunkf51f5ec2008-02-13 17:41:39 -0800120static void secmark_tg_destroy(const struct xt_target *target, void *targinfo)
Paul Moored621d352008-01-29 08:43:36 -0500121{
122 switch (mode) {
123 case SECMARK_MODE_SEL:
124 selinux_secmark_refcount_dec();
125 }
126}
127
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800128static struct xt_target secmark_tg_reg[] __read_mostly = {
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700129 {
130 .name = "SECMARK",
131 .family = AF_INET,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800132 .checkentry = secmark_tg_check,
Paul Moored621d352008-01-29 08:43:36 -0500133 .destroy = secmark_tg_destroy,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800134 .target = secmark_tg,
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700135 .targetsize = sizeof(struct xt_secmark_target_info),
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700136 .me = THIS_MODULE,
137 },
138 {
139 .name = "SECMARK",
140 .family = AF_INET6,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800141 .checkentry = secmark_tg_check,
Paul Moored621d352008-01-29 08:43:36 -0500142 .destroy = secmark_tg_destroy,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800143 .target = secmark_tg,
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700144 .targetsize = sizeof(struct xt_secmark_target_info),
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700145 .me = THIS_MODULE,
146 },
James Morris5e6874cd2006-06-09 00:30:57 -0700147};
148
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800149static int __init secmark_tg_init(void)
James Morris5e6874cd2006-06-09 00:30:57 -0700150{
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800151 return xt_register_targets(secmark_tg_reg, ARRAY_SIZE(secmark_tg_reg));
James Morris5e6874cd2006-06-09 00:30:57 -0700152}
153
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800154static void __exit secmark_tg_exit(void)
James Morris5e6874cd2006-06-09 00:30:57 -0700155{
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800156 xt_unregister_targets(secmark_tg_reg, ARRAY_SIZE(secmark_tg_reg));
James Morris5e6874cd2006-06-09 00:30:57 -0700157}
158
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800159module_init(secmark_tg_init);
160module_exit(secmark_tg_exit);