Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp3729605ybv; Mon, 10 Feb 2020 05:26:46 -0800 (PST) X-Google-Smtp-Source: APXvYqznxt8u6jqVUTN/C5OsdviYGHr+0Bci7sprnjz6TPJtrWxhcL38rJ2FLoBNehcp+BgSCPoG X-Received: by 2002:aca:534f:: with SMTP id h76mr813184oib.23.1581341206153; Mon, 10 Feb 2020 05:26:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1581341206; cv=none; d=google.com; s=arc-20160816; b=IRrACUfXzm7LvdvhOwoZu17e/Smo49lZ70i7OzGW4itEFipjkdxIYCzftutbg6zjb0 //HTKrFKBx7qqUcvBW0vkBbTnSYa3IdzFgTpyi0zh8KYdzI0wkEhwy50V0PSvS5frG1t H8jvZEUHee0+JHEiYVgd+5dllSG+x0Z0ETyH25QQxq5KSda0X4+8LYsIxaWvsrkdLZpv 3xUOFnpuoskIlZBJ48XQD/G0ytNwMMi6+4PWTJowkFcQXfMh9PH14RU7Rvk1cACG8NI1 Q2za1kSMbz1ZpZOlIml+rfJqBrfe6igbxhhjadhxqY6fccI3C/gIggdcSEVdhA89ly4/ I5hg== 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=7F7JFI2Wd6v9MrbjQi7MhOD9jFH1zH/y8qZ3Gon9O0c=; b=cSRRYhHNoCwupgxCtld6Y3/5Q5SXmkag/U1GK0RPBJVmgEae8ZPL7Vqe2YEYBpg+8V Hs9Rl7/8rJjn8URrMaoPTsxNPtn4jJvjVxl4LCjncuE+fIuR5/uaumhSWhf4sFcrrVVS f/0v0/GNlUmcE3mMWeb6+l3SYTv8Onh22dYI3PI5/q+Hd800yvacxsado3pW4twWt2Z7 xHRo2PW/zwo3oIOsDdK/kG5lr/oqEioTizs25ssaKViNSSLUkGKFp2OA+BybSjLwctXJ zDYvHoYyLbDrZlPef9PO9YkTFyEfN8J39oZnNLIOUKxFYj7LdFIEB0mm6qEjmXYXuSYH 9Rfg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=N07D8JwA; 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 t9si192836otp.57.2020.02.10.05.26.33; Mon, 10 Feb 2020 05:26:45 -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=N07D8JwA; 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 S1731192AbgBJNZ0 (ORCPT + 99 others); Mon, 10 Feb 2020 08:25:26 -0500 Received: from mail.kernel.org ([198.145.29.99]:57376 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728562AbgBJMgz (ORCPT ); Mon, 10 Feb 2020 07:36:55 -0500 Received: from localhost (unknown [209.37.97.194]) (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 CD54E2051A; Mon, 10 Feb 2020 12:36:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1581338214; bh=iW2erDbgdHRSyYnWrC6Cbf3Cy5uDHy6bDSkEM2YR5mw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=N07D8JwAgaDrzMY7goHxZw3vF/8krnCrMUL4jgmkmXgkB3dxTPJaFE/5hNkI6TtxU ALJfdDgqcMtlmg8lsIN3RpHePISriXK/cZWnwS6x7joNyfOoYKgaloyXE5Oe5rgDpG i5Jlx4LQoHkDWzGx1MiI75gIkYRivfwhmLUZ08Bw= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Eric Dumazet , John Fastabend , Jamal Hadi Salim , Jiri Pirko , Jakub Kicinski , Cong Wang , "David S. Miller" , syzbot+35d4dea36c387813ed31@syzkaller.appspotmail.com Subject: [PATCH 5.4 007/309] net_sched: fix an OOB access in cls_tcindex Date: Mon, 10 Feb 2020 04:29:23 -0800 Message-Id: <20200210122406.768264448@linuxfoundation.org> X-Mailer: git-send-email 2.25.0 In-Reply-To: <20200210122406.106356946@linuxfoundation.org> References: <20200210122406.106356946@linuxfoundation.org> User-Agent: quilt/0.66 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 From: Cong Wang [ Upstream commit 599be01ee567b61f4471ee8078870847d0a11e8e ] As Eric noticed, tcindex_alloc_perfect_hash() uses cp->hash to compute the size of memory allocation, but cp->hash is set again after the allocation, this caused an out-of-bound access. So we have to move all cp->hash initialization and computation before the memory allocation. Move cp->mask and cp->shift together as cp->hash may need them for computation too. Reported-and-tested-by: syzbot+35d4dea36c387813ed31@syzkaller.appspotmail.com Fixes: 331b72922c5f ("net: sched: RCU cls_tcindex") Cc: Eric Dumazet Cc: John Fastabend Cc: Jamal Hadi Salim Cc: Jiri Pirko Cc: Jakub Kicinski Signed-off-by: Cong Wang Signed-off-by: David S. Miller Signed-off-by: Greg Kroah-Hartman --- net/sched/cls_tcindex.c | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) --- a/net/sched/cls_tcindex.c +++ b/net/sched/cls_tcindex.c @@ -333,12 +333,31 @@ tcindex_set_parms(struct net *net, struc cp->fall_through = p->fall_through; cp->tp = tp; + if (tb[TCA_TCINDEX_HASH]) + cp->hash = nla_get_u32(tb[TCA_TCINDEX_HASH]); + + if (tb[TCA_TCINDEX_MASK]) + cp->mask = nla_get_u16(tb[TCA_TCINDEX_MASK]); + + if (tb[TCA_TCINDEX_SHIFT]) + cp->shift = nla_get_u32(tb[TCA_TCINDEX_SHIFT]); + + if (!cp->hash) { + /* Hash not specified, use perfect hash if the upper limit + * of the hashing index is below the threshold. + */ + if ((cp->mask >> cp->shift) < PERFECT_HASH_THRESHOLD) + cp->hash = (cp->mask >> cp->shift) + 1; + else + cp->hash = DEFAULT_HASH_SIZE; + } + if (p->perfect) { int i; if (tcindex_alloc_perfect_hash(net, cp) < 0) goto errout; - for (i = 0; i < cp->hash; i++) + for (i = 0; i < min(cp->hash, p->hash); i++) cp->perfect[i].res = p->perfect[i].res; balloc = 1; } @@ -350,15 +369,6 @@ tcindex_set_parms(struct net *net, struc if (old_r) cr = r->res; - if (tb[TCA_TCINDEX_HASH]) - cp->hash = nla_get_u32(tb[TCA_TCINDEX_HASH]); - - if (tb[TCA_TCINDEX_MASK]) - cp->mask = nla_get_u16(tb[TCA_TCINDEX_MASK]); - - if (tb[TCA_TCINDEX_SHIFT]) - cp->shift = nla_get_u32(tb[TCA_TCINDEX_SHIFT]); - err = -EBUSY; /* Hash already allocated, make sure that we still meet the @@ -376,16 +386,6 @@ tcindex_set_parms(struct net *net, struc if (tb[TCA_TCINDEX_FALL_THROUGH]) cp->fall_through = nla_get_u32(tb[TCA_TCINDEX_FALL_THROUGH]); - if (!cp->hash) { - /* Hash not specified, use perfect hash if the upper limit - * of the hashing index is below the threshold. - */ - if ((cp->mask >> cp->shift) < PERFECT_HASH_THRESHOLD) - cp->hash = (cp->mask >> cp->shift) + 1; - else - cp->hash = DEFAULT_HASH_SIZE; - } - if (!cp->perfect && !cp->h) cp->alloc_hash = cp->hash;