nlmsgtab.c 7.5 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Netlink message type permission tables, for user generated messages.
 *
 * Author: James Morris <jmorris@redhat.com>
 *
 * Copyright (C) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2,
 * as published by the Free Software Foundation.
 */
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <linux/if.h>
17
#include <linux/inet_diag.h>
L
Linus Torvalds 已提交
18 19
#include <linux/xfrm.h>
#include <linux/audit.h>
20
#include <linux/sock_diag.h>
L
Linus Torvalds 已提交
21 22 23

#include "flask.h"
#include "av_permissions.h"
24
#include "security.h"
L
Linus Torvalds 已提交
25

26
struct nlmsg_perm {
L
Linus Torvalds 已提交
27 28 29 30
	u16	nlmsg_type;
	u32	perm;
};

31
static const struct nlmsg_perm nlmsg_route_perms[] =
L
Linus Torvalds 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
{
	{ RTM_NEWLINK,		NETLINK_ROUTE_SOCKET__NLMSG_WRITE },
	{ RTM_DELLINK,		NETLINK_ROUTE_SOCKET__NLMSG_WRITE },
	{ RTM_GETLINK,		NETLINK_ROUTE_SOCKET__NLMSG_READ  },
	{ RTM_SETLINK,		NETLINK_ROUTE_SOCKET__NLMSG_WRITE },
	{ RTM_NEWADDR,		NETLINK_ROUTE_SOCKET__NLMSG_WRITE },
	{ RTM_DELADDR,		NETLINK_ROUTE_SOCKET__NLMSG_WRITE },
	{ RTM_GETADDR,		NETLINK_ROUTE_SOCKET__NLMSG_READ  },
	{ RTM_NEWROUTE,		NETLINK_ROUTE_SOCKET__NLMSG_WRITE },
	{ RTM_DELROUTE,		NETLINK_ROUTE_SOCKET__NLMSG_WRITE },
	{ RTM_GETROUTE,		NETLINK_ROUTE_SOCKET__NLMSG_READ  },
	{ RTM_NEWNEIGH,		NETLINK_ROUTE_SOCKET__NLMSG_WRITE },
	{ RTM_DELNEIGH,		NETLINK_ROUTE_SOCKET__NLMSG_WRITE },
	{ RTM_GETNEIGH,		NETLINK_ROUTE_SOCKET__NLMSG_READ  },
	{ RTM_NEWRULE,		NETLINK_ROUTE_SOCKET__NLMSG_WRITE },
	{ RTM_DELRULE,		NETLINK_ROUTE_SOCKET__NLMSG_WRITE },
	{ RTM_GETRULE,		NETLINK_ROUTE_SOCKET__NLMSG_READ  },
	{ RTM_NEWQDISC,		NETLINK_ROUTE_SOCKET__NLMSG_WRITE },
	{ RTM_DELQDISC,		NETLINK_ROUTE_SOCKET__NLMSG_WRITE },
	{ RTM_GETQDISC,		NETLINK_ROUTE_SOCKET__NLMSG_READ  },
	{ RTM_NEWTCLASS,	NETLINK_ROUTE_SOCKET__NLMSG_WRITE },
	{ RTM_DELTCLASS,	NETLINK_ROUTE_SOCKET__NLMSG_WRITE },
	{ RTM_GETTCLASS,	NETLINK_ROUTE_SOCKET__NLMSG_READ  },
	{ RTM_NEWTFILTER,	NETLINK_ROUTE_SOCKET__NLMSG_WRITE },
	{ RTM_DELTFILTER,	NETLINK_ROUTE_SOCKET__NLMSG_WRITE },
	{ RTM_GETTFILTER,	NETLINK_ROUTE_SOCKET__NLMSG_READ  },
	{ RTM_NEWACTION,	NETLINK_ROUTE_SOCKET__NLMSG_WRITE },
	{ RTM_DELACTION,	NETLINK_ROUTE_SOCKET__NLMSG_WRITE },
	{ RTM_GETACTION,	NETLINK_ROUTE_SOCKET__NLMSG_READ  },
	{ RTM_NEWPREFIX,	NETLINK_ROUTE_SOCKET__NLMSG_WRITE },
	{ RTM_GETMULTICAST,	NETLINK_ROUTE_SOCKET__NLMSG_READ  },
	{ RTM_GETANYCAST,	NETLINK_ROUTE_SOCKET__NLMSG_READ  },
64 65
	{ RTM_GETNEIGHTBL,	NETLINK_ROUTE_SOCKET__NLMSG_READ  },
	{ RTM_SETNEIGHTBL,	NETLINK_ROUTE_SOCKET__NLMSG_WRITE },
66 67 68
	{ RTM_NEWADDRLABEL,	NETLINK_ROUTE_SOCKET__NLMSG_WRITE },
	{ RTM_DELADDRLABEL,	NETLINK_ROUTE_SOCKET__NLMSG_WRITE },
	{ RTM_GETADDRLABEL,	NETLINK_ROUTE_SOCKET__NLMSG_READ  },
69 70
	{ RTM_GETDCB,		NETLINK_ROUTE_SOCKET__NLMSG_READ  },
	{ RTM_SETDCB,		NETLINK_ROUTE_SOCKET__NLMSG_WRITE },
71
	{ RTM_NEWNETCONF,	NETLINK_ROUTE_SOCKET__NLMSG_WRITE },
D
David Ahern 已提交
72
	{ RTM_DELNETCONF,	NETLINK_ROUTE_SOCKET__NLMSG_WRITE },
73
	{ RTM_GETNETCONF,	NETLINK_ROUTE_SOCKET__NLMSG_READ  },
74 75
	{ RTM_NEWMDB,		NETLINK_ROUTE_SOCKET__NLMSG_WRITE },
	{ RTM_DELMDB,		NETLINK_ROUTE_SOCKET__NLMSG_WRITE  },
76
	{ RTM_GETMDB,		NETLINK_ROUTE_SOCKET__NLMSG_READ  },
77
	{ RTM_NEWNSID,		NETLINK_ROUTE_SOCKET__NLMSG_WRITE },
N
Nicolas Dichtel 已提交
78
	{ RTM_DELNSID,		NETLINK_ROUTE_SOCKET__NLMSG_READ  },
79
	{ RTM_GETNSID,		NETLINK_ROUTE_SOCKET__NLMSG_READ  },
80 81
	{ RTM_NEWSTATS,		NETLINK_ROUTE_SOCKET__NLMSG_READ },
	{ RTM_GETSTATS,		NETLINK_ROUTE_SOCKET__NLMSG_READ  },
82
	{ RTM_NEWCACHEREPORT,	NETLINK_ROUTE_SOCKET__NLMSG_READ },
L
Linus Torvalds 已提交
83 84
};

