netlink.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Netlink event notifications for SELinux.
  3. *
  4. * Author: James Morris <jmorris@redhat.com>
  5. *
  6. * Copyright (C) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2,
  10. * as published by the Free Software Foundation.
  11. */
  12. #include <linux/init.h>
  13. #include <linux/types.h>
  14. #include <linux/slab.h>
  15. #include <linux/stddef.h>
  16. #include <linux/kernel.h>
  17. #include <linux/export.h>
  18. #include <linux/skbuff.h>
  19. #include <linux/netlink.h>
  20. #include <linux/selinux_netlink.h>
  21. #include <net/net_namespace.h>
  22. #include <net/netlink.h>
  23. #include "security.h"
  24. static struct sock *selnl;
  25. static int selnl_msglen(int msgtype)
  26. {
  27. int ret = 0;
  28. switch (msgtype) {
  29. case SELNL_MSG_SETENFORCE:
  30. ret = sizeof(struct selnl_msg_setenforce);
  31. break;
  32. case SELNL_MSG_POLICYLOAD:
  33. ret = sizeof(struct selnl_msg_policyload);
  34. break;
  35. default:
  36. BUG();
  37. }
  38. return ret;
  39. }
  40. static void selnl_add_payload(struct nlmsghdr *nlh, int len, int msgtype, void *data)
  41. {
  42. switch (msgtype) {
  43. case SELNL_MSG_SETENFORCE: {
  44. struct selnl_msg_setenforce *msg = nlmsg_data(nlh);
  45. memset(msg, 0, len);
  46. msg->val = *((int *)data);
  47. break;
  48. }
  49. case SELNL_MSG_POLICYLOAD: {
  50. struct selnl_msg_policyload *msg = nlmsg_data(nlh);
  51. memset(msg, 0, len);
  52. msg->seqno = *((u32 *)data);
  53. break;
  54. }
  55. default:
  56. BUG();
  57. }
  58. }
  59. static void selnl_notify(int msgtype, void *data)
  60. {
  61. int len;
  62. sk_buff_data_t tmp;
  63. struct sk_buff *skb;
  64. struct nlmsghdr *nlh;
  65. len = selnl_msglen(msgtype);
  66. skb = alloc_skb(NLMSG_SPACE(len), GFP_USER);
  67. if (!skb)
  68. goto oom;
  69. tmp = skb->tail;
  70. nlh = nlmsg_put(skb, 0, 0, msgtype, len, 0);
  71. if (!nlh)
  72. goto out_kfree_skb;
  73. selnl_add_payload(nlh, len, msgtype, data);
  74. nlh->nlmsg_len = skb->tail - tmp;
  75. NETLINK_CB(skb).dst_group = SELNLGRP_AVC;
  76. netlink_broadcast(selnl, skb, 0, SELNLGRP_AVC, GFP_USER);
  77. out:
  78. return;
  79. out_kfree_skb:
  80. kfree_skb(skb);
  81. oom:
  82. printk(KERN_ERR "SELinux: OOM in %s\n", __func__);
  83. goto out;
  84. }
  85. void selnl_notify_setenforce(int val)
  86. {
  87. selnl_notify(SELNL_MSG_SETENFORCE, &val);
  88. }
  89. void selnl_notify_policyload(u32 seqno)
  90. {
  91. selnl_notify(SELNL_MSG_POLICYLOAD, &seqno);
  92. }
  93. static int __init selnl_init(void)
  94. {
  95. selnl = netlink_kernel_create(&init_net, NETLINK_SELINUX,
  96. SELNLGRP_MAX, NULL, NULL, THIS_MODULE);
  97. if (selnl == NULL)
  98. panic("SELinux: Cannot create netlink socket.");
  99. netlink_set_nonroot(NETLINK_SELINUX, NL_NONROOT_RECV);
  100. return 0;
  101. }
  102. __initcall(selnl_init);