netlink.c 2.4 KB

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