Received: by 2002:ac0:b08d:0:0:0:0:0 with SMTP id l13csp4919201imc; Mon, 25 Feb 2019 13:38:10 -0800 (PST) X-Google-Smtp-Source: AHgI3Ib5PG9tV7Ut9xwNIQ/V9QpRIvSlofrE9dqZm1ax0Rm+A7fYL18NRx0qOR2hNxDM4UzcSSpX X-Received: by 2002:a17:902:264:: with SMTP id 91mr22633832plc.108.1551130690445; Mon, 25 Feb 2019 13:38:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1551130690; cv=none; d=google.com; s=arc-20160816; b=OfZa0LdaSQ17M4d9rBKzbxGDhYxnRxHEub8eVi63x/9I3D0HEXdXBbS6xZazafLdED XdGuhMNl34aNbHgKbgBQd/lp/IgAy1OaHBbCeoJLoAtvYbCQE1U7xV9F1WR8Q+9PHqVU Em4dHTYxX5lVeasVAhfq0eH5OZGvNmEvuVJmWsRoj0TLAi1hZoyTyQl3eBD0hOTXgPYl izhcymc8tvIkf6OMAcr4N+HyIOEk64a5jF7NEbEB5W4Xh9qw2wtxhbYP5YTTr+UAlCAZ BcUlkL7aQfOiOD7uDCaMBzrHuwNw6Bi1ccBchszltwmNkJwSWkNSJK76B/aLvluNtd0m MLFA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=uIxXTOXVnUREcsoMWIeXgd/9XXjl3SIpjsY8LZ1ghgo=; b=fjDbLDG4ELL7LXaHAL1dgmpHw/+Qqhtmab7mOaHKoTaDJQ9oTXxsp5tDPfYEzVHGVl ftKQ/PK77v2a8BsAeusv0oaex3laehd8qQMll7+hqW+/Hf5NItDUADo/ffoMakn6rNKu thPeuEemQVPalJWMIlCLGe655L6i4++oWQXi/hkgex+hZF+m3btARlso/Endw+iJnktH nmwJlq14qZwWo7zjQRl3aiGEjYkKWz0yeSLPjlfJuiWBqBR/V8VsCZ3ph/GP2LQWcjra DG5TF4nXYBgyqdzqQG186m0nydSkbuDCnl2EK2k/bmUhoIA0Hme1PfhkPXeJn5TxGJ5/ UO8A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=l1Sc0n+M; 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 a143si11407480pfd.24.2019.02.25.13.37.54; Mon, 25 Feb 2019 13:38:10 -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=@kernel.org header.s=default header.b=l1Sc0n+M; 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 S2387449AbfBYVhX (ORCPT + 99 others); Mon, 25 Feb 2019 16:37:23 -0500 Received: from mail.kernel.org ([198.145.29.99]:44482 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387437AbfBYVhV (ORCPT ); Mon, 25 Feb 2019 16:37:21 -0500 Received: from localhost (5356596B.cm-6-7b.dynamic.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 404C420578; Mon, 25 Feb 2019 21:37:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1551130640; bh=t12T4BRSDWQiLtqzp/zZCowNkEoAT030Bn8V1mrT5mA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=l1Sc0n+M/p2FgBiouCzvd4nVaJfIMAGa1qtwVcaA2xrTlJOuDLfmnuGU3YiNaO4Hs xYV7u7zaoG1kj7zYotAFXOd++bFgEityqXNMBWHYgF8ZPffaEKomuGAY2bs3BtlS1K jVM9pH4onQKYCSHrNSxaUY1TzoIbgWsb7LYyD/4o= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, syzbot+ec24e95ea483de0a24da@syzkaller.appspotmail.com, Eric Biggers , David Howells , James Morris Subject: [PATCH 4.20 148/183] KEYS: always initialize keyring_index_key::desc_len Date: Mon, 25 Feb 2019 22:12:01 +0100 Message-Id: <20190225195119.950878052@linuxfoundation.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190225195054.748060397@linuxfoundation.org> References: <20190225195054.748060397@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review X-Patchwork-Hint: ignore MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.20-stable review patch. If anyone has any objections, please let me know. ------------------ From: Eric Biggers commit ede0fa98a900e657d1fcd80b50920efc896c1a4c upstream. syzbot hit the 'BUG_ON(index_key->desc_len == 0);' in __key_link_begin() called from construct_alloc_key() during sys_request_key(), because the length of the key description was never calculated. The problem is that we rely on ->desc_len being initialized by search_process_keyrings(), specifically by search_nested_keyrings(). But, if the process isn't subscribed to any keyrings that never happens. Fix it by always initializing keyring_index_key::desc_len as soon as the description is set, like we already do in some places. The following program reproduces the BUG_ON() when it's run as root and no session keyring has been installed. If it doesn't work, try removing pam_keyinit.so from /etc/pam.d/login and rebooting. #include #include #include int main(void) { int id = add_key("keyring", "syz", NULL, 0, KEY_SPEC_USER_KEYRING); keyctl_setperm(id, KEY_OTH_WRITE); setreuid(5000, 5000); request_key("user", "desc", "", id); } Reported-by: syzbot+ec24e95ea483de0a24da@syzkaller.appspotmail.com Fixes: b2a4df200d57 ("KEYS: Expand the capacity of a keyring") Signed-off-by: Eric Biggers Signed-off-by: David Howells Cc: stable@vger.kernel.org Signed-off-by: James Morris Signed-off-by: Greg Kroah-Hartman --- security/keys/keyring.c | 4 +--- security/keys/proc.c | 3 +-- security/keys/request_key.c | 1 + security/keys/request_key_auth.c | 2 +- 4 files changed, 4 insertions(+), 6 deletions(-) --- a/security/keys/keyring.c +++ b/security/keys/keyring.c @@ -661,9 +661,6 @@ static bool search_nested_keyrings(struc BUG_ON((ctx->flags & STATE_CHECKS) == 0 || (ctx->flags & STATE_CHECKS) == STATE_CHECKS); - if (ctx->index_key.description) - ctx->index_key.desc_len = strlen(ctx->index_key.description); - /* Check to see if this top-level keyring is what we are looking for * and whether it is valid or not. */ @@ -914,6 +911,7 @@ key_ref_t keyring_search(key_ref_t keyri struct keyring_search_context ctx = { .index_key.type = type, .index_key.description = description, + .index_key.desc_len = strlen(description), .cred = current_cred(), .match_data.cmp = key_default_cmp, .match_data.raw_data = description, --- a/security/keys/proc.c +++ b/security/keys/proc.c @@ -166,8 +166,7 @@ static int proc_keys_show(struct seq_fil int rc; struct keyring_search_context ctx = { - .index_key.type = key->type, - .index_key.description = key->description, + .index_key = key->index_key, .cred = m->file->f_cred, .match_data.cmp = lookup_user_key_possessed, .match_data.raw_data = key, --- a/security/keys/request_key.c +++ b/security/keys/request_key.c @@ -545,6 +545,7 @@ struct key *request_key_and_link(struct struct keyring_search_context ctx = { .index_key.type = type, .index_key.description = description, + .index_key.desc_len = strlen(description), .cred = current_cred(), .match_data.cmp = key_default_cmp, .match_data.raw_data = description, --- a/security/keys/request_key_auth.c +++ b/security/keys/request_key_auth.c @@ -246,7 +246,7 @@ struct key *key_get_instantiation_authke struct key *authkey; key_ref_t authkey_ref; - sprintf(description, "%x", target_id); + ctx.index_key.desc_len = sprintf(description, "%x", target_id); authkey_ref = search_process_keyrings(&ctx);