Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp3524346pxv; Mon, 19 Jul 2021 02:17:07 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx8JD6AG86Qf0hbDi1ZSU1eKHtz2yKHXYAX8X4NxLAmgk9KrQ0Qql9Jv2Osql1kMWDIlgYI X-Received: by 2002:a92:507:: with SMTP id q7mr12145949ile.81.1626686227296; Mon, 19 Jul 2021 02:17:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626686227; cv=none; d=google.com; s=arc-20160816; b=vL2LvL+bezmIuLCd/hv3kf1dtMNPeRSngsWtXqW26ChMtnIxse4K5C80KkFU2LAqgI Xp9/+Oh7I+UTOM1QYWo40YkGcUolbNeYwZ7bTJWOwNSax2d1d5e9VMUgBK1K2G3L+oRk o34uAl/FBhZe9lZHkddl+cBbM8A2PxpAK2Lb7RdWag8uBKyU/5RM7i9iDBpvJv9DHwx5 ZiUfJOlXldEwlkZb9tzCk2PS5AvDQ+IInA2/uCEXxpoY2VYPkF7OrbeLozKIfft6wTi7 9n1hbMtdQ4v1onu2V2EOiBMlCQGCQBnCEWSonEWnfuCXgEl9vta7u747k++jztb6A6T0 ibZQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=1k48KCcy3y6vIAdtOHWsq+O6aGKfc6p+OZyi1Vzkxdg=; b=vRCU2aT17SVAS8wZCZVu8WY690aYHSryw1yJ8fet/2l+rVHWIZ8elSGg7Qnd+SkKf2 wKY60dPsfa2FaXQcqPHEltpW3tDDfwrguoyUq+OnhmnQI2k/p21EzikM3v7EdT1Syt6Z WQcCbHIfQcJcU2iktt1cawAYXs46cCWIM3bUvQMPf5xxSFMBYj8vfFouwvHh2d7xAuph 2nmsWcGANtQQGDjb8NzHabRBgoE5MicK2q/OrPZ6HZKZkM3eXh06zFTDhce4TGrT9Nkb TwJGx3xg2XzritbyHYB5pN0aYMLHJHWY4EZcPG4hghJDGYSPMw6ueoFCzaoioofyhRwe M2IA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@cloudflare.com header.s=google header.b=Q1WubCmk; 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=REJECT sp=REJECT dis=NONE) header.from=cloudflare.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id i23si18186525ila.157.2021.07.19.02.16.55; Mon, 19 Jul 2021 02:17:07 -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=@cloudflare.com header.s=google header.b=Q1WubCmk; 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=REJECT sp=REJECT dis=NONE) header.from=cloudflare.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235789AbhGSIel (ORCPT + 99 others); Mon, 19 Jul 2021 04:34:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59700 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235150AbhGSIel (ORCPT ); Mon, 19 Jul 2021 04:34:41 -0400 Received: from mail-lf1-x12b.google.com (mail-lf1-x12b.google.com [IPv6:2a00:1450:4864:20::12b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AB091C061574 for ; Mon, 19 Jul 2021 01:16:24 -0700 (PDT) Received: by mail-lf1-x12b.google.com with SMTP id b26so29067773lfo.4 for ; Mon, 19 Jul 2021 02:15:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloudflare.com; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=1k48KCcy3y6vIAdtOHWsq+O6aGKfc6p+OZyi1Vzkxdg=; b=Q1WubCmk3c2vuY8XLCwB23Tv9+DQkdc9eL4l+yn1NMuxC3edJzvGuvOWVENS38btuO ohj0f8EU306raJZ+26SBlCyu60z8Y2kC50BBDWUUyEyJQRI7HhQsDqKqddH0sSoaSR2Q 9u2TsPp4vLtJa8iObupi8nnCPkV819nrXkUNU= 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=1k48KCcy3y6vIAdtOHWsq+O6aGKfc6p+OZyi1Vzkxdg=; b=rHkFdTYlbbVEI3FzAng3mzvNEgGO8Jugods2MxKsTCB3dJOXvTr9OKPghIbwHfXQoC p7Zm9jHYOiKAz11fHyphI6VHMHidGIMWOSTXsUT15cO3kj45zmJaepqaeNi8efVkBoHI eoPQFIxxKzo1EeA3QUHJ0ZOYsr9CXnnISMm2vDWivSzQm0ErXKd2lJ+cXDKO721uKhXq vOTuO3IAYzWaSodmbMKNOVxxtgOJo3pwigq+EfkKx9FnNIFj0GlonDNPkAV6NexbxV5Y aaiCgbhYD0YFoXfkkFLihDQJp4Q1b+uLKSvSh6ALUi03vNv1YRGSfeZo+9faUlVt8khS hsPw== X-Gm-Message-State: AOAM5305ryVxFIY0vHS8P0ZJSTmKrFSiKVF7e4M5KE139XfCL9AFKNG6 dPmDAtLk2yeotuQ9FQgfuxt9wDqyjaiC+OOqNcn93TAgOEE= X-Received: by 2002:a05:6512:22d3:: with SMTP id g19mr12501835lfu.171.1626684540314; Mon, 19 Jul 2021 01:49:00 -0700 (PDT) MIME-Version: 1.0 References: <20210716100452.113652-1-lmb@cloudflare.com> <7854fbef-8ea5-5396-6369-99eef1dcccaa@iogearbox.net> In-Reply-To: <7854fbef-8ea5-5396-6369-99eef1dcccaa@iogearbox.net> From: Lorenz Bauer Date: Mon, 19 Jul 2021 09:48:48 +0100 Message-ID: Subject: Re: [PATCH bpf] bpf: fix OOB read when printing XDP link fdinfo To: Daniel Borkmann Cc: Andrii Nakryiko , Alexei Starovoitov , Andrii Nakryiko , "David S. Miller" , Jakub Kicinski , Jesper Dangaard Brouer , John Fastabend , kernel-team , Andrii Nakryiko , Networking , bpf , open list Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, 16 Jul 2021 at 22:01, Daniel Borkmann wrote: > > On 7/16/21 10:43 PM, Andrii Nakryiko wrote: > > On Fri, Jul 16, 2021 at 3:05 AM Lorenz Bauer wrote: > >> > >> We got the following UBSAN report on one of our testing machines: > >> > >> ================================================================================ > >> UBSAN: array-index-out-of-bounds in kernel/bpf/syscall.c:2389:24 > >> index 6 is out of range for type 'char *[6]' > >> CPU: 43 PID: 930921 Comm: systemd-coredum Tainted: G O 5.10.48-cloudflare-kasan-2021.7.0 #1 > >> Hardware name: > >> Call Trace: > >> dump_stack+0x7d/0xa3 > >> ubsan_epilogue+0x5/0x40 > >> __ubsan_handle_out_of_bounds.cold+0x43/0x48 > >> ? seq_printf+0x17d/0x250 > >> bpf_link_show_fdinfo+0x329/0x380 > >> ? bpf_map_value_size+0xe0/0xe0 > >> ? put_files_struct+0x20/0x2d0 > >> ? __kasan_kmalloc.constprop.0+0xc2/0xd0 > >> seq_show+0x3f7/0x540 > >> seq_read_iter+0x3f8/0x1040 > >> seq_read+0x329/0x500 > >> ? seq_read_iter+0x1040/0x1040 > >> ? __fsnotify_parent+0x80/0x820 > >> ? __fsnotify_update_child_dentry_flags+0x380/0x380 > >> vfs_read+0x123/0x460 > >> ksys_read+0xed/0x1c0 > >> ? __x64_sys_pwrite64+0x1f0/0x1f0 > >> do_syscall_64+0x33/0x40 > >> entry_SYSCALL_64_after_hwframe+0x44/0xa9 > >> > >> ================================================================================ > >> ================================================================================ > >> UBSAN: object-size-mismatch in kernel/bpf/syscall.c:2384:2 > >> > >> From the report, we can infer that some array access in bpf_link_show_fdinfo at index 6 > >> is out of bounds. The obvious candidate is bpf_link_type_strs[BPF_LINK_TYPE_XDP] with > >> BPF_LINK_TYPE_XDP == 6. It turns out that BPF_LINK_TYPE_XDP is missing from bpf_types.h > >> and therefore doesn't have an entry in bpf_link_type_strs: > >> > >> pos: 0 > >> flags: 02000000 > >> mnt_id: 13 > >> link_type: (null) > >> link_id: 4 > >> prog_tag: bcf7977d3b93787c > >> prog_id: 4 > >> ifindex: 1 > >> > >> Fixes: aa8d3a716b59 ("bpf, xdp: Add bpf_link-based XDP attachment API") > >> Signed-off-by: Lorenz Bauer > >> --- > > > > Well, oops. Thanks for the fix! > > > > Acked-by: Andrii Nakryiko > > > > It would be great to have a compilation error for something like this. > > I wonder if we can do something to detect this going forward? > > > >> include/linux/bpf_types.h | 1 + > >> 1 file changed, 1 insertion(+) > >> > >> diff --git a/include/linux/bpf_types.h b/include/linux/bpf_types.h > >> index a9db1eae6796..be95f2722ad9 100644 > >> --- a/include/linux/bpf_types.h > >> +++ b/include/linux/bpf_types.h > >> @@ -135,3 +135,4 @@ BPF_LINK_TYPE(BPF_LINK_TYPE_ITER, iter) > >> #ifdef CONFIG_NET > >> BPF_LINK_TYPE(BPF_LINK_TYPE_NETNS, netns) > >> #endif > >> +BPF_LINK_TYPE(BPF_LINK_TYPE_XDP, xdp) > > Lorenz, does this compile when you don't have CONFIG_NET configured? I would assume > this needs to go right below the netns one depending on CONFIG_NET.. at least the > bpf_xdp_link_lops are in net/core/dev.c which is only built under CONFIG_NET. It does compile, since the only use of the macro is to stringify a link type for fdinfo. I'll move it into CONFIG_NET to be consistent with netdev though. -- Lorenz Bauer | Systems Engineer 6th Floor, County Hall/The Riverside Building, SE1 7PB, UK www.cloudflare.com