Skip to contentSkip to conversation

selinux: Add netlink xperm support

PATCH In review Thiébaud Weksteen 2 participants 5 messages Sep 12, 2024 – Oct 7, 2024 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 selinux: Add netlink xperm support

Reuse the existing extended permissions infrastructure to support policies based on the netlink message types.

A new policy capability "netlink_xperm" is introduced. When disabled, the previous behaviour is preserved. That is, netlink_send will rely on the permission mappings defined in nlmsgtab.c (e.g, nlmsg_read for RTM_GETADDR on NETLINK_ROUTE). When enabled, the mappings are ignored and the generic "nlmsg" permission is used instead.

The new "nlmsg" permission is an extended permission. The 16 bits of the extended permission are mapped to the nlmsg_type field.

Example policy on Android, preventing regular apps from accessing the device's MAC address and ARP table, but allowing this access to privileged apps, looks as follows:

allow netdomain self:netlink_route_socket {
create read getattr write setattr lock append connect getopt setopt shutdown nlmsg
};
allowxperm netdomain self:netlink_route_socket nlmsg ~{
RTM_GETLINK RTM_GETNEIGH RTM_GETNEIGHTBL
};
allowxperm priv_app self:netlink_route_socket nlmsg {
RTM_GETLINK RTM_GETNEIGH RTM_GETNEIGHTBL
};

The constants in the example above (e.g., RTM_GETLINK) are explicitly defined in the policy.

It is possible to generate policies to support kernels that may or may not have the capability enabled by generating a rule for each scenario. For instance:

allow domain self:netlink_audit_socket nlmsg_read; allow domain self:netlink_audit_socket nlmsg; allowxperm domain self:netlink_audit_socket nlmsg { AUDIT_GET };

The approach of defining a new permission ("nlmsg") instead of relying on the existing permissions (e.g., "nlmsg_read", "nlmsg_readpriv" or "nlmsg_tty_audit") has been preferred because:
1. This is similar to the other extended permission ("ioctl");
2. With the new extended permission, the coarse-grained mapping is not necessary anymore. It could eventually be removed, which would be impossible if the extended permission was defined below these.
3. Having a single extra extended permission considerably simplifies the implementation here and in libselinux.

---
v3:
- Remove condition on SECCLASS_NETLINK_GENERIC_SOCKET in selinux_netlink_send.
- Remove comment in selinux_netlink_send.
- Add comment in selinux_nlmsg_lookup.
- Update commit message.
v2: Reorder classmap.h to keep the new permission "nlmsg" at the end.

 security/selinux/hooks.c                   | 51 +++++++++++---
 security/selinux/include/classmap.h        |  8 +--
 security/selinux/include/policycap.h       |  1 +
 security/selinux/include/policycap_names.h |  1 +
 security/selinux/include/security.h        |  6 ++
 security/selinux/nlmsgtab.c                | 27 ++++++++
 security/selinux/ss/avtab.h                |  5 +-
 security/selinux/ss/services.c             | 78 ++++++++++++----------
 8 files changed, 126 insertions(+), 51 deletions(-)
security/selinux/hooks.c +42 -9
index 400eca4ad0fb..a9abad82c098 100644
@@ -4592,14 +4592,10 @@ static int socket_sockcreate_sid(const struct task_security_struct *tsec,
 				       secclass, NULL, socksid);
 }
 
