Received: by 2002:a25:868d:0:0:0:0:0 with SMTP id z13csp92720ybk; Tue, 19 May 2020 16:25:28 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyLJ6I24V62VL2sa37S7NjfuXmyb1twUMzWdkh45V2flLfg0qyihbJm1IzhHznWhHl2Orv9 X-Received: by 2002:aa7:d492:: with SMTP id b18mr1023674edr.28.1589930728605; Tue, 19 May 2020 16:25:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1589930728; cv=none; d=google.com; s=arc-20160816; b=VjO5BXV319J844mk30tNetTdJyAcXEZqmu1GqDUZP5Nv4FUeC4ax6Ixg0HSnDr176t XEfQjtugQMXjKr+33eMoc4DXvtSxgI3WiA+hMwXO7OO0Ia5sjRoFPZsu7K2M/q21n+Bz o7i4kaEdgvOY6DVF0BejWYxzligJE3KdHMyFHufmAUodeWROOj9DA3hVfrtZOW6RvZKM 56zShQWFv7moT6z/lltm28dWHxQVqoVTC9LyohLcAQWLKFmJIUXxbmBGbo+Soy/pY60k S+vSFfb3+vnfV7VqNFnqxz2shpXN+AMU9hk0c7o6JFww1gQqVwizTLq4IrujoqVfTsWC LD0g== 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=NFJWmr9KEQenea6GLlA4NPI5u/eetb0vSSE/zevqVR8=; b=krsq1JY720DFMYxjJNSZaM1nGZMdT/hq9bBaavrKTl6cJv2UY+HR9JgDVZacl7ZDLZ fsbEqFjN9rIBr04oKRKkLteFIKfKzIqcnYP4r9GUga2TlzIGiIdGAcyMEV/ZvktEWTMj Nj/oC7P8MWMenOdQA09O26869EUWqXiewSic9Xhedyx6W2+RcVNqL2TdoRdwBesb6RT5 ObOc/V+HzdPFIxvp4k2+kdv4FZJSww2DTyo+LXnYoZOqaerVnGmor1RtX2Yc33XR7E9t TnSuePK05820woQfD1Ps90Ir52jysodCOPSzuTbGklJBCNAdZ4zaWmpz/tw6fMK1uro/ YKtQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=vNdj6UMX; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id dp6si915884ejc.472.2020.05.19.16.25.05; Tue, 19 May 2020 16:25:28 -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=@gmail.com header.s=20161025 header.b=vNdj6UMX; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728149AbgESXXc (ORCPT + 99 others); Tue, 19 May 2020 19:23:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56062 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725998AbgESXXb (ORCPT ); Tue, 19 May 2020 19:23:31 -0400 Received: from mail-qt1-x843.google.com (mail-qt1-x843.google.com [IPv6:2607:f8b0:4864:20::843]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 52DB7C061A0E; Tue, 19 May 2020 16:23:31 -0700 (PDT) Received: by mail-qt1-x843.google.com with SMTP id l1so1148133qtp.6; Tue, 19 May 2020 16:23:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=NFJWmr9KEQenea6GLlA4NPI5u/eetb0vSSE/zevqVR8=; b=vNdj6UMXyvgwDduOT4er4unGN5wDblCw0+Ab0NfuU0I8dqnG+Z/bw8oWQ220TUSEFS 9C7YFw8x77GkmrhRSrFhfDgW5hr4/zWkbFCZUmqceZY78dteRdezHotGKRirdhJzRyDX gwvTHKz2YOjAKJ+hddHNYu8HWEerwQuah8zNLsMZkIdpx/zgfqCJOSGDG7FrEaa6GbPC eYPg9+xqwwkql3xKO5l1k9j04U/SgMHgOdSRfG42Qw2sI9DplCuRM5GOn/LcKWRIN28x HOpOY8NCgky6lDmwqp0COLRwGHrEFGmrjmZKIh65HklgkFALEzqVJLGI3w3C/4JSnb8F Po+A== 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=NFJWmr9KEQenea6GLlA4NPI5u/eetb0vSSE/zevqVR8=; b=ls2wgnqH4W3bkOusUAAebZe4S51rTsXbL42jOiFqeROhUDa5mbO44SeZycfKykTKc4 AyjxmuyW5IPJQydVw29rMrI6vi225g3s+1NMnwc8oOdXDUILVJS14a93A/LWNuGIzsTM fHJF1s2nwVOzqt+ZY+fV3UNS9w+bcHA4lBbNBEFtHUHRsCi3J/5VOWrGPZ7N8LMZgQpX OsZ2Adb9c0q3MsAmPE9boVtkogFPlBSHFEIfH0R8CkTFz2JVj/1KtBHGW8bdzBc/lPTR jTmKZFIHdLSkoGUPkByhKfZ5snxvuma+MlstTfDpzco+25NSNTxKK1I9KkIBX9FLqR3z arAA== X-Gm-Message-State: AOAM531GLTVenDtxhqEl1LyrKftXUoVo/yBPjM88hwXAnPlQC16d9NsO 432qhngY2FLHQSt7Q7cu1vdtnY8D4HrjQNMrOjE= X-Received: by 2002:aed:2f02:: with SMTP id l2mr2472522qtd.117.1589930609786; Tue, 19 May 2020 16:23:29 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Andrii Nakryiko Date: Tue, 19 May 2020 16:23:18 -0700 Message-ID: Subject: Re: UBSAN: array-index-out-of-bounds in kernel/bpf/arraymap.c:177 To: Qian Cai 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 Tue, May 19, 2020 at 1:18 PM Qian Cai wrote: > > On Tue, May 19, 2020 at 3:30 PM Andrii Nakryiko > wrote: > > > > On Tue, May 19, 2020 at 8:00 AM Qian Cai wrote: > > > > > > 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--; > > > > This is an unnecessary performance hit for silencing what is clearly a > > false positive. I'm not sure that's the right solution here. It seems > > like something that's lacking on the tooling side instead. C language > > doesn't allow to express the intent here using flexible array > > approach. That doesn't mean that what we are doing here is wrong or > > undefined. > > Oh, so you worry about this ++ and -- hurt the performance? If so, how > about this? > > ubsan_disable_current(); > elem = array->value + array->elem_size * (index & array->index_mask); > ubsan_enable_current(); > > #ifdef UBSAN > ubsan_disable_current() > { > current->in_ubsan++; > } > #else > ubsan_disable_current() {} > #endif > > etc > > Production kernel would normally have UBSAN=n, so it is an noop. That would solve runtime performance hit, yes. > > Leaving this false positive unsilenced may also waste many people's > time over and over again, and increase the noisy level. Especially, it > seems this is one-off (not seen other parts of kernel doing like this) > and rather expensive to silence it in the UBSAN or/and compilers. I agree, it's bad to have this noise. But again, there is nothing wrong with the way it's used in BPF code base. We'd gladly use flexible array, if we could. But given we can't, I'd say the proper solution (in order of my preference) would be: - don't trigger false error, if zero-sized array is the member of union; - or have some sort of annotation at field declaration site (not a field access site). Is that possible?