Received: by 2002:ac0:b08d:0:0:0:0:0 with SMTP id l13csp4899987imc; Mon, 25 Feb 2019 13:15:07 -0800 (PST) X-Google-Smtp-Source: AHgI3IbkKkJxTt0G2SDAqVCg4xSX0Bgp1UPQBCTCHcmJsf8pmpRTON9h6bQNTF/nxRSUNuQbA9k2 X-Received: by 2002:a62:20d1:: with SMTP id m78mr21995118pfj.250.1551129307136; Mon, 25 Feb 2019 13:15:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1551129307; cv=none; d=google.com; s=arc-20160816; b=r8iBwFbK6CQ6pUhGzXLXb+UZHlBgbjpOYR2yJaxZwe0aSwNIgSIKT+x/kyk1hPmoi+ GyiXdnAuA0MKvipfnMmP5kcT9NnIeK0omvMPZR/aPoEgPiQavUnC+lFUV5whbePQaAOQ fhId+rD+1ADYj59vnCCKKk7DUwlAY16wapGPqsZVBbXoE5GsQ7WVOqTNuTL0AlbgQFli I0OZJKbcSlRRRWfAer/UfhHcLOz2voFM7y073kkYxZWlvcalMdn/5NHIX6MQvmFTjoNF tH5is2mfxB0uz3tocwRTILCy5+RBeDGBfFdKr3g7fLdfOwJZ4CSos/WmTvPSms7wSmdx gXmg== 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=JCFfjaButhxiUGrGKMnUYO4ZfsGZZoax+u7SuhXjKkk=; b=xtm0Cg0K89TmYi1Qr2L/qX7gS7LzgvvVe1CwimiuDndTlCR7qQ3LpLWC3H8YxNYR9k nkfJJ6gwTPaKHBnIcGW3CBshQVg5NhH08XzJhMk50QDeozjayyaJVRlVhdFxWdekCUoc Mi2cf014zZn0tRB5shb50jIBTfcuNborNnyYnhSVBfnYjd40Z9whhqpoIg1wGB9ZF4ve ln3pMMW11k8YW5OJeArsClr+KkmO7jJdOUoFfGJ+FCjSaVq7DXWiIOauHtaqWFjtVB/o xbNPLniaH6eAZWw3/9xtoRsFrsn1cx2S3cG9bmWqp0NIIwr0mtKNjtOg24NrvCK0q/0T bOaQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="xrpDhZ/g"; 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 w12si10268575pfn.95.2019.02.25.13.14.52; Mon, 25 Feb 2019 13:15:07 -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="xrpDhZ/g"; 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 S1729128AbfBYVOC (ORCPT + 99 others); Mon, 25 Feb 2019 16:14:02 -0500 Received: from mail.kernel.org ([198.145.29.99]:44770 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729115AbfBYVOB (ORCPT ); Mon, 25 Feb 2019 16:14:01 -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 9C45D2084D; Mon, 25 Feb 2019 21:13:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1551129240; bh=wRRby8rYUQuOhCvQ+bv7/C6AsJJdROSsb3Hx1zNr6CM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=xrpDhZ/gY3h3O0qPiJTIZCHiRA3zEakJtnrl2P0ezQWK7Jx1FkrmhDsLrBgLeHlLY jOLO6P6GtMopnsuNSOdfcvEDg1UZ0AbdqJM6m9/qdC1Pnli3/6w/6ZbiRa9YzFrg4+ KUz8ONATPXw8ov6I0/qnudvHoSfonorll7PZyeK4= 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.9 31/63] KEYS: always initialize keyring_index_key::desc_len Date: Mon, 25 Feb 2019 22:11:31 +0100 Message-Id: <20190225195038.209361516@linuxfoundation.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190225195035.713274200@linuxfoundation.org> References: <20190225195035.713274200@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.9-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 @@ -652,9 +652,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. */ @@ -912,6 +909,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 @@ -186,8 +186,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 = current_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 @@ -544,6 +544,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 @@ -254,7 +254,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);