Received: by 2002:a25:868d:0:0:0:0:0 with SMTP id z13csp3803433ybk; Tue, 19 May 2020 13:20:37 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz1qsBjlI6Cr1S7ZiDPBe8c1AI3G3e2aAV9n1VdAmV/jqUGRIr1mdRd20sg7lSd6UDf48rH X-Received: by 2002:a17:906:3b4f:: with SMTP id h15mr822403ejf.421.1589919636967; Tue, 19 May 2020 13:20:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1589919636; cv=none; d=google.com; s=arc-20160816; b=jar56/Yrlu/NzbIXFWwhFtkNtOz1Rq8rgVA84u83L1rz7E9LtbUBltvxLyICxMCIRL Od/UgNuUcyx98l073lXeLns+yRUrav/+8l0WNppvyoWsKBNdVSTmU0wrz7jI3oHQaSxN 3wzetQIyoI4LZMu9oR1hsw6IegxzWIJD+teo2cuVV0QUvxYkEwba0ilARVOqyGheYogI AE4BTGGNvACcX+xuBJg92URGjFB7LOJWlR0TI1KLEdb4bivO7VH9Uc4Y2bF0zKCUUddI p7Cd70zimcrUBRwB4Zz8MXkxVfkXpF48gVRtKEsfCicsG3FBDYAf88KpR9eY0TC5gjOH wfuQ== 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=lL4ozjO7sIu89/0ZdtpZ+grNZO7La5memrjZAz9fUk0=; b=eI7+2UDDgA48xX9ZO1BpKQSItfZbqZqCr4v2N9oCQnqHYNPk8LrZNqXdN0CJment3r lQdjDsAPcZsaYoN/q7UTp2m0pqcpd1AbtJKghrIRTVnkyuQrqPdjERvOdE5sdvDLObqc 7BankfCwsFP8DWhN99XkqKYfWfDK9u2LVuhz9Gh3UCJ4+7y4jTB2b/ATJibHYLuExbyV HxoG4aIJsNVt4p99DqJOu1KDBBYnt5fOpIJQVZ8pqZ4KYVSsB6O83IdbjbBPLxva9Eks o50+NG2YwJ2nqGR41hBMsLoeMOkV9VxnDO39kz5xfsETxUn3lN0GVvWCZjPQYdPafu1P 2SQw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@lca.pw header.s=google header.b=ZByOxcWZ; 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 11si277092edv.42.2020.05.19.13.20.12; Tue, 19 May 2020 13:20:36 -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=ZByOxcWZ; 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 S1726583AbgESUSp (ORCPT + 99 others); Tue, 19 May 2020 16:18:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55226 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726333AbgESUSo (ORCPT ); Tue, 19 May 2020 16:18:44 -0400 Received: from mail-ej1-x642.google.com (mail-ej1-x642.google.com [IPv6:2a00:1450:4864:20::642]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 367AFC08C5C1 for ; Tue, 19 May 2020 13:18:44 -0700 (PDT) Received: by mail-ej1-x642.google.com with SMTP id n24so552116ejd.0 for ; Tue, 19 May 2020 13:18:44 -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=lL4ozjO7sIu89/0ZdtpZ+grNZO7La5memrjZAz9fUk0=; b=ZByOxcWZEGVSx0wUUQIKY6Lw/heX5sMkMCAxTHHPK+6ioi6FiW5Ivw4Z7T0/Nsu58c KBdpFSMR5DZ9/Fan6TguF2CqRqBB1RiOHXF7adO0G8p+vAKeUpTqfShVLpqLUMDmSLi1 EQ9HfNNyPLSZop4afoNbYFWcjitqlo7fgxaKoCCTWTuiFmdSvBW7XY0iiFNWqgGj7KRs TC/MqL75oZY1jUN8E7jzoZ3x0UqfnmePmTE/KV1JCoRkX+RpHBWDUXURu+joTefkXso/ Zzj2NcqozV3LUz+vL8gbiGipZCgL0mKs7ynTlmNA5D2yFrKQ+OioPOkKCO8FSJo0vylo /yNA== 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=lL4ozjO7sIu89/0ZdtpZ+grNZO7La5memrjZAz9fUk0=; b=k8FbKPFd74FrCE8XM0GtxUse1Bn5xWpd6ESdB19x1CG59Ef9mlnBOADobOMBYA/UXC SmIrO2awl/O/2HfU/ledadus4JR3dGAUTkskv0qP+4QUQ5HhQRe/P/lwP8KADapeGCxl 7dAZSFLMSQIkxMatKy11BWRzWoTVxD2LVh3F6CY/uMLcf1pp90TG3M4cHuLD37GhFrhF ART58YhVfOq8ykLfp8nEau999Tu7vE8Ov94iJc0FNYNmkugH0rxGQgm/8YVWsPSmvZuj z3c5eTjFuvO1y18WSYWlRdUDQj8wet8vq1v8dFxsSOCuuPXephXZS/5jEjhzRCFdH3O1 4GPg== X-Gm-Message-State: AOAM530KfBZIw9o6EmqWae6hR75TB52fqIirJ8daVWQYbwQeoApego3b ipQ9ieJcCYT36MNERoILqStk4OfGW75/6pFrF+j60g== X-Received: by 2002:a17:906:934d:: with SMTP id p13mr869611ejw.452.1589919522687; Tue, 19 May 2020 13:18:42 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Qian Cai Date: Tue, 19 May 2020 16:18: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 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. 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.