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