xt_owner.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Kernel module to match various things tied to sockets associated with
  3. * locally generated outgoing packets.
  4. *
  5. * (C) 2000 Marc Boucher <marc@mbsi.ca>
  6. *
  7. * Copyright © CC Computer Consultants GmbH, 2007 - 2008
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/skbuff.h>
  15. #include <linux/file.h>
  16. #include <net/sock.h>
  17. #include <net/inet_sock.h>
  18. #include <linux/netfilter/x_tables.h>
  19. #include <linux/netfilter/xt_owner.h>
  20. static int owner_check(const struct xt_mtchk_param *par)
  21. {
  22. struct xt_owner_match_info *info = par->matchinfo;
  23. struct net *net = par->net;
  24. /* Only allow the common case where the userns of the writer
  25. * matches the userns of the network namespace.
  26. */
  27. if ((info->match & (XT_OWNER_UID|XT_OWNER_GID)) &&
  28. (current_user_ns() != net->user_ns))
  29. return -EINVAL;
  30. /* Ensure the uids are valid */
  31. if (info->match & XT_OWNER_UID) {
  32. kuid_t uid_min = make_kuid(net->user_ns, info->uid_min);
  33. kuid_t uid_max = make_kuid(net->user_ns, info->uid_max);
  34. if (!uid_valid(uid_min) || !uid_valid(uid_max) ||
  35. (info->uid_max < info->uid_min) ||
  36. uid_lt(uid_max, uid_min)) {
  37. return -EINVAL;
  38. }
  39. }
  40. /* Ensure the gids are valid */
  41. if (info->match & XT_OWNER_GID) {
  42. kgid_t gid_min = make_kgid(net->user_ns, info->gid_min);
  43. kgid_t gid_max = make_kgid(net->user_ns, info->gid_max);
  44. if (!gid_valid(gid_min) || !gid_valid(gid_max) ||
  45. (info->gid_max < info->gid_min) ||
  46. gid_lt(gid_max, gid_min)) {
  47. return -EINVAL;
  48. }
  49. }
  50. return 0;
  51. }
  52. static bool
  53. owner_mt(const struct sk_buff *skb, struct xt_action_param *par)
  54. {
  55. const struct xt_owner_match_info *info = par->matchinfo;
  56. const struct file *filp;
  57. struct sock *sk = skb_to_full_sk(skb);
  58. struct net *net = par->net;
  59. if (sk == NULL || sk->sk_socket == NULL)
  60. return (info->match ^ info->invert) == 0;
  61. else if (info->match & info->invert & XT_OWNER_SOCKET)
  62. /*
  63. * Socket exists but user wanted ! --socket-exists.
  64. * (Single ampersands intended.)
  65. */
  66. return false;
  67. filp = sk->sk_socket->file;
  68. if (filp == NULL)
  69. return ((info->match ^ info->invert) &
  70. (XT_OWNER_UID | XT_OWNER_GID)) == 0;
  71. if (info->match & XT_OWNER_UID) {
  72. kuid_t uid_min = make_kuid(net->user_ns, info->uid_min);
  73. kuid_t uid_max = make_kuid(net->user_ns, info->uid_max);
  74. if ((uid_gte(filp->f_cred->fsuid, uid_min) &&
  75. uid_lte(filp->f_cred->fsuid, uid_max)) ^
  76. !(info->invert & XT_OWNER_UID))
  77. return false;
  78. }
  79. if (info->match & XT_OWNER_GID) {
  80. kgid_t gid_min = make_kgid(net->user_ns, info->gid_min);
  81. kgid_t gid_max = make_kgid(net->user_ns, info->gid_max);
  82. if ((gid_gte(filp->f_cred->fsgid, gid_min) &&
  83. gid_lte(filp->f_cred->fsgid, gid_max)) ^
  84. !(info->invert & XT_OWNER_GID))
  85. return false;
  86. }
  87. return true;
  88. }
  89. static struct xt_match owner_mt_reg __read_mostly = {
  90. .name = "owner",
  91. .revision = 1,
  92. .family = NFPROTO_UNSPEC,
  93. .checkentry = owner_check,
  94. .match = owner_mt,
  95. .matchsize = sizeof(struct xt_owner_match_info),
  96. .hooks = (1 << NF_INET_LOCAL_OUT) |
  97. (1 << NF_INET_POST_ROUTING),
  98. .me = THIS_MODULE,
  99. };
  100. static int __init owner_mt_init(void)
  101. {
  102. return xt_register_match(&owner_mt_reg);
  103. }
  104. static void __exit owner_mt_exit(void)
  105. {
  106. xt_unregister_match(&owner_mt_reg);
  107. }
  108. module_init(owner_mt_init);
  109. module_exit(owner_mt_exit);
  110. MODULE_AUTHOR("Jan Engelhardt <jengelh@medozas.de>");
  111. MODULE_DESCRIPTION("Xtables: socket owner matching");
  112. MODULE_LICENSE("GPL");
  113. MODULE_ALIAS("ipt_owner");
  114. MODULE_ALIAS("ip6t_owner");