Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp4441391pxv; Tue, 6 Jul 2021 00:36:08 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxBwFZIScIG4oELTvR7joi6aguly8FFs3eyruVfSMHr7k9bKDHhbb+z7Om2a9qqDhkmQ3dY X-Received: by 2002:aa7:dc01:: with SMTP id b1mr20880656edu.239.1625556968781; Tue, 06 Jul 2021 00:36:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1625556968; cv=none; d=google.com; s=arc-20160816; b=LlZNaNvJ6Lmb31xSkDqfvvyLs0ysV1b67n75gO27ODDUlm2XAuG/J2vFVCxGaK8JV2 79sgRQctIj/VBHMHhUklMGw78pLfriPxt/C58TPJuzO4RJYQbAa1QuZWBX9UkDWKa/eU gN6l403b4Lv5YgwLb1lH7vWeF3oO3amcFcW9YJcbawrcisOK8gYF1vCEdeOeHJrHJ5g5 9hYT5TXT648ug/4+ZoVfvbZp/QVxeSDGng4VUS7JXM88HoPNL3d9BAlogxtd3HY5HyhX ITeX1W7A8mnHNoFx8m+/R6NcDZX3uBq4EXzk+KeSe7MT/TC6/ag69OC9d/vLpQZJtwTD 0RsQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=Gew95MZVARp719IXqnhXu+l8ESoMwp/ceIvRn2Onm3g=; b=JHllrLUxpO2ZJsBS6OsNIaRQFTyPX/HNfXsFdRA5f0I9nNi0x8T8KKePE1SBrcLaZf X7q2b4OUBNmGnOSryB4vd2DN7sZCBvLcEi/55Gm/pTrpM/ho8+00mm34uAlDP+NXd5jN O5VJvreeX8q50ITj0Z1BhsvtzmWdT3mmFhxuRiX9WpfHJFE4rLgb2+yXc6uMjsoWXygg kbQWrUegQyMSxljLlAvCKsO0oHWR2TzhWIa+6l8bYiKI/63xyrtJ1QkaWCkQ6zNyy2Sw m94aZJvxmiqqvFUMEDQEMgQ5jHdlLeSPBXFvuh4d2Pxdb9LzreVTWTi7IVzHBHXcnTer 1rTw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ibm.com header.s=pp1 header.b=olfjstCW; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=ibm.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id r2si9721083eds.576.2021.07.06.00.35.45; Tue, 06 Jul 2021 00:36:08 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@ibm.com header.s=pp1 header.b=olfjstCW; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=ibm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230389AbhGFHgN (ORCPT + 99 others); Tue, 6 Jul 2021 03:36:13 -0400 Received: from mx0b-001b2d01.pphosted.com ([148.163.158.5]:14304 "EHLO mx0a-001b2d01.pphosted.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S230367AbhGFHgM (ORCPT ); Tue, 6 Jul 2021 03:36:12 -0400 Received: from pps.filterd (m0098416.ppops.net [127.0.0.1]) by mx0b-001b2d01.pphosted.com (8.16.0.43/8.16.0.43) with SMTP id 1667Wf0L143497; Tue, 6 Jul 2021 03:33:03 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding; s=pp1; bh=Gew95MZVARp719IXqnhXu+l8ESoMwp/ceIvRn2Onm3g=; b=olfjstCWcoU196d8uV0n9vh3QwTaArdfTeZPWU3iGrKgDYWiPFFEpeZCLsStFDMWibmB hn45bHPPo1dAr7l41tB0CzU5PcUJB9I9rt7WHvUKwNj5yoWLMjTQAXJSqcKR4T7v4FX8 viJU1LzdQVVOsxQV/2h2R+s63hYJmJhYsAczl5mSCep+6wjIB3Mwfaep85GHHG/o2HzC JBZa/+RePWzA+2Q2vMOAWprOBJVNreEhCpiyzS6GMdQkAXUU3DoY8+QQt6mMM0wM7+rl d9tgrCyT+iW9pR/isOCDosA/kVK0NmVzRGkn6e6y5iQNJEqgxv4q+n3i7YEfQ3tRdzUQ oA== Received: from pps.reinject (localhost [127.0.0.1]) by mx0b-001b2d01.pphosted.com with ESMTP id 39mbkds3mw-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 06 Jul 2021 03:33:03 -0400 Received: from m0098416.ppops.net (m0098416.ppops.net [127.0.0.1]) by pps.reinject (8.16.0.43/8.16.0.43) with SMTP id 1667X3SR145418; Tue, 6 Jul 2021 03:33:03 -0400 Received: from ppma02fra.de.ibm.com (47.49.7a9f.ip4.static.sl-reverse.com [159.122.73.71]) by mx0b-001b2d01.pphosted.com with ESMTP id 39mbkds3m8-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 06 Jul 2021 03:33:02 -0400 Received: from pps.filterd (ppma02fra.de.ibm.com [127.0.0.1]) by ppma02fra.de.ibm.com (8.16.1.2/8.16.1.2) with SMTP id 1667FkG0013626; Tue, 6 Jul 2021 07:33:01 GMT Received: from b06cxnps4074.portsmouth.uk.ibm.com (d06relay11.portsmouth.uk.ibm.com [9.149.109.196]) by ppma02fra.de.ibm.com with ESMTP id 39jfh88kpt-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 06 Jul 2021 07:33:01 +0000 Received: from d06av21.portsmouth.uk.ibm.com (d06av21.portsmouth.uk.ibm.com [9.149.105.232]) by b06cxnps4074.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 1667WwAH23265774 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 6 Jul 2021 07:32:58 GMT Received: from d06av21.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id F255D52052; Tue, 6 Jul 2021 07:32:57 +0000 (GMT) Received: from bangoria.ibmuc.com (unknown [9.199.43.134]) by d06av21.portsmouth.uk.ibm.com (Postfix) with ESMTP id A32C552059; Tue, 6 Jul 2021 07:32:51 +0000 (GMT) From: Ravi Bangoria To: naveen.n.rao@linux.ibm.com, mpe@ellerman.id.au, ast@kernel.org, daniel@iogearbox.net Cc: ravi.bangoria@linux.ibm.com, sandipan@linux.ibm.com, paulus@samba.org, andrii@kernel.org, kafai@fb.com, songliubraving@fb.com, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, netdev@vger.kernel.org, bpf@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org Subject: [PATCH 4/4] bpf powerpc: Add addr > TASK_SIZE_MAX explicit check Date: Tue, 6 Jul 2021 13:02:11 +0530 Message-Id: <20210706073211.349889-5-ravi.bangoria@linux.ibm.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210706073211.349889-1-ravi.bangoria@linux.ibm.com> References: <20210706073211.349889-1-ravi.bangoria@linux.ibm.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-TM-AS-GCONF: 00 X-Proofpoint-GUID: qesT2bE4nOfw031iJk2-FJ_fyRTXBqXS X-Proofpoint-ORIG-GUID: t__WcHqeh_O8KkfEbK3T3y00H-PG9cSJ X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.391,18.0.790 definitions=2021-07-06_02:2021-07-02,2021-07-06 signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 impostorscore=0 mlxscore=0 lowpriorityscore=0 clxscore=1015 mlxlogscore=999 phishscore=0 priorityscore=1501 spamscore=0 malwarescore=0 adultscore=0 bulkscore=0 suspectscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2104190000 definitions=main-2107060037 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On PowerPC with KUAP enabled, any kernel code which wants to access userspace needs to be surrounded by disable-enable KUAP. But that is not happening for BPF_PROBE_MEM load instruction. So, when BPF program tries to access invalid userspace address, page-fault handler considers it as bad KUAP fault: Kernel attempted to read user page (d0000000) - exploit attempt? (uid: 0) Considering the fact that PTR_TO_BTF_ID (which uses BPF_PROBE_MEM mode) could either be a valid kernel pointer or NULL but should never be a pointer to userspace address, execute BPF_PROBE_MEM load only if addr > TASK_SIZE_MAX, otherwise set dst_reg=0 and move on. This will catch NULL, valid or invalid userspace pointers. Only bad kernel pointer will be handled by BPF exception table. [Alexei suggested for x86] Suggested-by: Alexei Starovoitov Signed-off-by: Ravi Bangoria --- arch/powerpc/net/bpf_jit_comp64.c | 38 +++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/arch/powerpc/net/bpf_jit_comp64.c b/arch/powerpc/net/bpf_jit_comp64.c index 1884c6dca89a..46becae76210 100644 --- a/arch/powerpc/net/bpf_jit_comp64.c +++ b/arch/powerpc/net/bpf_jit_comp64.c @@ -753,6 +753,14 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, struct codegen_context * /* dst = *(u8 *)(ul) (src + off) */ case BPF_LDX | BPF_MEM | BPF_B: case BPF_LDX | BPF_PROBE_MEM | BPF_B: + if (BPF_MODE(code) == BPF_PROBE_MEM) { + EMIT(PPC_RAW_ADDI(b2p[TMP_REG_1], src_reg, off)); + PPC_LI64(b2p[TMP_REG_2], TASK_SIZE_MAX); + EMIT(PPC_RAW_CMPLD(b2p[TMP_REG_1], b2p[TMP_REG_2])); + PPC_BCC(COND_GT, (ctx->idx + 4) * 4); + EMIT(PPC_RAW_XOR(dst_reg, dst_reg, dst_reg)); + PPC_JMP((ctx->idx + 2) * 4); + } EMIT(PPC_RAW_LBZ(dst_reg, src_reg, off)); if (insn_is_zext(&insn[i + 1])) addrs[++i] = ctx->idx * 4; @@ -763,6 +771,14 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, struct codegen_context * /* dst = *(u16 *)(ul) (src + off) */ case BPF_LDX | BPF_MEM | BPF_H: case BPF_LDX | BPF_PROBE_MEM | BPF_H: + if (BPF_MODE(code) == BPF_PROBE_MEM) { + EMIT(PPC_RAW_ADDI(b2p[TMP_REG_1], src_reg, off)); + PPC_LI64(b2p[TMP_REG_2], TASK_SIZE_MAX); + EMIT(PPC_RAW_CMPLD(b2p[TMP_REG_1], b2p[TMP_REG_2])); + PPC_BCC(COND_GT, (ctx->idx + 4) * 4); + EMIT(PPC_RAW_XOR(dst_reg, dst_reg, dst_reg)); + PPC_JMP((ctx->idx + 2) * 4); + } EMIT(PPC_RAW_LHZ(dst_reg, src_reg, off)); if (insn_is_zext(&insn[i + 1])) addrs[++i] = ctx->idx * 4; @@ -773,6 +789,14 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, struct codegen_context * /* dst = *(u32 *)(ul) (src + off) */ case BPF_LDX | BPF_MEM | BPF_W: case BPF_LDX | BPF_PROBE_MEM | BPF_W: + if (BPF_MODE(code) == BPF_PROBE_MEM) { + EMIT(PPC_RAW_ADDI(b2p[TMP_REG_1], src_reg, off)); + PPC_LI64(b2p[TMP_REG_2], TASK_SIZE_MAX); + EMIT(PPC_RAW_CMPLD(b2p[TMP_REG_1], b2p[TMP_REG_2])); + PPC_BCC(COND_GT, (ctx->idx + 4) * 4); + EMIT(PPC_RAW_XOR(dst_reg, dst_reg, dst_reg)); + PPC_JMP((ctx->idx + 2) * 4); + } EMIT(PPC_RAW_LWZ(dst_reg, src_reg, off)); if (insn_is_zext(&insn[i + 1])) addrs[++i] = ctx->idx * 4; @@ -783,6 +807,20 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, struct codegen_context * /* dst = *(u64 *)(ul) (src + off) */ case BPF_LDX | BPF_MEM | BPF_DW: case BPF_LDX | BPF_PROBE_MEM | BPF_DW: + if (BPF_MODE(code) == BPF_PROBE_MEM) { + EMIT(PPC_RAW_ADDI(b2p[TMP_REG_1], src_reg, off)); + PPC_LI64(b2p[TMP_REG_2], TASK_SIZE_MAX); + EMIT(PPC_RAW_CMPLD(b2p[TMP_REG_1], b2p[TMP_REG_2])); + if (off % 4) + PPC_BCC(COND_GT, (ctx->idx + 5) * 4); + else + PPC_BCC(COND_GT, (ctx->idx + 4) * 4); + EMIT(PPC_RAW_XOR(dst_reg, dst_reg, dst_reg)); + if (off % 4) + PPC_JMP((ctx->idx + 3) * 4); + else + PPC_JMP((ctx->idx + 2) * 4); + } PPC_BPF_LL(dst_reg, src_reg, off); ret = add_extable_entry(fp, image, pass, code, ctx, dst_reg); if (ret) -- 2.26.3