blob: 1ae556446e65a745f9caf670ea591f1ea3613ea5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Eric W. Biedermanb4b51022007-09-12 13:05:38 +020020#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22static struct sock *selnl;
23
24static int selnl_msglen(int msgtype)
25{
26 int ret = 0;
Eric Parisc544c022008-04-18 17:38:24 -040027
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 switch (msgtype) {
29 case SELNL_MSG_SETENFORCE:
30 ret = sizeof(struct selnl_msg_setenforce);
31 break;
Eric Parisc544c022008-04-18 17:38:24 -040032
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 case SELNL_MSG_POLICYLOAD:
34 ret = sizeof(struct selnl_msg_policyload);
35 break;
Eric Parisc544c022008-04-18 17:38:24 -040036
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 default:
38 BUG();
39 }
40 return ret;
41}
42
43static void selnl_add_payload(struct nlmsghdr *nlh, int len, int msgtype, void *data)
44{
45 switch (msgtype) {
46 case SELNL_MSG_SETENFORCE: {
47 struct selnl_msg_setenforce *msg = NLMSG_DATA(nlh);
Eric Parisc544c022008-04-18 17:38:24 -040048
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 memset(msg, 0, len);
50 msg->val = *((int *)data);
51 break;
52 }
Eric Parisc544c022008-04-18 17:38:24 -040053
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 case SELNL_MSG_POLICYLOAD: {
55 struct selnl_msg_policyload *msg = NLMSG_DATA(nlh);
Eric Parisc544c022008-04-18 17:38:24 -040056
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 memset(msg, 0, len);
58 msg->seqno = *((u32 *)data);
59 break;
60 }
61
62 default:
63 BUG();
64 }
65}
66
67static void selnl_notify(int msgtype, void *data)
68{
69 int len;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -070070 sk_buff_data_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 struct sk_buff *skb;
72 struct nlmsghdr *nlh;
Eric Parisc544c022008-04-18 17:38:24 -040073
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 len = selnl_msglen(msgtype);
Eric Parisc544c022008-04-18 17:38:24 -040075
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 skb = alloc_skb(NLMSG_SPACE(len), GFP_USER);
77 if (!skb)
78 goto oom;
79
80 tmp = skb->tail;
81 nlh = NLMSG_PUT(skb, 0, 0, msgtype, len);
82 selnl_add_payload(nlh, len, msgtype, data);
83 nlh->nlmsg_len = skb->tail - tmp;
Patrick McHardyac6d4392005-08-14 19:29:52 -070084 NETLINK_CB(skb).dst_group = SELNLGRP_AVC;
85 netlink_broadcast(selnl, skb, 0, SELNLGRP_AVC, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086out:
87 return;
Eric Parisc544c022008-04-18 17:38:24 -040088
Linus Torvalds1da177e2005-04-16 15:20:36 -070089nlmsg_failure:
90 kfree_skb(skb);
91oom:
Harvey Harrisondd6f9532008-03-06 10:03:59 +110092 printk(KERN_ERR "SELinux: OOM in %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 goto out;
94}
95
96void selnl_notify_setenforce(int val)
97{
98 selnl_notify(SELNL_MSG_SETENFORCE, &val);
99}
100
101void selnl_notify_policyload(u32 seqno)
102{
103 selnl_notify(SELNL_MSG_POLICYLOAD, &seqno);
104}
105
106static int __init selnl_init(void)
107{
Eric W. Biedermanb4b51022007-09-12 13:05:38 +0200108 selnl = netlink_kernel_create(&init_net, NETLINK_SELINUX,
109 SELNLGRP_MAX, NULL, NULL, THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 if (selnl == NULL)
111 panic("SELinux: Cannot create netlink socket.");
Eric Parisc544c022008-04-18 17:38:24 -0400112 netlink_set_nonroot(NETLINK_SELINUX, NL_NONROOT_RECV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 return 0;
114}
115
116__initcall(selnl_init);