xt_SECMARK.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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,2008 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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  16. #include <linux/module.h>
  17. #include <linux/security.h>
  18. #include <linux/skbuff.h>
  19. #include <linux/netfilter/x_tables.h>
  20. #include <linux/netfilter/xt_SECMARK.h>
  21. MODULE_LICENSE("GPL");
  22. MODULE_AUTHOR("James Morris <jmorris@redhat.com>");
  23. MODULE_DESCRIPTION("Xtables: packet security mark modification");
  24. MODULE_ALIAS("ipt_SECMARK");
  25. MODULE_ALIAS("ip6t_SECMARK");
  26. #define PFX "SECMARK: "
  27. static u8 mode;
  28. static unsigned int
  29. secmark_tg(struct sk_buff *skb, const struct xt_action_param *par)
  30. {
  31. u32 secmark = 0;
  32. const struct xt_secmark_target_info *info = par->targinfo;
  33. BUG_ON(info->mode != mode);
  34. switch (mode) {
  35. case SECMARK_MODE_SEL:
  36. secmark = info->secid;
  37. break;
  38. default:
  39. BUG();
  40. }
  41. skb->secmark = secmark;
  42. return XT_CONTINUE;
  43. }
  44. static int checkentry_lsm(struct xt_secmark_target_info *info)
  45. {
  46. int err;
  47. info->secctx[SECMARK_SECCTX_MAX - 1] = '\0';
  48. info->secid = 0;
  49. err = security_secctx_to_secid(info->secctx, strlen(info->secctx),
  50. &info->secid);
  51. if (err) {
  52. if (err == -EINVAL)
  53. pr_info("invalid security context \'%s\'\n", info->secctx);
  54. return err;
  55. }
  56. if (!info->secid) {
  57. pr_info("unable to map security context \'%s\'\n", info->secctx);
  58. return -ENOENT;
  59. }
  60. err = security_secmark_relabel_packet(info->secid);
  61. if (err) {
  62. pr_info("unable to obtain relabeling permission\n");
  63. return err;
  64. }
  65. security_secmark_refcount_inc();
  66. return 0;
  67. }
  68. static int secmark_tg_check(const struct xt_tgchk_param *par)
  69. {
  70. struct xt_secmark_target_info *info = par->targinfo;
  71. int err;
  72. if (strcmp(par->table, "mangle") != 0 &&
  73. strcmp(par->table, "security") != 0) {
  74. pr_info("target only valid in the \'mangle\' "
  75. "or \'security\' tables, not \'%s\'.\n", par->table);
  76. return -EINVAL;
  77. }
  78. if (mode && mode != info->mode) {
  79. pr_info("mode already set to %hu cannot mix with "
  80. "rules for mode %hu\n", mode, info->mode);
  81. return -EINVAL;
  82. }
  83. switch (info->mode) {
  84. case SECMARK_MODE_SEL:
  85. break;
  86. default:
  87. pr_info("invalid mode: %hu\n", info->mode);
  88. return -EINVAL;
  89. }
  90. err = checkentry_lsm(info);
  91. if (err)
  92. return err;
  93. if (!mode)
  94. mode = info->mode;
  95. return 0;
  96. }
  97. static void secmark_tg_destroy(const struct xt_tgdtor_param *par)
  98. {
  99. switch (mode) {
  100. case SECMARK_MODE_SEL:
  101. security_secmark_refcount_dec();
  102. }
  103. }
  104. static struct xt_target secmark_tg_reg __read_mostly = {
  105. .name = "SECMARK",
  106. .revision = 0,
  107. .family = NFPROTO_UNSPEC,
  108. .checkentry = secmark_tg_check,
  109. .destroy = secmark_tg_destroy,
  110. .target = secmark_tg,
  111. .targetsize = sizeof(struct xt_secmark_target_info),
  112. .me = THIS_MODULE,
  113. };
  114. static int __init secmark_tg_init(void)
  115. {
  116. return xt_register_target(&secmark_tg_reg);
  117. }
  118. static void __exit secmark_tg_exit(void)
  119. {
  120. xt_unregister_target(&secmark_tg_reg);
  121. }
  122. module_init(secmark_tg_init);
  123. module_exit(secmark_tg_exit);