85
static const struct nlmsg_perm nlmsg_tcpdiag_perms[] =
L
Linus Torvalds 已提交
86 87
{
	{ TCPDIAG_GETSOCK,	NETLINK_TCPDIAG_SOCKET__NLMSG_READ },
88
	{ DCCPDIAG_GETSOCK,	NETLINK_TCPDIAG_SOCKET__NLMSG_READ },
89
	{ SOCK_DIAG_BY_FAMILY,	NETLINK_TCPDIAG_SOCKET__NLMSG_READ },
90
	{ SOCK_DESTROY,		NETLINK_TCPDIAG_SOCKET__NLMSG_WRITE },
L
Linus Torvalds 已提交
91 92
};

93
static const struct nlmsg_perm nlmsg_xfrm_perms[] =
L
Linus Torvalds 已提交
94 95 96 97 98 99 100 101
{
	{ XFRM_MSG_NEWSA,	NETLINK_XFRM_SOCKET__NLMSG_WRITE },
	{ XFRM_MSG_DELSA,	NETLINK_XFRM_SOCKET__NLMSG_WRITE },
	{ XFRM_MSG_GETSA,	NETLINK_XFRM_SOCKET__NLMSG_READ  },
	{ XFRM_MSG_NEWPOLICY,	NETLINK_XFRM_SOCKET__NLMSG_WRITE },
	{ XFRM_MSG_DELPOLICY,	NETLINK_XFRM_SOCKET__NLMSG_WRITE },
	{ XFRM_MSG_GETPOLICY,	NETLINK_XFRM_SOCKET__NLMSG_READ  },
	{ XFRM_MSG_ALLOCSPI,	NETLINK_XFRM_SOCKET__NLMSG_WRITE },
102 103
	{ XFRM_MSG_ACQUIRE,	NETLINK_XFRM_SOCKET__NLMSG_WRITE },
	{ XFRM_MSG_EXPIRE,	NETLINK_XFRM_SOCKET__NLMSG_WRITE },
L
Linus Torvalds 已提交
104 105
	{ XFRM_MSG_UPDPOLICY,	NETLINK_XFRM_SOCKET__NLMSG_WRITE },
	{ XFRM_MSG_UPDSA,	NETLINK_XFRM_SOCKET__NLMSG_WRITE },
106 107 108 109 110
	{ XFRM_MSG_POLEXPIRE,	NETLINK_XFRM_SOCKET__NLMSG_WRITE },
	{ XFRM_MSG_FLUSHSA,	NETLINK_XFRM_SOCKET__NLMSG_WRITE },
	{ XFRM_MSG_FLUSHPOLICY,	NETLINK_XFRM_SOCKET__NLMSG_WRITE },
	{ XFRM_MSG_NEWAE,	NETLINK_XFRM_SOCKET__NLMSG_WRITE },
	{ XFRM_MSG_GETAE,	NETLINK_XFRM_SOCKET__NLMSG_READ  },
111
	{ XFRM_MSG_REPORT,	NETLINK_XFRM_SOCKET__NLMSG_READ  },
112
	{ XFRM_MSG_MIGRATE,	NETLINK_XFRM_SOCKET__NLMSG_WRITE },
113 114
	{ XFRM_MSG_NEWSADINFO,	NETLINK_XFRM_SOCKET__NLMSG_READ  },
	{ XFRM_MSG_GETSADINFO,	NETLINK_XFRM_SOCKET__NLMSG_READ  },
115
	{ XFRM_MSG_NEWSPDINFO,	NETLINK_XFRM_SOCKET__NLMSG_WRITE },
116
	{ XFRM_MSG_GETSPDINFO,	NETLINK_XFRM_SOCKET__NLMSG_READ  },
117
	{ XFRM_MSG_MAPPING,	NETLINK_XFRM_SOCKET__NLMSG_READ  },
L
Linus Torvalds 已提交
118 119
};

