linux-security-module.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: casey@schaufler-ca.com (Casey Schaufler)
To: linux-security-module@vger.kernel.org
Subject: [PATCH v1 20/22] Move common usercopy into security_getpeersec_stream
Date: Mon, 16 Jul 2018 11:24:42 -0700	[thread overview]
Message-ID: <ac4a4408-5c69-4b87-b57d-537dba588b18@schaufler-ca.com> (raw)
In-Reply-To: <8a325db8-e7eb-9581-2b77-fc987a165df7@schaufler-ca.com>

[PATCH 20/22] Move common usercopy into security_getpeersec_stream

The modules implementing hook for getpeersec_stream
don't need to be duplicating the copy-to-user checks.
Moving the user copy part into the infrastructure makes
the security module code simpler and reduces the places
where user copy code may go awry.

Signed-off-by: Casey Schaufler <casey@schaufler-ca.com>
---
 include/linux/lsm_hooks.h  | 10 ++++------
 include/linux/security.h   |  6 ++++--
 security/apparmor/lsm.c    | 28 ++++++++++------------------
 security/security.c        | 17 +++++++++++++++--
 security/selinux/hooks.c   | 22 +++++++---------------
 security/smack/smack_lsm.c | 19 ++++++++-----------
 6 files changed, 48 insertions(+), 54 deletions(-)

diff --git a/include/linux/lsm_hooks.h b/include/linux/lsm_hooks.h
index 7c321d11d994..8d247e7ce2fb 100644
--- a/include/linux/lsm_hooks.h
+++ b/include/linux/lsm_hooks.h
@@ -846,9 +846,8 @@
  *	SO_GETPEERSEC.  For tcp sockets this can be meaningful if the
  *	socket is associated with an ipsec SA.
  *	@sock is the local socket.
- *	@optval userspace memory where the security state is to be copied.
- *	@optlen userspace int where the module should copy the actual length
- *	of the security state.
+ *	@optval the security state.
+ *	@optlen the actual length of the security state.
  *	@len as input is the maximum length to copy to userspace provided
  *	by the caller.
  *	Return 0 if all is well, otherwise, typical getsockopt return
