Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp470208rwi; Fri, 14 Oct 2022 04:48:31 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5W85oM7+RqR+Yt2Ox7TcNX511gDYKuhDuf4sUQ1FZSBv1F24cF2UGw4JqLCqj7w7QrRlzI X-Received: by 2002:a17:902:c952:b0:184:ba4f:af39 with SMTP id i18-20020a170902c95200b00184ba4faf39mr4939351pla.58.1665748111305; Fri, 14 Oct 2022 04:48:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1665748111; cv=none; d=google.com; s=arc-20160816; b=zGtmmBnluQPXTwC5HoU3wluo3fKfbndYPgE45I/HarOlI1CgEfIoxEd6IDiNFzT6cc S8EE9w2fOJqWWvPv1G1QX3VYDvH3dehvgBz0/ZkT50nubhuIQGnR752i9HvhjGv0TVue qv9KJ3bTUFs7z7+PynS6KAU0vJgkix7n8ZIQsjZGnX0lwWyCfd0LXYAcJ6twsRpb1paE jSbkxR8Zv3JCJs0XRbD9SFKWQRHIXBx1TWXtfbXcsf6ZJpOCfYGjpcMqzObhb+2VSHKF FEvi2M/xt6Dwg1qitSIR52r6cJi9XY5wKp0DN90piFhe96CvyOytbSWN/hKynYn14P+a Ublg== 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=Tw0Y8kSMqIGMZW8Q97p5+y5pjsFvoM41y2OrydxHGy4=; b=LHdwm+ojOyhhy+kNhQODWlqZ8A/uM9S0fb2PpqfBLTnSkqp+0TGHnbRj0ZyeuBn9g4 x5o8K3AgMeWu8TQ91VOeGHIaHSbNwy9r5d6SXTHpn8knhS4CAAd5A9tzv2kcv+ItZKtj QFgFieQRBPhHMCEoPBriXuzxMtZ/xhQ0sCEzclaum62WgP/kb2ohU/w5K+V+aQlRhA48 tysTYHuWyBh64mOeGMeZGdfAd5i36anEkWrz7JCjaJVz97DIE4tSYnX2vJq22ZnkfSK9 T+3bTReVseIP7XYjELhDD8aMV1qqccCeGBIpzeDIalDK7l4hLFiK1W6Dw4ImgwI5vE/C fkvQ== 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 q93-20020a17090a4fe600b00205ef38b646si2431719pjh.80.2022.10.14.04.48.18; Fri, 14 Oct 2022 04:48:31 -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 S229534AbiJNLjp (ORCPT + 99 others); Fri, 14 Oct 2022 07:39:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55676 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229578AbiJNLjn (ORCPT ); Fri, 14 Oct 2022 07:39:43 -0400 Received: from mail.steuer-voss.de (mail.steuer-voss.de [85.183.69.95]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C5CDE116F; Fri, 14 Oct 2022 04:39:38 -0700 (PDT) X-Virus-Scanned: Debian amavisd-new at mail.steuer-voss.de Received: by mail.steuer-voss.de (Postfix, from userid 1000) id 831DD495; Fri, 14 Oct 2022 13:39:34 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by mail.steuer-voss.de (Postfix) with ESMTP id 815E831D; Fri, 14 Oct 2022 13:39:34 +0200 (CEST) Date: Fri, 14 Oct 2022 13:39:34 +0200 (CEST) From: Nikolaus Voss To: Mimi Zohar cc: David Howells , Jarkko Sakkinen , James Morris , "Serge E. Hallyn" , Yael Tzur , Cyril Hrubis , Petr Vorel , linux-integrity@vger.kernel.org, keyrings@vger.kernel.org, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v4] KEYS: encrypted: fix key instantiation with user-provided data In-Reply-To: <16fe5265c49fcecdf613fe9dd660efe4ae8d452e.camel@linux.ibm.com> Message-ID: <1b621acf-a1f1-ec9b-21f6-d081d69ed74@vosn.de> References: <20221013064308.857011E25@mail.steuer-voss.de> <924a29d81cc7e0d3e2f62f693a0d8fcef97b9779.camel@linux.ibm.com> <16fe5265c49fcecdf613fe9dd660efe4ae8d452e.camel@linux.ibm.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="8323329-2142412134-1665747574=:33507" 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 This message is in MIME format. The first part should be readable text, while the remaining parts are likely unreadable without MIME-aware tools. --8323329-2142412134-1665747574=:33507 Content-Type: text/plain; charset=ISO-8859-15; format=flowed Content-Transfer-Encoding: 8BIT On Fri, 14 Oct 2022, Mimi Zohar wrote: > On Fri, 2022-10-14 at 08:40 +0200, Nikolaus Voss wrote: >> On Thu, 13 Oct 2022, Mimi Zohar wrote: >>> On Thu, 2022-10-13 at 08:39 +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. >>>> >>>> Old keys created from user provided decrypted data saved with "keyctl pipe" >>>> are still valid, however if the key is recreated from decrypted data the >>>> old key must be converted to the correct format. This can be done with a >>>> small shell script, e.g.: >>>> >>>> BROKENKEY=abcdefABCDEF1234567890aaaaaaaaaa >>>> NEWKEY=$(echo -ne $BROKENKEY | xxd -p -c32) >>>> keyctl add user masterkey "$(cat masterkey.bin)" @u >>>> keyctl add encrypted testkey "new user:masterkey 32 $NEWKEY" @u >>>> >>>> It is encouraged to switch to a new key because the effective key size >>>> of the old keys is only half of the specified size. >>> >>> Both the old and new decrypted data size is 32 bytes. Is the above >>> statement necessary, especially since the Documentation example does >>> the equivalent? >> >> The old key has the same byte size but all bytes must be within the >> hex-asc?i range of characters, otherwise it is refused by the kernel. >> So if you wanted a 32 bytes key you get 16 effective bytes for the key. >> In the above example the string size of the $BROKENKEY is 32, while >> the string size of the $NEWKEY is 64. >> >> If you do >> >> $ echo $NEWKEY >> 6162636465664142434445463132333435363738393061616161616161616161 >> >> for the example, the range problem is obvious, so $NEWKEY is still broken. >> That's why it should only be used to recover data which should be >> reencypted with a new key. If you count exactly, the effective key size is >> _slightly_ longer than half of the specified size, but it is still a >> severe security problem. > > So the issue with NEWKEY isn't the "effective key size of the old keys > is only half of the specified size", but that the old key, itself, is > limited to the hex-ascii range of characters. The latter resulting in the former. If for BROKENKEY 32 bytes were specified, a brute force attacker knowing the key properties would only need to try at most 2^(16*8) keys, as if the key was only 16 bytes long. This is what I mean with "effective size" in contrast to the key's byte size which is 32 in my example. The security issue is a result of the combination of limiting the input range to hex-ascii and using memcpy() instead of hex2bin(). It could have been fixed either by allowing binary input or using hex2bin() (and doubling the ascii input key length). I chose the latter. Niko --8323329-2142412134-1665747574=:33507--