blob: 1760aee712fd2ef4ed805d6f0a9edffedd547e58 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Netlink event notifications for SELinux.
4 *
5 * Author: James Morris <jmorris@redhat.com>
6 *
7 * Copyright (C) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9#include <linux/init.h>
10#include <linux/types.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090011#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/stddef.h>
13#include <linux/kernel.h>
Paul Gortmaker44fc7ea2011-05-26 20:52:10 -040014#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/selinux_netlink.h>
Eric W. Biedermanb4b51022007-09-12 13:05:38 +020017#include <net/net_namespace.h>
David S. Miller01f534d2012-06-26 21:41:57 -070018#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
James Morris6a3fbe82011-08-30 12:09:15 +100020#include "security.h"
21
Ondrej Mosnacekcd2bb4c2021-01-06 14:26:21 +010022static struct sock *selnl __ro_after_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24static int selnl_msglen(int msgtype)
25{
26 int ret = 0;
Eric Parisc544c022008-04-18 17:38:24 -040027
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 switch (msgtype) {
29 case SELNL_MSG_SETENFORCE:
30 ret = sizeof(struct selnl_msg_setenforce);
31 break;
Eric Parisc544c022008-04-18 17:38:24 -040032
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 case SELNL_MSG_POLICYLOAD:
34 ret = sizeof(struct selnl_msg_policyload);
35 break;
Eric Parisc544c022008-04-18 17:38:24 -040036
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 default:
38 BUG();
39 }
40 return ret;
41}
42
43static void selnl_add_payload(struct nlmsghdr *nlh, int len, int msgtype, void *data)
44{
45 switch (msgtype) {
46 case SELNL_MSG_SETENFORCE: {
David S. Miller01f534d2012-06-26 21:41:57 -070047 struct selnl_msg_setenforce *msg = nlmsg_data(nlh);
Eric Parisc544c022008-04-18 17:38:24 -040048
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 memset(msg, 0, len);
50 msg->val = *((int *)data);
51 break;
52 }
Eric Parisc544c022008-04-18 17:38:24 -040053
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 case SELNL_MSG_POLICYLOAD: {
David S. Miller01f534d2012-06-26 21:41:57 -070055 struct selnl_msg_policyload *msg = nlmsg_data(nlh);
Eric Parisc544c022008-04-18 17:38:24 -040056
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 memset(msg, 0, len);
58 msg->seqno = *((u32 *)data);
59 break;
60 }
61
62 default:
63 BUG();
64 }
65}
66
67static void selnl_notify(int msgtype, void *data)
68{
69 int len;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -070070 sk_buff_data_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 struct sk_buff *skb;
72 struct nlmsghdr *nlh;
Eric Parisc544c022008-04-18 17:38:24 -040073
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 len = selnl_msglen(msgtype);
Eric Parisc544c022008-04-18 17:38:24 -040075
Hong zhi guo77954982013-03-27 06:49:35 +000076 skb = nlmsg_new(len, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 if (!skb)
78 goto oom;
79
80 tmp = skb->tail;
David S. Miller01f534d2012-06-26 21:41:57 -070081 nlh = nlmsg_put(skb, 0, 0, msgtype, len, 0);
82 if (!nlh)
83 goto out_kfree_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 selnl_add_payload(nlh, len, msgtype, data);
85 nlh->nlmsg_len = skb->tail - tmp;
Patrick McHardyac6d4392005-08-14 19:29:52 -070086 NETLINK_CB(skb).dst_group = SELNLGRP_AVC;
87 netlink_broadcast(selnl, skb, 0, SELNLGRP_AVC, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088out:
89 return;
Eric Parisc544c022008-04-18 17:38:24 -040090
David S. Miller01f534d2012-06-26 21:41:57 -070091out_kfree_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 kfree_skb(skb);
93oom:
peter enderborgd85a7832018-06-12 10:09:07 +020094 pr_err("SELinux: OOM in %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 goto out;
96}
97
98void selnl_notify_setenforce(int val)
99{
100 selnl_notify(SELNL_MSG_SETENFORCE, &val);
101}
102
103void selnl_notify_policyload(u32 seqno)
104{
105 selnl_notify(SELNL_MSG_POLICYLOAD, &seqno);
106}
107
108static int __init selnl_init(void)
109{
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +0000110 struct netlink_kernel_cfg cfg = {
111 .groups = SELNLGRP_MAX,
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +0000112 .flags = NL_CFG_F_NONROOT_RECV,
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +0000113 };
114
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +0000115 selnl = netlink_kernel_create(&init_net, NETLINK_SELINUX, &cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 if (selnl == NULL)
117 panic("SELinux: Cannot create netlink socket.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 return 0;
119}
120
121__initcall(selnl_init);