Received: by 2002:ac0:bc90:0:0:0:0:0 with SMTP id a16csp575916img; Fri, 22 Mar 2019 04:22:31 -0700 (PDT) X-Google-Smtp-Source: APXvYqztGcmxarBRukDyAfx40WRZuNfGzWY1RrqwBKd2IMvym+EUCjFVSsNI3cBntGEsZ/9H7ryk X-Received: by 2002:a65:50cc:: with SMTP id s12mr8357434pgp.130.1553253751183; Fri, 22 Mar 2019 04:22:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1553253751; cv=none; d=google.com; s=arc-20160816; b=zUbI6QPFKW98nqHhuxrI0+Y3tNz/cV0A5FfHGYcxpa5B8L286A+3Z8JzArefZY8qAQ VhiGJdlJqkLzUbw8SCshh1gAfLv9ytXFMPhAVD7z9Fvdg+0dLT1DqGEABQJd2SWuLh3Y y+D6vJgYIaltdvbMKQBB6lYmotBHG8ARYasJsyWW0SXtA4PxoIRH+YZTg86wI0fduz2f ma6ZfrnffXMKtVhCBPtntU9eyjNN5ReZexARCM1Dyi33TPMxPV/rPBXMdcdABMLB54Iu +NvIaQdyETysjzU3fgSAjt9GcE4epLngnfXXmPd1+s31AUVrSvCp1yPbOVItPOk8kKVE 5Alg== 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=rRI1qZhtdUCdg1AWzbqKAJGHQhNJ3NEKrQHgRrfAViA=; b=sE16IdMYCbBQLW6+AnGnFtRxpjphuNPkk9095WlQOefKryHkfmuuNFAWz7Zf+FAYF1 npNLMpO7hSRESYr327/D4Pdcz44R5jaTiyUXv0BdNnwiZFO8FPWLe61EsGjVVe7gtXTm VJdyTkn2xRtTzO/DrfjC23UIa6zPcgaG8cPxf/N5r6j/gor3ZFqyX4o4MMGkSjamLw8f EkcYLw0QAPz2c+g8VfohRKYN/LV1IJ8pY1IjICgTo2OdDO0Kb2TEml0CV6uwcNk2LYnC lVVJw+GpUacgMLiK75rhMJpMaCTruf7kxGyPlr/LUPsUe+uxSsas0+zz3Pjnuwe3hLb0 J83A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=X5Mhc90x; 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 70si7218390ple.294.2019.03.22.04.22.13; Fri, 22 Mar 2019 04:22:31 -0700 (PDT) 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=X5Mhc90x; 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 S1728443AbfCVLUq (ORCPT + 99 others); Fri, 22 Mar 2019 07:20:46 -0400 Received: from mail.kernel.org ([198.145.29.99]:47082 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728427AbfCVLUm (ORCPT ); Fri, 22 Mar 2019 07:20:42 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.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 394E92196F; Fri, 22 Mar 2019 11:20:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1553253641; bh=DtmfFXYYkAq17Iy4vcjWWktpZw1yD0gCJggTf0hk7+s=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=X5Mhc90xBki9XPXgJgKy9E7wAbVK00FHeEjW3HoPIO44AbHS3U9dSRKkdMqp40pkt DLfYQa63Jm9eFXiLqRxt1hliWD/b4ycEsPH/tN7hljCiEoQTyOcaXRiXJrSyLsCfaY POu/Q2RwM8M1UltW/1fuwPTDKyv0zgbvWlj0jWEM= 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 3.18 019/134] KEYS: always initialize keyring_index_key::desc_len Date: Fri, 22 Mar 2019 12:13:52 +0100 Message-Id: <20190322111211.314409481@linuxfoundation.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190322111210.465931067@linuxfoundation.org> References: <20190322111210.465931067@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 3.18-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 @@ -628,9 +628,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. */ @@ -888,6 +885,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 @@ -191,8 +191,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);