Received: by 2002:ab2:3141:0:b0:1ed:23cc:44d1 with SMTP id i1csp134570lqg; Thu, 29 Feb 2024 23:53:20 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUIAi/DgzvCkHqGSirgpIOtkZi/hdE/tV/UqiNbwfq8SO7WX1WzBG7cbV6Atsa8C2cry3hC/wlENuR9N+Lk9x5/sH3azNa98gcsqh1Jzw== X-Google-Smtp-Source: AGHT+IFZOOU7v5GdB/O6/dBODANUkr9q/VfVNrc9JBN3T8z62x/QxQj3tO6mFhJxpVQBU0I65kbs X-Received: by 2002:a05:6e02:1cab:b0:365:c8:7a15 with SMTP id x11-20020a056e021cab00b0036500c87a15mr1138280ill.20.1709279600245; Thu, 29 Feb 2024 23:53:20 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709279600; cv=pass; d=google.com; s=arc-20160816; b=X8M2at10ddNwY83SpJyMPHwEz9E44MLZPguKy7uxYHIVY2g9po/btKikxL2cd2sqaR Ip68syri2UqSBi0U5u1E9SkNtkREGo6ad/mBCLAkJpWBFtnJVFajbAdpipUneA+351zW Vv4twnYOSVrc2MltTPh756z3ozOElpSuWjC2UgJWJ0fUWICXYIyYlX8ae+VDqfw0MsOe fpz+RynX8zly386ipCygAxol1m/nu4UpfyBZajzSHT+lgmqKdW372sAXFKdOxQJRYBh1 /O2MMNATb0HUYJu9fQiErWpxFwtHgw7YznFu0oXifQptqih0GOdhOpoO3kzh2BAUwpyS wJzw== 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=VvTtEDHnQ2tJAV01/ewCh5v7ma0Q6U4p+ktj6tq0W/k=; fh=lRdqJ37rjKR9nV+5Z/gN3s8lz9m23kRWfPReK4OTEgU=; b=y1UMq8fu06SDri/8V46JonVtBnoCrYkvY1NlS3FFgqxf8hhKjp9ie+ptTEfqWwQHjo yWTYZfLGMsKmRSxpuo10hYw4yJQ0x8YbLai+MiCHzuKL0KAMBfyoN9PQr6/m/D9HBWOh L3uvsul7r966lDMfnUCwfNCdHLiKbT8+0zm+t5FYKm+wGY2D/MlkvSWHAcSwhMcEqKA0 gr1BQC2qSuKVClNQcPV40eewyYY6UD01Ay8/qqsIq6wUy0y8c8UUEjzcultJNqdAUqAi k+SV0ERGy3QwOdXeWCq4iSfprBGdRfZemdaCWZPlzdo5uy5SLshq61GPG/06Oa5/IYP7 /zbQ==; 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-88035-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-88035-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=QUARANTINE) header.from=csgroup.eu Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id a2-20020a63e402000000b005dcbb8409ecsi3012711pgi.611.2024.02.29.23.53.19 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 29 Feb 2024 23:53:20 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-88035-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; 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-88035-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-88035-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 94B132839FD for ; Fri, 1 Mar 2024 07:53:00 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 92A5469D0F; Fri, 1 Mar 2024 07:52:46 +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 D71FB224C9; Fri, 1 Mar 2024 07:52:43 +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=1709279566; cv=none; b=bJwHCuev+pQvlQrDcEwxBfa+kffHUk30mS5x509q077C2EtAC7f54ylmQgctTRrnX+tIVyLjDA1hHTsvrzEv6lbN09dli2FzoSOmpiT5ADjyWwdTvT1RV5WYBUnnxDPmcZ8LiveL5DQypMk/9cp657TaT+Bahq8f6hGW6TQUV7w= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709279566; c=relaxed/simple; bh=m9JyN+OfZnBfNduvWL6HlRt2Ws8qdQM+zYhc2tHrO60=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=TjLu8oj5/wja4dRS73eiuFFpI+WQoMqhXJ6PheR2+C0e24fljpIfsfbhoZp9yQyqQKQGIrBKsrj+/R7SjJQaG6qHYn2YQTfeHOE8+Zyzl3276TLY2GIxxjVWDOCzm9gpnH4htaVsvGpE+J3rN5ck6+oWA41EbkUfgyheC5SOD/M= 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 4TmL0g4qB7z9tHZ; Fri, 1 Mar 2024 08:52:35 +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 BqoZa5JD5ZwP; Fri, 1 Mar 2024 08:52:35 +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 4TmL0f46Qcz9tFS; Fri, 1 Mar 2024 08:52:34 +0100 (CET) Received: from localhost (localhost [127.0.0.1]) by messagerie.si.c-s.fr (Postfix) with ESMTP id 857FB8B774; Fri, 1 Mar 2024 08:52:34 +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 X5jlVshACLyB; Fri, 1 Mar 2024 08:52:34 +0100 (CET) Received: from PO20335.idsi0.si.c-s.fr (unknown [192.168.232.117]) by messagerie.si.c-s.fr (Postfix) with ESMTP id 6FC788B766; Fri, 1 Mar 2024 08:52:33 +0100 (CET) From: Christophe Leroy To: Alexei Starovoitov , Daniel Borkmann , John Fastabend , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , 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 v2 1/2] bpf: Take return from set_memory_ro() into account with bpf_prog_lock_ro() Date: Fri, 1 Mar 2024 08:52:24 +0100 Message-ID: <8f3b3823cce2177e5912ff5f2f11381a16db07db.1709279160.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=1709279548; l=2705; i=christophe.leroy@csgroup.eu; s=20211009; h=from:subject:message-id; bh=m9JyN+OfZnBfNduvWL6HlRt2Ws8qdQM+zYhc2tHrO60=; b=BM8D7LKNIqd4yL3OmV1oY3lqs5bdcfaokGX528YFWG10bGRYTPO7xmnY3X/YTK0+TrgdD1oe0 Jcv1743O7HSBXVnLAvfrfkWf80mkc3ESZY06KRQSThm9AifGg/N13eN 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. 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; bpf_prog_kallsyms_add(func[i]); } -- 2.43.0