-static int sock_has_perm(struct sock *sk, u32 perms)
+static bool sock_skip_has_perm(u32 sid)
 {
-	struct sk_security_struct *sksec = sk->sk_security;
-	struct common_audit_data ad;
-	struct lsm_network_audit net;
-
-	if (sksec->sid == SECINITSID_KERNEL)
-		return 0;
+	if (sid == SECINITSID_KERNEL)
+		return true;
 
 	/*
 	 * Before POLICYDB_CAP_USERSPACE_INITIAL_CONTEXT, sockets that
@@ -4613,7 +4609,19 @@ static int sock_has_perm(struct sock *sk, u32 perms)
 	 * setting.
 	 */
 	if (!selinux_policycap_userspace_initial_context() &&
-	    sksec->sid == SECINITSID_INIT)
+	    sid == SECINITSID_INIT)
+		return true;
+	return false;
+}
+
+
+static int sock_has_perm(struct sock *sk, u32 perms)
+{
+	struct sk_security_struct *sksec = sk->sk_security;
+	struct common_audit_data ad;
+	struct lsm_network_audit net;
+
+	if (sock_skip_has_perm(sksec->sid))
 		return 0;
 
 	ad_net_init_from_sk(&ad, &net, sk);
@@ -5939,6 +5947,26 @@ static unsigned int selinux_ip_postroute(void *priv,
 }
 #endif	/* CONFIG_NETFILTER */
 
+static int nlmsg_sock_has_extended_perms(struct sock *sk, u32 perms, u16 nlmsg_type)
+{
+	struct sk_security_struct *sksec = sk->sk_security;
+	struct common_audit_data ad;
+	struct lsm_network_audit net;
+	u8 driver;
+	u8 xperm;
+
+	if (sock_skip_has_perm(sksec->sid))
+		return 0;
+
+	ad_net_init_from_sk(&ad, &net, sk);
+
+	driver = nlmsg_type >> 8;
+	xperm = nlmsg_type & 0xff;
+
+	return avc_has_extended_perms(current_sid(), sksec->sid, sksec->sclass,
+			perms, driver, xperm, &ad);
+}
+
 static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb)
 {
 	int rc = 0;
@@ -5964,7 +5992,12 @@ static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb)
 
 		rc = selinux_nlmsg_lookup(sclass, nlh->nlmsg_type, &perm);
 		if (rc == 0) {
-			rc = sock_has_perm(sk, perm);
+			if (selinux_policycap_netlink_xperm()) {
+				rc = nlmsg_sock_has_extended_perms(
+					sk, perm, nlh->nlmsg_type);
+			} else {
+				rc = sock_has_perm(sk, perm);
+			}
 			if (rc)
 				return rc;
 		} else if (rc == -EINVAL) {
security/selinux/include/classmap.h +4 -4
index 7229c9bf6c27..cb2a52dcf0d7 100644
@@ -96,17 +96,17 @@ const struct security_class_mapping secclass_map[] = {
 	{ "shm", { COMMON_IPC_PERMS, "lock", NULL } },
 	{ "ipc", { COMMON_IPC_PERMS, NULL } },
 	{ "netlink_route_socket",
-	  { COMMON_SOCK_PERMS, "nlmsg_read", "nlmsg_write", NULL } },
+	  { COMMON_SOCK_PERMS, "nlmsg_read", "nlmsg_write", "nlmsg", NULL } },
 	{ "netlink_tcpdiag_socket",
-	  { COMMON_SOCK_PERMS, "nlmsg_read", "nlmsg_write", NULL } },
+	  { COMMON_SOCK_PERMS, "nlmsg_read", "nlmsg_write", "nlmsg", NULL } },
 	{ "netlink_nflog_socket", { COMMON_SOCK_PERMS, NULL } },
 	{ "netlink_xfrm_socket",
-	  { COMMON_SOCK_PERMS, "nlmsg_read", "nlmsg_write", NULL } },
+	  { COMMON_SOCK_PERMS, "nlmsg_read", "nlmsg_write", "nlmsg", NULL } },
 	{ "netlink_selinux_socket", { COMMON_SOCK_PERMS, NULL } },
 	{ "netlink_iscsi_socket", { COMMON_SOCK_PERMS, NULL } },
 	{ "netlink_audit_socket",
 	  { COMMON_SOCK_PERMS, "nlmsg_read", "nlmsg_write", "nlmsg_relay",
-	    "nlmsg_readpriv", "nlmsg_tty_audit", NULL } },
+	    "nlmsg_readpriv", "nlmsg_tty_audit", "nlmsg", NULL } },
 	{ "netlink_fib_lookup_socket", { COMMON_SOCK_PERMS, NULL } },
 	{ "netlink_connector_socket", { COMMON_SOCK_PERMS, NULL } },
 	{ "netlink_netfilter_socket", { COMMON_SOCK_PERMS, NULL } },
