Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp2302209lqp; Sun, 24 Mar 2024 12:12:32 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVaGvOl3x04bARM7wB3Vb/b3UCcs7usnHCpph1wvBXb0aDJ9O5OFyjO0P8+CtdeCyjmBFekYdcJF7qDuKrLmHVMQtp1h3FNw+2OvLTKqg== X-Google-Smtp-Source: AGHT+IF4CZP8WG5cAFaQtQE28zqP8hAcgmSnbjahM31k2IyuVyV8+J/FBratM4HkHLN7fhu/Lsy5 X-Received: by 2002:a17:902:eec4:b0:1dd:b728:b890 with SMTP id h4-20020a170902eec400b001ddb728b890mr4493996plb.18.1711307551850; Sun, 24 Mar 2024 12:12:31 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711307551; cv=pass; d=google.com; s=arc-20160816; b=MIrbNLOCmJyMJFArJ4miI2t43d0NGgM4Qv4dleNNaMtmVKMU4LqQNgJBM+FUVSdfBB yo5cRLT7VFJ3/VPwaRtciv5I7sIwPkk1Rf4EhM3/6w600BnF7372HOLm/2HuKtov4VTB s9+KzhxwFQYptUbljN8mKHm2DMX5frOvyHo+cvYuYpQJn7BCGPrcOLy/3yx94lfScPYb pObmZJMHk1jZ7uUq8sDJcejPVpZrYqsezDq9US/axIznXc6FXYh8X5Z5022jcx23w1WP MA88r1XrLfymdrHYRDRWFttGk3xuoCl0jJxpf6AsbOFWQTgvLDVVtUtFfx8TYoMQfP46 pStw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=0XPrkYswoGZZY13BgeysvBXQ3QtJFj2Q510OZd4P6Cw=; fh=2lCU4FkXdirc5qLqQEe0Di/nhzrgxjvgxWo2Zk/O95c=; b=ZT/sS+n8L3Sp3c1EZSlzEf1qxwN/1G8lVzzqpScPruAfqYAaHvZHZsjJQ3eeNzfqRB +zoSjXOkbmVT1WNLrBya06ndi1wSmeGyGhZTTHC36J+KMe8OvQFk0Lb4TYT/O9UdfS4N yx7SEprMDr43jvzt+d4vDsc74XAKHVMHdYmfOiNEb/HnasDEfsX1UIFimnG8O0rpUFnN TdF/D5doSG5fQcZ4pf2rEbboWrWqgWMNBoQIrPkCO/j42Hg+OQdggUDHPqgu7iGQv7aN heFAT3koRMvJy7sS/WqARe00acQQJnKW1DvncDFeORiiPk6fW46ZjPIyr59Fs0HQndHx 3vSQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=XEvH1XHq; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-112819-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-112819-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id j15-20020a170903024f00b001e096d44a94si3861008plh.355.2024.03.24.12.12.31 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 24 Mar 2024 12:12:31 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-112819-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=XEvH1XHq; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-112819-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-112819-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com 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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 392C7B20E3E for ; Sun, 24 Mar 2024 19:12:30 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C0479DF60; Sun, 24 Mar 2024 19:12:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="XEvH1XHq" Received: from mail-wr1-f49.google.com (mail-wr1-f49.google.com [209.85.221.49]) (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 2C64F79DE; Sun, 24 Mar 2024 19:12:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.49 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711307535; cv=none; b=BLrSOuZLO8BwIBJmo6+uq+JnC8O5YIw+DjuqWPMeahzSiEGUc5OX/2AjgaYNE/5Vebr8ntDmhVJnzRB7J9vOxOrfoDvQvuui4xJGvpVXy4y2TGpAKkLPzriBABUZ342mCMxyMVPm6Fe10yNFVGYy9ELaY4nWx70ZXQPy65X/OXs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711307535; c=relaxed/simple; bh=HxrL3yrY+BnVWjRVhzTVwqwSmIK98ryV6vDwq/y0eSk=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=nIJ6rP0wM5vgjcevHB39SP47IMJbciI4QWzE0dXWsxaL3dVjIEBQs/zTg3aQODZMzN4recP5lnelzDj6Cw0H3nkgCKIpJdpKAVmYkwfUcNSLiBAA9NzolQqOV5XQywnOSzuftfFQBOeJX2diQTsPjyD41pO3JeBYvX6nkC0st6A= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=XEvH1XHq; arc=none smtp.client-ip=209.85.221.49 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-wr1-f49.google.com with SMTP id ffacd0b85a97d-3416df43cabso2546374f8f.3; Sun, 24 Mar 2024 12:12:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1711307532; x=1711912332; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=0XPrkYswoGZZY13BgeysvBXQ3QtJFj2Q510OZd4P6Cw=; b=XEvH1XHqhIODVTSEhV6RXqWMfvyHUBJhLs/xUOSNiFXxLjUTcTTvRpQxHcf5P5aqrd nQ59pV0xSX/eIMuEUvda49fazRQ//EErGrtk1XCE2CakYFXLv+7sCQrh71+c3Q7J8iwN 2QdfbBS+QRU47ujX3iQXhjMEAxiiq5B7fmjWw4GJGaUrjlGK4zKqUrOI9nLB1ng8W1zj qQp8wqVpZ9TZUVfHD++Hjir9MGw2dc0mUmR85mDxtmbwuUlc7thA+6LhvBM/iB5Vysni w6dI2Jf1ZS2UMQN55E2PH1Ctlm0o/uhd/HKhmuDm8rUCf0i37B7bnmrO6OGuvaax1Xo2 PHIw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711307532; x=1711912332; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=0XPrkYswoGZZY13BgeysvBXQ3QtJFj2Q510OZd4P6Cw=; b=Mv8H/blDn7tw1uKOOCk3ukfUvn1NKQET7YO3JKVwTEvtfNF1/7CfOE8ZC2kDvs69op Sv/vk3vWbHm3hbTNcTzAqWOneVOUciHwkWrDGaiXKapHEVR9iNj+j2Uzyh7HIuJUFMGl qv09Z0GkaB0kIvJL1lXWhT9esdOsuGkHLld7wd6lhQStXtNlaY9AlFhkNYo3ZPMabx3b t9mYOUzPKd+wGZef45iYdS9AQvqqUF6/HjwTzRjpoaDb0zPzHGmzc5TnUCj61HkyGH4G Mzk60xNLNSYD4irRDZ79lGcI+XH2806WLZE7kCrEEzElOAasP8sxJC26aBjaCIaYdMhV +4pw== X-Forwarded-Encrypted: i=1; AJvYcCWqKjdL/1EKPbL2piuIvS5jL4Mt7XnQ7MvBnZSYba3rSGawfeV2Yu7kp7YmINpbR085ou8Fsm9imK9be5Lm8r0vqiYF4GHUBtGgS+FS6YFnJYg3DvRXb9j7zDZeny/F9w8EHfP1+dSyYJ+LZaHgBADUE4OexbQd97LC X-Gm-Message-State: AOJu0Yw3JyEM3QIkmMkK4BKVrYdzYc99ROq+DlyOXiBw6ixaiYtQ1zfk nBv3rAxFfJcozKnZ6ueHOzcMY3Av5gUaxYyV+/VINNVrTjFVvP2mSUzDu8WbMz21VUXmURnfRsQ vZULvb7PgFB0e6T1NFjP46h7Gwos= X-Received: by 2002:adf:f689:0:b0:341:bff9:2e4f with SMTP id v9-20020adff689000000b00341bff92e4fmr2868556wrp.44.1711307532417; Sun, 24 Mar 2024 12:12:12 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240321124640.8870-1-puranjay12@gmail.com> <9f2b63b5-569c-1e00-a635-93d9cd695517@iogearbox.net> <15ba79e3-14b2-d92e-3f94-e4f5f963e15d@iogearbox.net> In-Reply-To: From: Alexei Starovoitov Date: Sun, 24 Mar 2024 12:12:00 -0700 Message-ID: Subject: Re: [PATCH bpf v4] bpf: verifier: prevent userspace memory access To: Puranjay Mohan Cc: Daniel Borkmann , "David S. Miller" , David Ahern , Alexei Starovoitov , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , X86 ML , "H. Peter Anvin" , Jean-Philippe Brucker , Network Development , bpf , LKML , Ilya Leoshkevich Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Sun, Mar 24, 2024 at 3:44=E2=80=AFAM Puranjay Mohan wrote: > > Alexei Starovoitov writes: > > > On Fri, Mar 22, 2024 at 9:28=E2=80=AFAM Daniel Borkmann wrote: > >> > >> On 3/22/24 4:05 PM, Puranjay Mohan wrote: > >> [...] > >> >>> + /* Make it impossible to de-reference a userspace addr= ess */ > >> >>> + if (BPF_CLASS(insn->code) =3D=3D BPF_LDX && > >> >>> + (BPF_MODE(insn->code) =3D=3D BPF_PROBE_MEM || > >> >>> + BPF_MODE(insn->code) =3D=3D BPF_PROBE_MEMSX)) { > >> >>> + struct bpf_insn *patch =3D &insn_buf[0]; > >> >>> + u64 uaddress_limit =3D bpf_arch_uaddress_limit= (); > >> >>> + > >> >>> + if (!uaddress_limit) > >> >>> + goto next_insn; > >> >>> + > >> >>> + *patch++ =3D BPF_MOV64_REG(BPF_REG_AX, insn->s= rc_reg); > >> >>> + if (insn->off) > >> >>> + *patch++ =3D BPF_ALU64_IMM(BPF_ADD, BP= F_REG_AX, insn->off); > >> >>> + *patch++ =3D BPF_ALU64_IMM(BPF_RSH, BPF_REG_AX= , 32); > >> >>> + *patch++ =3D BPF_JMP_IMM(BPF_JLE, BPF_REG_AX, = uaddress_limit >> 32, 2); > >> >>> + *patch++ =3D *insn; > >> >>> + *patch++ =3D BPF_JMP_IMM(BPF_JA, 0, 0, 1); > >> >>> + *patch++ =3D BPF_MOV64_IMM(insn->dst_reg, 0); > >> >> > >> >> But how does this address other cases where we could fault e.g. non= -canonical, > >> >> vsyscall page, etc? Technically, we would have to call to copy_from= _kernel_nofault_allowed() > >> >> to really address all the cases aside from the overflow (good catch= btw!) where kernel > >> >> turns into user address. > >> > > >> > So, we are trying to ~simulate a call to > >> > copy_from_kernel_nofault_allowed() here. If the address under > >> > consideration is below TASK_SIZE (TASK_SIZE + 4GB to be precise) the= n we > >> > skip that load because that address could be mapped by the user. > >> > > >> > If the address is above TASK_SIZE + 4GB, we allow the load and it co= uld > >> > cause a fault if the address is invalid, non-canonical etc. Taking t= he > >> > fault is fine because JIT will add an exception table entry for > >> > for that load with BPF_PBOBE_MEM. > >> > >> Are you sure? I don't think the kernel handles non-canonical fixup. > > > > I believe it handles it fine otherwise our selftest bpf_testmod_return_= ptr: > > case 4: return (void *)(1ull << 60); /* non-canonical and invalid= */ > > would have been crashing for the last 3 years, > > since we've been running it. > > > >> > The vsyscall page is special, this approach skips all loads from thi= s > >> > page. I am not sure if that is acceptable. > >> > >> The bpf_probe_read_kernel() does handle it fine via copy_from_kernel_n= ofault(). > >> > >> So there is tail risk that BPF_PROBE_* could trigger a crash. > > > > For this patch let's do > > return max(TASK_SIZE_MAX + PAGE_SIZE, VSYSCALL_ADDR) > > to cover both with one check? > > I agree, will add this in the next version. Sorry. I didn't look at actual value when I suggested this. Let's think how to check for them with one conditional branch. Maybe we can REG_AX >>=3D 24 instead of 32 and do some clever math, since we need to: addr < 0x7ffffful || addr =3D=3D 0xfffffffffful ? If we have to do two branches that's fine for now, but we probably need to leave it to JITs, since they can emit two faster conditional moves and use single conditional jump or introduce new pseudo bpf insns equivalent to x86 set[ae|ne|..]