Received: by 2002:a25:868d:0:0:0:0:0 with SMTP id z13csp3770077ybk; Tue, 19 May 2020 12:32:09 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwBR7iUaenbVM66uUBZ5yYrCcLUcP7bW6z2ckN6HlbScQzR2K6/n4AVBG3IKNkUS2is1S9V X-Received: by 2002:a17:906:eda3:: with SMTP id sa3mr747927ejb.253.1589916728820; Tue, 19 May 2020 12:32:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1589916728; cv=none; d=google.com; s=arc-20160816; b=U3Yu9nTXX6JgGjpUI2wanMcOaVfOodiihqTKNyvKn9CbGzd6KFgEXpxH2hsaTYF8DD COkakQIEo5mr2KqLFa5/W6ftcnZQQgBjK0ktjT2BjJMJKQdzgE2qZVd5lAFQ/e9XVGQg QCW0/5JPxqL7p51tXz38zwqLrSgvcbftvzTzFRNVNZH/CSg1sEUt3yF8G0eX+YeIg0L8 C5kxZV8/h0NRk+1cSPoBxwVQy6iUOhV/57F7K8DkNenCZQF96DsMUxEhgHvmQ1YeEPuj rjuXuVYnI3rCSGLyNXuJEfytC/BMVzSd/hYYmBLSw/QpHPIqbHbmRRzm8wmlkIQi9KEh 2lyQ== 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=JLNTKuWXG5RL22GiqkJEO67M5+yDAhwqj8vxR1pC9Pc=; b=FQYv6fDOoE+ffDDSQv8VLH4/Sacd+kukr4nW907UCTMjfB7cXCwnumctyeW4cAR/Z4 FFXMXd/jIkEGqakwidnfnODRravhxZd7xNf2oltyvKR04ze6z2P35Yv16MqX4sC+zkvA hqM6mjM/BEHdhYI7G6FMHVl8cg0JbpRb73j6oLNVWfEkD0roy2s3Pn4NKEhFJJNi3wGi S5LoIVVeeMaumX7hg4TfIPlePmhW1sIIH+fCuhygfE/pVI9yP9EC4glY8Ord1Wbe/LVm ELnZ1/q2djgC+m3u9eodDw8UmR4oTp6wmYyvu3X5+lKMDGY9eU1c5+HbGGBfQkDncNrq 5tlA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=EmfmIBE2; 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 x14si495268ejb.0.2020.05.19.12.31.45; Tue, 19 May 2020 12:32:08 -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=EmfmIBE2; 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 S1726861AbgESTaK (ORCPT + 99 others); Tue, 19 May 2020 15:30:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47406 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726059AbgESTaK (ORCPT ); Tue, 19 May 2020 15:30:10 -0400 Received: from mail-qt1-x844.google.com (mail-qt1-x844.google.com [IPv6:2607:f8b0:4864:20::844]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F096CC08C5C0; Tue, 19 May 2020 12:30:09 -0700 (PDT) Received: by mail-qt1-x844.google.com with SMTP id n22so567438qtv.12; Tue, 19 May 2020 12:30:09 -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=JLNTKuWXG5RL22GiqkJEO67M5+yDAhwqj8vxR1pC9Pc=; b=EmfmIBE2lvjkW0+zUQudmRZciASPLFo2EKpqAG/Tz9Uw9YulJE1h0sVQPNn7eIjH77 MDYOvZ07qpNts20VQPNDx5dMX8cIesvElbn4ICM80h0kR3yoRXXicI0fvj52xGv4c/Bv kJeyVmKHuLsYCP0DW6c+HxtMqXPzR1Vf1SnDsamSEsbaVgtgo7BG4oi525yT8Hxadx/z BmIaFhw+MSqgffVhBNiEC8EYkAPM7RYwDcn1fBiPxV7zwCuTSnAlEHRqPqC82grv3dR+ UjA/whjYPkottaONimBQxVeLe9tdFMww8nDqdEWSUck7RHwmVxQd2WHmQFchcStMpzBP jHmg== 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=JLNTKuWXG5RL22GiqkJEO67M5+yDAhwqj8vxR1pC9Pc=; b=pcC1EWUABJd0XXMEM+8Z06lQLbluilNgeHW6RJus2o3Ws0H0mhv64XJRuXjaZgbu3V 5LRnv4d8HWu9dXCc/py89O+7S4kyJVFPh5pATl6qilWp6fQyVhX6B+hFwHw/7Rdt7c06 JeoAVbgjPAiWSfvhGyDzhbSosY1Q5SQ8u174F3OtyW73uhSu+Fj0V/wbrLqOUP2eZXft oxmVb6li3w2Wj2svtrG+CnRDpybqZ2bUoQPpuE6QjybAQOrwb3LXJPDRMJC0haWrpGcf xvGTjz5Pm9J02DDR0Onx4/qkUHauRwjjZteZkSzyyJvmjFk+vfZhwBhtQ2tbDAXedjWL PtAA== X-Gm-Message-State: AOAM531t3VJ6V3lY0tguXuQZjFZucDFMrcDesT0I6woqoop+3EzkjUrA u6hgbKINrw4UNu7vcBv55T8Vs9ez/hyVbo75e3M= X-Received: by 2002:ac8:1ae7:: with SMTP id h36mr1504281qtk.59.1589916609195; Tue, 19 May 2020 12:30:09 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Andrii Nakryiko Date: Tue, 19 May 2020 12:29:58 -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 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. > + > + return elem; > }