Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp198247pxb; Wed, 20 Apr 2022 20:03:05 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwTWu5d/LdXaKURPZOeoYls2LWDoUpYfo1mqKkrr8WFOkAn7XKvJs3ieYkQqVkGgldFqnhV X-Received: by 2002:aa7:d047:0:b0:41d:57cf:d588 with SMTP id n7-20020aa7d047000000b0041d57cfd588mr26347670edo.172.1650510185458; Wed, 20 Apr 2022 20:03:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1650510185; cv=none; d=google.com; s=arc-20160816; b=PbvoAU4lL5Id1Iehca+ry10Hy5IbVvVBWtFK31yONGyitBQFR/GjAfvI3P/Y5Prp1x w3RkM+MOPkgHNL1Q6Xo0MYtJ9sT2q/FNgH9t/Pst15x5p/wvk0rP6UijUN/GFSZKMYcj PCzsxTSMucWfrYzcGI7NsErPnoU4NuJobxoFFKYe/v5a/TPBmjasYnDCF90EeE84jZFT Qm+F90cBy9TVXcLTmSmOyPLNjM6hjnzHnnO0OyKaCsPX0+oA1Pl41X8ZhB8lNHoHaDmx c96vh+YwO7jQZNk1YwXXzbKfgxaNg/tk9Z2NIg0mU6oxq3YPWuDQAc9iFuB5oiW5O3mO J1Qg== 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=7wzRThF5HaMonZLDTFgx0pibWyxVIQs6oTZK8LgP7Rk=; b=mrEmH/hOYNvUOrB2/4hcxhpD1HNlf3qHwEKzx0HCqHW/hJBUkrntnuKyA4GKVrIa+d jz+WOTf2/225+1z5tO1wOmpr5Y+heVZVhJ7+ko/CygpRhyL/ZPn+FWqxEYYCXekgy0Ur AMeG1KTGfcvThhCEKI4Uqlz9MaLSEHRnQEmJfFzmaD84R8iAUi+v3cNCjsn5+rVveZ7A /8STxf6Hd365bqH/uSnG3oiustsMIKgj7ZMvA2rU1sWC9GLiGOr/Ho1njQSBUtOP+yXK glfWOWJ6LyR6mVBDvY+wKe6lQmIUZV4qpyclZqU/q8jWc4M4QtIo6s9mc8184Tkxrign kfqg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=DIRM9JAA; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id dv20-20020a170906b81400b006e7fd7464f6si2865622ejb.113.2022.04.20.20.02.23; Wed, 20 Apr 2022 20:03:05 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-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=@gmail.com header.s=20210112 header.b=DIRM9JAA; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S1380888AbiDTRK2 (ORCPT + 99 others); Wed, 20 Apr 2022 13:10:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56546 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1380890AbiDTRK0 (ORCPT ); Wed, 20 Apr 2022 13:10:26 -0400 Received: from mail-vs1-xe2b.google.com (mail-vs1-xe2b.google.com [IPv6:2607:f8b0:4864:20::e2b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6F899A1AC; Wed, 20 Apr 2022 10:07:39 -0700 (PDT) Received: by mail-vs1-xe2b.google.com with SMTP id r1so2130945vsi.12; Wed, 20 Apr 2022 10:07:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=7wzRThF5HaMonZLDTFgx0pibWyxVIQs6oTZK8LgP7Rk=; b=DIRM9JAA75v6UB8C3NSUnFYtbGPd/6hGhEKGkyI+Qzs60Io3TIPm88HkB1ucaEZpUO tpM93Zbvl4jgUFSDoE4EoTeK/OEylNiGBJXMwM7V+qQDeF1wYJ7OoFK0oty62mH9NDuz nG8q5XHmMEiRTwqLUHGRn/xscX49csCdN5yb2XWU6CJaStkPgBBY2zZj3pc2qa7BmrB2 JLhYu1voLabqjhPlEX5dEdbIrCkLwOekaD0SZh1jcGX20ucW3tZGLk8RK8icG4S1lVVz eRKqffFRMSZBeqmHCRtpIQ1PR7Hj4mN8iS96oc8YcGGNnXfRMwsVdtZDZtaSFVIdgCI9 L8aQ== 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; bh=7wzRThF5HaMonZLDTFgx0pibWyxVIQs6oTZK8LgP7Rk=; b=HXCEvX3FfW/eRPlZmFcjejoV1b/es2/JRqNpGWTF1JYVBlyhZ8swpIf6NJYFAI2xbF 6kNvgB/5+lG4RLWMm6LFmd9iUEACFtg9hUC9921RZar9YhWEWSCERzJVbkiTNsnNwPlx Rix2cPsSjOQflM65Ia5comUobDE+VqvXU++QTD7w6Aduq6UBFviyv1NPFcvDe7ViII1B ZD79ihfksAnLJ6aOe2vAvzOpemDGlzS9EqvweFKzRon07HDGSVTVXdgkAw1p0v3Au49T SrLV7CpGouCKrQ398mB7ym+ihf4YOx5U/6J383RbMJjegi2B4/Rjtpotqg1cfQTIGZEQ Md9g== X-Gm-Message-State: AOAM531aL6sbeLMBdh7Qu20UATMaDQSnZQ3aLEHzxfdtTpTg83PXXPJb ut1auZx7LYaKa5jYNwb4zaJAXHlmuODTi+Xt2dE= X-Received: by 2002:a67:f80b:0:b0:32a:17d6:7fb2 with SMTP id l11-20020a67f80b000000b0032a17d67fb2mr6249988vso.40.1650474458618; Wed, 20 Apr 2022 10:07:38 -0700 (PDT) MIME-Version: 1.0 References: <20220415141355.4329-1-tadeusz.struk@linaro.org> In-Reply-To: <20220415141355.4329-1-tadeusz.struk@linaro.org> From: Andrii Nakryiko Date: Wed, 20 Apr 2022 10:07:27 -0700 Message-ID: Subject: Re: [PATCH v2] bpf: Fix KASAN use-after-free Read in compute_effective_progs To: Tadeusz Struk Cc: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Networking , bpf , linux- stable , open list , syzbot+f264bffdfbd5614f3bb2@syzkaller.appspotmail.com Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=ham 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-kernel@vger.kernel.org On Fri, Apr 15, 2022 at 7:14 AM Tadeusz Struk wrote: > > Syzbot found a Use After Free bug in compute_effective_progs(). > The reproducer creates a number of BPF links, and causes a fault > injected alloc to fail, while calling bpf_link_detach on them. > Link detach triggers the link to be freed by bpf_link_free(), > which calls __cgroup_bpf_detach() and update_effective_progs(). > If the memory allocation in this function fails, the function restores > the pointer to the bpf_cgroup_link on the cgroup list, but the memory > gets freed just after it returns. After this, every subsequent call to > update_effective_progs() causes this already deallocated pointer to be > dereferenced in prog_list_length(), and triggers KASAN UAF error. > > To fix this issue don't preserve the pointer to the prog or link in the > list, but remove it and rearrange the effective table without > shrinking it. The subsequent call to __cgroup_bpf_detach() or > __cgroup_bpf_detach() will correct it. > > Cc: "Alexei Starovoitov" > Cc: "Daniel Borkmann" > Cc: "Andrii Nakryiko" > Cc: "Martin KaFai Lau" > Cc: "Song Liu" > Cc: "Yonghong Song" > Cc: "John Fastabend" > Cc: "KP Singh" > Cc: > Cc: > Cc: > Cc: > > Link: https://syzkaller.appspot.com/bug?id=8ebf179a95c2a2670f7cf1ba62429ec044369db4 > Fixes: af6eea57437a ("bpf: Implement bpf_link-based cgroup BPF program attachment") > Reported-by: > Signed-off-by: Tadeusz Struk > --- > v2: Add a fall back path that removes a prog from the effective progs > table in case detach fails to allocate memory in compute_effective_progs(). > --- > kernel/bpf/cgroup.c | 55 +++++++++++++++++++++++++++++++++++++++------ > 1 file changed, 48 insertions(+), 7 deletions(-) > > diff --git a/kernel/bpf/cgroup.c b/kernel/bpf/cgroup.c > index 128028efda64..5a64cece09f3 100644 > --- a/kernel/bpf/cgroup.c > +++ b/kernel/bpf/cgroup.c > @@ -723,10 +723,8 @@ static int __cgroup_bpf_detach(struct cgroup *cgrp, struct bpf_prog *prog, > pl->link = NULL; > > err = update_effective_progs(cgrp, atype); > - if (err) > - goto cleanup; > > - /* now can actually delete it from this cgroup list */ > + /* now can delete it from this cgroup list */ > list_del(&pl->node); > kfree(pl); > if (list_empty(progs)) > @@ -735,12 +733,55 @@ static int __cgroup_bpf_detach(struct cgroup *cgrp, struct bpf_prog *prog, > if (old_prog) > bpf_prog_put(old_prog); > static_branch_dec(&cgroup_bpf_enabled_key[atype]); > - return 0; > + > + if (!err) > + return 0; > > cleanup: > - /* restore back prog or link */ > - pl->prog = old_prog; > - pl->link = link; > + /* > + * If compute_effective_progs failed with -ENOMEM, i.e. alloc for > + * cgrp->bpf.inactive table failed, we can recover by removing > + * the detached prog from effective table and rearranging it. > + */ > + if (err == -ENOMEM) { > + struct bpf_prog_array_item *item; > + struct bpf_prog *prog_tmp, *prog_detach, *prog_last; > + struct bpf_prog_array *array; > + int index = 0, index_detach = -1; > + > + array = cgrp->bpf.effective[atype]; > + item = &array->items[0]; > + > + if (prog) > + prog_detach = prog; > + else > + prog_detach = link->link.prog; > + > + if (!prog_detach) > + return -EINVAL; > + > + while ((prog_tmp = READ_ONCE(item->prog))) { > + if (prog_tmp == prog_detach) > + index_detach = index; > + item++; > + index++; > + prog_last = prog_tmp; > + } > + > + /* Check if we found what's needed for removing the prog */ > + if (index_detach == -1 || index_detach == index-1) > + return -EINVAL; > + > + /* Remove the last program in the array */ > + if (bpf_prog_array_delete_safe_at(array, index-1)) > + return -EINVAL; > + > + /* and update the detached with the last just removed */ > + if (bpf_prog_array_update_at(array, index_detach, prog_last)) > + return -EINVAL; > + > + err = 0; > + } There are a bunch of problems with this implementation. 1. We should do this fallback right after update_effective_progs() returns error, before we get to list_del(&pl->node) and subsequent code that does some additional things (like clearing flags and stuff). This additional code needs to run even if update_effective_progs() fails. So I suggest to extract the logic of removing program from effective prog arrays into a helper function and doing err = update_effective_progs(...); if (err) purge_effective_progs(); where purge_effective_progs() will be the logic you are adding. And it will be void function because it can't fail. 2. We have to update not just cgrp->bpf.effective array, but all the descendants' lists as well. See what update_effective_progs() is doing, it has css_for_each_descendant_pre() iteration. You need to do it here as well. But instead of doing compute_effective_progs() which allocates a new copy of an array we'll need to update existing array in place. 3. Not clear why you need to do both bpf_prog_array_delete_safe_at() and bpf_prog_array_update_at(), isn't delete_safe_at() enought? > return err; > } > > -- > 2.35.1 >