SELinux模块向任何侦听套接字发送netlink广播。我想知道是否可以从另一个内核模块中监听netlink广播?
来自SELinux netlink代码:
netlink_broadcast(selnl, skb, 0, SELNLGRP_AVC, GFP_USER);
答案 0 :(得分:2)
我发现你可以通过使用常规套接字来监听netlink数据。并且,是的,它在内核空间中是可能的。
您基本上需要创建并绑定到套接字:
struct sock *sock = NULL;
struct sockaddr_nl addr = { 0 };
/* Create a netlink socket for SELinux traffic */
int rc = sock_create_kern(AF_NETLINK, SOCK_RAW, NETLINK_SELINUX,
&ctx.sock);
if (rc)
return rc;
addr.nl_family = AF_NETLINK;
addr.nl_pid = 0;
addr.nl_groups = SELNLGRP_AVC;
rc = kernel_bind(ctx.sock, (struct sockaddr *) &addr, sizeof(addr));
if (rc)
return rc;
/* Setup socket callback */
sock = ctx.sock->sk;
sock->sk_data_ready = netlink_data_ready;
sock->sk_allocation = GFP_KERNEL;
接收数据:
static void netlink_data_ready(struct sock *sk, int bytes)
{
struct sk_buff *skb = NULL;
struct nlmsghdr *nlh = NULL;
int rc = 0;
/* Receive the data packet (blocking) */
skb = skb_recv_datagram(sk, 0, 0, &rc);
if (rc) {
printk(KERN_ERROR "Failed on skb_recv_datagram(). rc=%d.", -rc);
return;
}
nlh = (struct nlmsghdr *) skb->data;
if (!nlh || !NLMSG_OK(nlh, bytes)) {
printk(KERN_ERROR "Invalid netlink header data.");
return;
}
if (nlh->nlmsg_type == SELNL_MSG_POLICYLOAD ||
nlh->nlmsg_type == SELNL_MSG_SETENFORCE) {
/* Insert code here */
}
}