netlink.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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/selinux_netlink.h>
  20. #include <net/net_namespace.h>
  21. #include <net/netlink.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 = nlmsg_new(len, GFP_USER);
  66. if (!skb)
  67. goto oom;
  68. tmp = skb->tail;
  69. nlh = nlmsg_put(skb, 0, 0, msgtype, len, 0);
  70. if (!nlh)
  71. goto out_kfree_skb;
  72. selnl_add_payload(nlh, len, msgtype, data);
  73. nlh->nlmsg_len = skb->tail - tmp;
  74. NETLINK_CB(skb).dst_group = SELNLGRP_AVC;
  75. netlink_broadcast(selnl, skb, 0, SELNLGRP_AVC, GFP_USER);
  76. out:
  77. return;
  78. out_kfree_skb:
  79. kfree_skb(skb);
  80. oom:
  81. printk(KERN_ERR "SELinux: OOM in %s\n", __func__);
  82. goto out;
  83. }
  84. void selnl_notify_setenforce(int val)
  85. {
  86. selnl_notify(SELNL_MSG_SETENFORCE, &val);
  87. }
  88. void selnl_notify_policyload(u32 seqno)
  89. {
  90. selnl_notify(SELNL_MSG_POLICYLOAD, &seqno);
  91. }
  92. static int __init selnl_init(void)
  93. {
  94. struct netlink_kernel_cfg cfg = {
  95. .groups = SELNLGRP_MAX,
  96. .flags = NL_CFG_F_NONROOT_RECV,
  97. };
  98. selnl = netlink_kernel_create(&init_net, NETLINK_SELINUX, &cfg);
  99. if (selnl == NULL)
  100. panic("SELinux: Cannot create netlink socket.");
  101. return 0;
  102. }
  103. __initcall(selnl_init);