Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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/stddef.h> |
| 15 | #include <linux/kernel.h> |
| 16 | #include <linux/list.h> |
| 17 | #include <linux/skbuff.h> |
| 18 | #include <linux/netlink.h> |
| 19 | #include <linux/selinux_netlink.h> |
| 20 | |
| 21 | static struct sock *selnl; |
| 22 | |
| 23 | static int selnl_msglen(int msgtype) |
| 24 | { |
| 25 | int ret = 0; |
| 26 | |
| 27 | switch (msgtype) { |
| 28 | case SELNL_MSG_SETENFORCE: |
| 29 | ret = sizeof(struct selnl_msg_setenforce); |
| 30 | break; |
| 31 | |
| 32 | case SELNL_MSG_POLICYLOAD: |
| 33 | ret = sizeof(struct selnl_msg_policyload); |
| 34 | break; |
| 35 | |
| 36 | default: |
| 37 | BUG(); |
| 38 | } |
| 39 | return ret; |
| 40 | } |
| 41 | |
| 42 | static void selnl_add_payload(struct nlmsghdr *nlh, int len, int msgtype, void *data) |
| 43 | { |
| 44 | switch (msgtype) { |
| 45 | case SELNL_MSG_SETENFORCE: { |
| 46 | struct selnl_msg_setenforce *msg = NLMSG_DATA(nlh); |
| 47 | |
| 48 | memset(msg, 0, len); |
| 49 | msg->val = *((int *)data); |
| 50 | break; |
| 51 | } |
| 52 | |
| 53 | case SELNL_MSG_POLICYLOAD: { |
| 54 | struct selnl_msg_policyload *msg = NLMSG_DATA(nlh); |
| 55 | |
| 56 | memset(msg, 0, len); |
| 57 | msg->seqno = *((u32 *)data); |
| 58 | break; |
| 59 | } |
| 60 | |
| 61 | default: |
| 62 | BUG(); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | static void selnl_notify(int msgtype, void *data) |
| 67 | { |
| 68 | int len; |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 69 | sk_buff_data_t tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | struct sk_buff *skb; |
| 71 | struct nlmsghdr *nlh; |
| 72 | |
| 73 | len = selnl_msglen(msgtype); |
| 74 | |
| 75 | skb = alloc_skb(NLMSG_SPACE(len), GFP_USER); |
| 76 | if (!skb) |
| 77 | goto oom; |
| 78 | |
| 79 | tmp = skb->tail; |
| 80 | nlh = NLMSG_PUT(skb, 0, 0, msgtype, len); |
| 81 | selnl_add_payload(nlh, len, msgtype, data); |
| 82 | nlh->nlmsg_len = skb->tail - tmp; |
Patrick McHardy | ac6d439 | 2005-08-14 19:29:52 -0700 | [diff] [blame] | 83 | NETLINK_CB(skb).dst_group = SELNLGRP_AVC; |
| 84 | netlink_broadcast(selnl, skb, 0, SELNLGRP_AVC, GFP_USER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | out: |
| 86 | return; |
| 87 | |
| 88 | nlmsg_failure: |
| 89 | kfree_skb(skb); |
| 90 | oom: |
| 91 | printk(KERN_ERR "SELinux: OOM in %s\n", __FUNCTION__); |
| 92 | goto out; |
| 93 | } |
| 94 | |
| 95 | void selnl_notify_setenforce(int val) |
| 96 | { |
| 97 | selnl_notify(SELNL_MSG_SETENFORCE, &val); |
| 98 | } |
| 99 | |
| 100 | void selnl_notify_policyload(u32 seqno) |
| 101 | { |
| 102 | selnl_notify(SELNL_MSG_POLICYLOAD, &seqno); |
| 103 | } |
| 104 | |
| 105 | static int __init selnl_init(void) |
| 106 | { |
Patrick McHardy | af65bdf | 2007-04-20 14:14:21 -0700 | [diff] [blame] | 107 | selnl = netlink_kernel_create(NETLINK_SELINUX, SELNLGRP_MAX, NULL, NULL, |
Patrick McHardy | 0662860 | 2005-08-15 12:33:26 -0700 | [diff] [blame] | 108 | THIS_MODULE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | if (selnl == NULL) |
| 110 | panic("SELinux: Cannot create netlink socket."); |
| 111 | netlink_set_nonroot(NETLINK_SELINUX, NL_NONROOT_RECV); |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | __initcall(selnl_init); |