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