Received: from makus.postgresql.org (makus.postgresql.org [98.129.198.125]) by mail.postgresql.org (Postfix) with ESMTP id D68281259185 for ; Fri, 27 Jan 2012 13:35:02 -0400 (AST) Received: from madness.at ([217.196.146.217] helo=cronos.madness.at) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1RqphS-0004KG-Rz for pgsql-bugs@postgresql.org; Fri, 27 Jan 2012 17:35:02 +0000 Received: from gatekeeper.kaltenbrunner.cc ([83.215.243.9] helo=[192.168.8.22]) by cronos.madness.at with esmtpsa (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.72) (envelope-from ) id 1RqphA-0007w7-Cy; Fri, 27 Jan 2012 18:34:42 +0100 Message-ID: <4F22E028.8040708@kaltenbrunner.cc> Date: Fri, 27 Jan 2012 18:34:32 +0100 From: Stefan Kaltenbrunner User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.24) Gecko/20111120 Icedove/3.1.16 MIME-Version: 1.0 To: Marko Kreen CC: Tom Lane , Postgres-Bugs Subject: Re: pgcrypto decrypt_iv() issue References: <4F1DB7BF.60304@kaltenbrunner.cc> <4F1F23E8.80100@kaltenbrunner.cc> <23889.1327646231@sss.pgh.pa.us> <20120127152056.GA4107@gmail.com> In-Reply-To: <20120127152056.GA4107@gmail.com> X-Enigmail-Version: 1.1.2 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -1.9 (-) X-Archive-Number: 201201/194 X-Sequence-Number: 32261 On 01/27/2012 04:20 PM, Marko Kreen wrote: > On Fri, Jan 27, 2012 at 01:37:11AM -0500, Tom Lane wrote: >> Stefan Kaltenbrunner writes: >>> from some looking at the code in pgcrypto.c it seems to me that the >>> coding pattern in most functions there only checks for errors from the >>> corresponding initialization function, in the case of say decrypt_iv() >>> that means only the IV and the key are actually "validated" because that >>> is what the init function sees(it never sees that data!), if the actual >>> decrypt call fails (because the data is maybe a bit weird^broken) it >>> will happily ignore that and return random data. >> >> Yeah. In pg_decrypt() we have >> >> err = px_combo_init(c, (uint8 *) VARDATA(key), klen, NULL, 0); >> if (!err) >> err = px_combo_decrypt(c, (uint8 *) VARDATA(data), dlen, >> (uint8 *) VARDATA(res), &rlen); >> >> but in pg_decrypt_iv() it's just >> >> err = px_combo_init(c, (uint8 *) VARDATA(key), klen, >> (uint8 *) VARDATA(iv), ivlen); >> if (!err) >> px_combo_decrypt(c, (uint8 *) VARDATA(data), dlen, >> (uint8 *) VARDATA(res), &rlen); >> >> It looks to me like the result of px_combo_decrypt should be assigned to >> "err" here. If I make that change, the test case you provide is >> rejected: >> >> ERROR: decrypt_iv error: Data not a multiple of block size >> >> but the module's regression tests all still pass, indicating that this >> sort of case isn't tested. >> >> pg_encrypt_iv() has the identical usage error with respect to >> px_combo_encrypt. >> >> Marko, does this look right to you? > > Yeah, it should be fixed. But note that "random data" is part of > decrypt() spec - the validation it can do is a joke. > > Its more important to do proper checks in encrypt() to avoid invalid > stored data, but there the recommended modes (CBC, CFB) can work > with any length data, so even there the impact is low. I agree - but in my case the input to those functions is actually coming from external untrusted systems - so if the data is (completely) invalid really want to get a proper error message instead of random memory content. > > pgcrypto.c is easily fixable and internal.c has proper checks. > But openssl.c does not. And I have a bigger openssl.c cleanup > pending. So I would prefer to add missing checks to cleaned-up > openssl.c and post them together (soonish). hmm so openssl.c has similiar "issues" but you only want to fix them together with a cleaned larger patch? sounds a bit of a problem for a backpatch... Stefan