security/selinux/include/policycap.h +1 -0
index dc3674eb29c1..079679fe7254 100644
@@ -14,6 +14,7 @@ enum {
 	POLICYDB_CAP_GENFS_SECLABEL_SYMLINKS,
 	POLICYDB_CAP_IOCTL_SKIP_CLOEXEC,
 	POLICYDB_CAP_USERSPACE_INITIAL_CONTEXT,
+	POLICYDB_CAP_NETLINK_XPERM,
 	__POLICYDB_CAP_MAX
 };
 #define POLICYDB_CAP_MAX (__POLICYDB_CAP_MAX - 1)
security/selinux/include/policycap_names.h +1 -0
index 2cffcc1ce851..e080827408c4 100644
@@ -17,6 +17,7 @@ const char *const selinux_policycap_names[__POLICYDB_CAP_MAX] = {
 	"genfs_seclabel_symlinks",
 	"ioctl_skip_cloexec",
 	"userspace_initial_context",
+	"netlink_xperm",
 };
 /* clang-format on */
 
security/selinux/include/security.h +6 -0
index 289bf9233f71..c7f2731abd03 100644
@@ -195,6 +195,12 @@ static inline bool selinux_policycap_userspace_initial_context(void)
 		selinux_state.policycap[POLICYDB_CAP_USERSPACE_INITIAL_CONTEXT]);
 }
 
