Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp3036598ybi; Mon, 17 Jun 2019 15:12:22 -0700 (PDT) X-Google-Smtp-Source: APXvYqzHXBV/8Pk338Ce1HYrQ1m/rnuQTQjeohjkmy14zD/k5Od8LjXok4tk/rfDcyRYDtDRT9uL X-Received: by 2002:a17:90a:ad89:: with SMTP id s9mr1399054pjq.41.1560809542191; Mon, 17 Jun 2019 15:12:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1560809542; cv=none; d=google.com; s=arc-20160816; b=gvIU2Rym3h+RZ7xuP0DBjH1qmzMW3RTLevkWEpKntE8zVNCMn2OUi95U6edK0b4PPh 5TjzO+oCFQA5aVRPuT37zW9VZmi0f9t/vrqqXC+HuwK/ifWmLPR0IH2lxB+IIKzRgpLo HhrM6bxfFu24f3/QJLJ5g//uaMOK7czAK7/y8HgWyM5v+7ILO9fGW8349sjyVg16VUfZ 9ypzF2jhrtmhuWWfNmyZdM8A9XBEQ8vjZfAGjUmnElLybX6E/I8RUylP1qfad2V7Xw+L Y+NwJZFHH8rB8IS0nX2DAORzz0+GX8mJ3kI992t1MDQD9Jy1PBqPk1ueBH/6go+Y8jU9 8dkw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature; bh=yPilZaF4h/dGO9ftIHQBkXDAqkrv70KGQtrlQh5r36Y=; b=PLeLl/8tfat3GN5F17jQhAicthCc6dOf0grRLJB7VfwSs3uHQpgmYHmInyV26M9WKX raJD9PAD2ICJuequJiZ4U5tSTLiDFRRILA84VkA+RMu0VAO2hPioTKVeIWCwAIBuqju7 hGFJI6GpaZ5hrOxX5A+aZTboJQayE97EIP42/tACO1HfNjgOgNIlHh3vq9v+fTtyx/ZS FUm5qj/7JECUMkbwoE9xEuxyuUWKZFHMyTqneyAvpeah2QqPQm2+QweJuJ2jl1wXXgv1 zwcrvUGKPgaivqwi8RxSjTxfa6g5OKUydmYF+AVik7Bz1ouF2tJ7aDliIr4dAEuOObWb nwIA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=RoQtqypU; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id u17si12403452pfc.210.2019.06.17.15.12.07; Mon, 17 Jun 2019 15:12:22 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=RoQtqypU; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728797AbfFQWLx (ORCPT + 99 others); Mon, 17 Jun 2019 18:11:53 -0400 Received: from mail-pl1-f195.google.com ([209.85.214.195]:46546 "EHLO mail-pl1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728736AbfFQWLs (ORCPT ); Mon, 17 Jun 2019 18:11:48 -0400 Received: by mail-pl1-f195.google.com with SMTP id e5so4728979pls.13; Mon, 17 Jun 2019 15:11:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=yPilZaF4h/dGO9ftIHQBkXDAqkrv70KGQtrlQh5r36Y=; b=RoQtqypU7XfqeDUuJTl9JPFyZrZZzgzKkIFcFpho0mpTztJ5TmiJZ/9vEwGfEnxb9e rGMzDmo1IzgXXocOONgm7tIG2TRoFtCwxnQyKxVlb8Ai0eX68cqInfAqmQ4TEOGFzi2E pzbZiJnZjyz8VhqMmg34AFoT+7MzpZiFZUfxw52Uft7/HF+dDbd0HTyYR5NxNPGpjGML kB+VPbtos4BpM5RCptwCoWxzFoceIUPRXf1i9lyePk2iTgrxX71pyXIuHH3CbXRpZ45f 5rss8EqI+xTICq9zmkDGhNDOMeD+KlihLEeY8tFPjIkMgXJRNS3+H0naOTtQnBC3TC0j 08GA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=yPilZaF4h/dGO9ftIHQBkXDAqkrv70KGQtrlQh5r36Y=; b=Xrnxl9KmgqsixPb1dnVUGvYZhEF7Y8+8g4EHM7pdfD5ETpgNUMdFx96kGUUvOPAo0b YEhJ6mGiyd+zzAqY1Cr3+DJ/F+d8tkx5P11x2XLXGBBjgAkj0lXUP6Ixeg8QKsRZAq5S Lbd/9tCqenIxn6J8SonJySwb0b1UpM8AzV4wJAMv5C3EohCkhNxrAQKMG1hlNBsx+hV5 jJQgR+tEVeFW+W8tp8r5sWRvzC8lucOVtXQmhxyUN8+21ImX+Lo2KgK440FI0gSQ6Wy0 /aLtZcvu4dRxURrh/4ZIYMz1k1J1KmWhzwKNGyEpiX/8sHP0dupOdQOZa9JNa3HrjN4I NVRg== X-Gm-Message-State: APjAAAXmkylHCt1uNsMB3tUwGEve6cYh2mgwzWjGPgJcntXn+f5HKN1y MMIcte7mZRMUUT6OeP1VgmQ= X-Received: by 2002:a17:902:42d:: with SMTP id 42mr105482899ple.228.1560809507937; Mon, 17 Jun 2019 15:11:47 -0700 (PDT) Received: from fainelli-desktop.igp.broadcom.net ([192.19.223.252]) by smtp.gmail.com with ESMTPSA id s129sm12551020pfb.186.2019.06.17.15.11.45 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Mon, 17 Jun 2019 15:11:47 -0700 (PDT) From: Florian Fainelli To: linux-arm-kernel@lists.infradead.org Cc: bcm-kernel-feedback-list@broadcom.com, Andrey Ryabinin , Abbott Liu , Florian Fainelli , glider@google.com, dvyukov@google.com, corbet@lwn.net, linux@armlinux.org.uk, christoffer.dall@arm.com, marc.zyngier@arm.com, arnd@arndb.de, nico@fluxnic.net, vladimir.murzin@arm.com, keescook@chromium.org, jinb.park7@gmail.com, alexandre.belloni@bootlin.com, ard.biesheuvel@linaro.org, daniel.lezcano@linaro.org, pombredanne@nexb.com, rob@landley.net, gregkh@linuxfoundation.org, akpm@linux-foundation.org, mark.rutland@arm.com, catalin.marinas@arm.com, yamada.masahiro@socionext.com, tglx@linutronix.de, thgarnie@google.com, dhowells@redhat.com, geert@linux-m68k.org, andre.przywara@arm.com, julien.thierry@arm.com, drjones@redhat.com, philip@cog.systems, mhocko@suse.com, kirill.shutemov@linux.intel.com, kasan-dev@googlegroups.com, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, kvmarm@lists.cs.columbia.edu, ryabinin.a.a@gmail.com Subject: [PATCH v6 3/6] ARM: Replace memory function for kasan Date: Mon, 17 Jun 2019 15:11:31 -0700 Message-Id: <20190617221134.9930-4-f.fainelli@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190617221134.9930-1-f.fainelli@gmail.com> References: <20190617221134.9930-1-f.fainelli@gmail.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Andrey Ryabinin Functions like memset/memmove/memcpy do a lot of memory accesses. If bad pointer passed to one of these function it is important to catch this. Compiler's instrumentation cannot do this since these functions are written in assembly. KASan replaces memory functions with manually instrumented variants. Original functions declared as weak symbols so strong definitions in mm/kasan/kasan.c could replace them. Original functions have aliases with '__' prefix in name, so we could call non-instrumented variant if needed. We must use __memcpy/__memset to replace memcpy/memset when we copy .data to RAM and when we clear .bss, because kasan_early_init can't be called before the initialization of .data and .bss. Reported-by: Russell King - ARM Linux Signed-off-by: Abbott Liu Signed-off-by: Florian Fainelli --- arch/arm/boot/compressed/decompress.c | 2 ++ arch/arm/boot/compressed/libfdt_env.h | 2 ++ arch/arm/include/asm/string.h | 17 +++++++++++++++++ arch/arm/kernel/head-common.S | 4 ++-- arch/arm/lib/memcpy.S | 3 +++ arch/arm/lib/memmove.S | 5 ++++- arch/arm/lib/memset.S | 3 +++ 7 files changed, 33 insertions(+), 3 deletions(-) diff --git a/arch/arm/boot/compressed/decompress.c b/arch/arm/boot/compressed/decompress.c index aa075d8372ea..3794fae5f818 100644 --- a/arch/arm/boot/compressed/decompress.c +++ b/arch/arm/boot/compressed/decompress.c @@ -47,8 +47,10 @@ extern char * strchrnul(const char *, int); #endif #ifdef CONFIG_KERNEL_XZ +#ifndef CONFIG_KASAN #define memmove memmove #define memcpy memcpy +#endif #include "../../../../lib/decompress_unxz.c" #endif diff --git a/arch/arm/boot/compressed/libfdt_env.h b/arch/arm/boot/compressed/libfdt_env.h index b36c0289a308..8091efc21407 100644 --- a/arch/arm/boot/compressed/libfdt_env.h +++ b/arch/arm/boot/compressed/libfdt_env.h @@ -19,4 +19,6 @@ typedef __be64 fdt64_t; #define fdt64_to_cpu(x) be64_to_cpu(x) #define cpu_to_fdt64(x) cpu_to_be64(x) +#undef memset + #endif diff --git a/arch/arm/include/asm/string.h b/arch/arm/include/asm/string.h index 111a1d8a41dd..1f9016bbf153 100644 --- a/arch/arm/include/asm/string.h +++ b/arch/arm/include/asm/string.h @@ -15,15 +15,18 @@ extern char * strchr(const char * s, int c); #define __HAVE_ARCH_MEMCPY extern void * memcpy(void *, const void *, __kernel_size_t); +extern void *__memcpy(void *dest, const void *src, __kernel_size_t n); #define __HAVE_ARCH_MEMMOVE extern void * memmove(void *, const void *, __kernel_size_t); +extern void *__memmove(void *dest, const void *src, __kernel_size_t n); #define __HAVE_ARCH_MEMCHR extern void * memchr(const void *, int, __kernel_size_t); #define __HAVE_ARCH_MEMSET extern void * memset(void *, int, __kernel_size_t); +extern void *__memset(void *s, int c, __kernel_size_t n); #define __HAVE_ARCH_MEMSET32 extern void *__memset32(uint32_t *, uint32_t v, __kernel_size_t); @@ -39,4 +42,18 @@ static inline void *memset64(uint64_t *p, uint64_t v, __kernel_size_t n) return __memset64(p, v, n * 8, v >> 32); } + + +#if defined(CONFIG_KASAN) && !defined(__SANITIZE_ADDRESS__) + +/* + * For files that not instrumented (e.g. mm/slub.c) we + * should use not instrumented version of mem* functions. + */ + +#define memcpy(dst, src, len) __memcpy(dst, src, len) +#define memmove(dst, src, len) __memmove(dst, src, len) +#define memset(s, c, n) __memset(s, c, n) +#endif + #endif diff --git a/arch/arm/kernel/head-common.S b/arch/arm/kernel/head-common.S index 997b02302c31..6e3b9179806b 100644 --- a/arch/arm/kernel/head-common.S +++ b/arch/arm/kernel/head-common.S @@ -99,7 +99,7 @@ __mmap_switched: THUMB( ldmia r4!, {r0, r1, r2, r3} ) THUMB( mov sp, r3 ) sub r2, r2, r1 - bl memcpy @ copy .data to RAM + bl __memcpy @ copy .data to RAM #endif ARM( ldmia r4!, {r0, r1, sp} ) @@ -107,7 +107,7 @@ __mmap_switched: THUMB( mov sp, r3 ) sub r2, r1, r0 mov r1, #0 - bl memset @ clear .bss + bl __memset @ clear .bss ldmia r4, {r0, r1, r2, r3} str r9, [r0] @ Save processor ID diff --git a/arch/arm/lib/memcpy.S b/arch/arm/lib/memcpy.S index 4a6997bb4404..a90423194606 100644 --- a/arch/arm/lib/memcpy.S +++ b/arch/arm/lib/memcpy.S @@ -61,6 +61,8 @@ /* Prototype: void *memcpy(void *dest, const void *src, size_t n); */ +.weak memcpy +ENTRY(__memcpy) ENTRY(mmiocpy) ENTRY(memcpy) @@ -68,3 +70,4 @@ ENTRY(memcpy) ENDPROC(memcpy) ENDPROC(mmiocpy) +ENDPROC(__memcpy) diff --git a/arch/arm/lib/memmove.S b/arch/arm/lib/memmove.S index d70304cb2cd0..aabacbe33c32 100644 --- a/arch/arm/lib/memmove.S +++ b/arch/arm/lib/memmove.S @@ -27,12 +27,14 @@ * occurring in the opposite direction. */ +.weak memmove +ENTRY(__memmove) ENTRY(memmove) UNWIND( .fnstart ) subs ip, r0, r1 cmphi r2, ip - bls memcpy + bls __memcpy stmfd sp!, {r0, r4, lr} UNWIND( .fnend ) @@ -225,3 +227,4 @@ ENTRY(memmove) 18: backward_copy_shift push=24 pull=8 ENDPROC(memmove) +ENDPROC(__memmove) diff --git a/arch/arm/lib/memset.S b/arch/arm/lib/memset.S index 5593a45e0a8c..c328d701b7a1 100644 --- a/arch/arm/lib/memset.S +++ b/arch/arm/lib/memset.S @@ -16,6 +16,8 @@ .text .align 5 +.weak memset +ENTRY(__memset) ENTRY(mmioset) ENTRY(memset) UNWIND( .fnstart ) @@ -135,6 +137,7 @@ UNWIND( .fnstart ) UNWIND( .fnend ) ENDPROC(memset) ENDPROC(mmioset) +ENDPROC(__memset) ENTRY(__memset32) UNWIND( .fnstart ) -- 2.17.1