Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp3366820ybb; Mon, 6 Apr 2020 07:37:08 -0700 (PDT) X-Google-Smtp-Source: APiQypLNfW/Gq/TpHRIz4pMg8nmmygHJXy0fkLdQiym0BUExaiaf4HONNcr0EgwzJdHIHb3qPEm/ X-Received: by 2002:aca:3302:: with SMTP id z2mr13287834oiz.3.1586183828018; Mon, 06 Apr 2020 07:37:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1586183828; cv=none; d=google.com; s=arc-20160816; b=RYx8/oHzt/mG5dzYVhOV4UshURccagn1WHq81rROAF9opjUbtAUIr0TiX0yYzZbRIG 9deRp6TiJc414hk33Kti0wSoo1znUipWrRlPocQOFIn2bMGNXdELR6A67/gMzDNVy/Uy VamDUDJhPQ7myE7Pq3o0VN93uVOh4kJPuNwINqFcfpPE+Cw+lABmkMW2vT/vNwYIj11w VkrzPc7XCmJb3Mac9G1TQtQmNFFtz09FAB0S4BIenaBEpaHtqls7pQI1CHz58tg6Zfv/ Eq67j/9hYV26CMELneLPvmpBbXE4CET3Q8T2BOzT2cNXkEmAUsvvF7fQo7vb15QYI+Vm VCBw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:organization:from:references:cc:to:subject :dkim-signature; bh=0nkfLsT2lQqitstTJWq4FYuR1t+BsPNmrkLpwDWnJdI=; b=v0pge85odoGvSBwBsIeXyAWCjWJK72YXfwAASvFpyTERYQl2T4hcxuJ6lkfoOf6T+T cBPTuFaunwm3nj4H6X2LrJO8RwbBCv2QvxQiaGwDmDJvp5rPvKBScMV+WccFdwerc5a2 sZeQfjL3f1TkQ8/w09oAIn0pa3jhJsCf3QGbgMrOqofeVqTd/TygTD7OCLwastnIUJzT tKxlDUZP27t28VdkXW0+HwZwis+2hqYpqKZlNDoZ2RhGOG8Xcx3wybPR7s27eDZdnLeh UnQp7Cg0XLQvra2yhsA5rlxByo9HjTXbaZQXqIjSNPUZfR2Hwoiv6ZT84ZFI1ZYQ0pP2 OhQg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Ck690N2i; 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=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 g124si7710842oia.0.2020.04.06.07.36.55; Mon, 06 Apr 2020 07:37:08 -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=@redhat.com header.s=mimecast20190719 header.b=Ck690N2i; 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=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728699AbgDFOgO (ORCPT + 99 others); Mon, 6 Apr 2020 10:36:14 -0400 Received: from us-smtp-1.mimecast.com ([207.211.31.81]:55274 "EHLO us-smtp-delivery-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1728630AbgDFOgO (ORCPT ); Mon, 6 Apr 2020 10:36:14 -0400 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 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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