Commit | Line | Data |
---|---|---|
d2912cb1 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
5e6874cd JM |
2 | /* |
3 | * Module for modifying the secmark field of the skb, for use by | |
4 | * security subsystems. | |
5 | * | |
6 | * Based on the nfmark match by: | |
7 | * (C) 1999-2001 Marc Boucher <marc@mbsi.ca> | |
8 | * | |
560ee653 | 9 | * (C) 2006,2008 Red Hat, Inc., James Morris <jmorris@redhat.com> |
5e6874cd | 10 | */ |
8bee4bad | 11 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
5e6874cd | 12 | #include <linux/module.h> |
2606fd1f | 13 | #include <linux/security.h> |
5e6874cd | 14 | #include <linux/skbuff.h> |
5e6874cd JM |
15 | #include <linux/netfilter/x_tables.h> |
16 | #include <linux/netfilter/xt_SECMARK.h> | |
17 | ||
18 | MODULE_LICENSE("GPL"); | |
19 | MODULE_AUTHOR("James Morris <jmorris@redhat.com>"); | |
2ae15b64 | 20 | MODULE_DESCRIPTION("Xtables: packet security mark modification"); |
5e6874cd JM |
21 | MODULE_ALIAS("ipt_SECMARK"); |
22 | MODULE_ALIAS("ip6t_SECMARK"); | |
23 | ||
5e6874cd JM |
24 | static u8 mode; |
25 | ||
d3c5ee6d | 26 | static unsigned int |
c7d13358 | 27 | secmark_tg(struct sk_buff *skb, const struct xt_secmark_target_info_v1 *info) |
5e6874cd JM |
28 | { |
29 | u32 secmark = 0; | |
5e6874cd | 30 | |
5e6874cd JM |
31 | switch (mode) { |
32 | case SECMARK_MODE_SEL: | |
2606fd1f | 33 | secmark = info->secid; |
5e6874cd | 34 | break; |
5e6874cd JM |
35 | default: |
36 | BUG(); | |
37 | } | |
38 | ||
3db05fea | 39 | skb->secmark = secmark; |
5e6874cd JM |
40 | return XT_CONTINUE; |
41 | } | |
42 | ||
c7d13358 | 43 | static int checkentry_lsm(struct xt_secmark_target_info_v1 *info) |
5e6874cd JM |
44 | { |
45 | int err; | |
601e68e1 | 46 | |
2606fd1f EP |
47 | info->secctx[SECMARK_SECCTX_MAX - 1] = '\0'; |
48 | info->secid = 0; | |
5e6874cd | 49 | |
2606fd1f EP |
50 | err = security_secctx_to_secid(info->secctx, strlen(info->secctx), |
51 | &info->secid); | |
5e6874cd JM |
52 | if (err) { |
53 | if (err == -EINVAL) | |
b2606644 FW |
54 | pr_info_ratelimited("invalid security context \'%s\'\n", |
55 | info->secctx); | |
4a5a5c73 | 56 | return err; |
5e6874cd JM |
57 | } |
58 | ||
2606fd1f | 59 | if (!info->secid) { |
b2606644 FW |
60 | pr_info_ratelimited("unable to map security context \'%s\'\n", |
61 | info->secctx); | |
4a5a5c73 | 62 | return -ENOENT; |
5e6874cd JM |
63 | } |
64 | ||
2606fd1f | 65 | err = security_secmark_relabel_packet(info->secid); |
5e6874cd | 66 | if (err) { |
b2606644 | 67 | pr_info_ratelimited("unable to obtain relabeling permission\n"); |
4a5a5c73 | 68 | return err; |
5e6874cd JM |
69 | } |
70 | ||
2606fd1f | 71 | security_secmark_refcount_inc(); |
4a5a5c73 | 72 | return 0; |
5e6874cd JM |
73 | } |
74 | ||
c7d13358 PNA |
75 | static int |
76 | secmark_tg_check(const char *table, struct xt_secmark_target_info_v1 *info) | |
5e6874cd | 77 | { |
4a5a5c73 | 78 | int err; |
5e6874cd | 79 | |
c7d13358 PNA |
80 | if (strcmp(table, "mangle") != 0 && |
81 | strcmp(table, "security") != 0) { | |
cc48baef | 82 | pr_info_ratelimited("only valid in \'mangle\' or \'security\' table, not \'%s\'\n", |
c7d13358 | 83 | table); |
d6b00a53 | 84 | return -EINVAL; |
560ee653 JM |
85 | } |
86 | ||
5e6874cd | 87 | if (mode && mode != info->mode) { |
b2606644 FW |
88 | pr_info_ratelimited("mode already set to %hu cannot mix with rules for mode %hu\n", |
89 | mode, info->mode); | |
d6b00a53 | 90 | return -EINVAL; |
5e6874cd JM |
91 | } |
92 | ||
93 | switch (info->mode) { | |
94 | case SECMARK_MODE_SEL: | |
5e6874cd | 95 | break; |
5e6874cd | 96 | default: |
b2606644 | 97 | pr_info_ratelimited("invalid mode: %hu\n", info->mode); |
d6b00a53 | 98 | return -EINVAL; |
5e6874cd JM |
99 | } |
100 | ||
2606fd1f EP |
101 | err = checkentry_lsm(info); |
102 | if (err) | |
103 | return err; | |
104 | ||
5e6874cd JM |
105 | if (!mode) |
106 | mode = info->mode; | |
d6b00a53 | 107 | return 0; |
5e6874cd JM |
108 | } |
109 | ||
a2df1648 | 110 | static void secmark_tg_destroy(const struct xt_tgdtor_param *par) |
d621d35e PM |
111 | { |
112 | switch (mode) { | |
113 | case SECMARK_MODE_SEL: | |
2606fd1f | 114 | security_secmark_refcount_dec(); |
d621d35e PM |
115 | } |
116 | } | |
117 | ||
c7d13358 PNA |
118 | static int secmark_tg_check_v0(const struct xt_tgchk_param *par) |
119 | { | |
120 | struct xt_secmark_target_info *info = par->targinfo; | |
121 | struct xt_secmark_target_info_v1 newinfo = { | |
122 | .mode = info->mode, | |
123 | }; | |
124 | int ret; | |
125 | ||
126 | memcpy(newinfo.secctx, info->secctx, SECMARK_SECCTX_MAX); | |
127 | ||
128 | ret = secmark_tg_check(par->table, &newinfo); | |
129 | info->secid = newinfo.secid; | |
130 | ||
131 | return ret; | |
132 | } | |
133 | ||
134 | static unsigned int | |
135 | secmark_tg_v0(struct sk_buff *skb, const struct xt_action_param *par) | |
136 | { | |
137 | const struct xt_secmark_target_info *info = par->targinfo; | |
138 | struct xt_secmark_target_info_v1 newinfo = { | |
139 | .secid = info->secid, | |
140 | }; | |
141 | ||
142 | return secmark_tg(skb, &newinfo); | |
143 | } | |
144 | ||
145 | static int secmark_tg_check_v1(const struct xt_tgchk_param *par) | |
146 | { | |
147 | return secmark_tg_check(par->table, par->targinfo); | |
148 | } | |
149 | ||
150 | static unsigned int | |
151 | secmark_tg_v1(struct sk_buff *skb, const struct xt_action_param *par) | |
152 | { | |
153 | return secmark_tg(skb, par->targinfo); | |
154 | } | |
155 | ||
156 | static struct xt_target secmark_tg_reg[] __read_mostly = { | |
157 | { | |
158 | .name = "SECMARK", | |
159 | .revision = 0, | |
160 | .family = NFPROTO_UNSPEC, | |
161 | .checkentry = secmark_tg_check_v0, | |
162 | .destroy = secmark_tg_destroy, | |
163 | .target = secmark_tg_v0, | |
164 | .targetsize = sizeof(struct xt_secmark_target_info), | |
165 | .me = THIS_MODULE, | |
166 | }, | |
167 | { | |
168 | .name = "SECMARK", | |
169 | .revision = 1, | |
170 | .family = NFPROTO_UNSPEC, | |
171 | .checkentry = secmark_tg_check_v1, | |
172 | .destroy = secmark_tg_destroy, | |
173 | .target = secmark_tg_v1, | |
174 | .targetsize = sizeof(struct xt_secmark_target_info_v1), | |
175 | .usersize = offsetof(struct xt_secmark_target_info_v1, secid), | |
176 | .me = THIS_MODULE, | |
177 | }, | |
5e6874cd JM |
178 | }; |
179 | ||
d3c5ee6d | 180 | static int __init secmark_tg_init(void) |
5e6874cd | 181 | { |
c7d13358 | 182 | return xt_register_targets(secmark_tg_reg, ARRAY_SIZE(secmark_tg_reg)); |
5e6874cd JM |
183 | } |
184 | ||
d3c5ee6d | 185 | static void __exit secmark_tg_exit(void) |
5e6874cd | 186 | { |
c7d13358 | 187 | xt_unregister_targets(secmark_tg_reg, ARRAY_SIZE(secmark_tg_reg)); |
5e6874cd JM |
188 | } |
189 | ||
d3c5ee6d JE |
190 | module_init(secmark_tg_init); |
191 | module_exit(secmark_tg_exit); |