From patchwork Thu Apr 19 10:33:21 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kleber Sacilotto de Souza X-Patchwork-Id: 900748 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.ubuntu.com (client-ip=91.189.94.19; helo=huckleberry.canonical.com; envelope-from=kernel-team-bounces@lists.ubuntu.com; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=canonical.com Received: from huckleberry.canonical.com (huckleberry.canonical.com [91.189.94.19]) by ozlabs.org (Postfix) with ESMTP id 40Rb1Q654yz9s1X; Thu, 19 Apr 2018 20:33:30 +1000 (AEST) Received: from localhost ([127.0.0.1] helo=huckleberry.canonical.com) by huckleberry.canonical.com with esmtp (Exim 4.86_2) (envelope-from ) id 1f96sX-00062m-UR; Thu, 19 Apr 2018 10:33:25 +0000 Received: from youngberry.canonical.com ([91.189.89.112]) by huckleberry.canonical.com with esmtps (TLS1.0:DHE_RSA_AES_128_CBC_SHA1:128) (Exim 4.86_2) (envelope-from ) id 1f96sV-00062Y-OM for kernel-team@lists.ubuntu.com; Thu, 19 Apr 2018 10:33:23 +0000 Received: from mail-wr0-f199.google.com ([209.85.128.199]) by youngberry.canonical.com with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.76) (envelope-from ) id 1f96sV-0006Ch-Gb for kernel-team@lists.ubuntu.com; Thu, 19 Apr 2018 10:33:23 +0000 Received: by mail-wr0-f199.google.com with SMTP id k27-v6so4525295wre.23 for ; Thu, 19 Apr 2018 03:33:23 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id; bh=rb8twsvgVNVOgHO2woeaGona/kXhllFSQ+lAkOBF100=; b=KMill1LX6IjvnZAF/ImEMC60v6rMn/vgKA3NuLsPDHSD2jzTv/2E5Zep/odquz9JyZ SqsvjjyZna6eH0qJ3iOH2OKcL4PkdYwegNC5xS6wae8yN8jZXdKcSRDqSM+oWtaqETAL 8PQwod6rR64s70TA1lwmzOkLdcfnJ3J+mGdEqLHwfU3omV+6Wec5S0zHk7q7MKVTKVIl kIchnlVwY913a8EhrCH9ZnhYzS9AMVfPf6FZrqzbru8xOBH7szI4wRfuGRxvrid7rdT7 yAa0v2BLCalcjVi8JIZSdx+2jfzpPMlk8CIpUShqPP92dWJEvdIma/zeWFdCsjcWWX2K Izig== X-Gm-Message-State: ALQs6tCH0rUj7Bx0asjPd6p4sVYfh8tNU1OAXfuK4kYsbcfWklrAS/W4 yeEJk3NhqcKVpLHTPh1QQP6BRIHAUF4qtPBBfAv9t8uZvZapXbNvjHGmpbhAkmtBR7mIfBXoyie u1mA61OXRdGkwEx68ojjhyrI7Qv1tlDhH6LQbVnFuEw== X-Received: by 10.80.153.50 with SMTP id k47mr7850265edb.45.1524134002997; Thu, 19 Apr 2018 03:33:22 -0700 (PDT) X-Google-Smtp-Source: AIpwx49jRyk7VIVud/gilgJLp+r5+kydTB3BTG4MajtSeBd8aGWL0ulEO0Onk7iGPcrp9nyWf7+Few== X-Received: by 10.80.153.50 with SMTP id k47mr7850257edb.45.1524134002818; Thu, 19 Apr 2018 03:33:22 -0700 (PDT) Received: from localhost ([2a02:8109:98c0:1604:55ca:c9ac:ade4:9bbd]) by smtp.gmail.com with ESMTPSA id f10sm2051930edi.8.2018.04.19.03.33.21 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 19 Apr 2018 03:33:22 -0700 (PDT) From: Kleber Sacilotto de Souza To: kernel-team@lists.ubuntu.com Subject: [SRU][Trusty][CVE-2017-13305][PATCH] KEYS: encrypted: fix buffer overread in valid_master_desc() Date: Thu, 19 Apr 2018 12:33:21 +0200 Message-Id: <20180419103321.23397-1-kleber.souza@canonical.com> X-Mailer: git-send-email 2.14.1 X-BeenThere: kernel-team@lists.ubuntu.com X-Mailman-Version: 2.1.20 Precedence: list List-Id: Kernel team discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: kernel-team-bounces@lists.ubuntu.com Sender: "kernel-team" From: Eric Biggers CVE-2017-13305 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 (cherry picked from commit 794b4bc292f5d31739d89c0202c54e7dc9bc3add) Signed-off-by: Kleber Sacilotto de Souza Acked-by: Thadeu Lima de Souza Cascardo Signed-off-by: Juerg Haefliger --- security/keys/encrypted-keys/encrypted.c | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/security/keys/encrypted-keys/encrypted.c b/security/keys/encrypted-keys/encrypted.c index c4c8df4b214d..258bd532cbf3 100644 --- a/security/keys/encrypted-keys/encrypted.c +++ b/security/keys/encrypted-keys/encrypted.c @@ -141,23 +141,22 @@ static int valid_ecryptfs_desc(const char *ecryptfs_desc) */ 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; } /*