120
static const struct nlmsg_perm nlmsg_audit_perms[] =
L
Linus Torvalds 已提交
121
{
122 123 124 125 126
	{ AUDIT_GET,		NETLINK_AUDIT_SOCKET__NLMSG_READ     },
	{ AUDIT_SET,		NETLINK_AUDIT_SOCKET__NLMSG_WRITE    },
	{ AUDIT_LIST,		NETLINK_AUDIT_SOCKET__NLMSG_READPRIV },
	{ AUDIT_ADD,		NETLINK_AUDIT_SOCKET__NLMSG_WRITE    },
	{ AUDIT_DEL,		NETLINK_AUDIT_SOCKET__NLMSG_WRITE    },
127 128 129
	{ AUDIT_LIST_RULES,	NETLINK_AUDIT_SOCKET__NLMSG_READPRIV },
	{ AUDIT_ADD_RULE,	NETLINK_AUDIT_SOCKET__NLMSG_WRITE    },
	{ AUDIT_DEL_RULE,	NETLINK_AUDIT_SOCKET__NLMSG_WRITE    },
130
	{ AUDIT_USER,		NETLINK_AUDIT_SOCKET__NLMSG_RELAY    },
131
	{ AUDIT_SIGNAL_INFO,	NETLINK_AUDIT_SOCKET__NLMSG_READ     },
132 133
	{ AUDIT_TRIM,		NETLINK_AUDIT_SOCKET__NLMSG_WRITE    },
	{ AUDIT_MAKE_EQUIV,	NETLINK_AUDIT_SOCKET__NLMSG_WRITE    },
M
Miloslav Trmac 已提交
134
	{ AUDIT_TTY_GET,	NETLINK_AUDIT_SOCKET__NLMSG_READ     },
135
	{ AUDIT_TTY_SET,	NETLINK_AUDIT_SOCKET__NLMSG_TTY_AUDIT	},
136 137
	{ AUDIT_GET_FEATURE,	NETLINK_AUDIT_SOCKET__NLMSG_READ     },
	{ AUDIT_SET_FEATURE,	NETLINK_AUDIT_SOCKET__NLMSG_WRITE    },
L
Linus Torvalds 已提交
138 139 140
};


