Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp1500382iog; Tue, 14 Jun 2022 07:18:34 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxWU680pNF/7Ts1fqJ3p9rWiVPepzMM4m55qArgzV3rpSbY45w4f/l3HEL8Zab2NZGowKc7 X-Received: by 2002:a05:6402:500b:b0:431:78d0:bf9d with SMTP id p11-20020a056402500b00b0043178d0bf9dmr6284475eda.184.1655216314555; Tue, 14 Jun 2022 07:18:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655216314; cv=none; d=google.com; s=arc-20160816; b=gA8oumKsO03p99EyFbf3pgSB9oN8hgirpjmDOb1EuAXlbYFfBGUVSSmhmGFyO1n3sc +ZSYaUlSrRaTw2wAqq965FjD8pEiyR9buB3ROP7sTyrSwiBI/bS8H2anowGnF0LSzIN2 Hfne3NtLsJvtD8xVihqE2hIh261GIt0X3wI8eYbywFmv9mN8PlKWSmP4QpmlGD3ZMDQH MvMK0FWb0erFgoUMJCRv7/m5QNViMI4W3jhqhc7KVdGpCM5rsw8RtXy7snNhyIvwh225 Q1BSt5EDSZFGGiX8N1ye7HW2W9HUKZOq29LtUtzDJJL+tZG7an46CmZDm9MIDGeP8vS/ OCFQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=ykJCkSDmaQwuRjSNMU1SJFHZmPWM0AX/lpikFCQOxUM=; b=FwSb9G5bd/Z4+YZJ6nu1OjOkafZ4RJ20Aw7CY1Cvmotpk1d0Xl3tgUACFU5wP5Q1fI Hbqjw8beyQtO/OfAEt++0Th40HG2ei9rSNlgGRyj9SY+sk77QWLbqg/AT1oQPD/EEpiN TcunzH4ZVms9TMKeB2vOentGfGyvA3aw9seF4NohwcPMR1Kk8/4VNK/hqp/huV8WoZRB wBTHkOz2lpysHxeOGlXSdtSXOHGrUBqxQkpleJo6vUWa83l6J7McRatVwhNQZcGRs6cV 3QUAoZZHwWp86fR5PIE5HLN2KPr9TD3hZ+s2CYUx7F+wYk3nNeLF/A6s7nLI8ZFhY1Y7 0LHQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=gxxmtVPd; spf=pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ee29-20020a056402291d00b0043141186b7dsi10804402edb.225.2022.06.14.07.17.50; Tue, 14 Jun 2022 07:18:34 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=gxxmtVPd; spf=pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244578AbiFNORZ (ORCPT + 99 others); Tue, 14 Jun 2022 10:17:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59872 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243055AbiFNORU (ORCPT ); Tue, 14 Jun 2022 10:17:20 -0400 Received: from mail-yb1-xb36.google.com (mail-yb1-xb36.google.com [IPv6:2607:f8b0:4864:20::b36]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9216D38D84 for ; Tue, 14 Jun 2022 07:17:18 -0700 (PDT) Received: by mail-yb1-xb36.google.com with SMTP id p13so15427839ybm.1 for ; Tue, 14 Jun 2022 07:17:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=ykJCkSDmaQwuRjSNMU1SJFHZmPWM0AX/lpikFCQOxUM=; b=gxxmtVPdfld7TYT/I1hV+Lvzk5/ov3SJdzPCt5lLKl/8Ky9y93Vwv61PLScLaZ2qPj F1LTPxOhnu9ufJ5MC2A1Vnzrje1RRYDcFo7VIbhIfbUJy4S4n1iLwKJMWYfBY0dCD0za A2MFcEglmZ837Ge9Ofbp9npK75tDWDk44O8Pe5eCnkA8jvGTYQ5qS6Fs9LbSNLbCRFMU iBffFODrbAKYHUR9see0yLm10CfSpiMI4OoYna0VVWCt/+yWzWKHRkXdiMTS5895VCfw eW62mECaVI0HHZcu96+ekHtvWdM1d3iftQ9M9Ki8ghZ1zXJwTGSFmhmQy8ebuNQ0FIRt GBfQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=ykJCkSDmaQwuRjSNMU1SJFHZmPWM0AX/lpikFCQOxUM=; b=22WxnxL/KWb8X9a2FHlUl2I8Dpdcy7Er+mnqRwHlIrqXR7G4B89au1qV0bt+w/yll6 rxYJ6QPEA3ZsP+abs088pwvzePX5Wtkq4iqSA5zwViY4zWQJL957qSIJABEIiVLI/1ZZ 6vHoyWR2aZ3XY6l/T67qjw7/KUwu3uy+tZfrvNGqLr7EY844VSKqj4m2y5Ye7f7/69XI UH15kH/0BHIfIhUFt9ILuVBO1BeBotNvfBUcDB02LjK2b5Fr//d8PxWvwN6J0+oosil7 0l3+hxP29wRWTz3xKmbXieO0y86cxzKML+ivNm7f+jkIeX4OIwOf0m2CZsY2fbVjOH2A QbeA== X-Gm-Message-State: AJIora+cqpP1PEPaub9bomwrdHu6sz/zLgiPkrOaGvFrpyRmZ2PWCGyY nCNu6iuFFSLZrkuWF+C9yxqfmWOcEfNOYanx+l15KA== X-Received: by 2002:a05:6902:a:b0:65c:b38e:6d9f with SMTP id l10-20020a056902000a00b0065cb38e6d9fmr5472805ybh.36.1655216237200; Tue, 14 Jun 2022 07:17:17 -0700 (PDT) MIME-Version: 1.0 References: <20220610110749.110881-1-soenke.huster@eknoes.de> <9f214837-dc68-ef1a-0199-27d6af582115@eknoes.de> <22131ee2-914c-3aad-d2c3-f340ad0c8ad0@eknoes.de> In-Reply-To: <22131ee2-914c-3aad-d2c3-f340ad0c8ad0@eknoes.de> From: Eric Dumazet Date: Tue, 14 Jun 2022 07:17:06 -0700 Message-ID: Subject: Re: [PATCH v2] Bluetooth: RFCOMM: Use skb_trim to trim checksum To: =?UTF-8?Q?S=C3=B6nke_Huster?= Cc: Marcel Holtmann , Johan Hedberg , Luiz Augusto von Dentz , "David S. Miller" , Jakub Kicinski , Paolo Abeni , linux-bluetooth@vger.kernel.org, netdev , LKML Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org On Tue, Jun 14, 2022 at 6:42 AM S=C3=B6nke Huster = wrote: > > Hi Eric, > > On 10.06.22 18:55, Eric Dumazet wrote: > > On Fri, Jun 10, 2022 at 8:35 AM S=C3=B6nke Huster wrote: > >> > >> Hi Eric, > >> > >> On 10.06.22 15:59, Eric Dumazet wrote: > >>> On Fri, Jun 10, 2022 at 4:08 AM Soenke Huster wrote: > >>>> > >>>> As skb->tail might be zero, it can underflow. This leads to a page > >>>> fault: skb_tail_pointer simply adds skb->tail (which is now MAX_UINT= ) > >>>> to skb->head. > >>>> > >>>> BUG: unable to handle page fault for address: ffffed1021de29ff > >>>> #PF: supervisor read access in kernel mode > >>>> #PF: error_code(0x0000) - not-present page > >>>> RIP: 0010:rfcomm_run+0x831/0x4040 (net/bluetooth/rfcomm/core.c:1= 751) > >>>> > >>>> By using skb_trim instead of the direct manipulation, skb->tail > >>>> is reset. Thus, the correct pointer to the checksum is used. > >>>> > >>>> Signed-off-by: Soenke Huster > >>>> --- > >>>> v2: Clarified how the bug triggers, minimize code change > >>>> > >>>> net/bluetooth/rfcomm/core.c | 2 +- > >>>> 1 file changed, 1 insertion(+), 1 deletion(-) > >>>> > >>>> diff --git a/net/bluetooth/rfcomm/core.c b/net/bluetooth/rfcomm/core= .c > >>>> index 7324764384b6..443b55edb3ab 100644 > >>>> --- a/net/bluetooth/rfcomm/core.c > >>>> +++ b/net/bluetooth/rfcomm/core.c > >>>> @@ -1747,7 +1747,7 @@ static struct rfcomm_session *rfcomm_recv_fram= e(struct rfcomm_session *s, > >>>> type =3D __get_type(hdr->ctrl); > >>>> > >>>> /* Trim FCS */ > >>>> - skb->len--; skb->tail--; > >>>> + skb_trim(skb, skb->len - 1); > >>>> fcs =3D *(u8 *)skb_tail_pointer(skb); > >>>> > >>>> if (__check_fcs(skb->data, type, fcs)) { > >>>> -- > >>>> 2.36.1 > >>>> > >>> > >>> Again, I do not see how skb->tail could possibly zero at this point. > >>> > >>> If it was, skb with illegal layout has been queued in the first place= , > >>> we need to fix the producer, not the consumer. > >>> > >> > >> Sorry, I thought that might be a right place as there is not much code= in the kernel > >> that manipulates ->tail directly. > >> > >>> A driver missed an skb_put() perhaps. > >>> > >> > >> I am using the (I guess quite unused) virtio_bt driver, and figured ou= t that the following > >> fixes the bug: > >> > >> --- a/drivers/bluetooth/virtio_bt.c > >> +++ b/drivers/bluetooth/virtio_bt.c > >> @@ -219,7 +219,7 @@ static void virtbt_rx_work(struct work_struct *wor= k) > >> if (!skb) > >> return; > >> > >> - skb->len =3D len; > >> + skb_put(skb, len); > > > > Removing skb->len=3Dlen seems about right. > > But skb_put() should be done earlier. > > > > We are approaching the skb producer :) > > > > Now you have to find/check who added this illegal skb in the virt queue= . > > > > Maybe virtbt_add_inbuf() ? > > I think here, the length of the skb can't really be known - an empty SKB = is put into > the virtqueue, and then filled with data in the device, which is implemen= ted in a Hypervisor. > Maybe my implementation of that device might then be wrong, on the other = hand I am pretty > sure the driver should be the one that sets the length of the skb. But th= e driver only > knows it in virtbt_rx_work, as it learns the size of the added buffer the= re for the first time. > > > > > Also there is kernel info leak I think. > > > > I think your are right! If this patch in drivers/bluetooth/virtio_bt.c fixes the issue, please submit a formal patch. You can take ownership of it, of course. If not, more investigation is needed on your side ;) Thanks ! > > > diff --git a/drivers/bluetooth/virtio_bt.c b/drivers/bluetooth/virtio_b= t.c > > index 67c21263f9e0f250f0719b8e7f1fe15b0eba5ee0..c9b832c447ee451f027430b= 284d7bb246f6ecb24 > > 100644 > > --- a/drivers/bluetooth/virtio_bt.c > > +++ b/drivers/bluetooth/virtio_bt.c > > @@ -37,6 +37,9 @@ static int virtbt_add_inbuf(struct virtio_bluetooth *= vbt) > > if (!skb) > > return -ENOMEM; > > > > + skb_put(skb, 1000); > > + memset(skb->data, 0, 1000); > > + > > sg_init_one(sg, skb->data, 1000); > > > > err =3D virtqueue_add_inbuf(vq, sg, 1, skb, GFP_KERNEL); > > > > > >> virtbt_rx_handle(vbt, skb); > >> > >> if (virtbt_add_inbuf(vbt) < 0) > >> > >> I guess this is the root cause? I just used Bluetooth for a while in t= he VM > >> and no error occurred, everything worked fine. > >> > >>> Can you please dump the skb here ? > >>> > >>> diff --git a/net/bluetooth/rfcomm/core.c b/net/bluetooth/rfcomm/core.= c > >>> index 7324764384b6773074032ad671777bf86bd3360e..358ccb4fe7214aea0bb40= 84188c7658316fe0ff7 > >>> 100644 > >>> --- a/net/bluetooth/rfcomm/core.c > >>> +++ b/net/bluetooth/rfcomm/core.c > >>> @@ -1746,6 +1746,11 @@ static struct rfcomm_session > >>> *rfcomm_recv_frame(struct rfcomm_session *s, > >>> dlci =3D __get_dlci(hdr->addr); > >>> type =3D __get_type(hdr->ctrl); > >>> > >>> + if (!skb->tail) { > >>> + DO_ONCE_LITE(skb_dump(KERN_ERR, skb, false)); > >>> + kfree_skb(skb); > >>> + return s; > >>> + } > >>> /* Trim FCS */ > >>> skb->len--; skb->tail--; > >>> fcs =3D *(u8 *)skb_tail_pointer(skb); > >> > >> If it might still help: > >> > >> skb len=3D4 headroom=3D9 headlen=3D4 tailroom=3D1728 > >> mac=3D(-1,-1) net=3D(0,-1) trans=3D-1 > >> shinfo(txflags=3D0 nr_frags=3D0 gso(size=3D0 type=3D0 segs=3D0)) > >> csum(0x0 ip_summed=3D0 complete_sw=3D0 valid=3D0 level=3D0) > >> hash(0x0 sw=3D0 l4=3D0) proto=3D0x0000 pkttype=3D0 iif=3D0 > >> skb linear: 00000000: 03 3f 01 1c > >>