From patchwork Tue Aug 17 21:58:27 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Collingbourne X-Patchwork-Id: 12442171 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-17.4 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_ADSP_CUSTOM_MED,DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id AF227C4338F for ; Tue, 17 Aug 2021 22:00:59 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 769816054E for ; Tue, 17 Aug 2021 22:00:59 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 769816054E Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:Mime-Version: Message-Id:Date:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To: References:List-Owner; bh=f9NJ53xYxcydSjVXMZomXL7r5U7G1/0/PaGzoiEXEh4=; b=RpV mwuThw6RrtGzu0Rt2/MBO0Dw9XaekxiPcDl2uoQdiVYRQiCDfHXc79M8XiLD6aRy08bwH+o/6QcWn gAJnko1rwTW7z7s0axPAz+q91fX2wjcB78K5JGfjpVG6gaYOWciktKwCK4vlpKhqFfxrMUij1CWZS MipwNeAdvCvXZVZEWkQGWZBApB+JwgS5KBI68H+3nm6QnwdjDRr+ySrcia1zp8kCdngc1IlvFAofE 8ATwzQTkV4PElmR1bafDHYtnTItBWR/qcOt7Nyny9LDK+Fl39c7hXwE4v+O0lgvMdwLV6JN1ZItrE IlascHA+F9wjE/uli7lpc4Qz8U9Wdbg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mG76i-003azd-Lr; Tue, 17 Aug 2021 21:58:52 +0000 Received: from mail-qk1-x749.google.com ([2607:f8b0:4864:20::749]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1mG76e-003ayw-0S for linux-arm-kernel@lists.infradead.org; Tue, 17 Aug 2021 21:58:50 +0000 Received: by mail-qk1-x749.google.com with SMTP id b9-20020a05620a1269b02903b8bd5c7d95so268499qkl.12 for ; Tue, 17 Aug 2021 14:58:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:message-id:mime-version:subject:from:to:cc; bh=q+WyYsP/aJu07FQ019eBkJ5hQs/+wIs4bY11xt8kySI=; b=Z6/l0CK4pfR6z/aUH+Fr8kXEDg1oNEgAyoHvPoFEgylgfanvBVfxUgPxqfOx5ob9Mo aA0eRnIO6dXFLnwUDQJJ/ukfqPgZIWsIfnhjTmmyR3k4mM1MrSNKAFoVmjx6STqQAZmV 8sxmrHvyLu75gjKvVD5jN1XAZBoV//f6LroYW6i6t5o99amXsYiwkU92grX2FCpnv/Mx F8UVk84XyZiGNPWfXBZ5JDwE/z5XBY6MRGWEEvhu4Utfyq3CU3xSO9j39uvwChgAwfFe aHmNsV1TgEySRZrlV+ePndyKdc9/DsasNSocxDcZbMbH7/YJ6i4hmFt7vcUKQfa5ENA1 aUxA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=q+WyYsP/aJu07FQ019eBkJ5hQs/+wIs4bY11xt8kySI=; b=HJ4Q/y5TMOuXOcveGNEsmA5r+dj5chdZg5+bbj88YFB3Vfzl9pfQpXbbiktYOg7oQ5 iTKldkRby0mDwZK2iqKtaeuobqWaA86i+Sg6Hc/+6vY5jhgaSmdKKxCfimdO37PgyJqT vRqgNA+gOALNyiRrN5XQF319keqHmzkmkyAMwInGFQbQwTQcyoPIeZadcyQvBtEu3AG8 sHYDRZq3lkX8GRtrkf358iLH1UAfikWzVv0IF0ku33/CpLH9JTP3/Djy78xbhqxs92Sc uyxP4Gimgud1OQIvqhS++QZSCWt0ie1hTYW60mpcwf2a2s2ItaXVos+oG1PtHfQtxUC4 sqgw== X-Gm-Message-State: AOAM530kBTD7rHk1v9euNBKeI5QNkxmWKoxnA14PlMzzzPVvWxrToZjp wE8dy8vAkePSbwGRR5fxEOKbi50vGqF0TYq0L1zAL11rs5xBYjEV8K7lld1brkHSQ19PlLejOht LezHFB12W8uMLvGSx880rDmlbnfgdwodqXJ3IuueJf6hsEQzn3a6MOXL2mE4x98jO6MzHRkpv X-Google-Smtp-Source: ABdhPJze31EUk5BGXPH4JJtpV9PBn0wlf9OwuMQSLFa/p77LmjQVwBpY6tP+PtWqphOL3zPgKaRPtM8= X-Received: from pcc-desktop.svl.corp.google.com ([2620:15c:2ce:200:bd34:3e5:a87a:6fbc]) (user=pcc job=sendgmr) by 2002:a05:6214:324:: with SMTP id j4mr5624500qvu.22.1629237522168; Tue, 17 Aug 2021 14:58:42 -0700 (PDT) Date: Tue, 17 Aug 2021 14:58:27 -0700 Message-Id: <20210817215827.2763811-1-pcc@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.33.0.rc1.237.g0d66db33f3-goog Subject: [PATCH] arm64: symbolize user-space stack on SIGSEGV From: Peter Collingbourne To: linux-arm-kernel@lists.infradead.org Cc: Peter Collingbourne X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210817_145848_790102_D7A02B49 X-CRM114-Status: GOOD ( 16.55 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On arm64, dump the userspace stack to the console when a SIGSEGV occurs. Print filename+offset to allow symbolization. This is a patch that I frequently need to apply locally in order to debug segfaults that occur in environments where attaching a debugger is infeasible. Although I don't expect this patch to be applied to the mainline kernel, I am posting it in case anyone else would find it useful (and because I almost lost track of it recently, and I don't want to have to write it again). Signed-off-by: Peter Collingbourne --- arch/arm64/mm/fault.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/arch/arm64/mm/fault.c b/arch/arm64/mm/fault.c index 349c488765ca..8db82788e80b 100644 --- a/arch/arm64/mm/fault.c +++ b/arch/arm64/mm/fault.c @@ -526,6 +526,38 @@ static bool is_write_abort(unsigned int esr) return (esr & ESR_ELx_WNR) && !(esr & ESR_ELx_CM); } +static void symbolize_addr(const char *prefix, unsigned long addr) +{ + struct vm_area_struct *vma = find_vma(current->mm, addr); + if (vma && vma->vm_file) { + char buf[1024]; + char *c = d_path(&vma->vm_file->f_path, buf, 1024); + printk(KERN_ERR "%s = 0x%lx (%s + 0x%lx)\n", prefix, addr, c, + addr - vma->vm_start + vma->vm_pgoff * PAGE_SIZE); + } else { + printk(KERN_ERR "%s = 0x%lx", prefix, addr); + } +} + +static void symbolize_stack(void) +{ + int i; + unsigned long frame = current_pt_regs()->regs[29]; + + symbolize_addr("pc", current_pt_regs()->pc); + symbolize_addr("lr", current_pt_regs()->regs[30]); + + for (i = 0; i != 64; ++i) { + unsigned long ret_addr; + unsafe_get_user(ret_addr, (unsigned long __user *)(frame + 8), + end); + symbolize_addr("ret_addr", ptrauth_clear_pac(ret_addr)); + + unsafe_get_user(frame, (unsigned long __user *)frame, end); + } +end:; +} + static int __kprobes do_page_fault(unsigned long far, unsigned int esr, struct pt_regs *regs) { @@ -671,6 +703,7 @@ static int __kprobes do_page_fault(unsigned long far, unsigned int esr, * Something tried to access memory that isn't in our memory * map. */ + symbolize_stack(); arm64_force_sig_fault(SIGSEGV, fault == VM_FAULT_BADACCESS ? SEGV_ACCERR : SEGV_MAPERR, far, inf->name);