Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | */ |
| 9 | #include <linux/init.h> |
| 10 | #include <linux/types.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 11 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/stddef.h> |
| 13 | #include <linux/kernel.h> |
Paul Gortmaker | 44fc7ea | 2011-05-26 20:52:10 -0400 | [diff] [blame] | 14 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/skbuff.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/selinux_netlink.h> |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 17 | #include <net/net_namespace.h> |
David S. Miller | 01f534d | 2012-06-26 21:41:57 -0700 | [diff] [blame] | 18 | #include <net/netlink.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | |
James Morris | 6a3fbe8 | 2011-08-30 12:09:15 +1000 | [diff] [blame] | 20 | #include "security.h" |
| 21 | |
Ondrej Mosnacek | cd2bb4c | 2021-01-06 14:26:21 +0100 | [diff] [blame] | 22 | static struct sock *selnl __ro_after_init; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
| 24 | static int selnl_msglen(int msgtype) |
| 25 | { |
| 26 | int ret = 0; |
Eric Paris | c544c02 | 2008-04-18 17:38:24 -0400 | [diff] [blame] | 27 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | switch (msgtype) { |
| 29 | case SELNL_MSG_SETENFORCE: |
| 30 | ret = sizeof(struct selnl_msg_setenforce); |
| 31 | break; |
Eric Paris | c544c02 | 2008-04-18 17:38:24 -0400 | [diff] [blame] | 32 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | case SELNL_MSG_POLICYLOAD: |
| 34 | ret = sizeof(struct selnl_msg_policyload); |
| 35 | break; |
Eric Paris | c544c02 | 2008-04-18 17:38:24 -0400 | [diff] [blame] | 36 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | default: |
| 38 | BUG(); |
| 39 | } |
| 40 | return ret; |
| 41 | } |
| 42 | |
| 43 | static void selnl_add_payload(struct nlmsghdr *nlh, int len, int msgtype, void *data) |
| 44 | { |
| 45 | switch (msgtype) { |
| 46 | case SELNL_MSG_SETENFORCE: { |
David S. Miller | 01f534d | 2012-06-26 21:41:57 -0700 | [diff] [blame] | 47 | struct selnl_msg_setenforce *msg = nlmsg_data(nlh); |
Eric Paris | c544c02 | 2008-04-18 17:38:24 -0400 | [diff] [blame] | 48 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | memset(msg, 0, len); |
| 50 | msg->val = *((int *)data); |
| 51 | break; |
| 52 | } |
Eric Paris | c544c02 | 2008-04-18 17:38:24 -0400 | [diff] [blame] | 53 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | case SELNL_MSG_POLICYLOAD: { |
David S. Miller | 01f534d | 2012-06-26 21:41:57 -0700 | [diff] [blame] | 55 | struct selnl_msg_policyload *msg = nlmsg_data(nlh); |
Eric Paris | c544c02 | 2008-04-18 17:38:24 -0400 | [diff] [blame] | 56 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | memset(msg, 0, len); |
| 58 | msg->seqno = *((u32 *)data); |
| 59 | break; |
| 60 | } |
| 61 | |
| 62 | default: |
| 63 | BUG(); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | static void selnl_notify(int msgtype, void *data) |
| 68 | { |
| 69 | int len; |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 70 | sk_buff_data_t tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | struct sk_buff *skb; |
| 72 | struct nlmsghdr *nlh; |
Eric Paris | c544c02 | 2008-04-18 17:38:24 -0400 | [diff] [blame] | 73 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | len = selnl_msglen(msgtype); |
Eric Paris | c544c02 | 2008-04-18 17:38:24 -0400 | [diff] [blame] | 75 | |
Hong zhi guo | 7795498 | 2013-03-27 06:49:35 +0000 | [diff] [blame] | 76 | skb = nlmsg_new(len, GFP_USER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | if (!skb) |
| 78 | goto oom; |
| 79 | |
| 80 | tmp = skb->tail; |
David S. Miller | 01f534d | 2012-06-26 21:41:57 -0700 | [diff] [blame] | 81 | nlh = nlmsg_put(skb, 0, 0, msgtype, len, 0); |
| 82 | if (!nlh) |
| 83 | goto out_kfree_skb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | selnl_add_payload(nlh, len, msgtype, data); |
| 85 | nlh->nlmsg_len = skb->tail - tmp; |
Patrick McHardy | ac6d439 | 2005-08-14 19:29:52 -0700 | [diff] [blame] | 86 | NETLINK_CB(skb).dst_group = SELNLGRP_AVC; |
| 87 | netlink_broadcast(selnl, skb, 0, SELNLGRP_AVC, GFP_USER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | out: |
| 89 | return; |
Eric Paris | c544c02 | 2008-04-18 17:38:24 -0400 | [diff] [blame] | 90 | |
David S. Miller | 01f534d | 2012-06-26 21:41:57 -0700 | [diff] [blame] | 91 | out_kfree_skb: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | kfree_skb(skb); |
| 93 | oom: |
peter enderborg | d85a783 | 2018-06-12 10:09:07 +0200 | [diff] [blame] | 94 | pr_err("SELinux: OOM in %s\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | goto out; |
| 96 | } |
| 97 | |
| 98 | void selnl_notify_setenforce(int val) |
| 99 | { |
| 100 | selnl_notify(SELNL_MSG_SETENFORCE, &val); |
| 101 | } |
| 102 | |
| 103 | void selnl_notify_policyload(u32 seqno) |
| 104 | { |
| 105 | selnl_notify(SELNL_MSG_POLICYLOAD, &seqno); |
| 106 | } |
| 107 | |
| 108 | static int __init selnl_init(void) |
| 109 | { |
Pablo Neira Ayuso | a31f2d1 | 2012-06-29 06:15:21 +0000 | [diff] [blame] | 110 | struct netlink_kernel_cfg cfg = { |
| 111 | .groups = SELNLGRP_MAX, |
Pablo Neira Ayuso | 9785e10 | 2012-09-08 02:53:53 +0000 | [diff] [blame] | 112 | .flags = NL_CFG_F_NONROOT_RECV, |
Pablo Neira Ayuso | a31f2d1 | 2012-06-29 06:15:21 +0000 | [diff] [blame] | 113 | }; |
| 114 | |
Pablo Neira Ayuso | 9f00d97 | 2012-09-08 02:53:54 +0000 | [diff] [blame] | 115 | selnl = netlink_kernel_create(&init_net, NETLINK_SELINUX, &cfg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | if (selnl == NULL) |
| 117 | panic("SELinux: Cannot create netlink socket."); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | __initcall(selnl_init); |