Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp1517962pxb; Thu, 14 Apr 2022 07:53:28 -0700 (PDT) X-Google-Smtp-Source: ABdhPJytNa8CvaCFYNj1pXRpBZvMTBkPKxhJ6DM0IJf7ztNIwOhztROqYeEV6hoBGCXMxyJLGQ0G X-Received: by 2002:a05:6a00:174c:b0:505:a513:666a with SMTP id j12-20020a056a00174c00b00505a513666amr4335005pfc.50.1649948008532; Thu, 14 Apr 2022 07:53:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649948008; cv=none; d=google.com; s=arc-20160816; b=omjBUPYUhDILhgiP3RB6Itb1lBwZC9t02SqRGyLv93iPFi4jrSNZiZRWOyQ3/zRDo6 /MP+p/1eNJoxJiI+CVIemD4MkdXOlyTkzxcAQMnFSxtqQIVDn4Yx2qb1b4BRqSCqUY5y JES8lG4IGN+TfoAppopMpddHqK5Lgo0OQi45jpDEaZwzVTLGD82mcPf41L4mt3lf0aWM XpYUVy1WTIiMiEajCHkac84HRNeWp3piNLekPozNg7uArrzsKxoAN03hZTNQe5XB5UWH C6n1+O4N2+2gv7lxzIsqdgoiLRK5bsclMhvW/D3q6SzF81Zx9/p/2tTEzN/PrXj+cJuy OcmA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:subject :from:references:cc:to:content-language:user-agent:mime-version:date :message-id:dkim-signature; bh=kujL7p0GwLveBSEOqYGp7ryLlCB5QH+7/CE7iGZN0TQ=; b=UwW9ohEKpEP+SWdrlaqTyjl0exrnad+QRTmU2/6pm5QNIoRiSS1kuMHB8EExJTfUTw Oaje0ZZwTg5/OajNZp8lg6uMp42NB19Kd1FM0qFPatC6B0qRN9S5vR5h08QEoH0GIkXH 4Ji7JJPJt52ZiujcSxzpDzjarJWZRQOTzyKtWUfoz/Jt9j4MhqhR4fWMfvS3OW2spfYQ DKP5KEXgBQcJmewHEhtQ+q+2AiZ6Hrauhwz2QnvycHPtAxJaKTLMmsU0IY6GohWmy3GN wDmRr7g3OoE/t4s+ia9qNawRdcWQtL3lPcwsbLOr2XDJ09hoFAdQsbdZ+/yvvOsnT+PU HF8w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=GExPk5AT; 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=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id t5-20020a170902e84500b00153b2d164cdsi7525126plg.213.2022.04.14.07.53.12; Thu, 14 Apr 2022 07:53:28 -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=@linaro.org header.s=google header.b=GExPk5AT; 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=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233849AbiDMRa0 (ORCPT + 99 others); Wed, 13 Apr 2022 13:30:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36740 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233288AbiDMRaY (ORCPT ); Wed, 13 Apr 2022 13:30:24 -0400 Received: from mail-pj1-x102f.google.com (mail-pj1-x102f.google.com [IPv6:2607:f8b0:4864:20::102f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1ABC36C1C8 for ; Wed, 13 Apr 2022 10:28:02 -0700 (PDT) Received: by mail-pj1-x102f.google.com with SMTP id mp16-20020a17090b191000b001cb5efbcab6so6884879pjb.4 for ; Wed, 13 Apr 2022 10:28:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=message-id:date:mime-version:user-agent:content-language:to:cc :references:from:subject:in-reply-to:content-transfer-encoding; bh=kujL7p0GwLveBSEOqYGp7ryLlCB5QH+7/CE7iGZN0TQ=; b=GExPk5ATZICbkE5mIW9Lihjs5TT+H85wuaaWrwsxiwX2vOfbDPkzTueBzSpeycJd28 B3cq90bS3TBuEciCNFfHlw0vlLAwZBqOXldlrsqJo1LeqoU8kewJoTI8DDzsjTeQ1EwN eVxw5viL/HADnH+iBGy/k9tHGrEkQ0FQaZy7naRprjo/hbp6yuq0HExm4t0VArNiACFe z1jO7mtxqcyIVXfsjofuN5z1Un0L0wfErSduSroSulRsf721KEVkubfsvw/WagFApY5m Y4IHRl+jmzDDhz7G9IvXHlpC/PmIQ2ToDOiCe5SHRuQJt7L8af1CZ4I4kfkJtMZO6pcf xJGw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent :content-language:to:cc:references:from:subject:in-reply-to :content-transfer-encoding; bh=kujL7p0GwLveBSEOqYGp7ryLlCB5QH+7/CE7iGZN0TQ=; b=oFvZie070N5n2eaEbY0QOPwZix/ILuzJPLbLkmBiauaTw2+lGSfabLuaWCagpcO4PB I1lSvKe7wVL51xgBYKlK4xvXIOPT9mFjD9AzuWBDyesSwmFWmyZ64jmMQuRyBD5n0Sl7 jX0X1tL+AQDXZhgYAfXbHhDiVdSV6n4g9krLXCfDkdMY8r0qW83oEzEb4xl/8oB9TTb7 XVVFRxkkcaTXjJYAPB6lptdlAHxFnXwzX/HaZ3mZP/23y/eoq54c81Q6BWY6pGysuitZ ZUzGt98QrPRCMCCe81gUzhbTwhdBmdnTfhvbWj/Rawl1vf+ziZIbbf0eRBbAiCb61ovp vmfQ== X-Gm-Message-State: AOAM530P/L58Y4KWs5BOZt+Nm4BPrsdA/bX+FoEpUhXkR00u7xn4yzTf a1Wdh5YFoKJam/pwicanoGW70w== X-Received: by 2002:a17:90b:3a91:b0:1cb:955d:905c with SMTP id om17-20020a17090b3a9100b001cb955d905cmr12094736pjb.164.1649870881553; Wed, 13 Apr 2022 10:28:01 -0700 (PDT) Received: from [192.168.254.17] ([50.39.160.154]) by smtp.gmail.com with ESMTPSA id k27-20020aa79d1b000000b005059ad6b943sm16367677pfp.166.2022.04.13.10.28.00 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 13 Apr 2022 10:28:01 -0700 (PDT) Message-ID: Date: Wed, 13 Apr 2022 10:28:00 -0700 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.7.0 Content-Language: en-US To: Andrii Nakryiko Cc: bpf , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Networking , linux- stable , open list , syzbot+f264bffdfbd5614f3bb2@syzkaller.appspotmail.com References: <20220405170356.43128-1-tadeusz.struk@linaro.org> From: Tadeusz Struk Subject: Re: [PATCH] bpf: Fix KASAN use-after-free Read in compute_effective_progs In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-3.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE 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-kernel@vger.kernel.org Hi Andrii, On 4/12/22 21:34, Andrii Nakryiko wrote: > On Tue, Apr 5, 2022 at 10:04 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 don't preserve the pointer to the link on the cgroup list >> in __cgroup_bpf_detach(), but proceed with the cleanup and retry calling >> update_effective_progs() again afterwards. > I think it's still problematic. BPF link might have been the last one > that holds bpf_prog's refcnt, so when link is put, its prog can stay > there in effective_progs array(s) and will cause use-after-free > anyways. > > It would be best to make sure that detach never fails. On detach > effective prog array can only shrink, so even if > update_effective_progs() fails to allocate memory, we should be able > to iterate and just replace that prog with NULL, as a fallback > strategy. it would be ideal if detach would never fail, but it would require some kind of prealloc, on attach maybe? Another option would be to minimize the probability of failing by sending it gfp_t flags (GFP_NOIO | GFP_NOFS | __GFP_NOFAIL)? Detach can really only fail if the kzalloc in compute_effective_progs() fails so maybe doing something like bellow would help: diff --git a/kernel/bpf/cgroup.c b/kernel/bpf/cgroup.c index 128028efda64..5a47740c317b 100644 --- a/kernel/bpf/cgroup.c +++ b/kernel/bpf/cgroup.c @@ -226,7 +226,8 @@ static bool hierarchy_allows_attach(struct cgroup *cgrp, */ static int compute_effective_progs(struct cgroup *cgrp, enum cgroup_bpf_attach_type atype, - struct bpf_prog_array **array) + struct bpf_prog_array **array, + gfp_t flags) { struct bpf_prog_array_item *item; struct bpf_prog_array *progs; @@ -241,7 +242,7 @@ static int compute_effective_progs(struct cgroup *cgrp, p = cgroup_parent(p); } while (p); - progs = bpf_prog_array_alloc(cnt, GFP_KERNEL); + progs = bpf_prog_array_alloc(cnt, flags); if (!progs) return -ENOMEM; @@ -308,7 +309,7 @@ int cgroup_bpf_inherit(struct cgroup *cgrp) INIT_LIST_HEAD(&cgrp->bpf.storages); for (i = 0; i < NR; i++) - if (compute_effective_progs(cgrp, i, &arrays[i])) + if (compute_effective_progs(cgrp, i, &arrays[i], GFP_KERNEL)) goto cleanup; for (i = 0; i < NR; i++) @@ -328,7 +329,8 @@ int cgroup_bpf_inherit(struct cgroup *cgrp) } static int update_effective_progs(struct cgroup *cgrp, - enum cgroup_bpf_attach_type atype) + enum cgroup_bpf_attach_type atype, + gfp_t flags) { struct cgroup_subsys_state *css; int err; @@ -340,7 +342,8 @@ static int update_effective_progs(struct cgroup *cgrp, if (percpu_ref_is_zero(&desc->bpf.refcnt)) continue; - err = compute_effective_progs(desc, atype, &desc->bpf.inactive); + err = compute_effective_progs(desc, atype, &desc->bpf.inactive, + flags); if (err) goto cleanup; } @@ -499,7 +502,7 @@ static int __cgroup_bpf_attach(struct cgroup *cgrp, bpf_cgroup_storages_assign(pl->storage, storage); cgrp->bpf.flags[atype] = saved_flags; - err = update_effective_progs(cgrp, atype); + err = update_effective_progs(cgrp, atype, GFP_KERNEL); if (err) goto cleanup; @@ -722,7 +725,7 @@ static int __cgroup_bpf_detach(struct cgroup *cgrp, struct bpf_prog *prog, pl->prog = NULL; pl->link = NULL; - err = update_effective_progs(cgrp, atype); + err = update_effective_progs(cgrp, atype, GFP_NOIO | GFP_NOFS | __GFP_NOFAIL); if (err) goto cleanup; >> -cleanup: >> - /* restore back prog or link */ >> - pl->prog = old_prog; >> - pl->link = link; >> + /* In case of error call update_effective_progs again */ >> + if (err) >> + err = update_effective_progs(cgrp, atype); > there is no guarantee that this will now succeed, right? so it's more > like "let's try just in case we are lucky this time"? Yes, there is no guarantee, but my thinking was that since we have freed some memory (see kfree(pl) above) let's retry and see if it works this time. If that is combined with the above gfp flags it is a good chance that it will work. What do you think? -- Thanks, Tadeusz