Received: by 2002:ac0:950c:0:0:0:0:0 with SMTP id f12csp3229760imc; Wed, 13 Mar 2019 12:11:35 -0700 (PDT) X-Google-Smtp-Source: APXvYqyWzDv4lmGg5ugEKJok/HNOf5q6/fyKOw7ViiStZsVOkk5LjOk1qHsJBm14yKQFQD+5tHCC X-Received: by 2002:a62:1ac3:: with SMTP id a186mr45543405pfa.48.1552504295455; Wed, 13 Mar 2019 12:11:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1552504295; cv=none; d=google.com; s=arc-20160816; b=ZSPCvuArRHiwOUTE0tqVuGcGt0v89x8ktAekPbwrXK22R57EJak4FKKc96+7sZm/7s O2Zat5j/15u3vFi7DipDlwy6q7gJX8/ifE7UZtOQPo+PJxQRt7O3wVaw+t8mySUmaJMW fsMWDSVu9qbd2qYOGi7eqZ+PKjoZSRLoBvc3C3ioYGlOhIHZr3bpzDy4VTw4ZCuHYGVk zO9KFHH9VSNIuCELvnucPC9zp+Yw6jgBWOwRmAe1y/s9RGHNK6IxW0iPwr6+k/lgfy7f FcttXIhj7zU7DrxBHRx+kv7DlGYObuHJkq+Y6djcNUUMk4N7B3QqdqZdod5bMvuNaU5R S1PA== 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:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=3tfZTfRNLpLSINFdu+gLNT9aK8edMjvLUWzKr7Pg9oY=; b=sLPT4AAii2k0Rf92vdA/OIvik2J/Zlu54wzIzrgraABowzB5iJO0CgrQoHwnzwdz/d xTKs588x0XNZZ1C1t7dWhc/sv7SMv9DYi6dFPn2KxE0KxeeRzQDYKD6dL54EtEND3/xJ 2XE2BCeQ4Pqy3Xmu1mx8UOGKQeuJHmeniK9H8icaZZezZVLMPw6Z86hub3+mnvv1udXd 5hMQyk+707vgPmdsIXSzGH/XDxuQNKQllGL4eSV+sAWCSrP820ZsYucl0jSOvl5V4L9v 8aikbL/eEzyKvqEuWBMhFVu2Z5k0Ah9L7KzE2LzlO84iKWeE29PjRRpqxjakMce+ZyT6 6Riw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=W3EVy6bH; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 9si11890963plc.121.2019.03.13.12.11.19; Wed, 13 Mar 2019 12:11:35 -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; dkim=pass header.i=@kernel.org header.s=default header.b=W3EVy6bH; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727180AbfCMTKq (ORCPT + 99 others); Wed, 13 Mar 2019 15:10:46 -0400 Received: from mail.kernel.org ([198.145.29.99]:41268 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727126AbfCMTKk (ORCPT ); Wed, 13 Mar 2019 15:10:40 -0400 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 5EA14217F5; Wed, 13 Mar 2019 19:10:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1552504239; bh=FKaYhBB/zZrnG1uUtZMifYIItmAabeW4lHxjAiVIZTM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=W3EVy6bHJKkGTyMpOVmejbv/nAleFR+z6M0XMcOo8X71/FO36YZ6JvIDajaO6glzZ sXpaYmxO02gu00kaH2b8rhMp75qoxW6B71GRa/IGpzcMR14MRr2IR8IlVJ3hkD1Hyq 22WWjUcMhFBscza892IXuvt32kighlsrDxM7E6s8= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Sean Tranchetti , Steffen Klassert , Sasha Levin Subject: [PATCH AUTOSEL 4.20 09/60] af_key: unconditionally clone on broadcast Date: Wed, 13 Mar 2019 15:09:30 -0400 Message-Id: <20190313191021.158171-9-sashal@kernel.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20190313191021.158171-1-sashal@kernel.org> References: <20190313191021.158171-1-sashal@kernel.org> MIME-Version: 1.0 X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Sean Tranchetti [ Upstream commit fc2d5cfdcfe2ab76b263d91429caa22451123085 ] Attempting to avoid cloning the skb when broadcasting by inflating the refcount with sock_hold/sock_put while under RCU lock is dangerous and violates RCU principles. It leads to subtle race conditions when attempting to free the SKB, as we may reference sockets that have already been freed by the stack. Unable to handle kernel paging request at virtual address 6b6b6b6b6b6c4b [006b6b6b6b6b6c4b] address between user and kernel address ranges Internal error: Oops: 96000004 [#1] PREEMPT SMP task: fffffff78f65b380 task.stack: ffffff8049a88000 pc : sock_rfree+0x38/0x6c lr : skb_release_head_state+0x6c/0xcc Process repro (pid: 7117, stack limit = 0xffffff8049a88000) Call trace: sock_rfree+0x38/0x6c skb_release_head_state+0x6c/0xcc skb_release_all+0x1c/0x38 __kfree_skb+0x1c/0x30 kfree_skb+0xd0/0xf4 pfkey_broadcast+0x14c/0x18c pfkey_sendmsg+0x1d8/0x408 sock_sendmsg+0x44/0x60 ___sys_sendmsg+0x1d0/0x2a8 __sys_sendmsg+0x64/0xb4 SyS_sendmsg+0x34/0x4c el0_svc_naked+0x34/0x38 Kernel panic - not syncing: Fatal exception Suggested-by: Eric Dumazet Signed-off-by: Sean Tranchetti Signed-off-by: Steffen Klassert Signed-off-by: Sasha Levin --- net/key/af_key.c | 40 +++++++++++++++------------------------- 1 file changed, 15 insertions(+), 25 deletions(-) diff --git a/net/key/af_key.c b/net/key/af_key.c index 9d61266526e7..7da629d59717 100644 --- a/net/key/af_key.c +++ b/net/key/af_key.c @@ -196,30 +196,22 @@ static int pfkey_release(struct socket *sock) return 0; } -static int pfkey_broadcast_one(struct sk_buff *skb, struct sk_buff **skb2, - gfp_t allocation, struct sock *sk) +static int pfkey_broadcast_one(struct sk_buff *skb, gfp_t allocation, + struct sock *sk) { int err = -ENOBUFS; - sock_hold(sk); - if (*skb2 == NULL) { - if (refcount_read(&skb->users) != 1) { - *skb2 = skb_clone(skb, allocation); - } else { - *skb2 = skb; - refcount_inc(&skb->users); - } - } - if (*skb2 != NULL) { - if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf) { - skb_set_owner_r(*skb2, sk); - skb_queue_tail(&sk->sk_receive_queue, *skb2); - sk->sk_data_ready(sk); - *skb2 = NULL; - err = 0; - } + if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf) + return err; + + skb = skb_clone(skb, allocation); + + if (skb) { + skb_set_owner_r(skb, sk); + skb_queue_tail(&sk->sk_receive_queue, skb); + sk->sk_data_ready(sk); + err = 0; } - sock_put(sk); return err; } @@ -234,7 +226,6 @@ static int pfkey_broadcast(struct sk_buff *skb, gfp_t allocation, { struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id); struct sock *sk; - struct sk_buff *skb2 = NULL; int err = -ESRCH; /* XXX Do we need something like netlink_overrun? I think @@ -253,7 +244,7 @@ static int pfkey_broadcast(struct sk_buff *skb, gfp_t allocation, * socket. */ if (pfk->promisc) - pfkey_broadcast_one(skb, &skb2, GFP_ATOMIC, sk); + pfkey_broadcast_one(skb, GFP_ATOMIC, sk); /* the exact target will be processed later */ if (sk == one_sk) @@ -268,7 +259,7 @@ static int pfkey_broadcast(struct sk_buff *skb, gfp_t allocation, continue; } - err2 = pfkey_broadcast_one(skb, &skb2, GFP_ATOMIC, sk); + err2 = pfkey_broadcast_one(skb, GFP_ATOMIC, sk); /* Error is cleared after successful sending to at least one * registered KM */ @@ -278,9 +269,8 @@ static int pfkey_broadcast(struct sk_buff *skb, gfp_t allocation, rcu_read_unlock(); if (one_sk != NULL) - err = pfkey_broadcast_one(skb, &skb2, allocation, one_sk); + err = pfkey_broadcast_one(skb, allocation, one_sk); - kfree_skb(skb2); kfree_skb(skb); return err; } -- 2.19.1