Received: by 2002:ab2:3319:0:b0:1ef:7a0f:c32d with SMTP id i25csp35240lqc; Thu, 7 Mar 2024 09:32:03 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVHyHxP2qNttfaBiMI4jZYfpQIZYMtvmvEe7YZQa2U2BJfe9t2syKxf7RJlJnzuR+PuZFSGOFNhrjSBfZ8eURgH9bsv7AVkhbBY9WUyVQ== X-Google-Smtp-Source: AGHT+IFNZAXljSrsH0c/fVVpN33heVdvtrZ7eDmKfMaS85WMmExyqZQdJpEjVEJxf3nP3Q27IdTY X-Received: by 2002:a2e:3e01:0:b0:2d2:3b9e:dcff with SMTP id l1-20020a2e3e01000000b002d23b9edcffmr1533641lja.46.1709832723734; Thu, 07 Mar 2024 09:32:03 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709832723; cv=pass; d=google.com; s=arc-20160816; b=l65WtnwbODlSFDG/g4gjhFPhDb29W5Iaq0FCNzHaJzfSBx3mcWQQMv+H2Hs94DSF2d A7utuO3BTPR5btLq6vGaI3c5VMKSWvMVydkoQc3cuh+znR8/8ZmofpS6AkuMCjoace+H vBRiKDP/lNopi2/Xkxqehho0hYPTyynbUS2fkAw2J9Pm+ucPnyjqBFx/s/riKgE/OYBL 9H/OQQODZXHvmyV5PNo/NHszTEsKy0P6iT9BNjFYBMiM2y0cpzLJnYK35dw69jVBgHnQ Z9xnvPLx++XgFhnajEScNYfkPDpt/d37qIXAAqQ0KRSpd3sAY9IhkSeHsRHT/0BjaaoE okDA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:content-language:in-reply-to:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:user-agent:date :message-id:from:references:cc:to:subject:dkim-signature; bh=6GKHuULFobI/veCzO71hJuFZMvHW2DBWIwJthjIFZt0=; fh=kHT+jd6GUWdiFE+9ndkDpTLAeOK5TiUiDPPuEBxKsEI=; b=Z+b22TfQgbpCs4IVNhQaKkukJaFcF/0T/smxWTpm5m0LENDOELWGF079GyuursasKR cQegyl8Dl0PkQ+QRYHAlgizTbrUcQ639LEQN+hJLDJnYOIE6x69ZmmAm22DVisRnDoWv HAR8hP/tQOUNhFSKTwXeCUGKnAQa3kEK2nYqXfmrP3ROcn4gAAg1IKwhwdw30ZaFMc6S Ezd6GqkrdKIlNg3g+iy2mOw2V1J/dRvzYqTtqoEsCqRx0RSxOv0DX8mRzLGy0E5W91bY ilgLyUfhx+XnowIlpWP+QifClt+VQ7etmjzkmeVBTAJNXrU2Oat3Is4T+7dh7tU3TO5y NdnA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@iogearbox.net header.s=default2302 header.b=JXWx3fcS; arc=pass (i=1 spf=pass spfdomain=iogearbox.net dkim=pass dkdomain=iogearbox.net dmarc=pass fromdomain=iogearbox.net); spf=pass (google.com: domain of linux-kernel+bounces-95943-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-95943-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=NONE dis=NONE) header.from=iogearbox.net Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id g33-20020a056402322100b00568016134a6si1159659eda.375.2024.03.07.09.32.03 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Mar 2024 09:32:03 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-95943-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@iogearbox.net header.s=default2302 header.b=JXWx3fcS; arc=pass (i=1 spf=pass spfdomain=iogearbox.net dkim=pass dkdomain=iogearbox.net dmarc=pass fromdomain=iogearbox.net); spf=pass (google.com: domain of linux-kernel+bounces-95943-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-95943-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=NONE dis=NONE) header.from=iogearbox.net Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 16F4A1F22C5D for ; Thu, 7 Mar 2024 17:32:03 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 441B7130E58; Thu, 7 Mar 2024 17:31:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=iogearbox.net header.i=@iogearbox.net header.b="JXWx3fcS" Received: from www62.your-server.de (www62.your-server.de [213.133.104.62]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 39861130AF1; Thu, 7 Mar 2024 17:31:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=213.133.104.62 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709832706; cv=none; b=RRnSRX1waA97wAXiTMwUvE7ST/2XdbaQq2u7kSnEBfmhjbp/X20ZUm6oQTvTi7ml22mI5DTKHEFM0W4tWhwHeYXI6aFQLMsz5l4NyzSor1O0+8VxO9URrpjJmlAKM4e99tI30deVh84bOkzTcM35nz8OYYBrZDDdV6ZLKLQhaRY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709832706; c=relaxed/simple; bh=Pw1wjnfdkstP4sMjq/SqSIfAkxHM4ROpPhW1adlHMZI=; h=Subject:To:Cc:References:From:Message-ID:Date:MIME-Version: In-Reply-To:Content-Type; b=MDWyNvEjUmqvPxv7Uaq5B2Vz3L76Z4uIx5LL6iFzhA/P/jST/DGfDUVj2d6oVCo463fCad9k7jifgRkBum7kL7BaJaVM1MTlACkNz1cQofWGYkzaXe9vF/XOYeaJVUTKxgwyzyrH/xV1fECr/YkISD2KrQE3DQ2Jsz7ubFuZcw4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=iogearbox.net; spf=pass smtp.mailfrom=iogearbox.net; dkim=pass (2048-bit key) header.d=iogearbox.net header.i=@iogearbox.net header.b=JXWx3fcS; arc=none smtp.client-ip=213.133.104.62 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=iogearbox.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=iogearbox.net DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=iogearbox.net; s=default2302; h=Content-Transfer-Encoding:Content-Type: In-Reply-To:MIME-Version:Date:Message-ID:From:References:Cc:To:Subject:Sender :Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID; bh=6GKHuULFobI/veCzO71hJuFZMvHW2DBWIwJthjIFZt0=; b=JXWx3fcSvqEd2usjf0tQN1DMCi vSO6xR4WJptmR2KKeHQgwZrwN8Kcxo2SsAwQYU3GqG1uUhHegfI3SUUIcj3xuPPVcnq4qvvd9Z59E QEXIJexwsh/xmTHCzcD+vFT4djVEoZUoeVEOEWDbhnGjDhNOexCanK8oqtsXqubWrWGy58R7eCA5a KMkwOmTd6RBHYUW7MOdrUMkaxa0IBDN9EMKZ1xegBgTvPlxJM2R9eO51SJIrfnnNNvBIZE0Kisxb1 7MM1fiEoUnsAibMICY+p3wL4QPlBDDx84amzwvLXj1OsBgSDB7e/8IGpBSO57JhZs1rwQ14crw6Zx jCusX5Yw==; Received: from sslproxy03.your-server.de ([88.198.220.132]) by www62.your-server.de with esmtpsa (TLS1.3) tls TLS_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1riHZL-000J8N-R9; Thu, 07 Mar 2024 18:30:11 +0100 Received: from [77.58.94.13] (helo=localhost.localdomain) by sslproxy03.your-server.de with esmtpsa (TLS1.3) tls TLS_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1riHZI-0034Od-1I; Thu, 07 Mar 2024 18:30:08 +0100 Subject: Re: [PATCH bpf-next RESEND v2 1/2] bpf: Take return from set_memory_ro() into account with bpf_prog_lock_ro() To: Christophe Leroy , Alexei Starovoitov , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Russell King , Puranjay Mohan , Tiezhu Yang , Hengqi Chen , Huacai Chen , WANG Xuerui , Johan Almbladh , Paul Burton , Thomas Bogendoerfer , "James E.J. Bottomley" , Helge Deller , Ilya Leoshkevich , Heiko Carstens , Vasily Gorbik , Alexander Gordeev , Christian Borntraeger , Sven Schnelle , "David S. Miller" , David Ahern , Wang YanQing , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" Cc: bpf@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linux-parisc@vger.kernel.org, linux-s390@vger.kernel.org, sparclinux@vger.kernel.org, netdev@vger.kernel.org, "linux-hardening @ vger . kernel . org" , Kees Cook References: <8f3b3823cce2177e5912ff5f2f11381a16db07db.1709279661.git.christophe.leroy@csgroup.eu> From: Daniel Borkmann Message-ID: <093da237-50c1-5898-1637-7a9a84e1076c@iogearbox.net> Date: Thu, 7 Mar 2024 18:30:06 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.7.2 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 In-Reply-To: <8f3b3823cce2177e5912ff5f2f11381a16db07db.1709279661.git.christophe.leroy@csgroup.eu> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit X-Authenticated-Sender: daniel@iogearbox.net X-Virus-Scanned: Clear (ClamAV 0.103.10/27207/Thu Mar 7 10:27:12 2024) On 3/1/24 8:57 AM, Christophe Leroy wrote: > set_memory_ro() can fail, leaving memory unprotected. > > Check its return and take it into account as an error. > > Link: https://github.com/KSPP/linux/issues/7 > Signed-off-by: Christophe Leroy > Cc: linux-hardening@vger.kernel.org > Reviewed-by: Kees Cook > --- > Sorry for the resend, I forgot to flag patch 2 as bpf-next > > Note: next patch is autonomous, it is sent as a follow-up of this one to minimize risk of conflict on filter.h because the two changes are too close to each other. > > v2: No modification (Just added link in patch message), patchwork discarded this series due to failed test of s390 but it seems unrelated, see https://lore.kernel.org/bpf/wvd5gzde5ejc2rzsbrtwqyof56uw5ea3rxntfrxtkdabzcuwt6@w7iczzhmay2i/T/#m2e61446f42d5dc3d78f2e0e8b7a783f15cfb109d > --- > include/linux/filter.h | 5 +++-- > kernel/bpf/core.c | 4 +++- > kernel/bpf/verifier.c | 4 +++- > 3 files changed, 9 insertions(+), 4 deletions(-) > > diff --git a/include/linux/filter.h b/include/linux/filter.h > index 36cc29a2934c..7dd59bccaeec 100644 > --- a/include/linux/filter.h > +++ b/include/linux/filter.h > @@ -884,14 +884,15 @@ bpf_ctx_narrow_access_offset(u32 off, u32 size, u32 size_default) > > #define bpf_classic_proglen(fprog) (fprog->len * sizeof(fprog->filter[0])) > > -static inline void bpf_prog_lock_ro(struct bpf_prog *fp) > +static inline int __must_check bpf_prog_lock_ro(struct bpf_prog *fp) > { > #ifndef CONFIG_BPF_JIT_ALWAYS_ON > if (!fp->jited) { > set_vm_flush_reset_perms(fp); > - set_memory_ro((unsigned long)fp, fp->pages); > + return set_memory_ro((unsigned long)fp, fp->pages); > } > #endif > + return 0; > } > > static inline void bpf_jit_binary_lock_ro(struct bpf_binary_header *hdr) > diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c > index 71c459a51d9e..c49619ef55d0 100644 > --- a/kernel/bpf/core.c > +++ b/kernel/bpf/core.c > @@ -2392,7 +2392,9 @@ struct bpf_prog *bpf_prog_select_runtime(struct bpf_prog *fp, int *err) > } > > finalize: > - bpf_prog_lock_ro(fp); > + *err = bpf_prog_lock_ro(fp); > + if (*err) > + return fp; > > /* The tail call compatibility check can only be done at > * this late stage as we need to determine, if we deal > diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c > index 1c34b91b9583..6ec134f76a11 100644 > --- a/kernel/bpf/verifier.c > +++ b/kernel/bpf/verifier.c > @@ -19096,7 +19096,9 @@ static int jit_subprogs(struct bpf_verifier_env *env) > * bpf_prog_load will add the kallsyms for the main program. > */ > for (i = 1; i < env->subprog_cnt; i++) { > - bpf_prog_lock_ro(func[i]); > + err = bpf_prog_lock_ro(func[i]); > + if (err) > + goto out_free; How does the error path take out the subprogs from kallsyms in your case? Suppose some of the loop iterations succeed before we hit an error. I believe the subprogs still exist in kallsyms here. > bpf_prog_kallsyms_add(func[i]); > } > >