Received: by 10.223.164.202 with SMTP id h10csp213494wrb; Mon, 13 Nov 2017 05:22:58 -0800 (PST) X-Google-Smtp-Source: AGs4zMZLBq5FNp+Avpx0x/5gngAbXvyrajmjZZChGGrgZTxDOoSh0gnEw5VPunSC/eWJvrgnCxu0 X-Received: by 10.84.131.161 with SMTP id d30mr5395200pld.270.1510579378434; Mon, 13 Nov 2017 05:22:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1510579378; cv=none; d=google.com; s=arc-20160816; b=Ym50gAkH3g+NCDvaVehS45V9BQJl+qiKTV7MnDiAkcWlC3+uLEta1eO0qBA36eeWGj lGj2lg/GIJvUiYjJ1e+/YFmDvw7awJcC0BGXGLoFPDiikpNb0OQvbMFYt1Hz3TIXLUFC ZG38EjAVwaE+H+plahE548iZ2KYFgNqFrHfLn9odT9d1giRE+spbaGMkwKeWEkOmTjoH dMbJahZ80J5UgcNSYp31YBh4b6WzrLyo8VFyR95JdhoU527zZX+RqqyBODgZiL7aqNj6 zAAy7L9+6V4BQ/JfHcZAR0jsVuQOWD0zhapJZeZTBU7/tAaek5IWe0kuVg8Xe0Jnq10+ yT5w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=mZgvK6CKVzB9rTd7HZe2taaUKnWK6PuqjsKcJkJ6dvk=; b=k6OAj9EUkEi0mDO+HcykRf9TqNghwywvUeHO9+Xn5vGUYIovGPJYA1yfQE4EPgdNlX OEd2PYnj6bWUO2MhF6d8zekTyGw75GZU4uVVXgnejTXZ4Cw4czuxHwvtqRM2BhImxgEj 6J5SyQFDb+m6ypd4xCEJPJ6RX7BwYEPYez4w3hpWx6n6SwObEHN0VbcJHZkmBKd5yZMJ BAneF1YeqEA8VISyzY7ZOtZ7TGzuKJplnpCrN0Jc3/wq0kjyh7+yBYzVnika7xbDOAZe bLRo9JnCQAMs+JexTLqB6tQJMpzkSk8cteX5yCnocPrp8TCQowjS22+czIhGX2rqrkLJ dtVg== 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id l68si14053337pgl.128.2017.11.13.05.22.45; Mon, 13 Nov 2017 05:22:58 -0800 (PST) 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754671AbdKMNBj (ORCPT + 95 others); Mon, 13 Nov 2017 08:01:39 -0500 Received: from mail.linuxfoundation.org ([140.211.169.12]:50240 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754652AbdKMNBc (ORCPT ); Mon, 13 Nov 2017 08:01:32 -0500 Received: from localhost (LFbn-1-12253-150.w90-92.abo.wanadoo.fr [90.92.67.150]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id 5EA67AB6; Mon, 13 Nov 2017 13:01:31 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Ben Hutchings , Eric Biggers , David Howells , Mimi Zohar , James Morris Subject: [PATCH 4.9 52/87] KEYS: trusted: fix writing past end of buffer in trusted_read() Date: Mon, 13 Nov 2017 13:56:09 +0100 Message-Id: <20171113125620.227450611@linuxfoundation.org> X-Mailer: git-send-email 2.15.0 In-Reply-To: <20171113125615.304035578@linuxfoundation.org> References: <20171113125615.304035578@linuxfoundation.org> User-Agent: quilt/0.65 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.9-stable review patch. If anyone has any objections, please let me know. ------------------ From: Eric Biggers commit a3c812f7cfd80cf51e8f5b7034f7418f6beb56c1 upstream. When calling keyctl_read() on a key of type "trusted", if the user-supplied buffer was too small, the kernel ignored the buffer length and just wrote past the end of the buffer, potentially corrupting userspace memory. Fix it by instead returning the size required, as per the documentation for keyctl_read(). We also don't even fill the buffer at all in this case, as this is slightly easier to implement than doing a short read, and either behavior appears to be permitted. It also makes it match the behavior of the "encrypted" key type. Fixes: d00a1c72f7f4 ("keys: add new trusted key-type") Reported-by: Ben Hutchings Signed-off-by: Eric Biggers Signed-off-by: David Howells Reviewed-by: Mimi Zohar Reviewed-by: James Morris Signed-off-by: James Morris Signed-off-by: Greg Kroah-Hartman --- security/keys/trusted.c | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) --- a/security/keys/trusted.c +++ b/security/keys/trusted.c @@ -1147,20 +1147,21 @@ static long trusted_read(const struct ke p = rcu_dereference_key(key); if (!p) return -EINVAL; - if (!buffer || buflen <= 0) - return 2 * p->blob_len; - ascii_buf = kmalloc(2 * p->blob_len, GFP_KERNEL); - if (!ascii_buf) - return -ENOMEM; - bufp = ascii_buf; - for (i = 0; i < p->blob_len; i++) - bufp = hex_byte_pack(bufp, p->blob[i]); - if ((copy_to_user(buffer, ascii_buf, 2 * p->blob_len)) != 0) { + if (buffer && buflen >= 2 * p->blob_len) { + ascii_buf = kmalloc(2 * p->blob_len, GFP_KERNEL); + if (!ascii_buf) + return -ENOMEM; + + bufp = ascii_buf; + for (i = 0; i < p->blob_len; i++) + bufp = hex_byte_pack(bufp, p->blob[i]); + if (copy_to_user(buffer, ascii_buf, 2 * p->blob_len) != 0) { + kzfree(ascii_buf); + return -EFAULT; + } kzfree(ascii_buf); - return -EFAULT; } - kzfree(ascii_buf); return 2 * p->blob_len; } From 1583955700647120953@xxx Mon Nov 13 12:57:50 +0000 2017 X-GM-THRID: 1583955700647120953 X-Gmail-Labels: Inbox,Category Promotions,HistoricalUnread