blob: ce3f481558d8682421ddf410d3adb5f4787df732 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/stddef.h>
16#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#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
James Morris6a3fbe82011-08-30 12:09:15 +100022#include "security.h"
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024static struct sock *selnl;
25
26static int selnl_msglen(int msgtype)
27{
28 int ret = 0;
Eric Parisc544c022008-04-18 17:38:24 -040029
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 switch (msgtype) {
31 case SELNL_MSG_SETENFORCE:
32 ret = sizeof(struct selnl_msg_setenforce);
33 break;
Eric Parisc544c022008-04-18 17:38:24 -040034
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 case SELNL_MSG_POLICYLOAD:
36 ret = sizeof(struct selnl_msg_policyload);
37 break;
Eric Parisc544c022008-04-18 17:38:24 -040038
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 default:
40 BUG();
41 }
42 return ret;
43}
44
45static void selnl_add_payload(struct nlmsghdr *nlh, int len, int msgtype, void *data)
46{
47 switch (msgtype) {
48 case SELNL_MSG_SETENFORCE: {
49 struct selnl_msg_setenforce *msg = NLMSG_DATA(nlh);
Eric Parisc544c022008-04-18 17:38:24 -040050
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 memset(msg, 0, len);
52 msg->val = *((int *)data);
53 break;
54 }
Eric Parisc544c022008-04-18 17:38:24 -040055
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 case SELNL_MSG_POLICYLOAD: {
57 struct selnl_msg_policyload *msg = NLMSG_DATA(nlh);
Eric Parisc544c022008-04-18 17:38:24 -040058
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 memset(msg, 0, len);
60 msg->seqno = *((u32 *)data);
61 break;
62 }
63
64 default:
65 BUG();
66 }
67}
68
69static void selnl_notify(int msgtype, void *data)
70{
71 int len;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -070072 sk_buff_data_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 struct sk_buff *skb;
74 struct nlmsghdr *nlh;
Eric Parisc544c022008-04-18 17:38:24 -040075
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 len = selnl_msglen(msgtype);
Eric Parisc544c022008-04-18 17:38:24 -040077
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 skb = alloc_skb(NLMSG_SPACE(len), GFP_USER);
79 if (!skb)
80 goto oom;
81
82 tmp = skb->tail;
83 nlh = NLMSG_PUT(skb, 0, 0, msgtype, len);
84 selnl_add_payload(nlh, len, msgtype, data);
85 nlh->nlmsg_len = skb->tail - tmp;
Patrick McHardyac6d4392005-08-14 19:29:52 -070086 NETLINK_CB(skb).dst_group = SELNLGRP_AVC;
87 netlink_broadcast(selnl, skb, 0, SELNLGRP_AVC, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088out:
89 return;
Eric Parisc544c022008-04-18 17:38:24 -040090
Linus Torvalds1da177e2005-04-16 15:20:36 -070091nlmsg_failure:
92 kfree_skb(skb);
93oom:
Harvey Harrisondd6f9532008-03-06 10:03:59 +110094 printk(KERN_ERR "SELinux: OOM in %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 goto out;
96}
97
98void selnl_notify_setenforce(int val)
99{
100 selnl_notify(SELNL_MSG_SETENFORCE, &val);
101}
102
103void selnl_notify_policyload(u32 seqno)
104{
105 selnl_notify(SELNL_MSG_POLICYLOAD, &seqno);
106}
107
108static int __init selnl_init(void)
109{
Eric W. Biedermanb4b51022007-09-12 13:05:38 +0200110 selnl = netlink_kernel_create(&init_net, NETLINK_SELINUX,
111 SELNLGRP_MAX, NULL, NULL, THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 if (selnl == NULL)
113 panic("SELinux: Cannot create netlink socket.");
Eric Parisc544c022008-04-18 17:38:24 -0400114 netlink_set_nonroot(NETLINK_SELINUX, NL_NONROOT_RECV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 return 0;
116}
117
118__initcall(selnl_init);