1 // SPDX-License-Identifier: GPL-2.0-only
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>
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>
22 static struct sock
*selnl
;
24 static int selnl_msglen(int msgtype
)
29 case SELNL_MSG_SETENFORCE
:
30 ret
= sizeof(struct selnl_msg_setenforce
);
33 case SELNL_MSG_POLICYLOAD
:
34 ret
= sizeof(struct selnl_msg_policyload
);
43 static void selnl_add_payload(struct nlmsghdr
*nlh
, int len
, int msgtype
, void *data
)
46 case SELNL_MSG_SETENFORCE
: {
47 struct selnl_msg_setenforce
*msg
= nlmsg_data(nlh
);
50 msg
->val
= *((int *)data
);
54 case SELNL_MSG_POLICYLOAD
: {
55 struct selnl_msg_policyload
*msg
= nlmsg_data(nlh
);
58 msg
->seqno
= *((u32
*)data
);
67 static void selnl_notify(int msgtype
, void *data
)
74 len
= selnl_msglen(msgtype
);
76 skb
= nlmsg_new(len
, GFP_USER
);
81 nlh
= nlmsg_put(skb
, 0, 0, msgtype
, len
, 0);
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
);
94 pr_err("SELinux: OOM in %s\n", __func__
);
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
);
117 panic("SELinux: Cannot create netlink socket.");
121 __initcall(selnl_init
);