blob: 7a6f9e6f5dfaa6b7e4e5eb57890542a99065b0e3 [file] [log] [blame]
James Morris5e6874cd2006-06-09 00:30:57 -07001/*
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 *
James Morris560ee652008-06-09 15:57:24 -07008 * (C) 2006,2008 Red Hat, Inc., James Morris <jmorris@redhat.com>
James Morris5e6874cd2006-06-09 00:30:57 -07009 *
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
21MODULE_LICENSE("GPL");
22MODULE_AUTHOR("James Morris <jmorris@redhat.com>");
Jan Engelhardt2ae15b62008-01-14 23:42:28 -080023MODULE_DESCRIPTION("Xtables: packet security mark modification");
James Morris5e6874cd2006-06-09 00:30:57 -070024MODULE_ALIAS("ipt_SECMARK");
25MODULE_ALIAS("ip6t_SECMARK");
26
27#define PFX "SECMARK: "
28
29static u8 mode;
30
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080031static unsigned int
Jan Engelhardt7eb35582008-10-08 11:35:19 +020032secmark_tg(struct sk_buff *skb, const struct xt_target_param *par)
James Morris5e6874cd2006-06-09 00:30:57 -070033{
34 u32 secmark = 0;
Jan Engelhardt7eb35582008-10-08 11:35:19 +020035 const struct xt_secmark_target_info *info = par->targinfo;
James Morris5e6874cd2006-06-09 00:30:57 -070036
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
Herbert Xu3db05fe2007-10-15 00:53:15 -070048 skb->secmark = secmark;
James Morris5e6874cd2006-06-09 00:30:57 -070049 return XT_CONTINUE;
50}
51
Jan Engelhardte1931b72007-07-07 22:16:26 -070052static bool checkentry_selinux(struct xt_secmark_target_info *info)
James Morris5e6874cd2006-06-09 00:30:57 -070053{
54 int err;
55 struct xt_secmark_target_selinux_info *sel = &info->u.sel;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080056
James Morrisa280b892006-07-30 20:46:38 -070057 sel->selctx[SECMARK_SELCTX_MAX - 1] = '\0';
James Morris5e6874cd2006-06-09 00:30:57 -070058
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);
Jan Engelhardte1931b72007-07-07 22:16:26 -070064 return false;
James Morris5e6874cd2006-06-09 00:30:57 -070065 }
66
67 if (!sel->selsid) {
68 printk(KERN_INFO PFX "unable to map SELinux context \'%s\'\n",
69 sel->selctx);
Jan Engelhardte1931b72007-07-07 22:16:26 -070070 return false;
James Morris5e6874cd2006-06-09 00:30:57 -070071 }
72
Paul Moored621d352008-01-29 08:43:36 -050073 err = selinux_secmark_relabel_packet_permission(sel->selsid);
James Morris5e6874cd2006-06-09 00:30:57 -070074 if (err) {
75 printk(KERN_INFO PFX "unable to obtain relabeling permission\n");
Jan Engelhardte1931b72007-07-07 22:16:26 -070076 return false;
James Morris5e6874cd2006-06-09 00:30:57 -070077 }
78
Paul Moored621d352008-01-29 08:43:36 -050079 selinux_secmark_refcount_inc();
Jan Engelhardte1931b72007-07-07 22:16:26 -070080 return true;
James Morris5e6874cd2006-06-09 00:30:57 -070081}
82
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +020083static bool secmark_tg_check(const struct xt_tgchk_param *par)
James Morris5e6874cd2006-06-09 00:30:57 -070084{
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +020085 struct xt_secmark_target_info *info = par->targinfo;
James Morris5e6874cd2006-06-09 00:30:57 -070086
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +020087 if (strcmp(par->table, "mangle") != 0 &&
88 strcmp(par->table, "security") != 0) {
James Morris560ee652008-06-09 15:57:24 -070089 printk(KERN_INFO PFX "target only valid in the \'mangle\' "
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +020090 "or \'security\' tables, not \'%s\'.\n", par->table);
James Morris560ee652008-06-09 15:57:24 -070091 return false;
92 }
93
James Morris5e6874cd2006-06-09 00:30:57 -070094 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);
Jan Engelhardte1931b72007-07-07 22:16:26 -070097 return false;
James Morris5e6874cd2006-06-09 00:30:57 -070098 }
99
100 switch (info->mode) {
101 case SECMARK_MODE_SEL:
102 if (!checkentry_selinux(info))
Jan Engelhardte1931b72007-07-07 22:16:26 -0700103 return false;
James Morris5e6874cd2006-06-09 00:30:57 -0700104 break;
105
106 default:
107 printk(KERN_INFO PFX "invalid mode: %hu\n", info->mode);
Jan Engelhardte1931b72007-07-07 22:16:26 -0700108 return false;
James Morris5e6874cd2006-06-09 00:30:57 -0700109 }
110
111 if (!mode)
112 mode = info->mode;
Jan Engelhardte1931b72007-07-07 22:16:26 -0700113 return true;
James Morris5e6874cd2006-06-09 00:30:57 -0700114}
115
Jan Engelhardta2df1642008-10-08 11:35:19 +0200116static void secmark_tg_destroy(const struct xt_tgdtor_param *par)
Paul Moored621d352008-01-29 08:43:36 -0500117{
118 switch (mode) {
119 case SECMARK_MODE_SEL:
120 selinux_secmark_refcount_dec();
121 }
122}
123
Jan Engelhardt55b69e92008-10-08 11:35:01 +0200124static 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,
James Morris5e6874cd2006-06-09 00:30:57 -0700133};
134
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800135static int __init secmark_tg_init(void)
James Morris5e6874cd2006-06-09 00:30:57 -0700136{
Jan Engelhardt55b69e92008-10-08 11:35:01 +0200137 return xt_register_target(&secmark_tg_reg);
James Morris5e6874cd2006-06-09 00:30:57 -0700138}
139
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800140static void __exit secmark_tg_exit(void)
James Morris5e6874cd2006-06-09 00:30:57 -0700141{
Jan Engelhardt55b69e92008-10-08 11:35:01 +0200142 xt_unregister_target(&secmark_tg_reg);
James Morris5e6874cd2006-06-09 00:30:57 -0700143}
144
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800145module_init(secmark_tg_init);
146module_exit(secmark_tg_exit);