Merge tag 'block-5.11-2021-01-16' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / security / selinux / netlink.c
blob621e2e9cd6a1d70595f8f303b519f2e68d37e7dd
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Netlink event notifications for SELinux.
5 * Author: James Morris <jmorris@redhat.com>
7 * Copyright (C) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com>
8 */
9 #include <linux/init.h>
10 #include <linux/types.h>
11 #include <linux/slab.h>
12 #include <linux/stddef.h>
13 #include <linux/kernel.h>
14 #include <linux/export.h>
15 #include <linux/skbuff.h>
16 #include <linux/selinux_netlink.h>
17 #include <net/net_namespace.h>
18 #include <net/netlink.h>
20 #include "security.h"
22 static struct sock *selnl;
24 static int selnl_msglen(int msgtype)
26 int ret = 0;
28 switch (msgtype) {
29 case SELNL_MSG_SETENFORCE:
30 ret = sizeof(struct selnl_msg_setenforce);
31 break;
33 case SELNL_MSG_POLICYLOAD:
34 ret = sizeof(struct selnl_msg_policyload);
35 break;
37 default:
38 BUG();
40 return ret;
43 static void selnl_add_payload(struct nlmsghdr *nlh, int len, int msgtype, void *data)
45 switch (msgtype) {
46 case SELNL_MSG_SETENFORCE: {
47 struct selnl_msg_setenforce *msg = nlmsg_data(nlh);
49 memset(msg, 0, len);
50 msg->val = *((int *)data);
51 break;
54 case SELNL_MSG_POLICYLOAD: {
55 struct selnl_msg_policyload *msg = nlmsg_data(nlh);
57 memset(msg, 0, len);
58 msg->seqno = *((u32 *)data);
59 break;
62 default:
63 BUG();
67 static void selnl_notify(int msgtype, void *data)
69 int len;
70 sk_buff_data_t tmp;
71 struct sk_buff *skb;
72 struct nlmsghdr *nlh;
74 len = selnl_msglen(msgtype);
76 skb = nlmsg_new(len, GFP_USER);
77 if (!skb)
78 goto oom;
80 tmp = skb->tail;
81 nlh = nlmsg_put(skb, 0, 0, msgtype, len, 0);
82 if (!nlh)
83 goto out_kfree_skb;
84 selnl_add_payload(nlh, len, msgtype, data);
85 nlh->nlmsg_len = skb->tail - tmp;
86 NETLINK_CB(skb).dst_group = SELNLGRP_AVC;
87 netlink_broadcast(selnl, skb, 0, SELNLGRP_AVC, GFP_USER);
88 out:
89 return;
91 out_kfree_skb:
92 kfree_skb(skb);
93 oom:
94 pr_err("SELinux: OOM in %s\n", __func__);
95 goto out;
98 void selnl_notify_setenforce(int val)
100 selnl_notify(SELNL_MSG_SETENFORCE, &val);
103 void selnl_notify_policyload(u32 seqno)
105 selnl_notify(SELNL_MSG_POLICYLOAD, &seqno);
108 static int __init selnl_init(void)
110 struct netlink_kernel_cfg cfg = {
111 .groups = SELNLGRP_MAX,
112 .flags = NL_CFG_F_NONROOT_RECV,
115 selnl = netlink_kernel_create(&init_net, NETLINK_SELINUX, &cfg);
116 if (selnl == NULL)
117 panic("SELinux: Cannot create netlink socket.");
118 return 0;
121 __initcall(selnl_init);