Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp3543079pxb; Mon, 24 Jan 2022 11:50:34 -0800 (PST) X-Google-Smtp-Source: ABdhPJxxfLKAW/HWXl7oCX6ycTlTr02DD0X11LVnGDawhsnIsoTKv/qTIL9mdycGIjQ1e26m8GcB X-Received: by 2002:a17:902:7fc8:b0:14a:e403:2f18 with SMTP id t8-20020a1709027fc800b0014ae4032f18mr15748926plb.45.1643053833996; Mon, 24 Jan 2022 11:50:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643053833; cv=none; d=google.com; s=arc-20160816; b=armc+/ojmZ75A1/tdipKb6pL0nv9NhUID/2Jn0D0PIp73neEyqfH9vX5/f7MYUQlX2 BRMlS5QxRaB8yGlVQRexnmt0tGGAm0XqkeNgHv4XWdlciELSxJlaWHQUqWgW4+OR/7H7 x/rjoaxSniSBuO3yGxWaA1uVTDRnRGBoxJpW4e1RALEoEHtyPAFJMFaPtfIpBVNR75El XiqHdDg8bmXUqFjHOg4mK2oSYrDH/v3K2G89AFli/Kct05kNXhUtJO5KgDnHUgYfG1CA BgeNGmDCwCFbTmHs3UTnMoHSRAOCQQvO4MMmlf3GJt3HNPu/oOPz35aTuXOsAQxLn3Ht pknw== 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=TFKsp+57v018bGervn3rQX2/toL0nwmJo1LVwhe4qDg=; b=mvnhI7lg7mgC1zukraIA9mxZbnT4nmfDqloiRAFaAqIr9psQMTFzAmya2AFhqKw1uX n26pxegfo0vIl5EJ2UxT6jx2rhBOzMNyPnY9ssbTIW54bxzugbcUSGEU+e+DOYhSs/y9 hEXtrJxUTagjVnGyUieH+2TUbABCbKux3hGCf0/aJU0KlrM7UVRu1OcOzabBOItCaHNw /mGzCaSpbZ/wiaTlFDiGjJN1ZCpjotcbVpcfWfP/9FBfXK3jLn04rPiBWUD1ZfIODVgg Xh+iPRCbB7cZR5L0AkPoJ8I/ZQA+rkJwquXp1bwSOAhpS/nx7PMgv9PBj+rZHghRNvl4 1KSA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=tAzFc365; 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=linux.dev Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id d6si14070287pgc.95.2022.01.24.11.50.18; Mon, 24 Jan 2022 11:50:33 -0800 (PST) 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=@linux.dev header.s=key1 header.b=tAzFc365; 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=linux.dev Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245047AbiAXSHt (ORCPT + 99 others); Mon, 24 Jan 2022 13:07:49 -0500 Received: from out0.migadu.com ([94.23.1.103]:19471 "EHLO out0.migadu.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230334AbiAXSH3 (ORCPT ); Mon, 24 Jan 2022 13:07:29 -0500 X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1643047648; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=TFKsp+57v018bGervn3rQX2/toL0nwmJo1LVwhe4qDg=; b=tAzFc3657Ljq58oSs1g1Uh5gBAS52GUGRtRzaAWSSDmM8vIMziT4XUSKTfHYQDufsnQiCv 9SA7a0/7slRa/2S4ClTsPByHC/qvHasd5HObqMOL5D9RPZ/Xg3D+0G8QX3KRuXrNFltFqx h1C9iGZMrHa5r64q9R3LHZv+a9jDzzI= From: andrey.konovalov@linux.dev To: Andrew Morton Cc: Andrey Konovalov , Marco Elver , Alexander Potapenko , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, linux-mm@kvack.org, Vincenzo Frascino , Catalin Marinas , Will Deacon , Mark Rutland , linux-arm-kernel@lists.infradead.org, Peter Collingbourne , Evgenii Stepanov , linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH v6 32/39] kasan, arm64: don't tag executable vmalloc allocations Date: Mon, 24 Jan 2022 19:05:06 +0100 Message-Id: In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: linux.dev Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Andrey Konovalov Besides asking vmalloc memory to be executable via the prot argument of __vmalloc_node_range() (see the previous patch), the kernel can skip that bit and instead mark memory as executable via set_memory_x(). Once tag-based KASAN modes start tagging vmalloc allocations, executing code from such allocations will lead to the PC register getting a tag, which is not tolerated by the kernel. Generic kernel code typically allocates memory via module_alloc() if it intends to mark memory as executable. (On arm64 module_alloc() uses __vmalloc_node_range() without setting the executable bit). Thus, reset pointer tags of pointers returned from module_alloc(). However, on arm64 there's an exception: the eBPF subsystem. Instead of using module_alloc(), it uses vmalloc() (via bpf_jit_alloc_exec()) to allocate its JIT region. Thus, reset pointer tags of pointers returned from bpf_jit_alloc_exec(). Resetting tags for these pointers results in untagged pointers being passed to set_memory_x(). This causes conflicts in arithmetic checks in change_memory_common(), as vm_struct->addr pointer returned by find_vm_area() is tagged. Reset pointer tag of find_vm_area(addr)->addr in change_memory_common(). Signed-off-by: Andrey Konovalov Acked-by: Catalin Marinas --- Changes v3->v4: - Reset pointer tag in change_memory_common(). Changes v2->v3: - Add this patch. --- arch/arm64/kernel/module.c | 3 ++- arch/arm64/mm/pageattr.c | 2 +- arch/arm64/net/bpf_jit_comp.c | 3 ++- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/arch/arm64/kernel/module.c b/arch/arm64/kernel/module.c index d3a1fa818348..f2d4bb14bfab 100644 --- a/arch/arm64/kernel/module.c +++ b/arch/arm64/kernel/module.c @@ -63,7 +63,8 @@ void *module_alloc(unsigned long size) return NULL; } - return p; + /* Memory is intended to be executable, reset the pointer tag. */ + return kasan_reset_tag(p); } enum aarch64_reloc_op { diff --git a/arch/arm64/mm/pageattr.c b/arch/arm64/mm/pageattr.c index a3bacd79507a..64e985eaa52d 100644 --- a/arch/arm64/mm/pageattr.c +++ b/arch/arm64/mm/pageattr.c @@ -85,7 +85,7 @@ static int change_memory_common(unsigned long addr, int numpages, */ area = find_vm_area((void *)addr); if (!area || - end > (unsigned long)area->addr + area->size || + end > (unsigned long)kasan_reset_tag(area->addr) + area->size || !(area->flags & VM_ALLOC)) return -EINVAL; diff --git a/arch/arm64/net/bpf_jit_comp.c b/arch/arm64/net/bpf_jit_comp.c index e96d4d87291f..2198af06ae6a 100644 --- a/arch/arm64/net/bpf_jit_comp.c +++ b/arch/arm64/net/bpf_jit_comp.c @@ -1150,7 +1150,8 @@ u64 bpf_jit_alloc_exec_limit(void) void *bpf_jit_alloc_exec(unsigned long size) { - return vmalloc(size); + /* Memory is intended to be executable, reset the pointer tag. */ + return kasan_reset_tag(vmalloc(size)); } void bpf_jit_free_exec(void *addr) -- 2.25.1