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: 10653709 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 0452D14BB for ; Tue, 23 Oct 2018 21:36:23 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E40CD2A3CD for ; Tue, 23 Oct 2018 21:36:22 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D89612A3D2; Tue, 23 Oct 2018 21:36:22 +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=-3.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5E1CB2A3CD for ; Tue, 23 Oct 2018 21:36:22 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 00C8B6B0269; Tue, 23 Oct 2018 17:36:13 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id EFF6D6B026A; Tue, 23 Oct 2018 17:36:12 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D9E4F6B026B; Tue, 23 Oct 2018 17:36:12 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-lf1-f69.google.com (mail-lf1-f69.google.com [209.85.167.69]) by kanga.kvack.org (Postfix) with ESMTP id 653016B0269 for ; Tue, 23 Oct 2018 17:36:12 -0400 (EDT) Received: by mail-lf1-f69.google.com with SMTP id q62-v6so243550lfg.4 for ; Tue, 23 Oct 2018 14:36:12 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:dkim-signature:from:to:cc:subject:date :message-id:in-reply-to:references:reply-to; bh=pGpymzbPbfMd0UY2O/PnMm8wYTcULxEjQ2M1QiLWUtQ=; b=lS3mHrU/hsMismAEwEp6n474/dVRrozYA5oPWv4te6thyxNFaO6vA/G9be6t9hQgPw botz66hr650x+mVx94W33n1Eki4htJTlg9TIhArhoPFK4T9pKWC/FiUaTHjSU+2wXCvR or2sKWvFoo665v6sKqMmAmo+CIxw+ZtEW4U4a+eGNGDWSAS7VJ6CNrUAJB3PnPy7tLSF HRS3x88HCWHjZYHkxziH6vUfeJbaqDSZohQCubIeaU5HIWAsT2o+gS+IUrXNBOcO3c7s bk36PgQo2mvKP7Myy45aDbljzPTr20L8G8tcsem0AJ0Tl8vRSQWjV96ccqhmrb9EyEGN TudQ== X-Gm-Message-State: ABuFfohJPxK3diFmEQ7PGjm/mwfH9NglxGUCZOw3+ouEtPbdA9vzQRHz I8IHJhOYu7DEZOxn8P06xVCUCZc1dUPf1HRBLuZIgTHKM7I2Xif/hasVEQ/tJ+qohJwcNnCRx9c QSzCR8J+WfHkehpBEhZevpd4vYX08XicOL8+quNNVsGggD4kwaoTHJKxQCFcHhDtpN0So6d9HxK p+u/XOUkMZFayoOe5KBkPZ7cBhiHBIYlCF/mvzGJfrZhJp//rBwcCLEXInIMQJ0L3JpGJe3ujKL GFolHD2sNfGWS62OpesUQSFCRQRX6h8VtNoP6464/LvDiA/j9AzY0o8ReEk00NWC/9uS60Xfc56 KfS4Og2B0fI8ti/7epX76fkhDU/U9oye1ZMYPQSbpGhouZ64DvN+CAXoyPGWmqPn+SbwqQSbmrI P X-Received: by 2002:a19:59d3:: with SMTP id n202-v6mr13260190lfb.125.1540330571682; Tue, 23 Oct 2018 14:36:11 -0700 (PDT) X-Received: by 2002:a19:59d3:: with SMTP id n202-v6mr13260166lfb.125.1540330570725; Tue, 23 Oct 2018 14:36:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1540330570; cv=none; d=google.com; s=arc-20160816; b=vg4YTBpYgR+opLtM3P07ZGCdvSFKGSIMtFhExhAFhroSNpwFqE2qGKFxxaIV0NyZnB K/5JrY5bfZ0c4LOlLjvbILZV9zzpxbJWCZFohUE59hg5RNkYPjH3+6u+WVQBQh72IpDo gop8kjScPRYhiJYpvDgg2xFYM2NuZ7evtbQuQ37pz0jLW3Qdin/dmqodpu+6Di5Ly0Tv dGBKrepb525ctSaD4srJaIGNXkGKYBLj0EFXILjRAqaHUCnA5XiAPrn+7cowU01+GSd0 QaNelUjBlcpse0/6y97CdEB1SF9VeuxpMUAF0PrjMwbfyZCWX4KJ+BtVSraLp4WIbzom Uzdw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=reply-to:references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=pGpymzbPbfMd0UY2O/PnMm8wYTcULxEjQ2M1QiLWUtQ=; b=pedGLwVL0FGfXIOy/ESogp6spFNiq8qC07vxyFM3Sw8714B2SXrnrvK+dkRL5Q9/UC nYZfIQWF7ilg83fdmgz25FSmr312NoHjgw8K/wHlEBDBBp48I+2kJfz6Giqf3uWecJR5 zru7uxR01jTliW9lrKBU3+yePgNj9BYkzOn63mmzhwojEJyzuhILILGxvfjn60z3jy5u nIVvNusk0yI9BEDNqvQVB8xbyes/8Gs8UnbS9i8+EyUxiILheXLk0X8NLK4F7ZHETN6e Kpw7Byaua0+A9bQVXGAAdkik7H02M0MeN+vDmPbE+uZ2m4jZAkDAt/G37hZ98Osjmmfl iR9w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=etVVDgLo; spf=pass (google.com: domain of igor.stoppa@gmail.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=igor.stoppa@gmail.com; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from mail-sor-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id k189sor918522lfk.58.2018.10.23.14.36.10 for (Google Transport Security); Tue, 23 Oct 2018 14:36:10 -0700 (PDT) Received-SPF: pass (google.com: domain of igor.stoppa@gmail.com designates 209.85.220.65 as permitted sender) client-ip=209.85.220.65; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=etVVDgLo; spf=pass (google.com: domain of igor.stoppa@gmail.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=igor.stoppa@gmail.com; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com 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-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 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org 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);