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