Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1YBASP-00072M-UL for pgsql-odbc@arkaria.postgresql.org; Tue, 13 Jan 2015 23:01:06 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1YBASP-0005Op-6z for pgsql-odbc@arkaria.postgresql.org; Tue, 13 Jan 2015 23:01:05 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1YBASN-0005Og-Br for pgsql-odbc@postgresql.org; Tue, 13 Jan 2015 23:01:03 +0000 Received: from smtp-outbound-1.vmware.com ([208.91.2.12]) by magus.postgresql.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1YBASH-0001s4-1h for pgsql-odbc@postgresql.org; Tue, 13 Jan 2015 23:01:01 +0000 Received: from sc9-mailhost3.vmware.com (sc9-mailhost3.vmware.com [10.113.161.73]) by smtp-outbound-1.vmware.com (Postfix) with ESMTP id 2CDE728707; Tue, 13 Jan 2015 15:00:51 -0800 (PST) Received: from EX13-CAS-009.vmware.com (EX13-CAS-009.vmware.com [10.113.191.61]) by sc9-mailhost3.vmware.com (Postfix) with ESMTP id 2389F419DB; Tue, 13 Jan 2015 15:00:51 -0800 (PST) Received: from [192.168.1.90] (10.113.160.246) by EX13-MBX-024.vmware.com (10.113.191.44) with Microsoft SMTP Server (TLS) id 15.0.913.22; Tue, 13 Jan 2015 15:00:36 -0800 Message-ID: <54B5A38B.4030306@vmware.com> Date: Wed, 14 Jan 2015 01:00:27 +0200 From: Heikki Linnakangas Organization: VMware User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Icedove/31.3.0 MIME-Version: 1.0 To: "Faith, Jeremy" , "pgsql-odbc@postgresql.org" Subject: Re: SQL_CURSOR_TYPE prepare execute issue References: <55BAADA01D8C504993894EAEA7517CF80D07AD@003FCH1MPN2-002.003f.mgd2.msft.net> In-Reply-To: <55BAADA01D8C504993894EAEA7517CF80D07AD@003FCH1MPN2-002.003f.mgd2.msft.net> Content-Type: text/plain; charset="windows-1252"; format=flowed Content-Transfer-Encoding: 7bit X-Originating-IP: [10.113.160.246] X-ClientProxiedBy: EX13-CAS-013.vmware.com (10.113.191.65) To EX13-MBX-024.vmware.com (10.113.191.44) X-Pg-Spam-Score: -6.9 (------) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-odbc Precedence: bulk Sender: pgsql-odbc-owner@postgresql.org On 01/13/2015 07:57 PM, Faith, Jeremy wrote: > Hi, > > With PosgreSQL 9.3.5 and pgsqlodbc 9.03.03 > > I have found an issue with setting cursor type to anything other than the default(SQL_CURSOR_FORWARD_ONLY) and the checking done on prepare/execute parameters. > > In particular with regards to protection from SQL injection attacks. > > Say for example there is a table > create table t1(c1 integer); > > When following is run > SQLSMALLINT dec_digits; > SQLUINTEGER col_size; > SQLHSTMT stmt; > char buf[100]; > > //... not showing con setup > SQLAllocHandle(SQL_HANDLE_STMT,con->sqlcon,&stmt); > > SQLSetStmtOption(stmt,SQL_CURSOR_TYPE,SQL_CURSOR_STATIC); > SQLPrepare(stmt,(SQLCHAR *)"select * from t1 where c1=?",SQL_NTS); > > sprintf(buf,"%s","1 and zzz='dummy'"); //i.e. injection attempt > //this could be much worse e.g. "1; drop table personnel;" > len=strlen(buf); > col_size=10; > dec_digits=-1; > SQLBindParameter(stmt,1,SQL_PARAM_INPUT,SQL_C_CHAR,SQL_INTEGER,col_size, > dec_digits,buf,0,&len); > > SQLExecute(stmt); > > The SQLExecute will fail(as it should) but with the cursor set to SQL_CURSOR_STATIC then the error is:- > stmt native_err=7 [42703]ERROR: column "zzz" does not exist; > > Whereas if the cursor type is not set(and is thus the default SQL_CURSOR_FORWARD_ONLY) then the error is much better(and safer):- > stmt native_err=7 [22P02]ERROR: invalid input syntax for integer: "1 and zzz='dummy' "; > > Also if in the odbc.ini file > UseServerSidePrepare = 0 > for the connection then error is always: column "zzz" does not exist, regardless of the cursor type. Hmm. The driver blindly assumes that if parameter is bound as SQL_INTEGER or SQL_SMALLINT, it doesn't require quoting. But clearly that's not true, if the passed parameter string is not a valid integer. > I found this problem when testing with PHP odbc_prepare/odbc_execute > functions as the odbc_prepare function sets the cursor type to > SQL_CURSOR_STATIC(by default though it can be overridden by setting > odbc.default_cursortype = SQL_CURSOR_FORWARD_ONLY in php.ini). PHP > needs UseServerSidePrepare=1 as it also calls SQLDescribeParam() > which fails if UseServerSidePrepare=0. > > The question is why does changing the cursor type change the way the > parameters are bound? Can anything be done to fix this? I just pushed a fix and a regression test to the git repository. Thanks for the report! - Heikki -- Sent via pgsql-odbc mailing list (pgsql-odbc@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-odbc