From patchwork Tue Oct 23 21:34:56 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Igor Stoppa X-Patchwork-Id: 10653747 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 2081313A4 for ; Tue, 23 Oct 2018 21:37:02 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0BFF22A3D3 for ; Tue, 23 Oct 2018 21:37:02 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id F37672A415; Tue, 23 Oct 2018 21:37:01 +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=-7.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,RCVD_IN_DNSWL_HI autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9DD112A3D3 for ; Tue, 23 Oct 2018 21:37:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729148AbeJXGB1 (ORCPT ); Wed, 24 Oct 2018 02:01:27 -0400 Received: from mail-lf1-f68.google.com ([209.85.167.68]:41140 "EHLO mail-lf1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728995AbeJXGBX (ORCPT ); Wed, 24 Oct 2018 02:01:23 -0400 Received: by mail-lf1-f68.google.com with SMTP id c16so2330762lfj.8; Tue, 23 Oct 2018 14:36:11 -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:reply-to; bh=pGpymzbPbfMd0UY2O/PnMm8wYTcULxEjQ2M1QiLWUtQ=; b=etVVDgLocA4AjN+jxAXLRtXyFmyCKDXzTHMYFdYt+3XfgEEQNZehB6ZAubiCYjyvy0 C5GrkJFQrsxCI5EKgJi2Hh9syjIDh3WhQy6uGJipgVAL10O0CGT25+kfZsvBKU7+PtEs hAvdrW/iABvj+SaTIJ7FSEwZoqcJMNTTM7IyOYe6ITWgreOcX1fRhiAu4Aie0D3L+6Ah 0CL0xm+qqL4/H+nxwdF5UjsZq5aTf3xxTf9y0JGxopS2MHxBOZ9aDWIX2bUY5gqeHeBx G6gvyawpjFtqRLlCBActSQCZFme72y8lx8IlyO4u8ngtDN4fbCBBViLuXUbGABcQ4L4f JH8Q== 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:reply-to; bh=pGpymzbPbfMd0UY2O/PnMm8wYTcULxEjQ2M1QiLWUtQ=; b=Ruzpohh2T6U0cMZ3n0HJ7EoD8TpJWudXh71M/DLgIdLmOwImEz7Z7hzdr+x+dtwIEi xFWKtO/GH+rjvTrKHa9BzmTW/RoEgH1AOWjcYInXH6rtkG7qWSbaSeKdWFbZHShvORIl ly3I0ATgwrkgb7PHl4BhFJLfcHBim+SKpLVS4EEZFsiA1hHu7tLCWTHF7wr9BV6aJY2r kXDFbeTQ9K+9ve0t+BRZL+N21tgaGWZ1SQ6r/S667V/J571UZhoqA/XDu2o8uG6IWf/v 8Pl9f3867foh/8UAOLNhyubX7lCQ6gd4YCANzLvKOaWaRKwN0rQT6kvchDIWdelvNiut vC5g== X-Gm-Message-State: AGRZ1gIZQ0//VRyNnN+MGHDaWTtAvnnoGq8hOtJCG+NemZAvZjp4otex twp3roVtX/XDsmKg768auvE= X-Google-Smtp-Source: AJdET5exRyQPy5ypGmiJjncDtEgR9KmCyJjS90PbVTRQUbcCgZs2pc0eSgkwJSERums9s+tyA2MWxw== X-Received: by 2002:a19:3809:: with SMTP id f9mr170930lfa.148.1540330570334; Tue, 23 Oct 2018 14:36:10 -0700 (PDT) Received: from localhost.localdomain (91-159-62-169.elisa-laajakaista.fi. [91.159.62.169]) by smtp.gmail.com with ESMTPSA id y127-v6sm377950lfc.13.2018.10.23.14.36.09 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 23 Oct 2018 14:36:09 -0700 (PDT) From: Igor Stoppa X-Google-Original-From: Igor Stoppa To: Mimi Zohar , Kees Cook , Matthew Wilcox , Dave Chinner , James Morris , Michal Hocko , kernel-hardening@lists.openwall.com, linux-integrity@vger.kernel.org, linux-security-module@vger.kernel.org Cc: igor.stoppa@huawei.com, Dave Hansen , Jonathan Corbet , Laura Abbott , Chris von Recklinghausen , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 09/17] prmem: hardened usercopy Date: Wed, 24 Oct 2018 00:34:56 +0300 Message-Id: <20181023213504.28905-10-igor.stoppa@huawei.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20181023213504.28905-1-igor.stoppa@huawei.com> References: <20181023213504.28905-1-igor.stoppa@huawei.com> Reply-To: Igor Stoppa Sender: owner-linux-security-module@vger.kernel.org Precedence: bulk List-ID: X-Virus-Scanned: ClamAV using ClamSMTP Prevent leaks of protected memory to userspace. The protection from overwrited from userspace is already available, once the memory is write protected. Signed-off-by: Igor Stoppa CC: Kees Cook CC: Chris von Recklinghausen CC: linux-mm@kvack.org CC: linux-kernel@vger.kernel.org --- include/linux/prmem.h | 24 ++++++++++++++++++++++++ mm/usercopy.c | 5 +++++ 2 files changed, 29 insertions(+) diff --git a/include/linux/prmem.h b/include/linux/prmem.h index cf713fc1c8bb..919d853ddc15 100644 --- a/include/linux/prmem.h +++ b/include/linux/prmem.h @@ -273,6 +273,30 @@ struct pmalloc_pool { uint8_t mode; }; +void __noreturn usercopy_abort(const char *name, const char *detail, + bool to_user, unsigned long offset, + unsigned long len); + +/** + * check_pmalloc_object() - helper for hardened usercopy + * @ptr: the beginning of the memory to check + * @n: the size of the memory to check + * @to_user: copy to userspace or from userspace + * + * If the check is ok, it will fall-through, otherwise it will abort. + * The function is inlined, to minimize the performance impact of the + * extra check that can end up on a hot path. + * Non-exhaustive micro benchmarking with QEMU x86_64 shows a reduction of + * the time spent in this fragment by 60%, when inlined. + */ +static inline +void check_pmalloc_object(const void *ptr, unsigned long n, bool to_user) +{ + if (unlikely(__is_wr_after_init(ptr, n) || __is_wr_pool(ptr, n))) + usercopy_abort("pmalloc", "accessing pmalloc obj", to_user, + (const unsigned long)ptr, n); +} + /* * The write rare functionality is fully implemented as __always_inline, * to prevent having an internal function call that is capable of modifying diff --git a/mm/usercopy.c b/mm/usercopy.c index 852eb4e53f06..a080dd37b684 100644 --- a/mm/usercopy.c +++ b/mm/usercopy.c @@ -22,8 +22,10 @@ #include #include #include +#include #include + /* * Checks if a given pointer and length is contained by the current * stack frame (if possible). @@ -284,6 +286,9 @@ void __check_object_size(const void *ptr, unsigned long n, bool to_user) /* Check for object in kernel to avoid text exposure. */ check_kernel_text_object((const unsigned long)ptr, n, to_user); + + /* Check if object is from a pmalloc chunk. */ + check_pmalloc_object(ptr, n, to_user); } EXPORT_SYMBOL(__check_object_size);