Received: by 2002:ac0:98c7:0:0:0:0:0 with SMTP id g7-v6csp3277267imd; Mon, 29 Oct 2018 04:47:24 -0700 (PDT) X-Google-Smtp-Source: AJdET5d9iaXNzcltByGOGgW++282JiqpeZsoN+cZT0BImwJd/YPyONX0jeIYUGtEFUEPrRbmlCoK X-Received: by 2002:a63:e54:: with SMTP id 20-v6mr12678887pgo.369.1540813644752; Mon, 29 Oct 2018 04:47:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1540813644; cv=none; d=google.com; s=arc-20160816; b=kWXj5hrkI0I2if+qu7jA+qRvp4NfrTvomnPMnHXucd87q5jQ6254WTCviGjoWDvnFr eoyvTvLjAHyCjMsqomsFX92MGe46S7uLS6qX4LxAD/fb19JjXuOdr9aJZlcHcwbKtDzd Df19WmqRoc6oQBq0ae659J4wS+Mvdj1QMh6jpOmrBjKPli0sUmhbB2VBbRT0WI3RV865 WW5TxF8mqLcwGSMFheJXGCTUCbmFyOs+5/ngaVZoqUJSFR1RgdsaL6Gaalx57jls3ZQT HLbcAjjq+Gjve2MGQbv+VHMEcdN7Wryg4G1JoKUQnG1fNqFsdUL8KvErZ6bEacQBgNbm 4J9Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-language :content-transfer-encoding:in-reply-to:mime-version:user-agent:date :message-id:organization:from:references:cc:to:subject:reply-to; bh=IIhrak9K9V9dJg1HR9eLVUnWovdXFMWh6zjvCsTZMU8=; b=WMa+VhAAzi/96yQXvH5X2qiG6CCZJYey+frVRdaJrqQgJ+t6sksLLVJeQMjbogJjQ0 JSdh3OOFDdbWBV7cPFvd5rElLaiATzRmRQufVo6EenTDrYpDD3H33FgKOo4RptdunosS lbtZz8m9TPOruI9dxnOEo5MDiqIVejv3iCyED9T6Uv5cyYtkToUOJTREgqY9KzqlJ6T7 h6I+ZGuKkG4dz0YZf52CmOZ2fO95s84hLWqK7JPQku/kO4KaZvI8OMoBPPlQ81PzdCsG /2cNPxrwsQIkWqJpFbhVhFle/5Bc3gV3wZpP2tnQFddozgy3EDDb8Qfia4OuJeIkHxFV 5PMg== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 33-v6si2640095plg.96.2018.10.29.04.47.08; Mon, 29 Oct 2018 04:47:24 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729100AbeJ2Udn (ORCPT + 99 others); Mon, 29 Oct 2018 16:33:43 -0400 Received: from mx1.redhat.com ([209.132.183.28]:60346 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728043AbeJ2Udm (ORCPT ); Mon, 29 Oct 2018 16:33:42 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 62232307EA9E; Mon, 29 Oct 2018 11:45:22 +0000 (UTC) Received: from crecklin.bos.csb (dhcp-17-195.bos.redhat.com [10.18.17.195]) by smtp.corp.redhat.com (Postfix) with ESMTP id E71465EE1D; Mon, 29 Oct 2018 11:45:15 +0000 (UTC) Reply-To: crecklin@redhat.com Subject: Re: [PATCH 09/17] prmem: hardened usercopy To: Igor Stoppa , 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 , linux-mm@kvack.org, linux-kernel@vger.kernel.org References: <20181023213504.28905-1-igor.stoppa@huawei.com> <20181023213504.28905-10-igor.stoppa@huawei.com> From: Chris von Recklinghausen Organization: Red Hat Message-ID: Date: Mon, 29 Oct 2018 07:45:14 -0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.9.1 MIME-Version: 1.0 In-Reply-To: <20181023213504.28905-10-igor.stoppa@huawei.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Content-Language: en-US X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.44]); Mon, 29 Oct 2018 11:45:23 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 10/23/2018 05:34 PM, Igor Stoppa wrote: > 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); > Could you add code somewhere (lkdtm driver if possible) to demonstrate the issue and verify the code change? Thanks, Chris