Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp1276323lqp; Fri, 22 Mar 2024 10:05:35 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCW2d7phjXl5aLl6nipzttBGsUwWHtWJxQD+FOo03Wbu3Thvxa9BIkl8ZN6VQJ2Y+2Qg1jaiPldN6d3fKICvbIr7gf7OIkTah/JxoHi95A== X-Google-Smtp-Source: AGHT+IEzW3pqZp9N8VRpi2fRNZ9xrpqsdnW9PPpe8jdV/0cbKA+DbPKMp9DCND9Qwhf5i47/om26 X-Received: by 2002:a17:902:e747:b0:1de:fc12:f66c with SMTP id p7-20020a170902e74700b001defc12f66cmr419631plf.27.1711127135172; Fri, 22 Mar 2024 10:05:35 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711127135; cv=pass; d=google.com; s=arc-20160816; b=w9bPAKAat5SkWNg8llx3HD7Vo2uSmpHmFT4WNAyL2mJPl8Li2/e2uPaH6XWB5UfgwR xqxUw/emlSm2lDxKIF50syYKl2TT7TpfFFQJS361z/oAAoZQD+5FE3+QnhK8JgpKf+N7 sM18jjdVI2HXGmrpvMw88/gjRomsR9bb4l+4UhJ0zWIKMl51TyHDrFLIKWeQ5XwuKazB bFrDbShX35plHl2e1w3i1919zq2Wyu1uvaV2da6hZlJH1meHxwzcT2BT8EcoKc7KT2f8 8sYab4VT/bG4kHf9egJTdPoYRBGfuAuhDOeCX9IkRPOWQ6cFSQtrA8eh+s6+xvNbYcdi HMXw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=mime-version:list-unsubscribe:list-subscribe:list-id:precedence :message-id:date:references:in-reply-to:subject:to:from :dkim-signature; bh=prwGqiUDr2MIwI/u9qAD6n1rOIPI2RfxncckA/0+HG4=; fh=640q2th+ly+DqZ+bvrapWyF0Jhl5fU7LhJ1ClFThXOQ=; b=lAKh1RVBwdyUQi86ntb3kd3sKiCk29VRTmoZoICelDwJWFOOvWpn6vRDf+hmyq9OF7 LZRDL4ki7yI3ftk+kf7l7zonCQENoeCNL9nGBnEHL3Ac7Yh6YLgB6VGadDGnQeRHSFPd u3NPpU/U/UgSOkd5Q1/i2gVWkE37ubaTeuMXM9M+cfw226JtGBUxmgZg6oFBPGUKzmm2 411STbmitz4IPCjsdF9zNTLfMmbwusa45omosZ9oA4fmO4A/uKhX7UCql+q6RTPNFEU5 fW+xeIEuENLDJhXf3tkHaz94vFjTyF994saJSLjN4haUhqGlON45aLOxOzbxuP+K8iVr EVYA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=fSTRqoW8; 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-111829-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-111829-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id u17-20020a170902e81100b001e030ca0607si2464574plg.161.2024.03.22.10.05.34 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Mar 2024 10:05:35 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-111829-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=fSTRqoW8; 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-111829-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-111829-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 8C1782898A1 for ; Fri, 22 Mar 2024 16:57:40 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2E3C05FDC9; Fri, 22 Mar 2024 16:53:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="fSTRqoW8" Received: from mail-wr1-f46.google.com (mail-wr1-f46.google.com [209.85.221.46]) (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 79EB15FDA9; Fri, 22 Mar 2024 16:53:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.46 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711126434; cv=none; b=TbIVGtju+9PBzPLrtD1kX5qqn4d4LfcPylAMRoLv3hp9WChhiEhb984HARgOG6TzQn0LUqaHGp8C4v4oZiVRHH0yz0Y0d5h2R0d/mI0jhH3IvuL200vbjpeGCOpR+yTZ6KQlozgAL+hKVQCCYSgvpq0QRMWsUsu1S03g5068/YE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711126434; c=relaxed/simple; bh=Rc4e+9q8cteiwcWOIW7T38A8/ToMIlusvAVBDyOL15M=; h=From:To:Subject:In-Reply-To:References:Date:Message-ID: MIME-Version:Content-Type; b=S9f987mJBqRcXtUS1B54gNZz9B2zDOITnnnrwUPgaUKDh8wzgIvHxKDocdfUgNm3A3DWvkG6+2xzOlEfW2E1V00h9waHpcsvLITLPDmfgKMymTzVBA4qIlNMfWqUs3fw+8VuVSuWCSMOKzF4uv3HsRPodk3wGpVQnfOGV+BXR5M= 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=fSTRqoW8; arc=none smtp.client-ip=209.85.221.46 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-f46.google.com with SMTP id ffacd0b85a97d-33e570ef661so1085874f8f.1; Fri, 22 Mar 2024 09:53:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1711126431; x=1711731231; darn=vger.kernel.org; h=mime-version:message-id:date:references:in-reply-to:subject:to:from :from:to:cc:subject:date:message-id:reply-to; bh=prwGqiUDr2MIwI/u9qAD6n1rOIPI2RfxncckA/0+HG4=; b=fSTRqoW8949esGuLPcYwLS05SHr93J/p4Q+HD3xdNqD32TbrFtjBMb/Cq27vplJFav Q4Q2yxfLIwU2IMT9CTvA3raUW3NxxJq0cARd8qNii89PELbVD1y4MrsYX50IC4j38GmE 1HLwWRqzv9eSsV6oA7GYDMgBlNYKeB9LkPRYH8o1F98R2hUEBxbga+x4oZgsnbMNyN+q fpzQq3ermhDhLIXPBM4qg6CcTW2HAAa3nTIlGeuq5nz1YP6QpkhnqnvQzlBo4gbXBIey LQEXdzK8+oNky4SuXsOvXNrl14W1P0wKnyOyscIUcThiphObd7hhjplIVShdT6Qp4dBH SxVQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711126431; x=1711731231; h=mime-version:message-id:date:references:in-reply-to:subject:to:from :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=prwGqiUDr2MIwI/u9qAD6n1rOIPI2RfxncckA/0+HG4=; b=m/wT/3ElWiJII232Yc9R3+cqwoqHOEfhKNgR+cKHGP04iFjuBuoTcRObZxv9AdlKPY X/oE1L+vwp7PbuihPBMOkAiLvInbPlV/VkslHA0JlI0/Fsgxy5lVuvzMbivzJKLb7jqQ 9xWQ2LvLuqHqKHXgdjPlpINR6c8J2NflLwNsy7JoXSA4E0PGF04uGdS/KGGSTHb9rZkB PmfFJ2Yo7rTWToWbHixJrrmLwPTA4FXx9jaaDqeVx2bms2QIYX0l0YdlPKszoIJi8OmD FY7AQg5RiHNxq3RzOwrI62Bk+ffdUc3fHwu8JKo+exTuUe4ZC1Pm3OqiT9BS/bZ+5NAl NULw== X-Forwarded-Encrypted: i=1; AJvYcCV5rhlBgcA4TcW5c+BaenT/qO4MsKUEUqekTmdbX9Eers8Ul12WmVQqbkVINI6zdXyIyVNlfKAmyzG+DHkamO9/lHilmSm8QBz7x2FDlc947jxCXJgSIGXKhuG01AQxpTlnnKbvjm0IbwT+4y/CecWIEdpTEs1n7Gln X-Gm-Message-State: AOJu0YzuzuorbiMDA5wObNvxfU9nbYbbAuEhxif043FJJSZpzRZahBHs wqL6ZWeqvb56VP2yMVKgQB4gyBrcK1QQUZs2v3D81vlm7u2DI5fS X-Received: by 2002:a5d:4944:0:b0:33e:7cdd:b559 with SMTP id r4-20020a5d4944000000b0033e7cddb559mr1991710wrs.56.1711126430520; Fri, 22 Mar 2024 09:53:50 -0700 (PDT) Received: from localhost (54-240-197-231.amazon.com. [54.240.197.231]) by smtp.gmail.com with ESMTPSA id y17-20020adff151000000b0033e43756d11sm2435796wro.85.2024.03.22.09.53.49 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 22 Mar 2024 09:53:50 -0700 (PDT) From: Puranjay Mohan To: 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@kernel.org, "H. Peter Anvin" , Jean-Philippe Brucker , netdev@vger.kernel.org, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, Ilya Leoshkevich Subject: Re: [PATCH bpf v4] bpf: verifier: prevent userspace memory access In-Reply-To: <15ba79e3-14b2-d92e-3f94-e4f5f963e15d@iogearbox.net> References: <20240321124640.8870-1-puranjay12@gmail.com> <9f2b63b5-569c-1e00-a635-93d9cd695517@iogearbox.net> <15ba79e3-14b2-d92e-3f94-e4f5f963e15d@iogearbox.net> Date: Fri, 22 Mar 2024 16:53:47 +0000 Message-ID: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain Daniel Borkmann writes: > On 3/22/24 4:05 PM, Puranjay Mohan wrote: > [...] >>>> + /* Make it impossible to de-reference a userspace address */ >>>> + if (BPF_CLASS(insn->code) == BPF_LDX && >>>> + (BPF_MODE(insn->code) == BPF_PROBE_MEM || >>>> + BPF_MODE(insn->code) == BPF_PROBE_MEMSX)) { >>>> + struct bpf_insn *patch = &insn_buf[0]; >>>> + u64 uaddress_limit = bpf_arch_uaddress_limit(); >>>> + >>>> + if (!uaddress_limit) >>>> + goto next_insn; >>>> + >>>> + *patch++ = BPF_MOV64_REG(BPF_REG_AX, insn->src_reg); >>>> + if (insn->off) >>>> + *patch++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_AX, insn->off); >>>> + *patch++ = BPF_ALU64_IMM(BPF_RSH, BPF_REG_AX, 32); >>>> + *patch++ = BPF_JMP_IMM(BPF_JLE, BPF_REG_AX, uaddress_limit >> 32, 2); >>>> + *patch++ = *insn; >>>> + *patch++ = BPF_JMP_IMM(BPF_JA, 0, 0, 1); >>>> + *patch++ = 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) then 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 could >> cause a fault if the address is invalid, non-canonical etc. Taking the >> 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. Atleast for ARM64 for I don't see a differentiation between the handling of canonical and non-canonical addresses. do_translation_fault() checks if addr < TASK_SIZE and calls do_page_fault() or if the address is greater than TASK_SIZE (it is a kernel address), do_bad_area() is called. Both of these call __do_kernel_fault() if fault is from kernel mode and it does fixup_exception(). > >> The vsyscall page is special, this approach skips all loads from this >> page. I am not sure if that is acceptable. > > The bpf_probe_read_kernel() does handle it fine via copy_from_kernel_nofault(). bpf_probe_read_kernel() is skipping reading from the vsyscall page, that is what this patch does as well. ARM64, RISCV, and some other archs don't implement copy_from_kernel_nofault_allowed() so I think the we should fix the common case where the BPF program should not be allowed to access memory below TASK_SIZE. This would be true for all architectures. > > So there is tail risk that BPF_PROBE_* could trigger a crash. Other archs might Can you explain this a bit more, how will BPF_PROBE_* trigger a crash? > have other quirks, e.g. in case of loongarch it says highest bit set means kernel > space. Thanks, Puranjay