xt_SECMARK.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Module for modifying the secmark field of the skb, for use by
  3. * security subsystems.
  4. *
  5. * Based on the nfmark match by:
  6. * (C) 1999-2001 Marc Boucher <marc@mbsi.ca>
  7. *
  8. * (C) 2006 Red Hat, Inc., James Morris <jmorris@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. */
  15. #include <linux/module.h>
  16. #include <linux/skbuff.h>
  17. #include <linux/selinux.h>
  18. #include <linux/netfilter/x_tables.h>
  19. #include <linux/netfilter/xt_SECMARK.h>
  20. MODULE_LICENSE("GPL");
  21. MODULE_AUTHOR("James Morris <jmorris@redhat.com>");
  22. MODULE_DESCRIPTION("ip[6]tables SECMARK modification module");
  23. MODULE_ALIAS("ipt_SECMARK");
  24. MODULE_ALIAS("ip6t_SECMARK");
  25. #define PFX "SECMARK: "
  26. static u8 mode;
  27. static unsigned int target(struct sk_buff **pskb, const struct net_device *in,
  28. const struct net_device *out, unsigned int hooknum,
  29. const struct xt_target *target,
  30. const void *targinfo)
  31. {
  32. u32 secmark = 0;
  33. const struct xt_secmark_target_info *info = targinfo;
  34. BUG_ON(info->mode != mode);
  35. switch (mode) {
  36. case SECMARK_MODE_SEL:
  37. secmark = info->u.sel.selsid;
  38. break;
  39. default:
  40. BUG();
  41. }
  42. (*pskb)->secmark = secmark;
  43. return XT_CONTINUE;
  44. }
  45. static int checkentry_selinux(struct xt_secmark_target_info *info)
  46. {
  47. int err;
  48. struct xt_secmark_target_selinux_info *sel = &info->u.sel;
  49. sel->selctx[SECMARK_SELCTX_MAX - 1] = '\0';
  50. err = selinux_string_to_sid(sel->selctx, &sel->selsid);
  51. if (err) {
  52. if (err == -EINVAL)
  53. printk(KERN_INFO PFX "invalid SELinux context \'%s\'\n",
  54. sel->selctx);
  55. return 0;
  56. }
  57. if (!sel->selsid) {
  58. printk(KERN_INFO PFX "unable to map SELinux context \'%s\'\n",
  59. sel->selctx);
  60. return 0;
  61. }
  62. err = selinux_relabel_packet_permission(sel->selsid);
  63. if (err) {
  64. printk(KERN_INFO PFX "unable to obtain relabeling permission\n");
  65. return 0;
  66. }
  67. return 1;
  68. }
  69. static int checkentry(const char *tablename, const void *entry,
  70. const struct xt_target *target, void *targinfo,
  71. unsigned int hook_mask)
  72. {
  73. struct xt_secmark_target_info *info = targinfo;
  74. if (mode && mode != info->mode) {
  75. printk(KERN_INFO PFX "mode already set to %hu cannot mix with "
  76. "rules for mode %hu\n", mode, info->mode);
  77. return 0;
  78. }
  79. switch (info->mode) {
  80. case SECMARK_MODE_SEL:
  81. if (!checkentry_selinux(info))
  82. return 0;
  83. break;
  84. default:
  85. printk(KERN_INFO PFX "invalid mode: %hu\n", info->mode);
  86. return 0;
  87. }
  88. if (!mode)
  89. mode = info->mode;
  90. return 1;
  91. }
  92. static struct xt_target xt_secmark_target[] = {
  93. {
  94. .name = "SECMARK",
  95. .family = AF_INET,
  96. .checkentry = checkentry,
  97. .target = target,
  98. .targetsize = sizeof(struct xt_secmark_target_info),
  99. .table = "mangle",
  100. .me = THIS_MODULE,
  101. },
  102. {
  103. .name = "SECMARK",
  104. .family = AF_INET6,
  105. .checkentry = checkentry,
  106. .target = target,
  107. .targetsize = sizeof(struct xt_secmark_target_info),
  108. .table = "mangle",
  109. .me = THIS_MODULE,
  110. },
  111. };
  112. static int __init xt_secmark_init(void)
  113. {
  114. return xt_register_targets(xt_secmark_target,
  115. ARRAY_SIZE(xt_secmark_target));
  116. }
  117. static void __exit xt_secmark_fini(void)
  118. {
  119. xt_unregister_targets(xt_secmark_target, ARRAY_SIZE(xt_secmark_target));
  120. }
  121. module_init(xt_secmark_init);
  122. module_exit(xt_secmark_fini);