Received: by 2002:a05:6a10:2785:0:0:0:0 with SMTP id ia5csp17531pxb; Tue, 12 Jan 2021 18:37:15 -0800 (PST) X-Google-Smtp-Source: ABdhPJxzh7laPZty2KD2UdV3Cuz5QoQW53TR73yccA0ykXrD0CcS7QSsJ6Wd8vMbHU1OK2tszwuc X-Received: by 2002:a50:d491:: with SMTP id s17mr2760edi.169.1610505435109; Tue, 12 Jan 2021 18:37:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1610505435; cv=none; d=google.com; s=arc-20160816; b=q7RsBebYXNxdGIjY+f8BrMB9BS0Qlw10pkobe67vinT1W2e1RwLuqsKiK46S1gVxEm 3xOtUQ3exTEAgdn+N/w1TCx1otun1JpJLxbvVNCPLJtMOSW2WWDrCNHuBCG+3QSPU2CX 3s8HoTe6ccEExVuYzHIchwOTt9KFRqqDla071+n7kfEA3Q4lAsvnoTXthv6jeXOuU+8B XJKKMJGOLYgcvjDVF1egFCaanSDBlnnTk32QeK4MSKpEAQ8I3T5Mv0hONbEwQlYUL6C3 jmkOTREA8rKW4zYK53OBzGPQGB7YN7Yvp5PuIHjBLHCG/MeMcvgzX4K1lpqLEChRWpeh jSjw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=XpRF98H6QxDvYHR2YECMLpFGbIbXZ63ebl8zScm/1pg=; b=Go7cL90ma9byla41oBmkvpFrv/oa7L755mFVJ1cNlbj6GufPoVm9AnHAoMjhaoJoMW fj5Y4dr07QVRzRfkxNaHRQ7lRCld3RxkCuXgkn06S6GPelwz4ROabo1xY8H7TcZDSzeC MUKCSWYb+XkJcHA/ODai/rHxhcweIut2dFPhqvx+inRgz6YiCfZ9PwTFFsuGdPMN8Fgg Qd8wKlYOaBA3vb6Txns75+0a5+IORQWQDcqcqRw9iRQQhwVKkMtox8KowQnRf4ymOBUv QtN9XXgWXXybaPI0p226RZ2BGjTCi3cu+H7Lk2qBIOZtmHtuyf41otbBclSZoAlkxTd/ OJcw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=s9ReiTEz; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id qo17si268067ejb.296.2021.01.12.18.36.51; Tue, 12 Jan 2021 18:37:15 -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=@google.com header.s=20161025 header.b=s9ReiTEz; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2393274AbhALVfx (ORCPT + 99 others); Tue, 12 Jan 2021 16:35:53 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40344 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2437133AbhALVQl (ORCPT ); Tue, 12 Jan 2021 16:16:41 -0500 Received: from mail-pf1-x433.google.com (mail-pf1-x433.google.com [IPv6:2607:f8b0:4864:20::433]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D4119C061786 for ; Tue, 12 Jan 2021 13:15:54 -0800 (PST) Received: by mail-pf1-x433.google.com with SMTP id d2so2166563pfq.5 for ; Tue, 12 Jan 2021 13:15:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=XpRF98H6QxDvYHR2YECMLpFGbIbXZ63ebl8zScm/1pg=; b=s9ReiTEzcZGXMZbagV8knRZSxCcfNVCjwtbeMAoWw0oEcRx0WpKzSWgGwGluIx57DX Eg2kT+voymHTzoqSMUkg0WjqjdoWzBCeO+5LjqJon1nIoGnaJD8sLXQymj8U6tXI3yRT BTqoIe3shIxBg5NTcW6AYlqdJaRy61qvC1jA1WgkniemFkU6kHqmu43WscGQFB+91xws v6xe9x1eRl/ypiiJfncTKke2kN+qQPFEFANaPElaaNgmbUO4wK6Jn2nNTzmyrvM2zFmM 0jQkBZ1VXkc0FPR9buNshD8x3j95Cxoq0aB1qCNIMyj/aX6ohjMC0WtykCM91Rolulr9 Lu5w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=XpRF98H6QxDvYHR2YECMLpFGbIbXZ63ebl8zScm/1pg=; b=JmiYj6plQEV4P3pJr067A4aM0LCxWUXzx6Fmvh2x4iZ5ddHW5L3Uy7KqXN6YTNqjAV +ompKdGGdV3NYCgkqOBPCQV4e5D6IUwG2rIqe9CXZlfjqvmuHbtFBHaAO5hO2Aa/qvKJ bJdXzBsui6z+qAKPl7GKASX1eLwT/xZ/pFaaQjY2MW+Dfcwurvh6eMEKYYz2dLRond9W 4iHP+7+GAmbz3u9OmzNO5eYBt7MVDd7siCe8meM1RbvGuCBd2EiTCHjc7C8YgzOQ5+XF C2HAl6bsjWrxBEq3KEiv9pubNdxVYXlHF2lfQg7VMZ6LB64QVHPtRK+wJyKCNE7HB+g0 6mIw== X-Gm-Message-State: AOAM531QqZj/5hqx8Lfyss6r7j2VIIBt7AEBdk6+oWuEL5Gfzp/znvx/ Aww0OCaY0Qb/v1LfXCribsSHdcfuc9NdIoKFaR2lsQ== X-Received: by 2002:a63:1f47:: with SMTP id q7mr1015387pgm.10.1610486154136; Tue, 12 Jan 2021 13:15:54 -0800 (PST) MIME-Version: 1.0 References: <20210112205542.1375847-1-natechancellor@gmail.com> In-Reply-To: <20210112205542.1375847-1-natechancellor@gmail.com> From: Nick Desaulniers Date: Tue, 12 Jan 2021 13:15:42 -0800 Message-ID: Subject: Re: [PATCH] ubsan: Implement __ubsan_handle_alignment_assumption To: Nathan Chancellor Cc: Kees Cook , Andrew Morton , LKML , clang-built-linux Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Jan 12, 2021 at 12:55 PM Nathan Chancellor wrote: > > When building ARCH=mips 32r2el_defconfig with CONFIG_UBSAN_ALIGNMENT: > > ld.lld: error: undefined symbol: __ubsan_handle_alignment_assumption > >>> referenced by slab.h:557 (include/linux/slab.h:557) > >>> main.o:(do_initcalls) in archive init/built-in.a > >>> referenced by slab.h:448 (include/linux/slab.h:448) > >>> do_mounts_rd.o:(rd_load_image) in archive init/built-in.a > >>> referenced by slab.h:448 (include/linux/slab.h:448) > >>> do_mounts_rd.o:(identify_ramdisk_image) in archive init/built-in.a > >>> referenced 1579 more times > > Implement this for the kernel based on LLVM's > handleAlignmentAssumptionImpl because the kernel is not linked against > the compiler runtime. > > Link: https://github.com/ClangBuiltLinux/linux/issues/1245 > Link: https://github.com/llvm/llvm-project/blob/llvmorg-11.0.1/compiler-rt/lib/ubsan/ubsan_handlers.cpp#L151-L190 > Signed-off-by: Nathan Chancellor > --- > lib/ubsan.c | 28 ++++++++++++++++++++++++++++ > lib/ubsan.h | 6 ++++++ > 2 files changed, 34 insertions(+) > > diff --git a/lib/ubsan.c b/lib/ubsan.c > index 3e3352f3d0da..a1e6cc9993f8 100644 > --- a/lib/ubsan.c > +++ b/lib/ubsan.c > @@ -427,3 +427,31 @@ void __ubsan_handle_load_invalid_value(void *_data, void *val) > ubsan_epilogue(); > } > EXPORT_SYMBOL(__ubsan_handle_load_invalid_value); > + > +void __ubsan_handle_alignment_assumption(void *_data, unsigned long ptr, > + unsigned long align, > + unsigned long offset) > +{ > + struct alignment_assumption_data *data = _data; > + unsigned long real_ptr; > + > + if (suppress_report(&data->location)) > + return; > + > + ubsan_prologue(&data->location, "alignment-assumption"); > + > + if (offset) > + pr_err("assumption of %lu byte alignment (with offset of %lu byte) for pointer of type %s failed", > + align, offset, data->type->type_name); > + else > + pr_err("assumption of %lu byte alignment for pointer of type %s failed", > + align, data->type->type_name); > + > + real_ptr = ptr - offset; > + pr_err("%saddress is %lu aligned, misalignment offset is %lu bytes", > + offset ? "offset " : "", BIT(ffs(real_ptr)), if real_ptr is an unsigned long, do we want to use `__ffs(real_ptr) + 1` here rather than ffs which takes an int? It seems the kernel is missing a definition of ffsl. :( Otherwise code LGTM. > + real_ptr & (align - 1)); > + > + ubsan_epilogue(); > +} > +EXPORT_SYMBOL(__ubsan_handle_alignment_assumption); > diff --git a/lib/ubsan.h b/lib/ubsan.h > index 7b56c09473a9..9a0b71c5ff9f 100644 > --- a/lib/ubsan.h > +++ b/lib/ubsan.h > @@ -78,6 +78,12 @@ struct invalid_value_data { > struct type_descriptor *type; > }; > > +struct alignment_assumption_data { > + struct source_location location; > + struct source_location assumption_location; > + struct type_descriptor *type; > +}; > + > #if defined(CONFIG_ARCH_SUPPORTS_INT128) > typedef __int128 s_max; > typedef unsigned __int128 u_max; > > base-commit: 7c53f6b671f4aba70ff15e1b05148b10d58c2837 > -- > 2.30.0 > -- Thanks, ~Nick Desaulniers