Received: by 2002:ac0:aa62:0:0:0:0:0 with SMTP id w31-v6csp3975997ima; Tue, 23 Oct 2018 14:37:58 -0700 (PDT) X-Google-Smtp-Source: ACcGV619Glns4+pP/6UGg8z2HMiyEmfHUoFOvi8dsVfYmerwNqIfDbBk4Etop+yzNMWJds1IsQq0 X-Received: by 2002:a63:dd0a:: with SMTP id t10-v6mr48417008pgg.379.1540330678548; Tue, 23 Oct 2018 14:37:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1540330678; cv=none; d=google.com; s=arc-20160816; b=om3FZjbeql6iSGOcq+v56S/Ez71MBiIGZXIWsysLjCdVEK375VvjdgVOENJ03/AQ40 AxWE2+8QKkvcYoKOCbBrA4pGaJHxUUBdwBSii4AJANF6mg5CPk8vVz29eleIDXMJKHi8 dVQcymZeU+Hzx/iuHwM7FYHw8hvZAevxldxCQBTiKts35nyyvoYPTxM8Fkzn11QuQ02q EwUliY/KA4+SFC8yttYnZIQeKzWLqvIpxzT/7w0kw7cLLD4g4QSbMzERP4cfeA24PVoy WuQj+Du2BPFnAiqnw2apf8z+IJFjr+gVCiiudO/rriIXcM2qZhI3anAFn5Rh/0d+ihnN GLvA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:reply-to:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=pGpymzbPbfMd0UY2O/PnMm8wYTcULxEjQ2M1QiLWUtQ=; b=wXQ7OhBx8L3oHiaaZ+DgmvGnXNnudO6ASmxjEXSSjUaYVy0IQLrNj7Cn19oa5N9oPV RXywKGTQlaODcZ1E5p7O0S3DTsw+xP+DKFgVgJwkwN5/Im3Xc3Kic6E1pFrCa8iOGv5G 0rqe/mlTD0LTZSCT4/sXkNJCBRpY+jpIAopvQDeWKL0Hne4yHejF4sV3A0O1Y3Pq5V0o RdEtaYJgGYq70jIfkzZfppJVFc8/+h8NHGzyrcTGH+afDvRabFnq2L1JaTrlTbwGx28N hrtKYVOyv6TBnbpVGYFgKv2zuMOtViXAslpmudCokHnzN5NjuVbIaTE9rPXLjT9avtUD bfKA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=etVVDgLo; 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 j142-v6si2568750pfd.204.2018.10.23.14.37.43; Tue, 23 Oct 2018 14:37:58 -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=etVVDgLo; 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 S1729159AbeJXGB1 (ORCPT + 99 others); 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-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: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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); -- 2.17.1