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