From patchwork Mon Apr 9 11:59:15 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: kpark3469@gmail.com X-Patchwork-Id: 10331193 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 2B4FB6022E for ; Mon, 9 Apr 2018 12:04:25 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1EEC128B09 for ; Mon, 9 Apr 2018 12:04:25 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 135C028B40; Mon, 9 Apr 2018 12:04:25 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.3 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from mother.openwall.net (mother.openwall.net [195.42.179.200]) by mail.wl.linuxfoundation.org (Postfix) with SMTP id 0B64028B09 for ; Mon, 9 Apr 2018 12:04:23 +0000 (UTC) Received: (qmail 15900 invoked by uid 550); 9 Apr 2018 12:04:05 -0000 Mailing-List: contact kernel-hardening-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Delivered-To: mailing list kernel-hardening@lists.openwall.com Received: (qmail 15740 invoked from network); 9 Apr 2018 12:04:03 -0000 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=zqw89oX0VDYEFbNnHUf8KMoRxXhoyIJsc7kMYSn54rA=; b=YxooVLzSWU7h7Zh4iFKXBe8ZkG7GbQ6WwEatl7f58wF0ssespeCi/oigO2YgRf7HgS SSHKyDo/qXTWHAspjpzmQZN7gJDRutiKu1jdVqZaEh9KvmpLii5VK84VM/QEQIFUWpzN E4x+/pVOU8ZMUCgjXeU/GNkkmeqimoHXUfYk0EkdEcYe4NNebzTmX4bqdciusFnV0Ek+ mr7lw7CB5tPonL6JinAbH4LX1gMs7VVJ4CMEUKHKeR4EsaZYlBtINgP1EMD2nARTZWw8 McRTq2VvKxBjZp+y2lD2xUrk04jBY7dMwglfkmKhwUREIf4JMLiP/qAo3pbWmgiwr9J/ q5YQ== 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=zqw89oX0VDYEFbNnHUf8KMoRxXhoyIJsc7kMYSn54rA=; b=Hd4tqsuV6FVAgit10743BKNkdc8HH6WzD/8PVjUYgDpGift6CLlloh93EFPwVszZDA 42QoQC39PUUTudXTf9wN6DApmW8HiraXhKQJOX5pJNrTpoF9RLKsxDIpyx4hbIf5dKlY RQ0zpBND53jgMrUMbk9PBZM8EyQhJr54LVXSrVB08fiU/SHkLlyCHd/pmEdHKgJVj0BB VDFemqxws+0dpJjdlcpXC4G6R0z9hWQMwU1HgyrOfd9JRLuXh8TbtsVncjqYeV9iVbP5 1YcGoGnU+je/amiMA7UF3Lmll2l5Xa4Rel2B9hXBGIxils3YBRnI+UWh6wqSilZpwm/N s9mQ== X-Gm-Message-State: ALQs6tBKMYhLeY+lcc1V+tRWyuZDZ3Ni4D1c0UZWSoFpbFszAlv4lRTd vktVMb3ptPjANQ3anUP9GjbVxg== X-Google-Smtp-Source: AIpwx49eYLdenUWA6ev8OwkTdbYX8+QnVDVdCwxGtQvi9bWzn/zmbLmQ8GNp1Ua0Iof9LZ1Sa2UQaA== X-Received: by 10.28.192.7 with SMTP id q7mr19651016wmf.44.1523275431330; Mon, 09 Apr 2018 05:03:51 -0700 (PDT) From: kpark3469@gmail.com To: kernel-hardening@lists.openwall.com Cc: catalin.marinas@arm.com, keescook@chromium.org, will.deacon@arm.com, mark.rutland@arm.com, james.morse@arm.com, panand@redhat.com, keun-o.park@darkmatter.ae, psodagud@codeaurora.org, jpoimboe@redhat.com, mingo@kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v3 2/3] arm64: usercopy: implement arch_within_stack_frames Date: Mon, 9 Apr 2018 15:59:15 +0400 Message-Id: <1523275156-29087-3-git-send-email-kpark3469@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1523275156-29087-2-git-send-email-kpark3469@gmail.com> References: <1523275156-29087-1-git-send-email-kpark3469@gmail.com> <1523275156-29087-2-git-send-email-kpark3469@gmail.com> X-Virus-Scanned: ClamAV using ClamSMTP From: James Morse This implements arch_within_stack_frames() for arm64 that should validate if a given object is contained by a kernel stack frame. Signed-off-by: James Morse Reviewed-by: Sahara Reviewed-by: Kees Cook --- arch/arm64/Kconfig | 1 + arch/arm64/kernel/stacktrace.c | 76 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig index 177be0d..72d0747 100644 --- a/arch/arm64/Kconfig +++ b/arch/arm64/Kconfig @@ -128,6 +128,7 @@ config ARM64 select HAVE_SYSCALL_TRACEPOINTS select HAVE_KPROBES select HAVE_KRETPROBES + select HAVE_ARCH_WITHIN_STACK_FRAMES select IOMMU_DMA if IOMMU_SUPPORT select IRQ_DOMAIN select IRQ_FORCED_THREADING diff --git a/arch/arm64/kernel/stacktrace.c b/arch/arm64/kernel/stacktrace.c index d5718a0..5eb3784 100644 --- a/arch/arm64/kernel/stacktrace.c +++ b/arch/arm64/kernel/stacktrace.c @@ -27,6 +27,11 @@ #include #include +#define FAKE_FRAME(frame, my_func) do { \ + frame.fp = (unsigned long)__builtin_frame_address(0); \ + frame.pc = (unsigned long)my_func; \ +} while (0) + /* * AArch64 PCS assigns the frame pointer to x29. * @@ -100,6 +105,77 @@ void notrace walk_stackframe(struct task_struct *tsk, struct stackframe *frame, } } +struct check_frame_arg { + unsigned long obj_start; + unsigned long obj_end; + unsigned long frame_start; + int discard_frames; + int err; +}; + +static int check_frame(struct stackframe *frame, void *d) +{ + struct check_frame_arg *arg = d; + unsigned long frame_end = frame->fp; + + /* object overlaps multiple frames */ + if (arg->obj_start < frame->fp && frame->fp < arg->obj_end) { + arg->err = BAD_STACK; + return 1; + } + + /* + * Discard frames and check object is in a frame written early + * enough. + */ + if (arg->discard_frames) + arg->discard_frames--; + else if ((arg->frame_start <= arg->obj_start && + arg->obj_start < frame_end) && + (arg->frame_start < arg->obj_end && arg->obj_end <= frame_end)) + return 1; + + /* object exists in a previous frame */ + if (arg->obj_end < arg->frame_start) { + arg->err = BAD_STACK; + return 1; + } + + arg->frame_start = frame_end + 0x10; + + return 0; +} + +/* Check obj doesn't overlap a stack frame record */ +int arch_within_stack_frames(const void *stack, + const void *stack_end, + const void *obj, unsigned long obj_len) +{ + struct stackframe frame; + struct check_frame_arg arg; + + if (!IS_ENABLED(CONFIG_FRAME_POINTER)) + return NOT_STACK; + + arg.err = GOOD_FRAME; + arg.obj_start = (unsigned long)obj; + arg.obj_end = arg.obj_start + obj_len; + + FAKE_FRAME(frame, arch_within_stack_frames); + arg.frame_start = frame.fp; + + /* + * Skip 4 non-inlined frames: , + * arch_within_stack_frames(), check_stack_object() and + * __check_object_size(). + */ + arg.discard_frames = 4; + + walk_stackframe(current, &frame, check_frame, &arg); + + return arg.err; +} + #ifdef CONFIG_STACKTRACE struct stack_trace_data { struct stack_trace *trace;