Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp560497rwb; Wed, 28 Sep 2022 06:25:03 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7+0ddMjXhUMRlfy8CyNEdxcyKH2rmc0FMIwaRU2Fi5he3reB3askPux0p9pr5Iw72BDO5N X-Received: by 2002:a17:902:f803:b0:17a:45e:7cf4 with SMTP id ix3-20020a170902f80300b0017a045e7cf4mr2370126plb.44.1664371502748; Wed, 28 Sep 2022 06:25:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664371502; cv=none; d=google.com; s=arc-20160816; b=nS3IyOYBqcsyDaACjH1+suJ/Xdg2PjSwhs4jNdIOAboioqNzdGW1Mn1egenY5i6yXo ggFGw2sVa2A8LMXWV7w3DOtUR6jUhsy89uUj0qG9g4lBIkgYQ7lNdxt/P3ys6Xx0IeGZ MFnq1e8AUgHVEn4Nxhpi1S4yiIbV1RnUXjxo8/KUVtq4GJixt331zRHClFmo5MURm/h0 FDcveswQb9JmGiVBWnGMifZOrObqm22o1IoNbOQ6dQeu1wQrMteTV9uMnryTC7Aky1y0 c+If8BsXDQHFhNnWdtgNNdDClTSm2ZNcXI9m43NGZg6gQWEdLSgp9yT4utX+CyGI1hK3 KqFA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:references:message-id:in-reply-to :subject:cc:to:from:date; bh=xI0Y4Lw8iLeIaHIE3E/GVEWK4sLAI/wP+SV8CX2uKoM=; b=X6vUNVh+7IAbEswUUvC1UVmFXlLBckTj48dlg3lX6dnsJmlEJLxyxvACTq6XxN6YFe krP4tE9qyM5IYL2wU9JDPbzOTPyihPUOl6NHwRbRgz3DVlqwR9RPrxkxEuHDNqpMWHrm yvCozpAcs085psZZYFIjj2aJ8ycqewRr8DAA6BuLzNAorxksWJ3wm5P537kL6g2yWSe4 PiYBxl3lrLdBOiGM9/P6GyrBZvVeD6xQoOzZQIWgFX0Mz334Y3M1jiIHQ/vA0N6vbVvZ o8d5JJKw2MFY+1C/VmQqYt6KgociqjVhCzeVz+JvRmNWGiJImwAbW6lShA43D+f6/v9o tD8A== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id d16-20020a056a0024d000b0053641d594b2si2961600pfv.40.2022.09.28.06.24.50; Wed, 28 Sep 2022 06:25:02 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233648AbiI1NEA (ORCPT + 99 others); Wed, 28 Sep 2022 09:04:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34160 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234123AbiI1NDx (ORCPT ); Wed, 28 Sep 2022 09:03:53 -0400 Received: from mail.steuer-voss.de (mail.steuer-voss.de [85.183.69.95]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C3DBFA0278; Wed, 28 Sep 2022 06:03:51 -0700 (PDT) X-Virus-Scanned: Debian amavisd-new at mail.steuer-voss.de Received: by mail.steuer-voss.de (Postfix, from userid 1000) id D1393921; Wed, 28 Sep 2022 15:03:46 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by mail.steuer-voss.de (Postfix) with ESMTP id CE12B8E3; Wed, 28 Sep 2022 15:03:46 +0200 (CEST) Date: Wed, 28 Sep 2022 15:03:46 +0200 (CEST) From: Nikolaus Voss To: Jarkko Sakkinen cc: Mimi Zohar , David Howells , Yael Tzur , James Morris , "Serge E. Hallyn" , linux-integrity@vger.kernel.org, keyrings@vger.kernel.org, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] KEYS: encrypted: fix key instantiation with user-provided data In-Reply-To: Message-ID: References: <20220919072317.E41421357@mail.steuer-voss.de> <372b91d-5ee6-ae24-2279-0dc7621489c@vosn.de> MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII; format=flowed X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, 21 Sep 2022, Jarkko Sakkinen wrote: > On Tue, Sep 20, 2022 at 09:58:56AM +0200, Nikolaus Voss wrote: >> On Tue, 20 Sep 2022, Jarkko Sakkinen wrote: >>> On Fri, Sep 16, 2022 at 07:45:29AM +0200, Nikolaus Voss wrote: >>>> Commit cd3bc044af48 ("KEYS: encrypted: Instantiate key with user-provided >>>> decrypted data") added key instantiation with user provided decrypted data. >>>> The user data is hex-ascii-encoded but was just memcpy'ed to the binary buffer. >>>> Fix this to use hex2bin instead. >>>> >>>> Fixes: cd3bc044af48 ("KEYS: encrypted: Instantiate key with user-provided decrypted data") >>>> Cc: stable >>>> Signed-off-by: Nikolaus Voss >>>> --- >>>> security/keys/encrypted-keys/encrypted.c | 6 +++--- >>>> 1 file changed, 3 insertions(+), 3 deletions(-) >>>> >>>> diff --git a/security/keys/encrypted-keys/encrypted.c b/security/keys/encrypted-keys/encrypted.c >>>> index e05cfc2e49ae..1e313982af02 100644 >>>> --- a/security/keys/encrypted-keys/encrypted.c >>>> +++ b/security/keys/encrypted-keys/encrypted.c >>>> @@ -627,7 +627,7 @@ static struct encrypted_key_payload *encrypted_key_alloc(struct key *key, >>>> pr_err("encrypted key: instantiation of keys using provided decrypted data is disabled since CONFIG_USER_DECRYPTED_DATA is set to false\n"); >>>> return ERR_PTR(-EINVAL); >>>> } >>>> - if (strlen(decrypted_data) != decrypted_datalen) { >>>> + if (strlen(decrypted_data) != decrypted_datalen * 2) { >>> >>> This looks wrong. What does cap decrypted_data, and why strnlen() >>> is not used? >> >> This is a plausibility check to ensure the user-specified key length >> (decrypted_datalen) matches the length of the user specified key. strnlen() >> would not add any extra security here, the data has already been copied. > > I'd prefer unconditional use of strnlen() because it always > gives you at least some guarantees over deducing why strlen() > is fine in a particular code block. I agree. Unfortunately, there is no blob size available in encrypted_key_alloc(), so this would mean changing function signatures and code to get this downstream. This would be well worth a patch on its own. > > >>> >>>> pr_err("encrypted key: decrypted data provided does not match decrypted data length provided\n"); >>> >>> Using pr_err() is probably wrong here and has different prefix >>> than elsewhere in the file (also most of other uses of pr_err() >>> are wrong apparently). Nothing bad is really happening. >> >> It actually _is_ an error preventing key instatiation. User space keyctl >> cannot be verbose about the reason why instantiation failed so it makes >> sense to be verbose in kernel space. To me, this seems consistent with other >> occurrences of pr_err() in this file, maybe I misunderstood you? > > Then it should be pr_info(), or even pr_debug(), given that it is not a > kernel issue. > >> Btw, this patch changes neither string length checking nor log levels. > > I understand this. It has been my own mistake to ack that pr_err(). > > However, does not fully apply to strlen() part. Since you are > changing that line anyway, it'd be better to replace strlen() > with strnlen(). This e.g. protects the code block changes in > the context where it is called. I'd love to do it if it was simple. Niko