xt_owner.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 <linux/netfilter/x_tables.h>
  18. #include <linux/netfilter/xt_owner.h>
  19. static bool
  20. owner_mt(const struct sk_buff *skb, struct xt_action_param *par)
  21. {
  22. const struct xt_owner_match_info *info = par->matchinfo;
  23. const struct file *filp;
  24. if (skb->sk == NULL || skb->sk->sk_socket == NULL)
  25. return (info->match ^ info->invert) == 0;
  26. else if (info->match & info->invert & XT_OWNER_SOCKET)
  27. /*
  28. * Socket exists but user wanted ! --socket-exists.
  29. * (Single ampersands intended.)
  30. */
  31. return false;
  32. filp = skb->sk->sk_socket->file;
  33. if (filp == NULL)
  34. return ((info->match ^ info->invert) &
  35. (XT_OWNER_UID | XT_OWNER_GID)) == 0;
  36. if (info->match & XT_OWNER_UID)
  37. if ((filp->f_cred->fsuid >= info->uid_min &&
  38. filp->f_cred->fsuid <= info->uid_max) ^
  39. !(info->invert & XT_OWNER_UID))
  40. return false;
  41. if (info->match & XT_OWNER_GID)
  42. if ((filp->f_cred->fsgid >= info->gid_min &&
  43. filp->f_cred->fsgid <= info->gid_max) ^
  44. !(info->invert & XT_OWNER_GID))
  45. return false;
  46. return true;
  47. }
  48. static struct xt_match owner_mt_reg __read_mostly = {
  49. .name = "owner",
  50. .revision = 1,
  51. .family = NFPROTO_UNSPEC,
  52. .match = owner_mt,
  53. .matchsize = sizeof(struct xt_owner_match_info),
  54. .hooks = (1 << NF_INET_LOCAL_OUT) |
  55. (1 << NF_INET_POST_ROUTING),
  56. .me = THIS_MODULE,
  57. };
  58. static int __init owner_mt_init(void)
  59. {
  60. return xt_register_match(&owner_mt_reg);
  61. }
  62. static void __exit owner_mt_exit(void)
  63. {
  64. xt_unregister_match(&owner_mt_reg);
  65. }
  66. module_init(owner_mt_init);
  67. module_exit(owner_mt_exit);
  68. MODULE_AUTHOR("Jan Engelhardt <jengelh@medozas.de>");
  69. MODULE_DESCRIPTION("Xtables: socket owner matching");
  70. MODULE_LICENSE("GPL");
  71. MODULE_ALIAS("ipt_owner");
  72. MODULE_ALIAS("ip6t_owner");