xt_cgroup.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. #include <linux/skbuff.h>
  15. #include <linux/module.h>
  16. #include <linux/netfilter/x_tables.h>
  17. #include <linux/netfilter/xt_cgroup.h>
  18. #include <net/sock.h>
  19. MODULE_LICENSE("GPL");
  20. MODULE_AUTHOR("Daniel Borkmann <dborkman@redhat.com>");
  21. MODULE_DESCRIPTION("Xtables: process control group matching");
  22. MODULE_ALIAS("ipt_cgroup");
  23. MODULE_ALIAS("ip6t_cgroup");
  24. static int cgroup_mt_check_v0(const struct xt_mtchk_param *par)
  25. {
  26. struct xt_cgroup_info_v0 *info = par->matchinfo;
  27. if (info->invert & ~1)
  28. return -EINVAL;
  29. return 0;
  30. }
  31. static int cgroup_mt_check_v1(const struct xt_mtchk_param *par)
  32. {
  33. struct xt_cgroup_info_v1 *info = par->matchinfo;
  34. struct cgroup *cgrp;
  35. if ((info->invert_path & ~1) || (info->invert_classid & ~1))
  36. return -EINVAL;
  37. if (!info->has_path && !info->has_classid) {
  38. pr_info("xt_cgroup: no path or classid specified\n");
  39. return -EINVAL;
  40. }
  41. if (info->has_path && info->has_classid) {
  42. pr_info("xt_cgroup: both path and classid specified\n");
  43. return -EINVAL;
  44. }
  45. info->priv = NULL;
  46. if (info->has_path) {
  47. cgrp = cgroup_get_from_path(info->path);
  48. if (IS_ERR(cgrp)) {
  49. pr_info("xt_cgroup: invalid path, errno=%ld\n",
  50. PTR_ERR(cgrp));
  51. return -EINVAL;
  52. }
  53. info->priv = cgrp;
  54. }
  55. return 0;
  56. }
  57. static bool
  58. cgroup_mt_v0(const struct sk_buff *skb, struct xt_action_param *par)
  59. {
  60. const struct xt_cgroup_info_v0 *info = par->matchinfo;
  61. if (skb->sk == NULL || !sk_fullsock(skb->sk))
  62. return false;
  63. return (info->id == sock_cgroup_classid(&skb->sk->sk_cgrp_data)) ^
  64. info->invert;
  65. }
  66. static bool cgroup_mt_v1(const struct sk_buff *skb, struct xt_action_param *par)
  67. {
  68. const struct xt_cgroup_info_v1 *info = par->matchinfo;
  69. struct sock_cgroup_data *skcd = &skb->sk->sk_cgrp_data;
  70. struct cgroup *ancestor = info->priv;
  71. if (!skb->sk || !sk_fullsock(skb->sk))
  72. return false;
  73. if (ancestor)
  74. return cgroup_is_descendant(sock_cgroup_ptr(skcd), ancestor) ^
  75. info->invert_path;
  76. else
  77. return (info->classid == sock_cgroup_classid(skcd)) ^
  78. info->invert_classid;
  79. }
  80. static void cgroup_mt_destroy_v1(const struct xt_mtdtor_param *par)
  81. {
  82. struct xt_cgroup_info_v1 *info = par->matchinfo;
  83. if (info->priv)
  84. cgroup_put(info->priv);
  85. }
  86. static struct xt_match cgroup_mt_reg[] __read_mostly = {
  87. {
  88. .name = "cgroup",
  89. .revision = 0,
  90. .family = NFPROTO_UNSPEC,
  91. .checkentry = cgroup_mt_check_v0,
  92. .match = cgroup_mt_v0,
  93. .matchsize = sizeof(struct xt_cgroup_info_v0),
  94. .me = THIS_MODULE,
  95. .hooks = (1 << NF_INET_LOCAL_OUT) |
  96. (1 << NF_INET_POST_ROUTING) |
  97. (1 << NF_INET_LOCAL_IN),
  98. },
  99. {
  100. .name = "cgroup",
  101. .revision = 1,
  102. .family = NFPROTO_UNSPEC,
  103. .checkentry = cgroup_mt_check_v1,
  104. .match = cgroup_mt_v1,
  105. .matchsize = sizeof(struct xt_cgroup_info_v1),
  106. .destroy = cgroup_mt_destroy_v1,
  107. .me = THIS_MODULE,
  108. .hooks = (1 << NF_INET_LOCAL_OUT) |
  109. (1 << NF_INET_POST_ROUTING) |
  110. (1 << NF_INET_LOCAL_IN),
  111. },
  112. };
  113. static int __init cgroup_mt_init(void)
  114. {
  115. return xt_register_matches(cgroup_mt_reg, ARRAY_SIZE(cgroup_mt_reg));
  116. }
  117. static void __exit cgroup_mt_exit(void)
  118. {
  119. xt_unregister_matches(cgroup_mt_reg, ARRAY_SIZE(cgroup_mt_reg));
  120. }
  121. module_init(cgroup_mt_init);
  122. module_exit(cgroup_mt_exit);