netlink.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 "security.h"
  23. static struct sock *selnl;
  24. static int selnl_msglen(int msgtype)
  25. {
  26. int ret = 0;
  27. switch (msgtype) {
  28. case SELNL_MSG_SETENFORCE:
  29. ret = sizeof(struct selnl_msg_setenforce);
  30. break;
  31. case SELNL_MSG_POLICYLOAD:
  32. ret = sizeof(struct selnl_msg_policyload);
  33. break;
  34. default:
  35. BUG();
  36. }
  37. return ret;
  38. }
  39. static void selnl_add_payload(struct nlmsghdr *nlh, int len, int msgtype, void *data)
  40. {
  41. switch (msgtype) {
  42. case SELNL_MSG_SETENFORCE: {
  43. struct selnl_msg_setenforce *msg = NLMSG_DATA(nlh);
  44. memset(msg, 0, len);
  45. msg->val = *((int *)data);
  46. break;
  47. }
  48. case SELNL_MSG_POLICYLOAD: {
  49. struct selnl_msg_policyload *msg = NLMSG_DATA(nlh);
  50. memset(msg, 0, len);
  51. msg->seqno = *((u32 *)data);
  52. break;
  53. }
  54. default:
  55. BUG();
  56. }
  57. }
  58. static void selnl_notify(int msgtype, void *data)
  59. {
  60. int len;
  61. sk_buff_data_t tmp;
  62. struct sk_buff *skb;
  63. struct nlmsghdr *nlh;
  64. len = selnl_msglen(msgtype);
  65. skb = alloc_skb(NLMSG_SPACE(len), GFP_USER);
  66. if (!skb)
  67. goto oom;
  68. tmp = skb->tail;
  69. nlh = NLMSG_PUT(skb, 0, 0, msgtype, len);
  70. selnl_add_payload(nlh, len, msgtype, data);
  71. nlh->nlmsg_len = skb->tail - tmp;
  72. NETLINK_CB(skb).dst_group = SELNLGRP_AVC;
  73. netlink_broadcast(selnl, skb, 0, SELNLGRP_AVC, GFP_USER);
  74. out:
  75. return;
  76. nlmsg_failure:
  77. kfree_skb(skb);
  78. oom:
  79. printk(KERN_ERR "SELinux: OOM in %s\n", __func__);
  80. goto out;
  81. }
  82. void selnl_notify_setenforce(int val)
  83. {
  84. selnl_notify(SELNL_MSG_SETENFORCE, &val);
  85. }
  86. void selnl_notify_policyload(u32 seqno)
  87. {
  88. selnl_notify(SELNL_MSG_POLICYLOAD, &seqno);
  89. }
  90. static int __init selnl_init(void)
  91. {
  92. selnl = netlink_kernel_create(&init_net, NETLINK_SELINUX,
  93. SELNLGRP_MAX, NULL, NULL, THIS_MODULE);
  94. if (selnl == NULL)
  95. panic("SELinux: Cannot create netlink socket.");
  96. netlink_set_nonroot(NETLINK_SELINUX, NL_NONROOT_RECV);
  97. return 0;
  98. }
  99. __initcall(selnl_init);