Linux Audio

Check our new training course

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