PageRenderTime 84ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/net/netfilter/xt_SECMARK.c

https://bitbucket.org/abioy/linux
C | 146 lines | 107 code | 25 blank | 14 comment | 17 complexity | eb33cb6460cba95cca906a0a488dbfab MD5 | raw file
Possible License(s): CC-BY-SA-3.0, GPL-2.0, LGPL-2.0, AGPL-1.0
  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. #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("Xtables: packet security mark modification");
  23. MODULE_ALIAS("ipt_SECMARK");
  24. MODULE_ALIAS("ip6t_SECMARK");
  25. #define PFX "SECMARK: "
  26. static u8 mode;
  27. static unsigned int
  28. secmark_tg(struct sk_buff *skb, const struct xt_target_param *par)
  29. {
  30. u32 secmark = 0;
  31. const struct xt_secmark_target_info *info = par->targinfo;
  32. BUG_ON(info->mode != mode);
  33. switch (mode) {
  34. case SECMARK_MODE_SEL:
  35. secmark = info->u.sel.selsid;
  36. break;
  37. default:
  38. BUG();
  39. }
  40. skb->secmark = secmark;
  41. return XT_CONTINUE;
  42. }
  43. static bool checkentry_selinux(struct xt_secmark_target_info *info)
  44. {
  45. int err;
  46. struct xt_secmark_target_selinux_info *sel = &info->u.sel;
  47. sel->selctx[SECMARK_SELCTX_MAX - 1] = '\0';
  48. err = selinux_string_to_sid(sel->selctx, &sel->selsid);
  49. if (err) {
  50. if (err == -EINVAL)
  51. printk(KERN_INFO PFX "invalid SELinux context \'%s\'\n",
  52. sel->selctx);
  53. return false;
  54. }
  55. if (!sel->selsid) {
  56. printk(KERN_INFO PFX "unable to map SELinux context \'%s\'\n",
  57. sel->selctx);
  58. return false;
  59. }
  60. err = selinux_secmark_relabel_packet_permission(sel->selsid);
  61. if (err) {
  62. printk(KERN_INFO PFX "unable to obtain relabeling permission\n");
  63. return false;
  64. }
  65. selinux_secmark_refcount_inc();
  66. return true;
  67. }
  68. static bool secmark_tg_check(const struct xt_tgchk_param *par)
  69. {
  70. struct xt_secmark_target_info *info = par->targinfo;
  71. if (strcmp(par->table, "mangle") != 0 &&
  72. strcmp(par->table, "security") != 0) {
  73. printk(KERN_INFO PFX "target only valid in the \'mangle\' "
  74. "or \'security\' tables, not \'%s\'.\n", par->table);
  75. return false;
  76. }
  77. if (mode && mode != info->mode) {
  78. printk(KERN_INFO PFX "mode already set to %hu cannot mix with "
  79. "rules for mode %hu\n", mode, info->mode);
  80. return false;
  81. }
  82. switch (info->mode) {
  83. case SECMARK_MODE_SEL:
  84. if (!checkentry_selinux(info))
  85. return false;
  86. break;
  87. default:
  88. printk(KERN_INFO PFX "invalid mode: %hu\n", info->mode);
  89. return false;
  90. }
  91. if (!mode)
  92. mode = info->mode;
  93. return true;
  94. }
  95. static void secmark_tg_destroy(const struct xt_tgdtor_param *par)
  96. {
  97. switch (mode) {
  98. case SECMARK_MODE_SEL:
  99. selinux_secmark_refcount_dec();
  100. }
  101. }
  102. static struct xt_target secmark_tg_reg __read_mostly = {
  103. .name = "SECMARK",
  104. .revision = 0,
  105. .family = NFPROTO_UNSPEC,
  106. .checkentry = secmark_tg_check,
  107. .destroy = secmark_tg_destroy,
  108. .target = secmark_tg,
  109. .targetsize = sizeof(struct xt_secmark_target_info),
  110. .me = THIS_MODULE,
  111. };
  112. static int __init secmark_tg_init(void)
  113. {
  114. return xt_register_target(&secmark_tg_reg);
  115. }
  116. static void __exit secmark_tg_exit(void)
  117. {
  118. xt_unregister_target(&secmark_tg_reg);
  119. }
  120. module_init(secmark_tg_init);
  121. module_exit(secmark_tg_exit);