@@ -1680,9 +1679,8 @@ union security_list_options {
 	int (*socket_setsockopt)(struct socket *sock, int level, int optname);
 	int (*socket_shutdown)(struct socket *sock, int how);
 	int (*socket_sock_rcv_skb)(struct sock *sk, struct sk_buff *skb);
-	int (*socket_getpeersec_stream)(struct socket *sock,
-					char __user *optval,
-					int __user *optlen, unsigned int len);
+	int (*socket_getpeersec_stream)(struct socket *sock, char **optval,
+					int *optlen, unsigned int len);
 	int (*socket_getpeersec_dgram)(struct socket *sock,
 					struct sk_buff *skb,
 					struct secids *secid);
diff --git a/include/linux/security.h b/include/linux/security.h
index 9095f63c65a9..7d3300d34f25 100644
--- a/include/linux/security.h
+++ b/include/linux/security.h
@@ -1377,8 +1377,10 @@ static inline int security_sock_rcv_skb(struct sock *sk,
 	return 0;
 }
 
-static inline int security_socket_getpeersec_stream(struct socket *sock, char __user *optval,
-						    int __user *optlen, unsigned len)
+static inline int security_socket_getpeersec_stream(struct socket *sock,
+						    char __user *optval,
+						    int __user *optlen,
+						    unsigned int len)
 {
 	return -ENOPROTOOPT;
 }
diff --git a/security/apparmor/lsm.c b/security/apparmor/lsm.c
index b0200481c811..7a2a8d0efa09 100644
--- a/security/apparmor/lsm.c
+++ b/security/apparmor/lsm.c
@@ -1026,10 +1026,8 @@ static struct aa_label *sk_peer_label(struct sock *sk)
  *
  * Note: for tcp only valid if using ipsec or cipso on lan
  */
-static int apparmor_socket_getpeersec_stream(struct socket *sock,
-					     char __user *optval,
-					     int __user *optlen,
-					     unsigned int len)
+static int apparmor_socket_getpeersec_stream(struct socket *sock, char **optval,
+					     int *optlen, unsigned int len)
 {
 	char *name;
 	int slen, error = 0;
@@ -1046,22 +1044,16 @@ static int apparmor_socket_getpeersec_stream(struct socket *sock,
 				 FLAG_SHOW_MODE | FLAG_VIEW_SUBNS |
 				 FLAG_HIDDEN_UNCONFINED, GFP_KERNEL);
 	/* don't include terminating \0 in slen, it breaks some apps */
-	if (slen < 0) {
+	if (slen < 0)
 		error = -ENOMEM;
-	} else {
-		if (slen > len) {
-			error = -ERANGE;
-		} else if (copy_to_user(optval, name, slen)) {
-			error = -EFAULT;
-			goto out;
-		}
-		if (put_user(slen, optlen))
-			error = -EFAULT;
-out:
-		kfree(name);
-
+	else if (slen > len)
+		error = -ERANGE;
+	else {
+		*optlen = slen;
+		*optval = name;
 	}
-
+	if (error)
+		kfree(name);
 done:
 	end_current_label_crit_section(label);
 
diff --git a/security/security.c b/security/security.c
index 90e741db0a42..521afa12293e 100644
--- a/security/security.c
+++ b/security/security.c
@@ -1930,8 +1930,21 @@ EXPORT_SYMBOL(security_sock_rcv_skb);
 int security_socket_getpeersec_stream(struct socket *sock, char __user *optval,
 				      int __user *optlen, unsigned len)
 {
-	return call_int_hook(socket_getpeersec_stream, -ENOPROTOOPT, sock,
-				optval, optlen, len);
+	char *tval = NULL;
+	u32 tlen;
+	int rc;
+
+	rc = call_int_hook(socket_getpeersec_stream, -ENOPROTOOPT, sock,
+			   &tval, &tlen, len);
+	if (rc == 0) {
+		tlen = strlen(tval) + 1;
+		if (put_user(tlen, optlen))
+			rc = -EFAULT;
+		else if (copy_to_user(optval, tval, tlen))
+			rc = -EFAULT;
+		kfree(tval);
+	}
+	return rc;
 }
 
 int security_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb,
diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
index c12c36f72258..6614d46feac4 100644
--- a/security/selinux/hooks.c
+++ b/security/selinux/hooks.c
@@ -4986,10 +4986,8 @@ static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
 	return err;
 }
 
-static int selinux_socket_getpeersec_stream(struct socket *sock,
-					    __user char *optval,
-					    __user int *optlen,
-					    unsigned int len)
+static int selinux_socket_getpeersec_stream(struct socket *sock, char **optval,
+					    int *optlen, unsigned int len)
 {
 	int err = 0;
 	char *scontext;
@@ -5010,18 +5008,12 @@ static int selinux_socket_getpeersec_stream(struct socket *sock,
 		return err;
 
 	if (scontext_len > len) {
-		err = -ERANGE;
-		goto out_len;
+		kfree(scontext);
+		return -ERANGE;
 	}
-
-	if (copy_to_user(optval, scontext, scontext_len))
-		err = -EFAULT;
-
-out_len:
-	if (put_user(scontext_len, optlen))
-		err = -EFAULT;
-	kfree(scontext);
-	return err;
+	*optval = scontext;
+	*optlen = scontext_len;
+	return 0;
 }
 
 static int selinux_socket_getpeersec_dgram(struct socket *sock,
diff --git a/security/smack/smack_lsm.c b/security/smack/smack_lsm.c
index 157c6a731305..d4552b2286bc 100644
--- a/security/smack/smack_lsm.c
+++ b/security/smack/smack_lsm.c
@@ -3895,14 +3895,12 @@ static int smack_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
  *
  * returns zero on success, an error code otherwise
  */
-static int smack_socket_getpeersec_stream(struct socket *sock,
-					  char __user *optval,
-					  int __user *optlen, unsigned len)
+static int smack_socket_getpeersec_stream(struct socket *sock, char **optval,
+					  int *optlen, unsigned int len)
 {
 	struct socket_smack *ssp;
 	char *rcp = "";
 	int slen = 1;
-	int rc = 0;
 
 	ssp = smack_sock(sock->sk);
 	if (ssp->smk_packet != NULL) {
@@ -3911,14 +3909,13 @@ static int smack_socket_getpeersec_stream(struct socket *sock,
 	}
 
 	if (slen > len)
-		rc = -ERANGE;
-	else if (copy_to_user(optval, rcp, slen) != 0)
-		rc = -EFAULT;
-
-	if (put_user(slen, optlen) != 0)
-		rc = -EFAULT;
+		return -ERANGE;
 
-	return rc;
+	*optval = kstrdup(rcp, GFP_ATOMIC);
+	if (*optval == NULL)
+		return -ENOMEM;
+	*optlen = slen;
+	return 0;
 }
 
 
-- 
2.17.1


--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
the body of a message to majordomo at vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

  parent reply	other threads:[~2018-07-16 18:24 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-07-16 17:53 [PATCH v1 00/22] LSM: Full security module stacking Casey Schaufler
2018-07-16 18:22 ` [PATCH v1 01/22] procfs: add smack subdir to attrs Casey Schaufler
2018-07-16 18:22 ` [PATCH v1 02/22] Smack: Abstract use of cred security blob Casey Schaufler
2018-07-16 18:22 ` [PATCH v1 03/22] SELinux: " Casey Schaufler
2018-07-16 18:22 ` [PATCH v1 04/22] LSM: Infrastructure management of the " Casey Schaufler
2018-07-16 18:22 ` [PATCH v1 05/22] SELinux: Abstract use of file " Casey Schaufler
2018-07-16 18:22 ` [PATCH v1 06/22] LSM: Infrastructure management of the " Casey Schaufler
2018-07-16 18:23 ` [PATCH v1 07/22] LSM: Infrastructure management of the task " Casey Schaufler
2018-07-16 18:23 ` PATCH v1 08/22] SELinux: Abstract use of inode " Casey Schaufler
2018-07-16 18:23 ` [PATCH v1 09/22] Smack: " Casey Schaufler
2018-07-16 18:23 ` [PATCH v1 10/22] LSM: Infrastructure management of the inode security Casey Schaufler
2018-07-16 18:23 ` [PATCH v1 11/22] LSM: Infrastructure management of the superblock security blob Casey Schaufler
2018-07-16 18:23 ` [PATCH v1 12/22] LSM: Infrastructure management of the sock security Casey Schaufler
2018-07-16 18:23 ` [PATCH v1 13/22] LSM: Infrastructure management of the ipc security blob Casey Schaufler
2018-07-16 18:23 ` [PATCH v1 14/22] LSM: Infrastructure management of the key " Casey Schaufler
2018-07-16 18:24 ` [PATCH v1 15/22] LSM: Mark security blob allocation failures as unlikely Casey Schaufler
2018-07-16 18:24 ` [PATCH v1 16/22] LSM: Sharing of security blobs Casey Schaufler
2018-07-16 18:24 ` [PATCH v1 17/22] LSM: Allow mount options from multiple security modules Casey Schaufler
2018-07-16 18:24 ` [PATCH v1 18/22] LSM: Use multiple secids in security module interfaces Casey Schaufler
2018-07-16 18:24 ` [PATCH v1 19/22] LSM: Use multiple secids in LSM interfaces Casey Schaufler
2018-07-16 18:24 ` Casey Schaufler [this message]
     [not found]   ` <CGME20180803091011eucas1p29e46a12d1986f11e63547ea1ec8e8663@eucas1p2.samsung.com>
2018-08-03  9:10     ` [PATCH v1 20/22] Move common usercopy into security_getpeersec_stream Piotr Sawicki
2018-08-03 15:27       ` Casey Schaufler
2018-07-16 18:24 ` [PATCH v1 21/22] LSM: Multiple concurrent major security modules Casey Schaufler
     [not found]   ` <CGME20181009140944eucas1p1b935c2b8b2534cb15e36d28b7f9b134b@eucas1p1.samsung.com>
2018-10-09 14:09     ` Piotr Sawicki
2018-07-16 18:24 ` [PATCH v1 22/22] Netfilter: Add a selection for Smack Casey Schaufler
2018-07-16 18:28 ` [PATCH v1 00/22] LSM: Full security module stacking Casey Schaufler
2018-07-16 18:53 ` James Morris
2018-07-16 19:49   ` Casey Schaufler
2018-08-14 17:05 ` Sargun Dhillon
2018-08-14 18:28   ` Casey Schaufler
2018-08-14 23:22     ` Jordan Glover
2018-08-14 23:50       ` Casey Schaufler
2018-08-15  5:19         ` Kees Cook
2018-08-16 16:05           ` Salvatore Mesoraca
2018-08-22  9:59         ` James Morris

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ac4a4408-5c69-4b87-b57d-537dba588b18@schaufler-ca.com \
    --to=casey@schaufler-ca.com \
    --cc=linux-security-module@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).