Daniel Borkmann | 82a3713 | 2013-12-29 18:27:12 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Xtables module to match the process control group. |
| 3 | * |
| 4 | * Might be used to implement individual "per-application" firewall |
| 5 | * policies in contrast to global policies based on control groups. |
| 6 | * Matching is based upon processes tagged to net_cls' classid marker. |
| 7 | * |
| 8 | * (C) 2013 Daniel Borkmann <dborkman@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 | #include <linux/skbuff.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/netfilter/x_tables.h> |
| 18 | #include <linux/netfilter/xt_cgroup.h> |
| 19 | #include <net/sock.h> |
| 20 | |
| 21 | MODULE_LICENSE("GPL"); |
| 22 | MODULE_AUTHOR("Daniel Borkmann <dborkman@redhat.com>"); |
| 23 | MODULE_DESCRIPTION("Xtables: process control group matching"); |
| 24 | MODULE_ALIAS("ipt_cgroup"); |
| 25 | MODULE_ALIAS("ip6t_cgroup"); |
| 26 | |
Tejun Heo | 4ec8ff0 | 2015-12-07 17:38:54 -0500 | [diff] [blame] | 27 | static int cgroup_mt_check_v0(const struct xt_mtchk_param *par) |
Daniel Borkmann | 82a3713 | 2013-12-29 18:27:12 +0100 | [diff] [blame] | 28 | { |
Tejun Heo | 4ec8ff0 | 2015-12-07 17:38:54 -0500 | [diff] [blame] | 29 | struct xt_cgroup_info_v0 *info = par->matchinfo; |
Daniel Borkmann | 82a3713 | 2013-12-29 18:27:12 +0100 | [diff] [blame] | 30 | |
| 31 | if (info->invert & ~1) |
| 32 | return -EINVAL; |
| 33 | |
Daniel Borkmann | caa8ad9 | 2014-08-18 15:46:28 +0200 | [diff] [blame] | 34 | return 0; |
Daniel Borkmann | 82a3713 | 2013-12-29 18:27:12 +0100 | [diff] [blame] | 35 | } |
| 36 | |
Tejun Heo | c38c459 | 2015-12-07 17:38:55 -0500 | [diff] [blame] | 37 | static int cgroup_mt_check_v1(const struct xt_mtchk_param *par) |
| 38 | { |
| 39 | struct xt_cgroup_info_v1 *info = par->matchinfo; |
| 40 | struct cgroup *cgrp; |
| 41 | |
| 42 | if ((info->invert_path & ~1) || (info->invert_classid & ~1)) |
| 43 | return -EINVAL; |
| 44 | |
| 45 | if (!info->has_path && !info->has_classid) { |
| 46 | pr_info("xt_cgroup: no path or classid specified\n"); |
| 47 | return -EINVAL; |
| 48 | } |
| 49 | |
| 50 | if (info->has_path && info->has_classid) { |
| 51 | pr_info("xt_cgroup: both path and classid specified\n"); |
| 52 | return -EINVAL; |
| 53 | } |
| 54 | |
Cong Wang | c6f3be7 | 2018-01-31 15:02:47 -0800 | [diff] [blame] | 55 | info->priv = NULL; |
Tejun Heo | c38c459 | 2015-12-07 17:38:55 -0500 | [diff] [blame] | 56 | if (info->has_path) { |
| 57 | cgrp = cgroup_get_from_path(info->path); |
| 58 | if (IS_ERR(cgrp)) { |
| 59 | pr_info("xt_cgroup: invalid path, errno=%ld\n", |
| 60 | PTR_ERR(cgrp)); |
| 61 | return -EINVAL; |
| 62 | } |
| 63 | info->priv = cgrp; |
| 64 | } |
| 65 | |
| 66 | return 0; |
| 67 | } |
| 68 | |
Daniel Borkmann | 82a3713 | 2013-12-29 18:27:12 +0100 | [diff] [blame] | 69 | static bool |
Tejun Heo | 4ec8ff0 | 2015-12-07 17:38:54 -0500 | [diff] [blame] | 70 | cgroup_mt_v0(const struct sk_buff *skb, struct xt_action_param *par) |
Daniel Borkmann | 82a3713 | 2013-12-29 18:27:12 +0100 | [diff] [blame] | 71 | { |
Tejun Heo | 4ec8ff0 | 2015-12-07 17:38:54 -0500 | [diff] [blame] | 72 | const struct xt_cgroup_info_v0 *info = par->matchinfo; |
Daniel Borkmann | 82a3713 | 2013-12-29 18:27:12 +0100 | [diff] [blame] | 73 | |
Daniel Borkmann | afb7718 | 2015-03-27 19:37:41 +0100 | [diff] [blame] | 74 | if (skb->sk == NULL || !sk_fullsock(skb->sk)) |
Daniel Borkmann | 82a3713 | 2013-12-29 18:27:12 +0100 | [diff] [blame] | 75 | return false; |
| 76 | |
Tejun Heo | 2a56a1f | 2015-12-07 17:38:52 -0500 | [diff] [blame] | 77 | return (info->id == sock_cgroup_classid(&skb->sk->sk_cgrp_data)) ^ |
| 78 | info->invert; |
Daniel Borkmann | 82a3713 | 2013-12-29 18:27:12 +0100 | [diff] [blame] | 79 | } |
| 80 | |
Tejun Heo | c38c459 | 2015-12-07 17:38:55 -0500 | [diff] [blame] | 81 | static bool cgroup_mt_v1(const struct sk_buff *skb, struct xt_action_param *par) |
| 82 | { |
| 83 | const struct xt_cgroup_info_v1 *info = par->matchinfo; |
| 84 | struct sock_cgroup_data *skcd = &skb->sk->sk_cgrp_data; |
| 85 | struct cgroup *ancestor = info->priv; |
| 86 | |
| 87 | if (!skb->sk || !sk_fullsock(skb->sk)) |
| 88 | return false; |
| 89 | |
| 90 | if (ancestor) |
| 91 | return cgroup_is_descendant(sock_cgroup_ptr(skcd), ancestor) ^ |
| 92 | info->invert_path; |
| 93 | else |
| 94 | return (info->classid == sock_cgroup_classid(skcd)) ^ |
| 95 | info->invert_classid; |
| 96 | } |
| 97 | |
| 98 | static void cgroup_mt_destroy_v1(const struct xt_mtdtor_param *par) |
| 99 | { |
| 100 | struct xt_cgroup_info_v1 *info = par->matchinfo; |
| 101 | |
| 102 | if (info->priv) |
| 103 | cgroup_put(info->priv); |
| 104 | } |
| 105 | |
Tejun Heo | 4ec8ff0 | 2015-12-07 17:38:54 -0500 | [diff] [blame] | 106 | static struct xt_match cgroup_mt_reg[] __read_mostly = { |
| 107 | { |
| 108 | .name = "cgroup", |
| 109 | .revision = 0, |
| 110 | .family = NFPROTO_UNSPEC, |
| 111 | .checkentry = cgroup_mt_check_v0, |
| 112 | .match = cgroup_mt_v0, |
| 113 | .matchsize = sizeof(struct xt_cgroup_info_v0), |
| 114 | .me = THIS_MODULE, |
| 115 | .hooks = (1 << NF_INET_LOCAL_OUT) | |
| 116 | (1 << NF_INET_POST_ROUTING) | |
| 117 | (1 << NF_INET_LOCAL_IN), |
| 118 | }, |
Tejun Heo | c38c459 | 2015-12-07 17:38:55 -0500 | [diff] [blame] | 119 | { |
| 120 | .name = "cgroup", |
| 121 | .revision = 1, |
| 122 | .family = NFPROTO_UNSPEC, |
| 123 | .checkentry = cgroup_mt_check_v1, |
| 124 | .match = cgroup_mt_v1, |
| 125 | .matchsize = sizeof(struct xt_cgroup_info_v1), |
| 126 | .destroy = cgroup_mt_destroy_v1, |
| 127 | .me = THIS_MODULE, |
| 128 | .hooks = (1 << NF_INET_LOCAL_OUT) | |
| 129 | (1 << NF_INET_POST_ROUTING) | |
| 130 | (1 << NF_INET_LOCAL_IN), |
| 131 | }, |
Daniel Borkmann | 82a3713 | 2013-12-29 18:27:12 +0100 | [diff] [blame] | 132 | }; |
| 133 | |
| 134 | static int __init cgroup_mt_init(void) |
| 135 | { |
Tejun Heo | 4ec8ff0 | 2015-12-07 17:38:54 -0500 | [diff] [blame] | 136 | return xt_register_matches(cgroup_mt_reg, ARRAY_SIZE(cgroup_mt_reg)); |
Daniel Borkmann | 82a3713 | 2013-12-29 18:27:12 +0100 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | static void __exit cgroup_mt_exit(void) |
| 140 | { |
Tejun Heo | 4ec8ff0 | 2015-12-07 17:38:54 -0500 | [diff] [blame] | 141 | xt_unregister_matches(cgroup_mt_reg, ARRAY_SIZE(cgroup_mt_reg)); |
Daniel Borkmann | 82a3713 | 2013-12-29 18:27:12 +0100 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | module_init(cgroup_mt_init); |
| 145 | module_exit(cgroup_mt_exit); |