141
static int nlmsg_perm(u16 nlmsg_type, u32 *perm, const struct nlmsg_perm *tab, size_t tabsize)
L
Linus Torvalds 已提交
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
{
	int i, err = -EINVAL;

	for (i = 0; i < tabsize/sizeof(struct nlmsg_perm); i++)
		if (nlmsg_type == tab[i].nlmsg_type) {
			*perm = tab[i].perm;
			err = 0;
			break;
		}

	return err;
}

int selinux_nlmsg_lookup(u16 sclass, u16 nlmsg_type, u32 *perm)
{
	int err = 0;

	switch (sclass) {
	case SECCLASS_NETLINK_ROUTE_SOCKET:
161
		/* RTM_MAX always point to RTM_SETxxxx, ie RTM_NEWxxx + 3 */
162
		BUILD_BUG_ON(RTM_MAX != (RTM_NEWCHAIN + 3));
L
Linus Torvalds 已提交
163 164 165 166 167 168 169 170 171 172
		err = nlmsg_perm(nlmsg_type, perm, nlmsg_route_perms,
				 sizeof(nlmsg_route_perms));
		break;

	case SECCLASS_NETLINK_TCPDIAG_SOCKET:
		err = nlmsg_perm(nlmsg_type, perm, nlmsg_tcpdiag_perms,
				 sizeof(nlmsg_tcpdiag_perms));
		break;

	case SECCLASS_NETLINK_XFRM_SOCKET:
173
		BUILD_BUG_ON(XFRM_MSG_MAX != XFRM_MSG_MAPPING);
L
Linus Torvalds 已提交
174 175 176 177 178
		err = nlmsg_perm(nlmsg_type, perm, nlmsg_xfrm_perms,
				 sizeof(nlmsg_xfrm_perms));
		break;

	case SECCLASS_NETLINK_AUDIT_SOCKET:
179 180 181
		if ((nlmsg_type >= AUDIT_FIRST_USER_MSG &&
		     nlmsg_type <= AUDIT_LAST_USER_MSG) ||
		    (nlmsg_type >= AUDIT_FIRST_USER_MSG2 &&
182
		     nlmsg_type <= AUDIT_LAST_USER_MSG2)) {
183 184 185 186 187
			*perm = NETLINK_AUDIT_SOCKET__NLMSG_RELAY;
		} else {
			err = nlmsg_perm(nlmsg_type, perm, nlmsg_audit_perms,
					 sizeof(nlmsg_audit_perms));
		}
L
Linus Torvalds 已提交
188 189 190 191 192 193 194 195 196 197
		break;

	/* No messaging from userspace, or class unknown/unhandled */
	default:
		err = -ENOENT;
		break;
	}

	return err;
}