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 |
4b560b44 | 27 | secmark_tg(struct sk_buff *skb, const struct xt_action_param *par) |
5e6874cd JM |
28 | { |
29 | u32 secmark = 0; | |
7eb35586 | 30 | const struct xt_secmark_target_info *info = par->targinfo; |
5e6874cd | 31 | |
5e6874cd JM |
32 | switch (mode) { |
33 | case SECMARK_MODE_SEL: | |
2606fd1f | 34 | secmark = info->secid; |
5e6874cd | 35 | break; |
5e6874cd JM |
36 | default: |
37 | BUG(); | |
38 | } | |
39 | ||
3db05fea | 40 | skb->secmark = secmark; |
5e6874cd JM |
41 | return XT_CONTINUE; |
42 | } | |
43 | ||
2606fd1f | 44 | static int checkentry_lsm(struct xt_secmark_target_info *info) |
5e6874cd JM |
45 | { |
46 | int err; | |
601e68e1 | 47 | |
2606fd1f EP |
48 | info->secctx[SECMARK_SECCTX_MAX - 1] = '\0'; |
49 | info->secid = 0; | |
5e6874cd | 50 | |
2606fd1f EP |
51 | err = security_secctx_to_secid(info->secctx, strlen(info->secctx), |
52 | &info->secid); | |
5e6874cd JM |
53 | if (err) { |
54 | if (err == -EINVAL) | |
b2606644 FW |
55 | pr_info_ratelimited("invalid security context \'%s\'\n", |
56 | info->secctx); | |
4a5a5c73 | 57 | return err; |
5e6874cd JM |
58 | } |
59 | ||
2606fd1f | 60 | if (!info->secid) { |
b2606644 FW |
61 | pr_info_ratelimited("unable to map security context \'%s\'\n", |
62 | info->secctx); | |
4a5a5c73 | 63 | return -ENOENT; |
5e6874cd JM |
64 | } |
65 | ||
2606fd1f | 66 | err = security_secmark_relabel_packet(info->secid); |
5e6874cd | 67 | if (err) { |
b2606644 | 68 | pr_info_ratelimited("unable to obtain relabeling permission\n"); |
4a5a5c73 | 69 | return err; |
5e6874cd JM |
70 | } |
71 | ||
2606fd1f | 72 | security_secmark_refcount_inc(); |
4a5a5c73 | 73 | return 0; |
5e6874cd JM |
74 | } |
75 | ||
135367b8 | 76 | static int secmark_tg_check(const struct xt_tgchk_param *par) |
5e6874cd | 77 | { |
af5d6dc2 | 78 | struct xt_secmark_target_info *info = par->targinfo; |
4a5a5c73 | 79 | int err; |
5e6874cd | 80 | |
af5d6dc2 JE |
81 | if (strcmp(par->table, "mangle") != 0 && |
82 | strcmp(par->table, "security") != 0) { | |
cc48baef FW |
83 | pr_info_ratelimited("only valid in \'mangle\' or \'security\' table, not \'%s\'\n", |
84 | par->table); | |
d6b00a53 | 85 | return -EINVAL; |
560ee653 JM |
86 | } |
87 | ||
5e6874cd | 88 | if (mode && mode != info->mode) { |
b2606644 FW |
89 | pr_info_ratelimited("mode already set to %hu cannot mix with rules for mode %hu\n", |
90 | mode, info->mode); | |
d6b00a53 | 91 | return -EINVAL; |
5e6874cd JM |
92 | } |
93 | ||
94 | switch (info->mode) { | |
95 | case SECMARK_MODE_SEL: | |
5e6874cd | 96 | break; |
5e6874cd | 97 | default: |
b2606644 | 98 | pr_info_ratelimited("invalid mode: %hu\n", info->mode); |
d6b00a53 | 99 | return -EINVAL; |
5e6874cd JM |
100 | } |
101 | ||
2606fd1f EP |
102 | err = checkentry_lsm(info); |
103 | if (err) | |
104 | return err; | |
105 | ||
5e6874cd JM |
106 | if (!mode) |
107 | mode = info->mode; | |
d6b00a53 | 108 | return 0; |
5e6874cd JM |
109 | } |
110 | ||
a2df1648 | 111 | static void secmark_tg_destroy(const struct xt_tgdtor_param *par) |
d621d35e PM |
112 | { |
113 | switch (mode) { | |
114 | case SECMARK_MODE_SEL: | |
2606fd1f | 115 | security_secmark_refcount_dec(); |
d621d35e PM |
116 | } |
117 | } | |
118 | ||
55b69e91 JE |
119 | static struct xt_target secmark_tg_reg __read_mostly = { |
120 | .name = "SECMARK", | |
121 | .revision = 0, | |
122 | .family = NFPROTO_UNSPEC, | |
123 | .checkentry = secmark_tg_check, | |
124 | .destroy = secmark_tg_destroy, | |
125 | .target = secmark_tg, | |
126 | .targetsize = sizeof(struct xt_secmark_target_info), | |
127 | .me = THIS_MODULE, | |
5e6874cd JM |
128 | }; |
129 | ||
d3c5ee6d | 130 | static int __init secmark_tg_init(void) |
5e6874cd | 131 | { |
55b69e91 | 132 | return xt_register_target(&secmark_tg_reg); |
5e6874cd JM |
133 | } |
134 | ||
d3c5ee6d | 135 | static void __exit secmark_tg_exit(void) |
5e6874cd | 136 | { |
55b69e91 | 137 | xt_unregister_target(&secmark_tg_reg); |
5e6874cd JM |
138 | } |
139 | ||
d3c5ee6d JE |
140 | module_init(secmark_tg_init); |
141 | module_exit(secmark_tg_exit); |