Received: by 10.223.176.5 with SMTP id f5csp2908583wra; Mon, 5 Feb 2018 12:03:58 -0800 (PST) X-Google-Smtp-Source: AH8x226LHPR+RyebkA6bKeywXGdIeeG6XXklDfaXzinghIL9PJoA14afc7rhkPRduyfkqHl+H3DT X-Received: by 10.101.73.197 with SMTP id t5mr10531pgs.48.1517861038381; Mon, 05 Feb 2018 12:03:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1517861038; cv=none; d=google.com; s=arc-20160816; b=lVnHq/Z8sran6Jc0yG97baGs0EmbwdxggxBHkL2T1vjpF6tJu9dw+8zUZtpETmr3+w epiOqxzboROL3JVnQmAoyPTXnrXeNjUIUjgO0lTJ9GQjUIwOwOcmCjO77u5IZ4gj/NAt AaPdg0khQSKrwVZGsIgJB5J0QroINnCq3Df4SiG60/RbS7090Fdn7QJHJ9DpEkfYSC2i Hs0t+cRBSt8ynEE00ircCC3E0Y9jSRgoN63RixQe1ijjoDHoWDmfm1hZPJ0/y+tC1i/3 NWs8nLKSm9LeevDsCtWSO0SxPtYoTRzHppy8/a88YWw3Lfh/UZ7b6Z+Ek/ngO5VTo2mv N+Sg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dkim-signature:arc-authentication-results; bh=8ZB65BZPe50/BHY876N4JslTJNqTT5A2Tmwyd0OK2cM=; b=aw7BoMBiCDWPX2oP6OVWJYcDaiIOzjQ5vVJU2yFkxZYxwhWTHUUmPILZXRp2w6Xjpq Rk9wPw24Ld9BE/Ylv2zW8jx6zTOAV9FCj1GebD14LAWs5tNCqRPU8+X+/OxGTVIh9Umi DZh5/AhOsJdXfHy80CYm9ByqWDcGNftQz3TMIIa3IkwPKIUg/J4K4OXBnI142fMI/iff G4rdxZF6wTgaQm7V7RyiYM8CXYFRpSGT+RCgyJSi9lOnp7JF8LnSLvljA8ywVZz6kwmx yG5HavXVw7/X+edCkMNBQ0fYZcLECvDKJI6B2nFB8c/rffvQIXCtuUqaIKioQCxiTOPR Sstw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@android.com header.s=20161025 header.b=HeHkKh85; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=android.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 17si7420419pfw.21.2018.02.05.12.03.42; Mon, 05 Feb 2018 12:03: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; dkim=pass header.i=@android.com header.s=20161025 header.b=HeHkKh85; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=android.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751770AbeBEUDJ (ORCPT + 99 others); Mon, 5 Feb 2018 15:03:09 -0500 Received: from mail-pg0-f68.google.com ([74.125.83.68]:36020 "EHLO mail-pg0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751562AbeBEUDB (ORCPT ); Mon, 5 Feb 2018 15:03:01 -0500 Received: by mail-pg0-f68.google.com with SMTP id x25so6896962pge.3 for ; Mon, 05 Feb 2018 12:03:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=android.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=8ZB65BZPe50/BHY876N4JslTJNqTT5A2Tmwyd0OK2cM=; b=HeHkKh85b00Ggny43DafxZYGG0d9j1Lg5D4G6LV7HCkJf3TCVuwnufo2ZgtvIXGYpr bV1HnbEjVz8rZZwhZRJqLm4t/Dk4+iG17wLfvJ7XnukqrmVcCmMBi9/h4ghp3FSyYKWe GS1TmiookfNUR4OhCQ6OmmFFOk4U+tGXfFOm7NFklyV/ZiCxeeFGWQYB4BScVda3X+dD z9dcTs+oWl7Z91Eye5mWig7hmW7grPfyd5raq81ie9NQGzBTG5Cwug+uTsSkgKTZ036u UUrs8nr549hmwc3T9tB9T+MonPd7NTe6FIt+5fUkte4o6auSqaJkKJODFR/qVwkKNdf9 QYDg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=8ZB65BZPe50/BHY876N4JslTJNqTT5A2Tmwyd0OK2cM=; b=UmuY0j2/3ru+yb5Ep7FY3qlNhZGBMP8599KfOgF34vF8dhf70V4KlQSnKzqfiCHmeS +G0ErdQEWW72ifhes7BlBg9xFcmf2YW2egCYWtWQJG/4gmzpG5s2KS/hdFcotmaDb6iS qXs0yge1IH5KTTGT5fdQvdVBomPLqhaU2lwXHf1lz/Fv5/aGzzjLHDWFbNQPSRKdZ/BW tAXfquex9RcLMhHb5XE8m9Co/9qZyCVjj78FQ+KtwqqJXdOQkCYDm2Xm0wHx+y9J3H4l zWhGavIP5WgxZg2miVX7dYaAwRH6AuG9aMMHYwbEcw8PJ4GMfEjwrwNXFDohXKGCQR1g YMcg== X-Gm-Message-State: AKwxytfnMYRuy6rGzdb26wYshKW0wu4wxzyxm/NyoOTIfeWWoGbxMAzL Pv6kuSTgBpSMsYo+yBeLjpWyXw== X-Received: by 10.98.10.199 with SMTP id 68mr22094660pfk.202.1517860980010; Mon, 05 Feb 2018 12:03:00 -0800 (PST) Received: from jinqian2.mtv.corp.google.com ([2620:0:1000:1601:756c:3217:2035:e37b]) by smtp.gmail.com with ESMTPSA id p25sm4519722pgd.19.2018.02.05.12.02.58 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 05 Feb 2018 12:02:59 -0800 (PST) From: Jin Qian To: Mimi Zohar , David Safford , David Howells , James Morris , "Serge E. Hallyn" , linux-security-module@vger.kernel.org, keyrings@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Eric Biggers , stable@vger.kernel.org, Jin Qian Subject: [PATCH 1/1] KEYS: encrypted: fix buffer overread in valid_master_desc() Date: Mon, 5 Feb 2018 12:02:46 -0800 Message-Id: <20180205200246.12253-1-jinqian@android.com> X-Mailer: git-send-email 2.16.0.rc1.238.g530d649a79-goog Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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: stable@vger.kernel.org Cc: Mimi Zohar Signed-off-by: Eric Biggers Signed-off-by: David Howells Signed-off-by: James Morris Signed-off-by: Jin Qian --- 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 a871159bf03c..ead2fd60244d 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; } /* -- 2.16.0.rc1.238.g530d649a79-goog