Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp4478194ybz; Tue, 28 Apr 2020 12:00:00 -0700 (PDT) X-Google-Smtp-Source: APiQypIGt+NepaYMLOLGD5Ji+1xsZUlD9vc2V1SWk6yhHxmUZXZJuDRsWYlcTdciBtmDVsxUNZTv X-Received: by 2002:a17:906:468c:: with SMTP id a12mr26828020ejr.149.1588100400395; Tue, 28 Apr 2020 12:00:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588100400; cv=none; d=google.com; s=arc-20160816; b=ilenEubGukwLlcpxcZsy2/NLUrulKSrILmAtGWpsYK+nwBo4gEvHJRxd0S3faQ5kwz iLvKyDYE24yKdhDlJopLhcAPDFg8aIuiyX4XLkBiiqLGeJ02mtKZ3yQcxsEYqowjRsAL lYV3IaoxaH6PpyUdjP0cLfPQX9MqUkMLKgkL3TwKdaOxOr5eYfQFoaabNb7EIlqZc2p8 BUQ/k91Z2V64ORprvt2xX5QshSEjcNryK0bF+vX+pmHMdFXi3uM8dfruBgMztuwShyoQ MuN3qNN9/gKRAtWFh02tfwNakvHL++lGP3eBwpWRBmgbtea7AJhwn4NO6Cye2YwOWq2M X8Ew== 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:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=QZFVVw20a6jT/LlKgVCuWrrLypdqXqdJmd4PGXCMKCo=; b=bCnckGH9l4zR9YWtAujpLl/6aKYNnz2DLG5guoLLJ8t6PvqqoreAAvT0eTeJqvP4+m 7dDzH8IRvmAcSLl1rDeiNWoeo7WRwMUR6lmRkkINXBu3nBVYRdb5C4saKxwpOBSgOfQM bkHHpfoRel204X8d8/ruDoTzr8wrfe/9clAoYSsOgiDSZ+7V3m8c0ErY5lGlooU2OfBm ckNhRFB0Vd4mpkUqVjnrs1DHB/WNEGosBBp1pWEnuZdUZ00dX/10mxcClxsdX042RtgH NW2H3tnmE2N8tWihvswkS1X3GoqDo1QZcxjD2fNHRr0FBAck3FvDp7ud7eg6UeUTFRJW sOJg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=G850iyVJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id ay15si2445030ejb.1.2020.04.28.11.59.36; Tue, 28 Apr 2020 12:00:00 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=G850iyVJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729380AbgD1S3h (ORCPT + 99 others); Tue, 28 Apr 2020 14:29:37 -0400 Received: from mail.kernel.org ([198.145.29.99]:43336 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729369AbgD1S3e (ORCPT ); Tue, 28 Apr 2020 14:29:34 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 95D3C20BED; Tue, 28 Apr 2020 18:29:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1588098573; bh=7BTCKefDT84d2/S2Tue3QMduXeDM7kTMjbqZL7HrA/E=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=G850iyVJeX3hCRJS83fo1BayE5Mi/fFlKRNYkskpqB5Xm691jCuyk7nQQig+84tjV IpOZb5X6niuXAqdCNntLFSDB6CYl62R7T3d2EJt28EZDsUEUWrNp0FPAZa3EgLNjGX mlPZKl0My496yhLmLtl+Eoj7yv1gQTEIY0JeUnV0= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Waiman Long , David Howells , Sasha Levin Subject: [PATCH 5.6 075/167] KEYS: Avoid false positive ENOMEM error on key read Date: Tue, 28 Apr 2020 20:24:11 +0200 Message-Id: <20200428182234.441830864@linuxfoundation.org> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200428182225.451225420@linuxfoundation.org> References: <20200428182225.451225420@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Waiman Long [ Upstream commit 4f0882491a148059a52480e753b7f07fc550e188 ] By allocating a kernel buffer with a user-supplied buffer length, it is possible that a false positive ENOMEM error may be returned because the user-supplied length is just too large even if the system do have enough memory to hold the actual key data. Moreover, if the buffer length is larger than the maximum amount of memory that can be returned by kmalloc() (2^(MAX_ORDER-1) number of pages), a warning message will also be printed. To reduce this possibility, we set a threshold (PAGE_SIZE) over which we do check the actual key length first before allocating a buffer of the right size to hold it. The threshold is arbitrary, it is just used to trigger a buffer length check. It does not limit the actual key length as long as there is enough memory to satisfy the memory request. To further avoid large buffer allocation failure due to page fragmentation, kvmalloc() is used to allocate the buffer so that vmapped pages can be used when there is not a large enough contiguous set of pages available for allocation. In the extremely unlikely scenario that the key keeps on being changed and made longer (still <= buflen) in between 2 __keyctl_read_key() calls, the __keyctl_read_key() calling loop in keyctl_read_key() may have to be iterated a large number of times, but definitely not infinite. Signed-off-by: Waiman Long Signed-off-by: David Howells Signed-off-by: Sasha Levin --- security/keys/internal.h | 12 +++++++++ security/keys/keyctl.c | 58 +++++++++++++++++++++++++++++----------- 2 files changed, 55 insertions(+), 15 deletions(-) diff --git a/security/keys/internal.h b/security/keys/internal.h index ba3e2da14ceff..6d0ca48ae9a50 100644 --- a/security/keys/internal.h +++ b/security/keys/internal.h @@ -16,6 +16,8 @@ #include #include #include +#include +#include struct iovec; @@ -349,4 +351,14 @@ static inline void key_check(const struct key *key) #endif +/* + * Helper function to clear and free a kvmalloc'ed memory object. + */ +static inline void __kvzfree(const void *addr, size_t len) +{ + if (addr) { + memset((void *)addr, 0, len); + kvfree(addr); + } +} #endif /* _INTERNAL_H */ diff --git a/security/keys/keyctl.c b/security/keys/keyctl.c index 106e16f9006b4..5e01192e222a0 100644 --- a/security/keys/keyctl.c +++ b/security/keys/keyctl.c @@ -339,7 +339,7 @@ long keyctl_update_key(key_serial_t id, payload = NULL; if (plen) { ret = -ENOMEM; - payload = kmalloc(plen, GFP_KERNEL); + payload = kvmalloc(plen, GFP_KERNEL); if (!payload) goto error; @@ -360,7 +360,7 @@ long keyctl_update_key(key_serial_t id, key_ref_put(key_ref); error2: - kzfree(payload); + __kvzfree(payload, plen); error: return ret; } @@ -827,7 +827,8 @@ long keyctl_read_key(key_serial_t keyid, char __user *buffer, size_t buflen) struct key *key; key_ref_t key_ref; long ret; - char *key_data; + char *key_data = NULL; + size_t key_data_len; /* find the key first */ key_ref = lookup_user_key(keyid, 0, 0); @@ -878,24 +879,51 @@ can_read_key: * Allocating a temporary buffer to hold the keys before * transferring them to user buffer to avoid potential * deadlock involving page fault and mmap_sem. + * + * key_data_len = (buflen <= PAGE_SIZE) + * ? buflen : actual length of key data + * + * This prevents allocating arbitrary large buffer which can + * be much larger than the actual key length. In the latter case, + * at least 2 passes of this loop is required. */ - key_data = kmalloc(buflen, GFP_KERNEL); + key_data_len = (buflen <= PAGE_SIZE) ? buflen : 0; + for (;;) { + if (key_data_len) { + key_data = kvmalloc(key_data_len, GFP_KERNEL); + if (!key_data) { + ret = -ENOMEM; + goto key_put_out; + } + } - if (!key_data) { - ret = -ENOMEM; - goto key_put_out; - } - ret = __keyctl_read_key(key, key_data, buflen); + ret = __keyctl_read_key(key, key_data, key_data_len); + + /* + * Read methods will just return the required length without + * any copying if the provided length isn't large enough. + */ + if (ret <= 0 || ret > buflen) + break; + + /* + * The key may change (unlikely) in between 2 consecutive + * __keyctl_read_key() calls. In this case, we reallocate + * a larger buffer and redo the key read when + * key_data_len < ret <= buflen. + */ + if (ret > key_data_len) { + if (unlikely(key_data)) + __kvzfree(key_data, key_data_len); + key_data_len = ret; + continue; /* Allocate buffer */ + } - /* - * Read methods will just return the required length without - * any copying if the provided length isn't large enough. - */ - if (ret > 0 && ret <= buflen) { if (copy_to_user(buffer, key_data, ret)) ret = -EFAULT; + break; } - kzfree(key_data); + __kvzfree(key_data, key_data_len); key_put_out: key_put(key); -- 2.20.1