Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1n6NNa-0006JS-0H for pgsql-hackers@arkaria.postgresql.org; Sun, 09 Jan 2022 01:52:18 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1n6NNX-0007qt-AK for pgsql-hackers@arkaria.postgresql.org; Sun, 09 Jan 2022 01:52:15 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1n6NNW-0007qk-PY for pgsql-hackers@lists.postgresql.org; Sun, 09 Jan 2022 01:52:15 +0000 Received: from mail-ot1-x332.google.com ([2607:f8b0:4864:20::332]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1n6NNP-0007o5-6l for pgsql-hackers@postgresql.org; Sun, 09 Jan 2022 01:52:14 +0000 Received: by mail-ot1-x332.google.com with SMTP id n17-20020a9d64d1000000b00579cf677301so11266659otl.8 for ; Sat, 08 Jan 2022 17:52:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=leadboat.com; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=EESBR40czj2Gv35vdJ3iPK2f6P8HFLnjTTDgIqW6tow=; b=XuCPkqIifZnMhHqG0cgfzIuJe5wgGk2pewfKwgGsibdxGclX8dzICXd5foufGh+DiY n3Ad6Z9B4R4tw0QkbEkci/ZnbvcsIXB7WpTkfrl/Q60XmXbdBOlHqq8Ag24wZ90OPoyo a89kbGa/f0av3C8YjvO/euclHYEsgJ74CbJro= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=EESBR40czj2Gv35vdJ3iPK2f6P8HFLnjTTDgIqW6tow=; b=Oqcjzbta/v5+eRiinyxGnMd0PkPMwQFQmiS6ik2mXBFA8B6WgsOSmNtiZkBKjt8J3+ 5tIZ7qIThz6ATdYFZnTaRlCaYUhTFbbUmSvd82R5BzLAr0//ylBLrqvnDzoD/fzR1mr9 KxQmF8HCFiiGUssEzH22sfc0vydspLWJH0H+aH4rtewW/1VvEm5NH6J3yCY73lkR41Se BXud3xd34NARx1czfFGphA0YkibjpdmkVHqXsYMTfbsk/W/886k433KgYxxhUU35PeEz kqujhT2oEcIWvzco5bvQXsxiCGuWJrB2nw9IoxI2/nDiVdbDlEZ6UT6+ElDkyIGRdAQt 5O3A== X-Gm-Message-State: AOAM532ekw7t4Mwa3JTHTxCjb9GYlcGPsgYJlqAET3w8HXJCgAuMBYV3 wR3nWQI2bEAjUM8mwKdoyEarsFbLwizoCFpz X-Google-Smtp-Source: ABdhPJz14yNVPeyw5Z05vUMPex01Y6fSZjmABn7GTs6YskQOYtm3GbJyCiQGWe8YwpeT3SJ6Lv7+iw== X-Received: by 2002:a9d:2233:: with SMTP id o48mr12201874ota.381.1641693124856; Sat, 08 Jan 2022 17:52:04 -0800 (PST) Received: from rfd.leadboat.com ([2600:1702:a20:5750::2e]) by smtp.gmail.com with ESMTPSA id k19sm590336oot.32.2022.01.08.17.52.03 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 08 Jan 2022 17:52:04 -0800 (PST) Date: Sat, 8 Jan 2022 17:52:02 -0800 From: Noah Misch To: Zhihong Yu Cc: PostgreSQL-development Subject: Re: null iv parameter passed to combo_init() Message-ID: <20220109015202.GB283924@rfd.leadboat.com> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.24 (2015-08-30) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Fri, Jan 07, 2022 at 04:32:01PM -0800, Zhihong Yu wrote: > In contrib/pgcrypto/pgcrypto.c : > > err = px_combo_init(c, (uint8 *) VARDATA_ANY(key), klen, NULL, 0); > > Note: NULL is passed as iv. > > When combo_init() is called, > > if (ivlen > ivs) > memcpy(ivbuf, iv, ivs); > else > memcpy(ivbuf, iv, ivlen); > > It seems we need to consider the case of null being passed as iv for > memcpy() because of this: > > /usr/include/string.h:44:28: note: nonnull attribute specified here I agree it's time to fix cases like this, given https://postgr.es/m/flat/20200904023648.GB3426768@rfd.leadboat.com. However, it should be one patch fixing all (or at least many) of them. > --- a/contrib/pgcrypto/px.c > +++ b/contrib/pgcrypto/px.c > @@ -198,10 +198,13 @@ combo_init(PX_Combo *cx, const uint8 *key, unsigned klen, > if (ivs > 0) > { > ivbuf = palloc0(ivs); > - if (ivlen > ivs) > - memcpy(ivbuf, iv, ivs); > - else > - memcpy(ivbuf, iv, ivlen); > + if (iv != NULL) > + { > + if (ivlen > ivs) > + memcpy(ivbuf, iv, ivs); > + else > + memcpy(ivbuf, iv, ivlen); > + } > } If someone were to pass NULL iv with nonzero ivlen, that will silently malfunction. I'd avoid that risk by writing this way: --- a/contrib/pgcrypto/px.c +++ b/contrib/pgcrypto/px.c @@ -202,3 +202,3 @@ combo_init(PX_Combo *cx, const uint8 *key, unsigned klen, memcpy(ivbuf, iv, ivs); - else + else if (ivlen > 0) memcpy(ivbuf, iv, ivlen); That also gives the compiler an additional optimization strategy.