/net/netfilter/xt_owner.c

http://github.com/mirrors/linux · C · 150 lines · 109 code · 23 blank · 18 comment · 28 complexity · 5cb5daa0f928d6d13eaa403c7d91df5b MD5 · raw file

  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Kernel module to match various things tied to sockets associated with
  4. * locally generated outgoing packets.
  5. *
  6. * (C) 2000 Marc Boucher <marc@mbsi.ca>
  7. *
  8. * Copyright © CC Computer Consultants GmbH, 2007 - 2008
  9. */
  10. #include <linux/module.h>
  11. #include <linux/skbuff.h>
  12. #include <linux/file.h>
  13. #include <linux/cred.h>
  14. #include <net/sock.h>
  15. #include <net/inet_sock.h>
  16. #include <linux/netfilter/x_tables.h>
  17. #include <linux/netfilter/xt_owner.h>
  18. static int owner_check(const struct xt_mtchk_param *par)
  19. {
  20. struct xt_owner_match_info *info = par->matchinfo;
  21. struct net *net = par->net;
  22. if (info->match & ~XT_OWNER_MASK)
  23. return -EINVAL;
  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 = xt_net(par);
  59. if (!sk || !sk->sk_socket || !net_eq(net, sock_net(sk)))
  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. unsigned int i, match = false;
  81. kgid_t gid_min = make_kgid(net->user_ns, info->gid_min);
  82. kgid_t gid_max = make_kgid(net->user_ns, info->gid_max);
  83. struct group_info *gi = filp->f_cred->group_info;
  84. if (gid_gte(filp->f_cred->fsgid, gid_min) &&
  85. gid_lte(filp->f_cred->fsgid, gid_max))
  86. match = true;
  87. if (!match && (info->match & XT_OWNER_SUPPL_GROUPS) && gi) {
  88. for (i = 0; i < gi->ngroups; ++i) {
  89. kgid_t group = gi->gid[i];
  90. if (gid_gte(group, gid_min) &&
  91. gid_lte(group, gid_max)) {
  92. match = true;
  93. break;
  94. }
  95. }
  96. }
  97. if (match ^ !(info->invert & XT_OWNER_GID))
  98. return false;
  99. }
  100. return true;
  101. }
  102. static struct xt_match owner_mt_reg __read_mostly = {
  103. .name = "owner",
  104. .revision = 1,
  105. .family = NFPROTO_UNSPEC,
  106. .checkentry = owner_check,
  107. .match = owner_mt,
  108. .matchsize = sizeof(struct xt_owner_match_info),
  109. .hooks = (1 << NF_INET_LOCAL_OUT) |
  110. (1 << NF_INET_POST_ROUTING),
  111. .me = THIS_MODULE,
  112. };
  113. static int __init owner_mt_init(void)
  114. {
  115. return xt_register_match(&owner_mt_reg);
  116. }
  117. static void __exit owner_mt_exit(void)
  118. {
  119. xt_unregister_match(&owner_mt_reg);
  120. }
  121. module_init(owner_mt_init);
  122. module_exit(owner_mt_exit);
  123. MODULE_AUTHOR("Jan Engelhardt <jengelh@medozas.de>");
  124. MODULE_DESCRIPTION("Xtables: socket owner matching");
  125. MODULE_LICENSE("GPL");
  126. MODULE_ALIAS("ipt_owner");
  127. MODULE_ALIAS("ip6t_owner");