xt_owner.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. /* For now only allow adding matches from the initial user namespace */
  24. if ((info->match & (XT_OWNER_UID|XT_OWNER_GID)) &&
  25. (current_user_ns() != &init_user_ns))
  26. return -EINVAL;
  27. return 0;
  28. }
  29. static bool
  30. owner_mt(const struct sk_buff *skb, struct xt_action_param *par)
  31. {
  32. const struct xt_owner_match_info *info = par->matchinfo;
  33. const struct file *filp;
  34. struct sock *sk = skb_to_full_sk(skb);
  35. if (sk == NULL || sk->sk_socket == NULL)
  36. return (info->match ^ info->invert) == 0;
  37. else if (info->match & info->invert & XT_OWNER_SOCKET)
  38. /*
  39. * Socket exists but user wanted ! --socket-exists.
  40. * (Single ampersands intended.)
  41. */
  42. return false;
  43. filp = sk->sk_socket->file;
  44. if (filp == NULL)
  45. return ((info->match ^ info->invert) &
  46. (XT_OWNER_UID | XT_OWNER_GID)) == 0;
  47. if (info->match & XT_OWNER_UID) {
  48. kuid_t uid_min = make_kuid(&init_user_ns, info->uid_min);
  49. kuid_t uid_max = make_kuid(&init_user_ns, info->uid_max);
  50. if ((uid_gte(filp->f_cred->fsuid, uid_min) &&
  51. uid_lte(filp->f_cred->fsuid, uid_max)) ^
  52. !(info->invert & XT_OWNER_UID))
  53. return false;
  54. }
  55. if (info->match & XT_OWNER_GID) {
  56. kgid_t gid_min = make_kgid(&init_user_ns, info->gid_min);
  57. kgid_t gid_max = make_kgid(&init_user_ns, info->gid_max);
  58. if ((gid_gte(filp->f_cred->fsgid, gid_min) &&
  59. gid_lte(filp->f_cred->fsgid, gid_max)) ^
  60. !(info->invert & XT_OWNER_GID))
  61. return false;
  62. }
  63. return true;
  64. }
  65. static struct xt_match owner_mt_reg __read_mostly = {
  66. .name = "owner",
  67. .revision = 1,
  68. .family = NFPROTO_UNSPEC,
  69. .checkentry = owner_check,
  70. .match = owner_mt,
  71. .matchsize = sizeof(struct xt_owner_match_info),
  72. .hooks = (1 << NF_INET_LOCAL_OUT) |
  73. (1 << NF_INET_POST_ROUTING),
  74. .me = THIS_MODULE,
  75. };
  76. static int __init owner_mt_init(void)
  77. {
  78. return xt_register_match(&owner_mt_reg);
  79. }
  80. static void __exit owner_mt_exit(void)
  81. {
  82. xt_unregister_match(&owner_mt_reg);
  83. }
  84. module_init(owner_mt_init);
  85. module_exit(owner_mt_exit);
  86. MODULE_AUTHOR("Jan Engelhardt <jengelh@medozas.de>");
  87. MODULE_DESCRIPTION("Xtables: socket owner matching");
  88. MODULE_LICENSE("GPL");
  89. MODULE_ALIAS("ipt_owner");
  90. MODULE_ALIAS("ip6t_owner");