Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using output parameter results in invalid data from client memory when not assigned in SP #900

Closed
mathieuk opened this issue Dec 7, 2018 · 5 comments

Comments

@mathieuk
Copy link

mathieuk commented Dec 7, 2018

PHP Driver version or file name

5.3.0

SQL Server version

SQL2016

Client operating system

Linux / CentOS 7

PHP version

PHP 7.2.11

Microsoft ODBC Driver version

unixODBC 2.3.6
ODBC Driver 17 for SQL Server

Problem description

When calling a stored procedure with a string output that remains untouched during the stored procedure, the driver will return memory that it shouldn't (but from a specific location).

Expected behavior and actual behavior

Expected:
When the output parameter is not assigned it should become NULL.

Actual:
Memory from a specific location is returned which may return sensitive information. For instance: I've seen my PHP code (from unrelated bits) returned.

Repro code or steps to reproduce

<?php
$pdo = new PDO(/* ... */);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$pdo->query("CREATE OR ALTER PROCEDURE nullOutputBug(@OUTPUT NVARCHAR(255) OUTPUT) AS SELECT 1,2,3");

$output = null;

$stmt = $pdo->prepare("nullOutputBug @OUTPUT = :output");
$stmt->bindParam('output', $output, PDO::PARAM_STR, 32);

$result = $stmt->execute();


echo "========================\n";
echo "Output variable now contains:\n\n";

echo $output;
echo "\n\n========================\n";

?>

This now consistently returns the following for me:

$ php test.php
========================
Output variable now contains:

xecutetputBug(@OUTPUT NVARCHAR(255) OUTPUT) AS SELECT 1,2,3

========================

Varying the parameter length will change the location it reads from and the length of data returned. As said, I've had chunks of my PHP code returned but also (parts) of my database name.

@mathieuk mathieuk changed the title Using output parameter results invalid data from client memory when not assigned in SP Using output parameter results in invalid data from client memory when not assigned in SP Dec 7, 2018
@yitam
Copy link
Contributor

yitam commented Dec 7, 2018

Thanks @mathieuk yes I can reproduce this.

@yitam yitam added bug and removed bug labels Dec 7, 2018
@yitam
Copy link
Contributor

yitam commented Dec 10, 2018

@mathieuk in your particular example when a result set is waiting to be processed (SELECT 1, 2, 3), the output param is not set until closeCursor is called to finalize the output parameter's value. FYI, this page explains why closing the cursor is required

To prove my point, you can try adding $stmt->closeCursor(); or $stmt->nextRowset() right after the line $result = $stmt->execute();

Alternatively, you can change your stored procedure as follows:

$pdo->query("CREATE OR ALTER PROCEDURE nullOutputBug(@OUTPUT NVARCHAR(255) OUTPUT) AS PRINT 1");

I hope this clarifies things.

@mathieuk
Copy link
Author

mathieuk commented Dec 10, 2018 via email

@yitam
Copy link
Contributor

yitam commented Dec 11, 2018

Yes @mathieuk I agree. Looking into this now

@yitam
Copy link
Contributor

yitam commented Feb 22, 2019

Closing the issue. The PR was merged in the 5.6.0.

@yitam yitam closed this as completed Feb 22, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants