Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp3543018pxb; Mon, 24 Jan 2022 11:50:30 -0800 (PST) X-Google-Smtp-Source: ABdhPJwPfBTr7l1+y1u7TJ36IzIalUyk28yHHZq6so2SI9/qg/WUWGdkc0V7XbaQ6gFmbxLX66af X-Received: by 2002:a17:90b:3841:: with SMTP id nl1mr2901867pjb.42.1643053829777; Mon, 24 Jan 2022 11:50:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643053829; cv=none; d=google.com; s=arc-20160816; b=hoXBM3fi3q0oQ0TIHGoqhZrZgPZIW/E1lI27/Vjn4V0z1IjEsryQno7oafMoIsMF2o KjI7T5CIMPDdKkpSbldWkCpgnQ02Ziqzwro/r7Dt0+QPoMia46kVJw5W3pToO/APe0iS Iy6ncJBXX/0a2DS8ogiIsEAVmOfZ1XepySwwDCNY/eicDTXmP1Ue5UQ2++Fvo6RGCHGP 29iW3YSbxGxAA7J8/oagZpWcUYQTxNVeaO0d0ZDeDgl2mV5ZdsFy3zapK/L5vJNWaZlX /62Dl8OAXK7C4lQoC8gJUVZb17fsm2w8P7PAnEbWzrdbEu9SrZ89Dp9DGxUNr33r9VMG Latw== 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=4/y0HjGmlj8gRZl7sKKC15OiIQ+URUy4QXcXfGAQLsE=; b=Ds2ozTd0bPWnKOygR3TcTsxx5IOtN3KBdi/Ec7+OwAb577OoiudAjTb0sjDm52YkNy MheXbPkA8i/GJr6sOL6ffCRHXlGvhxVRdXp+mEdDOtMnBb7Uf0xsZKGzWZgA+LX4O9Hi Cl8Zzi7zJUa5bOSy+juljVjatAjQpXGwP/zIEqf16f3WvDzSYorX2c8UT7Iwj0cH3b9y G8L5Tx8CcY48nAtrFgkpjQEq8F495GB5A44xOF/YXUur+j6xzLqPCcvPIAqgevrQy8/B c/oZbvGugnWXH+px5qoONI5cvPpsvY/5FvdewKu33sxzLnKQ+M3qp3fU7jo+3WwIGSo/ e2lw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=cEt2JHsQ; 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 a8si15855160plh.16.2022.01.24.11.50.17; Mon, 24 Jan 2022 11:50:29 -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=cEt2JHsQ; 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 S245076AbiAXSGb (ORCPT + 99 others); Mon, 24 Jan 2022 13:06:31 -0500 Received: from out0.migadu.com ([94.23.1.103]:18946 "EHLO out0.migadu.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S245002AbiAXSGY (ORCPT ); Mon, 24 Jan 2022 13:06:24 -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=1643047583; 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=4/y0HjGmlj8gRZl7sKKC15OiIQ+URUy4QXcXfGAQLsE=; b=cEt2JHsQwrkLVfMoZK13nWffMlEiwZaIGbGyF4gC8cAeDpRvqj1bkguYhGcbPfveiE3JA2 +1sTkZavlWgOjB5XJNxkdXXXbmh7xEABFORykbamGRFegBc2vbxDSqj+Mw7AZ4f8QGXi31 /c/hBakatNfmGBIuDYr5ea0bqnV1CIo= 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 26/39] kasan, vmalloc: unpoison VM_ALLOC pages after mapping Date: Mon, 24 Jan 2022 19:05:00 +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 Make KASAN unpoison vmalloc mappings after they have been mapped in when it's possible: for vmalloc() (indentified via VM_ALLOC) and vm_map_ram(). The reasons for this are: - For vmalloc() and vm_map_ram(): pages don't get unpoisoned in case mapping them fails. - For vmalloc(): HW_TAGS KASAN needs pages to be mapped to set tags via kasan_unpoison_vmalloc(). As a part of these changes, the return value of __vmalloc_node_range() is changed to area->addr. This is a non-functional change, as __vmalloc_area_node() returns area->addr anyway. Signed-off-by: Andrey Konovalov Reviewed-by: Alexander Potapenko --- Changes v3->v4: - Don't forget to save tagged addr to vm_struct->addr for VM_ALLOC so that find_vm_area(addr)->addr == addr for vmalloc(). - Reword comments. - Update patch description. Changes v2->v3: - Update patch description. --- mm/vmalloc.c | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/mm/vmalloc.c b/mm/vmalloc.c index 92e635b7490c..b65adac1cd80 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -2210,14 +2210,15 @@ void *vm_map_ram(struct page **pages, unsigned int count, int node) mem = (void *)addr; } - mem = kasan_unpoison_vmalloc(mem, size); - if (vmap_pages_range(addr, addr + size, PAGE_KERNEL, pages, PAGE_SHIFT) < 0) { vm_unmap_ram(mem, count); return NULL; } + /* Mark the pages as accessible, now that they are mapped. */ + mem = kasan_unpoison_vmalloc(mem, size); + return mem; } EXPORT_SYMBOL(vm_map_ram); @@ -2445,7 +2446,14 @@ static struct vm_struct *__get_vm_area_node(unsigned long size, setup_vmalloc_vm(area, va, flags, caller); - area->addr = kasan_unpoison_vmalloc(area->addr, requested_size); + /* + * Mark pages for non-VM_ALLOC mappings as accessible. Do it now as a + * best-effort approach, as they can be mapped outside of vmalloc code. + * For VM_ALLOC mappings, the pages are marked as accessible after + * getting mapped in __vmalloc_node_range(). + */ + if (!(flags & VM_ALLOC)) + area->addr = kasan_unpoison_vmalloc(area->addr, requested_size); return area; } @@ -3055,7 +3063,7 @@ void *__vmalloc_node_range(unsigned long size, unsigned long align, const void *caller) { struct vm_struct *area; - void *addr; + void *ret; unsigned long real_size = size; unsigned long real_align = align; unsigned int shift = PAGE_SHIFT; @@ -3117,10 +3125,13 @@ void *__vmalloc_node_range(unsigned long size, unsigned long align, prot = arch_vmap_pgprot_tagged(prot); /* Allocate physical pages and map them into vmalloc space. */ - addr = __vmalloc_area_node(area, gfp_mask, prot, shift, node); - if (!addr) + ret = __vmalloc_area_node(area, gfp_mask, prot, shift, node); + if (!ret) goto fail; + /* Mark the pages as accessible, now that they are mapped. */ + area->addr = kasan_unpoison_vmalloc(area->addr, real_size); + /* * In this function, newly allocated vm_struct has VM_UNINITIALIZED * flag. It means that vm_struct is not fully initialized. @@ -3132,7 +3143,7 @@ void *__vmalloc_node_range(unsigned long size, unsigned long align, if (!(vm_flags & VM_DEFER_KMEMLEAK)) kmemleak_vmalloc(area, size, gfp_mask); - return addr; + return area->addr; fail: if (shift > PAGE_SHIFT) { @@ -3816,7 +3827,10 @@ struct vm_struct **pcpu_get_vm_areas(const unsigned long *offsets, } spin_unlock(&vmap_area_lock); - /* mark allocated areas as accessible */ + /* + * Mark allocated areas as accessible. Do it now as a best-effort + * approach, as they can be mapped outside of vmalloc code. + */ for (area = 0; area < nr_vms; area++) vms[area]->addr = kasan_unpoison_vmalloc(vms[area]->addr, vms[area]->size); -- 2.25.1