Received: by 2002:ac0:98c7:0:0:0:0:0 with SMTP id g7-v6csp2467690imd; Fri, 2 Nov 2018 11:56:04 -0700 (PDT) X-Google-Smtp-Source: AJdET5e5odH6rXznDe9YdL6v2TG0wYlCe61vcaoxNQxeWVSM6mGZWF57KXMcDd77WncFZOrSAZBZ X-Received: by 2002:a17:902:e005:: with SMTP id ca5-v6mr12514432plb.195.1541184964113; Fri, 02 Nov 2018 11:56:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1541184964; cv=none; d=google.com; s=arc-20160816; b=Nwo4X+FsNQzICMZrJMWOWyPFJpqF9U5uuLgkebLhtdl/kg0/7tYwQ/eKgF8PdMAqFq Y+udQo5RrRJIH0p+nRrS6jeUeEBCT4u2TiA6dLoZbCKfeetW1Txa912ghz/NC9TwZI+g IiDp1KtQdzezXhU/XsHuFg0K8NKo5w8aMzaMweHRsJPkJ6mRVXtVfymHj9lxD77wuz2w zHEXks7FwHa5ljdcHJxoTw6c52pdLIzD1niInYk0YCOiO1aWldp3loSjzg6OG1NtMf5A R2t70ZT+IK7Z4kmnNcGffjHHmZZk5kBcuCQEcuMVqUV+YvlMfnHN/29mijn+PN7e+PQe ectg== 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=AV+rGhAezvQ9IXSsqPNdpiM4N4982ehN5FVupVYk9nc=; b=u+2z8Q+l0WXMjkfSGqW6wFmJ6faccpQ4AZPgIAT4rksldHeHtZMCcNVXONECaJV9N9 O4TdSFgeTCLjwHPcnRRhx3Bux/R4WNHQN+2A/Lot/3EYOtZdoJ4MGqQlg2QHp/goWLZR /qcCst8uBcbhYHa2mcR3+DlwGgZNq4p12kZ3FtStKdxdj+0BPQMUF9cTRC5KfME8snIA lEQtvv4Yypxbywnt3GFi+yhZ9TYUCDAClMXKXO4xA5fszLBslpigS3Z7ps+dyAda1TZ3 o2fvNUxVvUoy+RHIRKjxj5/7kug7+rcY3n0WM4vgSSJoBWer8CnCCLwzJ7Q//z6U76E6 qdgg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=iw+Nnxdb; 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 h9si6982259pgb.319.2018.11.02.11.55.49; Fri, 02 Nov 2018 11:56:04 -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=iw+Nnxdb; 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 S1731357AbeKCEDa (ORCPT + 99 others); Sat, 3 Nov 2018 00:03:30 -0400 Received: from mail.kernel.org ([198.145.29.99]:59290 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729676AbeKCED3 (ORCPT ); Sat, 3 Nov 2018 00:03:29 -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 B3C9E2082D; Fri, 2 Nov 2018 18:55:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1541184913; bh=Ky+jMheRdJJlXhjCJJvNDHxKtbPum6EPf17w4+y4ZJ8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=iw+NnxdbS8F72z7sz+zzDPJVuRx27FOhTMv/8AqMhAK2BckOgn6nAA/AE63BasogI 86SGLbGFpysjQGU9alYL2y7m6jsJsl41Vkwzxf75XkTjtRxd1Z7lSD6FfxsQoa7r/s mBa4XN08BPxfuzvfbc4WNxs88Ka02WkJeXZygtYA= 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.14 127/143] sctp: fix race on sctp_id2asoc Date: Fri, 2 Nov 2018 19:35:12 +0100 Message-Id: <20181102182908.035747724@linuxfoundation.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181102182857.064326086@linuxfoundation.org> References: <20181102182857.064326086@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.14-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 @@ -250,11 +250,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; }