netlabel_user.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * NetLabel NETLINK Interface
  3. *
  4. * This file defines the NETLINK interface for the NetLabel system. The
  5. * NetLabel system manages static and dynamic label mappings for network
  6. * protocols such as CIPSO and RIPSO.
  7. *
  8. * Author: Paul Moore <paul.moore@hp.com>
  9. *
  10. */
  11. /*
  12. * (c) Copyright Hewlett-Packard Development Company, L.P., 2006
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  22. * the GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  27. *
  28. */
  29. #include <linux/init.h>
  30. #include <linux/types.h>
  31. #include <linux/list.h>
  32. #include <linux/socket.h>
  33. #include <linux/audit.h>
  34. #include <linux/tty.h>
  35. #include <linux/security.h>
  36. #include <net/sock.h>
  37. #include <net/netlink.h>
  38. #include <net/genetlink.h>
  39. #include <net/netlabel.h>
  40. #include <asm/bug.h>
  41. #include "netlabel_mgmt.h"
  42. #include "netlabel_unlabeled.h"
  43. #include "netlabel_cipso_v4.h"
  44. #include "netlabel_user.h"
  45. /*
  46. * NetLabel NETLINK Setup Functions
  47. */
  48. /**
  49. * netlbl_netlink_init - Initialize the NETLINK communication channel
  50. *
  51. * Description:
  52. * Call out to the NetLabel components so they can register their families and
  53. * commands with the Generic NETLINK mechanism. Returns zero on success and
  54. * non-zero on failure.
  55. *
  56. */
  57. int netlbl_netlink_init(void)
  58. {
  59. int ret_val;
  60. ret_val = netlbl_mgmt_genl_init();
  61. if (ret_val != 0)
  62. return ret_val;
  63. ret_val = netlbl_cipsov4_genl_init();
  64. if (ret_val != 0)
  65. return ret_val;
  66. ret_val = netlbl_unlabel_genl_init();
  67. if (ret_val != 0)
  68. return ret_val;
  69. return 0;
  70. }
  71. /*
  72. * NetLabel Audit Functions
  73. */
  74. /**
  75. * netlbl_audit_start_common - Start an audit message
  76. * @type: audit message type
  77. * @secid: LSM context ID
  78. *
  79. * Description:
  80. * Start an audit message using the type specified in @type and fill the audit
  81. * message with some fields common to all NetLabel audit messages. Returns
  82. * a pointer to the audit buffer on success, NULL on failure.
  83. *
  84. */
  85. struct audit_buffer *netlbl_audit_start_common(int type, u32 secid)
  86. {
  87. struct audit_context *audit_ctx = current->audit_context;
  88. struct audit_buffer *audit_buf;
  89. uid_t audit_loginuid;
  90. const char *audit_tty;
  91. char audit_comm[sizeof(current->comm)];
  92. struct vm_area_struct *vma;
  93. char *secctx;
  94. u32 secctx_len;
  95. audit_buf = audit_log_start(audit_ctx, GFP_ATOMIC, type);
  96. if (audit_buf == NULL)
  97. return NULL;
  98. audit_loginuid = audit_get_loginuid(audit_ctx);
  99. if (current->signal &&
  100. current->signal->tty &&
  101. current->signal->tty->name)
  102. audit_tty = current->signal->tty->name;
  103. else
  104. audit_tty = "(none)";
  105. get_task_comm(audit_comm, current);
  106. audit_log_format(audit_buf,
  107. "netlabel: auid=%u uid=%u tty=%s pid=%d",
  108. audit_loginuid,
  109. current->uid,
  110. audit_tty,
  111. current->pid);
  112. audit_log_format(audit_buf, " comm=");
  113. audit_log_untrustedstring(audit_buf, audit_comm);
  114. if (current->mm) {
  115. down_read(&current->mm->mmap_sem);
  116. vma = current->mm->mmap;
  117. while (vma) {
  118. if ((vma->vm_flags & VM_EXECUTABLE) &&
  119. vma->vm_file) {
  120. audit_log_d_path(audit_buf,
  121. " exe=",
  122. vma->vm_file->f_dentry,
  123. vma->vm_file->f_vfsmnt);
  124. break;
  125. }
  126. vma = vma->vm_next;
  127. }
  128. up_read(&current->mm->mmap_sem);
  129. }
  130. if (secid != 0 &&
  131. security_secid_to_secctx(secid, &secctx, &secctx_len) == 0)
  132. audit_log_format(audit_buf, " subj=%s", secctx);
  133. return audit_buf;
  134. }
  135. /**
  136. * netlbl_audit_nomsg - Send an audit message without additional text
  137. * @type: audit message type
  138. * @secid: LSM context ID
  139. *
  140. * Description:
  141. * Send an audit message with only the common NetLabel audit fields.
  142. *
  143. */
  144. void netlbl_audit_nomsg(int type, u32 secid)
  145. {
  146. struct audit_buffer *audit_buf;
  147. audit_buf = netlbl_audit_start_common(type, secid);
  148. audit_log_end(audit_buf);
  149. }