Commit | Line | Data |
---|---|---|
1da177e4 LT |
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> | |
5a0e3ad6 | 14 | #include <linux/slab.h> |
1da177e4 LT |
15 | #include <linux/stddef.h> |
16 | #include <linux/kernel.h> | |
44fc7ea0 | 17 | #include <linux/export.h> |
1da177e4 LT |
18 | #include <linux/skbuff.h> |
19 | #include <linux/netlink.h> | |
20 | #include <linux/selinux_netlink.h> | |
b4b51029 | 21 | #include <net/net_namespace.h> |
01f534d0 | 22 | #include <net/netlink.h> |
1da177e4 | 23 | |
6a3fbe81 JM |
24 | #include "security.h" |
25 | ||
1da177e4 LT |
26 | static struct sock *selnl; |
27 | ||
28 | static int selnl_msglen(int msgtype) | |
29 | { | |
30 | int ret = 0; | |
c544c028 | 31 | |
1da177e4 LT |
32 | switch (msgtype) { |
33 | case SELNL_MSG_SETENFORCE: | |
34 | ret = sizeof(struct selnl_msg_setenforce); | |
35 | break; | |
c544c028 | 36 | |
1da177e4 LT |
37 | case SELNL_MSG_POLICYLOAD: |
38 | ret = sizeof(struct selnl_msg_policyload); | |
39 | break; | |
c544c028 | 40 | |
1da177e4 LT |
41 | default: |
42 | BUG(); | |
43 | } | |
44 | return ret; | |
45 | } | |
46 | ||
47 | static void selnl_add_payload(struct nlmsghdr *nlh, int len, int msgtype, void *data) | |
48 | { | |
49 | switch (msgtype) { | |
50 | case SELNL_MSG_SETENFORCE: { | |
01f534d0 | 51 | struct selnl_msg_setenforce *msg = nlmsg_data(nlh); |
c544c028 | 52 | |
1da177e4 LT |
53 | memset(msg, 0, len); |
54 | msg->val = *((int *)data); | |
55 | break; | |
56 | } | |
c544c028 | 57 | |
1da177e4 | 58 | case SELNL_MSG_POLICYLOAD: { |
01f534d0 | 59 | struct selnl_msg_policyload *msg = nlmsg_data(nlh); |
c544c028 | 60 | |
1da177e4 LT |
61 | memset(msg, 0, len); |
62 | msg->seqno = *((u32 *)data); | |
63 | break; | |
64 | } | |
65 | ||
66 | default: | |
67 | BUG(); | |
68 | } | |
69 | } | |
70 | ||
71 | static void selnl_notify(int msgtype, void *data) | |
72 | { | |
73 | int len; | |
27a884dc | 74 | sk_buff_data_t tmp; |
1da177e4 LT |
75 | struct sk_buff *skb; |
76 | struct nlmsghdr *nlh; | |
c544c028 | 77 | |
1da177e4 | 78 | len = selnl_msglen(msgtype); |
c544c028 | 79 | |
1da177e4 LT |
80 | skb = alloc_skb(NLMSG_SPACE(len), GFP_USER); |
81 | if (!skb) | |
82 | goto oom; | |
83 | ||
84 | tmp = skb->tail; | |
01f534d0 DM |
85 | nlh = nlmsg_put(skb, 0, 0, msgtype, len, 0); |
86 | if (!nlh) | |
87 | goto out_kfree_skb; | |
1da177e4 LT |
88 | selnl_add_payload(nlh, len, msgtype, data); |
89 | nlh->nlmsg_len = skb->tail - tmp; | |
ac6d439d PM |
90 | NETLINK_CB(skb).dst_group = SELNLGRP_AVC; |
91 | netlink_broadcast(selnl, skb, 0, SELNLGRP_AVC, GFP_USER); | |
1da177e4 LT |
92 | out: |
93 | return; | |
c544c028 | 94 | |
01f534d0 | 95 | out_kfree_skb: |
1da177e4 LT |
96 | kfree_skb(skb); |
97 | oom: | |
dd6f953a | 98 | printk(KERN_ERR "SELinux: OOM in %s\n", __func__); |
1da177e4 LT |
99 | goto out; |
100 | } | |
101 | ||
102 | void selnl_notify_setenforce(int val) | |
103 | { | |
104 | selnl_notify(SELNL_MSG_SETENFORCE, &val); | |
105 | } | |
106 | ||
107 | void selnl_notify_policyload(u32 seqno) | |
108 | { | |
109 | selnl_notify(SELNL_MSG_POLICYLOAD, &seqno); | |
110 | } | |
111 | ||
112 | static int __init selnl_init(void) | |
113 | { | |
a31f2d17 PNA |
114 | struct netlink_kernel_cfg cfg = { |
115 | .groups = SELNLGRP_MAX, | |
9785e10a | 116 | .flags = NL_CFG_F_NONROOT_RECV, |
a31f2d17 PNA |
117 | }; |
118 | ||
9f00d977 | 119 | selnl = netlink_kernel_create(&init_net, NETLINK_SELINUX, &cfg); |
1da177e4 LT |
120 | if (selnl == NULL) |
121 | panic("SELinux: Cannot create netlink socket."); | |
1da177e4 LT |
122 | return 0; |
123 | } | |
124 | ||
125 | __initcall(selnl_init); |