blob: 8a77725423e0848e671a1f5bdb021fa414de6059 [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>
Paul Gortmaker44fc7ea2011-05-26 20:52:10 -040017#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/skbuff.h>
19#include <linux/netlink.h>
20#include <linux/selinux_netlink.h>
Eric W. Biedermanb4b51022007-09-12 13:05:38 +020021#include <net/net_namespace.h>
David S. Miller01f534d2012-06-26 21:41:57 -070022#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
James Morris6a3fbe82011-08-30 12:09:15 +100024#include "security.h"
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026static struct sock *selnl;
27
28static int selnl_msglen(int msgtype)
29{
30 int ret = 0;
Eric Parisc544c022008-04-18 17:38:24 -040031
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 switch (msgtype) {
33 case SELNL_MSG_SETENFORCE:
34 ret = sizeof(struct selnl_msg_setenforce);
35 break;
Eric Parisc544c022008-04-18 17:38:24 -040036
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 case SELNL_MSG_POLICYLOAD:
38 ret = sizeof(struct selnl_msg_policyload);
39 break;
Eric Parisc544c022008-04-18 17:38:24 -040040
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 default:
42 BUG();
43 }
44 return ret;
45}
46
47static void selnl_add_payload(struct nlmsghdr *nlh, int len, int msgtype, void *data)
48{
49 switch (msgtype) {
50 case SELNL_MSG_SETENFORCE: {
David S. Miller01f534d2012-06-26 21:41:57 -070051 struct selnl_msg_setenforce *msg = nlmsg_data(nlh);
Eric Parisc544c022008-04-18 17:38:24 -040052
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 memset(msg, 0, len);
54 msg->val = *((int *)data);
55 break;
56 }
Eric Parisc544c022008-04-18 17:38:24 -040057
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 case SELNL_MSG_POLICYLOAD: {
David S. Miller01f534d2012-06-26 21:41:57 -070059 struct selnl_msg_policyload *msg = nlmsg_data(nlh);
Eric Parisc544c022008-04-18 17:38:24 -040060
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 memset(msg, 0, len);
62 msg->seqno = *((u32 *)data);
63 break;
64 }
65
66 default:
67 BUG();
68 }
69}
70
71static void selnl_notify(int msgtype, void *data)
72{
73 int len;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -070074 sk_buff_data_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 struct sk_buff *skb;
76 struct nlmsghdr *nlh;
Eric Parisc544c022008-04-18 17:38:24 -040077
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 len = selnl_msglen(msgtype);
Eric Parisc544c022008-04-18 17:38:24 -040079
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 skb = alloc_skb(NLMSG_SPACE(len), GFP_USER);
81 if (!skb)
82 goto oom;
83
84 tmp = skb->tail;
David S. Miller01f534d2012-06-26 21:41:57 -070085 nlh = nlmsg_put(skb, 0, 0, msgtype, len, 0);
86 if (!nlh)
87 goto out_kfree_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 selnl_add_payload(nlh, len, msgtype, data);
89 nlh->nlmsg_len = skb->tail - tmp;
Patrick McHardyac6d4392005-08-14 19:29:52 -070090 NETLINK_CB(skb).dst_group = SELNLGRP_AVC;
91 netlink_broadcast(selnl, skb, 0, SELNLGRP_AVC, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092out:
93 return;
Eric Parisc544c022008-04-18 17:38:24 -040094
David S. Miller01f534d2012-06-26 21:41:57 -070095out_kfree_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 kfree_skb(skb);
97oom:
Harvey Harrisondd6f9532008-03-06 10:03:59 +110098 printk(KERN_ERR "SELinux: OOM in %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 goto out;
100}
101
102void selnl_notify_setenforce(int val)
103{
104 selnl_notify(SELNL_MSG_SETENFORCE, &val);
105}
106
107void selnl_notify_policyload(u32 seqno)
108{
109 selnl_notify(SELNL_MSG_POLICYLOAD, &seqno);
110}
111
112static int __init selnl_init(void)
113{
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +0000114 struct netlink_kernel_cfg cfg = {
115 .groups = SELNLGRP_MAX,
116 };
117
Eric W. Biedermanb4b51022007-09-12 13:05:38 +0200118 selnl = netlink_kernel_create(&init_net, NETLINK_SELINUX,
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +0000119 THIS_MODULE, &cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 if (selnl == NULL)
121 panic("SELinux: Cannot create netlink socket.");
Eric Parisc544c022008-04-18 17:38:24 -0400122 netlink_set_nonroot(NETLINK_SELINUX, NL_NONROOT_RECV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 return 0;
124}
125
126__initcall(selnl_init);