Received: by 2002:ab2:3319:0:b0:1ef:7a0f:c32d with SMTP id i25csp343078lqc; Thu, 7 Mar 2024 21:38:39 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCU/ZxqYopKfiw5Po2MozgEHm3XJ2BMf4MLiLb5cPDucUhT/4Zgep+Usbus3EO7YdGuqkDzb5fXXSItzdwW/4VI+An5LGe96REgXT5rpKA== X-Google-Smtp-Source: AGHT+IFFfdMhDd9ouogdroCetGRnb0tUYQ+NyKRhSVshk1SOZJ/M/NY/U0piW02NSLTGGBzCscF6 X-Received: by 2002:a05:622a:1489:b0:42e:c0dd:735d with SMTP id t9-20020a05622a148900b0042ec0dd735dmr10181106qtx.65.1709876319441; Thu, 07 Mar 2024 21:38:39 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709876319; cv=pass; d=google.com; s=arc-20160816; b=oxR0jUNebCPDpvMhwMeGicgfkphKzaQYF30glqeQeHg4Vw5HQoTkbfayM8HyQ/42wi 0Blrv6jXmcWNVdlBhFwSho3lHAHjBHCRaCH2afk/Ev9HWi1wFyAU8aRSied6PR+NCXk7 OLccQckCfhbZDAoWgLsaok5zbyXrDa8euOoKW5CQtM9VvlkqwHx6BdT8AIZNKQFoMF1M bRh+3xx4y4CHzyRK82MSUnfcZGd86/mHxqqf+n9Wt+VH42e3lrAW9GxJT2bzdgDeBJ15 Jt6CjZtPouObA938hvNMhPKzlg8a2l2pftxn1aPANDuElSWfjQM40JjoIu3GjNzrt0BD alsQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from; bh=Y8kDbgwPnRmkDgt0YexFCKSotWRQ+tZrGvbxbmOeXQ8=; fh=i7aYcO/HVh3h0S84XweK07PmGne9knM97EBHenmwQEE=; b=CgKk2RGdU32EsGxydm1l3lhLtDqw5NiL7i9VhpowtIyQfKO+KXJk2A2opB7omzLVom 0yImDIYnjggoiEYljUqHVR9TAY4pw6pHYpic2wyXTBKQdykSFoH/+gOHRaRJM897vKBJ jG38qayGArgV72MZ+dAzoC+2v9eg1PHu3Kt2D4bkiiZZZfj1985tDUaszZ2kPpdeXUoo 1K74eLhAJ2i78l3HZLWiCT747S/dCdgUry7RMNdhVEmAUIFKBNMi4hLW2GUB5VXnvCF6 erv8XG8w7lB0QdOeX/5iHdgMNzI0hAkU/EdUaaeHm/46MmypI6ZcL3NrHnuLXJaEUPIZ Zp2Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=csgroup.eu dmarc=pass fromdomain=csgroup.eu); spf=pass (google.com: domain of linux-kernel+bounces-96534-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-96534-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=QUARANTINE) header.from=csgroup.eu Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id j2-20020ac85f82000000b0042f01a365a4si7218616qta.114.2024.03.07.21.38.39 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Mar 2024 21:38:39 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-96534-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=csgroup.eu dmarc=pass fromdomain=csgroup.eu); spf=pass (google.com: domain of linux-kernel+bounces-96534-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-96534-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=QUARANTINE) header.from=csgroup.eu 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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 257AC1C21DC1 for ; Fri, 8 Mar 2024 05:38:39 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id CA6753612C; Fri, 8 Mar 2024 05:38:26 +0000 (UTC) Received: from pegase1.c-s.fr (pegase1.c-s.fr [93.17.236.30]) (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 9A0452E84A; Fri, 8 Mar 2024 05:38:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=93.17.236.30 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709876306; cv=none; b=ZLaCfdAo2FAu5k82lRdBk0WlwiXlXdizBMztDGjOfHe8yt4d0DfPUY6ezqWkkX6/S2uqWGkWhjyIIe/dHlurpoL1wK2LmRbfhi8yYD5+I/BGyjMwyTlDHZEfOn4sroLKbAvsJUws6CFJXrQx5s9+aOzbZSL4OtyKUBB2XL0inWo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709876306; c=relaxed/simple; bh=V1cL1/7e+ZqqsHNFqCEQO4q4BiPuZ0HpS/Plz61y9Ik=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=Yb4mx4iTKX3Qhp7CE4rgzdbCFtV4Ke1bPp8CgsRRlR5ZVNgHDqhOKJQh9gL45DOOVdItUQHnlsHQIkKYtgsRg9jC565Zi/+qUQR9yYJfJbBtHjo2j9UtDmrUtofy7Sl+3eRPKCqFqqz0lgtEqIA6n4JtSM+aaX6AjvdmvGcF5xc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=csgroup.eu; spf=pass smtp.mailfrom=csgroup.eu; arc=none smtp.client-ip=93.17.236.30 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=csgroup.eu Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=csgroup.eu Received: from localhost (mailhub3.si.c-s.fr [192.168.12.233]) by localhost (Postfix) with ESMTP id 4TrZhQ6xLsz9snT; Fri, 8 Mar 2024 06:38:14 +0100 (CET) X-Virus-Scanned: amavisd-new at c-s.fr Received: from pegase1.c-s.fr ([192.168.12.234]) by localhost (pegase1.c-s.fr [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id PpRWOs160HmF; Fri, 8 Mar 2024 06:38:14 +0100 (CET) Received: from messagerie.si.c-s.fr (messagerie.si.c-s.fr [192.168.25.192]) by pegase1.c-s.fr (Postfix) with ESMTP id 4TrZhQ5gqCz9sl0; Fri, 8 Mar 2024 06:38:14 +0100 (CET) Received: from localhost (localhost [127.0.0.1]) by messagerie.si.c-s.fr (Postfix) with ESMTP id BB8898B775; Fri, 8 Mar 2024 06:38:14 +0100 (CET) X-Virus-Scanned: amavisd-new at c-s.fr Received: from messagerie.si.c-s.fr ([127.0.0.1]) by localhost (messagerie.si.c-s.fr [127.0.0.1]) (amavisd-new, port 10023) with ESMTP id xGi3Awh1Plf7; Fri, 8 Mar 2024 06:38:14 +0100 (CET) Received: from PO20335.idsi0.si.c-s.fr (PO23130.IDSI0.si.c-s.fr [192.168.232.110]) by messagerie.si.c-s.fr (Postfix) with ESMTP id 291F78B763; Fri, 8 Mar 2024 06:38:14 +0100 (CET) From: Christophe Leroy To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa Cc: Christophe Leroy , 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 Subject: [PATCH bpf-next v3 1/2] bpf: Take return from set_memory_ro() into account with bpf_prog_lock_ro() Date: Fri, 8 Mar 2024 06:38:07 +0100 Message-ID: <286def78955e04382b227cb3e4b6ba272a7442e3.1709850515.git.christophe.leroy@csgroup.eu> X-Mailer: git-send-email 2.43.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1709876288; l=3060; i=christophe.leroy@csgroup.eu; s=20211009; h=from:subject:message-id; bh=V1cL1/7e+ZqqsHNFqCEQO4q4BiPuZ0HpS/Plz61y9Ik=; b=/YEOM3OHP8KDJyOO3ehJ1RVP4GzRAcAID6cCJZ/d0dp047mlx2Jh+9clH0DgIt4qgXGkgno5Y LywIbfa5G6aCqIarVYXz5gOrWTacPl27NnwyvDHDUe/9WDzqxsL3gUf X-Developer-Key: i=christophe.leroy@csgroup.eu; a=ed25519; pk=HIzTzUj91asvincQGOFx6+ZF5AoUuP9GdOtQChs7Mm0= Content-Transfer-Encoding: 8bit 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 --- 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. v3: Perform bpf_prog_kallsyms_add() after protection of all subprog have succeeded. 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 | 8 ++++++-- 3 files changed, 12 insertions(+), 5 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 9ee4536d0a09..ae28bab2b138 100644 --- a/kernel/bpf/core.c +++ b/kernel/bpf/core.c @@ -2393,7 +2393,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 ee86e4d7d5fc..c97108935c9a 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -19153,10 +19153,14 @@ 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]); - bpf_prog_kallsyms_add(func[i]); + err = bpf_prog_lock_ro(func[i]); + if (err) + goto out_free; } + for (i = 1; i < env->subprog_cnt; i++) + bpf_prog_kallsyms_add(func[i]); + /* Last step: make now unused interpreter insns from main * prog consistent for later dump requests, so they can * later look the same as if they were interpreted only. -- 2.43.0