From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.5 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH, MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,USER_AGENT_SANE_1 autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id EA6B2C2BA19 for ; Mon, 6 Apr 2020 14:36:15 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id AE30D2419A for ; Mon, 6 Apr 2020 14:36:15 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="Ck690N2i" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org AE30D2419A Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 4BFAC8E0032; Mon, 6 Apr 2020 10:36:15 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 449B38E000D; Mon, 6 Apr 2020 10:36:15 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 310BF8E0032; Mon, 6 Apr 2020 10:36:15 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0168.hostedemail.com [216.40.44.168]) by kanga.kvack.org (Postfix) with ESMTP id 10D058E000D for ; Mon, 6 Apr 2020 10:36:15 -0400 (EDT) Received: from smtpin28.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id BB6342C1E for ; Mon, 6 Apr 2020 14:36:14 +0000 (UTC) X-FDA: 76677680268.28.desk94_70b035ef49c34 X-HE-Tag: desk94_70b035ef49c34 X-Filterd-Recvd-Size: 4818 Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [207.211.31.120]) by imf31.hostedemail.com (Postfix) with ESMTP for ; Mon, 6 Apr 2020 14:36:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1586183773; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=0nkfLsT2lQqitstTJWq4FYuR1t+BsPNmrkLpwDWnJdI=; b=Ck690N2itVI10ztlckgwbjSQlxISUuKVi1auNknOM4UUwB6YIRWnCBmgpcDK4n8ZWh5uo2 jpJUUWTvfmiUUQSN52p5SoA45Y7eh085DlYpuoleXNrSCsBA3zQvEcfTN1TI6l9jcfxxQJ xhYAu9UnFhwrWwT1Ku5i4AfHoKZSjYA= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-153-nI0v8Z2RN-uPTgsg4y3jrw-1; Mon, 06 Apr 2020 10:36:12 -0400 X-MC-Unique: nI0v8Z2RN-uPTgsg4y3jrw-1 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 mimecast-mx01.redhat.com (Postfix) with ESMTPS id 13AD0107ACCA; Mon, 6 Apr 2020 14:36:10 +0000 (UTC) Received: from llong.remote.csb (ovpn-115-20.rdu2.redhat.com [10.10.115.20]) by smtp.corp.redhat.com (Postfix) with ESMTP id 75CCD94B5A; Mon, 6 Apr 2020 14:36:08 +0000 (UTC) Subject: Re: [PATCH] mm: Add kvfree_sensitive() for freeing sensitive data objects To: David Rientjes Cc: Andrew Morton , David Howells , Jarkko Sakkinen , James Morris , "Serge E. Hallyn" , linux-mm@kvack.org, keyrings@vger.kernel.org, linux-kernel@vger.kernel.org, Linus Torvalds References: <20200406023700.1367-1-longman@redhat.com> From: Waiman Long Organization: Red Hat Message-ID: <3f6658e5-0082-7759-1d08-eda9cb3eba2f@redhat.com> Date: Mon, 6 Apr 2020 10:36:07 -0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.7.2 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=windows-1252 Content-Language: en-US X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 Content-Transfer-Encoding: quoted-printable 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: On 4/6/20 12:20 AM, David Rientjes wrote: > On Sun, 5 Apr 2020, Waiman Long wrote: > >> For kvmalloc'ed data object that contains sensitive information like >> cryptographic key, we need to make sure that the buffer is always >> cleared before freeing it. Using memset() alone for buffer clearing ma= y >> not provide certainty as the compiler may compile it away. To be sure, >> the special memzero_explicit() has to be used. >> >> This patch introduces a new kvfree_sensitive() for freeing those >> sensitive data objects allocated by kvmalloc(). The relevnat places >> where kvfree_sensitive() can be used are modified to use it. >> >> Fixes: 4f0882491a14 ("KEYS: Avoid false positive ENOMEM error on key r= ead") >> Suggested-by: Linus Torvalds >> Signed-off-by: Waiman Long >> --- >> include/linux/mm.h | 17 +++++++++++++++++ >> security/keys/internal.h | 11 ----------- >> security/keys/keyctl.c | 16 +++++----------- >> 3 files changed, 22 insertions(+), 22 deletions(-) >> >> diff --git a/include/linux/mm.h b/include/linux/mm.h >> index 7dd5c4ccbf85..c26f279f1956 100644 >> --- a/include/linux/mm.h >> +++ b/include/linux/mm.h >> @@ -758,6 +758,23 @@ static inline void *kvcalloc(size_t n, size_t siz= e, gfp_t flags) >> =20 >> extern void kvfree(const void *addr); >> =20 >> +/** >> + * kvfree_sensitive - free a data object containing sensitive informa= tion >> + * @addr - address of the data object to be freed >> + * @len - length of the data object >> + * >> + * Use the special memzero_explicit() function to clear the content o= f a >> + * kvmalloc'ed object containing sensitive data to make sure that the >> + * compiler won't optimize out the data clearing. >> + */ >> +static inline void kvfree_sensitive(const void *addr, size_t len) >> +{ >> + if (addr) { > Shouldn't this be if (unlikely(ZERO_OR_NULL_PTR(addr))? > ZERO_OR_NULL_PTR is defined in slab.h. Using it may cause some header file dependency problem. To guard against the possibility of 0-length allocation request, how about just =A0=A0=A0 if (likely(addr && len)) { Cheers, Longman