Received: by 2002:ac0:a594:0:0:0:0:0 with SMTP id m20-v6csp4636392imm; Mon, 14 May 2018 10:19:19 -0700 (PDT) X-Google-Smtp-Source: AB8JxZqJ4qKZCkDikBTA6pmSOpovvvAcha7vcCQa+rRx4uC8/iOBg4Gzx+ndtaqIny2oybyX+rXi X-Received: by 2002:a17:902:b946:: with SMTP id h6-v6mr11147081pls.3.1526318359272; Mon, 14 May 2018 10:19:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1526318359; cv=none; d=google.com; s=arc-20160816; b=DydYC+ZqPYwdPfcYrX1HVRgvmyBa8eZLWDJxL1O1x+Xibnf59X7kXV4Js1OrvZ+O8i zaIMwaYHIwP4K+4I5Wonp7anDyL58Rhb0fdgCj9aYTHTiKYLXSWoB10eZJt5kL12BrDH n/uqROYqw1rg039g0nOsQ5W5gWOYo4N94Xu+8EpcDHm27DV3Oo5osctM/tU95RUve3kx /R0D3707AZl3fhiVp85+VdkyntEyc5lITauDTXfl8/rqGWbUbQSCCN9TekOoCjmk7rbY xUiDhb87PD4Izfao0ttyI41SIT2Dq1bCrD2Rc/9XKHBnmRpow6ko13bRBL2InGI5NzHt RHhg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:to:subject:ironport-phdr :arc-authentication-results; bh=VHuJ7nwejl+x7Up4EYTcb53eRIYC7tr3ggBkS/Ddphg=; b=ZNKCKhSz7LxrDkMJ1rse7F9LBsNriHelGYwC4hoI+HWdPFmSgeamzSB0czTYqyPKrL SQ1cYpVFnXGttSDKdbgnWNg7Q2prCpcss55aFXBrPEa45yw2OaLsR6wqFFbIvbxA4OgM uqbB/T47Tp4PwNOb2GmbHkiBbpcjJKV6SeY2zAQN3RjbvY/cPsRo29Ast7MSvCG3r4M8 iAmmUf9k256b8bprD65CHnPKUudCnc9heOlzom92mKPUCkWvOhvZFUNupKw7BYJnzXvv oZaXGuE8KAVYoKxMgj3WXC5kL/z3M/G6/0Cnk0ljw7azCKxvt/FJ96cv+LJXzPHVF63R Wuow== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v12-v6si4927264pgs.538.2018.05.14.10.19.04; Mon, 14 May 2018 10:19:19 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932725AbeENPXZ (ORCPT + 99 others); Mon, 14 May 2018 11:23:25 -0400 Received: from uphb19pa09.eemsg.mail.mil ([214.24.26.83]:1510 "EHLO USFB19PA12.eemsg.mail.mil" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S932486AbeENPXU (ORCPT ); Mon, 14 May 2018 11:23:20 -0400 X-Greylist: delayed 718 seconds by postgrey-1.27 at vger.kernel.org; Mon, 14 May 2018 11:23:19 EDT Received: from emsm-gh1-uea10.ncsc.mil ([214.29.60.2]) by USFB19PA12.eemsg.mail.mil with ESMTP/TLS/AES256-SHA; 14 May 2018 15:11:01 +0000 X-IronPort-AV: E=Sophos;i="5.49,400,1520899200"; d="scan'208";a="11754731" IronPort-PHdr: =?us-ascii?q?9a23=3A4e8cjhchiQEwIvxvHGex/rBhlGMj4u6mDksu8p?= =?us-ascii?q?Mizoh2WeGdxc29ZR2N2/xhgRfzUJnB7Loc0qyK6/umATRIyK3CmUhKSIZLWR?= =?us-ascii?q?4BhJdetC0bK+nBN3fGKuX3ZTcxBsVIWQwt1Xi6NU9IBJS2PAWK8TW94jEIBx?= =?us-ascii?q?rwKxd+KPjrFY7OlcS30P2594HObwlSizexfb1/IA+qoQnNq8IbnZZsJqEtxx?= =?us-ascii?q?XTv3BGYf5WxWRmJVKSmxbz+MK994N9/ipTpvws6ddOXb31cKokQ7NYCi8mM3?= =?us-ascii?q?0u683wqRbDVwqP6WACXWgQjxFFHhLK7BD+Xpf2ryv6qu9w0zSUMMHqUbw5Xy?= =?us-ascii?q?mp4rx1QxH0ligIKz858HnWisNuiqJbvAmhrAF7z4LNfY2ZKOZycqbbcNwUX2?= =?us-ascii?q?pBWttaWTJHDI2ycoADC/MNMfhEo4X4oVYFsBmwChS2BO731zFGmHH206053e?= =?us-ascii?q?ovHw7J0w4vEM4BvnnPsNX4Nr0fXfypwKTGzzjOae5d1zfn6IjPdxAsueyCXa?= =?us-ascii?q?5ufsrJyUkgCQXFhUiNp4zgJTyV0uANvHab7uF9Uu+vkHMoqxpqrzizxsYjlo?= =?us-ascii?q?nJhoUPxlDC7iV22pw5JdK/SE5leNOpFoZbuSKCN4ZuX88vTG5ltDw6x7Ebo5?= =?us-ascii?q?K3YicHxIo9yxLCbfGMbpKG7Qj5VOmLJDd1nHdleLWiiBms6UWg0ej8VtWs0F?= =?us-ascii?q?ZNsypFjsHAtnAT2BzX7ciKUud98V272TaOygDT8ftIIVw0lKXHK54hxaQ8lp?= =?us-ascii?q?wPvkTYAiD6gkD2jK6Sdkk8++io7froYqn+q5OBOIJ5hRvyP6QzlsClH+g1PR?= =?us-ascii?q?YCU3KG9eik0b3s50z5QLFEjv0slanZtYjXJd8Gqa6iGAJVzoYi5Aq/Dzehyt?= =?us-ascii?q?gYm2IHI0hfdBKIiIjpJUnCIOrkAvenn1SsjDBryujaMbL7GZXCMHjCnaz6fb?= =?us-ascii?q?lh605T0hczzd5b551KELENOe78VVXruNPECR85NhS+w/z7B9VlyoMeRWWPD7?= =?us-ascii?q?edMKPTt1+I++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=?us-ascii?q?laZY8WDSKrJMqty5N1gJu7HzZj2RaHBlUbyIf9YheWblrgzSVMxE8Xpjqhgi?= =?us-ascii?q?L+wDtqxXVh5JG69QfthujjbxEaIXVjQGh5kUyqedHyiMoVFgD8VC0Avzjg6U?= =?us-ascii?q?fhzLVAv4x7LnLPWgEQJm7xNW40FuOSsbqEK+xI6JouqihRGLC+ZlCcDLz6ox?= =?us-ascii?q?IcyC7lN2BXwjc9djqjvtPymBkszCq+IXto5FHefsI4kRTS6cfXQvlS9jEGXi?= =?us-ascii?q?59iCTSQF+mMI/ttemZi4zetaieXmStX9UHaSTtwp6Bsm646HdsDBmXnrW3nc?= =?us-ascii?q?PqVxMz0jLh3p9sWGPKtEC4Kqvi0qmhecdgZFNpHxeo6c99AJt/iaM2jZQd2D?= =?us-ascii?q?4dnJrDrlQdlmKmCslWwaLzajI2QDcPx9PEqFz+1FZLMmOCx4W/UG6UhMRmeY?= =?us-ascii?q?/pMSstxish4pUSW++v57tekH4w+wDgoA=3D=3D?= X-IPAS-Result: =?us-ascii?q?A2C6BQDLpfla/wHyM5BcGwEBAQEDAQEBCQEBAYMYK4FcK?= =?us-ascii?q?INylHJFAQEBAQEBBn8IIXUalSo2AYRAAoMRITgUAQIBAQEBAQECAWsogjUkA?= =?us-ascii?q?YJOAQEBAQMjBGILEQMBAgECAiYCAk8IBgEMBgIBAYJfQIF0DaplgWkzg3gBA?= =?us-ascii?q?V6DZYIngQmHHIEMgQeBDyMMgic1h3OCVAKHOIRgjB4JjksGjG+SCDMhgVIrC?= =?us-ascii?q?AIYCCEPgn6QGAFRIzB6AQGPbQEB?= Received: from tarius.tycho.ncsc.mil ([144.51.242.1]) by EMSM-GH1-UEA10.NCSC.MIL with ESMTP; 14 May 2018 15:11:00 +0000 Received: from moss-pluto.infosec.tycho.ncsc.mil (moss-pluto [192.168.25.131]) by tarius.tycho.ncsc.mil (8.14.4/8.14.4) with ESMTP id w4EFAxGU013791; Mon, 14 May 2018 11:10:59 -0400 Subject: Re: [PATCH 20/23] LSM: Move common usercopy into To: Casey Schaufler , LSM , LKLM , Paul Moore , SE Linux , "SMACK-discuss@lists.01.org" , John Johansen , Kees Cook , Tetsuo Handa , James Morris References: <7e8702ce-2598-e0a3-31a2-bc29157fb73d@schaufler-ca.com> From: Stephen Smalley Message-ID: Date: Mon, 14 May 2018 11:12:00 -0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.7.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 05/10/2018 08:55 PM, Casey Schaufler wrote: > From: Casey Schaufler > Date: Thu, 10 May 2018 15:54:25 -0700 > Subject: [PATCH 20/23] LSM: 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 > --- > 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 81504623afb4..84bc9ec01931 100644 > --- a/include/linux/lsm_hooks.h > +++ b/include/linux/lsm_hooks.h > @@ -841,9 +841,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 > @@ -1674,9 +1673,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 ab70064c283f..712d138e0148 100644 > --- a/include/linux/security.h > +++ b/include/linux/security.h > @@ -1369,8 +1369,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 90453dbb4fac..7444cfa689b3 100644 > --- a/security/apparmor/lsm.c > +++ b/security/apparmor/lsm.c > @@ -1017,10 +1017,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; > @@ -1037,22 +1035,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 cbe1a497ec5a..6144ff52d862 100644 > --- a/security/security.c > +++ b/security/security.c > @@ -1924,8 +1924,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; Why are you recomputing tlen here from what the module provided, and further presuming it must be nul-terminated? > + 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 81f104d9e85e..9520341daa78 100644 > --- a/security/selinux/hooks.c > +++ b/security/selinux/hooks.c > @@ -4955,10 +4955,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; > @@ -4979,18 +4977,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, struct sk_buff *skb, struct secids *secid) > diff --git a/security/smack/smack_lsm.c b/security/smack/smack_lsm.c > index 660a55ee8a57..12b00aac0c94 100644 > --- a/security/smack/smack_lsm.c > +++ b/security/smack/smack_lsm.c > @@ -3878,14 +3878,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) { > @@ -3894,14 +3892,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; > } > > >