Received: by 2002:a25:868d:0:0:0:0:0 with SMTP id z13csp3579406ybk; Tue, 19 May 2020 08:03:23 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx7JXKIq8Aig1jX8UbHOsXAh5WAQuxlMqfdKnKIzE296/Pk0TIY1AqiPQkYfbFdTohmVKeo X-Received: by 2002:a17:906:4009:: with SMTP id v9mr1661872ejj.63.1589900603699; Tue, 19 May 2020 08:03:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1589900603; cv=none; d=google.com; s=arc-20160816; b=yxMkR04MITxaKwzWxAiWThzq3b1eNwO+k6/mWC85ZQ6k/uWOv1eVEpuPPtdXvhYPks 0zD1Kr+bVVQGdf9/9O7g5nV7xoPqoF9ZbeK75FgmhDwiUWBSdffexbR5KRVwvi709xa0 QSyT7F5VkUSgkAZ/DP7SAJYXfikCO4AaDs5SAgBnB4qSQBAQGUNZ5HVzaEMuc405rPgf y08K/PPA/yhs1c8YUAFIg5Va98NfCBbnHUzrCvDxFqY7Mt0rQBuezYfT93BxT6D3AB0N zPiRRpZlScW6Vuw2wFoNV4KIBiK+TskNFLAQMSuNYyz3guJvySNzLh6bS1tIMTPf8WDz lJWA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=olWeaL9L3Th4SH3qpwoevbMzoFhixmGWtefArTEgTk4=; b=GQCZFQkVKO5hd+cize2CmJ9SWy9Ji3oYlgUpTnuA7V9zKkEbFWjpGMN3iZ9PeLeX/H a2Xd2lzllwJfJ82SU3YrzgvhEagaW1IzX40KNPdnrIbDN7IA0fFycLp/eDbi5afFk5Kj mKydMw7nmokUjxoWWh5k2/KRDd8YlKreqHyQweIzuOCJb7Ryk386q8RAjk9ySzV7DDEe bvQtgdrf2fdX6F4dRoVkjrVHJg+cX7wFWUXy4EP76jCp7PzCR29e+l+uOgl9kdrsVG2F /md0jwgmIG4MrjTx+Buf5GPxiP1I8axcF4uesACwm7bSjCvMFEFgtCOOYqmmGurZcnDn EGrA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@lca.pw header.s=google header.b=bEyFs82H; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id oy11si184ejb.386.2020.05.19.08.02.51; Tue, 19 May 2020 08:03:23 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@lca.pw header.s=google header.b=bEyFs82H; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729194AbgESPAo (ORCPT + 99 others); Tue, 19 May 2020 11:00:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33356 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728953AbgESPAo (ORCPT ); Tue, 19 May 2020 11:00:44 -0400 Received: from mail-ed1-x544.google.com (mail-ed1-x544.google.com [IPv6:2a00:1450:4864:20::544]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D4789C08C5C2 for ; Tue, 19 May 2020 08:00:43 -0700 (PDT) Received: by mail-ed1-x544.google.com with SMTP id l5so5271175edn.7 for ; Tue, 19 May 2020 08:00:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=lca.pw; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=olWeaL9L3Th4SH3qpwoevbMzoFhixmGWtefArTEgTk4=; b=bEyFs82H4jPOzYgYPlYzidt1XghxF9RQPbBVSYNlHHo+64d1MNTn8faNwlkFFp8XOf gG93TwfVwkG1x1SYSw+sjMMWFJkF7MEU4WVXqVGtcdcINUqMvvJ0+cCULgJtCQLQs61K vXj6dXSp1aRiCwt0AazexjVnkE4ZNAkpCDQS9XxiM0RaP1T3r5NtDEln4DdTvI5ROC75 IyyM7+pvwZ7/s+bKvbD/iAAfpgQHgYVyLMUKpVP+x5wk348rr8fMWFwaLt3cdSNfwjEe PHIFvD/DButvkbeAQN6sl0IFWPv3XAMn/cdJYslshqL4n0Fjc65uCPcTOXm8wnzgg8Ac dmKg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=olWeaL9L3Th4SH3qpwoevbMzoFhixmGWtefArTEgTk4=; b=HNLK/4Uj6R7nFN25XmbiSHRKE9EK/CqwN5Ie1WARukVGYkiVdWcqeZvSDNtbaxGyQl w1FkqTSUAYXiWCzxjYk1OHX7EoTgTVsTzrMtZWMfX0WwDndq1xCxM85FoSBA8k0mECks RWm6q4JLUBbZJVMiRz++bY/AYLQtgqDUQAcyNsYLDJjeCo52aHWPSXbxmUTY6Mx8nZ29 ScX8VQCy8P1aUyqs5k92epf9weLkZ/D2p4wVAgfRPvVI8quT8LI2GsEhhYubu1Lb2LQA mg6FAXlGiTyn9tiYumRo28S2PL8Ot+Rwkx1zuzQRP/1M4tFC/RF+JPsOsyQlzyV6r7ik /h9Q== X-Gm-Message-State: AOAM5324QGfkV6yh86IhsvsOSVbIY5pKLMrC6snzzBRfYHFh1NvBfGQg YBKlbkrcCsoD+mhpMd4PxNc9QG4ABvxQWcQuy2TWgA== X-Received: by 2002:a50:ee1a:: with SMTP id g26mr17925721eds.18.1589900442359; Tue, 19 May 2020 08:00:42 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Qian Cai Date: Tue, 19 May 2020 11:00:31 -0400 Message-ID: Subject: Re: UBSAN: array-index-out-of-bounds in kernel/bpf/arraymap.c:177 To: Andrii Nakryiko Cc: Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , Andrii Nakryiko , John Fastabend , KP Singh , Linux Netdev List , bpf , Linux Kernel Mailing List , clang-built-linux , Kees Cook Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, May 18, 2020 at 8:25 PM Andrii Nakryiko wrote: > > On Mon, May 18, 2020 at 5:09 PM Qian Cai wrote: > > > > On Mon, May 18, 2020 at 7:55 PM Andrii Nakryiko > > wrote: > > > > > > On Sun, May 17, 2020 at 7:45 PM Qian Cai wrote: > > > > > > > > With Clang 9.0.1, > > > > > > > > return array->value + array->elem_size * (index & array->index_mask); > > > > > > > > but array->value is, > > > > > > > > char value[0] __aligned(8); > > > > > > This, and ptrs and pptrs, should be flexible arrays. But they are in a > > > union, and unions don't support flexible arrays. Putting each of them > > > into anonymous struct field also doesn't work: > > > > > > /data/users/andriin/linux/include/linux/bpf.h:820:18: error: flexible > > > array member in a struct with no named members > > > struct { void *ptrs[] __aligned(8); }; > > > > > > So it probably has to stay this way. Is there a way to silence UBSAN > > > for this particular case? > > > > I am not aware of any way to disable a particular function in UBSAN > > except for the whole file in kernel/bpf/Makefile, > > > > UBSAN_SANITIZE_arraymap.o := n > > > > If there is no better way to do it, I'll send a patch for it. > > > That's probably going to be too drastic, we still would want to > validate the rest of arraymap.c code, probably. Not sure, maybe > someone else has better ideas. This works although it might makes sense to create a pair of ubsan_disable_current()/ubsan_enable_current() for it. diff --git a/kernel/bpf/arraymap.c b/kernel/bpf/arraymap.c index 11584618e861..6415b089725e 100644 --- a/kernel/bpf/arraymap.c +++ b/kernel/bpf/arraymap.c @@ -170,11 +170,16 @@ static void *array_map_lookup_elem(struct bpf_map *map, void *key) { struct bpf_array *array = container_of(map, struct bpf_array, map); u32 index = *(u32 *)key; + void *elem; if (unlikely(index >= array->map.max_entries)) return NULL; - return array->value + array->elem_size * (index & array->index_mask); + current->in_ubsan++; + elem = array->value + array->elem_size * (index & array->index_mask); + current->in_ubsan--; + + return elem; }