+static inline bool selinux_policycap_netlink_xperm(void)
+{
+	return READ_ONCE(
+		selinux_state.policycap[POLICYDB_CAP_NETLINK_XPERM]);
+}
+
 struct selinux_policy_convert_data;
 
 struct selinux_load_state {
security/selinux/nlmsgtab.c +27 -0
index 8ff670cf1ee5..acc7d74b99d5 100644
@@ -170,6 +170,33 @@ int selinux_nlmsg_lookup(u16 sclass, u16 nlmsg_type, u32 *perm)
 {
 	int err = 0;
 
+	if (selinux_policycap_netlink_xperm()) {
+		switch (sclass) {
+		case SECCLASS_NETLINK_ROUTE_SOCKET:
+			*perm = NETLINK_ROUTE_SOCKET__NLMSG;
+			break;
+		case SECCLASS_NETLINK_TCPDIAG_SOCKET:
+			*perm = NETLINK_TCPDIAG_SOCKET__NLMSG;
+			break;
+		case SECCLASS_NETLINK_XFRM_SOCKET:
+			*perm = NETLINK_XFRM_SOCKET__NLMSG;
+			break;
+		case SECCLASS_NETLINK_AUDIT_SOCKET:
+			*perm = NETLINK_AUDIT_SOCKET__NLMSG;
+			break;
+		/* While it is possible to add a similar permission to other
+		 * netlink classes, note that the extended permission value is
+		 * matched against the nlmsg_type field. Notably,
+		 * SECCLASS_NETLINK_GENERIC_SOCKET uses dynamic values for this
+		 * field, which means that it cannot be added as-is.
+		 */
+		default:
+			err = -ENOENT;
+			break;
+		}
+		return err;
+	}
+
 	switch (sclass) {
 	case SECCLASS_NETLINK_ROUTE_SOCKET:
 		/* RTM_MAX always points to RTM_SETxxxx, ie RTM_NEWxxx + 3.
security/selinux/ss/avtab.h +3 -2
index 8e8820484c55..f4407185401c 100644
@@ -53,8 +53,9 @@ struct avtab_key {
  */
 struct avtab_extended_perms {
 /* These are not flags. All 256 values may be used */
-#define AVTAB_XPERMS_IOCTLFUNCTION 0x01
-#define AVTAB_XPERMS_IOCTLDRIVER   0x02
+#define AVTAB_XPERMS_IOCTLFUNCTION	0x01
+#define AVTAB_XPERMS_IOCTLDRIVER	0x02
+#define AVTAB_XPERMS_NLMSG		0x03
 	/* extension of the avtab_key specified */
 	u8 specified; /* ioctl, netfilter, ... */
 	/*
security/selinux/ss/services.c +42 -36

119 lines not on this page · raw message

Signature · 1 line
2.46.0.662.g92d0881bb0-goog
maintainer
#1 lore raw
Quoted 60 lines · On Sep 11, 2024 "=?UTF-8?q?Thi=C3=A9baud=20Weksteen?=" <[email protected]> wrote:

Reuse the existing extended permissions infrastructure to support policies based on the netlink message types.

A new policy capability "netlink_xperm" is introduced. When disabled, the previous behaviour is preserved. That is, netlink_send will rely on the permission mappings defined in nlmsgtab.c (e.g, nlmsg_read for RTM_GETADDR on NETLINK_ROUTE). When enabled, the mappings are ignored and the generic "nlmsg" permission is used instead.

The new "nlmsg" permission is an extended permission. The 16 bits of the extended permission are mapped to the nlmsg_type field.

Example policy on Android, preventing regular apps from accessing the device's MAC address and ARP table, but allowing this access to privileged apps, looks as follows:

allow netdomain self:netlink_route_socket {
create read getattr write setattr lock append connect getopt setopt shutdown nlmsg
};
allowxperm netdomain self:netlink_route_socket nlmsg ~{
RTM_GETLINK RTM_GETNEIGH RTM_GETNEIGHTBL
};
allowxperm priv_app self:netlink_route_socket nlmsg {
RTM_GETLINK RTM_GETNEIGH RTM_GETNEIGHTBL
};

The constants in the example above (e.g., RTM_GETLINK) are explicitly defined in the policy.

It is possible to generate policies to support kernels that may or may not have the capability enabled by generating a rule for each scenario. For instance:

allow domain self:netlink_audit_socket nlmsg_read; allow domain self:netlink_audit_socket nlmsg; allowxperm domain self:netlink_audit_socket nlmsg { AUDIT_GET };

The approach of defining a new permission ("nlmsg") instead of relying on the existing permissions (e.g., "nlmsg_read", "nlmsg_readpriv" or "nlmsg_tty_audit") has been preferred because:
1. This is similar to the other extended permission ("ioctl");
2. With the new extended permission, the coarse-grained mapping is not necessary anymore. It could eventually be removed, which would be impossible if the extended permission was defined below these.
3. Having a single extra extended permission considerably simplifies the implementation here and in libselinux.

---
v3:
- Remove condition on SECCLASS_NETLINK_GENERIC_SOCKET in selinux_netlink_send.
- Remove comment in selinux_netlink_send.
- Add comment in selinux_nlmsg_lookup.
- Update commit message.
v2: Reorder classmap.h to keep the new permission "nlmsg" at the end.

Quoted 9 lines
 security/selinux/hooks.c                   | 51 +++++++++++---
 security/selinux/include/classmap.h        |  8 +--
 security/selinux/include/policycap.h       |  1 +
 security/selinux/include/policycap_names.h |  1 +
 security/selinux/include/security.h        |  6 ++
 security/selinux/nlmsgtab.c                | 27 ++++++++
 security/selinux/ss/avtab.h                |  5 +-
 security/selinux/ss/services.c             | 78 ++++++++++++----------
 8 files changed, 126 insertions(+), 51 deletions(-)

Looks good to me, thanks for the revision. We're in the merge window right now so I'm going to merge this into selinux/dev-staging now and I'll move it into selinux/dev after -rc1 is released.

--
paul-moore.com

maintainer
#3 lore raw
Quoted 7 lines · Thiébaud Weksteen
Quoted 4 lines

Looks good to me, thanks for the revision. We're in the merge window right now so I'm going to merge this into selinux/dev-staging now and I'll move it into selinux/dev after -rc1 is released.

Great, thanks Paul. I've also created a pull request to update the notebook:
https://github.com/SELinuxProject/selinux-notebook/pull/40

Excellent, thank you!

Signature · 1 line
paul-moore.com
maintainer
#4 lore raw
In reply to Paul Moore
Quoted 71 lines · Paul Moore
Quoted 60 lines · On Sep 11, 2024 "=?UTF-8?q?Thi=C3=A9baud=20Weksteen?=" <[email protected]> wrote:

Reuse the existing extended permissions infrastructure to support policies based on the netlink message types.

A new policy capability "netlink_xperm" is introduced. When disabled, the previous behaviour is preserved. That is, netlink_send will rely on the permission mappings defined in nlmsgtab.c (e.g, nlmsg_read for RTM_GETADDR on NETLINK_ROUTE). When enabled, the mappings are ignored and the generic "nlmsg" permission is used instead.

The new "nlmsg" permission is an extended permission. The 16 bits of the extended permission are mapped to the nlmsg_type field.

Example policy on Android, preventing regular apps from accessing the device's MAC address and ARP table, but allowing this access to privileged apps, looks as follows:

allow netdomain self:netlink_route_socket {
create read getattr write setattr lock append connect getopt setopt shutdown nlmsg
};
allowxperm netdomain self:netlink_route_socket nlmsg ~{
RTM_GETLINK RTM_GETNEIGH RTM_GETNEIGHTBL
};
allowxperm priv_app self:netlink_route_socket nlmsg {
RTM_GETLINK RTM_GETNEIGH RTM_GETNEIGHTBL
};

The constants in the example above (e.g., RTM_GETLINK) are explicitly defined in the policy.

It is possible to generate policies to support kernels that may or may not have the capability enabled by generating a rule for each scenario. For instance:

allow domain self:netlink_audit_socket nlmsg_read; allow domain self:netlink_audit_socket nlmsg; allowxperm domain self:netlink_audit_socket nlmsg { AUDIT_GET };

The approach of defining a new permission ("nlmsg") instead of relying on the existing permissions (e.g., "nlmsg_read", "nlmsg_readpriv" or "nlmsg_tty_audit") has been preferred because:
1. This is similar to the other extended permission ("ioctl");
2. With the new extended permission, the coarse-grained mapping is not necessary anymore. It could eventually be removed, which would be impossible if the extended permission was defined below these.
3. Having a single extra extended permission considerably simplifies the implementation here and in libselinux.

---
v3:
- Remove condition on SECCLASS_NETLINK_GENERIC_SOCKET in selinux_netlink_send.
- Remove comment in selinux_netlink_send.
- Add comment in selinux_nlmsg_lookup.
- Update commit message.
v2: Reorder classmap.h to keep the new permission "nlmsg" at the end.

Quoted 9 lines
 security/selinux/hooks.c                   | 51 +++++++++++---
 security/selinux/include/classmap.h        |  8 +--
 security/selinux/include/policycap.h       |  1 +
 security/selinux/include/policycap_names.h |  1 +
 security/selinux/include/security.h        |  6 ++
 security/selinux/nlmsgtab.c                | 27 ++++++++
 security/selinux/ss/avtab.h                |  5 +-
 security/selinux/ss/services.c             | 78 ++++++++++++----------
 8 files changed, 126 insertions(+), 51 deletions(-)
Quoted 3 lines

Looks good to me, thanks for the revision. We're in the merge window right now so I'm going to merge this into selinux/dev-staging now and I'll move it into selinux/dev after -rc1 is released.

Thanks for your patience, I just merged this into selinux/dev, you should see it upstream shortly.

Signature · 1 line
paul-moore.com