Skip to content

Commit 9843668

Browse files
committed
selinux: streamline selinux_nlmsg_lookup()
Streamline the code in selinux_nlmsg_lookup() to improve the code flow, readability, and remove the unnecessary local variables. Tested-by: Thiébaud Weksteen <[email protected]> Signed-off-by: Paul Moore <[email protected]>
1 parent d1d991e commit 9843668

File tree

1 file changed

+38
-50
lines changed

1 file changed

+38
-50
lines changed

security/selinux/nlmsgtab.c

Lines changed: 38 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -168,34 +168,12 @@ static int nlmsg_perm(u16 nlmsg_type, u32 *perm, const struct nlmsg_perm *tab, s
168168

169169
int selinux_nlmsg_lookup(u16 sclass, u16 nlmsg_type, u32 *perm)
170170
{
171-
int err = 0;
172-
173-
if (selinux_policycap_netlink_xperm()) {
174-
switch (sclass) {
175-
case SECCLASS_NETLINK_ROUTE_SOCKET:
176-
*perm = NETLINK_ROUTE_SOCKET__NLMSG;
177-
break;
178-
case SECCLASS_NETLINK_TCPDIAG_SOCKET:
179-
*perm = NETLINK_TCPDIAG_SOCKET__NLMSG;
180-
break;
181-
case SECCLASS_NETLINK_XFRM_SOCKET:
182-
*perm = NETLINK_XFRM_SOCKET__NLMSG;
183-
break;
184-
case SECCLASS_NETLINK_AUDIT_SOCKET:
185-
*perm = NETLINK_AUDIT_SOCKET__NLMSG;
186-
break;
187-
/* While it is possible to add a similar permission to other
188-
* netlink classes, note that the extended permission value is
189-
* matched against the nlmsg_type field. Notably,
190-
* SECCLASS_NETLINK_GENERIC_SOCKET uses dynamic values for this
191-
* field, which means that it cannot be added as-is.
192-
*/
193-
default:
194-
err = -ENOENT;
195-
break;
196-
}
197-
return err;
198-
}
171+
/* While it is possible to add a similar permission to other netlink
172+
* classes, note that the extended permission value is matched against
173+
* the nlmsg_type field. Notably, SECCLASS_NETLINK_GENERIC_SOCKET uses
174+
* dynamic values for this field, which means that it cannot be added
175+
* as-is.
176+
*/
199177

200178
switch (sclass) {
201179
case SECCLASS_NETLINK_ROUTE_SOCKET:
@@ -205,42 +183,52 @@ int selinux_nlmsg_lookup(u16 sclass, u16 nlmsg_type, u32 *perm)
205183
* before updating the BUILD_BUG_ON() macro!
206184
*/
207185
BUILD_BUG_ON(RTM_MAX != (RTM_NEWTUNNEL + 3));
208-
err = nlmsg_perm(nlmsg_type, perm, nlmsg_route_perms,
209-
sizeof(nlmsg_route_perms));
210-
break;
211186

187+
if (selinux_policycap_netlink_xperm()) {
188+
*perm = NETLINK_ROUTE_SOCKET__NLMSG;
189+
return 0;
190+
}
191+
return nlmsg_perm(nlmsg_type, perm, nlmsg_route_perms,
192+
sizeof(nlmsg_route_perms));
193+
break;
212194
case SECCLASS_NETLINK_TCPDIAG_SOCKET:
213-
err = nlmsg_perm(nlmsg_type, perm, nlmsg_tcpdiag_perms,
214-
sizeof(nlmsg_tcpdiag_perms));
195+
if (selinux_policycap_netlink_xperm()) {
196+
*perm = NETLINK_TCPDIAG_SOCKET__NLMSG;
197+
return 0;
198+
}
199+
return nlmsg_perm(nlmsg_type, perm, nlmsg_tcpdiag_perms,
200+
sizeof(nlmsg_tcpdiag_perms));
215201
break;
216-
217202
case SECCLASS_NETLINK_XFRM_SOCKET:
218203
/* If the BUILD_BUG_ON() below fails you must update the
219204
* structures at the top of this file with the new mappings
220205
* before updating the BUILD_BUG_ON() macro!
221206
*/
222207
BUILD_BUG_ON(XFRM_MSG_MAX != XFRM_MSG_GETDEFAULT);
223-
err = nlmsg_perm(nlmsg_type, perm, nlmsg_xfrm_perms,
224-
sizeof(nlmsg_xfrm_perms));
225-
break;
226208

209+
if (selinux_policycap_netlink_xperm()) {
210+
*perm = NETLINK_XFRM_SOCKET__NLMSG;
211+
return 0;
212+
}
213+
return nlmsg_perm(nlmsg_type, perm, nlmsg_xfrm_perms,
214+
sizeof(nlmsg_xfrm_perms));
215+
break;
227216
case SECCLASS_NETLINK_AUDIT_SOCKET:
228-
if ((nlmsg_type >= AUDIT_FIRST_USER_MSG &&
229-
nlmsg_type <= AUDIT_LAST_USER_MSG) ||
230-
(nlmsg_type >= AUDIT_FIRST_USER_MSG2 &&
231-
nlmsg_type <= AUDIT_LAST_USER_MSG2)) {
217+
if (selinux_policycap_netlink_xperm()) {
218+
*perm = NETLINK_AUDIT_SOCKET__NLMSG;
219+
return 0;
220+
} else if ((nlmsg_type >= AUDIT_FIRST_USER_MSG &&
221+
nlmsg_type <= AUDIT_LAST_USER_MSG) ||
222+
(nlmsg_type >= AUDIT_FIRST_USER_MSG2 &&
223+
nlmsg_type <= AUDIT_LAST_USER_MSG2)) {
232224
*perm = NETLINK_AUDIT_SOCKET__NLMSG_RELAY;
233-
} else {
234-
err = nlmsg_perm(nlmsg_type, perm, nlmsg_audit_perms,
235-
sizeof(nlmsg_audit_perms));
225+
return 0;
236226
}
237-
break;
238-
239-
/* No messaging from userspace, or class unknown/unhandled */
240-
default:
241-
err = -ENOENT;
227+
return nlmsg_perm(nlmsg_type, perm, nlmsg_audit_perms,
228+
sizeof(nlmsg_audit_perms));
242229
break;
243230
}
244231

245-
return err;
232+
/* No messaging from userspace, or class unknown/unhandled */
233+
return -ENOENT;
246234
}

0 commit comments

Comments
 (0)