Received: by 10.223.176.5 with SMTP id f5csp721213wra; Fri, 9 Feb 2018 06:12:56 -0800 (PST) X-Google-Smtp-Source: AH8x225X3DGQMGM5o0cJ0TFLbaZYLUxoGWzHEcTLC0+xE069o9punO64WEo9B2Mxpp8wSOW293b7 X-Received: by 10.99.51.200 with SMTP id z191mr1526039pgz.120.1518185576146; Fri, 09 Feb 2018 06:12:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1518185576; cv=none; d=google.com; s=arc-20160816; b=P3kjduJPPrx/KdmE9Hv7TH5jJlwwePpddpCxJC35Fq/ul2UZwGmrFVxMxpN4yDP/Yn AlcWw9WVrW3QRAI/jBlTGBBy4p/hSVqYUX/4jch8eK8hFGBlsIcGJgU4Ts11gPma3oZI 6Luq8eSZvHE6bs9KzTGOuZu6YTZ86FhgOdDxHVrs0EYjag7+MuhTrNdvJXCWPca1Pzsh V1aPrgGGHk0vglZ4zwJQ9RWcrdadUuM6BzvzwhE4IseS6vgsIzwFc3htL2FYbDHQC+qd f1quXgqDMQKCJDsYs2TXlzg5vvz/j1UJYY4KCimEA0v8DHoHDeOMJX+0Tah1czFQ27mI 9gaQ== 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=Qr7nwHH8eTsLzx7DxTIlHdKeEjCXr0WnyFlyF15Zcq0=; b=BHKOirddZ104s6KQAaTCHX7sVKU3VOVsjCGZP0OydN6vkETCcaaNzMjccHwm0TxgYh sbfME9FZ9q+v34JmeMDES5+rSrxhP475iTM5qsUSkMNZLhPzh4yM6m4kQzbABsGEBuIc Nb75tpHHGoyVyRCpjhu5fSJC324Tr2SFh0Khq538wv/OLZg5WFbb/GI278VqXNRpyU83 6BTKkDR+JbPE82wBbKKoK5C/HdYJ+6peqSbydgV8qLO/FbjukxpmlN15fc4lvFvDQG7x DkvQ0Usu6EeJW2pCHYxjSGbAs2GnpRwEnwzidCFRgpUn2AG46H6Wv2K9qMXR7SFdSex+ 6w0A== 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 p2-v6si1576720plo.798.2018.02.09.06.12.40; Fri, 09 Feb 2018 06:12:56 -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 S1752761AbeBINly (ORCPT + 99 others); Fri, 9 Feb 2018 08:41:54 -0500 Received: from mail.linuxfoundation.org ([140.211.169.12]:49372 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752703AbeBINlr (ORCPT ); Fri, 9 Feb 2018 08:41:47 -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 84093D2A; Fri, 9 Feb 2018 13:41:46 +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.9 34/92] KEYS: encrypted: fix buffer overread in valid_master_desc() Date: Fri, 9 Feb 2018 14:39:03 +0100 Message-Id: <20180209133933.630472581@linuxfoundation.org> X-Mailer: git-send-email 2.16.1 In-Reply-To: <20180209133931.211869118@linuxfoundation.org> References: <20180209133931.211869118@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 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; } /*