Commit | Line | Data |
---|---|---|
5e6874cd JM |
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 | * | |
560ee653 | 8 | * (C) 2006,2008 Red Hat, Inc., James Morris <jmorris@redhat.com> |
5e6874cd JM |
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 | #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 | ||
21 | MODULE_LICENSE("GPL"); | |
22 | MODULE_AUTHOR("James Morris <jmorris@redhat.com>"); | |
2ae15b64 | 23 | MODULE_DESCRIPTION("Xtables: packet security mark modification"); |
5e6874cd JM |
24 | MODULE_ALIAS("ipt_SECMARK"); |
25 | MODULE_ALIAS("ip6t_SECMARK"); | |
26 | ||
27 | #define PFX "SECMARK: " | |
28 | ||
29 | static u8 mode; | |
30 | ||
d3c5ee6d | 31 | static unsigned int |
7eb35586 | 32 | secmark_tg(struct sk_buff *skb, const struct xt_target_param *par) |
5e6874cd JM |
33 | { |
34 | u32 secmark = 0; | |
7eb35586 | 35 | const struct xt_secmark_target_info *info = par->targinfo; |
5e6874cd JM |
36 | |
37 | BUG_ON(info->mode != mode); | |
38 | ||
39 | switch (mode) { | |
40 | case SECMARK_MODE_SEL: | |
41 | secmark = info->u.sel.selsid; | |
42 | break; | |
43 | ||
44 | default: | |
45 | BUG(); | |
46 | } | |
47 | ||
3db05fea | 48 | skb->secmark = secmark; |
5e6874cd JM |
49 | return XT_CONTINUE; |
50 | } | |
51 | ||
e1931b78 | 52 | static bool checkentry_selinux(struct xt_secmark_target_info *info) |
5e6874cd JM |
53 | { |
54 | int err; | |
55 | struct xt_secmark_target_selinux_info *sel = &info->u.sel; | |
601e68e1 | 56 | |
a280b899 | 57 | sel->selctx[SECMARK_SELCTX_MAX - 1] = '\0'; |
5e6874cd JM |
58 | |
59 | err = selinux_string_to_sid(sel->selctx, &sel->selsid); | |
60 | if (err) { | |
61 | if (err == -EINVAL) | |
62 | printk(KERN_INFO PFX "invalid SELinux context \'%s\'\n", | |
63 | sel->selctx); | |
e1931b78 | 64 | return false; |
5e6874cd JM |
65 | } |
66 | ||
67 | if (!sel->selsid) { | |
68 | printk(KERN_INFO PFX "unable to map SELinux context \'%s\'\n", | |
69 | sel->selctx); | |
e1931b78 | 70 | return false; |
5e6874cd JM |
71 | } |
72 | ||
d621d35e | 73 | err = selinux_secmark_relabel_packet_permission(sel->selsid); |
5e6874cd JM |
74 | if (err) { |
75 | printk(KERN_INFO PFX "unable to obtain relabeling permission\n"); | |
e1931b78 | 76 | return false; |
5e6874cd JM |
77 | } |
78 | ||
d621d35e | 79 | selinux_secmark_refcount_inc(); |
e1931b78 | 80 | return true; |
5e6874cd JM |
81 | } |
82 | ||
af5d6dc2 | 83 | static bool secmark_tg_check(const struct xt_tgchk_param *par) |
5e6874cd | 84 | { |
af5d6dc2 | 85 | struct xt_secmark_target_info *info = par->targinfo; |
5e6874cd | 86 | |
af5d6dc2 JE |
87 | if (strcmp(par->table, "mangle") != 0 && |
88 | strcmp(par->table, "security") != 0) { | |
560ee653 | 89 | printk(KERN_INFO PFX "target only valid in the \'mangle\' " |
af5d6dc2 | 90 | "or \'security\' tables, not \'%s\'.\n", par->table); |
560ee653 JM |
91 | return false; |
92 | } | |
93 | ||
5e6874cd JM |
94 | if (mode && mode != info->mode) { |
95 | printk(KERN_INFO PFX "mode already set to %hu cannot mix with " | |
96 | "rules for mode %hu\n", mode, info->mode); | |
e1931b78 | 97 | return false; |
5e6874cd JM |
98 | } |
99 | ||
100 | switch (info->mode) { | |
101 | case SECMARK_MODE_SEL: | |
102 | if (!checkentry_selinux(info)) | |
e1931b78 | 103 | return false; |
5e6874cd JM |
104 | break; |
105 | ||
106 | default: | |
107 | printk(KERN_INFO PFX "invalid mode: %hu\n", info->mode); | |
e1931b78 | 108 | return false; |
5e6874cd JM |
109 | } |
110 | ||
111 | if (!mode) | |
112 | mode = info->mode; | |
e1931b78 | 113 | return true; |
5e6874cd JM |
114 | } |
115 | ||
a2df1648 | 116 | static void secmark_tg_destroy(const struct xt_tgdtor_param *par) |
d621d35e PM |
117 | { |
118 | switch (mode) { | |
119 | case SECMARK_MODE_SEL: | |
120 | selinux_secmark_refcount_dec(); | |
121 | } | |
122 | } | |
123 | ||
55b69e91 JE |
124 | static struct xt_target secmark_tg_reg __read_mostly = { |
125 | .name = "SECMARK", | |
126 | .revision = 0, | |
127 | .family = NFPROTO_UNSPEC, | |
128 | .checkentry = secmark_tg_check, | |
129 | .destroy = secmark_tg_destroy, | |
130 | .target = secmark_tg, | |
131 | .targetsize = sizeof(struct xt_secmark_target_info), | |
132 | .me = THIS_MODULE, | |
5e6874cd JM |
133 | }; |
134 | ||
d3c5ee6d | 135 | static int __init secmark_tg_init(void) |
5e6874cd | 136 | { |
55b69e91 | 137 | return xt_register_target(&secmark_tg_reg); |
5e6874cd JM |
138 | } |
139 | ||
d3c5ee6d | 140 | static void __exit secmark_tg_exit(void) |
5e6874cd | 141 | { |
55b69e91 | 142 | xt_unregister_target(&secmark_tg_reg); |
5e6874cd JM |
143 | } |
144 | ||
d3c5ee6d JE |
145 | module_init(secmark_tg_init); |
146 | module_exit(secmark_tg_exit); |