Received: by 10.223.185.116 with SMTP id b49csp2388143wrg; Thu, 15 Feb 2018 10:51:14 -0800 (PST) X-Google-Smtp-Source: AH8x227Jx21d1OrLp/lKJiOsCicJEPQc3BswTuf86fj3d864FahDdwutKY4oygcewHGBRu4unm99 X-Received: by 2002:a17:902:9683:: with SMTP id n3-v6mr3323662plp.177.1518720674872; Thu, 15 Feb 2018 10:51:14 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1518720674; cv=none; d=google.com; s=arc-20160816; b=oDkm/MEMYiWCmYuWK8Ft5Qz9sGiRhVzjRhbEhORygfUi0qkJ4L1P5RNzza2CjVtF95 vG9j3+62QChOyFf1jx4xyb6jkNNy+ZeS+DoK8ATe192gt4FysIJgIIOOC3xwnYMSQ/LF MIdW5dWGoUaRWIjse9XuM74N8hw4YQbu1VzVBfwYACxgw2DLoJUL89Qc5SbiQYJoiJ9C ggCRc5DEOjHSCjOZU+C4NkDa9WCEGBzE8TuakXQJFjwOgEHd50FIjLMi8SsPVIKL7oO/ VUA6vllYmTGLdd4iJs3TVi8QLmjsQfjgpuhHUTOu5DI4n2a4rhnDFpv0qyFg6o7b+lZ0 tORw== 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=vgb8dxUM2fkvgh59sBzUT5xuI/YQ8kOI2SDMrIdbg7c=; b=zEe7uUTTMbFo+5DTnYEzqVgtdVVPKLacem/qwR2MjcQ+m46YW+RemdiwWChkfepZeg 2/6Ur1gmr9M6axVfjXSsxbr4Jshy+K12P+tQO+WaMKbJGQlQGqueSo6Se2iejlQ0UpRn D1Frv3YU6GmO5EuuyzIs4hvLfjkV6oUqohJH5fjccOgSayZEMJHW4xxJ1vKZNw4OnEfg u94JbnVdywUC6hh8DROHXCf0hVRT3T/xkPsXTcKneNaX1GT7GmtY/LVgw22fy1cXYIsO Ms0Lq37fwMbN37ohr4clkRE4VO9VH0PgsjUlTOKne9IEmIz9IowK8wpk+mTakWmanfpa /4KA== 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 j11si121569pgs.614.2018.02.15.10.51.00; Thu, 15 Feb 2018 10:51:14 -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 S1162246AbeBOPV7 (ORCPT + 99 others); Thu, 15 Feb 2018 10:21:59 -0500 Received: from mail.linuxfoundation.org ([140.211.169.12]:49298 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1162138AbeBOPV4 (ORCPT ); Thu, 15 Feb 2018 10:21:56 -0500 Received: from localhost (LFbn-1-12258-90.w90-92.abo.wanadoo.fr [90.92.71.90]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id 62B57DD9; Thu, 15 Feb 2018 15:21:55 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Mimi Zohar , Eric Biggers , David Howells , James Morris , Jin Qian Subject: [PATCH 4.4 034/108] KEYS: encrypted: fix buffer overread in valid_master_desc() Date: Thu, 15 Feb 2018 16:16:31 +0100 Message-Id: <20180215151227.176503087@linuxfoundation.org> X-Mailer: git-send-email 2.16.1 In-Reply-To: <20180215151222.267507937@linuxfoundation.org> References: <20180215151222.267507937@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.4-stable review patch. If anyone has any objections, please let me know. ------------------ From: Eric Biggers commit 794b4bc292f5d31739d89c0202c54e7dc9bc3add upstream. With the 'encrypted' key type it was possible for userspace to provide a data blob ending with a master key description shorter than expected, e.g. 'keyctl add encrypted desc "new x" @s'. When validating such a master key description, validate_master_desc() could read beyond the end of the buffer. Fix this by using strncmp() instead of memcmp(). [Also clean up the code to deduplicate some logic.] Cc: Mimi Zohar Signed-off-by: Eric Biggers Signed-off-by: David Howells Signed-off-by: James Morris Signed-off-by: Jin Qian Signed-off-by: Greg Kroah-Hartman --- security/keys/encrypted-keys/encrypted.c | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) --- a/security/keys/encrypted-keys/encrypted.c +++ b/security/keys/encrypted-keys/encrypted.c @@ -141,23 +141,22 @@ static int valid_ecryptfs_desc(const cha */ static int valid_master_desc(const char *new_desc, const char *orig_desc) { - if (!memcmp(new_desc, KEY_TRUSTED_PREFIX, KEY_TRUSTED_PREFIX_LEN)) { - if (strlen(new_desc) == KEY_TRUSTED_PREFIX_LEN) - goto out; - if (orig_desc) - if (memcmp(new_desc, orig_desc, KEY_TRUSTED_PREFIX_LEN)) - goto out; - } else if (!memcmp(new_desc, KEY_USER_PREFIX, KEY_USER_PREFIX_LEN)) { - if (strlen(new_desc) == KEY_USER_PREFIX_LEN) - goto out; - if (orig_desc) - if (memcmp(new_desc, orig_desc, KEY_USER_PREFIX_LEN)) - goto out; - } else - goto out; + int prefix_len; + + if (!strncmp(new_desc, KEY_TRUSTED_PREFIX, KEY_TRUSTED_PREFIX_LEN)) + prefix_len = KEY_TRUSTED_PREFIX_LEN; + else if (!strncmp(new_desc, KEY_USER_PREFIX, KEY_USER_PREFIX_LEN)) + prefix_len = KEY_USER_PREFIX_LEN; + else + return -EINVAL; + + if (!new_desc[prefix_len]) + return -EINVAL; + + if (orig_desc && strncmp(new_desc, orig_desc, prefix_len)) + return -EINVAL; + return 0; -out: - return -EINVAL; } /*