Received: from makus.postgresql.org (makus.postgresql.org [98.129.198.125]) by mail.postgresql.org (Postfix) with ESMTP id 7736B1259185 for ; Fri, 27 Jan 2012 11:21:17 -0400 (AST) Received: from mail-ee0-f46.google.com ([74.125.83.46]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1Rqnc1-0001sJ-Dr for pgsql-bugs@postgresql.org; Fri, 27 Jan 2012 15:21:17 +0000 Received: by eekc14 with SMTP id c14so595590eek.19 for ; Fri, 27 Jan 2012 07:21:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=date:from:to:cc:subject:message-id:references:mime-version :content-type:content-disposition:in-reply-to; bh=FxUSwzKLlGvSXZ3KKMm9kO+qnNjWB3ElzZtQb15l93k=; b=SNX11SfJO86tlSJI5D1kd34prCm1REKFPdf03UGFaK3zSVECZFMh3BAFAiwIvB8X+e NQ/HfRL29zArrGIh4yFQg5w9/NqCRpDTpR54mY/z/t5Sg8gyIB341rIWxxeZ/5p2xtLL BscnT8V05wrB6j+j9Wv2Py6SlYQzSSi2meVR8= Received: by 10.14.14.161 with SMTP id d33mr2353726eed.82.1327677660464; Fri, 27 Jan 2012 07:21:00 -0800 (PST) Received: from gmail.com (turu7.ee. [217.159.207.194]) by mx.google.com with ESMTPS id y54sm30408734eef.8.2012.01.27.07.20.58 (version=SSLv3 cipher=OTHER); Fri, 27 Jan 2012 07:20:59 -0800 (PST) Date: Fri, 27 Jan 2012 17:20:56 +0200 From: Marko Kreen To: Tom Lane Cc: Stefan Kaltenbrunner , Postgres-Bugs Subject: Re: pgcrypto decrypt_iv() issue Message-ID: <20120127152056.GA4107@gmail.com> References: <4F1DB7BF.60304@kaltenbrunner.cc> <4F1F23E8.80100@kaltenbrunner.cc> <23889.1327646231@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <23889.1327646231@sss.pgh.pa.us> X-Pg-Spam-Score: -2.6 (--) X-Archive-Number: 201201/192 X-Sequence-Number: 32259 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. 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). But I'm bit unclear about fate of /contrib cleanup patches vs. 9.2, so if they won't get in, it's ok to apply quick fixes to current tree, it won't inconvinience me much. -- marko