netlabel_unlabeled.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * NetLabel Unlabeled Support
  3. *
  4. * This file defines functions for dealing with unlabeled packets for the
  5. * NetLabel system. The NetLabel system manages static and dynamic label
  6. * mappings for network 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/types.h>
  30. #include <linux/rcupdate.h>
  31. #include <linux/list.h>
  32. #include <linux/spinlock.h>
  33. #include <linux/socket.h>
  34. #include <linux/string.h>
  35. #include <linux/skbuff.h>
  36. #include <linux/audit.h>
  37. #include <net/sock.h>
  38. #include <net/netlink.h>
  39. #include <net/genetlink.h>
  40. #include <net/netlabel.h>
  41. #include <asm/bug.h>
  42. #include "netlabel_user.h"
  43. #include "netlabel_domainhash.h"
  44. #include "netlabel_unlabeled.h"
  45. /* Accept unlabeled packets flag */
  46. static DEFINE_SPINLOCK(netlabel_unlabel_acceptflg_lock);
  47. static u8 netlabel_unlabel_acceptflg = 0;
  48. /* NetLabel Generic NETLINK CIPSOv4 family */
  49. static struct genl_family netlbl_unlabel_gnl_family = {
  50. .id = GENL_ID_GENERATE,
  51. .hdrsize = 0,
  52. .name = NETLBL_NLTYPE_UNLABELED_NAME,
  53. .version = NETLBL_PROTO_VERSION,
  54. .maxattr = NLBL_UNLABEL_A_MAX,
  55. };
  56. /* NetLabel Netlink attribute policy */
  57. static struct nla_policy netlbl_unlabel_genl_policy[NLBL_UNLABEL_A_MAX + 1] = {
  58. [NLBL_UNLABEL_A_ACPTFLG] = { .type = NLA_U8 },
  59. };
  60. /*
  61. * Helper Functions
  62. */
  63. /**
  64. * netlbl_unlabel_acceptflg_set - Set the unlabeled accept flag
  65. * @value: desired value
  66. * @audit_info: NetLabel audit information
  67. *
  68. * Description:
  69. * Set the value of the unlabeled accept flag to @value.
  70. *
  71. */
  72. static void netlbl_unlabel_acceptflg_set(u8 value,
  73. struct netlbl_audit *audit_info)
  74. {
  75. struct audit_buffer *audit_buf;
  76. u8 old_val;
  77. rcu_read_lock();
  78. old_val = netlabel_unlabel_acceptflg;
  79. spin_lock(&netlabel_unlabel_acceptflg_lock);
  80. netlabel_unlabel_acceptflg = value;
  81. spin_unlock(&netlabel_unlabel_acceptflg_lock);
  82. rcu_read_unlock();
  83. audit_buf = netlbl_audit_start_common(AUDIT_MAC_UNLBL_ALLOW,
  84. audit_info);
  85. if (audit_buf != NULL) {
  86. audit_log_format(audit_buf,
  87. " unlbl_accept=%u old=%u", value, old_val);
  88. audit_log_end(audit_buf);
  89. }
  90. }
  91. /*
  92. * NetLabel Command Handlers
  93. */
  94. /**
  95. * netlbl_unlabel_accept - Handle an ACCEPT message
  96. * @skb: the NETLINK buffer
  97. * @info: the Generic NETLINK info block
  98. *
  99. * Description:
  100. * Process a user generated ACCEPT message and set the accept flag accordingly.
  101. * Returns zero on success, negative values on failure.
  102. *
  103. */
  104. static int netlbl_unlabel_accept(struct sk_buff *skb, struct genl_info *info)
  105. {
  106. u8 value;
  107. struct netlbl_audit audit_info;
  108. if (info->attrs[NLBL_UNLABEL_A_ACPTFLG]) {
  109. value = nla_get_u8(info->attrs[NLBL_UNLABEL_A_ACPTFLG]);
  110. if (value == 1 || value == 0) {
  111. netlbl_netlink_auditinfo(skb, &audit_info);
  112. netlbl_unlabel_acceptflg_set(value, &audit_info);
  113. return 0;
  114. }
  115. }
  116. return -EINVAL;
  117. }
  118. /**
  119. * netlbl_unlabel_list - Handle a LIST message
  120. * @skb: the NETLINK buffer
  121. * @info: the Generic NETLINK info block
  122. *
  123. * Description:
  124. * Process a user generated LIST message and respond with the current status.
  125. * Returns zero on success, negative values on failure.
  126. *
  127. */
  128. static int netlbl_unlabel_list(struct sk_buff *skb, struct genl_info *info)
  129. {
  130. int ret_val = -EINVAL;
  131. struct sk_buff *ans_skb;
  132. void *data;
  133. ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  134. if (ans_skb == NULL)
  135. goto list_failure;
  136. data = genlmsg_put_reply(ans_skb, info, &netlbl_unlabel_gnl_family,
  137. 0, NLBL_UNLABEL_C_LIST);
  138. if (data == NULL) {
  139. ret_val = -ENOMEM;
  140. goto list_failure;
  141. }
  142. rcu_read_lock();
  143. ret_val = nla_put_u8(ans_skb,
  144. NLBL_UNLABEL_A_ACPTFLG,
  145. netlabel_unlabel_acceptflg);
  146. rcu_read_unlock();
  147. if (ret_val != 0)
  148. goto list_failure;
  149. genlmsg_end(ans_skb, data);
  150. ret_val = genlmsg_reply(ans_skb, info);
  151. if (ret_val != 0)
  152. goto list_failure;
  153. return 0;
  154. list_failure:
  155. kfree_skb(ans_skb);
  156. return ret_val;
  157. }
  158. /*
  159. * NetLabel Generic NETLINK Command Definitions
  160. */
  161. static struct genl_ops netlbl_unlabel_genl_c_accept = {
  162. .cmd = NLBL_UNLABEL_C_ACCEPT,
  163. .flags = GENL_ADMIN_PERM,
  164. .policy = netlbl_unlabel_genl_policy,
  165. .doit = netlbl_unlabel_accept,
  166. .dumpit = NULL,
  167. };
  168. static struct genl_ops netlbl_unlabel_genl_c_list = {
  169. .cmd = NLBL_UNLABEL_C_LIST,
  170. .flags = 0,
  171. .policy = netlbl_unlabel_genl_policy,
  172. .doit = netlbl_unlabel_list,
  173. .dumpit = NULL,
  174. };
  175. /*
  176. * NetLabel Generic NETLINK Protocol Functions
  177. */
  178. /**
  179. * netlbl_unlabel_genl_init - Register the Unlabeled NetLabel component
  180. *
  181. * Description:
  182. * Register the unlabeled packet NetLabel component with the Generic NETLINK
  183. * mechanism. Returns zero on success, negative values on failure.
  184. *
  185. */
  186. int netlbl_unlabel_genl_init(void)
  187. {
  188. int ret_val;
  189. ret_val = genl_register_family(&netlbl_unlabel_gnl_family);
  190. if (ret_val != 0)
  191. return ret_val;
  192. ret_val = genl_register_ops(&netlbl_unlabel_gnl_family,
  193. &netlbl_unlabel_genl_c_accept);
  194. if (ret_val != 0)
  195. return ret_val;
  196. ret_val = genl_register_ops(&netlbl_unlabel_gnl_family,
  197. &netlbl_unlabel_genl_c_list);
  198. if (ret_val != 0)
  199. return ret_val;
  200. return 0;
  201. }
  202. /*
  203. * NetLabel KAPI Hooks
  204. */
  205. /**
  206. * netlbl_unlabel_getattr - Get the security attributes for an unlabled packet
  207. * @secattr: the security attributes
  208. *
  209. * Description:
  210. * Determine the security attributes, if any, for an unlabled packet and return
  211. * them in @secattr. Returns zero on success and negative values on failure.
  212. *
  213. */
  214. int netlbl_unlabel_getattr(struct netlbl_lsm_secattr *secattr)
  215. {
  216. int ret_val;
  217. rcu_read_lock();
  218. if (netlabel_unlabel_acceptflg == 1) {
  219. netlbl_secattr_init(secattr);
  220. ret_val = 0;
  221. } else
  222. ret_val = -ENOMSG;
  223. rcu_read_unlock();
  224. return ret_val;
  225. }
  226. /**
  227. * netlbl_unlabel_defconf - Set the default config to allow unlabeled packets
  228. *
  229. * Description:
  230. * Set the default NetLabel configuration to allow incoming unlabeled packets
  231. * and to send unlabeled network traffic by default.
  232. *
  233. */
  234. int netlbl_unlabel_defconf(void)
  235. {
  236. int ret_val;
  237. struct netlbl_dom_map *entry;
  238. struct netlbl_audit audit_info;
  239. /* Only the kernel is allowed to call this function and the only time
  240. * it is called is at bootup before the audit subsystem is reporting
  241. * messages so don't worry to much about these values. */
  242. security_task_getsecid(current, &audit_info.secid);
  243. audit_info.loginuid = 0;
  244. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  245. if (entry == NULL)
  246. return -ENOMEM;
  247. entry->type = NETLBL_NLTYPE_UNLABELED;
  248. ret_val = netlbl_domhsh_add_default(entry, &audit_info);
  249. if (ret_val != 0)
  250. return ret_val;
  251. netlbl_unlabel_acceptflg_set(1, &audit_info);
  252. return 0;
  253. }