Received: by 2002:ac0:98c7:0:0:0:0:0 with SMTP id g7-v6csp2461651imd; Fri, 2 Nov 2018 11:49:11 -0700 (PDT) X-Google-Smtp-Source: AJdET5eKm/b0tchjBh8qbIft+VXxIj9Nk0RcBkk7oTB5RKloFuGVpek6pbKXr06XUf311/J7hcmN X-Received: by 2002:a63:d21:: with SMTP id c33mr1331867pgl.299.1541184551808; Fri, 02 Nov 2018 11:49:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1541184551; cv=none; d=google.com; s=arc-20160816; b=HCsg88M2lO+iAQg3l5XaYvQ4KA+jX2l2T5M6a91BE9HstwiVkyr+qbPewDXuFcAtu4 BcAbdMPx9hOCNSVuV3ESuYjhAdAcC/2kP6utl42h7QoPA9nAXxUgDdjTXxVi4yhjzU4b moZGBEu/J+0OOdNzFxP7M+AhpMPubuJ2aeL/PCLO2lskoMlfG03r23bUS+/h3BGVka6l BYRNX0CoxGMCZFAm8XyV0zaDUKzfvWBIiKPRYwxx4x3h52eY+4db/Q7DGNdNTp1AwRE2 IB74y4uXmPiMTaD4MtCjgriIXQRkevFRKfEyLIpl5b3pBQELn1PqXTVcoxElX799EOE1 ROqQ== 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=6jMG26Bxt/Q6nqqkcSH0UheWGjQUR+bLMDIO0vO7UfA=; b=q9GY1mnGbnIezsxY+wv7B/hZLITl/95MBkBzc4UHUSOOSx9OblO4aGPgzm/23gXfsr iqb1LcLy3amjry4gVGXeO7EzUkQen3UDq8YbGSAVjh0D4HRkaIWDeS3y9LLPthoFh6RX aXx9ox/4aiDotrvFU1sai3+eb4qPraIaGPYJSP7Ej2+pL9alAE+/umkct9EOsw5pSaa8 jDlzm1rUfifwRVK2/57yZWTpNSVso1Q76v0ZoHNM9IrU0z8RalcIfA5gpVUl0S3UFjAj W8/o958ZjuE7h5Im81smQ/aDsniQ+fmY87itV2BggHBoWB3V+o0cG3lrEq9leQBMUD75 moUQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=HAaNDYnw; 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 b40-v6si22218530pla.285.2018.11.02.11.48.56; Fri, 02 Nov 2018 11:49:11 -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=HAaNDYnw; 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 S1729448AbeKCDzh (ORCPT + 99 others); Fri, 2 Nov 2018 23:55:37 -0400 Received: from mail.kernel.org ([198.145.29.99]:51190 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727629AbeKCDzh (ORCPT ); Fri, 2 Nov 2018 23:55:37 -0400 Received: from localhost (5356596B.cm-6-7b.dynamic.ziggo.nl [83.86.89.107]) (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 8AAEB20837; Fri, 2 Nov 2018 18:47:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1541184443; bh=CSBfPXU3aKS3akEwoxUMoTJ9tG8/2D5SlQUs0yzCRic=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=HAaNDYnwn/y0/Zc15onpZ5i3BFAhMcJigkhLUABiysb5KMn7AdSxsWSDAREWFfnKW gRyseczSnlApdiypgYfFeltB8OVxjiykkRWGnBS6q4J12YCpvY3k5PvnGD6GF5hMaR cykngVCmLmg0N8FT56RJBPq4OJIyz826blWPUcMo= 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.18 117/150] sctp: fix race on sctp_id2asoc Date: Fri, 2 Nov 2018 19:34:39 +0100 Message-Id: <20181102182911.223628407@linuxfoundation.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181102182902.250560510@linuxfoundation.org> References: <20181102182902.250560510@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.18-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 @@ -270,11 +270,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; }