blob: 828fb6a4e9410e6a0bae91c2126a7645db4f4daa [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/selinux_netlink.h>
Eric W. Biedermanb4b51022007-09-12 13:05:38 +020020#include <net/net_namespace.h>
David S. Miller01f534d2012-06-26 21:41:57 -070021#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
James Morris6a3fbe82011-08-30 12:09:15 +100023#include "security.h"
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025static struct sock *selnl;
26
27static int selnl_msglen(int msgtype)
28{
29 int ret = 0;
Eric Parisc544c022008-04-18 17:38:24 -040030
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 switch (msgtype) {
32 case SELNL_MSG_SETENFORCE:
33 ret = sizeof(struct selnl_msg_setenforce);
34 break;
Eric Parisc544c022008-04-18 17:38:24 -040035
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 case SELNL_MSG_POLICYLOAD:
37 ret = sizeof(struct selnl_msg_policyload);
38 break;
Eric Parisc544c022008-04-18 17:38:24 -040039
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 default:
41 BUG();
42 }
43 return ret;
44}
45
46static void selnl_add_payload(struct nlmsghdr *nlh, int len, int msgtype, void *data)
47{
48 switch (msgtype) {
49 case SELNL_MSG_SETENFORCE: {
David S. Miller01f534d2012-06-26 21:41:57 -070050 struct selnl_msg_setenforce *msg = nlmsg_data(nlh);
Eric Parisc544c022008-04-18 17:38:24 -040051
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 memset(msg, 0, len);
53 msg->val = *((int *)data);
54 break;
55 }
Eric Parisc544c022008-04-18 17:38:24 -040056
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 case SELNL_MSG_POLICYLOAD: {
David S. Miller01f534d2012-06-26 21:41:57 -070058 struct selnl_msg_policyload *msg = nlmsg_data(nlh);
Eric Parisc544c022008-04-18 17:38:24 -040059
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 memset(msg, 0, len);
61 msg->seqno = *((u32 *)data);
62 break;
63 }
64
65 default:
66 BUG();
67 }
68}
69
70static void selnl_notify(int msgtype, void *data)
71{
72 int len;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -070073 sk_buff_data_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 struct sk_buff *skb;
75 struct nlmsghdr *nlh;
Eric Parisc544c022008-04-18 17:38:24 -040076
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 len = selnl_msglen(msgtype);
Eric Parisc544c022008-04-18 17:38:24 -040078
Hong zhi guo77954982013-03-27 06:49:35 +000079 skb = nlmsg_new(len, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 if (!skb)
81 goto oom;
82
83 tmp = skb->tail;
David S. Miller01f534d2012-06-26 21:41:57 -070084 nlh = nlmsg_put(skb, 0, 0, msgtype, len, 0);
85 if (!nlh)
86 goto out_kfree_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 selnl_add_payload(nlh, len, msgtype, data);
88 nlh->nlmsg_len = skb->tail - tmp;
Patrick McHardyac6d4392005-08-14 19:29:52 -070089 NETLINK_CB(skb).dst_group = SELNLGRP_AVC;
90 netlink_broadcast(selnl, skb, 0, SELNLGRP_AVC, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091out:
92 return;
Eric Parisc544c022008-04-18 17:38:24 -040093
David S. Miller01f534d2012-06-26 21:41:57 -070094out_kfree_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 kfree_skb(skb);
96oom:
Harvey Harrisondd6f9532008-03-06 10:03:59 +110097 printk(KERN_ERR "SELinux: OOM in %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 goto out;
99}
100
101void selnl_notify_setenforce(int val)
102{
103 selnl_notify(SELNL_MSG_SETENFORCE, &val);
104}
105
106void selnl_notify_policyload(u32 seqno)
107{
108 selnl_notify(SELNL_MSG_POLICYLOAD, &seqno);
109}
110
111static int __init selnl_init(void)
112{
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +0000113 struct netlink_kernel_cfg cfg = {
114 .groups = SELNLGRP_MAX,
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +0000115 .flags = NL_CFG_F_NONROOT_RECV,
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +0000116 };
117
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +0000118 selnl = netlink_kernel_create(&init_net, NETLINK_SELINUX, &cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 if (selnl == NULL)
120 panic("SELinux: Cannot create netlink socket.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 return 0;
122}
123
124__initcall(selnl_init);