Received: by 2002:ac0:bc90:0:0:0:0:0 with SMTP id a16csp582020img; Fri, 22 Mar 2019 04:30:43 -0700 (PDT) X-Google-Smtp-Source: APXvYqwn6+BmXsDSN/mHnd/GPRqxi4X7/kLzAbqBv3NN9KhHwdIsyfedYeD+rhwSCrNEFPRS3p8o X-Received: by 2002:a62:1d8c:: with SMTP id d134mr8629830pfd.185.1553254242879; Fri, 22 Mar 2019 04:30:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1553254242; cv=none; d=google.com; s=arc-20160816; b=hK2wJvaAquUuPRAntW2dyo6RBaHjSTT2ieQJwRhFFnSX/5/6KI7ILMbb5NhMTQkBh9 rfXLbfJnrCWkvp3zhWhRGd8q5pEdCC0/tAZU1rveAfX/tYPScrSpr+IYOS/sKdg/5519 ysjjED20P467IJpO7e39jjr46Lm6ChhMrCozKuAwywhYuvw9Kl75K00aMP0O3AN2iLK/ SKjca47agBHzcyeuhvu/i8GmBKXaxk5V78yzxhp8ADKU+hmfEJmLqBYdxwZ4Yb058LQ0 zP0VYld2OWSLCKwj4YM+6HEZUu+UMcGuQ+mpeVT97RK/jdDEl1oQQS/ddqTljse8qch8 OrtQ== 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=vBdUvwr24y2boc8QFcatR7yIwnXV2UScSiA0KDWQsJU=; b=W36mNXlenLgZJFQDIaDD0IqKOUoc4H9Yx/S25C6Zz6ZIrcscV+tSVOiFHPh1WQV5c7 VKyihah+XZfrngx9mxSM1CTMBqjW6w7R9HrLZPB0oMulkpSKPZRECviTdgPKRePLcXGD TBfGLGSkwnL+4hpyO+B10EdQuMzfM+j2HLusDS6KDfy5xMC4QIseh//+5SOM5QN9hvbJ 1o02KJDo5zCnw2uORoi6R+kI9XnruCN/K17ZBsnzVGb0LHNdte8PWRpEIfUfvfUKa3CL NeP3K5WHYH5jzHDhndQQ3mKUoeJZ4U13Px8RZQ5LTkoXSAh/b2tkBKOFuqtK//LNBNCr M7OQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=JbKxm1RU; 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 3si7013870plz.116.2019.03.22.04.30.27; Fri, 22 Mar 2019 04:30:42 -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=JbKxm1RU; 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 S1729780AbfCVL3S (ORCPT + 99 others); Fri, 22 Mar 2019 07:29:18 -0400 Received: from mail.kernel.org ([198.145.29.99]:57096 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729504AbfCVL3O (ORCPT ); Fri, 22 Mar 2019 07:29:14 -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 668F520449; Fri, 22 Mar 2019 11:29:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1553254153; bh=je6lKdgxFF6M/u0La/NRvZ/ZGuzY4Ej0z2oo3s6G0Yk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=JbKxm1RUJ7nyb7d7nkJFKeIjWzlC7Lgpc2ghhW0O5g6PusiU9VEMo6BpECs/A2mmr KVszL8JBa1J8wd21XZqLBLNe0rOV/OIbrayhiFmzhdci0+4a/9HftsGm2QlKt2NWO1 RS0wQsC3miAb6MfjX9u3rL0fzLPQT59SXTeZyyGY= 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.4 022/230] KEYS: always initialize keyring_index_key::desc_len Date: Fri, 22 Mar 2019 12:12:40 +0100 Message-Id: <20190322111238.285513111@linuxfoundation.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190322111236.796964179@linuxfoundation.org> References: <20190322111236.796964179@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.4-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 @@ -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);