Skip to contentSkip to conversation

selinux: cleanup the security_node_sid() function

RFC PATCH New Paul Moore 1 participant 2 messages Jul 3, 2012 SELinux security module

lore
Share & tools
lore

Paul Moore

author
#0 lore raw

RFC PATCH v2 1/2 selinux: cleanup the security_node_sid() function

Remove some unnecessary arguments and simplify the function as a result.

---
 security/selinux/include/security.h |    3 +--
 security/selinux/netnode.c          |    6 ++----
 security/selinux/ss/services.c      |   28 ++++++----------------------
 3 files changed, 9 insertions(+), 28 deletions(-)
security/selinux/include/security.h +1 -2
index dde2005..81c5838 100644
@@ -144,8 +144,7 @@ int security_port_sid(u8 protocol, u16 port, u32 *out_sid);
 
 int security_netif_sid(char *name, u32 *if_sid);
 
-int security_node_sid(u16 domain, void *addr, u32 addrlen,
-	u32 *out_sid);
+int security_node_sid(u16 family, void *addr, u32 *out_sid);
 
 int security_validate_transition(u32 oldsid, u32 newsid, u32 tasksid,
 				 u16 tclass);
security/selinux/netnode.c +2 -4
index 28f911c..8d5a490 100644
@@ -213,13 +213,11 @@ static int sel_netnode_sid_slow(void *addr, u16 family, u32 *sid)
 		goto out;
 	switch (family) {
 	case PF_INET:
-		ret = security_node_sid(PF_INET,
-					addr, sizeof(struct in_addr), sid);
+		ret = security_node_sid(PF_INET, addr, sid);
 		new->nsec.addr.ipv4 = *(__be32 *)addr;
 		break;
 	case PF_INET6:
-		ret = security_node_sid(PF_INET6,
-					addr, sizeof(struct in6_addr), sid);
+		ret = security_node_sid(PF_INET6, addr, sid);
 		new->nsec.addr.ipv6 = *(struct in6_addr *)addr;
 		break;
 	default:
security/selinux/ss/services.c +6 -22
index 4321b8f..e8fd07d 100644
@@ -2070,30 +2070,20 @@ static int match_ipv6_addrmask(u32 *input, u32 *addr, u32 *mask)
 
 /**
  * security_node_sid - Obtain the SID for a node (host).
- * @domain: communication domain aka address family
+ * @family: address family
  * @addrp: address
- * @addrlen: address length in bytes
  * @out_sid: security identifier
  */
-int security_node_sid(u16 domain,
-		      void *addrp,
-		      u32 addrlen,
-		      u32 *out_sid)
+int security_node_sid(u16 family, void *addrp, u32 *out_sid)
 {
-	int rc;
+	int rc = 0;
 	struct ocontext *c;
 
 	read_lock(&policy_rwlock);
 
-	switch (domain) {
+	switch (family) {
 	case AF_INET: {
-		u32 addr;
-
-		rc = -EINVAL;
-		if (addrlen != sizeof(u32))
-			goto out;
-
-		addr = *((u32 *)addrp);
+		u32 addr = *((u32 *)addrp);
 
 		c = policydb.ocontexts[OCON_NODE];
 		while (c) {
@@ -2105,9 +2095,6 @@ int security_node_sid(u16 domain,
 	}
 
 	case AF_INET6:
-		rc = -EINVAL;
-		if (addrlen != sizeof(u64) * 2)
-			goto out;
 		c = policydb.ocontexts[OCON_NODE6];
 		while (c) {
 			if (match_ipv6_addrmask(addrp, c->u.node6.addr,
@@ -2118,7 +2105,6 @@ int security_node_sid(u16 domain,
 		break;
 
 	default:
-		rc = 0;
 		*out_sid = SECINITSID_NODE;
 		goto out;
 	}
@@ -2132,11 +2118,9 @@ int security_node_sid(u16 domain,
 				goto out;
 		}
 		*out_sid = c->sid[0];
-	} else {
+	} else
 		*out_sid = SECINITSID_NODE;
-	}
 
-	rc = 0;
 out:
 	read_unlock(&policy_rwlock);
 	return rc;

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.

author
#1 lore raw

RFC PATCH v2 2/2 selinux: redefine a port definition to include the IP address

[NOTE: this patch is very much a RFC patch intended to solicit
feedback on the approach; it will compile cleanly but I make no claims about it actually producing a working kernel]

The current SELinux port definition does not include an IP address which makes it difficult to restrict connect() and bind() operations using SELinux. As an example, how would you allow a process to bind() to 127.0.0.1:80 and not 0.0.0.0:80?

This patch fixes this by adding an IP address to the SELinux port definition via a SELinux node label. Not only does this allow us to solve problems like the example above, it also brings the SELinux port definition in line with the actual usage of network ports; ports are always defined by the 3-tuple of address, protocol, and port.

It is important to note that this patch does bump the policy version number due to changes in how the network ports are defined in policy. Existing policies should continue to work as they do now, with the network port access controls effectively ignoring the associated IP addresses. An updated SELinux userspace will be needed to take advantage of this new functionality.

--
CHANGELOG:
* v2
- Included missing policydb_compat entry
- Use the full node context since we can't use portcon statements in a module anyway
- Eric prefers long lines (>80 char) over split lines so unsplit long lines
- Subscribed to the SELinux list from this email so this patch actually gets posted
* v1
- Initial version

---
 security/selinux/hooks.c            |   11 ++-
 security/selinux/include/netport.h  |    2 -
 security/selinux/include/objsec.h   |    9 ++-
 security/selinux/include/security.h |    6 +-
 security/selinux/netport.c          |   49 +++++++++++---
 security/selinux/ss/policydb.c      |   26 ++++++-
 security/selinux/ss/policydb.h      |    2 +
 security/selinux/ss/services.c      |  126 +++++++++++++++++++----------------
 8 files changed, 151 insertions(+), 80 deletions(-)
security/selinux/hooks.c +8 -3
index 372ec65..746535d 100644
@@ -3869,8 +3869,9 @@ static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, in
 			inet_get_local_port_range(&low, &high);
 
 			if (snum < max(PROT_SOCK, low) || snum > high) {
-				err = sel_netport_sid(sk->sk_protocol,
-						      snum, &sid);
+				err = sel_netport_sid(sk->sk_family, addrp,
+						      sk->sk_protocol, snum,
+						      &sid);
 				if (err)
 					goto out;
 				ad.type = LSM_AUDIT_DATA_NET;
@@ -3945,6 +3946,7 @@ static int selinux_socket_connect(struct socket *sock, struct sockaddr *address,
 		struct lsm_network_audit net = {0,};
 		struct sockaddr_in *addr4 = NULL;
 		struct sockaddr_in6 *addr6 = NULL;
+		char *addrp;
 		unsigned short snum;
 		u32 sid, perm;
 
@@ -3952,15 +3954,18 @@ static int selinux_socket_connect(struct socket *sock, struct sockaddr *address,
 			addr4 = (struct sockaddr_in *)address;
 			if (addrlen < sizeof(struct sockaddr_in))
 				return -EINVAL;
+			addrp = (char *)&addr4->sin_addr.s_addr;
 			snum = ntohs(addr4->sin_port);
 		} else {
 			addr6 = (struct sockaddr_in6 *)address;
 			if (addrlen < SIN6_LEN_RFC2133)
 				return -EINVAL;
+			addrp = (char *)&addr6->sin6_addr.s6_addr;
 			snum = ntohs(addr6->sin6_port);
 		}
 
-		err = sel_netport_sid(sk->sk_protocol, snum, &sid);
+		err = sel_netport_sid(sk->sk_family, addrp,
+				      sk->sk_protocol, snum, &sid);
 		if (err)
 			goto out;
 
security/selinux/include/netport.h +1 -1
index 4d965b8..26c55b1 100644
@@ -26,6 +26,6 @@
 #ifndef _SELINUX_NETPORT_H
 #define _SELINUX_NETPORT_H
 
-int sel_netport_sid(u8 protocol, u16 pnum, u32 *sid);
+int sel_netport_sid(u16 family, void *addrp, u8 protocol, u16 pnum, u32 *sid);
 
 #endif
security/selinux/include/objsec.h +7 -2
index 26c7eee..6ca1484 100644
@@ -89,9 +89,14 @@ struct netnode_security_struct {
 };
 
 struct netport_security_struct {
-	u32 sid;			/* SID for this node */
-	u16 port;			/* port number */
+	u16 family;
+	union {
+		__be32 ipv4;		/* IPv4 node address */
+		struct in6_addr ipv6;	/* IPv6 node address */
+	} addr;
 	u8 protocol;			/* transport protocol */
+	u16 port;			/* port number */
+	u32 sid;			/* SID for this port */
 };
 
 struct sk_security_struct {
security/selinux/include/security.h +4 -2
index 81c5838..57e7996 100644
@@ -33,13 +33,14 @@
 #define POLICYDB_VERSION_ROLETRANS	26
 #define POLICYDB_VERSION_NEW_OBJECT_DEFAULTS	27
 #define POLICYDB_VERSION_DEFAULT_TYPE	28
+#define POLICYDB_VERSION_PORTADDR	29
 
 /* Range of policy versions we understand*/
 #define POLICYDB_VERSION_MIN   POLICYDB_VERSION_BASE
 #ifdef CONFIG_SECURITY_SELINUX_POLICYDB_VERSION_MAX
 #define POLICYDB_VERSION_MAX	CONFIG_SECURITY_SELINUX_POLICYDB_VERSION_MAX_VALUE
 #else
-#define POLICYDB_VERSION_MAX	POLICYDB_VERSION_DEFAULT_TYPE
+#define POLICYDB_VERSION_MAX	POLICYDB_VERSION_PORTADDR
 #endif
 
 /* Mask for just the mount related flags */
@@ -140,7 +141,8 @@ int security_context_to_sid_force(const char *scontext, u32 scontext_len,
 int security_get_user_sids(u32 callsid, char *username,
 			   u32 **sids, u32 *nel);
 
-int security_port_sid(u8 protocol, u16 port, u32 *out_sid);
+int security_port_sid(u16 family, void *addrp, u8 protocol, u16 port,
+		      u32 *out_sid);
 
 int security_netif_sid(char *name, u32 *if_sid);
 
security/selinux/netport.c +39 -10

117 lines not on this page · raw message

security/selinux/ss/policydb.c +20 -6
index 9cd9b7c..b8673fc 100644
@@ -143,6 +143,11 @@ static struct policydb_compat_info policydb_compat[] = {
 		.sym_num	= SYM_NUM,
 		.ocon_num	= OCON_NUM,
 	},
+	{
+		.version	= POLICYDB_VERSION_PORTADDR,
+		.sym_num	= SYM_NUM,
+		.ocon_num	= OCON_NUM,
+	},
 };
 
 static struct policydb_compat_info *policydb_lookup_compat(int version)
@@ -2081,7 +2086,7 @@ out:
 static int ocontext_read(struct policydb *p, struct policydb_compat_info *info,
 			 void *fp)
 {
-	int i, j, rc;
+	int i, j, k, rc;
 	u32 nel, len;
 	__le32 buf[3];
 	struct ocontext *l, *c;
@@ -2147,7 +2152,14 @@ static int ocontext_read(struct policydb *p, struct policydb_compat_info *info,
 				c->u.port.protocol = le32_to_cpu(buf[0]);
 				c->u.port.low_port = le32_to_cpu(buf[1]);
 				c->u.port.high_port = le32_to_cpu(buf[2]);
-				rc = context_read_and_validate(&c->context[0], p, fp);
+				if (p->policyvers >= POLICYDB_VERSION_PORTADDR) {
+					c->u.port.node_present = 1;
+					rc = context_read_and_validate(&c->u.port.node_ctx, p, fp);
+					if (rc)
+						goto out;
+				}
+				rc = context_read_and_validate(&c->context[0],
+							       p, fp);
 				if (rc)
 					goto out;
 				break;
@@ -2185,9 +2197,7 @@ static int ocontext_read(struct policydb *p, struct policydb_compat_info *info,
 				if (rc)
 					goto out;
 				break;
-			case OCON_NODE6: {
-				int k;
-
+			case OCON_NODE6:
 				rc = next_entry(nodebuf, fp, sizeof(u32) * 8);
 				if (rc)
 					goto out;
@@ -2200,7 +2210,6 @@ static int ocontext_read(struct policydb *p, struct policydb_compat_info *info,
 					goto out;
 				break;
 			}
-			}
 		}
 	}
 	rc = 0;
@@ -3064,6 +3073,11 @@ static int ocontext_write(struct policydb *p, struct policydb_compat_info *info,
 				rc = put_entry(buf, sizeof(u32), 3, fp);
 				if (rc)
 					return rc;
+				if (p->policyvers >= POLICYDB_VERSION_PORTADDR) {
+					rc = context_write(p, &c->u.port.node_ctx, fp);
+					if (rc)
+						return rc;
+				}
 				rc = context_write(p, &c->context[0], fp);
 				if (rc)
 					return rc;
security/selinux/ss/policydb.h +2 -0
index da63747..f6db32e 100644
@@ -164,9 +164,11 @@ struct ocontext {
 	union {
 		char *name;	/* name of initial SID, fs, netif, fstype, path */
 		struct {
+			u8 node_present;
 			u8 protocol;
 			u16 low_port;
 			u16 high_port;
+			struct context node_ctx;
 		} port;		/* TCP or UDP port information */
 		struct {
 			u32 addr;
security/selinux/ss/services.c +70 -56

167 lines not on this page · raw message

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.