Received: by 2002:a05:7412:cfc7:b0:fc:a2b0:25d7 with SMTP id by7csp690341rdb; Sun, 18 Feb 2024 07:20:03 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUn4fVUufUQa/slwzrpRdBjWUlfZBAEuwFhZ0PuCJ6LIau1DcdAhvkEQI0czMFBVu2t1OFyKI/QYKMffISEgccG7c1jOm+bads69XXaqA== X-Google-Smtp-Source: AGHT+IFvdlaASwHoUmg5zvsq/HSbjhlgICyCS/dA3Tt4Yed96dFZBDkuFis2yudQhoQE8pWRegC1 X-Received: by 2002:ac8:5b84:0:b0:42e:380:74e3 with SMTP id a4-20020ac85b84000000b0042e038074e3mr2149370qta.68.1708269603277; Sun, 18 Feb 2024 07:20:03 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708269603; cv=pass; d=google.com; s=arc-20160816; b=Bw+xUphOV3WZxuigvPmyxEP96b/4yD8H8xLqQywwXsICOUepeMNhg/ChVjPtkVZlBh NMhJD4hSOnTMhtlGMmPmeErzPkILePu42+wYizq5JLRJm7o20bjXkyUG5yRSnYO6IE/6 NixAeqkDGNQ7Pp76JI0jyIK1Jkjgp6hVbLJWI7VaOlMD4kBIUgK6Iy16bUBxoG1Sr61a E8A8S+pXbhMpZW11ZlDPZ3vqKfyZc3Xa7ZBnIna41iOQZPVcl+9sOTl902cM+v1HN0zz U2gNSeSFe8G9/N526ahFXmfBe0A2zIK6SB2vP8nCQGoJBoi+LB4vXWb0BpHwSk5ym9av 7zKw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature; bh=eiWOUozWnezeYn8cXQvtDvCvh6gTYyUB/9KcoNgO2S8=; fh=rjwKuN/A3VdH80herkOXVa5axcd4ig0T1sM+49AFFQ4=; b=I9ZCkNuCppe5x8lu3HcC7eaFyxiu9WV8SJIdHm5KUYuSsmmlS1/5M4HaNAY8yDoy83 KdpK45s4dMdiT1P4gTih80WFgSAAge7PK3cniw5qfDHP1apDra16oo7wMxgeB/fe+hYd BF4Jo8U/SStFaQsjepMSKRsUnunR1AGmSNBXqLqAkLvrVO9I1VX/7CzBmkWPF1A/RVU0 8lhFvo3TJdbm8Vy2yUeTTo6fFm+JPvwm8xWort6O3bg8CCjsDmPmdUIkVGqviJiNKxGe 1G2aUwuUTCxq78empFkvX3Ndjw9YV5+4a9ChY7Kp/C+S+sp6NYMaUp4vhEHGhpTEflp3 iVPw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=Z0XxTgjq; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-70428-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-70428-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id d9-20020a05622a05c900b0042c513c8cc8si4482148qtb.68.2024.02.18.07.20.03 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 18 Feb 2024 07:20:03 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-70428-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; dkim=pass header.i=@chromium.org header.s=google header.b=Z0XxTgjq; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-70428-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-70428-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org 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 EEEFF1C20B80 for ; Sun, 18 Feb 2024 15:20:02 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6E9E36D1C3; Sun, 18 Feb 2024 15:19:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="Z0XxTgjq" Received: from mail-pf1-f172.google.com (mail-pf1-f172.google.com [209.85.210.172]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 804AE6BFD8 for ; Sun, 18 Feb 2024 15:19:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708269588; cv=none; b=CKmAlcWvwEsyENfHifmkJD63I9rQoszoDgfnTfZXniavbux4AVHZ9IImc7mK5YpoDGsALxvrLC3otuXT6nslxKI9jLlkteNc2W4oEYS3VU2hKMsPud2wJ6M7nPJDnhAcdIFNB8wEZPz2JYabTdCO5snNVXONgPNslTZmiKaGLqc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708269588; c=relaxed/simple; bh=lYFHSwKGcbZg0KftqGMF5GiOnvyk1uaue8OvErSz/MI=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=AwJXLted7GPU07EzaqeT98GaRJHuByy7frj9TydWRjjaeHneUsAKOyqCTz8s6buOgh27NiWuZKqT1b3rh7VqISSDzO5+4zwo0N8vrBIaqLp/ZCmmaf4DtkQTtIlk85QKzP8plBSC10+CrCGspsWFLAoRtC9BXc5eluxvAhzGy60= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=Z0XxTgjq; arc=none smtp.client-ip=209.85.210.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Received: by mail-pf1-f172.google.com with SMTP id d2e1a72fcca58-6e45c59fc6cso187462b3a.0 for ; Sun, 18 Feb 2024 07:19:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1708269586; x=1708874386; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=eiWOUozWnezeYn8cXQvtDvCvh6gTYyUB/9KcoNgO2S8=; b=Z0XxTgjqWyazp15zaW/ZnhQPP/vQUJ16DOZ1Hq0r+BT7AlgmbJ9FONeOG3yHNtb9PL NY72Vj0zKPqaAgRpopK2+aqqkrQfRItybiUPkatXi9wwD13ZxQHcl8NYYoFU5Jp6RHdm c0o9K96cAg54FH5HIBpabLJjAOkt0mQ2F2mQg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708269586; x=1708874386; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=eiWOUozWnezeYn8cXQvtDvCvh6gTYyUB/9KcoNgO2S8=; b=kNOe55oEiBdJGDHejR1c6gVPCVAHlXaBb8E8JA2PmCtFolUU4z1Ohe4a3ykr6rk53t Cs0NCPcN60E6G1mP/AvvYjJsapkQvP0uMFmOEOOUAzuMyBYTMtrgMkPo6JCP/4lcg0qi LceqjcG8JXqTIfs4Pffo1HXS6DDD0cld5QZRsG1o+N9aXQyZUO2emBXy6rflC0bJYJC/ N5NDNT0W+M4nlAhAmqNHigXIFY8tW8PG2TDsq9cpBFQ+LBbO76lrdfGYop6TqL5VhEfY /c8AgX+SLx0CliXw0gpclfha6WAbsfv4Blqs3GyoW2iJRWNVuKsU/EPsf8gkN/vr0UbP gawA== X-Forwarded-Encrypted: i=1; AJvYcCX++j9DcLIo6o59mnYRnS4+JTjNssAh/5JcOoCm/6xGQF5Nav9pYOtCHWAkWlMCGy4Cd8ceeWm32VR9Nv1i28q3T+e+KDDk3pRGZg0C X-Gm-Message-State: AOJu0YzV4PnmAoFAfE0PEwXOFeKUtNzYhRNsS3VXE7cEpGybtEPlh7Bl +cfkiYICsWhORdhZufmqQoxNArbubX4Yqy+5ypQ5YUrIZvyEr1jnUyZ/S0+peg== X-Received: by 2002:aa7:8202:0:b0:6e1:dbd:e800 with SMTP id k2-20020aa78202000000b006e10dbde800mr9961775pfi.17.1708269585774; Sun, 18 Feb 2024 07:19:45 -0800 (PST) Received: from www.outflux.net ([198.0.35.241]) by smtp.gmail.com with ESMTPSA id r22-20020aa78456000000b006e24991dd5bsm2894532pfn.98.2024.02.18.07.19.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 18 Feb 2024 07:19:45 -0800 (PST) Date: Sun, 18 Feb 2024 07:19:44 -0800 From: Kees Cook To: Christophe Leroy Cc: 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 , Russell King , Puranjay Mohan , Zi Shen Lim , Catalin Marinas , Will Deacon , 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" , Andreas Larsson , Wang YanQing , David Ahern , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , 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" Subject: Re: [PATCH bpf-next 2/2] bpf: Take return from set_memory_rox() into account with bpf_jit_binary_lock_ro() Message-ID: <202402180711.22F5C511E5@keescook> References: <135feeafe6fe8d412e90865622e9601403c42be5.1708253445.git.christophe.leroy@csgroup.eu> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: On Sun, Feb 18, 2024 at 11:55:02AM +0100, Christophe Leroy wrote: > set_memory_rox() can fail, leaving memory unprotected. > > Check return and bail out when bpf_jit_binary_lock_ro() returns > and error. > > Signed-off-by: Christophe Leroy > --- > Previous patch introduces a dependency on this patch because it modifies bpf_prog_lock_ro(), but they are independant. > It is possible to apply this patch as standalone by handling trivial conflict with unmodified bpf_prog_lock_ro(). > --- > arch/arm/net/bpf_jit_32.c | 25 ++++++++++++------------- > arch/arm64/net/bpf_jit_comp.c | 21 +++++++++++++++------ > arch/loongarch/net/bpf_jit.c | 21 +++++++++++++++------ > arch/mips/net/bpf_jit_comp.c | 3 ++- > arch/parisc/net/bpf_jit_core.c | 8 +++++++- > arch/s390/net/bpf_jit_comp.c | 6 +++++- > arch/sparc/net/bpf_jit_comp_64.c | 6 +++++- > arch/x86/net/bpf_jit_comp32.c | 3 +-- > include/linux/filter.h | 4 ++-- > 9 files changed, 64 insertions(+), 33 deletions(-) > > diff --git a/arch/arm/net/bpf_jit_32.c b/arch/arm/net/bpf_jit_32.c > index 1d672457d02f..01516f83a95a 100644 > --- a/arch/arm/net/bpf_jit_32.c > +++ b/arch/arm/net/bpf_jit_32.c > @@ -2222,28 +2222,21 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog) > /* If building the body of the JITed code fails somehow, > * we fall back to the interpretation. > */ > - if (build_body(&ctx) < 0) { > - image_ptr = NULL; > - bpf_jit_binary_free(header); > - prog = orig_prog; > - goto out_imms; > - } > + if (build_body(&ctx) < 0) > + goto out_free; > build_epilogue(&ctx); > > /* 3.) Extra pass to validate JITed Code */ > - if (validate_code(&ctx)) { > - image_ptr = NULL; > - bpf_jit_binary_free(header); > - prog = orig_prog; > - goto out_imms; > - } > + if (validate_code(&ctx)) > + goto out_free; > flush_icache_range((u32)header, (u32)(ctx.target + ctx.idx)); > > if (bpf_jit_enable > 1) > /* there are 2 passes here */ > bpf_jit_dump(prog->len, image_size, 2, ctx.target); > > - bpf_jit_binary_lock_ro(header); > + if (bpf_jit_binary_lock_ro(header)) > + goto out_free; > prog->bpf_func = (void *)ctx.target; > prog->jited = 1; > prog->jited_len = image_size; > @@ -2260,5 +2253,11 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog) > bpf_jit_prog_release_other(prog, prog == orig_prog ? > tmp : orig_prog); > return prog; > + > +out_free: > + image_ptr = NULL; > + bpf_jit_binary_free(header); > + prog = orig_prog; > + goto out_imms; These gotos give me the creeps, but yes, it does appear to be in the style of the existing error handling. > [...] > diff --git a/arch/x86/net/bpf_jit_comp32.c b/arch/x86/net/bpf_jit_comp32.c > index b18ce19981ec..f2be1dcf3b24 100644 > --- a/arch/x86/net/bpf_jit_comp32.c > +++ b/arch/x86/net/bpf_jit_comp32.c > @@ -2600,8 +2600,7 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog) > if (bpf_jit_enable > 1) > bpf_jit_dump(prog->len, proglen, pass + 1, image); > > - if (image) { > - bpf_jit_binary_lock_ro(header); > + if (image && !bpf_jit_binary_lock_ro(header)) { I find the "!" kind of hard to read the "inverted" logic (0 is success), so if this gets a revision, maybe do "== 0"?: if (image && bpf_jit_binary_lock_ro(header) == 0) { But that's just me. So, regardless: Reviewed-by: Kees Cook -- Kees Cook