Skip to content

4.042 improperly encoding blobs when sql_type is SQL_UNKNOWN_TYPE #117

Open
@cthulhuology

Description

@cthulhuology

The utf8 encoding changes have resulted in a regression that has unexpected side effects.
Consider the following table:

create table foo (foo longblob);

and script:

use DBI;

my $dbh = DBI->connect('DBI:mysql:database=test','desktop','',{ mysql_enable_utf8 => 1 })
or die $DBI::errstr;

my $sth = $dbh->prepare('INSERT INTO foo (foo) values (?)');
$sth->execute("I18N Web Testing æøå");

my $rth = $dbh->prepare('SELECT foo FROM foo');
$rth->execute();
while (@Row = $rth->fetchrow_array) {
print $row[0],"\n";
}

Now the longblob mysql type should be considered a SQL_BLOB, but because the exec call doesn't call bind_param with the attrib set to SQL_BLOB, the default value of 0 is used SQL_UNKNOWN_TYPE and passed to bind_param. If you sv_dump at 939 in dbdimp.c you'll see:

SV = PV(0x162d950) at 0x15f8630
REFCNT = 1
FLAGS = (POK,IsCOW,READONLY,PROTECT,pPOK)
PV = 0x15dc200 "I18N Web Testing \303\246\303\270\303\245"\0
CUR = 23
LEN = 25
COW_REFCNT = 0

And sql_type is the default value of 0.

the output of the scrip will print the value stored in the database:

I18N Web Testing æøå`

because it is double encoding the characters, and not preserving the blob value because the
sql_type_is_binary check returns false because the SQL_UNKNOWN_TYPE value of sql_type.

One example of widely used code that exhibit this behavior is Apache::Session (Apache::Session::MySQL), which calls bind_param explicitly but never passes the SQL_BLOB at and of the call sites. Since Apache::Session is using a Storable (which is binary data stored in a blob) the additional UTF8 encoding can result in data corruption which can cause perl to crash with an OOM error when materialize is invoked. This is a potential security threat.

Metadata

Metadata

Assignees

No one assigned

    Labels

    utf8Unicode and UTF-8 handling

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions