Subject: [PATCH 4.17 11/18] netlink: Dont shift on 64 for ngroups

From: Greg Kroah-Hartman <[email-masked]>
Date: Tue, 7 Aug 2018 20:51:14 +0200

4.17-stable review patch.  If anyone has any objections, please let me know.

------------------

From: Dmitry Safonov <[email-masked]>

commit 91874ecf32e41b5d86a4cb9d60e0bee50d828058 upstream.

It's legal to have 64 groups for netlink_sock.

As user-supplied nladdr->nl_groups is __u32, it's possible to subscribe
only to first 32 groups.

The check for correctness of .bind() userspace supplied parameter
is done by applying mask made from ngroups shift. Which broke Android
as they have 64 groups and the shift for mask resulted in an overflow.

Fixes: 61f4b23769f0 ("netlink: Don't shift with UB on nlk->ngroups")
Cc: "David S. Miller" <[email-masked]>
Cc: Herbert Xu <[email-masked]>
Cc: Steffen Klassert <[email-masked]>
Cc: [email-masked]
Cc: [email-masked]
Reported-and-Tested-by: Nathan Chancellor <[email-masked]>
Signed-off-by: Dmitry Safonov <[email-masked]>
Signed-off-by: David S. Miller <[email-masked]>
Signed-off-by: Greg Kroah-Hartman <[email-masked]>

---
 net/netlink/af_netlink.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

--- a/net/netlink/af_netlink.c
+++ b/net/netlink/af_netlink.c
@@ -1013,8 +1013,8 @@ static int netlink_bind(struct socket *s
 
 	if (nlk->ngroups == 0)
 		groups = 0;
-	else
-		groups &= (1ULL << nlk->ngroups) - 1;
+	else if (nlk->ngroups < 8*sizeof(groups))
+		groups &= (1UL << nlk->ngroups) - 1;
 
 	bound = nlk->bound;
 	if (bound) {