Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp3079688rwb; Mon, 15 Aug 2022 17:37:26 -0700 (PDT) X-Google-Smtp-Source: AA6agR5CYHlXYfmzXJO9LN067/3bjEThtXmY5AULv7z9zdZ+zIW+niv7h5KeP7vem4LAm/w51Hy6 X-Received: by 2002:a17:907:e88:b0:730:8e7d:2281 with SMTP id ho8-20020a1709070e8800b007308e7d2281mr12826950ejc.20.1660610246701; Mon, 15 Aug 2022 17:37:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660610246; cv=none; d=google.com; s=arc-20160816; b=Md+ZW5UVzEw3dbCmh6OqT2E7+JOiXiDR+O4H/NgIQH+SMiMufZ+i0eW5fsZBtyN+mL ROJ/OVgz33MZAPk7/PwHlb4EYegJv5+t8M0ufYVH5R+8Xc1b5wzEfUCGr786BcJVmx6q BZ85aZIGoy/WO37REkjRaD2PxzwM1EfjhTF2a0OQ0qQIPMyNuwdQm0gwgx9OpYWldTwq hOrQvApuUOCaIpHDIJIzrQjeIZwF0pd3kB3KcZ06nm8nAOomADOBxJxygHwXbS5RutaO FTYWx0imsA01ZSFzKnBjSRJxAOUiE/QESKK+d+GgBV5d2GK8wkZVa2lfCOkMwwsyVZk/ MZlg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=R3I9Lbc40g3R6SyuQpna1+8FI1R5c1knS6u3z0db7/0=; b=dt+x+zNIYzKK3W1Q/Dln9WVVnhsoKtJbC8yHizXpppg2EYrDBsDk100rEWjPrS7+J8 BKFdsRFYWy0lGjijHwMMmvOCYizhzYn/tzJszInDzv6UKtivf6N8s2CplL1nkJRdDLcU CNj0ZlG49nLy40bT/L2/pQlqc87ysAEOAFFrF+9tHACbxHBeRhPiXnLE5FdXhUT0V4F2 gcy/bbIlPYDwWMgjpeXtFN/OoXwJEZmwQAkXvvixtAyMY25iZn61K4GRIVeq9rR75sGQ FgPt2i93au+I/59xMsJlfD1qGX1OtEFUXYCJa57XTI06aqQkXFrVHnHKdchKZlTifk+1 x6Dg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=aJT+08Qp; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id c10-20020a05640227ca00b0043c77b9ca58si10817049ede.82.2022.08.15.17.37.01; Mon, 15 Aug 2022 17:37:26 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=aJT+08Qp; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1356610AbiHPACj (ORCPT + 99 others); Mon, 15 Aug 2022 20:02:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60860 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1356578AbiHOXyo (ORCPT ); Mon, 15 Aug 2022 19:54:44 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0FAD91617F5; Mon, 15 Aug 2022 13:19:32 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 8AC06B81181; Mon, 15 Aug 2022 20:19:32 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D148CC433D6; Mon, 15 Aug 2022 20:19:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1660594771; bh=4RHd20ascr7WjRRQ03cxtX2b/qudhSpb5JatRmaVYo8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=aJT+08Qp+h24lU+qt4JiweGozDOheYGALarPpLg1SlQ8TQZyIzogOeEUM5cbrS/LU jHUoqwXXs9kBWiWuPD/KXf4Yjo9l9DQEtFsXqA+hkowLb67rLVVSWCjhcUZUtZhj3c HcC1fDNjgebzEv+2bTTleEs6Vnp7W6SSaYo8oe/o= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Andrii Nakryiko , Alexei Starovoitov , Sasha Levin Subject: [PATCH 5.19 0512/1157] bpf: fix potential 32-bit overflow when accessing ARRAY map element Date: Mon, 15 Aug 2022 19:57:48 +0200 Message-Id: <20220815180500.176226318@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220815180439.416659447@linuxfoundation.org> References: <20220815180439.416659447@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Andrii Nakryiko [ Upstream commit 87ac0d600943994444e24382a87aa19acc4cd3d4 ] If BPF array map is bigger than 4GB, element pointer calculation can overflow because both index and elem_size are u32. Fix this everywhere by forcing 64-bit multiplication. Extract this formula into separate small helper and use it consistently in various places. Speculative-preventing formula utilizing index_mask trick is left as is, but explicit u64 casts are added in both places. Fixes: c85d69135a91 ("bpf: move memory size checks to bpf_map_charge_init()") Signed-off-by: Andrii Nakryiko Link: https://lore.kernel.org/r/20220715053146.1291891-2-andrii@kernel.org Signed-off-by: Alexei Starovoitov Signed-off-by: Sasha Levin --- kernel/bpf/arraymap.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/kernel/bpf/arraymap.c b/kernel/bpf/arraymap.c index fe40d3b9458f..1d05d63e6fa5 100644 --- a/kernel/bpf/arraymap.c +++ b/kernel/bpf/arraymap.c @@ -156,6 +156,11 @@ static struct bpf_map *array_map_alloc(union bpf_attr *attr) return &array->map; } +static void *array_map_elem_ptr(struct bpf_array* array, u32 index) +{ + return array->value + (u64)array->elem_size * index; +} + /* Called from syscall or from eBPF program */ static void *array_map_lookup_elem(struct bpf_map *map, void *key) { @@ -165,7 +170,7 @@ static void *array_map_lookup_elem(struct bpf_map *map, void *key) if (unlikely(index >= array->map.max_entries)) return NULL; - return array->value + array->elem_size * (index & array->index_mask); + return array->value + (u64)array->elem_size * (index & array->index_mask); } static int array_map_direct_value_addr(const struct bpf_map *map, u64 *imm, @@ -339,7 +344,7 @@ static int array_map_update_elem(struct bpf_map *map, void *key, void *value, value, map->value_size); } else { val = array->value + - array->elem_size * (index & array->index_mask); + (u64)array->elem_size * (index & array->index_mask); if (map_flags & BPF_F_LOCK) copy_map_value_locked(map, val, value, false); else @@ -408,8 +413,7 @@ static void array_map_free_timers(struct bpf_map *map) return; for (i = 0; i < array->map.max_entries; i++) - bpf_timer_cancel_and_free(array->value + array->elem_size * i + - map->timer_off); + bpf_timer_cancel_and_free(array_map_elem_ptr(array, i) + map->timer_off); } /* Called when map->refcnt goes to zero, either from workqueue or from syscall */ @@ -420,7 +424,7 @@ static void array_map_free(struct bpf_map *map) if (map_value_has_kptrs(map)) { for (i = 0; i < array->map.max_entries; i++) - bpf_map_free_kptrs(map, array->value + array->elem_size * i); + bpf_map_free_kptrs(map, array_map_elem_ptr(array, i)); bpf_map_free_kptr_off_tab(map); } @@ -556,7 +560,7 @@ static void *bpf_array_map_seq_start(struct seq_file *seq, loff_t *pos) index = info->index & array->index_mask; if (info->percpu_value_buf) return array->pptrs[index]; - return array->value + array->elem_size * index; + return array_map_elem_ptr(array, index); } static void *bpf_array_map_seq_next(struct seq_file *seq, void *v, loff_t *pos) @@ -575,7 +579,7 @@ static void *bpf_array_map_seq_next(struct seq_file *seq, void *v, loff_t *pos) index = info->index & array->index_mask; if (info->percpu_value_buf) return array->pptrs[index]; - return array->value + array->elem_size * index; + return array_map_elem_ptr(array, index); } static int __bpf_array_map_seq_show(struct seq_file *seq, void *v) @@ -690,7 +694,7 @@ static int bpf_for_each_array_elem(struct bpf_map *map, bpf_callback_t callback_ if (is_percpu) val = this_cpu_ptr(array->pptrs[i]); else - val = array->value + array->elem_size * i; + val = array_map_elem_ptr(array, i); num_elems++; key = i; ret = callback_fn((u64)(long)map, (u64)(long)&key, -- 2.35.1