Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp47599imu; Thu, 8 Nov 2018 14:31:52 -0800 (PST) X-Google-Smtp-Source: AJdET5cvjz1huuLdka2vwV/LRYAD6eEUwBvxeOBLj7Sl1IM/MhThokXaviMDZEStdB0tcSvsV3TT X-Received: by 2002:a17:902:343:: with SMTP id 61-v6mr6064813pld.327.1541716312568; Thu, 08 Nov 2018 14:31:52 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1541716312; cv=none; d=google.com; s=arc-20160816; b=J6KAG6Y+haruNQ4OSFSj1N3jZ+T02XK6Ui8CtmTk2dq10rO6fj3vwtI4jiRwHPEmet tycdnv4zSARrzKA+Swc43hRPy4nnW82XuBviNHgpsX7O08qxX/+GFP/hRgMZlZHsCwfs eUAfoJPasiN61w85zR8QCMoV/YJdekyJuAC/+w4l+lR4hK6XiFN8Hf9uIa3GvjDe8B00 6bFVXv4cUww+biGVXPfngnXpRmdndURnC7uirKNI33XaqioLTi5JwuqHz5SdZZnVIRyr gTbaNvtv+8Fve9Da3XD6d01Fh7+Ci1hlP3HRA1uGV5ThnLGftl6uKNOrazJRTrNgWNOz YQ7w== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=UAGjlJg/BnkGmhpzxR99M7Oib98fmghV163OFypQHwU=; b=i/r1F2kI8xVc1SnNZ5LHjbhj+XRckwyjUx8u+07cNPzppGwVnbLo+VKCCEzBT+jfrW NAQRU4e+c/XPGuDg3TRGuDSZWoSJ+RRTMcYRWx7Z7L0lhTnyziSON9ngt8ZVyE+z0wW9 3BIR4hj1L8RxLhoGgeGThCZILN9TOb+SBJP6viPHoSmj2IP1593SvNDgt1moY3HIkk2F zT5jJwKT26G9oGWMRM4wu7GLB9zaFnBSTZui6dKzntRO5dtRpnMbc7IBacC9Nv4J/fye 1e4qvgJpEtsDTlZliWagsm+lsX1zX0Sz8fImx5FLrMBH9EvF80siPbErDMAsnsc40QxW 0Zvg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=fk7jwr8P; 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 j20si4626325pgg.162.2018.11.08.14.31.37; Thu, 08 Nov 2018 14:31:52 -0800 (PST) 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=fk7jwr8P; 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 S1730672AbeKIHiW (ORCPT + 99 others); Fri, 9 Nov 2018 02:38:22 -0500 Received: from mail.kernel.org ([198.145.29.99]:57520 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729282AbeKIHiV (ORCPT ); Fri, 9 Nov 2018 02:38:21 -0500 Received: from localhost (unknown [208.72.13.198]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id BED9520818; Thu, 8 Nov 2018 22:00:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1541714452; bh=AvxLej8yCvcs0wPmzUJbRdzHuvM4BmIN7fgqdRO2r7w=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=fk7jwr8PzQN63KLTQ+ACP8nKMaabA8VCnz8tXnAU4pAHFntpedaOzBwDNfMlT0hNz MA766nW5XxYOl3tutmhu/6C1MjfItZS/CapCfoE0LJLgTQ9VQb8t13Gp0Kks2besXT UWD2EhTYA4nay7cDh4lMkdBZczJoVII5Zi/nEoqM= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, syzbot+c7dd55d7aec49d48e49a@syzkaller.appspotmail.com, Dmitry Vyukov , Marcelo Ricardo Leitner , Neil Horman , "David S. Miller" Subject: [PATCH 4.4 097/114] sctp: fix race on sctp_id2asoc Date: Thu, 8 Nov 2018 13:51:52 -0800 Message-Id: <20181108215109.488441089@linuxfoundation.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181108215059.051093652@linuxfoundation.org> References: <20181108215059.051093652@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.4-stable review patch. If anyone has any objections, please let me know. ------------------ From: Marcelo Ricardo Leitner [ Upstream commit b336decab22158937975293aea79396525f92bb3 ] syzbot reported an use-after-free involving sctp_id2asoc. Dmitry Vyukov helped to root cause it and it is because of reading the asoc after it was freed: CPU 1 CPU 2 (working on socket 1) (working on socket 2) sctp_association_destroy sctp_id2asoc spin lock grab the asoc from idr spin unlock spin lock remove asoc from idr spin unlock free(asoc) if asoc->base.sk != sk ... [*] This can only be hit if trying to fetch asocs from different sockets. As we have a single IDR for all asocs, in all SCTP sockets, their id is unique on the system. An application can try to send stuff on an id that matches on another socket, and the if in [*] will protect from such usage. But it didn't consider that as that asoc may belong to another socket, it may be freed in parallel (read: under another socket lock). We fix it by moving the checks in [*] into the protected region. This fixes it because the asoc cannot be freed while the lock is held. Reported-by: syzbot+c7dd55d7aec49d48e49a@syzkaller.appspotmail.com Acked-by: Dmitry Vyukov Signed-off-by: Marcelo Ricardo Leitner Acked-by: Neil Horman Signed-off-by: David S. Miller Signed-off-by: Greg Kroah-Hartman --- net/sctp/socket.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) --- a/net/sctp/socket.c +++ b/net/sctp/socket.c @@ -248,11 +248,10 @@ struct sctp_association *sctp_id2assoc(s spin_lock_bh(&sctp_assocs_id_lock); asoc = (struct sctp_association *)idr_find(&sctp_assocs_id, (int)id); + if (asoc && (asoc->base.sk != sk || asoc->base.dead)) + asoc = NULL; spin_unlock_bh(&sctp_assocs_id_lock); - if (!asoc || (asoc->base.sk != sk) || asoc->base.dead) - return NULL; - return asoc; }